ComfyUI Node

Pt View

Reshape a tensor without leaving the graph

By HowToSD·Created about a year ago·Updated about a year ago· 7
Pt View
  • tens
  • TENSOR
new_shape

Somewhere in every training workflow you hit the moment where a tensor is the wrong shape: the model expects [batch, seq, features] and you're holding [batch*seq, features], or you need to flatten channels for a fully-connected head. In stock ComfyUI you'd be stuck eye-balling it. In ComfyUI-Pt-Wrapper, Pt View is the node that does torch.view() for you - type the shape you want, get the reshaped tensor back.

How it works

It's a one-liner wrapper around PyTorch's view(). You give it a tensor and a new_shape string like [6, 4] or [2, -1] (list or tuple both parse), it runs ast.literal_eval on the string, and calls tens.view(*shape). The -1 is the magic part: PyTorch infers that dimension automatically from the total element count, so [2, -1] on a [2, 3, 4] tensor gives you [2, 12]. Useful when you don't want to do the multiplication in your head.

The inputs that matter

Only two, and one is a tensor wire:

  • tens - any TENSOR in the pack (they come from Pt Zeros, Pt From Numpy, model outputs, etc.).
  • new_shape - the target shape as text: "[6, 4]", "(6, 4)", "[2, -1]". The docs' example is (2,3,4)new_shape="[6, 4]" → output (6, 4).

Output is a single TENSOR, ready to feed the next node.

The traps

Two real ones, both inherited directly from how view() works:

  • Element count must match. view can't change the total number of elements, so (2,3,4)[5, 5] throws "shape '[5, 5]' is invalid for input of size 24". No guessing; count first.
  • Non-contiguous tensors. If the input tensor is a slice or a permutation (non-contiguous in memory), view raises "view size is not compatible with input tensor's size and stride". This isn't your bug - it's a memory-layout thing. The pack also ships Pt Reshape, which copies when needed, so just swap nodes.

There's no IS_CHANGED on this one, so ComfyUI may cache the output; if you edit new_shape and nothing seems to change, re-run the queue.

Wiring it in

Pt View sits anywhere you need a shape change mid-pipeline - typically between a tensor-creation or dataset node and a model node whose expected input shape you're feeding. It's a utility, not the star of the show, but reshape errors are among the most common ways a Pt Wrapper workflow refuses to run, and this is the tool that fixes them.

Install

The whole pack in one shot:

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

Restart ComfyUI, or use ComfyUI Manager and search "ComfyUI-Pt-Wrapper". No extra dependencies beyond the pack's standard install.

Common issues

  • "shape invalid for input of size N" - element count mismatch; recalculate or use -1.
  • "not compatible with input tensor's size and stride" - non-contiguous input; use Pt Reshape.
  • Confusing output after editing - re-run the queue to bust the cache.

It's a small node and it does exactly one thing. Learn the -1 trick and you'll reach for it constantly.

CategoryData Analysis

Inputs (2)

NameTypeDefaultDescription
tensTENSOR
new_shapeSTRING

Outputs (1)

NameTypeDescription
TENSORTENSOR