Nodes/ComfyDL/Truncate/Pad
ComfyUI Node

Truncate/Pad

Force every sequence to the same length, the boring way that just works

By Cynthia-lxx·Created 2 months ago·Updated 2 days ago· 6
Truncate/Pad
  • sequence
  • padded
num_steps64
padding_token0

Text comes in variable lengths. Tensors don't. Truncate/Pad is the bridge: it takes a token-index sequence and either cuts it down or pads it out to exactly num_steps tokens, producing a fixed-length long tensor you can batch with its siblings. It's the d2l truncate_pad utility as a node, and it's the piece that makes the pack's RNN language-model demos possible at all.

The behavior is exactly as boring as it should be. Feed it a sequence longer than num_steps and it keeps the first num_steps tokens and throws the rest away. Feed it a shorter one and it appends padding_token copies until it reaches the target length. Either way you get a fixed-length tensor back. Boring is good here - the whole reason this node exists is that sequence padding is a place where getting fancy produces silent bugs, and this does the textbook thing.

Inputs

  • sequence - an optional cdlTensor of token indices. This is where your encoded sentence goes (typically the output of Vocab Encode).
  • num_steps - the target length, default 64. Every sequence through this node comes out exactly this long.
  • padding_token - the index used to fill short sequences, default 0. Conventionally that's your <pad> index from the vocab - with the default reserved tokens in Vocab Build (<pad>,<bos>,<eos>), <pad> gets sorted in and is often index 0, but don't assume; check your vocab.

Output

One output, padded, a cdlTensor of dtype long - integer token indices, shape (num_steps,). Wire it into your RNN/GRU node or batch it with others. Because the output is long dtype, it won't silently convert to floats like some other pack nodes do - the indices stay integer, which is what the models expect.

Installing it

Part of ComfyDL. ComfyUI Manager, search "ComfyDL", or:

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

Restart. Only matplotlib to install, no model downloads.

Gotchas

Two things to keep in mind. First, the sequence input is optional - leave it unwired and the node returns a fully-padded tensor of all padding_tokens, which is handy for testing but not a sentence. Second, the token-index vs token-string distinction: this node expects encoded indices (a cdlTensor), not the comma-joined strings that Tokenize emits. Encode first, then pad. Also note truncation is from the front - it keeps the beginning of the sequence, not the end. For the RNN language-model demos in this pack that's the right call, but if you ever feed in reversed sentences for a translation task, you'll want to flip your thinking. Young niche pack, no community threads to lean on, but this is textbook logic - if it misbehaves, it's almost always a wrong padding_token index rather than a bug in the node.

Categoryd2l/TorchOps

Inputs (3)

NameTypeDefaultDescription
num_stepsINT641–10000
padding_tokenINT00–100000
sequenceoptTENSOR

Outputs (1)

NameTypeDescription
paddedTENSOR