Nodes/ComfyDL/RNN Language Model (from scratch)
ComfyUI Node

RNN Language Model (from scratch)

RNN Language Model (from scratch)

By Cynthia-lxx·Created 2 months ago·Updated about 16 hours ago· 6
RNN Language Model (from scratch)
  • rnn
  • model
vocab_size32
lr0.010

Some things are worth building from scratch once, even if you'd never ship them. That's the whole philosophy of the "from scratch" nodes in ComfyDL, and CdlRNNLMScratch is the purest example in the language-model corner: it takes an RNN you built and adds a language-model output layer where the weights are explicit nn.Parameters and the token encoding is a literal one-hot step - no hidden nn.Linear doing the work for you.

You reach for this node when you want to actually understand how a language model predicts the next token, rather than just trusting that it does. Its high-level sibling (CdlRNNLM) uses a lazy linear head and looks almost identical from the outside; the difference is that this version shows its work.

How it works

The wrapper expects a from-scratch RNN - the kind where you built W_xh and W_hh by hand. It adds two explicit parameters: W_hq, mapping num_hiddens to vocab_size, and a bias b_q. On each forward pass it:

  1. One-hot encodes your token indices into (num_steps, batch, vocab_size) tensors - the vocab is genuinely, explicitly the feature dimension here, which is why the RNN's num_inputs must equal vocab_size.
  2. Pushes the one-hot tokens through your RNN to get a hidden state per step.
  3. Applies hidden · W_hq + b_q at every step to produce logits over the vocabulary.

The output layer weights are shared across time steps, and because they're plain parameters you can inspect them after training - which is the whole appeal. It also carries a lr for training and inherits a predict() method, so once built it plugs straight into ComfyDL's RNN LM Predict node for text generation.

Inputs and outputs that matter

  • rnn - a cdlModel RNN built from scratch (the RNN (from scratch) node), with num_inputs equal to vocab_size.
  • vocab_size (default 32) - vocabulary size; this sets both the one-hot width and the output layer's width.
  • lr (default 0.01) - learning rate carried for training.

The model output takes a token-index tensor (batch, num_steps) and returns logits (num_steps, batch, vocab_size). Pair the whole thing with ComfyDL's vocabulary node (CdlVocabBuild) and the RNN LM Predict node to see it write text.

Installing ComfyDL

cd ComfyUI/custom_nodes
git clone https://github.com/Cynthia-lxx/ComfyDL ./ComfyDL
pip install -r ./ComfyDL/requirements.txt

Restart ComfyUI. The pack's requirements are light (matplotlib, IPython, matplotlib-inline) and there are no downloads for this node. ComfyUI Manager may not list this young educational pack - the clone route always works.

Gotchas

Match the pairings or nothing runs: this scratch LM wants the from-scratch RNN (which exposes the .sigma used to size its parameters), and it wants vocab_size to agree with that RNN's num_inputs. Mix in a high-level nn.RNN and the internals won't line up. One more honesty note: one-hot encoding over a big vocabulary is memory-hungry - for a 10k-token word-level vocab you'll feel it, which is exactly why real systems moved to learned embeddings (and why this node is for learning, not production).

CategoryComfyDL/NLP Models

Inputs (3)

NameTypeDefaultDescription
rnncdlModel
vocab_sizeINT322–100000
lrFLOAT0.0100.0001–1

Outputs (1)

NameTypeDescription
modelcdlModel