ComfyUI Node

SliceTensor

Take the first N rows of a tensor when you only need a chunk

By TashaSkyUp·Created about a year ago·Updated about a year ago· 1
SliceTensor
  • input_data
  • TORCH_TENSOR
start0
end5
step1

Sometimes you don't want the whole tensor - you want the first five samples, or every other row, or a quick subset to sanity-check a workflow without chewing through 60,000 MNIST images. SliceTensor does exactly that: a plain Python tensor[start:end:step] on the first dimension, exposed as three text inputs. It's the "trim the batch" node, and it's as simple as it sounds.

It's part of EternalKernel PyTorch Nodes (TashaSkyUp), the raw-PyTorch-in-ComfyUI pack. Where ReshapeTensor fixes shape problems, SliceTensor fixes size problems - trimming a big features tensor down to a bite-sized test set before you wire it into training.

How it works

The implementation is a single slice: input_data[start:end:step], applied along dimension 0 (the batch/rows axis). Start, end, and step are typed as strings and cast to ints inside the node - another of this pack's "everything is a text field" quirks.

Some practical notes on how slicing semantics work, because they're the whole game:

  • start is inclusive, end is exclusive - 0 to 5 gives you rows 0–4.
  • end can exceed the tensor's size; Python just stops at the end. So 0 to 999999 is a safe way to say "everything."
  • step of 1 is contiguous; 2 takes every other row; a negative step reverses (though this node doesn't let you go backwards-and-empty easily, so don't overthink it).
  • Slicing is a view in PyTorch, not a copy, so it's cheap and memory-friendly.

For a 2D features tensor (N, features), you get back (count, features). For an image stack (N, C, H, W), you get (count, C, H, W). Same dtype, same values, fewer rows.

Inputs and outputs

  • input_data (required TORCH_TENSOR) - the tensor to slice.
  • start (required STRING) - first index, default 0.
  • end (required STRING) - exclusive stop, default 5.
  • step (required STRING) - stride, default 1.
  • Output: TORCH_TENSOR - the sliced subset.

Installing it

Shared with the whole pack:

cd ComfyUI/custom_nodes
git clone https://github.com/TashaSkyUp/EternalKernelPytorchNodes.git
cd EternalKernelPytorchNodes
pip install -r requirements.txt

Restart ComfyUI and it's under ETK/pytorch - or ComfyUI Manager, searching "EternalKernel PyTorch Nodes".

Troubleshooting

The realistic failure modes are few. A non-integer string (a stray space, a letter) throws a plain int() ValueError - the fields aren't validated at the port, so the error hits mid-run. Passing a negative step with a start of 0 gives you an empty tensor rather than an error, which reads as "why is my training doing nothing?" rather than a crash. And remember this slices only the first dimension; if you're trying to cut features off a single sample or slice across channels, this node can't do it - that's a job for ReshapeTensor plus a slice, or a Python-adjacent approach. Also, because this pack loosens ComfyUI's return-type validation globally, a None flowing in here will fail downstream with a mystery error - check the wire feeding input_data.

CategoryETK/pytorch

Inputs (4)

NameTypeDefaultDescription
input_dataTORCH_TENSOR
startSTRING0
endSTRING5
stepSTRING1

Outputs (1)

NameTypeDescription
TORCH_TENSORTORCH_TENSOR