liveBook · Manning

Tl.where

As explained in GPU Programming with Triton

tl.where is a Triton operation that selects values elementwise according to a condition without requiring divergent branch execution.

For an elementwise condition, different lanes may need different values. tl.where(mask, a, b) selects a or b independently for each lane, keeping the warp on one control-flow path instead of serializing divergent branches. section 3.4.2 section 3.7

output = tl.where(x > 0, x, tl.zeros_like(x))

A ReLU selects each positive input value and otherwise selects zero. section 3.4.2

Selection does not make candidate computation conditional: both candidate expressions are often evaluated before the select. When they require substantial work, producing them can cost more than the select itself. section 3.4.2

How GPU Programming with Triton covers it

How deep it goes
Explains lane-wise selection and candidate costs without walking through a complete kernel.
Examples
Uses ReLU selection and contrasts routing existing candidates with computing candidates before selection.
What you can do after
Apply tl.where to select elementwise values while keeping lanes on one control-flow path.
How it connects
Connects elementwise selection to warp divergence, conditional branching, and Triton masking.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of GPU Programming with Triton. The section links point to the parts of the book it draws on.