LatentOperationHardTanh
Tanh with training wheels, and a -1..1 guarantee
- op
Tanh is the classic "squash everything into -1..1" curve, but it's a smooth transcendental and it costs. HardTanh is the piecewise shortcut: values in the middle follow a straight line through zero, anything above 1 pins to 1, anything below -1 pins to -1. Same bounding guarantee, built from three straight segments. For latent work the guarantee is the point - after HardTanh you know every value sits in -1..1, which is exactly the range Latent11ToImage expects for a clean preview.
It's one of the activation nodes in hnmr293's ComfyUI-latent-ops pack, and like every Operation node there, it emits a deferred recipe.
How it works
The node captures alpha and builds a closure that runs torch.nn.functional.hardtanh(latent * alpha) when applied. The alpha pre-scales the latent before the clamp: at the default 1.0, values between -1 and 1 pass through linearly and everything outside is pinned to the edges. Raise alpha and more of the input lands in the clamped zones; lower it and the linear region shrinks. Either way the output is hard-bounded to -1..1, which makes it the cheapest way to guarantee a zero-centered bounded latent before downstream math or a preview.
What matters
alpha- FLOAT, default 1.0, range -10000 to 10000, step 0.0001. Pre-scale of the input.op- the only output. ALATENT_OPERATION- a deferred operation.
And the pack-wide caveat: the op output isn't a latent and won't connect to one. This pack ships no Apply node, so you need a consumer that accepts LATENT_OPERATION, or the op never runs. Same story for all 30+ Operation nodes in this pack.
Installing it
No extra deps, no model downloads - ComfyUI Manager → search "ComfyUI-latent-ops" → install, or:
cd ComfyUI/custom_nodes
git clone https://github.com/hnmr293/ComfyUI-latent-ops
Restart ComfyUI; it's under hnmr/latent_ops.
Troubleshooting
The pinning is the gotcha, same as any hard clamp: values that saturate lose their differences, so a latent with most of its mass beyond ±1 will come out flat. If you need the smooth version that never hard-cuts, tanh (also in this pack) is the classic choice. And if you chain HardTanh into Latent11ToImage for a preview, remember the 11 node assumes -1..1 - which HardTanh guarantees, so this pairing is one of the few latent-preview setups that just works.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| alpha | FLOAT | 1.0000-10000–10000 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| op | LATENT_OPERATION | — |