Nodes/ComfyDL/Broadcast (DEPRECATED)
ComfyUI Node

Broadcast (DEPRECATED)

Stretch a tensor to a bigger shape without copying the data

By Cynthia-lxx·Created 2 months ago·Updated 2 days ago· 6
Broadcast (DEPRECATED)
  • tensor
  • output
target_shape2,3

CdlBroadcast takes a tensor and expands it to a target shape using PyTorch broadcasting - the trick where a smaller tensor pretends to be bigger by reusing the same data along an axis of size 1. It's a torch.broadcast_to wrapper, and it's one of those "small node, big intuition payoff" things: understanding broadcasting explains half the shape errors you'll ever hit in PyTorch, and this node lets you trigger them on purpose and watch.

How it works

You type the target shape as a comma-separated string, and the node calls torch.broadcast_to. The rules it follows are the standard ones:

  • Dimensions are compared from the right. A trailing dimension of size 1 can stretch to match a bigger one; a trailing dimension that's 1 in the target gets... well, you can't shrink with broadcast - you can only expand where your tensor has a 1 (or where it's missing a dimension entirely).
  • So a [1, 4] tensor can broadcast to [3, 4] (the size-1 leading dimension stretches to 3), but a [3, 4] tensor cannot broadcast to [2, 4] - 3 isn't 1 and isn't 2.

The classic trick this enables: add a fake batch dimension of size 1, broadcast it out to a full batch, and get the same values repeated along that axis. No repeat(), no copying - just a view over the same memory.

Inputs and output

  • tensor - the cdlTensor to expand.
  • target_shape - a string like "3,1,4" (comma-separated integers). The default "2,3" is just an example, not a useful one.

The output is output, a cdlTensor with the target shape.

Where you'd use it

Mostly inside ComfyDL tensor plumbing: when you've got a per-channel mean or a per-batch scalar and you need it shaped like the tensor it'll be combined with. If you're following a d2l-style training workflow and a multiply or a plot keeps failing on shape mismatch, broadcasting is usually the missing mental model - and this node is the way to apply it without writing code.

Installing it

It ships with ComfyDL, one install for the whole pack:

cd ComfyUI/custom_nodes
git clone https://github.com/Cynthia-lxx/ComfyDL
pip install -r ./ComfyDL/requirements.txt

Restart ComfyUI, or search "ComfyDL" in ComfyUI Manager.

Gotchas

  • A bad shape string fails silently. If the target can't be parsed (typo, wrong separator, an empty field), the node returns your original tensor unchanged - no error, no warning. If the node seems to be doing nothing, check the target_shape string character by character.
  • You can only expand, never shrink. Broadcast doesn't reshape or resize; it stretches size-1 dimensions (and pads missing leading dims with implicit 1s). Want a [3,4] out of a [4]? [4] broadcasts to [1,4], so "3,4" works. Want a [2,4] out of a [3,4]? That's a resize, not a broadcast - use CdlReshape or truncate/pad instead.
  • The result is a view, not a copy. That's cheap and fast, but it means the "broadcast" tensor shares memory with the original - don't mutate one expecting the other to stay put.
Categoryd2l/_Legacy/Tensor Basic

Inputs (2)

NameTypeDefaultDescription
tensorTENSOR
target_shapeSTRING2,3

Outputs (1)

NameTypeDescription
outputTENSOR