TECHNOLOGY

Triton conditional work: where selects values while a scalar mapped branch selects execution

In this article
THE QUICK TAKE

A selection expression does not automatically skip its unused producer; use the actual control-flow interface when avoiding expensive branch computation is part of the design.

Separate selection from control flow

The where API explicitly states that both value arguments are evaluated regardless of the condition. It selects which resulting value is returned. Therefore, a source-level conditional-looking expression does not by itself establish that an expensive alternative was skipped. This guide concerns execution of alternative computations, rather than repeating the separate rule that memory operations need appropriate access masks.

Identify a scalar branch interface

The map_elementwise API applies a scalar function over tensor values. The language source contrasts value selection with a scalar function containing a branch, allowing only the selected side to execute. This provides a concrete abstraction for a piecewise operation when avoiding unnecessary producer work matters. It does not promise that every branch distribution will run faster than evaluating vectorized alternatives.

Define the piecewise computation first

Consider an activation where a cheap expression covers common inputs while another expression requires more arithmetic. With where, the expensive producer remains part of argument evaluation even where its value is discarded. A scalar conditional can express another execution policy. Preserve the same mathematical branch boundary and output types while comparing them, so a purported speedup is not actually a change in the activation definition or a difference in how exceptional input values are handled.

Test branch distribution explicitly

A workload containing only the cheap branch can expose avoided work, but it does not describe a mixed workload. Compare homogeneous and mixed inputs under the actual target and inspect generated code. SIMT execution and compiler transformations can affect the benefit of conditional work. Use a reference that checks values on both sides and at the boundary before interpreting timing. This distinguishes correct control-flow intent from an unverified assumption that every element independently saves the full cost of the discarded branch.

Report executed policy and numerical behavior

Record branch condition, producer expressions, selected interface, compiler revision, input distribution, and observed code path. Guides to SIMT divergence and inference evaluation provide context. Evaluate correctness and cost together. A meaningful optimization explains which producer work disappeared under which conditions, because a ternary-looking expression, a scalar branch, and a masked memory operation are different tools with different contracts and cannot be treated as interchangeable syntax.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles