Skip to main content

fuel_ir/
dispatch.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Cross-crate dispatch table types — Phase 6b's empirical
3//! `(op, dtype, size_class) → (backend, device)` lookup.
4//!
5//! These types live here, not in `fuel-core`, because both
6//! `fuel-core` (which produces tables via the [`Judge`] in its
7//! `judge` module) and `fuel-graph-router` (which consumes them at
8//! op-dispatch time to pick between competing backends) need the
9//! same shapes. The producer-side `Judge` and the
10//! `populate_dispatch_table` / `cached` cache APIs live in
11//! `fuel-core` because they call into `Tensor`'s realize path.
12//! Only the data + the pure lookup helpers are here.
13//!
14//! [`Judge`]: https://github.com/ThinkersJournal/fuel/blob/main/fuel-core/src/judge/mod.rs
15
16use crate::DType;
17use crate::Shape;
18use crate::probe::BackendId;
19use std::collections::HashMap;
20
21#[cfg(feature = "serde")]
22use serde::{Deserialize, Serialize};
23
24/// Schema version for persisted profile reports. Bump when the
25/// entry layout changes in a way that can't be covered by
26/// `#[serde(default)]`.
27///
28/// **v2 (2026-06-08)**: per-alternative measurement landed —
29/// [`ProfileEntry`] gains `kernel_source` so the AOCL/MKL/portable-cpu
30/// kernel siblings at one `(op, dtype, size_class, BackendId::Cpu)`
31/// cell each get their own measured entry. v1 reports lacked the
32/// field and were ambiguous about which CPU-substrate kernel they
33/// timed; old reports load as `Ok(None)` (cache miss → re-Judge).
34///
35/// **v3 (2026-07-04)**: dtype-axis coverage landed — the Judge's
36/// measurement matrix now iterates `{F32, F16, BF16}` for every
37/// profiled op/size (Judge Layer-2 coverage arc, dtype slice). The
38/// [`ProfileEntry`] *layout* is unchanged (`dtype` was always a
39/// field), but the *coverage* is: a v3 report carries f16/bf16 cells
40/// a v2 report never had. The bump is deliberate rather than
41/// serde-forced: without it, `populate_dispatch_table`'s idempotence
42/// guard would keep an upgraded install's f32-only v2 profile forever
43/// (the guard no-ops when a cached report exists), silently denying
44/// the ranker the new per-dtype latencies. Bumping makes v2 reports
45/// load as `Ok(None)` (cache miss → clean re-Judge of the full dtype
46/// matrix) — the same migration shape v1→v2 used. A v2 report is not
47/// *wrong*, just partial; the empty-oracle version gate in
48/// `ProfileJudgeOracle::from_report` and `ProfileReport::load` both
49/// reject it so no partial data leaks into the cost composer.
50///
51/// **v4 (2026-07-04)**: matmul [`SizeClass`] repr change — the
52/// aspect-blind `log2(total_elements)` key that keyed a matmul on
53/// `m·n` (output) is replaced, *for matmul only*, by an aspect key
54/// packing `(log2(m·n), log2(m), log2(k))` (see [`SizeClass`]). This
55/// fixes a latent producer/consumer key disagreement: the Judge keyed
56/// matmul cells on `m·n` while the `fuel-dispatch` ranker keyed its
57/// realize-time lookup on `m·k` (LHS input), so every *non-square*
58/// matmul lookup missed the profiled cell. Producer and consumer now
59/// derive the key from the operand shapes through the SAME
60/// [`SizeClass::matmul`] / [`SizeClass::for_op`] helper. The
61/// [`SizeClass`] field also widens `u8 → u32` to hold the packed key.
62/// v3 reports (scalar matmul keys) load as `Ok(None)` — cache miss →
63/// clean re-Judge — the established migration (v1→v2, v2→v3). The
64/// `ProfileReport::load` / `ProfileJudgeOracle::from_report` version
65/// gates reject the older schema so no scalar matmul key leaks into
66/// the aspect-keyed lookup path.
67///
68/// **v5 (2026-08-27)**: device + kernel IDENTITY landed — [`ProfileEntry`]
69/// gains `kernel_revision_hash` (variant-granular kernel identity, so the many
70/// tilings of one provider stop collapsing into one Judge cell) and `device`
71/// (the profiled hardware's [`EquivalenceKey`](crate::probe::EquivalenceKey),
72/// so a report is REFUSED on hardware it was not measured on rather than
73/// answering for a device it never saw — `device_index` was an ordinal that
74/// rebinds across boots and under `CUDA_VISIBLE_DEVICES`). The bump is
75/// REQUIRED, not merely serde-forced: a v4 report read by v5 code would
76/// `#[serde(default)]` `kernel_revision_hash` to `0`, landing on the untracked
77/// sentinel and collapsing every sibling; and every v4 report was keyed by an
78/// ordinal that may ALREADY be wrong on a multi-GPU or re-imaged box.
79/// Discarding them (load → `Ok(None)`) is the correct outcome — anything
80/// written pre-v5 is indistinguishable from a right report and may be
81/// mis-attributed. This is also the cheapest re-profile there will ever be
82/// (baseline, siblings ≈ 1); every later one, once candidate kernels exist,
83/// costs strictly more.
84pub const PROFILE_REPORT_VERSION: u32 = 5;
85
86/// Op kinds the Judge profiles. Adding a variant + a Judge match
87/// arm extends the profile matrix; existing reports parse forward
88/// thanks to `#[non_exhaustive]`.
89///
90/// Phase 7.5 storage unification — Phase C grows this enum one op
91/// family per migration step. Each variant is the dispatch key that
92/// pairs with [`crate::DType`] and
93/// [`crate::probe::BackendId`] to select a kernel. The naming
94/// convention is `<Op><Family>` (e.g. `ReluElementwise`,
95/// `MulElementwise`); families currently in use:
96///
97/// - elementwise binary: `Add/Sub/Mul/Div` + `Elementwise`
98/// - elementwise unary: `Relu/Neg/Sqr/Sqrt/Recip/Abs/Tanh/...`
99///   + `Elementwise`
100/// - dense linear algebra: `MatMul`
101#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
102#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
103#[non_exhaustive]
104pub enum OpKind {
105    /// Dense matrix multiply, `[M, K] @ [K, N] → [M, N]`.
106    MatMul,
107    /// Elementwise addition of two equally-shaped tensors.
108    AddElementwise,
109    /// Elementwise subtraction (`a - b`).
110    SubElementwise,
111    /// Elementwise multiplication.
112    MulElementwise,
113    /// Elementwise division (`a / b`).
114    DivElementwise,
115    /// Elementwise rectified linear unit (`max(0, x)`).
116    ReluElementwise,
117    /// Elementwise negation (`-x`).
118    NegElementwise,
119    /// Elementwise square (`x * x`).
120    SqrElementwise,
121    /// Elementwise square root.
122    SqrtElementwise,
123    /// Elementwise reciprocal (`1 / x`).
124    RecipElementwise,
125    /// Elementwise absolute value.
126    AbsElementwise,
127    /// Elementwise hyperbolic tangent.
128    TanhElementwise,
129    /// Elementwise exponential (`e^x`).
130    ExpElementwise,
131    /// Elementwise natural logarithm (`ln(x)`).
132    LogElementwise,
133    /// Elementwise sine.
134    SinElementwise,
135    /// Elementwise cosine.
136    CosElementwise,
137    /// Elementwise logistic sigmoid (`1 / (1 + e^-x)`).
138    SigmoidElementwise,
139    /// SiLU / Swish activation (`x * sigmoid(x)`).
140    SiluElementwise,
141    /// GELU activation, tanh approximation
142    /// (`0.5*x*(1 + tanh(√(2/π) * (x + 0.044715*x³)))`).
143    GeluTanhElementwise,
144    /// Heaviside step function (`1` where `x > 0`, `0` otherwise) —
145    /// the derivative of [`OpKind::ReluElementwise`].
146    StepElementwise,
147
148    /// Sum-reduce one or more dimensions of the input. The reduced
149    /// dims and the input shape live in
150    /// [`OpParams::Reduce`](super::dispatch::OpKind); the output is
151    /// the input with those dims dropped (or rank-0 when every dim
152    /// is reduced).
153    SumReduce,
154    /// Max-reduce — same shape contract as [`OpKind::SumReduce`].
155    MaxReduce,
156    /// Min-reduce — same shape contract as [`OpKind::SumReduce`].
157    MinReduce,
158    /// Arithmetic-mean reduce — same shape contract as
159    /// [`OpKind::SumReduce`]; divides the sum by the product of reduced
160    /// dim sizes.
161    MeanReduce,
162
163    /// Convert input bytes from one dtype to another. The target
164    /// dtype lives on the output Storage's `dtype` field (and on
165    /// the Node's dtype); the source dtype is read from the input
166    /// Storage at runtime by the wrapper, which dispatches to the
167    /// right typed conversion kernel internally. Binding-table
168    /// lookup is keyed on the *target* dtype (= the Node's dtype).
169    Cast,
170    /// 2D convolution forward pass. Shape + geometry flow through
171    /// [`OpParams::Conv2D`](super::dispatch::OpKind). Two-input
172    /// case: `(x, weight)`; three-input case adds `bias`. Output
173    /// shape `[N, Cout, Hout, Wout]`.
174    Conv2D,
175    /// 2D transposed convolution. Shape + geometry flow through
176    /// `OpParams::ConvTranspose2D`. Inputs and bias mirror Conv2D,
177    /// but weight has transposed channel order (`[Cin, Cout/groups,
178    /// Kh, Kw]`) and the variant carries `output_padding`.
179    ConvTranspose2D,
180    /// Sum-reduce a tensor to a smaller broadcast-compatible shape.
181    /// The output rank may be lower than the input's; in that case
182    /// the output shape is implicitly left-padded with 1s for axis
183    /// alignment. Carried by `OpParams::ReduceSumTo` with both the
184    /// input and target shapes.
185    ReduceSumTo,
186    /// Max-reduce a tensor to a smaller broadcast-compatible shape —
187    /// the max-symmetric counterpart of `ReduceSumTo`. Same axis
188    /// alignment rules; per-axis reduction is `max` instead of `+`.
189    /// Carried by `OpParams::ReduceMaxTo` with both shapes.
190    ReduceMaxTo,
191    /// Fused matmul + bias-add. Inputs `[a, b, bias]`, output
192    /// `[..., M, N] = a @ b + bias[None..., :]`. Reuses
193    /// `OpParams::Matmul` for shape (kernel inits accumulator with
194    /// the bias element instead of zero).
195    FusedLinear,
196    /// Multi-head scaled-dot-product attention (the math definition,
197    /// not a tiled FlashAttention-2 kernel — naive on CPU). Inputs
198    /// `[q, k, v, optional alibi_slopes]`. Geometry, softmax_scale,
199    /// causal, window, softcap all flow through `OpParams::FlashAttn`.
200    FlashAttn,
201    /// Backward of [`OpKind::FlashAttn`]: produces dQ from `(q, k, v, do, [alibi])`.
202    /// Reuses `OpParams::FlashAttn` (same shape/geometry/causal flags
203    /// as the forward — the recompute pass needs every forward parameter).
204    /// Output shape == q shape. The dK and dV gradients are emitted as
205    /// separate [`OpKind::FlashAttnBackwardK`] / [`OpKind::FlashAttnBackwardV`] nodes
206    /// against the same inputs; CPU backends recompute the softmax
207    /// state independently per call. A 3-output fused variant would
208    /// share the recompute but needs multi-output infrastructure that
209    /// doesn't exist yet.
210    FlashAttnBackwardQ,
211    /// Backward of [`OpKind::FlashAttn`]: produces dK. See [`OpKind::FlashAttnBackwardQ`].
212    /// Output shape == k shape.
213    FlashAttnBackwardK,
214    /// Backward of [`OpKind::FlashAttn`]: produces dV. See [`OpKind::FlashAttnBackwardQ`].
215    /// Output shape == v shape.
216    FlashAttnBackwardV,
217    /// Paged-cache scaled-dot-product attention. Inputs `[q, k_cache,
218    /// v_cache, block_table, context_lens, optional alibi_slopes]`.
219    /// Geometry + scale + softcap flow through `OpParams::PagedAttn`.
220    PagedAttn,
221    /// Affine transformation `y = mul * x + add` with scalar
222    /// coefficients. `Op::AddScalar(c)` maps as `mul=1, add=c`;
223    /// `Op::MulScalar(c)` maps as `mul=c, add=0`.
224    Affine,
225    /// Element-wise clamp: `y = clamp(x, min, max)`.
226    ClampElementwise,
227    /// Element-wise integer power: `y = x.powi(exp)`.
228    PowIElementwise,
229    /// Backward of [`OpKind::PowIElementwise`]: `(x, upstream) → grad_x = exp ·
230    /// x^(exp-1) · upstream`. Two inputs; carries the same `exp: i32`
231    /// in `OpParams::PowI` as the forward. Single-launch alternative
232    /// to the autograd primitive decomposition (PowI(exp-1) →
233    /// MulScalar → Mul, 3 nodes). Per-dtype.
234    PowIElementwiseBackward,
235    /// Element-wise tensor maximum: `y[i] = max(lhs[i], rhs[i])`.
236    MaximumElementwise,
237    /// Element-wise tensor minimum: `y[i] = min(lhs[i], rhs[i])`.
238    MinimumElementwise,
239    /// Element-wise equality `a == b`. Both inputs share dtype `T`;
240    /// output is a `U8` mask (`1` where equal, `0` otherwise). The
241    /// binding-table dtype list is keyed `[T, T, U8]`, mirroring the
242    /// ArgMax convention of carrying the output dtype explicitly when
243    /// it differs from the inputs.
244    EqualElementwise,
245    /// Element-wise inequality `a != b`. Same shape contract as
246    /// [`OpKind::EqualElementwise`]; output `U8` mask (`1` where unequal,
247    /// `0` otherwise). NaN follows IEEE-754: `NaN != NaN` is true,
248    /// so `ne` returns `1` on NaN-vs-NaN positions.
249    NotEqualElementwise,
250    /// Element-wise strictly-less `a < b`. Same shape contract as
251    /// [`OpKind::EqualElementwise`]; output `U8` mask. NaN-on-either-side is
252    /// always false (IEEE-754 unordered comparison).
253    LessElementwise,
254    /// Element-wise less-or-equal `a <= b`. Same shape contract.
255    /// NaN comparisons are unordered → `0`.
256    LessEqualElementwise,
257    /// Element-wise strictly-greater `a > b`. Same shape contract.
258    /// NaN comparisons are unordered → `0`.
259    GreaterElementwise,
260    /// Element-wise greater-or-equal `a >= b`. Same shape contract.
261    /// NaN comparisons are unordered → `0`.
262    GreaterEqualElementwise,
263    /// Ternary select: `out[i] = if cond[i] != 0 { a[i] } else { b[i] }`.
264    /// Inputs `(cond, a, b)` — `cond` is `U8`, `a` and `b` share dtype
265    /// `T` and shape with `cond`. Output is `T` shape `cond.shape()`.
266    /// Binding-table dtype list `[U8, T, T, T]`.
267    Where,
268    /// Element-wise floor (`⌊x⌋`). Same dtype as input. Backward is
269    /// the zero distribution almost everywhere; gradient through
270    /// rounding ops is dropped silently.
271    FloorElementwise,
272    /// Element-wise ceiling (`⌈x⌉`). Same dtype as input. Backward
273    /// drops gradient (mirrors Floor).
274    CeilElementwise,
275    /// Element-wise round-to-nearest with **banker's rounding**
276    /// (round-half-to-even, IEEE 754 roundeven). Backward drops
277    /// gradient (mirrors Floor/Ceil).
278    RoundElementwise,
279    /// Element-wise sign (`-1` if negative, `0` if zero, `1` if
280    /// positive). Same dtype as input. Backward drops gradient
281    /// (zero almost everywhere; the subgradient at 0 is taken as 0).
282    SignElementwise,
283    /// Element-wise Gauss error function (`erf(x)`). Same dtype as
284    /// input. Backward: `d/dx erf(x) = (2/√π) * exp(-x²)` —
285    /// expressed via `Sqr` + `Neg` + `Exp` + `MulScalar`.
286    ErfElementwise,
287    /// Element-wise GELU activation, **exact erf formulation**:
288    /// `0.5 * x * (1 + erf(x/√2))`. Distinct from
289    /// [`OpKind::GeluTanhElementwise`] (tanh approximation). Same dtype as input.
290    /// Backward decomposes into the standard-normal CDF + `x * φ(x)`
291    /// (PDF) chain via existing primitives.
292    GeluElementwise,
293    /// Element-wise binary power: `out[i] = pow(a[i], b[i])`. Both
294    /// inputs share dtype `T` and shape; output is `T` with the same
295    /// shape. Distinct from [`OpKind::PowIElementwise`] (scalar `i32`
296    /// exponent). NaN follows IEEE-754 (e.g. `pow(-2, 0.5) = NaN`).
297    PowElementwise,
298    /// Element-wise reciprocal square root: `out[i] = 1 / sqrt(x[i])`.
299    /// Single op (vs Sqrt + Recip — combining loses precision and
300    /// doubles kernel launches). Critical for RMSNorm (`x / sqrt(mean(x²)+eps)`
301    /// → `x * rsqrt(mean(x²)+eps)`).
302    RsqrtElementwise,
303    /// Element-wise remainder, **PyTorch convention**:
304    /// `out[i] = a[i] - floor(a[i] / b[i]) * b[i]`. The sign of the
305    /// result matches the sign of the divisor (differs from C99 `fmod`
306    /// which has the sign of the dividend; `f32::%` is also fmod-style).
307    /// Same dtype + shape on both inputs.
308    RemElementwise,
309    /// Reverse the order of elements along one dim. Dtype-agnostic
310    /// at the byte level. Materializing op (Layout strides are
311    /// unsigned; the negative-stride view path requires a wider
312    /// stride representation that's a separate scope).
313    Flip,
314    /// Cyclic shift along one dim by a signed `shift`. Wraps
315    /// (e.g. `roll([0,1,2], shift=1) = [2,0,1]`). Materializing op,
316    /// dtype-agnostic.
317    Roll,
318    /// Running cumulative sum along one dim:
319    /// `out[..., i, ...] = sum(in[..., 0..=i, ...])`. Same shape as
320    /// input. Per-dtype kernel (sum needs typed addition).
321    CumSum,
322    /// Pad along one dim with `before`/`after` extra slots. Modes:
323    /// Constant (fill with a value), Reflect (mirror edges),
324    /// Replicate (repeat edges). Only Constant is implemented in
325    /// the v1 cut; Reflect/Replicate fall through to a clean error.
326    Pad,
327    /// Upper-triangular mask along the last two dims. Materializing,
328    /// dtype-agnostic at the byte level (output is x or zero per
329    /// position; just selects bytes from src or the zero-init buffer).
330    Triu,
331    /// Lower-triangular mask along the last two dims (mirror of [`OpKind::Triu`]).
332    Tril,
333    /// Numerically-stable log-softmax along the last dim. Per-dtype
334    /// (uses log/exp). Output shape == input shape.
335    LogSoftmaxLastDim,
336    /// Backward of [`OpKind::LogSoftmaxLastDim`]: takes `(forward_output, upstream)`
337    /// and produces the input gradient. Per-dtype.
338    LogSoftmaxLastDimBackward,
339    /// MaskedFill: fill positions where mask is nonzero with a scalar.
340    /// Inputs `(x, mask)` where mask is U8. Per-dtype on x; output
341    /// shape == x shape.
342    MaskedFill,
343    /// Backward helper for `Pad`. Per-dtype since accumulation is
344    /// typed addition. Routes all 3 modes uniformly — the kernel
345    /// switches on `mode_tag`.
346    PadBackward,
347    /// Concatenate N inputs along one dim. Inputs must agree on
348    /// every dim except the concat dim; output's concat-dim size
349    /// is the sum of inputs' concat-dim sizes.
350    Concat,
351    /// Softmax along the last dim, numerically stable
352    /// (subtract per-row max, exp, divide by sum).
353    SoftmaxLastDim,
354    /// Backward of [`OpKind::SoftmaxLastDim`]: `(y, g) → y · (g - sum(y · g, last))`.
355    /// Per-dtype; output shape == y shape.
356    SoftmaxLastDimBackward,
357    /// RMS normalization along the last dim, no affine params:
358    /// `y = x / sqrt(mean(x², last) + eps)`.
359    RmsNormLastDim,
360    /// Backward of [`OpKind::RmsNormLastDim`]: `(x, g_y) → grad_x` per the
361    /// closed-form formula in
362    /// `fuel-reference-backend::ops::rms_norm_last_dim_backward`.
363    /// Per-dtype + eps; output shape == x shape.
364    RmsNormLastDimBackward,
365    /// Layer normalization along the last dim, no affine params:
366    /// `y = (x - mean(x)) / sqrt(var(x) + eps)`.
367    LayerNormLastDim,
368    /// Backward of [`OpKind::LayerNormLastDim`]: `(x, g) → grad_x` per the
369    /// canonical formula. Per-dtype + eps; output shape == x shape.
370    LayerNormLastDimBackward,
371    /// Backward of [`OpKind::ReduceMaxTo`]: `(x, upstream) → grad_x` of x's
372    /// shape. Routes upstream to argmax positions; ties split
373    /// equally. Per-dtype; carries shape pair via
374    /// `OpParams::ReduceMaxToBackward`.
375    ReduceMaxToBackward,
376    /// Pick slices from a source tensor along `dim` using a rank-1
377    /// U32 index tensor. Output's `dim` size = number of indices.
378    IndexSelect,
379    /// N-dimensional gather along `dim`. Source and indices have
380    /// the same rank; output shape equals indices.shape(). For
381    /// each output position, source is read at the same multi-index
382    /// except `dim`'s coord is read from the indices tensor.
383    Gather,
384    /// Fused rotary position embedding. Inputs `(x, cos, sin)`;
385    /// `cos`/`sin` broadcast across leading dims. Used by every
386    /// modern transformer's attention layer.
387    Rope,
388    /// Add `src` values into a copy of `base` at positions given
389    /// by a rank-1 U32 `indices` tensor along `dim`. Inputs:
390    /// `(base, indices, src)`. Output shape == base shape.
391    IndexAdd,
392    /// N-dimensional scatter-add: the functional inverse of
393    /// `Gather`. Inputs `(base, indices, src)`; indices and src
394    /// share the same shape; for each position `p`, the source
395    /// value is added into base at the multi-index where `dim`'s
396    /// coord is `indices[p]`.
397    ScatterAdd,
398    /// Argmax along one dim — produces a U32 index tensor with
399    /// `dim` removed from the output shape.
400    ArgMaxDim,
401    /// Argmin along one dim — same shape contract as
402    /// [`OpKind::ArgMaxDim`].
403    ArgMinDim,
404    /// Quantized matmul: `C = A @ dequant(W_Q)`. Activations are
405    /// f32 (or eventually bf16); weights are a U32-typed byte
406    /// stream of quantized blocks. The quant format is carried in
407    /// [`OpParams::QMatMul`](super::dispatch::OpKind).
408    QMatMul,
409    /// In-place rectangular scatter write: copies source's bytes
410    /// into a slab of destination defined per-axis by `ranges`.
411    /// Backs Phase E.3.2's persistent KV-cache writes
412    /// (`InferenceContext` + `KvCache`). See
413    /// `Op::WriteSlice` for the IR
414    /// contract and [`OpParams::WriteSlice`](super::dispatch::OpKind)
415    /// for the kernel-side params.
416    WriteSlice,
417    /// In-place ring-buffer scatter write: copies source's bytes into
418    /// destination at a slab that wraps modulo `modulus` on the
419    /// rotating axis. The write position comes from a dynamic input
420    /// (rank-0 U32). Backs sliding-window KV caches (Mistral /
421    /// Phi-3 sliding-window / sliding-window Qwen). See
422    /// `Op::WriteSliceRotating`
423    /// for the IR contract.
424    WriteSliceRotating,
425    /// In-place scatter write whose start on ONE axis is read from a
426    /// device-resident `i64` scalar input (rank-0) at kernel launch,
427    /// not host-baked into the range — the CUDA-graph-capturable
428    /// KV-cache append. Like [`WriteSlice`](Self::WriteSlice) but the
429    /// `ranges[axis].0` placeholder is overridden by `*offset` device-
430    /// side; no modulo wrap. Backs `DecodeSession` / CapturedRun. See
431    /// `Op::WriteSliceDoff` for the
432    /// IR contract.
433    WriteSliceDoff,
434    /// Cross-device copy: produce a fresh tensor on the target
435    /// device, copying bytes from the input's residency. Backs
436    /// `Op::Copy` for the bridge-retirement
437    /// trajectory's Phase 2 (D2H through the binding table).
438    ///
439    /// Binding-table key shape `[T, T]` (input dtype, output dtype —
440    /// always equal). The `BackendId` axis encodes the **source**
441    /// backend (where the kernel runs — the source owns its own
442    /// download path); the executor allocates the output Storage on
443    /// the `Op::Copy { target }` location field via a dedicated
444    /// `WorkItemKind::Copy` arm.
445    Copy,
446    // ---- In-place ops (Phase 3 of the in-place ops infrastructure;
447    // ---- see docs/session-prompts/in-place-ops-infrastructure.md) ----
448    //
449    // Each in-place OpKind mirrors a non-inplace functional cousin.
450    // The dispatch difference is structural rather than computational:
451    // the executor adopts the target's Storage Arc as the output slot
452    // instead of allocating fresh bytes. The kernel reads + writes the
453    // same buffer through a single write lock. Binding-table key
454    // shape `[T]` (the in-place op's single input dtype = output
455    // dtype). `KernelCaps::strided_input` semantics apply identically
456    // to the non-inplace cousin (same-pointer dispatch handles strided
457    // inputs as long as the stride pattern doesn't produce overlapping
458    // writes; the unary kernels write index `i` after reading index `i`
459    // so no aliasing issues).
460    /// In-place [`OpKind::ReluElementwise`] — `x = max(0, x)`.
461    ReluInplace,
462    /// In-place [`OpKind::SiluElementwise`] — `x = x · sigmoid(x)`.
463    SiluInplace,
464    /// In-place [`OpKind::GeluTanhElementwise`] (tanh approximation) —
465    /// `x = 0.5 · x · (1 + tanh(√(2/π) · (x + 0.044715·x³)))`.
466    GeluTanhInplace,
467    /// In-place [`OpKind::TanhElementwise`] — `x = tanh(x)`.
468    TanhInplace,
469    /// In-place [`OpKind::SigmoidElementwise`] — `x = 1 / (1 + exp(-x))`.
470    SigmoidInplace,
471    /// In-place [`OpKind::NegElementwise`] — `x = -x`.
472    NegInplace,
473    /// In-place [`OpKind::AbsElementwise`] — `x = |x|`.
474    AbsInplace,
475    /// In-place [`OpKind::SqrElementwise`] — `x = x²`.
476    SqrInplace,
477    /// In-place [`OpKind::SqrtElementwise`] — `x = √x`.
478    SqrtInplace,
479    /// In-place [`OpKind::RsqrtElementwise`] — `x = 1/√x`.
480    RsqrtInplace,
481    /// In-place [`OpKind::RecipElementwise`] — `x = 1/x`.
482    RecipInplace,
483    /// In-place [`OpKind::ExpElementwise`] — `x = exp(x)`.
484    ExpInplace,
485    /// In-place [`OpKind::LogElementwise`] — `x = ln(x)`.
486    LogInplace,
487    /// In-place [`OpKind::SinElementwise`] — `x = sin(x)`.
488    SinInplace,
489    /// In-place [`OpKind::CosElementwise`] — `x = cos(x)`.
490    CosInplace,
491    /// In-place [`OpKind::SignElementwise`] — `x = sign(x)`.
492    SignInplace,
493    /// In-place [`OpKind::FloorElementwise`] — `x = ⌊x⌋`.
494    FloorInplace,
495    /// In-place [`OpKind::CeilElementwise`] — `x = ⌈x⌉`.
496    CeilInplace,
497    /// In-place [`OpKind::RoundElementwise`] — `x = round(x)`.
498    RoundInplace,
499    /// In-place [`OpKind::ErfElementwise`] — `x = erf(x)`.
500    ErfInplace,
501    /// In-place [`OpKind::GeluElementwise`] — exact-GeLU
502    /// `x = 0.5 · x · (1 + erf(x/√2))`.
503    GeluInplace,
504    /// In-place [`OpKind::ClampElementwise`] — `x = clamp(x, min, max)`.
505    /// Scalar `(min, max)` flow through `OpParams::Clamp`.
506    ClampInplace,
507    /// In-place [`OpKind::PowIElementwise`] — `x = x.powi(exp)`.
508    /// Scalar `exp` flows through `OpParams::PowI`.
509    PowIInplace,
510    /// In-place [`OpKind::Affine`] — `x = mul · x + add`. The
511    /// `(mul, add)` coefficients flow through
512    /// `OpParams::Affine`; the kernel reads + writes the same
513    /// buffer. Single-input, single-output. Backs
514    /// `FusedOps::INPLACE_AFFINE`.
515    InplaceAffine,
516    /// Fused softmax + negative log-likelihood with integer class
517    /// targets — the standard PyTorch / Liger-Kernel training loss.
518    /// Inputs `[logits, targets]` where `logits: [n_rows, vocab]`
519    /// (F32, flattened from rank-N) and `targets: [n_rows]` (I64).
520    /// Output F32 — scalar for Mean/Sum reductions, `[n_rows]` for
521    /// None. Geometry + reduction + `ignore_index` flow through
522    /// `OpParams::FusedSoftmaxCrossEntropy`. Backs
523    /// `FusedOps::FUSED_SOFTMAX_CROSS_ENTROPY`.
524    FusedSoftmaxCrossEntropy,
525    /// Depthwise 1-D causal convolution + bias + optional fused SiLU.
526    /// Inputs `[x, weight, bias]` where
527    /// `x: [batch, channels, seq + kernel - 1]` (left-pre-padded by
528    /// caller), `weight: [channels, 1, kernel]`, `bias: [channels]`.
529    /// Output `[batch, channels, seq]`. Geometry + `use_silu` flow
530    /// through `OpParams::CausalConv1d`. Backs
531    /// `FusedOps::CAUSAL_CONV1D`
532    /// — the Mamba-1 / Mamba-2 prefill convolution fusion.
533    CausalConv1d,
534    /// Mamba-1's selective state-space scan (forward). Five inputs
535    /// `[u, delta, a, b, c]` — see
536    /// `FusedOps::SELECTIVE_SCAN`
537    /// for the full shape contract. Output `y: [batch, seqlen, dim]`.
538    /// Geometry + `delta_softplus` flow through
539    /// `OpParams::SelectiveScan`.
540    SelectiveScan,
541    /// Mamba-2's State-Space Duality chunked scan (forward). Five
542    /// inputs `[x, dt, a, b, c]` — see
543    /// `FusedOps::SSD_CHUNK_SCAN`
544    /// for the full shape contract. Output
545    /// `y: [batch, seqlen, heads, head_dim]`. Geometry + `chunk_size`
546    /// flow through `OpParams::SsdChunkScan`.
547    SsdChunkScan,
548    /// bitsandbytes-style 4-bit NormalFloat quantized matmul. Three
549    /// inputs `[activations, w_packed, absmax]` — see
550    /// `FusedOps::NF4_MATMUL`
551    /// for the full shape contract. Output `[..., M, N]` matches the
552    /// activations' dtype. Geometry + `block_size` flow through
553    /// `OpParams::Nf4Matmul`.
554    Nf4Matmul,
555    /// AWQ (mit-han-lab) asymmetric int4 weight-only matmul. Four inputs
556    /// `[activations, weight, zeros, scales]` in baracuda's W4A16 kernel layout
557    /// — see `FusedOps::AWQ_MATMUL` for the full shape contract. Output
558    /// `[..., M, N]` matches the activations' dtype. Geometry, `group_size` and
559    /// `split_k_iters` flow through `OpParams::AwqMatmul`.
560    AwqMatmul,
561    /// Matmul with a per-row scale epilogue (ROADMAP row 172 item 1). Inputs
562    /// `[a, b, row_scale]`, `row_scale` shaped `[..., M, 1]`; output
563    /// `[..., M, N] = (a @ b) * row_scale`. Reuses `OpParams::Matmul` for shape.
564    /// See `FusedOps::ROW_SCALED_MATMUL`.
565    RowScaledMatMul,
566    /// The `k` largest values along the last axis and their `U32` indices
567    /// (ROADMAP row 172 item 2). One input `[.., n]`, a bundled two-slot output
568    /// `[values .. k ; indices .. k]`; geometry rides in `OpParams::TopKLastDim`.
569    /// See `FusedOps::TOP_K_LAST_DIM`.
570    TopKLastDim,
571    /// `TopKLastDim(a @ b)` as one node (ROADMAP row 172 item 2 step B). Inputs
572    /// `[a, b]` (equal batch dims), the bundled TopK output over the matmul's
573    /// last axis; geometry rides in `OpParams::MatMulTopK`. See
574    /// `FusedOps::MATMUL_TOP_K`.
575    MatMulTopK,
576    /// A **runtime-registered** (Tier-2, JIT-synthesized / import-time) fused
577    /// op. Carries no static kernel family: the concrete op is named by the
578    /// runtime `FusedOpId` on the graph node, and its kernel resolves from the
579    /// runtime sidecar (`fuel_dispatch::runtime_fused_kernels`), never the
580    /// `OpKind`-keyed binding table. Exists so a `CompiledNode` for such an op
581    /// has an honest `op` tag; no binding-table registration may use it.
582    RuntimeFused,
583    /// Data-determined nonzero-index extraction — the keystone primitive
584    /// for data-dependent dynamic shapes. One input `x`; one bundled
585    /// output `[indices [capacity] U32 ; count [1] U32]`. Geometry +
586    /// `count_sym` flow through `OpParams::NonZeroIndices`. See
587    /// `Op::NonZeroIndices`.
588    NonZeroIndices,
589}
590
591impl OpKind {
592    pub fn as_str(self) -> &'static str {
593        match self {
594            OpKind::MatMul => "matmul",
595            OpKind::AddElementwise => "add",
596            OpKind::SubElementwise => "sub",
597            OpKind::MulElementwise => "mul",
598            OpKind::DivElementwise => "div",
599            OpKind::ReluElementwise => "relu",
600            OpKind::NegElementwise => "neg",
601            OpKind::SqrElementwise => "sqr",
602            OpKind::SqrtElementwise => "sqrt",
603            OpKind::RecipElementwise => "recip",
604            OpKind::AbsElementwise => "abs",
605            OpKind::TanhElementwise => "tanh",
606            OpKind::ExpElementwise => "exp",
607            OpKind::LogElementwise => "log",
608            OpKind::SinElementwise => "sin",
609            OpKind::CosElementwise => "cos",
610            OpKind::SigmoidElementwise => "sigmoid",
611            OpKind::SiluElementwise => "silu",
612            OpKind::GeluTanhElementwise => "gelu_tanh",
613            OpKind::StepElementwise => "step",
614            OpKind::SumReduce => "sum_reduce",
615            OpKind::MaxReduce => "max_reduce",
616            OpKind::MinReduce => "min_reduce",
617            OpKind::MeanReduce => "mean_reduce",
618            OpKind::Cast => "cast",
619            OpKind::Conv2D => "conv2d",
620            OpKind::ConvTranspose2D => "conv_transpose2d",
621            OpKind::ReduceSumTo => "reduce_sum_to",
622            OpKind::ReduceMaxTo => "reduce_max_to",
623            OpKind::FusedLinear => "fused_linear",
624            OpKind::FlashAttn => "flash_attn",
625            OpKind::FlashAttnBackwardQ => "flash_attn_backward_q",
626            OpKind::FlashAttnBackwardK => "flash_attn_backward_k",
627            OpKind::FlashAttnBackwardV => "flash_attn_backward_v",
628            OpKind::PagedAttn => "paged_attn",
629            OpKind::Affine => "affine",
630            OpKind::ClampElementwise => "clamp",
631            OpKind::PowIElementwise => "powi",
632            OpKind::PowIElementwiseBackward => "powi_backward",
633            OpKind::MaximumElementwise => "maximum",
634            OpKind::MinimumElementwise => "minimum",
635            OpKind::EqualElementwise => "eq",
636            OpKind::NotEqualElementwise => "ne",
637            OpKind::LessElementwise => "lt",
638            OpKind::LessEqualElementwise => "le",
639            OpKind::GreaterElementwise => "gt",
640            OpKind::GreaterEqualElementwise => "ge",
641            OpKind::Where => "where",
642            OpKind::FloorElementwise => "floor",
643            OpKind::CeilElementwise => "ceil",
644            OpKind::RoundElementwise => "round",
645            OpKind::SignElementwise => "sign",
646            OpKind::ErfElementwise => "erf",
647            OpKind::GeluElementwise => "gelu",
648            OpKind::PowElementwise => "pow",
649            OpKind::RsqrtElementwise => "rsqrt",
650            OpKind::RemElementwise => "rem",
651            OpKind::Flip => "flip",
652            OpKind::Roll => "roll",
653            OpKind::CumSum => "cumsum",
654            OpKind::Pad => "pad",
655            OpKind::PadBackward => "pad_backward",
656            OpKind::Triu => "triu",
657            OpKind::Tril => "tril",
658            OpKind::LogSoftmaxLastDim => "log_softmax_last_dim",
659            OpKind::LogSoftmaxLastDimBackward => "log_softmax_last_dim_backward",
660            OpKind::MaskedFill => "masked_fill",
661            OpKind::Concat => "concat",
662            OpKind::SoftmaxLastDim => "softmax_last_dim",
663            OpKind::SoftmaxLastDimBackward => "softmax_last_dim_backward",
664            OpKind::RmsNormLastDim => "rms_norm_last_dim",
665            OpKind::RmsNormLastDimBackward => "rms_norm_last_dim_backward",
666            OpKind::LayerNormLastDim => "layer_norm_last_dim",
667            OpKind::LayerNormLastDimBackward => "layer_norm_last_dim_backward",
668            OpKind::ReduceMaxToBackward => "reduce_max_to_backward",
669            OpKind::IndexSelect => "index_select",
670            OpKind::Gather => "gather",
671            OpKind::Rope => "rope",
672            OpKind::IndexAdd => "index_add",
673            OpKind::ScatterAdd => "scatter_add",
674            OpKind::ArgMaxDim => "argmax_dim",
675            OpKind::ArgMinDim => "argmin_dim",
676            OpKind::QMatMul => "qmatmul",
677            OpKind::WriteSlice => "write_slice",
678            OpKind::WriteSliceRotating => "write_slice_rotating",
679            OpKind::WriteSliceDoff => "write_slice_doff",
680            OpKind::Copy => "copy",
681            OpKind::ReluInplace => "relu_inplace",
682            OpKind::SiluInplace => "silu_inplace",
683            OpKind::GeluTanhInplace => "gelu_tanh_inplace",
684            OpKind::TanhInplace => "tanh_inplace",
685            OpKind::SigmoidInplace => "sigmoid_inplace",
686            OpKind::NegInplace => "neg_inplace",
687            OpKind::AbsInplace => "abs_inplace",
688            OpKind::SqrInplace => "sqr_inplace",
689            OpKind::SqrtInplace => "sqrt_inplace",
690            OpKind::RsqrtInplace => "rsqrt_inplace",
691            OpKind::RecipInplace => "recip_inplace",
692            OpKind::ExpInplace => "exp_inplace",
693            OpKind::LogInplace => "log_inplace",
694            OpKind::SinInplace => "sin_inplace",
695            OpKind::CosInplace => "cos_inplace",
696            OpKind::SignInplace => "sign_inplace",
697            OpKind::FloorInplace => "floor_inplace",
698            OpKind::CeilInplace => "ceil_inplace",
699            OpKind::RoundInplace => "round_inplace",
700            OpKind::ErfInplace => "erf_inplace",
701            OpKind::GeluInplace => "gelu_inplace",
702            OpKind::ClampInplace => "clamp_inplace",
703            OpKind::PowIInplace => "powi_inplace",
704            OpKind::InplaceAffine => "inplace_affine",
705            OpKind::FusedSoftmaxCrossEntropy => "fused_softmax_cross_entropy",
706            OpKind::CausalConv1d => "causal_conv1d",
707            OpKind::SelectiveScan => "selective_scan",
708            OpKind::SsdChunkScan => "ssd_chunk_scan",
709            OpKind::Nf4Matmul => "nf4_matmul",
710            OpKind::AwqMatmul => "awq_matmul",
711            OpKind::RowScaledMatMul => "row_scaled_matmul",
712            OpKind::TopKLastDim => "top_k_last_dim",
713            OpKind::MatMulTopK => "matmul_top_k",
714            OpKind::RuntimeFused => "runtime_fused",
715            OpKind::NonZeroIndices => "nonzero_indices",
716        }
717    }
718}
719
720impl std::fmt::Display for OpKind {
721    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
722        f.write_str(self.as_str())
723    }
724}
725
726/// Dispatch size bucket. One `u32` field carries two representations,
727/// disambiguated by the [`OpKind`] that always sits beside it in the
728/// dispatch key (a matmul key is only ever compared to matmul keys):
729///
730/// - **Total-elements key** (every non-matmul op — elementwise,
731///   reduce, cast, …): the value is `log2(total_elements)`. A 256×256
732///   elementwise tensor (65,536 elements) → `16`; a 1024×1024
733///   (1,048,576) → `20`. Built by [`SizeClass::from_elem_count`].
734///   Two shapes that round to the same bucket share a profile entry.
735///
736/// - **MatMul aspect key**: matmul cost is aspect-dependent — a
737///   `[1,K]×[K,N]` GEMV is bandwidth-bound, a square `[S,S]×[S,S]`
738///   GEMM of the *same output size* is compute-bound — so
739///   `log2(total_elements)` alone can't tell them apart. A matmul
740///   packs all three dims: `(log2(m·n) << 16) | (log2(m) << 8) |
741///   log2(k)`, read from `lhs=[…,m,k]` / `rhs=[…,k,n]`. `log2(m·n)`
742///   occupies the high byte so [`DispatchTable::pick_nearest`]'s
743///   magnitude ordering still tracks output size, while the low bytes
744///   distinguish aspect (a GEMV and a same-output-size square never
745///   collide, because their `log2(m)` bytes differ). Built by
746///   [`SizeClass::matmul`].
747///
748/// **Producer/consumer single-sourcing.** The `fuel-core` Judge
749/// (producer) and every consumer (the `fuel-dispatch` ranker at
750/// realize time) derive the key through the SAME helpers —
751/// [`SizeClass::matmul`] from native `(m,n,k)` on the producer side,
752/// [`SizeClass::for_op`] from the operand shapes on the consumer side.
753/// A given matmul shape therefore maps to one identical key on both
754/// sides, so a cell the Judge profiles is found by the ranker at
755/// dispatch time (the bug this repr fixes: the old scalar key had the
756/// producer on `m·n` and the consumer on `m·k`, so non-square matmul
757/// lookups always missed).
758#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
759#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
760pub struct SizeClass(pub u32);
761
762impl SizeClass {
763    /// Bucket a raw element count as `log2(n)` — the total-elements key
764    /// used by every non-matmul op. `n == 0` clamps to `1` (→ `0`).
765    pub fn from_elem_count(n: usize) -> Self {
766        let n = n.max(1);
767        let log2 = 63 - (n as u64).leading_zeros();
768        SizeClass(log2)
769    }
770
771    /// Aspect-carrying dispatch key for a matmul `[m,k] × [k,n]`.
772    ///
773    /// THE single source of matmul-key derivation: both the Judge
774    /// (producer, from its native `(m,n,k)`) and the ranker (consumer,
775    /// via [`SizeClass::for_op`] from operand shapes) call this, so both
776    /// sides agree for every shape. Packs `(log2(m·n) << 16) |
777    /// (log2(m) << 8) | log2(k)`; each `log2` fits a byte (≤ 63 for any
778    /// `usize`). A `[1,K]×[K,N]` GEMV (`m = 1`) never collides with a
779    /// square GEMM of equal output size — the `log2(m)` byte differs.
780    pub fn matmul(m: usize, n: usize, k: usize) -> Self {
781        let mn = Self::from_elem_count(m.saturating_mul(n)).0 & 0xff;
782        let ml = Self::from_elem_count(m).0 & 0xff;
783        let kl = Self::from_elem_count(k).0 & 0xff;
784        SizeClass((mn << 16) | (ml << 8) | kl)
785    }
786
787    /// Aspect-carrying dispatch key for a decode attention op
788    /// ([`OpKind::FlashAttn`]): `q = [B, Hq, Sq, D]`, K/V attended
789    /// length `k_len`, head dim `d`.
790    ///
791    /// THE single source of attention-key derivation, mirroring
792    /// [`SizeClass::matmul`]: both the Judge (producer, from its native
793    /// `OpSize::Attention` `(hq, k_len, d)`) and the flash-vs-decomposed
794    /// bake (consumer, via [`SizeClass::for_op`] from the flash node's
795    /// operand shapes) call this, so both sides agree for every shape.
796    /// Packs `(log2(hq·k_len·d) << 16) | (log2(k_len) << 8) | log2(d)`:
797    /// the high 16 bits track total attention work (`O(Hq·k_len·D)` for
798    /// the QKᵀ + PV matmuls at seq_q=1), so [`DispatchTable::pick_nearest`]'s
799    /// magnitude ordering still follows problem size, while the low bytes
800    /// carry the `k_len` (context-length / bandwidth regime) and head-dim
801    /// aspects — so two decode shapes that differ only in `k_len` (short
802    /// vs capacity context) never collide onto one cell. Each `log2` fits
803    /// a byte (≤ 63 for any `usize`).
804    pub fn attention(hq: usize, k_len: usize, d: usize) -> Self {
805        let work = hq.saturating_mul(k_len).saturating_mul(d);
806        let mag = Self::from_elem_count(work).0 & 0xff;
807        let kl = Self::from_elem_count(k_len).0 & 0xff;
808        let dl = Self::from_elem_count(d).0 & 0xff;
809        SizeClass((mag << 16) | (kl << 8) | dl)
810    }
811
812    /// Derive the dispatch key for `op` from its input operand shapes —
813    /// the single entry point every consumer uses. A [`OpKind::MatMul`]
814    /// reads `(m,n,k)` from `lhs=[…,m,k]` / `rhs=[…,k,n]` (trailing two
815    /// dims; leading batch dims don't affect the key) and keys via
816    /// [`SizeClass::matmul`]; every other op keys on the first operand's
817    /// total element count via [`SizeClass::from_elem_count`]. Empty
818    /// shapes (nullary op) → `SizeClass(0)`.
819    pub fn for_op(op: OpKind, input_shapes: &[Shape]) -> Self {
820        if op == OpKind::MatMul
821            && let (Some(lhs), Some(rhs)) = (input_shapes.first(), input_shapes.get(1))
822        {
823            let (ld, rd) = (lhs.dims(), rhs.dims());
824            if ld.len() >= 2 && rd.len() >= 2 {
825                let m = ld[ld.len() - 2];
826                let k = ld[ld.len() - 1];
827                let n = rd[rd.len() - 1];
828                return Self::matmul(m, n, k);
829            }
830        }
831        if op == OpKind::FlashAttn {
832            // Operand layout: q = [B, Hq, Sq, D], k = [B, Hkv, k_len, D]
833            // (v matches k). Read `hq`/`d` from q and the attended length
834            // `k_len` from k's length axis — the leading B batch dim and
835            // k's Hkv don't affect the key (mirrors matmul's batch-blind
836            // trailing-dim read), so a 2D per-head profile and the batched
837            // op agree.
838            if let (Some(q), Some(k)) = (input_shapes.first(), input_shapes.get(1)) {
839                let (qd, kd) = (q.dims(), k.dims());
840                if qd.len() >= 4 && kd.len() >= 2 {
841                    let hq = qd[1];
842                    let d = qd[qd.len() - 1];
843                    let k_len = kd[kd.len() - 2];
844                    return Self::attention(hq, k_len, d);
845                }
846            }
847        }
848        match input_shapes.first() {
849            Some(s) => Self::from_elem_count(s.elem_count()),
850            None => SizeClass(0),
851        }
852    }
853}
854
855/// Single (op_kind, dtype, size_class) × (backend, device_index,
856/// kernel_source) datum produced by one measurement run.
857///
858/// **Per-alternative measurement (2026-06-08)**: `kernel_source`
859/// distinguishes kernels that register at the same
860/// `(op, dtypes, backend)` decision point in the binding table —
861/// AOCL vs MKL vs portable-cpu, cuBLAS vs CUTLASS, etc. Each
862/// sibling alternative produces its own [`ProfileEntry`]; the
863/// dispatch table picks the best entry per cell across both
864/// backends and kernel_sources. Default empty string preserves the
865/// pre-v2 shape for kernels that don't need to distinguish.
866#[derive(Debug, Clone, PartialEq)]
867#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
868pub struct ProfileEntry {
869    pub op: OpKind,
870    pub dtype: DType,
871    pub size_class: SizeClass,
872    pub backend: BackendId,
873    pub device_index: u32,
874    /// Median wall-clock time per invocation over `iterations`.
875    pub latency_ns: u64,
876    /// Number of timed iterations that produced `latency_ns`.
877    pub iterations: u32,
878    /// Max relative element-wise error vs the reference backend's
879    /// output on the same input.
880    pub max_rel_error: f32,
881    /// Diagnostic tag identifying which kernel sibling produced this
882    /// measurement when multiple alternatives register at the same
883    /// `(op, dtypes, backend)` binding-table key. Mirrors
884    /// `BindingEntry::kernel_source` (in `fuel-dispatch`). `""` is
885    /// the pre-v2 default for kernels that don't need to distinguish;
886    /// conventional values include `"portable-cpu"`, `"aocl"`,
887    /// `"mkl"`, `"cublas"`, `"cutlass"`.
888    ///
889    /// Owned `String` (not `&'static str`) so the field round-trips
890    /// through serde — deserialized reports can't carry borrowed
891    /// data with a `'static` lifetime. The producer-side `Judge`
892    /// converts from `BindingEntry::kernel_source: &'static str`
893    /// via `.to_string()` at measurement time.
894    #[cfg_attr(feature = "serde", serde(default))]
895    pub kernel_source: String,
896    /// Content-derived revision of the exact kernel build that produced
897    /// this measurement. Mirrors `BindingEntry::kernel_revision_hash`
898    /// (`fuel-dispatch`) and the FKC ledger key's `kernel_revision_hash`;
899    /// with `kernel_source` it completes Fuel's canonical kernel identity
900    /// `ImplId = (backend, op, dtypes, kernel_source, kernel_revision_hash)`.
901    ///
902    /// `kernel_source` is provider-granular (`"cublas"`, `"baracuda"`, …);
903    /// `kernel_revision_hash` is *variant*-granular — forty tilings of one
904    /// provider emit forty distinct source strings and forty distinct
905    /// content-derived hashes, so they no longer collapse into one Judge
906    /// cell. `0` means "untracked" (the pre-v5 default and the value test
907    /// fixtures use when the axis is irrelevant); the Judge keys on it, so
908    /// two DISTINCT non-zero revisions occupy two distinct cells.
909    #[cfg_attr(feature = "serde", serde(default))]
910    pub kernel_revision_hash: u64,
911    /// The hardware identity this measurement was taken on — the
912    /// [`EquivalenceKey`](crate::probe::EquivalenceKey) of the profiled
913    /// device: `(backend, vendor_id, device_id, compute_capability,
914    /// driver_version)`. A `ProfileReport` is one Judge run on ONE machine.
915    /// `device_index` above is a within-run ORDINAL that rebinds across boots
916    /// and under `CUDA_VISIBLE_DEVICES`, so it is NOT a portable identity —
917    /// worse than none, because it answers confidently for a different
918    /// physical device. The ordinal is retained for within-run indexing; this
919    /// field is what `ProfileReport::load` validates against the loading
920    /// machine's hardware, so a report written on a 4070 Laptop is REFUSED on
921    /// a Radeon VII instead of loading and mis-ranking. `backend` here always
922    /// equals the sibling `backend` field above (checked on ingest).
923    ///
924    /// `#[serde(default)]` supplies a sentinel identity ONLY so a pre-v5
925    /// report — which lacks this field — still deserializes and is then
926    /// rejected by the version gate; the sentinel matches no real device (see
927    /// `default_device_identity`).
928    #[cfg_attr(feature = "serde", serde(default = "default_device_identity"))]
929    pub device: crate::probe::EquivalenceKey,
930}
931
932/// Sentinel hardware identity for the `#[serde(default)]` on
933/// [`ProfileEntry::device`]. Reached ONLY on the pre-v5 deserialize path (a
934/// v4 report lacks the field → deserializes with this → the version gate then
935/// returns `Ok(None)` before it is ever consulted). Deliberately matches NO
936/// real device — an `driver_version` string no backend reports — so that if a
937/// future reordering ever let it past the version gate, it would refuse every
938/// load (loud) rather than silently match the wrong hardware. Applied by hand
939/// rather than a blanket `Default`, precisely so the value is a deliberate
940/// refuse-everything sentinel and not a zero that could coincidentally match.
941#[cfg(feature = "serde")]
942fn default_device_identity() -> crate::probe::EquivalenceKey {
943    crate::probe::EquivalenceKey {
944        backend: BackendId::Cpu,
945        vendor_id: 0,
946        device_id: 0,
947        compute_capability: None,
948        driver_version: "<unset: pre-v5 profile, rejected by the version gate>".to_string(),
949    }
950}
951
952/// A persistable table of every profile measurement the Judge
953/// produced in one run.
954#[derive(Debug, Clone, PartialEq)]
955#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
956pub struct ProfileReport {
957    pub version: u32,
958    pub entries: Vec<ProfileEntry>,
959}
960
961#[cfg(feature = "serde")]
962impl ProfileReport {
963    /// Atomic write to `path` as JSON (sibling `.tmp` + rename).
964    pub fn save(&self, path: &std::path::Path) -> crate::Result<()> {
965        let json = serde_json::to_vec_pretty(self)
966            .map_err(|e| crate::Error::Msg(format!("judge: JSON encode failed: {e}")))?;
967        let tmp = path.with_extension("tmp");
968        std::fs::write(&tmp, &json)
969            .map_err(|e| crate::Error::Msg(format!("judge: write {tmp:?} failed: {e}")))?;
970        std::fs::rename(&tmp, path).map_err(|e| {
971            crate::Error::Msg(format!("judge: rename {tmp:?} → {path:?} failed: {e}"))
972        })?;
973        Ok(())
974    }
975
976    /// Load a previously-persisted report, validated against the hardware of
977    /// the loading machine. `current` is that machine's device equivalence
978    /// classes (via `crate::probe::equivalence_classes`); requiring it in the
979    /// signature makes it impossible to load without declaring what hardware
980    /// you are on — the gate cannot be forgotten by a caller.
981    ///
982    /// Returns:
983    /// - `Ok(None)` on a missing file.
984    /// - `Ok(None)` on a schema-version mismatch **that still deserializes**
985    ///   (an additive change: the pre-v5 fields `#[serde(default)]` in, then
986    ///   this version gate rejects the report).
987    /// - `Ok(None)` on FOREIGN HARDWARE. A `ProfileReport` is the output of ONE
988    ///   Judge run on ONE machine, so if ANY entry's `device` identity is not
989    ///   among `current`, the run happened elsewhere and the WHOLE report is
990    ///   refused — the entries that do match are not a valid subset, they are a
991    ///   foreign report with coincidental overlap, and the `device_index` it
992    ///   was keyed by rebinds across machines.
993    /// - `Err` on a NON-ADDITIVE schema change that fails to PARSE. Such a
994    ///   change never reaches the version gate above, because deserialization
995    ///   fails first — which is exactly why every new field carries
996    ///   `#[serde(default)]`. A future "check the version first, it's cheaper"
997    ///   cleanup MUST preserve `Ok(None)` for a deserializable stale report;
998    ///   `v4_report_yields_no_oracle_entries` pins that invariant.
999    ///
1000    /// All three `Ok(None)` cases are "cache miss, re-run the Judge" signals.
1001    pub fn load(
1002        path: &std::path::Path,
1003        current: &[crate::probe::EquivalenceKey],
1004    ) -> crate::Result<Option<Self>> {
1005        let bytes = match std::fs::read(path) {
1006            Ok(b) => b,
1007            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
1008            Err(e) => {
1009                return Err(crate::Error::Msg(format!(
1010                    "judge: read {path:?} failed: {e}"
1011                )));
1012            }
1013        };
1014        let report: Self = serde_json::from_slice(&bytes)
1015            .map_err(|e| crate::Error::Msg(format!("judge: parse {path:?} failed: {e}")))?;
1016        if report.version != PROFILE_REPORT_VERSION {
1017            return Ok(None);
1018        }
1019        // Whole-report hardware validation: one run, one machine. Any entry
1020        // measured on a device this machine does not have means the report is
1021        // foreign — refuse it entirely rather than serve a coincidental subset.
1022        if report.entries.iter().any(|e| !current.contains(&e.device)) {
1023            return Ok(None);
1024        }
1025        Ok(Some(report))
1026    }
1027}
1028
1029/// A selection criterion — what "best" means for a lookup.
1030#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1031#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1032pub enum Criterion {
1033    /// Lowest median latency.
1034    Fastest,
1035    /// Lowest max relative error vs the reference backend.
1036    MostAccurate,
1037    /// Weighted blend — lower is better.
1038    Balanced,
1039}
1040
1041impl Criterion {
1042    pub fn as_str(self) -> &'static str {
1043        match self {
1044            Criterion::Fastest => "fastest",
1045            Criterion::MostAccurate => "accurate",
1046            Criterion::Balanced => "balanced",
1047        }
1048    }
1049}
1050
1051impl std::fmt::Display for Criterion {
1052    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1053        f.write_str(self.as_str())
1054    }
1055}
1056
1057/// Default weight applied to `max_rel_error` in the Balanced
1058/// criterion's cost function.
1059pub const DEFAULT_ACCURACY_PENALTY: f64 = 100.0;
1060
1061/// Lookup key into [`DispatchTable`]. Combines the per-op axes
1062/// (what + on what data + how big) with the user's criterion.
1063#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1064struct DispatchKey {
1065    op: OpKind,
1066    dtype: DType,
1067    size_class: SizeClass,
1068    criterion: Criterion,
1069}
1070
1071/// Where the dispatch table decided an op should run, and which
1072/// kernel sibling won at that decision point.
1073///
1074/// **Per-alternative measurement (2026-06-08)**: `kernel_source`
1075/// surfaces the winning [`ProfileEntry::kernel_source`] so
1076/// consumers (Router, planner, telemetry) can name the actual
1077/// kernel — critical when multiple alternatives register at
1078/// `(BackendId::Cpu)` (e.g. AOCL vs MKL vs portable-cpu) and the
1079/// (backend, device) pair alone is ambiguous. `""` for legacy
1080/// reports / single-impl cells.
1081///
1082/// `Pick` stays `Copy` (and `&'static str` is `Copy`); the picker
1083/// preserves the string identity by reading it directly from the
1084/// winning `ProfileEntry` — its `String` value is matched against
1085/// the well-known interned set via [`kernel_source_intern`] so the
1086/// `&'static str` is the same pointer the binding-table entry
1087/// carries.
1088///
1089/// `Pick` is only ever constructed at runtime by `DispatchTable::rebuild_from`
1090/// from in-memory [`ProfileEntry`] data — never deserialized from JSON
1091/// or other owned input. We therefore intentionally do NOT derive
1092/// `Deserialize` (the `&'static str` field can't be deserialized from
1093/// owned input). `Serialize` is also dropped for symmetry; the public
1094/// serialization surface is [`ProfileReport`] / [`ProfileEntry`], and
1095/// `Pick` is regenerated by calling [`DispatchTable::build`].
1096#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1097pub struct Pick {
1098    pub backend: BackendId,
1099    pub device_index: u32,
1100    /// Diagnostic tag from the winning [`ProfileEntry::kernel_source`].
1101    /// `""` when no sibling distinguishes the cell.
1102    pub kernel_source: &'static str,
1103    /// Revision of the winning kernel build, from
1104    /// [`ProfileEntry::kernel_revision_hash`]. Completes the kernel
1105    /// identity that `kernel_source` alone (provider-granular) leaves
1106    /// ambiguous among the variant siblings of one provider. `0` when
1107    /// untracked. `u64` is `Copy`, so `Pick` stays `Copy`.
1108    pub kernel_revision_hash: u64,
1109}
1110
1111/// Intern a kernel-source string into a `&'static str`. Well-known conventional
1112/// tags return their static literal (the fast path); any OTHER non-empty tag is
1113/// interned through a process-static pool (leaked once per distinct string) so
1114/// it round-trips unchanged. The set mirrors the documented convention on
1115/// [`BindingEntry::kernel_source`](../../fuel_dispatch/struct.BindingEntry.html#structfield.kernel_source).
1116///
1117/// An unknown tag is NEVER collapsed to `""`. `""` is the pre-v2
1118/// "undifferentiated" marker, so returning it for a new provider's kernel makes
1119/// that kernel indistinguishable from an untagged one — a category error in a
1120/// field that keys persisted profiles (the router then cannot resolve
1121/// `(backend, device, "")` to the right same-backend sibling). A new provider's
1122/// tag is legitimate; preserve it. The bounded leak — one allocation per
1123/// distinct tag a process ever sees, and providers are few — is the right trade
1124/// for never losing kernel identity. (This reverses an earlier decision that
1125/// preferred dropping the tag over the leak; the collision cost was underpriced.)
1126///
1127/// SAFETY of the leak — an ASSUMPTION this code relies on but does not enforce:
1128/// it is bounded only because the tag SET is bounded, and the bound is a property
1129/// of the data path, not of the type. The input CAN be a deserialized `String`
1130/// (`ProfileEntry::kernel_source` comes from JSON), not only a compile-time
1131/// `&'static str`. Tags stay bounded because they derive from bindings registered
1132/// on THIS machine and `ProfileReport::load`'s hardware gate refuses foreign
1133/// reports, so a persisted profile cannot smuggle in arbitrary tags. What would
1134/// break it: a profile carrying unbounded distinct tags — an untrusted or
1135/// machine-generated one — would leak without bound, at which point this interner
1136/// needs a cap.
1137pub fn kernel_source_intern(s: &str) -> &'static str {
1138    match s {
1139        "" => "",
1140        "portable-cpu" => "portable-cpu",
1141        "aocl" => "aocl",
1142        "mkl" => "mkl",
1143        "cublas" => "cublas",
1144        "cutlass" => "cutlass",
1145        "baracuda" => "baracuda",
1146        "slang" => "slang",
1147        other => intern_unknown_kernel_source(other),
1148    }
1149}
1150
1151/// Intern an unfamiliar kernel-source tag into a stable `&'static str`, leaking
1152/// it exactly once. Bounded by the number of distinct tags a process ever sees
1153/// (a handful of providers), so the leak does not grow unboundedly. Preserving
1154/// the tag beats collapsing it to `""` — see [`kernel_source_intern`].
1155fn intern_unknown_kernel_source(s: &str) -> &'static str {
1156    use std::collections::HashSet;
1157    use std::sync::{Mutex, OnceLock};
1158    static POOL: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();
1159    let pool = POOL.get_or_init(|| Mutex::new(HashSet::new()));
1160    let mut set = pool.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
1161    if let Some(&existing) = set.get(s) {
1162        return existing;
1163    }
1164    let leaked: &'static str = Box::leak(s.to_owned().into_boxed_str());
1165    set.insert(leaked);
1166    leaked
1167}
1168
1169/// Options for building a [`DispatchTable`].
1170#[derive(Debug, Clone, Copy)]
1171pub struct DispatchOptions {
1172    pub accuracy_penalty: f64,
1173}
1174
1175impl Default for DispatchOptions {
1176    fn default() -> Self {
1177        Self {
1178            accuracy_penalty: DEFAULT_ACCURACY_PENALTY,
1179        }
1180    }
1181}
1182
1183impl DispatchOptions {
1184    pub fn with_balanced_penalty(mut self, k: f64) -> Self {
1185        self.accuracy_penalty = k;
1186        self
1187    }
1188}
1189
1190/// O(1) runtime dispatch table, constructed once from a
1191/// [`ProfileReport`] and then queried at realize time.
1192#[derive(Debug, Clone)]
1193pub struct DispatchTable {
1194    entries: HashMap<DispatchKey, Pick>,
1195    /// All size classes present for each `(op, dtype)` — sorted
1196    /// ascending so `pick_nearest` can do a linear scan for the
1197    /// closest profiled bucket.
1198    size_index: HashMap<(OpKind, DType), Vec<SizeClass>>,
1199    accuracy_penalty: f64,
1200}
1201
1202impl DispatchTable {
1203    /// Build a dispatch table from a profile report with default options.
1204    pub fn build(report: &ProfileReport) -> Self {
1205        Self::build_with(report, DispatchOptions::default())
1206    }
1207
1208    pub fn build_with(report: &ProfileReport, opts: DispatchOptions) -> Self {
1209        let mut tbl = Self {
1210            entries: HashMap::new(),
1211            size_index: HashMap::new(),
1212            accuracy_penalty: opts.accuracy_penalty,
1213        };
1214        tbl.rebuild_from(report);
1215        tbl
1216    }
1217
1218    fn rebuild_from(&mut self, report: &ProfileReport) {
1219        self.entries.clear();
1220        self.size_index.clear();
1221        let mut groups: HashMap<(OpKind, DType, SizeClass), Vec<&ProfileEntry>> = HashMap::new();
1222        for e in &report.entries {
1223            groups
1224                .entry((e.op, e.dtype, e.size_class))
1225                .or_default()
1226                .push(e);
1227        }
1228        for ((op, dtype, size_class), group) in &groups {
1229            for &criterion in &[
1230                Criterion::Fastest,
1231                Criterion::MostAccurate,
1232                Criterion::Balanced,
1233            ] {
1234                if let Some(winner) = self.pick_winner(group, criterion) {
1235                    let key = DispatchKey {
1236                        op: *op,
1237                        dtype: *dtype,
1238                        size_class: *size_class,
1239                        criterion,
1240                    };
1241                    self.entries.insert(
1242                        key,
1243                        Pick {
1244                            backend: winner.backend,
1245                            device_index: winner.device_index,
1246                            kernel_source: kernel_source_intern(&winner.kernel_source),
1247                            kernel_revision_hash: winner.kernel_revision_hash,
1248                        },
1249                    );
1250                }
1251            }
1252            self.size_index
1253                .entry((*op, *dtype))
1254                .or_default()
1255                .push(*size_class);
1256        }
1257        for classes in self.size_index.values_mut() {
1258            classes.sort_by_key(|s| s.0);
1259            classes.dedup();
1260        }
1261    }
1262
1263    fn pick_winner<'a>(
1264        &self,
1265        group: &[&'a ProfileEntry],
1266        crit: Criterion,
1267    ) -> Option<&'a ProfileEntry> {
1268        match crit {
1269            Criterion::Fastest => group.iter().copied().min_by_key(|e| e.latency_ns),
1270            Criterion::MostAccurate => group.iter().copied().min_by(|a, b| {
1271                a.max_rel_error
1272                    .total_cmp(&b.max_rel_error)
1273                    .then(a.latency_ns.cmp(&b.latency_ns))
1274            }),
1275            Criterion::Balanced => group.iter().copied().min_by(|a, b| {
1276                let sa =
1277                    a.latency_ns as f64 * (1.0 + self.accuracy_penalty * a.max_rel_error as f64);
1278                let sb =
1279                    b.latency_ns as f64 * (1.0 + self.accuracy_penalty * b.max_rel_error as f64);
1280                sa.total_cmp(&sb)
1281            }),
1282        }
1283    }
1284
1285    /// Exact lookup — returns `None` if the requested size class
1286    /// wasn't profiled.
1287    pub fn pick(
1288        &self,
1289        op: OpKind,
1290        dtype: DType,
1291        size_class: SizeClass,
1292        criterion: Criterion,
1293    ) -> Option<Pick> {
1294        self.entries
1295            .get(&DispatchKey {
1296                op,
1297                dtype,
1298                size_class,
1299                criterion,
1300            })
1301            .copied()
1302    }
1303
1304    /// Nearest-neighbour lookup. Ties go to the larger size class.
1305    pub fn pick_nearest(
1306        &self,
1307        op: OpKind,
1308        dtype: DType,
1309        size_class: SizeClass,
1310        criterion: Criterion,
1311    ) -> Option<Pick> {
1312        if let Some(p) = self.pick(op, dtype, size_class, criterion) {
1313            return Some(p);
1314        }
1315        let classes = self.size_index.get(&(op, dtype))?;
1316        if classes.is_empty() {
1317            return None;
1318        }
1319        let target = size_class.0 as i32;
1320        let nearest = classes.iter().min_by_key(|c| {
1321            let diff = (c.0 as i32 - target).abs();
1322            (diff, -(c.0 as i32))
1323        })?;
1324        self.pick(op, dtype, *nearest, criterion)
1325    }
1326
1327    pub fn len(&self) -> usize {
1328        self.entries.len()
1329    }
1330    pub fn is_empty(&self) -> bool {
1331        self.entries.is_empty()
1332    }
1333
1334    /// Every distinct `(op, dtype, size_class)` for which the table
1335    /// has at least one criterion entry. Sorted, stable across calls.
1336    pub fn keys(&self) -> Vec<(OpKind, DType, SizeClass)> {
1337        let mut seen: std::collections::HashSet<(OpKind, DType, SizeClass)> = Default::default();
1338        for k in self.entries.keys() {
1339            seen.insert((k.op, k.dtype, k.size_class));
1340        }
1341        let mut out: Vec<_> = seen.into_iter().collect();
1342        out.sort_by(|a, b| {
1343            a.0.as_str()
1344                .cmp(b.0.as_str())
1345                .then(format!("{:?}", a.1).cmp(&format!("{:?}", b.1)))
1346                .then(a.2.0.cmp(&b.2.0))
1347        });
1348        out
1349    }
1350}
1351
1352#[cfg(test)]
1353mod size_class_tests {
1354    use super::*;
1355
1356    /// The interner must NEVER collapse a non-empty unknown tag to `""` — `""`
1357    /// is the pre-v2 "undifferentiated" marker, so an unknown provider's tag
1358    /// coming back as `""` makes its kernel indistinguishable from an untagged
1359    /// one, in a field that keys persisted profiles. An ARBITRARY never-known
1360    /// tag must round-trip. Born-red: the pre-interner unknown arm `debug_assert`s
1361    /// and returns `""`, so this panics in a debug/test build.
1362    #[test]
1363    fn intern_preserves_an_arbitrary_unknown_tag() {
1364        // No provider registers this and it is not in the known set — the point
1365        // is that the floor holds for any FUTURE provider, not just a named one.
1366        let unknown = "some-future-provider-xyz";
1367        assert_eq!(
1368            kernel_source_intern(unknown),
1369            unknown,
1370            "an unknown tag must be preserved, never collapsed to \"\""
1371        );
1372        assert_ne!(kernel_source_intern(unknown), "");
1373        // Interning is idempotent: the same tag returns the same pointer.
1374        assert!(std::ptr::eq(
1375            kernel_source_intern(unknown),
1376            kernel_source_intern(unknown)
1377        ));
1378        // Known tags still fast-path to their literal; empty stays empty.
1379        assert_eq!(kernel_source_intern("slang"), "slang");
1380        assert_eq!(kernel_source_intern(""), "");
1381    }
1382
1383    /// The shared matmul key derivation gives producer == consumer for a
1384    /// NON-SQUARE matmul (SizeClass v4, slice 2.5).
1385    ///
1386    /// **Born-red**: before v4 `SizeClass` was `log2(total_elements)` and
1387    /// matmul keyed on a single scalar — the Judge (producer) on `m·n`,
1388    /// the ranker (consumer) on `m·k` (LHS elem count). For a non-square
1389    /// matmul those disagree, so the ranker never found the profiled
1390    /// cell. The `matmul` / `for_op` helpers now derive one identical
1391    /// key on both sides.
1392    #[test]
1393    fn matmul_key_producer_equals_consumer_non_square() {
1394        // FFN-width decode GEMV `[1,2048]×[2048,5632]`: m=1, k=2048, n=5632.
1395        let (m, n, k) = (1usize, 5632usize, 2048usize);
1396
1397        // Producer derives from its native (m, n, k).
1398        let producer = SizeClass::matmul(m, n, k);
1399        // Consumer derives from the operand shapes lhs=[m,k]/rhs=[k,n]
1400        // through the shared `for_op` entry point.
1401        let consumer = SizeClass::for_op(
1402            OpKind::MatMul,
1403            &[Shape::from_dims(&[m, k]), Shape::from_dims(&[k, n])],
1404        );
1405        assert_eq!(
1406            producer, consumer,
1407            "producer (native m,n,k) and consumer (operand shapes) must \
1408             derive one identical key",
1409        );
1410
1411        // Old-bug witness: the pre-v4 scalar keys the two sides used
1412        // (m·n vs m·k) disagree for this non-square shape.
1413        assert_ne!(
1414            SizeClass::from_elem_count(m * n), // old producer key
1415            SizeClass::from_elem_count(m * k), // old consumer key
1416            "the non-square shape is exactly where the old scalar keys \
1417             diverged",
1418        );
1419    }
1420
1421    /// Batched matmul keys the same as its per-head 2D shape — `for_op`
1422    /// reads only the trailing two dims, so a Judge that profiles the 2D
1423    /// per-head GEMV and a consumer costing the batched op agree.
1424    #[test]
1425    fn matmul_key_batched_ignores_leading_dims() {
1426        let per_head = SizeClass::for_op(
1427            OpKind::MatMul,
1428            &[Shape::from_dims(&[1, 64]), Shape::from_dims(&[64, 128])],
1429        );
1430        let batched = SizeClass::for_op(
1431            OpKind::MatMul,
1432            &[
1433                Shape::from_dims(&[32, 1, 64]),
1434                Shape::from_dims(&[32, 64, 128]),
1435            ],
1436        );
1437        assert_eq!(per_head, batched);
1438        assert_eq!(per_head, SizeClass::matmul(1, 128, 64));
1439    }
1440
1441    /// A bandwidth-bound GEMV and a compute-bound square GEMM of the SAME
1442    /// output size key to DISTINCT buckets — the aspect distinction.
1443    /// Under the old scalar `log2(m·n)` key they collided.
1444    #[test]
1445    fn gemv_and_same_output_square_are_distinct() {
1446        // GEMV `[1,2048]×[2048,4096]` → output m·n = 4096.
1447        let gemv = SizeClass::matmul(1, 4096, 2048);
1448        // Square `[64,64]×[64,64]` → output m·n = 4096 — SAME total.
1449        let square = SizeClass::matmul(64, 64, 64);
1450
1451        // Old scalar key: identical (both log2(4096) = 12) → collision.
1452        // `1 * 4096` is the GEMV output 1·4096, written to mirror `64 * 64`
1453        // (square output 64·64) so the two colliding shapes read in parallel —
1454        // the `1 *` is the GEMV's `m` dim, not a no-op. Stripping it would
1455        // destroy the shape structure the test documents, so identity_op is
1456        // wrong here.
1457        #[allow(clippy::identity_op)] // `1 *` is the GEMV `m` dim, kept for parallel with `64 * 64`
1458        let gemv_elems = 1 * 4096; // [1, 4096]
1459        let square_elems = 64 * 64; // [64, 64]
1460        assert_eq!(
1461            SizeClass::from_elem_count(gemv_elems),
1462            SizeClass::from_elem_count(square_elems),
1463            "sanity: equal output size → the OLD scalar keys collided",
1464        );
1465        // v4 aspect key: distinct (the log2(m) byte differs, 0 vs 6).
1466        assert_ne!(
1467            gemv, square,
1468            "a bandwidth-bound GEMV must not share a dispatch cell with a \
1469             compute-bound square of equal output size",
1470        );
1471    }
1472
1473    /// Non-matmul ops are untouched: `for_op` keys on the first
1474    /// operand's total element count, identical to `from_elem_count`.
1475    #[test]
1476    fn non_matmul_keeps_total_elements_key() {
1477        let sc = SizeClass::for_op(OpKind::AddElementwise, &[Shape::from_dims(&[1024])]);
1478        assert_eq!(sc, SizeClass::from_elem_count(1024));
1479        assert_eq!(sc.0, 10); // log2(1024)
1480    }
1481
1482    /// FlashAttn: the shared `attention` key derivation gives
1483    /// producer == consumer (Judge Layer-2 coverage arc, slice 3).
1484    ///
1485    /// The Judge (producer) keys a decode-attention cell via
1486    /// `SizeClass::attention(hq, k_len, d)` from its native
1487    /// `OpSize::Attention`; slice-4's bake (consumer) keys via
1488    /// `SizeClass::for_op(FlashAttn, [q_shape, k_shape])` from the flash
1489    /// node's operand shapes. Both must derive one identical key so the
1490    /// bake's lookup HITS the profiled cell.
1491    #[test]
1492    fn attention_key_producer_equals_consumer() {
1493        // TinyLlama-ish decode: Hq=32, Hkv=4, D=64, k_len=128, seq_q=1.
1494        let (b, hq, hkv, d, k_len) = (1usize, 32usize, 4usize, 64usize, 128usize);
1495
1496        // Producer derives from its native (hq, k_len, d).
1497        let producer = SizeClass::attention(hq, k_len, d);
1498        // Consumer derives from the flash node's operand shapes
1499        // q=[B,Hq,Sq,D] / k=[B,Hkv,k_len,D] through the shared `for_op`.
1500        let consumer = SizeClass::for_op(
1501            OpKind::FlashAttn,
1502            &[
1503                Shape::from_dims(&[b, hq, 1, d]),
1504                Shape::from_dims(&[b, hkv, k_len, d]),
1505                Shape::from_dims(&[b, hkv, k_len, d]),
1506            ],
1507        );
1508        assert_eq!(
1509            producer, consumer,
1510            "producer (native hq,k_len,d) and consumer (operand shapes) must \
1511             derive one identical attention key",
1512        );
1513    }
1514
1515    /// Two decode attention shapes that differ only in `k_len` (short
1516    /// vs capacity context) must key to DISTINCT buckets — the oracle
1517    /// keeps the MIN latency across a colliding key, which would let the
1518    /// short-context cell poison the capacity cell's cost.
1519    #[test]
1520    fn attention_decode_shapes_distinct_by_klen() {
1521        let small = SizeClass::attention(32, 128, 64);
1522        let cap = SizeClass::attention(32, 4096, 64);
1523        assert_ne!(
1524            small, cap,
1525            "short-context and capacity-context decode attention must not \
1526             share a dispatch cell",
1527        );
1528    }
1529
1530    /// `for_op(FlashAttn)` reads `hq`/`d` from q and `k_len` from k's
1531    /// length axis; the leading batch dim and k's Hkv (GQA) don't affect
1532    /// the key — so a per-head 2D-ish profile and the batched op agree,
1533    /// exactly like the matmul batch-blind trailing-dim read.
1534    #[test]
1535    fn attention_key_ignores_batch_and_hkv() {
1536        let base = SizeClass::for_op(
1537            OpKind::FlashAttn,
1538            &[
1539                Shape::from_dims(&[1, 32, 1, 64]),
1540                Shape::from_dims(&[1, 4, 128, 64]),
1541            ],
1542        );
1543        // Different B and different Hkv, same Hq/k_len/D → same key.
1544        let other = SizeClass::for_op(
1545            OpKind::FlashAttn,
1546            &[
1547                Shape::from_dims(&[8, 32, 1, 64]),
1548                Shape::from_dims(&[8, 8, 128, 64]),
1549            ],
1550        );
1551        assert_eq!(base, other);
1552        assert_eq!(base, SizeClass::attention(32, 128, 64));
1553    }
1554}