Nodes/ComfyUI Neural Network Toolkit NNT /NNT Define Positional Encoding
ComfyUI Node

NNT Define Positional Encoding

How a transformer knows where it is

By inventorado·Created 2 years ago·Updated 2 years ago· 69
NNT Define Positional Encoding
  • LAYER_STACK
  • LIST
d_model512
max_seq_length512
dropout0.1
encoding_typesinusoidal
learnableFalse
normalizeTrue

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. sinusoidal to learn, rotary to 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. For sinusoidal it just caps how far the pattern goes; for learned it'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 for sinusoidal (it doesn't need it), on if you want the model to adapt the pattern.
  • normalize - scales the encodings so their values stay tame; leave True unless 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.

CategoryNNT Neural Network Toolkit/Transformers

Inputs (7)

NameTypeDefaultDescription
d_modelINT51264–2048
max_seq_lengthINT51216–2048
dropoutFLOAT0.10–0.9
encoding_typeCOMBOsinusoidal4 options: sinusoidal, learned, rotary, alibi
learnableCOMBOFalse2 options: True, False
normalizeCOMBOTrue2 options: True, False
LAYER_STACKoptLIST

Outputs (1)

NameTypeDescription
LISTLIST