Expand description
Reduction-pattern fusion pass — fuses reduction-chained element-wise
regions (LayerNorm, softmax, …) that the linear element-wise fuser in
crate::optimizer::fusion cannot capture.
§Why a separate pass
The element-wise chain fuser merges only linear producer → consumer chains in which every fused node is the sole consumer of its predecessor. Reduction patterns break that rule on purpose:
LayerNorm: Softmax:
x x
│ │
┌──┴── mean (reduction) ┌───┴── max (reduction)
│ │ │ │
▼ ▼ ▼ ▼
sub ◄───┘ (broadcast) sub ◄────┘ (broadcast)
│ │
▼ ▼
var (reduction) exp
│ │
▼ ▼
normalize ◄─ (broadcast) sum (reduction)
│ │
▼ ▼
scale_shift divide ◄─ (broadcast)A reduction node produces a small statistic that is broadcast back to
one or more element-wise consumers, so the reduction output fans out to
several nodes (out-degree ≥ 2) and the region re-converges at a single
sink. That diamond shape is precisely what the chain fuser rejects (it
treats any fan-out as “parallel branches, do not fuse”). Fusing these
regions into one kernel removes the intermediate broadcast round-trips to
device memory — the dominant cost of an un-fused LayerNorm/softmax.
§What is fused
A set of nodes is a reduction-fusion region rooted at a fusible kernel
r when:
ris a fusibleNodeKind::KernelLaunch.- The region
R = {r} ∪ membersforms a single-entry, single-exit (SESE) subgraph:rdominates every member, and a unique membersinkis reachable from every member and is the only node inRwith a successor outsideR(post-dominates the region). - Every member is a fusible kernel with a launch configuration compatible
with
r(same total thread count). - The region is closed: no edge leaves a non-
sinkmember to a node outsideR. This is what makes the rewrite semantics-preserving — every intermediate buffer is consumed entirely inside the fused kernel and is never observed by the rest of the graph. - The region contains genuine broadcast fan-out: at least one member
has out-degree ≥ 2 inside
R(the reduction → broadcast shape). This is what distinguishes a reduction region from a plain linear chain (handled by the element-wise fuser) and forces region size ≥ 3.
When a region matches, the pass emits a ReductionFusionGroup describing
the merge. As with crate::optimizer::fusion, the graph itself is not
mutated — the descriptor is consumed downstream by the PTX codegen layer,
and the rewrite helper materialises the fused ComputeGraph when a
concrete rewritten DAG is required (e.g. for the CPU simulator).
§Algorithm
- Topological + dominance analysis.
- For each fusible kernel
rin topological order that is not yet claimed, grow the dominated, fusible, closed region belowrand test the SESE + fan-out conditions. - Accept the largest such region; mark its members claimed so they are not re-used by a later (smaller) region.
Structs§
- Reduction
Fusion Group - A reduction-broadcast region that the pass merges into a single kernel.
- Reduction
Fusion Plan - The complete reduction-fusion plan produced by
analyse.
Enums§
- Reduction
Pattern - The classified shape of a fused reduction region.
Functions§
- analyse
- Runs the reduction-fusion analysis pass on
graph. - rewrite
- Rewrites
graphby collapsing every region inplaninto a single fused kernel node, producing a newComputeGraph.