Nodes/comfyui-spawner-nodes/张量维度扩展
ComfyUI Node

张量维度扩展

Add a singleton dimension to a tensor when a node demands one more axis

By spawner1145·Created about a year ago·Updated 11 months ago· 2
张量维度扩展
  • tensor
  • expanded_tensor
dim1

This is the smallest node in the pack and it knows it. TensorUnsqueeze is a one-trick wrapper around PyTorch's unsqueeze: it inserts a dimension of size 1 at the position you pick. [B, H] becomes [B, 1, H]. That's the whole job, and it exists because this pack's tensor world is strict about shapes - its attention and fusion nodes demand 3D [B, S, H] tensors, and a 2D pooled vector will get rejected before it can think about attending to anything.

You'll reach for it in exactly that situation: you pooled something down to [B, H] with TensorPooledMerge, and now a 3D-expecting node (or ConditioningPacker's embedding path) won't take it. Unsqueeze at dim=1 turns it into a [B, 1, H] "sequence of length one" - semantically "one token," which is what a pooled summary is anyway. It's the shape plumbing that keeps a graph moving.

The inputs are two: tensor, and dim (default 1, range -4..4, negatives count from the end). Output is expanded_tensor, same content, one more axis. Errors are friendly: if you ask for a dimension that doesn't exist, it tells you the tensor's current rank and you fix it in one glance.

Honestly, this node is the kind of thing you'd normally do with a one-line Python node in a workflow script - and that's fine. Its value is purely ergonomic: you get a drag-in node that does the one operation, with the dim validated, instead of reaching for a general-purpose reshape tool and mis-typing the argument. It's also the clearest possible example of this pack's philosophy: expose the raw tensor ops, let the user compose them.

One note to keep you from confusion: it's unsqueeze (add a dim), not squeeze (remove a size-1 dim). The pack only ships the add direction. If you need the reverse, you're outside this pack's toolbox - shape the tensor upstream so the size-1 axis never needs removing.

Install

ComfyUI Manager, search comfyui-spawner-nodes, install, restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/spawner1145/comfyui-spawner-nodes

Deps are three pure-Python libs (piexif, pypng, xmltodict), no models. README is a stub ("some toolkits"), UI is Chinese-labeled, author (spawner1145) is a Chinese-speaking dev known for a Wan2.1 SD extension. The pack is a personal toolkit that escaped; it works and is barely documented.

Troubleshooting

  • "无法在维度 X 扩展" - you asked for a dim outside the valid range for the tensor's rank. The error names the rank; negative dims count from the end, so -1 on a 2D tensor is usually safe.
  • Output still won't wire into a node - unsqueeze fixes rank, not feature width. If the next node also needs matching hidden dims, you want TensorShapeAdapter too.
  • Nothing visibly changed - that's the nature of a size-1 axis. The data is identical; only the shape metadata moved. Check with TensorInspector if you want proof.
Categoryspawner/tensor

Inputs (2)

NameTypeDefaultDescription
tensorTENSOR
dimINT1-4–4

Outputs (1)

NameTypeDescription
expanded_tensorTENSOR