ComfyUI Node

Pt Gather

Pulling specific elements out of a tensor, without losing your mind

By HowToSD·Created about a year ago·Updated about a year ago· 7
Pt Gather
  • tens
  • TENSOR
dim0
index

gather is one of those PyTorch operations that everyone half-remembers and nobody explains well, so a node that wraps it is genuinely useful. Pt Gather grabs elements from a tensor at positions given by an index tensor, one position per output slot. If you've ever wanted to "look up" a value at coordinates rather than slice a contiguous block, this is the tool. In the Pt-Wrapper pack it sits in the "Indexing and Slicing" family, next to Pt Index Select and Pt Masked Select - gather is the precise, coordinate-based cousin of those.

How it works

The output tensor has the same shape as the index tensor, and every element of the output comes from input[index] along the dimension you chose. Say your tensor is [[10, 20, 30, 40, 50], [100, 200, 300, 400, 500]] and your index is [[0, 4, 3], [4, 3, 0]] with dim = 1. Row 0 looks up column 0 → 10, column 4 → 50, column 3 → 40; row 1 does the same with its own columns. The result is [[10, 50, 40], [500, 400, 100]]. The index values are positions along dim, so with dim = 0 they're row indices instead of column indices.

The fiddly bit everyone forgets: the index tensor must have the same number of dimensions as the input, and its values must be in range for the dimension you're gathering along. That's where new users hit the wall - a 1D index against a 2D tensor throws immediately.

The inputs

  • tens - the input tensor you're pulling from.
  • dim - an integer, default 0, range −10 to 10, the axis you gather along. Negative values count from the end.
  • index - a multiline text field containing a Python list of lists, e.g. [[0,4,3],[4,3,0]]. It's parsed with ast.literal_eval, so it must be valid list syntax.

The output is a TENSOR shaped like the index tensor, full of the gathered values.

Installing the pack

Pt Gather ships in HowToSD/ComfyUI-Pt-Wrapper, the author's no-code PyTorch pack spun off from ComfyUI-Data-Analysis. Easiest path is ComfyUI Manager: search "ComfyUI-Pt-Wrapper" and install. Or:

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

Restart ComfyUI. The pack's requirements.txt brings in transformers, datasets, peft, gensim, scikit-learn and more, so the first install is heavier than the node looks. No model downloads are needed for the tensor-op nodes.

Common issues

  • "index ... is out of bounds" - an index value exceeds the size of dim. If your tensor has 5 columns and an index contains a 5, that's the error.
  • Shape mismatch - index and input must have equal rank. A 2D index needs a 2D input.
  • Wrong dim - gathering along dim = 0 when you meant dim = 1 won't error, it'll just silently return something meaningless. Print the result with Pt Show Text first, or feed the output into Pt Show Size to sanity-check.

A note on the pack itself: it's a small, single-author educational project with close to zero presence in r/comfyui or r/StableDiffusion - I checked, there's basically no discussion. That means the author's node reference docs and example workflows are your best teacher. It's a real limitation, not a dig; just set expectations.

CategoryData Analysis

Inputs (3)

NameTypeDefaultDescription
tensTENSOR
dimINT0-10–10
indexSTRING

Outputs (1)

NameTypeDescription
TENSORTENSOR