Pt Where
The element-wise if/else your tensor math keeps needing
- condition_tens
- true_tens
- false_tens
- TENSOR
Every so often your tensor math needs a conditional - "where this condition is true, use this; where it's false, use that." In normal code that's an if, but tensors don't do if. Pt Where is the pack's version: three tensors in, one tensor out, each element chosen by a per-element condition. It's the closest thing the pack has to an element-wise if/else, and once you've needed it, you'll reach for it constantly.
Pt Where comes from the "Indexing and Slicing" shelf of ComfyUI-Pt-Wrapper (HowToSD's 200-node PyTorch training pack, the spin-off of ComfyUI-Data-Analysis). It's a torch.where wrapper, and it shows up in the real workflows whenever data needs masking or conditional selection - clamping gradients, building attention masks, replacing out-of-range values, mixing two feature maps by a learned condition.
How it works. Three required inputs, one output:
condition_tens- a boolean tensor (the pack checks and enforces this). Where it'sTrue, output takes fromtrue_tens; whereFalse, fromfalse_tens.true_tens/false_tens- the two value sources. Shapes must broadcast with the condition.
The implementation is torch.where(condition, true, false), and broadcasting means the value tensors can be smaller than the condition - a (8, 512) condition with a scalar-ish true source works, which is handy for "set everything matching to zero" moves. Output is TENSOR, same shape as the condition.
The classic uses, concretely. Masking is the big one: build a condition like "tokens that aren't padding" (from your tokenizer's attention mask) and use Where to zero out the padding positions in an embedding or attention output - same trick that Hf Tokenizer Encode and Sp Encode's masks enable, done by hand. Thresholding is the other: "where activations exceed X, clamp to X" or replace NaNs with a safe value. In a from-scratch model built from pack pieces, Where is often the cheapest way to implement a hard gating mechanism without adding a whole new layer.
Where beginners get tripped up. The condition must be a boolean tensor - feeding it a float comparison result from another node that hasn't been converted will error or misbehave, and the error message reads more cryptically than the cause. Broadcasting surprises: if your three tensors have mismatched shapes that almost broadcast, you get a runtime error mid-run, not at the node, which is confusing. And the non-obvious one: Where evaluates both branches for every element - it's not lazy like an if. If one of your value tensors would throw an error computing, you'll get that error even where the condition would never pick it. So if "the node that feeds true_tens works fine alone but errors when wired here," that's lazy-vs-eager sneaking up on you.
Install: ComfyUI Manager → "ComfyUI-Pt-Wrapper", or:
cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper
then restart. No model downloads; the pack's heavy requirements.txt is the only setup cost.
Troubleshooting: "condition must be bool" errors - compare tensors with a comparison node first, or cast. Runtime shape errors - check all three tensors broadcast; the condition's shape wins. Weird results where values seem "swapped" - verify which branch is True and which is False; it's easy to wire them backwards and get the exact inverse of what you wanted.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| condition_tens | TENSOR | — | |
| true_tens | TENSOR | — | |
| false_tens | TENSOR | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| TENSOR | TENSOR | — |