Ptn Embedding
Turning token IDs into dense vectors
- PTMODEL
Ptn Embedding is the embedding layer node in the HowToSD/ComfyUI-Pt-Wrapper pack, wrapping nn.Embedding. It takes integer token IDs - the output of a tokenizer - and looks each one up in a table of learned vectors. token_id 42 comes out as a fixed-size dense vector of floats. This is the first layer of basically every text model in this pack, and the thing that makes words learnable by a neural network in the first place.
Why you'd reach for it
Neural networks do arithmetic on numbers, not words. Before a model can process text, each token has to become a vector, and an embedding layer is the trainable version of that mapping: as training proceeds, tokens that behave similarly end up with similar vectors. In this pack's text-classification workflows (IMDB sentiment, and the RNN/Transformer classifier nodes), the pipeline is tokenizer → Ptn Embedding → recurrent or attention layers → classifier. You'd also grab it as a standalone building block when hand-assembling a text model with Ptn Chained Model. If you've used token embeddings for SD prompts, the concept is identical - this is the trainable look-up table.
How it works
nn.Embedding(num_embeddings, embedding_dim) builds a table of num_embeddings rows, each embedding_dim wide. Feeding it a tensor of IDs returns the corresponding rows, so a batch of token-ID sequences becomes a (batch, seq, embedding_dim) tensor. The padding_idx input is the subtle one: set it to a non-negative token ID (say 0, if your tokenizer reserves 0 for padding) and that row gets zeroed and excluded from gradient updates, so padding tokens never learn nonsense embeddings. Default is -1, which disables the behavior. Output is a PTMODEL to chain or train.
The inputs
- num_embeddings (default 10000) - vocabulary size. Must be ≥ the largest token ID your tokenizer can emit, or lookups go out of bounds.
- embedding_dim (default 100) - the size of each vector. Bigger = more capacity, more parameters.
- padding_idx (default -1) - token ID treated as padding (frozen, zeroed). Set to your padding token or leave -1.
Output: PTMODEL.
Installing the pack
In the "Training" category. Install via ComfyUI Manager (search "ComfyUI-Pt-Wrapper") or:
cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper
Restart ComfyUI. Install pulls in transformers, datasets, peft, scikit-learn, gensim and more; no model downloads at install.
Common issues
- Out-of-bounds IDs - a token ID ≥
num_embeddingsthrows at forward time. If your tokenizer's vocab is bigger than the embedding table, raisenum_embeddings. - Padding mismatch - if you set
padding_idxto 0 but your tokenizer pads with a different ID, the padding tokens still learn. Make the two agree. - ID dtype - input must be integer IDs, not floats. Tokenizer nodes in this pack output ints; if you fed a float tensor, cast it.
- Thin community - single-author educational pack, nearly invisible on r/comfyui. The repo's tokenizer and text-classification docs are your real support.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| num_embeddings | INT | 100001–1000000 | — |
| embedding_dim | INT | 1001–1000000 | — |
| padding_idx | INT | -1-1–1000000 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| PTMODEL | PTMODEL | — |