NNT Define Transformer Encoder Layer
The node that powers everything since 2017
- LAYER_STACK
- LIST
Almost every model you've ever downloaded for image or text generation is built from layers like the one NNT Define Transformer Encoder Layer defines. This node adds a full torch.nn.TransformerEncoderLayer to your stack: multi-head self-attention, then a feedforward network, with residual connections and layer norm wrapping both. If you want to actually feel what a transformer block is - not just read about it - this is the node that lets you build one by dragging and dropping.
What it actually does
Like every define node in this pack, it appends a layer description dict to the LAYER_STACK list that NntCompileModel compiles into a model. The attention part lets every position in the sequence look at every other position (that's the "self-attention" that replaced recurrence), the feedforward part is a plain two-layer MLP applied per position, and the whole thing is wrapped in residuals so gradients survive deep stacks. Stack several of these and you've got the encoder half of the "attention is all you need" architecture.
Inputs that matter
d_model- the embedding width; every token's representation is this many numbers. This is the big capacity knob. Keep it divisible bynhead- the default 512 / 8 heads works because 512 is a multiple of 8.nhead- how many attention heads. Each head learns to attend over a different subspace. 8 is the standard starting point.dim_feedforward- the width of the inner MLP layer, typically 4×d_model(2048 for d_model 512). This is where most of the layer's parameters live.activation-relu,gelu,silu, ortanhfor the feedforward part.geluis the modern default in actual models;reluis the classic.norm_first- whether to normalize before the sublayers (Pre-LN, what modern models do) or after (Post-LN, the original paper). If you're stacking many layers and it won't train, try Pre-LN.
Then dropout (0.1 is a reasonable start) and batch_first - keep True so your tensors are [batch, seq, d_model].
The honest gotcha
This pack is honest that it's a work in progress, and the transformer section is the bleeding edge of that. The layer nodes produce clean, teachable definitions, but the current compile path in NntCompileModel builds the classic conv/dense/pool blocks directly and doesn't guarantee every transformer entry materializes in the compiled model yet. Before building a big transformer stack, use the compile node's "Only create script" mode to see what actually gets constructed - and lean on the README's example workflows for what's known-good today. If your goal is learning what a transformer encoder is, the node is still worth playing with even in its current state.
Installing NNT
Part of inventorado/ComfyUI_NNT. Install via ComfyUI Manager (search "ComfyUI Neural Network Toolkit") or:
cd ComfyUI/custom_nodes
git clone https://github.com/inventorado/ComfyUI_NNT.git
cd ComfyUI_NNT
pip install -r requirements.txt
Then restart ComfyUI. The requirements pull a full scientific stack - torch, scikit-learn, pandas, transformers, shap - so give the first install time. The pack's example workflows need ComfyUI-Jjk-Nodes for text display; Manager's "Install Missing Custom Nodes" covers it.
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| d_model | INT | 51264–2048 | — |
| nhead | INT | 81–32 | — |
| dim_feedforward | INT | 2048128–8192 | — |
| dropout | FLOAT | 0.10–0.9 | — |
| activation | COMBO | relu | 4 options: relu, gelu, silu, tanh |
| batch_first | COMBO | True | 2 options: True, False |
| norm_first | COMBO | False | 2 options: True, False |
| LAYER_STACKopt | LIST | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| LIST | LIST | — |