NNT Define Positional Encoding
How a transformer knows where it is
- LAYER_STACK
- LIST
Here's the dirty secret of attention: swap the order of two words and a plain attention layer can't tell the difference. It treats the sequence as a bag of positions. NNT Define Positional Encoding is the node that fixes that - it adds positional information to your layer stack so the model actually knows where each token sits. In the Neural Network Toolkit's transformer family, this is the quiet little node that makes everything else meaningful.
What it actually does
It appends a PositionalEncoding entry to the LAYER_STACK list that NntCompileModel compiles into a model, carrying four encoding strategies:
sinusoidal- the classic "Attention Is All You Need" scheme: fixed sine and cosine waves of different frequencies, one per embedding dimension. No learned parameters, works for sequence lengths it never saw in training. This is the default and the one to start with.learned- a learnable lookup table of position embeddings, trained alongside the model. Flexible, but only reliable up to the sequence length it was trained on.rotary(RoPE) - rotates the query/key vectors by an angle proportional to position. The current hotness; most modern models (including most you've downloaded) use a variant of this.alibi- instead of adding anything to the embeddings, it adds a distance-based bias directly to the attention scores. Very long-context friendly.
Inputs that matter
encoding_type- pick one of the four above.sinusoidalto learn,rotaryto meet the technique the modern models actually use.d_model- the embedding width; the encodings are shaped to match it. Keep it consistent with the attention/transformer layers it pairs with.max_seq_length- the longest sequence you'll feed it. Forsinusoidalit just caps how far the pattern goes; forlearnedit's the hard limit.dropout- applied right after the encodings are added; 0.1 is a reasonable default.learnable- whether to make the encoding parameters trainable. Off forsinusoidal(it doesn't need it), on if you want the model to adapt the pattern.normalize- scales the encodings so their values stay tame; leaveTrueunless you have a reason.
The honest gotcha
By now you know the drill with this pack: it's a self-described work in progress, and the transformer section is its most experimental part. These nodes produce clean definitions that are great for learning - but the current compile path doesn't guarantee every transformer entry materializes in the compiled model yet. Use NntCompileModel's "Only create script" mode to check what actually builds, and treat the transformer nodes as teaching tools first, production stack second (or third). Pair this node with NntDefineTransformerEncoderLayer and you can assemble a legitimately educational "mini GPT-style" stack by hand.
Installing NNT
Part of inventorado/ComfyUI_NNT. ComfyUI Manager (search "ComfyUI Neural Network Toolkit") is easiest:
cd ComfyUI/custom_nodes
git clone https://github.com/inventorado/ComfyUI_NNT.git
cd ComfyUI_NNT
pip install -r requirements.txt
Restart ComfyUI after. The requirements pull a full scientific stack - torch, scikit-learn, pandas, transformers, shap, seaborn - so budget a real install. The pack's example workflows also want ComfyUI-Jjk-Nodes for their text displays; Manager's "Install Missing Custom Nodes" handles it.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| d_model | INT | 51264–2048 | — |
| max_seq_length | INT | 51216–2048 | — |
| dropout | FLOAT | 0.10–0.9 | — |
| encoding_type | COMBO | sinusoidal | 4 options: sinusoidal, learned, rotary, alibi |
| learnable | COMBO | False | 2 options: True, False |
| normalize | COMBO | True | 2 options: True, False |
| LAYER_STACKopt | LIST | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| LIST | LIST | — |