ComfyUI Node

Pt Sum

Add up a tensor along any dimension, or all of them

By HowToSD·Created about a year ago·Updated about a year ago· 7
Pt Sum
  • tens
  • TENSOR
dim
keepdimfalse

Pt Sum adds up a tensor. That's it - and it's more useful than it sounds, because "add up" has options. You can total the entire tensor into a single number, or sum along a specific dimension and keep the rest, which is the operation behind everything from batch totals to row-wise and column-wise aggregations. In the ComfyUI-Pt-Wrapper world, where you're building data pipelines and training graphs out of raw tensor math, sum is the reduction node you'll lean on constantly.

What it actually does

It's a wrapper around torch.sum(tens, dim=..., keepdim=...). One TENSOR in, one TENSOR out. The inputs:

  • tens - the tensor to reduce.
  • dim - a multiline string controlling which dimensions to sum over. The pack's convention: type 0, [0], or (1, 2) - an integer, a list, or a tuple. Leave it empty to sum the whole tensor down to a single value, which is the default behavior people forget they have.
  • keepdim - a boolean (default false). When true, the reduced dimension stays around as size 1 instead of vanishing.

The dim field is where the power is. Sum over dim 0 of a (3, 4) tensor and you get a (4,) vector - column totals. Sum over (1, 2) of a (2, 3, 4) tensor and you get a (2,) vector, one total per batch item. Sum over nothing and you get one scalar. Being able to choose the reduction axes is what separates this from a novelty node.

And keepdim matters for exactly one practical reason: downstream math. If you sum over dim 0 but keepdim is false, the result has a lower rank than your input, and broadcasting it back against the original tensor gets awkward. Turn keepdim on when you plan to divide or subtract the sum against the thing you summed.

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 conflicts with other nodes.

Gotchas

The dim string has the pack's usual parsing rule: it must be valid Python syntax - 0, [0], or (1, 2), brackets included. And here's the subtle one: the pack's other reduction nodes parse an empty dim string as "no reduction," but for sum, an empty dim means sum everything. That's fine once you know it, but it means leaving the field blank and getting a scalar can surprise you if you expected a no-op.

Overflow is worth a thought too: summing big tensors in float16 or bfloat16 can accumulate precision loss or overflow to inf. For safety-critical totals, float32 or higher is the boring, correct choice.

CategoryData Analysis

Inputs (3)

NameTypeDefaultDescription
tensTENSOR
dimSTRING
keepdimBOOLEANfalse

Outputs (1)

NameTypeDescription
TENSORTENSOR