Nodes/ComfyDL/Init Seq2Seq Weights
ComfyUI Node

Init Seq2Seq Weights

Xavier weight init in one node

By Cynthia-lxx·Created 2 months ago·Updated about 16 hours ago· 6
Init Seq2Seq Weights
  • model
  • model

Neural networks don't train themselves into good solutions from arbitrary starting weights - a bad initialization can stall or blow up training before it ever gets going. ComfyDL's CdlInitSeq2Seq is the fix, applied as a node: it runs Xavier (Glorot) uniform initialization across a sequence-to-sequence model's linear and GRU layers. You build your encoder, you run this over it, and the weights start in the sweet spot that keeps activations and gradients from either vanishing or exploding.

It's a small node that does one job, and it's a genuine best practice rather than a nicety. The d2l book (which ComfyDL ports into nodes) initializes the seq2seq encoder's weights precisely because the models are sensitive to it.

How it works

Under the hood it calls model.apply(init_seq2seq), which walks every submodule of the model and applies the initializer where it applies. The logic is short and targeted:

  • For nn.Linear layers - the embedding-to-GRU projections and output heads - weights get xavier_uniform_.
  • For nn.GRU modules it iterates the GRU's flat parameter names and Xavier-initializes every parameter whose name contains "weight" (gate and hidden weights alike), leaving biases alone.

Everything else is left untouched. The choice of Xavier uniform matters because it scales the initial weight range by the number of inputs and outputs of each layer, which keeps the signal variance roughly constant as it flows through - exactly what deep seq2seq stacks need.

Inputs and outputs that matter

  • model - a cdlModel (typically a CdlSeq2SeqEncoder you built, or any hand-assembled module containing Linear/GRU layers).

The output is the same model, mutated in place and passed through - so it's a pass-through filter you drop onto the model wire between building and training. Because the node returns the identical instance, you can also stack it anywhere in a model-building chain without breaking downstream connections. There are no weights to download and nothing to configure: wire it in and it does its thing.

Where it fits

The intended flow in ComfyDL is: build a CdlSeq2SeqEncoder → pipe the model through CdlInitSeq2Seq → forward-pass or train it via the Model Utils nodes. If you're experimenting with machine-translation-style workflows and your loss is stuck at random-guess level for suspiciously long, check that you actually ran this - the default PyTorch initializations are fine for many models, but seq2seq GRU stacks are the case where the d2l recipes call for explicit Xavier init.

Installing ComfyDL

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

Then restart ComfyUI. ComfyDL's requirements are minimal (matplotlib, IPython, matplotlib-inline) and this node needs no model downloads. If ComfyUI Manager doesn't surface "ComfyDL" (young pack, may not be in the registry), the clone command above is the dependable install.

Gotchas

In-place mutation is the behavior to know about: this node modifies the model you hand it rather than returning a copy, so if you built one encoder and wanted to compare "Xavier-init vs. default" you'd need two separate builds (or ComfyDL's model-clone utility) - initializing the same instance twice just re-initializes it. And don't expect it to touch convolutional or embedding layers; it's deliberately scoped to Linear and GRU, which is all the seq2seq recipe needs.

CategoryComfyDL/NLP Models

Inputs (1)

NameTypeDefaultDescription
modelcdlModel

Outputs (1)

NameTypeDescription
modelcdlModel