ComfyUI Node

Pt Remainder

The modulo operator, finally, as a node

By HowToSD·Created about a year ago·Updated about a year ago· 7
Pt Remainder
  • tens_a
  • tens_b
  • TENSOR

Pt Remainder is the % operator from Python and PyTorch, wrapped up as a ComfyUI node. If you're working with tensors in ComfyUI-Pt-Wrapper, you will eventually want modulo - wrapping indices around the length of a sequence, making a value oscillate between 0 and some bound, checking divisibility in a training loop. It's a small node, but it's one of those operations you reach for constantly once you realize it's there.

What it actually does

It takes two TENSOR inputs, tens_a and tens_b, and computes the element-wise remainder of tens_a / tens_b using PyTorch's torch.remainder. That's an important detail: it's not the truncating % you might remember from C. torch.remainder gives the floored-modulo result, which means the sign of the output follows the divisor (tens_b), not the dividend. For the common case of wrapping things into [0, n) - index arithmetic, periodic patterns, hash-style bucketing - that's exactly the behavior you want.

The two inputs don't have to be the same shape. This is element-wise with broadcasting, so tens_b can be a scalar-shaped or lower-rank tensor and it'll stretch across tens_a. The output is a single TENSOR of whatever shape results, ready to wire into the next math or display node.

There are only two inputs, both required, both TENSOR:

  • tens_a - the dividend.
  • tens_b - the divisor.

How to install it

This is one node in the ComfyUI-Pt-Wrapper pack - install the pack, get the node. ComfyUI Manager is the easy route: search ComfyUI-Pt-Wrapper in the Custom Nodes Manager, install, restart. Or clone it:

cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper

Then restart ComfyUI. The pack's requirements.txt is a heavy one (transformers, datasets, accelerate, peft, gensim, sentencepiece, pandas, scikit-learn, scipy, seaborn, matplotlib), so the first install takes a while and can collide with other nodes pinning different versions of transformers. Worth knowing before you commit to the pack for a single modulo node.

Gotchas

Division by zero is the obvious trap. torch.remainder(x, 0) gives you NaN or inf values rather than a clean crash - which is arguably worse, because it doesn't stop the graph and you only find out later when everything downstream is garbage. Check your tens_b isn't zero or near-zero, especially if it comes from somewhere dynamic.

The other thing to remember: sign behavior. Because this is floored modulo, torch.remainder(-1, 5) is 4, not -1. If you're porting a formula that assumed C-style truncating modulo, the results will look subtly wrong until you adjust.

CategoryData Analysis

Inputs (2)

NameTypeDefaultDescription
tens_aTENSOR
tens_bTENSOR

Outputs (1)

NameTypeDescription
TENSORTENSOR