Nodes/ComfyDL/RNN (from scratch)
ComfyUI Node

RNN (from scratch)

No hidden torch layer, just weights

By Cynthia-lxx·Created 2 months ago·Updated about 16 hours ago· 6
RNN (from scratch)
    • model
    num_inputs32
    num_hiddens64
    sigma0.010

    If you ever tried to learn what an RNN actually does and bounced off a wall of equations, this node is the antidote. It comes from ComfyDL, a small educational pack that wraps the "Dive into Deep Learning" (d2l) textbook code into ComfyUI nodes. The "(from scratch)" in its name is doing real work: this model's parameters aren't hidden inside torch.nn.RNN. They're three tensors - W_xh, W_hh, b_h - created by hand, and you can dump them with Model Info and stare at them.

    That's the whole point of the node. It's a builder, not a black box. You're not running an image pipeline here; you're learning (or re-learning) the mechanics of sequence modeling by wiring the pieces yourself.

    What it builds and how it ticks

    CdlRNNScratch returns a model (a cdlModel in ComfyUI's type system) whose forward pass is the textbook recurrence: at every timestep it computes

    state = tanh(X @ W_xh + state @ W_hh + b_h)

    in a plain Python loop. No cuDNN magic, no fused kernels - just matrix multiplies and a tanh, one step at a time. The input has to be time-major, shaped (num_steps, batch_size, num_inputs), which trips people up because most of the PyTorch world thinks batch-first. It's a deliberate d2l convention, and it means if you feed this thing through ComfyDL's Model Forward node with a batch-first tensor, the shapes will complain.

    Only three knobs matter, and they map straight to the chapter in the book:

    • num_inputs - the width of each input vector. When you're building a character language model this is the vocab size, not the number of characters in your corpus. The demo wires the vocab size from Vocab Build straight into here.
    • num_hiddens - the hidden state width. Bigger = more capacity, more parameters, slower.
    • sigma - the standard deviation of the random init. The weights start as randn * sigma, and d2l's classic value is 0.01. You almost never need to touch it; it exists so you can reproduce the textbook's experiments.

    Where it fits in a real workflow

    On its own, this model isn't useful yet - it's a bare recurrence with no output head. The intended pattern, straight from the pack's shipped "RNN Language Model Demo" workflow, is:

    1. Tokenize some text, build a vocab, and feed its size into num_inputs.
    2. Wire this model into the RNN Language Model (from scratch) node, which adds the projection from hidden state to vocab-sized logits.
    3. Send that to RNN LM Predict and get generated text.

    You can also just wire the model output into Model Info / Model Params to read off parameter counts and layer names - genuinely handy for checking your mental model of how big these things get.

    The honest catch

    This is a from-scratch teaching RNN. For anything you actually want to run at scale, use the pack's sibling RNN (high-level) node or the GRU node, which call PyTorch's real, optimized nn.RNN/nn.GRU. "From scratch" is slower, harder to shape, and exists to be readable - that's its job. Reach for this one when you want to see the weights, not when you want to ship.

    One trap worth naming: this node is not a language model. If you wire its output into RNN LM Predict directly, you'll get a TypeError, because the predict method lives on the wrapper, not the raw RNN. Wrap it first.

    Installing ComfyDL

    ComfyDL is one repo, one install, for every node in this pack:

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

    Then restart ComfyUI and look under ComfyDL/NLP Models in the node menu. The clone directory must be named ComfyDL (that's the module ComfyUI imports). Requirements are featherlight - just matplotlib, IPython and matplotlib-inline - because torch and torchvision already ship with ComfyUI. Note that ComfyUI Manager may not find this pack by search yet, since the repo's registry metadata (PublisherId) is still blank in pyproject.toml; the git clone above is the reliable path.

    There's essentially no community discussion of ComfyDL anywhere yet - you're early. If you hit a wall, the reference documentation lives in the repo's FUNCTIONS.md and the underlying theory is free in the d2l book it adapts.

    CategoryComfyDL/NLP Models

    Inputs (3)

    NameTypeDefaultDescription
    num_inputsINT321–100000
    num_hiddensINT641–4096
    sigmaFLOAT0.0100.0001–1

    Outputs (1)

    NameTypeDescription
    modelcdlModel