Nodes/ComfyUI-latent-ops/LatentOperationClamp
ComfyUI Node

LatentOperationClamp

Keep latent values inside a range

By hnmr293·Created about a year ago·Updated about a year ago· 2
LatentOperationClamp
    • op
    min0.0000
    max1.0000

    Latent math tends to wander. Add a few operations together, run a nonlinearity, and suddenly your latent has values at 14 and -9 when the model downstream expects something sane. LatentOperationClamp is the simplest cure there is: push every value into a range you declare, and anything outside gets pinned to the edge. It's the latent-space version of "turn it down and back on again" - crude, and exactly right a lot of the time.

    It's part of hnmr293's ComfyUI-latent-ops pack, and it follows the pack's universal pattern: it builds a deferred operation and hands you the recipe, not the result.

    How it works

    The node captures your min and max and builds a closure that runs latent.clamp(min=min, max=max) when applied. In one call, every element below min becomes min, every element above max becomes max, and everything in between passes through untouched. The default range is 0 to 1 - which is exactly the range Latent01ToImage expects, so this is the natural partner when you want to preview a latent that's been pushed around by other ops.

    What matters

    • min - FLOAT, default 0. The lower bound. The author's tooltip: "Minimum value to clamp to."
    • max - FLOAT, default 1. The upper bound. Tooltip: "Maximum value to clamp to."
    • op - the only output. A deferred operation.

    Both bounds are plain numbers, no encoder required, and the 0.0001 step means you can dial a bound precisely without touching anything else. For the full pack logic: this is a LATENT_OPERATION output - there's no Apply node in the pack, so you need a consumer that accepts that type, or the op never runs.

    Installing it

    No deps, no models - ComfyUI Manager → search "ComfyUI-latent-ops" → install, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/hnmr293/ComfyUI-latent-ops
    

    Restart ComfyUI; the node is under hnmr/latent_ops.

    Troubleshooting

    Keep min below max - flip them and you'll get a degenerate clamp that pins everything to one side, and the results will make no sense. If your values are only running away in one direction, you don't need both bounds: LatentOperationClampMin and LatentOperationClampMax each handle one side. And remember this clamps, it doesn't rescale: if your data spans -5 to 15 and you want it to spread across 0-1, clamp it and then hit it with a NormalizeMinMax. Clamping loses information at the edges by design - that's fine when you're pinning outliers, wrong when you're trying to compress a real gradient.

    Categoryhnmr/latent_ops

    Inputs (2)

    NameTypeDefaultDescription
    minFLOAT0.0000-10000–10000Minimum value to clamp to.
    maxFLOAT1.0000-10000–10000Maximum value to clamp to.

    Outputs (1)

    NameTypeDescription
    opLATENT_OPERATION