ComfyUI Node

Pt Sub

Subtract one tensor from another, element-wise

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

Pt Sub subtracts one tensor from another, element by element. It's arithmetic's quiet workhorse: residual math in training pipelines, computing deltas and differences, removing a baseline or mean from data, comparing two tensors. There's nothing flashy here - it's the - operator, wrapped as a node - but it's one of the nodes you'll reach for every single time you're building a data pipeline, which is exactly the point of a pack like ComfyUI-Pt-Wrapper.

What it actually does

It's a wrapper around torch.sub(tens_a, tens_b). Two TENSOR inputs, one TENSOR out, same shape as the inputs. It computes tens_a - tens_b element-wise.

The useful flexibility: the two inputs don't need identical shapes. This is element-wise subtraction with broadcasting, so tens_b can be a smaller-rank tensor - a scalar, a vector, a per-channel mean - and it stretches to match tens_a. That's the pattern you'll use constantly: subtract a mean vector from every row, remove a baseline from a signal, compute a per-element residual. tens_a is what you're subtracting from; tens_b is what gets subtracted.

Only the two inputs:

  • tens_a - the tensor being subtracted from.
  • tens_b - the tensor to subtract.

Output is a single TENSOR.

How to install it

It's part of the ComfyUI-Pt-Wrapper pack. ComfyUI Manager: search ComfyUI-Pt-Wrapper, install, restart. Or:

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

Restart after. The pack's requirements.txt is the heavy training stack (transformers, datasets, accelerate, peft, gensim, sentencepiece, pandas, scikit-learn, scipy) - slow first install, possible version conflicts with other nodes pinning different transformers versions.

Gotchas

Broadcasting is both the feature and the failure mode. If your two tensors have shapes that can't be broadcast together - say (2, 3, 4) minus (5, 6) - you get a runtime error that may not immediately say "broadcast." If subtraction fails with a shape error, check whether you meant tens_a and tens_b the right way around and whether the smaller tensor's dimensions line up with the target.

Also remember the order is not symmetric. a - b and b - a are different tensors, and if you wire the inputs backward you'll silently get negative deltas everywhere. Easy to do when you're dragging wires quickly; hard to spot if you're not checking signs. When in doubt, branch a copy into Pt Show Text and eyeball it.

CategoryData Analysis

Inputs (2)

NameTypeDefaultDescription
tens_aTENSOR
tens_bTENSOR

Outputs (1)

NameTypeDescription
TENSORTENSOR