AssertDims
A tripwire for latent shapes that fails loudly instead of weirdly
- latent
- latent
- first_dim
- last_dim
Most latent bugs don't fail. A tensor with the wrong number of dimensions silently flows through the graph and you get garbage out the other end - or a cryptic torch error three nodes downstream that takes twenty minutes to trace. AssertDims is the opposite: it checks the dimensionality of your latent and throws a loud, readable error if it's wrong. It's a tripwire, and if you're doing anything custom with latents, you'll want a few of them.
This comes from hnmr293 (Hoshino), the veteran Japanese dev behind sd-webui-cutoff, sd-webui-llul, and the bigger ComfyUI-nodes-hnmr pack. AssertDims is part of ComfyUI-latent-ops, a focused latent-math toolkit where everything lands under hnmr/latent_ops.
What it does
Under the hood it's trivial, in a good way. The node grabs latent["samples"], compares samples.ndim to your dims value, and raises a ValueError if they don't match. If they do match, it passes the latent through untouched (a copy, so the rest of your graph is safe) and hands you two extra numbers.
That dims input is the only setting that matters. It defaults to 1, which is almost never right - a normal SD 1.5/SDXL latent is 4D (batch, channels, height, width), and after some operations it can be 3D or 5D. Set it to what you expect, and the node enforces it.
The outputs
latent- the same latent, unchanged. Wire this onward.first_dim- the size of dimension 0 (usually your batch size).last_dim- the size of the last dimension (for aB, C, H, Wlatent, that's width).
Those two INTs are genuinely handy. Drop AssertDims in front of a custom sampler or a latent-math chain, feed first_dim/last_dim into whatever downstream math you're doing, and you get both a guard and the actual values for free.
Installing it
Same story for the whole pack: no extra Python deps, no model downloads, just torch math on latents you already have. ComfyUI Manager → search "ComfyUI-latent-ops" → install. Or by hand:
cd ComfyUI/custom_nodes
git clone https://github.com/hnmr293/ComfyUI-latent-ops
Restart ComfyUI and it shows up under hnmr/latent_ops.
Troubleshooting
When it fires, the error names both sides - "Expected 3 dimensions, but got 4" - which is exactly what you need. If you're unsure what a latent actually is, deliberately set a wrong dims and read the "got" half, or just read first_dim/last_dim. Note that it checks raw tensor dimensions, not pixel resolution: a 512×512 latent is still 4D, so AssertDims won't catch a wrong-size latent - for that you want its sibling AssertShape, which checks the full shape. Use AssertDims where dimensionality is the contract (batch-split pipes, per-sampler inputs) and let it hard-stop the graph when the contract breaks.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| latent | LATENT | — | |
| dims | INT | 11–100 | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| latent | LATENT | — |
| first_dim | INT | — |
| last_dim | INT | — |