ComfyUI Node

Pt To Numpy

Step out of the tensor world without leaving the graph

By HowToSD·Created about a year ago·Updated about a year ago· 7
Pt To Numpy
  • tens
  • NDARRAY

PyTorch tensors and NumPy arrays are cousins, but they're not the same object, and there are whole ecosystems of nodes that only speak NumPy. Pt To Numpy is the translator: it takes a TENSOR and returns an NDARRAY, so you can hand your tensor-shaped data to numpy-flavored tools without leaving the ComfyUI graph.

Mechanically it does the standard dance: tens.cpu().detach().numpy(). The .cpu() means it moves the data off the GPU if it lives there, and .detach() cuts the autograd history, so the result is a plain numpy array with no connection to the computation graph. For anything you're inspecting, saving, or handing to numpy-based analysis nodes, that's exactly right - you don't want GPU tensors with gradient history floating around downstream. One input (tens, TENSOR), one output (NDARRAY).

Where this shines: Pt-Wrapper and its sibling pack ComfyUI-Data-Analysis come from the same author and the two interoperate well, and the numpy bridge is a big part of why. It's also your escape hatch when some numpy-only custom node in your graph is the only thing that can do what you need - feed it an NDARRAY and keep your workflow in one place.

The one thing to know: numpy is a view of the tensor's data when the tensor is on CPU and contiguous, but since the node detaches first, you don't have to worry about mutation leaking back into the graph. And because it moves data to CPU, there's a real (if small) copy cost on big tensors - don't put one in a hot loop that runs per-batch during training if you can avoid it.

Install

ComfyUI Manager (search "ComfyUI-Pt-Wrapper"), or:

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

Restart ComfyUI. The pack pulls in a heavy dependency tree (transformers, datasets, peft, accelerate, scikit-learn, pandas, seaborn, matplotlib, gensim, sentencepiece). No model files to download.

Common issues

The usual failure is feeding in a tensor that lives on the GPU and expecting the conversion to be free - it's a copy, so on huge tensors it can stutter. And if your downstream numpy node complains about dtype or non-contiguous arrays, cast or .contiguous() on the tensor side before converting; the node itself is as straightforward as conversions get.

CategoryData Analysis

Inputs (1)

NameTypeDefaultDescription
tensTENSOR

Outputs (1)

NameTypeDescription
NDARRAYNDARRAY