Nodes/ComfyUI-Pt-Wrapper/Ptn Layer Norm
ComfyUI Node

Ptn Layer Norm

The stabilizer for transformer and image models

By HowToSD·Created about a year ago·Updated about a year ago· 7
Ptn Layer Norm
    • PTMODEL
    normalized_shape[Specify shape here]
    elementwise_affinetrue
    biastrue

    LayerNorm is one of those boring layers that quietly makes deep models train at all, and this node hands it to you as a drop-in graph block. PtnLayerNorm wraps nn.LayerNorm: it normalizes activations to zero mean and unit variance over a shape you choose, then (optionally) applies a learned scale and shift. In the pack's transformer-from-scratch workflow it sits right after the multi-head attention and feedforward blocks, which is exactly where you'll use it - after attention, before the residual add, that's the standard encoder layout.

    How it works

    You give it a normalized_shape and it normalizes over the last dimensions of your tensor. The author's docstring has the two cases you'll actually meet:

    • Text, shape [8, 1024, 768] (batch, seq, hidden): [768] normalizes per token across the hidden dim - the standard transformer choice. [1024, 768] would normalize per sample across the whole sequence.
    • Images, shape [8, 4, 256, 256]: [256, 256] normalizes within each channel over spatial dims; [4, 256, 256] normalizes per sample across channels and space.

    With elementwise_affine on, each normalized element gets a learnable gamma * x + beta; that's what lets the model undo the normalization if it wants to. Note this is computed from the current batch's statistics - there's no running-mean mode, unlike BatchNorm.

    The inputs that matter

    • normalized_shape (default "[Specify shape here]") - a string holding a Python list, like "[768]" or "[256, 256]". The default is a placeholder; you must replace it before use. It should match the trailing dimensions of your input.
    • elementwise_affine (default True) - adds the learned weight and bias. Keep it on for trainable layers; turn it off for pure normalization.
    • bias (default True) - the learned bias term. Only has an effect when elementwise_affine is on.

    One PTMODEL out, ready to chain into a residual block or feedforward stack.

    How you'd use it

    In the transformer-from-scratch workflow, LayerNorm nodes wrap the attention and feedforward sub-models (via the residual-connection nodes), and the [256, 256]-style shapes show up if you ever normalize image feature maps. It's also handy for stabilizing your own hand-built nets when training gets wobbly.

    Installing

    Same as the whole pack. ComfyUI Manager → search "Pt-Wrapper", or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper
    

    Restart ComfyUI; the pack's requirements install on first launch.

    Where people get burned

    The placeholder default is the trap - leave normalized_shape as the literal [Specify shape here] and you'll get a parse error. The other common mistake is putting the wrong trailing shape in: normalized_shape must match the tensor's last axes, so if your tensor is [8, 1024, 768] and you write [768, 1024], it breaks. Shape errors here are loud, though, which beats the silent training degradation most layers give you.

    CategoryTraining

    Inputs (3)

    NameTypeDefaultDescription
    normalized_shapeSTRING[Specify shape here]
    elementwise_affineBOOLEANtrue
    biasBOOLEANtrue

    Outputs (1)

    NameTypeDescription
    PTMODELPTMODEL