Skip to main content

fuel_dispatch/
kernel.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Kernel reference + op parameters + binding table. Phase 7.5 B1+B5.
3//!
4//! [`KernelRef`] is the uniform function-pointer type that every
5//! dispatch wrapper matches. Backend-specific typed kernels live in
6//! their backend crates (e.g. `fuel_cpu_backend::byte_kernels`); the
7//! *wrapper* functions here in fuel-storage bridge the dispatch-
8//! erased `Storage` to those typed kernels by matching on
9//! `BackendStorage::Cpu(...)` etc.
10//!
11//! [`OpParams`] is the typed extras bag — one variant per op family
12//! that needs auxiliary data beyond inputs and outputs. Most
13//! elementwise ops use `OpParams::None`; reductions carry their
14//! reduce dims; conv2d carries kernel/stride/padding; etc.
15//!
16//! [`KernelBindingTable`] (B5) maps
17//! `(OpKind, SmallVec<[DType; N]>, BackendId) -> KernelRef`. The dtype
18//! list carries per-operand types — inputs in order, then outputs —
19//! so mixed-precision ops (e.g. `Cast: src→dst`) and same-dtype ops
20//! (e.g. `Add: [T, T, T]`) share the same key shape. Backends register
21//! their dispatch wrappers via the `dispatch::register_*` functions;
22//! op-builder methods consult the table at DAG construction time using
23//! `Graph::target_backend(id)` as the BackendId key.
24//!
25//! ## Architecture (cycle-avoidance)
26//!
27//! - Backend crates (`fuel-cpu-backend`, …): typed kernels on their
28//!   concrete storage types (`CpuStorageBytes`, …). No fuel-storage
29//!   dependency.
30//! - fuel-storage (this crate): dispatch wrappers that match
31//!   `BackendStorage::Cpu(...)`, extract the typed storage, and
32//!   call the backend's typed kernel. KernelBindingTable lives here.
33//! - Backend crates depend on fuel-storage? No — only the wrappers
34//!   do. fuel-storage already depends on backend crates for variant
35//!   types; this round-trip closes naturally.
36
37use std::collections::HashMap;
38use std::sync::Arc;
39use std::sync::RwLock;
40
41use fuel_graph::QuantType;
42use fuel_graph::registry::FusedOpId;
43use fuel_ir::conv::{ParamsConv1D, ParamsConvTranspose1D};
44use fuel_ir::dispatch::OpKind;
45use fuel_ir::probe::BackendId;
46use fuel_ir::{DType, Error, Layout, Result};
47use smallvec::SmallVec;
48
49/// Inline capacity for the per-operand dtype list in the binding-table
50/// key. 8 covers every op currently in flight without spilling to heap:
51/// PagedAttn (q + 2 caches + block_table + context_lens + alibi + out)
52/// at 7 entries is the worst case in inference; mixed-precision matmul
53/// is ≤ 4. Bumping later is one constant change.
54pub type KernelDTypes = SmallVec<[DType; 8]>;
55
56/// One entry of the coverage diagnostic: `(backend, op, operand dtypes)`.
57///
58/// Named rather than spelled out because the bare 3-tuple appears in a return
59/// position where it was unreadable, and `#[allow(type_complexity)]` would
60/// have asserted the tuple is fine as-is. It is not — the alias is the fix and
61/// the lint was right.
62///
63/// NOT `BindingKey`: that name is already a distinct `pub enum` in this
64/// module (`Static` / …). The compiler caught the collision; the lesson is
65/// that a type alias introduced for a lint still needs a name check.
66pub type CoverageBinding = (BackendId, OpKind, Vec<DType>);
67
68/// Per-binding capability flags. Today carries one flag — `strided_input`
69/// — that signals "this kernel walks input strides explicitly and so
70/// can consume non-contiguous input layouts (including stride-0
71/// broadcast axes) without auto-Contiguize materializing them first."
72/// The executor's contiguize gate consults this to skip the materialize
73/// step for capable kernels, so broadcast/transpose/slice layouts can
74/// reach the kernel as metadata-only views.
75///
76/// Default is the conservative all-false; binding sites opt in via
77/// [`KernelBindingTable::register_with_caps`]. Forward-extensible by
78/// adding fields (no enum/bitflags churn).
79#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
80pub struct KernelCaps {
81    /// Kernel handles non-contiguous input layouts directly (consumes
82    /// the strides side-channel from `KernelRef::layouts`). When true,
83    /// the executor passes non-contiguous inputs through unchanged
84    /// instead of running auto-Contiguize first. Inputs with non-zero
85    /// `start_offset` still go through auto-Contiguize today (slicing
86    /// the device buffer to honor offset is a separate concern).
87    pub strided_input: bool,
88    /// This kernel BAKES a specific broadcast pattern into its body — a
89    /// `broadcast_stride0: required` operand it reads as stride-0 (hoisting
90    /// `in[0]` / dropping the bcast-axis stride terms). It is therefore NOT
91    /// a generic dense-safe kernel: a dense operand routed here would be
92    /// read WRONG (element 0 repeatedly). The realize pick
93    /// ([`KernelBindingTable::lookup_with_caps`]) SKIPS such a sibling so a dense operand
94    /// is never routed into a broadcast-baked kernel — the exclusion is
95    /// order-independent (it does not rest on the generic contract being
96    /// registered first). Selecting it CORRECTLY, for an operand genuinely
97    /// stride-0 on exactly the required axes, is the layout-aware follow-on
98    /// (path 1b) that also teaches the contiguize gate; today the safe
99    /// behavior is to exclude it, so a `required`-broadcast contract is a
100    /// present-but-never-mis-selected sibling. Defaults `false` — every
101    /// existing binding is unaffected (the pick stays first-registered).
102    pub requires_broadcast: bool,
103}
104
105impl KernelCaps {
106    /// All flags off. Equivalent to `Default::default()`; provided as a
107    /// const for use in const-context registration tables.
108    pub const fn empty() -> Self {
109        Self {
110            strided_input: false,
111            requires_broadcast: false,
112        }
113    }
114
115    /// Just `strided_input` on. Ergonomic for binary/unary registrations
116    /// that opt in to the wrapper-side broadcast path.
117    pub const fn strided_input() -> Self {
118        Self {
119            strided_input: true,
120            requires_broadcast: false,
121        }
122    }
123}
124
125use fuel_memory::Storage;
126
127/// Uniform function-pointer signature for per-backend op kernels.
128///
129/// Inputs are passed as a slice of `&Storage` to handle multi-input
130/// ops (binary, ternary, custom). Outputs are passed as a slice of
131/// `&mut Storage` to handle multi-output ops (topk, var_mean,
132/// custom). Most ops use single-element slices.
133///
134/// `layouts` is a side-channel that parallels `inputs.append(outputs)`
135/// — i.e. `layouts[0..inputs.len()]` are the input layouts in order,
136/// followed by `layouts[inputs.len()..]` for the output layouts. This
137/// is the load-bearing primitive for stride-aware kernels (broadcast,
138/// transpose, gather-from-strided). Kernels that only support
139/// contiguous-equal-shape inputs (today's default) can ignore
140/// `layouts` because the executor's auto-Contiguize pass guarantees
141/// every input is contiguous before this is called; the layout slice
142/// then carries `Layout::contiguous(shape)` entries that are useful
143/// for shape inference but redundant with `Storage.len_bytes / dtype`.
144///
145/// `OpParams` carries op-family-specific extras (reduce dims,
146/// conv2d geometry, etc.). Most kernels match a specific variant;
147/// mismatches are programming bugs that the dispatch resolver
148/// must prevent. Pure layout/shape duplication that used to live in
149/// `OpParams` (e.g. the old `Reduce::input_layout`) now flows through
150/// `layouts` instead.
151///
152/// **Output Storage is pre-allocated** by the executor before the
153/// kernel is called. Kernels write into the pre-allocated bytes;
154/// they never allocate.
155///
156/// ## Multi-output kernels (Option C, Session 5)
157///
158/// Multi-output ops (e.g. SelectiveScan returning `(y, last_state)`)
159/// emit ONE `KernelRef`. The contract:
160///
161/// - `outputs.len() == 1`. The single `Arc<RwLock<Storage>>` is the
162///   producer's *bundled* Storage — its `bundle()` returns
163///   `Some(&[OutputView; N])` describing each logical slot's
164///   `dtype`/`shape`/`layout`/`byte_offset` inside the bundle's
165///   underlying byte buffer.
166/// - The kernel writes each logical output into its slot's byte
167///   range by acquiring a single write lock on the output and
168///   striding by the slot's `byte_offset`. The bundle metadata is
169///   the authoritative per-slot spec — `outputs[0].read().unwrap()
170///   .bundle().expect("bundled storage").get(slot_idx)` is the
171///   canonical access path.
172/// - Per-slot dtype tags do NOT travel through the kernel's
173///   `KernelDTypes` key (which describes inputs + the bundle's
174///   primary dtype only); the bundle metadata IS the per-slot
175///   dispatch info.
176/// - The bundle is pre-allocated by the executor via
177///   `allocate_bundled_storage(device, &output_views_spec)` (see
178///   `fuel_backend_contract::storage::allocate_bundled_storage`). Kernels
179///   never allocate; they only fill bytes.
180///
181/// Consumers of multi-output producers are NOT multi-output kernels —
182/// they're `Op::View` (zero-copy slot projection) or `Op::ViewOwned`
183/// (independent slot buffer), which the executor handles directly
184/// without invoking a kernel.
185///
186/// **Production-correct**: kernels return `Result`, never panic.
187pub type KernelRef = fn(
188    inputs: &[Arc<RwLock<Storage>>],
189    outputs: &mut [Arc<RwLock<Storage>>],
190    layouts: &[Layout],
191    params: &OpParams,
192) -> Result<()>;
193
194/// How many rows a [`OpParams::Matmul`] computes — the data-determined-M
195/// state for sparse-MoE / capacity-buffer matmuls. `m` in `OpParams::Matmul`
196/// is always the row **capacity** (buffer/stride/size); this says how many
197/// of those rows to actually compute (`<= m`), the rest left zeroed.
198#[derive(Debug, Clone, PartialEq, Eq)]
199pub enum MatmulM {
200    /// Compute every one of the `m` rows — the universal static default.
201    All,
202    /// Compute exactly this many rows of the `m`-row capacity buffer. Set
203    /// when the count is known at compile (an input-determined extent) or
204    /// after a [`Self::Deferred`] is resolved at execute.
205    Rows(usize),
206    /// The row count is **data-determined** (a producer binds it mid-pass,
207    /// e.g. `Op::NonZeroIndices`'s per-expert token count) — resolve at
208    /// execute from `produced_syms`, then patch to [`Self::Rows`].
209    Deferred(fuel_ir::DynScalar),
210}
211
212/// Typed extras bag passed to kernels via [`KernelRef`]. One variant
213/// per op family that needs auxiliary parameters; `None` for
214/// parameter-less ops (most elementwise unary and binary).
215#[derive(Debug, Clone)]
216pub enum OpParams {
217    /// Op needs no auxiliary parameters. Used by elementwise
218    /// unary (relu, neg, sqr, …), elementwise binary (add, mul,
219    /// sub, div, …), shape-only ops (reshape, transpose), etc.
220    None,
221
222    /// Reduction (sum, max, mean, …) along specific dims. The input
223    /// tensor's [`Layout`] flows through the new `layouts` side-channel
224    /// (`layouts[0]`) on `KernelRef`, so this variant carries only the
225    /// op-specific extras: which dims to reduce and the keepdim flag.
226    ///
227    /// `dims` is the sorted list of dims to reduce; `keepdim`
228    /// controls whether reduced dims are retained as size-1 in
229    /// the output (today fuel-graph never asks for keepdim, but
230    /// the field is reserved for the future).
231    Reduce { dims: Vec<usize>, keepdim: bool },
232
233    /// Matrix multiplication. Carries the dimensions explicitly
234    /// because [`Storage`](fuel_memory::Storage) only holds bytes + dtype;
235    /// the kernel needs the batch shape and `(m, n, k)` to walk
236    /// inputs and outputs.
237    ///
238    /// Shape contract: lhs `[..lhs_batch.., m, k]` @
239    /// rhs `[..rhs_batch.., k, n]` → out `[..lhs_batch.., m, n]`.
240    /// Per-axis the batch dims must either match or follow GQA-style
241    /// divisibility (`lhs_dim > rhs_dim && lhs_dim % rhs_dim == 0`)
242    /// — the kernel maps each lhs batch slot to the corresponding
243    /// rhs slot via `rhs_axis_idx = lhs_axis_idx / n_rep_axis`.
244    ///
245    /// Equal-batch case: `lhs_batch_dims == rhs_batch_dims`. Rank-2
246    /// case: both batch vectors are empty. Both work uniformly.
247    ///
248    /// Inputs are guaranteed contiguous by the executor's
249    /// auto-Contiguize pass; transpose flags don't appear here
250    /// because `Op::Transpose` is its own metadata-only op in
251    /// fuel-graph.
252    Matmul {
253        lhs_batch_dims: Vec<usize>,
254        rhs_batch_dims: Vec<usize>,
255        /// The output/LHS row **capacity** (drives size checks + strides).
256        m: usize,
257        n: usize,
258        k: usize,
259        /// How many of the `m` rows to actually compute (data-determined-M
260        /// sparse-MoE support). `MatmulM::All` for every ordinary matmul.
261        m_compute: MatmulM,
262    },
263
264    /// 1D convolution geometry (forward path).
265    Conv1D(ParamsConv1D),
266
267    /// 2D convolution geometry (forward path). Carries the tuple-shaped
268    /// stride/padding that fuel-graph's `Op::Conv2D` uses (asymmetric
269    /// supported), plus the input/weight/output shapes the kernel
270    /// needs to walk the multi-index. Storage holds only bytes +
271    /// dtype, so spatial shapes flow through OpParams.
272    ///
273    /// Inputs: `x` shape `[N, Cin, Hin, Win]`, `weight` shape
274    /// `[Cout, Cin/groups, Kh, Kw]`, optional `bias` shape `[Cout]`.
275    /// Output: `[N, Cout, Hout, Wout]`.
276    Conv2D {
277        x_shape: [usize; 4],
278        w_shape: [usize; 4],
279        out_shape: [usize; 4],
280        stride: (usize, usize),
281        padding: (usize, usize),
282        dilation: (usize, usize),
283        groups: usize,
284    },
285
286    /// 1D transposed-convolution geometry.
287    ConvTranspose1D(ParamsConvTranspose1D),
288
289    /// 2D transposed-convolution geometry. Mirrors `OpParams::Conv2D`
290    /// in shape (inline fields, asymmetric stride/padding/dilation,
291    /// `groups`), with the additional `output_padding` parameter that
292    /// transposed conv needs to disambiguate output spatial size.
293    ///
294    /// Inputs: `x` shape `[N, Cin, Hin, Win]`, `weight` shape
295    /// `[Cin, Cout/groups, Kh, Kw]` (note transposed channel order
296    /// vs Conv2D), optional `bias` shape `[Cout]`.
297    /// Output: `[N, Cout, Hout, Wout]`.
298    ConvTranspose2D {
299        x_shape: [usize; 4],
300        w_shape: [usize; 4],
301        out_shape: [usize; 4],
302        stride: (usize, usize),
303        padding: (usize, usize),
304        output_padding: (usize, usize),
305        dilation: (usize, usize),
306        groups: usize,
307    },
308
309    /// Sum-reduce a tensor to a smaller broadcast-compatible target
310    /// shape. The kernel left-pads `output_shape` with 1s to match
311    /// `input_shape`'s rank, then for each axis: if the padded output
312    /// dim equals the input dim that axis carries through; if it's 1
313    /// the axis is summed away.
314    ReduceSumTo {
315        input_shape: Vec<usize>,
316        output_shape: Vec<usize>,
317    },
318
319    /// Max-reduce a tensor to a smaller broadcast-compatible target
320    /// shape — the max-symmetric counterpart of `ReduceSumTo`. Same
321    /// axis-alignment rules; per-axis reduction is `max` instead of
322    /// `+`.
323    ReduceMaxTo {
324        input_shape: Vec<usize>,
325        output_shape: Vec<usize>,
326    },
327
328    /// Backward of `ReduceMaxTo`. Inputs: `(x, upstream)` where
329    /// `x.shape == input_shape` and `upstream.shape == output_shape`.
330    /// Output: `grad_x` of `input_shape`. The kernel recomputes the
331    /// forward max via `input_shape → output_shape` axis alignment,
332    /// builds a tie-count mask, and routes upstream back to argmax
333    /// positions (fair-share on ties).
334    ReduceMaxToBackward {
335        input_shape: Vec<usize>,
336        output_shape: Vec<usize>,
337    },
338
339    /// Multi-head scaled-dot-product attention shape + math params.
340    /// `q` is `[B, Hq, Sq, D]`, `k` and `v` are `[B, Hkv, Sk, D]`
341    /// (Hkv ≤ Hq, GQA-divisible). Optional 4th input `alibi_slopes`
342    /// has shape `[Hq]` (presence is implicit in `inputs.len() == 4`).
343    ///
344    /// `sk` is the **physical** K/V length axis (the allocated extent —
345    /// used for strides + byte-length checks). `k_len` is the
346    /// **logical** attended length (`k_len <= sk`): a kernel attends only
347    /// the first `k_len` rows of the K/V buffers and bottom-right-aligns
348    /// the causal mask at offset `k_len - sq` (Phase D symbolic extents:
349    /// flash over a fixed-capacity KV-cache with a runtime live prefix).
350    /// For the static path the lowering sets `k_len == sk`, so a kernel
351    /// that loops `0..k_len` with the `k_len - sq` causal offset is
352    /// byte-identical to the old `0..sk` / `kj > qi` form.
353    FlashAttn {
354        b: usize,
355        hq: usize,
356        hkv: usize,
357        sq: usize,
358        sk: usize,
359        d: usize,
360        k_len: usize,
361        softmax_scale: f32,
362        causal: bool,
363        window_size_left: Option<usize>,
364        window_size_right: Option<usize>,
365        softcap: Option<f32>,
366    },
367
368    /// Paged-cache attention. `q` is `[B, Hq, Sq, D]`; `k_cache` and
369    /// `v_cache` are `[num_blocks, block_size, Hkv, D]`. The 4th input
370    /// is `block_table: [B, max_blocks_per_seq]` (U32 — physical block
371    /// index per logical position). The 5th is `context_lens: [B]`
372    /// (U32 — true context length per sequence). Optional 6th is
373    /// `alibi_slopes: [Hq]`.
374    PagedAttn {
375        b: usize,
376        hq: usize,
377        hkv: usize,
378        sq: usize,
379        d: usize,
380        block_size: usize,
381        max_blocks_per_seq: usize,
382        num_blocks: usize,
383        softmax_scale: f32,
384        softcap: Option<f32>,
385    },
386
387    /// Slice along a single dim with explicit start/end/step. The
388    /// dim is implicit in the input Layout's relabeling for
389    /// multi-dim slice; this variant covers the simple case.
390    Slice {
391        dim: usize,
392        start: usize,
393        end: usize,
394        step: usize,
395    },
396
397    // (Earlier `OpParams::Pad { padding: Vec<(usize, usize)>, fill_bytes: Vec<u8> }`
398    // was a speculative multi-dim shape with no consumers. The single-
399    // dim shape that Op::Pad actually emits lives at the bottom of this
400    // enum; if multi-dim padding lands later, it can extend either
401    // shape additively.)
402    /// Cast input dtype → target dtype. The target lives on the
403    /// output Storage's `dtype` field; this variant signals the
404    /// op family without requiring a re-read.
405    Cast,
406
407    /// Affine transformation `y = mul * x + add`. Used by
408    /// `Tensor::affine` / `scale_and_shift`.
409    Affine { mul: f64, add: f64 },
410
411    /// Element-wise clamp: `y = clamp(x, min, max)`.
412    Clamp { min: f64, max: f64 },
413
414    /// Element-wise integer power: `y = x.powi(exp)`.
415    PowI { exp: i32 },
416
417    /// Concatenate N inputs along one dim. The kernel needs the
418    /// outer/inner element counts (product of dims before/after
419    /// the concat dim) plus each input's size along the concat
420    /// dim — that's all that distinguishes a concat from a
421    /// sequence of slab-copies. Order matches `Node::inputs`.
422    ///
423    /// `axis` is the original concat dim in the output's shape;
424    /// kernels that want to walk strided rank-N inputs need it
425    /// to build the per-axis stride mask (the outer/inner factoring
426    /// alone loses the position info).
427    Concat {
428        /// Product of output dims before the concat dim.
429        outer_count: usize,
430        /// Per-input size along the concat dim (length = N inputs).
431        input_dim_sizes: Vec<usize>,
432        /// Product of output dims after the concat dim.
433        inner_count: usize,
434        /// Original concat axis index in the output's rank-N shape.
435        axis: usize,
436    },
437
438    /// Softmax along the last dim. The kernel walks
439    /// `outer_count` rows of `last_dim` elements each.
440    SoftmaxLastDim { outer_count: usize, last_dim: usize },
441
442    /// Last-dim norm parameters shared by RMS-norm and LayerNorm
443    /// (no affine flavor for both today). The OpKind selects which
444    /// kernel reads this variant.
445    NormLastDim {
446        outer_count: usize,
447        last_dim: usize,
448        eps: f64,
449    },
450
451    /// Pick slices along a single dim using a rank-1 U32 index
452    /// tensor. The kernel needs the outer/inner counts (dims
453    /// before/after the selected axis), the source's selected-dim
454    /// size for index bounds checking, and the index count.
455    IndexSelect {
456        outer_count: usize,
457        source_dim_size: usize,
458        n_indices: usize,
459        inner_count: usize,
460    },
461
462    /// N-dimensional gather along `dim`. Source and output shapes
463    /// agree on every dim except `dim`; the indices tensor (U32)
464    /// has output_shape and supplies the source coord for `dim`
465    /// at every output position.
466    Gather {
467        source_shape: Vec<usize>,
468        output_shape: Vec<usize>,
469        dim: usize,
470    },
471
472    /// Rotary position embedding parameters. The kernel walks
473    /// `outer_count` × `seq` × `head_dim` elements; `cos`/`sin`
474    /// have `[seq, head_dim]` shape and broadcast across the
475    /// outer dims.
476    Rope {
477        outer_count: usize,
478        seq: usize,
479        head_dim: usize,
480    },
481
482    /// Quantized matmul shape: `A [batch, m, k] @ dequant(W) [n, k] →
483    /// out [batch, m, n]`. The weight tensor is a flat U32-typed
484    /// byte stream representing `n * k / elements_per_block` blocks
485    /// of `quant_type`. Activations are F32 today; output is F32.
486    QMatMul {
487        quant_type: QuantType,
488        batch_count: usize,
489        m: usize,
490        n: usize,
491        k: usize,
492    },
493
494    /// Index-add along a single dim with rank-1 U32 indices.
495    /// Output is `base` with `src[..., i, ...]` accumulated into
496    /// `base[..., indices[i], ...]` for every i ∈ 0..n_indices.
497    IndexAdd {
498        outer_count: usize,
499        base_dim_size: usize,
500        n_indices: usize,
501        inner_count: usize,
502    },
503
504    /// N-dimensional scatter-add. Indices and src share the same
505    /// shape; base may differ only along `dim`. The kernel walks
506    /// every src/indices position, reads `indices[p]` for the
507    /// destination's `dim` coord, and accumulates `src[p]` into
508    /// `base` at that destination.
509    ScatterAdd {
510        base_shape: Vec<usize>,
511        src_shape: Vec<usize>,
512        dim: usize,
513    },
514
515    /// Flip the order of elements along one dim. The flat-3-axis
516    /// view (outer × dim × inner) lets the kernel walk a tight loop
517    /// without re-deriving the axis split per call. `axis` is the
518    /// original dim index in the input's rank-N shape — needed by
519    /// stride-aware kernels to build a per-axis flip mask.
520    Flip {
521        outer_count: usize,
522        dim_size: usize,
523        inner_count: usize,
524        axis: usize,
525    },
526
527    /// Cyclic shift along one dim by `shift` positions (positive
528    /// shifts move elements to higher indices, wrapping around).
529    /// Same flat-3-axis view as `Flip`. `shift` is signed: negative
530    /// shifts move elements to lower indices.
531    Roll {
532        outer_count: usize,
533        dim_size: usize,
534        inner_count: usize,
535        shift: i64,
536        axis: usize,
537    },
538
539    /// Running cumulative sum along one dim. Same flat-3-axis view
540    /// as `Flip`/`Roll`. Output is always the same dtype as input;
541    /// kernel needs typed addition so it's per-dtype (unlike Flip/Roll).
542    CumSum {
543        outer_count: usize,
544        dim_size: usize,
545        inner_count: usize,
546        axis: usize,
547    },
548
549    /// Triangular mask parameters (used by both Triu and Tril — the
550    /// op-kind picks the direction). `batch_count` is the product of
551    /// leading dims; `rows`/`cols` are the last two dims. `diagonal`
552    /// is signed (0 = main diagonal, positive shifts up, negative
553    /// shifts down).
554    Triangular {
555        batch_count: usize,
556        rows: usize,
557        cols: usize,
558        diagonal: i64,
559    },
560
561    /// LogSoftmax along the last dim. Walks `outer_count` rows of
562    /// `last_dim` elements each. Per-dtype kernel (uses log/exp).
563    LogSoftmaxLastDim { outer_count: usize, last_dim: usize },
564
565    /// MaskedFill: per-element fill where mask is nonzero. The kernel
566    /// reads the element count from the layout; `fill_bytes` is
567    /// pre-encoded in the output's dtype (one element's worth).
568    MaskedFill { fill_bytes: Vec<u8> },
569
570    /// Backward helper for Pad. Carries the input shape so the kernel
571    /// can size its scatter-add buffer; the output shape is implicit
572    /// from the input shape + padding.
573    PadBackward {
574        in_shape: Vec<usize>,
575        out_shape: Vec<usize>,
576        padding: Vec<(usize, usize)>,
577        mode_tag: u8,
578    },
579
580    /// Multi-dim Pad: per-axis (before, after) plus a `mode_tag`
581    /// (0=Constant, 1=Reflect, 2=Replicate) and pre-encoded
582    /// `fill_bytes` for Constant fill. Dtype-agnostic at the byte
583    /// level: the kernel just copies bytes per the input/output shapes,
584    /// which is why fill is bytes (already encoded in the output's
585    /// dtype) rather than `f64` (which would force the kernel to
586    /// know its dtype).
587    ///
588    /// `in_shape.len() == out_shape.len() == padding.len()`, and
589    /// `out_shape[i] == in_shape[i] + padding[i].0 + padding[i].1`.
590    /// `fill_bytes.len() == dtype_size_in_bytes` (one element's worth).
591    Pad {
592        in_shape: Vec<usize>,
593        out_shape: Vec<usize>,
594        padding: Vec<(usize, usize)>,
595        mode_tag: u8,
596        fill_bytes: Vec<u8>,
597    },
598
599    /// In-place scatter write parameters. The destination shape +
600    /// source shape together determine the kernel walk; `ranges`
601    /// gives the per-axis (start, end) slab inside the destination.
602    /// `dest_shape[i] >= ranges[i].1`, and source shape is implicitly
603    /// `ranges[i].1 - ranges[i].0` along axis `i`.
604    ///
605    /// Phase E.3.2: backs `Op::WriteSlice` for persistent KV-cache
606    /// writes.
607    ///
608    /// `deferred_dyn_offset` is the **data-determined dynamic-shapes** seam
609    /// (increment 2b). Normally `None` — `ranges` is already resolved at
610    /// compile (static, or an *input-determined* `dyn_offset` bound in the
611    /// per-pass `SymEnv`). It is `Some((axis, sym))` only when the offset is
612    /// *data-determined* — its `SymId` is UNBOUND at compile because a
613    /// producer (e.g. `Op::NonZeroIndices`) computes it mid-pass. The
614    /// `WorkItemKind::WriteSlice` executor arm resolves it from
615    /// `produced_syms` and overrides `ranges[axis]` to `(base, base+width)`
616    /// just before the kernel runs; the kernel wrappers ignore this field.
617    WriteSlice {
618        dest_shape: Vec<usize>,
619        ranges: Vec<(usize, usize)>,
620        deferred_dyn_offset: Option<(usize, fuel_ir::DynScalar)>,
621    },
622
623    /// In-place ring-buffer scatter write parameters. Like
624    /// `WriteSlice` except `axis` wraps modulo `modulus`; the
625    /// dynamic start on that axis is read from an extra input (rank-0
626    /// U32). On the rotating axis, `ranges[axis].0` is ignored
627    /// (dynamic) and `ranges[axis].1 - ranges[axis].0` is the slab
628    /// width (must equal the source dim on that axis, and must not
629    /// exceed `modulus`).
630    ///
631    /// Phase C: backs `Op::WriteSliceRotating` for sliding-window
632    /// KV caches.
633    WriteSliceRotating {
634        dest_shape: Vec<usize>,
635        axis: usize,
636        modulus: usize,
637        ranges: Vec<(usize, usize)>,
638    },
639
640    /// In-place scatter write whose start on `axis` is read from a
641    /// **device-resident** rank-0 `I64` input (`inputs[2]`) at kernel
642    /// launch — the CUDA-graph-capturable KV-cache append. Like
643    /// `WriteSlice` except `ranges[axis].0` is a placeholder overridden
644    /// device-side by the offset; no modulo wrap. The CUDA wrapper
645    /// passes the offset buffer's device pointer straight through as
646    /// `dyn_start_dev` (NO D2H — that would break capture); the CPU
647    /// wrapper reads it host-side and delegates to `write_slice_cpu`.
648    /// Bounds (`offset + width <= dest_shape[axis]`) are the caller's
649    /// contract; the kernel does not clamp.
650    ///
651    /// CapturedRun (2026-07): backs `Op::WriteSliceDoff` for
652    /// `DecodeSession` decode-step capture/replay.
653    WriteSliceDoff {
654        dest_shape: Vec<usize>,
655        axis: usize,
656        ranges: Vec<(usize, usize)>,
657    },
658
659    /// FusedSoftmaxCrossEntropy execution parameters. Inputs:
660    /// `logits [n_rows, vocab]` (F32, flattened from the original
661    /// `[..., V]` shape) and `targets [n_rows]` (I64). The kernel
662    /// walks row-by-row, computing stable log-softmax + NLL +
663    /// `ignore_index` masking + the requested reduction in a single
664    /// pass, allocating only an `[n_rows]` per-row accumulator (and
665    /// a scalar for Mean/Sum). Output is F32 scalar `[]` for
666    /// Mean/Sum, F32 `[n_rows]` for None.
667    ///
668    /// `n_rows` and `vocab` are derived at translate time from the
669    /// logits layout; the kernel uses them to iterate without
670    /// re-parsing shapes.
671    FusedSoftmaxCrossEntropy {
672        n_rows: usize,
673        vocab: usize,
674        reduction: fuel_graph::registry::Reduction,
675        ignore_index: i64,
676    },
677
678    /// CausalConv1d execution parameters. Inputs:
679    /// `x [batch, channels, seq_in]` (pre-padded by caller with
680    /// `kernel - 1` left zeros), `weight [channels, 1, kernel]`,
681    /// `bias [channels]`. Output `[batch, channels, seq_out]` where
682    /// `seq_out = seq_in - (kernel - 1)`. All tensors share dtype.
683    ///
684    /// Carries both `seq_in` and `seq_out` so the kernel walks the
685    /// time axis without re-deriving from shapes. `use_silu` toggles
686    /// the fused SiLU activation on the output store (matches
687    /// baracuda's `causal_conv1d_*_run` signature flag).
688    CausalConv1d {
689        batch: usize,
690        channels: usize,
691        seq_in: usize,
692        seq_out: usize,
693        kernel: usize,
694        use_silu: bool,
695    },
696
697    /// SelectiveScan execution parameters. 5 required inputs:
698    /// `u [batch, seqlen, dim]`, `delta [batch, seqlen, dim]`,
699    /// `a [dim, dstate]`, `b [batch, seqlen, dstate]`,
700    /// `c [batch, seqlen, dstate]`. Output `y [batch, seqlen, dim]`.
701    /// All tensors share dtype.
702    ///
703    /// `delta_softplus` toggles applying softplus(delta) before use
704    /// (matches baracuda's `selective_scan_*_run` flag).
705    SelectiveScan {
706        batch: usize,
707        seqlen: usize,
708        dim: usize,
709        dstate: usize,
710        delta_softplus: bool,
711    },
712
713    /// SsdChunkScan execution parameters. 5 inputs:
714    /// `x [batch, seqlen, heads, head_dim]`,
715    /// `dt [batch, seqlen, heads]`, `a [heads]`,
716    /// `b [batch, seqlen, heads, state_dim]`,
717    /// `c [batch, seqlen, heads, state_dim]`. Output `y` matches `x`'s
718    /// shape. All tensors share dtype.
719    ///
720    /// `chunk_size` is the SSD block size (GPU-parallelism knob).
721    /// On CPU the kernel runs a sequential scan regardless;
722    /// validation requires `chunk_size > 0` and
723    /// `seqlen % chunk_size == 0`.
724    SsdChunkScan {
725        batch: usize,
726        seqlen: usize,
727        heads: usize,
728        head_dim: usize,
729        state_dim: usize,
730        chunk_size: usize,
731    },
732
733    /// Nf4Matmul execution parameters. 3 inputs:
734    /// `activations [batch, m, k]` (rank ≥ 2; leading dims flattened
735    /// into `batch`), `w_packed [n, k/2]` U8, `absmax [n, k/block_size]`
736    /// F32. Output `[batch, m, n]` matching activations' dtype.
737    ///
738    /// `block_size` is the per-output-row, per-block scale granularity
739    /// (typically 64 in bitsandbytes). `k` must be even (w_packed
740    /// layout requirement) and a multiple of `block_size`.
741    Nf4Matmul {
742        batch: usize,
743        m: usize,
744        n: usize,
745        k: usize,
746        block_size: usize,
747    },
748
749    /// AwqMatmul execution parameters. 4 inputs: `activations [batch, m, k]`
750    /// (rank ≥ 2; leading dims flattened into `batch`), `weight [n, k/8]` U32,
751    /// `zeros [n, zeros_w]` U32, `scales [n, 8 * zeros_w]` F16 (baracuda W4A16
752    /// kernel layout). Output `[batch, m, n]` matching activations' dtype.
753    ///
754    /// `group_size` ∈ {64, 128}; `split_k_iters` is the CUDA launch-tuning
755    /// parameter (typically 8).
756    AwqMatmul {
757        batch: usize,
758        m: usize,
759        n: usize,
760        k: usize,
761        group_size: usize,
762        split_k_iters: i32,
763    },
764
765    /// A **runtime fused op's** live scalar args (the `extract:` values from
766    /// `FusedOpParams::Runtime { scalars }`), in the region's pattern
767    /// pre-order — the same canonical slot order the synthesized kernel's
768    /// trailing `, float p0, float p1, …` launch parameters are declared in
769    /// (the synthesizer's scalar ABI appends them after `long long n`, always
770    /// `float`-typed regardless of the kernel's element dtype). Only produced
771    /// by `compile_one`'s `is_runtime` arm; a param-free runtime op carries
772    /// `OpParams::None` instead.
773    JitScalars {
774        /// Slot values, pattern pre-order. `f64` at rest (the graph-side
775        /// carrier); the CUDA launch narrows each to `f32` per the ABI.
776        scalars: Vec<f64>,
777    },
778
779    /// NonZeroIndices execution parameters. 1 input `x` (the value
780    /// tensor); one bundled output `[indices [capacity] U32 ; count
781    /// [1] U32]` where `capacity == x.elem_count()`. The kernel writes
782    /// the ascending flat indices of `x`'s nonzero elements into slot 0
783    /// (first `count` entries) and the runtime `count` into slot 1.
784    ///
785    /// `count_sym` is the [`fuel_ir::symbol::SymId`] the executor binds to the realized
786    /// `count` in the per-pass `SymEnv` *after* this op runs — the
787    /// data-determined dynamic-shape seam. The kernel itself ignores it.
788    NonZeroIndices {
789        capacity: usize,
790        count_sym: fuel_ir::SymId,
791    },
792}
793
794// =============================================================================
795// Phase 7.5 B5 — kernel binding table
796// =============================================================================
797
798/// Maps `(OpKind, KernelDTypes, BackendId)` triples to dispatch wrapper
799/// functions, where `KernelDTypes` lists per-operand dtypes (inputs in
800/// order, then outputs). Built once at backend registration time
801/// (typically a process-wide `OnceLock` though that's not enforced
802/// here for testability), consulted at execute time via lookup.
803///
804/// Same-dtype ops register `[T, T, ..., T]` for the right operand
805/// count; mixed-precision ops (Cast, future F32×BF16→F32 matmul)
806/// register the exact combo. Variadic ops (Concat) register a
807/// canonical short shape `[T, T]` (one input dtype + output dtype);
808/// the lookup site for those ops collapses its dtypes vector to the
809/// same shorthand.
810///
811/// Backends register their wrappers via the `dispatch::register_*`
812/// functions in this crate (e.g.
813/// [`crate::dispatch::register_cpu_kernels`]).
814/// Cost-fn signature for primitive-op registrations stored in
815/// [`KernelBindingTable`]. Mirrors the fused-op cost-fn shape but
816/// takes [`OpParams`] (the binding-table param payload) instead of
817/// `FusedOpParams`.
818///
819/// Implementations return a [`crate::fused::CostEstimate`] computed
820/// statically from shapes + dtypes + op-specific params + backend
821/// capabilities. The architecture's Layer-1 (FLOP-count + bandwidth)
822/// cost model lives here; Layer-2 empirical refinement composes on
823/// top via the telemetry framework, not by changing this signature.
824pub type CostFn = fn(
825    &[fuel_ir::Shape],
826    &[DType],
827    &OpParams,
828    &fuel_ir::backend::BackendCapabilities,
829) -> crate::fused::CostEstimate;
830
831/// Sentinel "no cost claim" function — returns
832/// [`crate::fused::CostEstimate::default`] (all-zero). The fill pass
833/// (`fill_unset_cpu_cost`) recognizes this exact function pointer
834/// to decide which entries get the OpKind-family default.
835pub fn unknown_cost(
836    _shapes: &[fuel_ir::Shape],
837    _dtypes: &[DType],
838    _params: &OpParams,
839    _caps: &fuel_ir::backend::BackendCapabilities,
840) -> crate::fused::CostEstimate {
841    crate::fused::CostEstimate::default()
842}
843
844/// One concrete kernel registered against a `(OpKind, dtypes, backend)`
845/// decision-point key. Phase 7.6 step 9a: a single key now carries
846/// `SmallVec<[BindingEntry; 2]>` — multiple alternatives compete at
847/// the same decision point (e.g. cuBLAS bf16 matmul + CUTLASS bf16
848/// matmul at `(MatMul, [BF16, BF16, BF16], Cuda)`). The route picker
849/// (step 9b) selects among them at plan time; today's single-impl
850/// callers (`lookup`, `lookup_with_caps`, `lookup_precision`,
851/// `lookup_cost`) return the first entry, preserving the pre-9a
852/// behavior for sites that haven't migrated.
853#[derive(Clone, Copy, Debug)]
854pub struct BindingEntry {
855    pub kernel: KernelRef,
856    pub caps: KernelCaps,
857    pub precision: crate::fused::PrecisionGuarantee,
858    pub cost: CostFn,
859    /// Diagnostic tag identifying the kernel's implementation source
860    /// when multiple alternatives register at the same `(op, dtypes,
861    /// backend)` key. Used to distinguish e.g. AOCL's matmul from
862    /// MKL's matmul from the portable scalar matmul — all three live
863    /// at `(MatMul, [F32×3], Cpu)` as siblings.
864    ///
865    /// `""` is the default for kernels that don't need to distinguish.
866    /// Conventional values: `"portable-cpu"` for fuel-cpu-backend
867    /// scalar/gemm kernels, `"aocl"` for AOCL-BLAS, `"mkl"` for oneMKL,
868    /// `"cublas"` / `"cutlass"` for CUDA variants. The string is
869    /// diagnostic-only — never used for dispatch routing (the kernel
870    /// function pointer is the dispatch identifier).
871    pub kernel_source: &'static str,
872    /// Whether this kernel's FKC accept-contract is the **GENERIC**
873    /// (fully-permissive strided) one — admissible anywhere because it
874    /// imposes no structure tightness (FKC §4.12). This is the
875    /// structural-**miss** fallback bit: when the winning contract at a
876    /// dispatch cell is generic, a structure-specialized kernel would have
877    /// fit but none is registered, and the Baracuda miss telemetry records
878    /// the demand.
879    ///
880    /// Computed **once at registration** by the FKC importer from the
881    /// kernel's retained `ResolvedLayout` set
882    /// ([`crate::fkc::is_generic_contract`]) and stamped here, so
883    /// the live dispatch pick site reads a precomputed bool rather than
884    /// re-deriving genericity from the lossy single-bool [`KernelCaps`].
885    ///
886    /// **Hand-written registrations default `false`.** Every non-FKC
887    /// `register*` path routes through
888    /// [`KernelBindingTable::register_full_with_source`], which sets this
889    /// `false`: genericity is a claim about being the FKC generic-fallback
890    /// contract in Baracuda's structure-key specialization space, and a
891    /// hand-written kernel makes no such claim. This holds even for a
892    /// hand-written strided kernel (`register_with_caps(strided_input=true)`):
893    /// strided-capability is not the same as being the generic fallback, and
894    /// conflating them would fabricate a miss demand signal for cells that are
895    /// not part of Baracuda's matrix. Only the FKC importer, which computed
896    /// genericity from the declared contract, ever sets this `true`.
897    pub is_generic: bool,
898    /// Stable per-implementation-version hash — the fifth axis of the FKC
899    /// kernel-identity basis tuple (FKC §4.11), pinning the exact kernel build
900    /// so a persisted plan / telemetry record re-resolves to it. `0`
901    /// ([`crate::fused::KernelRevisionHash::UNTRACKED`]) for hand-written
902    /// registrations (which carry no revision); the FKC importer threads the
903    /// contract-computed revision from `ResolvedPrimitive.revision` here. Read
904    /// by the Baracuda dispatch-telemetry `ImplId` at the plan pick site so the
905    /// `chosen`/`candidates` ids carry a real revision instead of a stamped `0`.
906    pub kernel_revision_hash: u64,
907    /// The contract's parsed cost-expression AST (FKC §4.4 / adoption plan
908    /// §2.3), when the kernel came from an FKC-imported contract that
909    /// declares one. `&'static` so the entry stays `Copy` (`CompiledCostExpr`
910    /// derives `Debug`, which is all `&'static T` needs to stay `Copy` /
911    /// `Clone` / `Debug` itself). `None` for every hand-written registration
912    /// (`register`/`register_full*`) — those carry no declared cost AST, only
913    /// a `CostFn` fn-pointer. Only the FKC importer, once it lowers a
914    /// contract's cost block to a live `CostFn` (a later Group-2 task),
915    /// stamps `Some(&CompiledCostExpr)` here.
916    pub cost_expr: Option<&'static crate::fkc::CompiledCostExpr>,
917}
918
919/// The binding table's op key — generalized so **runtime-registered** fused
920/// ops live in the SAME registry as static kernels (the 2026-07-08
921/// decisions-log end-state; retires the transitional runtime-kernel sidecar).
922///
923/// `Static` is the compile-time `OpKind` family every existing registration
924/// uses (a bare `OpKind` converts via `From`, so those call sites are
925/// unchanged). `RuntimeFused` names a Tier-2 (JIT-synthesized / import-time)
926/// fused op by its runtime `FusedOpId` (`>= RUNTIME_FUSED_BASE`) — the id IS
927/// the op identity, so no fake `OpKind` ever enters the table.
928#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
929pub enum BindingKey {
930    /// A compile-time op family (every static kernel registration).
931    Static(OpKind),
932    /// A runtime-registered fused op, keyed by its runtime id.
933    RuntimeFused(FusedOpId),
934}
935
936impl From<OpKind> for BindingKey {
937    fn from(op: OpKind) -> Self {
938        BindingKey::Static(op)
939    }
940}
941
942impl BindingKey {
943    /// The `Static` op kind, if this is a static-family key.
944    pub fn static_op(self) -> Option<OpKind> {
945        match self {
946            BindingKey::Static(op) => Some(op),
947            BindingKey::RuntimeFused(_) => None,
948        }
949    }
950}
951
952/// `Clone` is load-bearing, not a convenience: the process-wide table is
953/// published as a copy-on-write snapshot (`RwLock<Arc<KernelBindingTable>>`
954/// in `dispatch.rs`), so a writer clones the current table, mutates the
955/// clone, and swaps the pointer. Readers therefore never hold a lock guard.
956/// The clone is shallow-ish and cheap — [`BindingEntry`] is `Copy` and the
957/// keys are `Copy`/`SmallVec`, so it is a `HashMap` bulk copy of POD, on a
958/// write-rare path (backend init + runtime-fusion adoption).
959#[derive(Default, Clone)]
960pub struct KernelBindingTable {
961    bindings: HashMap<(BindingKey, KernelDTypes, BackendId), SmallVec<[BindingEntry; 2]>>,
962}
963
964/// The outcome of [`KernelBindingTable::unique_runtime_fused`] — three
965/// distinct facts, deliberately not collapsed into an `Option`.
966#[derive(Debug)]
967pub enum RuntimeFusedLookup<'a> {
968    /// No runtime-fused binding for this `(fid, backend)`.
969    NotBound,
970    /// Exactly one dtype tuple is registered; this is its first alternative.
971    Unique(&'a [DType], &'a BindingEntry),
972    /// Several dtype tuples share this id. **No kernel is returned**, because
973    /// any choice among them would be arbitrary — and one of them may have a
974    /// different operand arity than the caller expects.
975    Ambiguous { signatures: Vec<Vec<DType>> },
976}
977
978impl KernelBindingTable {
979    pub fn new() -> Self {
980        Self {
981            bindings: HashMap::new(),
982        }
983    }
984
985    /// Register a dispatch wrapper for `(op, dtypes, backend)` with the
986    /// default (all-false) capabilities. `dtypes` is the per-operand
987    /// dtype list — inputs in order, then outputs.
988    ///
989    /// Phase 7.6 step 9a: multiple distinct kernels may register
990    /// against the same `(op, dtypes, backend)` key — they become
991    /// sibling alternatives at one decision point. Registration is
992    /// **append-only and never panics**; the **same** `KernelRef`
993    /// function pointer registered twice is detected by
994    /// [`Self::finalize`] (run once after bulk registration) and
995    /// returned as a `Result`, not raised as an inline panic.
996    ///
997    /// PrecisionGuarantee defaults to
998    /// [`PrecisionGuarantee::UNAUDITED`](crate::fused::PrecisionGuarantee::UNAUDITED).
999    /// Step-7b convention: the always-built backend
1000    /// (fuel-cpu-backend) runs [`Self::fill_unset_cpu_precision`] at
1001    /// the end of its bulk registration pass to upgrade every UNAUDITED
1002    /// CPU entry to `PRIMITIVE_DETERMINISTIC_CPU` (bit-stable per
1003    /// hardware). Kernels with weaker guarantees should call
1004    /// [`Self::register_with_precision`] explicitly *before* the fill
1005    /// pass to opt out of the default.
1006    pub fn register(
1007        &mut self,
1008        op: impl Into<BindingKey>,
1009        dtypes: &[DType],
1010        backend: BackendId,
1011        kernel: KernelRef,
1012    ) {
1013        self.register_full(
1014            op,
1015            dtypes,
1016            backend,
1017            kernel,
1018            KernelCaps::empty(),
1019            crate::fused::PrecisionGuarantee::UNAUDITED,
1020            unknown_cost,
1021        );
1022    }
1023
1024    /// Register a dispatch wrapper with explicit capability flags.
1025    /// Used by binding sites that opt into kernel-side broadcast or
1026    /// other non-default behavior — the executor consults the caps to
1027    /// decide whether to auto-Contiguize inputs before kernel call.
1028    pub fn register_with_caps(
1029        &mut self,
1030        op: impl Into<BindingKey>,
1031        dtypes: &[DType],
1032        backend: BackendId,
1033        kernel: KernelRef,
1034        caps: KernelCaps,
1035    ) {
1036        self.register_full(
1037            op,
1038            dtypes,
1039            backend,
1040            kernel,
1041            caps,
1042            crate::fused::PrecisionGuarantee::UNAUDITED,
1043            unknown_cost,
1044        );
1045    }
1046
1047    /// Register with an explicit [`crate::fused::PrecisionGuarantee`].
1048    /// Use this when a kernel has a precision claim that differs from
1049    /// the bulk-fill default (e.g., a multi-threaded reduction with
1050    /// non-deterministic accumulation order — bit_stable_on_same_hardware
1051    /// must be false).
1052    pub fn register_with_precision(
1053        &mut self,
1054        op: impl Into<BindingKey>,
1055        dtypes: &[DType],
1056        backend: BackendId,
1057        kernel: KernelRef,
1058        precision: crate::fused::PrecisionGuarantee,
1059    ) {
1060        self.register_full(
1061            op,
1062            dtypes,
1063            backend,
1064            kernel,
1065            KernelCaps::empty(),
1066            precision,
1067            unknown_cost,
1068        );
1069    }
1070
1071    /// Backwards-compatible alias for full-form registration without
1072    /// an explicit cost function. New code should prefer
1073    /// [`Self::register_full`].
1074    pub fn register_with_caps_and_precision(
1075        &mut self,
1076        op: impl Into<BindingKey>,
1077        dtypes: &[DType],
1078        backend: BackendId,
1079        kernel: KernelRef,
1080        caps: KernelCaps,
1081        precision: crate::fused::PrecisionGuarantee,
1082    ) {
1083        self.register_full(op, dtypes, backend, kernel, caps, precision, unknown_cost);
1084    }
1085
1086    /// Phase 7.6 step 8: full-form register with caps + precision +
1087    /// cost. Cost defaults to [`unknown_cost`] in the other
1088    /// signatures; sites with non-default cost claims must use this
1089    /// form (and not be overwritten by the bulk
1090    /// [`Self::fill_unset_cpu_cost`] pass, which only touches
1091    /// entries whose cost is still `unknown_cost`).
1092    ///
1093    /// Phase 7.6 step 9a: appends to the alternative set for `(op,
1094    /// dtypes, backend)`. Distinct `KernelRef` function pointers
1095    /// compose as siblings. Append-only — a duplicate `KernelRef` is
1096    /// detected by [`Self::finalize`] after bulk registration, not by
1097    /// an inline panic (see [`Self::register`]).
1098    pub fn register_full(
1099        &mut self,
1100        op: impl Into<BindingKey>,
1101        dtypes: &[DType],
1102        backend: BackendId,
1103        kernel: KernelRef,
1104        caps: KernelCaps,
1105        precision: crate::fused::PrecisionGuarantee,
1106        cost: CostFn,
1107    ) {
1108        self.register_full_with_source(op, dtypes, backend, kernel, caps, precision, cost, "");
1109    }
1110
1111    /// Phase: backend-extensions refactor (2026-06-07). Same as
1112    /// [`Self::register_full`] but tags the binding with a
1113    /// `kernel_source` identifier used to distinguish kernels that
1114    /// register at the same `(op, dtypes, backend)` key from
1115    /// different implementation sources (AOCL vs MKL vs portable
1116    /// CPU, etc.). The tag is diagnostic-only — the picker still
1117    /// distinguishes alternatives by function-pointer identity.
1118    pub fn register_full_with_source(
1119        &mut self,
1120        op: impl Into<BindingKey>,
1121        dtypes: &[DType],
1122        backend: BackendId,
1123        kernel: KernelRef,
1124        caps: KernelCaps,
1125        precision: crate::fused::PrecisionGuarantee,
1126        cost: CostFn,
1127        kernel_source: &'static str,
1128    ) {
1129        // Hand-written registrations make no FKC generic-fallback claim —
1130        // see [`BindingEntry::is_generic`]. The FKC importer uses
1131        // [`Self::register_full_with_source_generic`] to stamp the computed
1132        // bit. Hand-written kernels carry no revision, so the revision axis is
1133        // `UNTRACKED` (`0`); only the FKC importer threads a real revision.
1134        self.register_full_with_source_generic(
1135            op,
1136            dtypes,
1137            backend,
1138            kernel,
1139            caps,
1140            precision,
1141            cost,
1142            kernel_source,
1143            false,
1144            crate::fused::KernelRevisionHash::UNTRACKED.0,
1145            None,
1146        );
1147    }
1148
1149    /// Full-form register with the FKC structural-fallback `is_generic` bit
1150    /// (see [`BindingEntry::is_generic`]). The FKC importer computes the bit
1151    /// once from a kernel's retained `ResolvedLayout` set
1152    /// ([`crate::fkc::is_generic_contract`]) and stamps it here;
1153    /// every hand-written path routes through
1154    /// [`Self::register_full_with_source`], which passes `false`.
1155    ///
1156    /// Otherwise identical to [`Self::register_full_with_source`]:
1157    /// append-only, never-panic; the same `KernelRef` on one key is caught by
1158    /// [`Self::finalize`], not an inline panic.
1159    pub fn register_full_with_source_generic(
1160        &mut self,
1161        op: impl Into<BindingKey>,
1162        dtypes: &[DType],
1163        backend: BackendId,
1164        kernel: KernelRef,
1165        caps: KernelCaps,
1166        precision: crate::fused::PrecisionGuarantee,
1167        cost: CostFn,
1168        kernel_source: &'static str,
1169        is_generic: bool,
1170        kernel_revision_hash: u64,
1171        cost_expr: Option<&'static crate::fkc::CompiledCostExpr>,
1172    ) {
1173        let key = (op.into(), SmallVec::from_slice(dtypes), backend);
1174        let entry = BindingEntry {
1175            kernel,
1176            caps,
1177            precision,
1178            cost,
1179            kernel_source,
1180            is_generic,
1181            kernel_revision_hash,
1182            cost_expr,
1183        };
1184        // Append-only (never-panic): distinct `KernelRef`s compose as
1185        // sibling alternatives at one decision point; the *same*
1186        // function pointer registered twice is NOT rejected inline.
1187        // [`Self::finalize`] detects duplicates after bulk registration
1188        // and returns a `Result`, so the static init boundary can
1189        // fail-fast (`.expect()`) while a dynamic importer surfaces the
1190        // error — honoring never-panic on the import path.
1191        self.bindings.entry(key).or_default().push(entry);
1192    }
1193
1194    /// Validate the fully-assembled table: within the alternative set
1195    /// for any one `(op, dtypes, backend)` key, each `KernelRef`
1196    /// function pointer must appear **at most once**. Distinct
1197    /// alternatives compose as siblings; the same pointer twice is a
1198    /// registration bug (a copy-paste in a hand-written table, or a
1199    /// contract importer wiring one kernel onto two keys that collapse).
1200    ///
1201    /// `register*` is append-only and never panics; this single pass —
1202    /// run once after bulk registration — surfaces a duplicate as a
1203    /// typed `Result`. The static init boundary
1204    /// ([`crate::dispatch::global_bindings`]) `.expect()`s it (fail-fast
1205    /// at startup for hand-written tables); the FKC contract importer
1206    /// will call it with `?`. This is the never-panic replacement for
1207    /// the former inline duplicate `panic!`.
1208    pub fn finalize(&self) -> Result<()> {
1209        for ((op, dtypes, backend), alts) in self.bindings.iter() {
1210            for i in 0..alts.len() {
1211                let ptr_i = alts[i].kernel as *const () as usize;
1212                for entry_j in &alts[i + 1..] {
1213                    if entry_j.kernel as *const () as usize == ptr_i {
1214                        return Err(Error::Msg(format!(
1215                            "KernelBindingTable: duplicate KernelRef registered \
1216                             for (op={op:?}, dtypes={dtypes:?}, backend={backend:?}). \
1217                             The same function pointer was registered twice; \
1218                             distinct alternatives at one decision point must be \
1219                             distinct `fn` items."
1220                        )));
1221                    }
1222                }
1223            }
1224        }
1225        Ok(())
1226    }
1227
1228    /// Phase 7.6 step 7b: upgrade every UNAUDITED-precision CPU
1229    /// registration to the supplied `default`. Convention is to call
1230    /// this at the *end* of bulk registration, so any
1231    /// `register_with_precision(...)` calls that explicitly claimed a
1232    /// non-default value are preserved. The architecture-target
1233    /// shape would be precision-per-call-site, but for the ~335 CPU
1234    /// primitive registrations that all share the deterministic
1235    /// F32-accumulator property, a fill pass keeps the call sites
1236    /// concise without sacrificing the architectural commitment
1237    /// (every entry ends up with an explicit, non-UNAUDITED precision
1238    /// claim before lookup is ever exercised).
1239    ///
1240    /// Only entries with `backend == BackendId::Cpu` and the current
1241    /// `PrecisionGuarantee::UNAUDITED` sentinel are touched. Non-CPU
1242    /// backends register their own precision claims; non-UNAUDITED
1243    /// entries are preserved.
1244    ///
1245    /// Step 9a: applies to **every alternative** registered under a
1246    /// CPU key, not just the first — so a key with N siblings all
1247    /// starting at UNAUDITED ends with N entries upgraded to `default`.
1248    pub fn fill_unset_cpu_precision(&mut self, default: crate::fused::PrecisionGuarantee) {
1249        for ((_, _, backend), alts) in self.bindings.iter_mut() {
1250            if *backend != BackendId::Cpu {
1251                continue;
1252            }
1253            for entry in alts.iter_mut() {
1254                let p = &mut entry.precision;
1255                // Upgrade entries that claim nothing — EXCEPT an audited
1256                // "no static bound applies" conclusion.
1257                //
1258                // GAP-095: the old predicate was purely structural ("all four
1259                // value fields at defaults"), and the comment here asserted
1260                // that "a real weaker claim would set at least one of them."
1261                // That is false for `PrecisionGuarantee::none(reason)`, which
1262                // is field-identical to UNAUDITED and sets none of them. The
1263                // fill therefore overwrote an explicit audit conclusion with
1264                // `bit_stable_on_same_hardware: true` — asserting on the
1265                // kernel's behalf exactly what its audit had declined to
1266                // claim. FKC-4.8-0001 sanctions this fill for blocks left
1267                // absent or `audited: false`; it does not sanction that.
1268                //
1269                // Note what must KEEP being upgraded: a contract claim that
1270                // the V-FKC-9 ledger gate downgraded collapses to the
1271                // UNAUDITED sentinel exactly, so it stays eligible here. That
1272                // is load-bearing — every one of the CPU table's bit-stable
1273                // entries arrives through this path (GAP-077), so excluding
1274                // them would break the FKC-4.8-0001 coverage commitment
1275                // outright.
1276                if p.makes_no_static_claim() && !p.is_audited_no_bound() {
1277                    *p = default;
1278                }
1279            }
1280        }
1281    }
1282
1283    /// Phase 7.6 step 8: upgrade every still-`unknown_cost` CPU
1284    /// registration to the cost function returned by `dispatcher` for
1285    /// its OpKind. The dispatcher's contract: given an `OpKind`,
1286    /// return the appropriate cost-family function — typically
1287    /// [`crate::cost::default_cost_for_op_kind`] in production code,
1288    /// but tests may pass their own dispatcher to exercise specific
1289    /// cost shapes.
1290    ///
1291    /// Convention is to call this at the *end* of bulk registration,
1292    /// so any `register_full(...)` calls that explicitly claimed a
1293    /// non-default cost are preserved. The function-pointer equality
1294    /// check identifies UNKNOWN entries — any caller that wants a
1295    /// "weaker" claim with non-zero values must use a distinct
1296    /// function pointer (which won't compare equal to `unknown_cost`
1297    /// and thus won't be overwritten).
1298    ///
1299    /// Step 9a: applies to **every alternative** with a CPU backend —
1300    /// each sibling entry at a key starting at `unknown_cost` is
1301    /// upgraded independently.
1302    pub fn fill_unset_cpu_cost(&mut self, dispatcher: fn(OpKind) -> CostFn) {
1303        self.fill_unset_cost_for_backend(BackendId::Cpu, dispatcher);
1304    }
1305
1306    /// Per-backend variant of [`Self::fill_unset_cpu_cost`]. Walks
1307    /// every alternative whose backend matches `backend` and replaces
1308    /// `unknown_cost` entries with the dispatcher's choice for the
1309    /// op. Used by [`crate::vulkan_dispatch::register_vulkan_kernels`]
1310    /// to bulk-fill cost functions after per-kernel precision
1311    /// registration; future MKL/AOCL/Metal backends register through
1312    /// this same helper.
1313    ///
1314    /// Backend-specific cost adjustments (e.g. higher
1315    /// `kernel_overhead_ns` for Vulkan command-buffer submission vs
1316    /// CPU's nested-loop call overhead) are a Layer-2 refinement —
1317    /// today's `default_cost_for_op_kind` returns the same CPU-flavored
1318    /// functions regardless of backend, which over-estimates Vulkan
1319    /// throughput on small tensors. Either pass a Vulkan-flavored
1320    /// dispatcher to this method, or rely on the empirical calibration
1321    /// framework's per-(op, dtype, backend) refinement.
1322    pub fn fill_unset_cost_for_backend(
1323        &mut self,
1324        backend: BackendId,
1325        dispatcher: fn(OpKind) -> CostFn,
1326    ) {
1327        let sentinel = unknown_cost as *const () as usize;
1328        for ((key, _, this_backend), alts) in self.bindings.iter_mut() {
1329            if *this_backend != backend {
1330                continue;
1331            }
1332            // Runtime-fused entries keep the sentinel: their cost derives from
1333            // the op's recipe (keyed by the runtime id), not an OpKind table.
1334            let BindingKey::Static(op) = key else {
1335                continue;
1336            };
1337            for entry in alts.iter_mut() {
1338                if (entry.cost as usize) == sentinel {
1339                    entry.cost = dispatcher(*op);
1340                }
1341            }
1342        }
1343    }
1344
1345    /// Look up the [`crate::fused::PrecisionGuarantee`] for a
1346    /// registered `(op, dtypes, backend)` triple. Returns the **first
1347    /// alternative**'s precision; multi-impl callers wanting all
1348    /// alternatives use [`Self::lookup_alternatives`]. Returns
1349    /// `PrecisionGuarantee::UNAUDITED` if the binding is missing —
1350    /// callers (notably the step-7b lint) treat that as "no claim"
1351    /// rather than panicking.
1352    pub fn lookup_precision(
1353        &self,
1354        op: impl Into<BindingKey>,
1355        dtypes: &[DType],
1356        backend: BackendId,
1357    ) -> crate::fused::PrecisionGuarantee {
1358        let key = (op.into(), SmallVec::from_slice(dtypes), backend);
1359        self.bindings
1360            .get(&key)
1361            .and_then(|alts| alts.first())
1362            .map(|e| e.precision)
1363            .unwrap_or(crate::fused::PrecisionGuarantee::UNAUDITED)
1364    }
1365
1366    /// Look up the [`CostFn`] for a registered `(op, dtypes, backend)`
1367    /// triple. Returns the **first alternative**'s cost; multi-impl
1368    /// callers wanting all alternatives use
1369    /// [`Self::lookup_alternatives`]. Returns [`unknown_cost`] for
1370    /// missing bindings — the step-8 lint treats that as "no cost
1371    /// claim."
1372    pub fn lookup_cost(
1373        &self,
1374        op: impl Into<BindingKey>,
1375        dtypes: &[DType],
1376        backend: BackendId,
1377    ) -> CostFn {
1378        let key = (op.into(), SmallVec::from_slice(dtypes), backend);
1379        self.bindings
1380            .get(&key)
1381            .and_then(|alts| alts.first())
1382            .map(|e| e.cost)
1383            .unwrap_or(unknown_cost)
1384    }
1385
1386    /// Iterate `(op, dtypes, backend, precision)` over every
1387    /// registered alternative — one tuple per `BindingEntry`, not per
1388    /// key. Used by the step-7b coverage lint, which groups by
1389    /// OpKind and checks the bit-stable commitment per group; the
1390    /// grouping handles N-alternatives-per-key naturally.
1391    pub fn iter_precision(
1392        &self,
1393    ) -> impl Iterator<
1394        Item = (
1395            OpKind,
1396            &[DType],
1397            BackendId,
1398            crate::fused::PrecisionGuarantee,
1399        ),
1400    > {
1401        self.bindings
1402            .iter()
1403            .filter_map(|((key, dtypes, backend), alts)| {
1404                // Static-kernel audit view: runtime-fused entries are outside the
1405                // per-OpKind commitments these iterators exist to check.
1406                key.static_op().map(|op| (op, dtypes, backend, alts))
1407            })
1408            .flat_map(|(op, dtypes, backend, alts)| {
1409                alts.iter()
1410                    .map(move |e| (op, dtypes.as_slice(), *backend, e.precision))
1411            })
1412    }
1413
1414    /// Iterate `(op, dtypes, backend, &entry)` over every registered
1415    /// alternative — one tuple per `BindingEntry`, mirroring
1416    /// [`Self::iter_precision`] but yielding the FULL entry (by reference)
1417    /// instead of projecting out just the `precision` field. Used by the
1418    /// FKC empirical verification harness (Task 4.4+), which needs the
1419    /// whole `BindingEntry` (the `kernel` fn pointer to invoke, `caps`,
1420    /// `kernel_revision_hash` for ledger keys, ...), not just one field.
1421    /// Runtime-fused/JIT entries are filtered out (`static_op()` returns
1422    /// `None` for them) — same "static-kernel audit view" as
1423    /// `iter_precision`/`iter_cost`.
1424    pub fn iter_entries(
1425        &self,
1426    ) -> impl Iterator<Item = (OpKind, &[DType], BackendId, &BindingEntry)> {
1427        self.bindings
1428            .iter()
1429            .filter_map(|((key, dtypes, backend), alts)| {
1430                key.static_op().map(|op| (op, dtypes, backend, alts))
1431            })
1432            .flat_map(|(op, dtypes, backend, alts)| {
1433                alts.iter()
1434                    .map(move |e| (op, dtypes.as_slice(), *backend, e))
1435            })
1436    }
1437
1438    /// Iterate `(op, dtypes, backend, cost_fn)` over every registered
1439    /// alternative — one tuple per `BindingEntry`. Used by the step-8
1440    /// coverage lint to check the "every primitive op has a
1441    /// non-default cost function" commitment per OpKind.
1442    pub fn iter_cost(&self) -> impl Iterator<Item = (OpKind, &[DType], BackendId, CostFn)> {
1443        self.bindings
1444            .iter()
1445            .filter_map(|((key, dtypes, backend), alts)| {
1446                key.static_op().map(|op| (op, dtypes, backend, alts))
1447            })
1448            .flat_map(|(op, dtypes, backend, alts)| {
1449                alts.iter()
1450                    .map(move |e| (op, dtypes.as_slice(), *backend, e.cost))
1451            })
1452    }
1453
1454    /// Iterate every registered `(op, dtypes, backend)` decision-point
1455    /// key — one item per key, regardless of how many alternatives
1456    /// share it. Consumed by `SystemTopology::build_at` to derive
1457    /// which backends have kernels registered + which (op, dtype)
1458    /// pairs each backend covers.
1459    pub fn iter_keys(&self) -> impl Iterator<Item = (OpKind, &[DType], BackendId)> {
1460        self.bindings.keys().filter_map(|(key, dtypes, backend)| {
1461            key.static_op().map(|op| (op, dtypes.as_slice(), *backend))
1462        })
1463    }
1464
1465    /// Whether ANY runtime-fused entry is bound for `(fid, backend)`,
1466    /// regardless of dtype tuple — the coarse capability-gate query (the
1467    /// optimizer's fuse/don't-fuse predicate is id-level; the dtype-precise
1468    /// check is the dispatch-time [`Self::lookup_with_caps`] itself).
1469    pub fn has_runtime_fused(&self, fid: FusedOpId, backend: BackendId) -> bool {
1470        self.bindings
1471            .keys()
1472            .any(|(k, _, b)| *b == backend && matches!(k, BindingKey::RuntimeFused(f) if *f == fid))
1473    }
1474
1475    /// Resolve `(fid, backend)` to a runtime-fused binding, **refusing to pick
1476    /// when the answer is ambiguous**.
1477    ///
1478    /// A [`FusedOpId`] names a RECIPE, not a signature: `register_runtime_fused`
1479    /// deduplicates on the region's base-map hash, so one id can legitimately
1480    /// carry several dtype tuples — `[F32, F32]` and `[F32, F32, F32]` are
1481    /// different arities under the same id. The pre-GAP-213 `first_runtime_fused` scanned
1482    /// for `(fid, backend)` and **binds `dtypes` without testing it**, so it
1483    /// answers such a question by `HashMap::iter()` order: a coin flip per
1484    /// process, and capable of returning a kernel of the wrong operand arity.
1485    ///
1486    /// The three outcomes are kept as three values rather than folded into an
1487    /// `Option`, because *not bound* and *ambiguous* are different facts with
1488    /// different fixes, and an `Option` would make them indistinguishable to the
1489    /// caller — the same collapse that let GAP-001 read as a version regression
1490    /// for eleven days.
1491    ///
1492    /// **Scope: this is the diagnostic/test resolution path.** Production
1493    /// dispatch never came through here — it uses [`Self::lookup_with_caps`],
1494    /// which takes the dtype tuple and does an exact keyed `get`, so it can
1495    /// neither be ambiguous nor return a wrong arity. See GAP-213.
1496    pub fn unique_runtime_fused(
1497        &self,
1498        fid: FusedOpId,
1499        backend: BackendId,
1500    ) -> RuntimeFusedLookup<'_> {
1501        let mut matches = self.bindings.iter().filter(|((k, _, b), _)| {
1502            *b == backend && matches!(k, BindingKey::RuntimeFused(f) if *f == fid)
1503        });
1504        let Some(((_, dtypes, _), alts)) = matches.next() else {
1505            return RuntimeFusedLookup::NotBound;
1506        };
1507        if matches.next().is_some() {
1508            let mut signatures: Vec<Vec<DType>> = self
1509                .bindings
1510                .iter()
1511                .filter(|((k, _, b), _)| {
1512                    *b == backend && matches!(k, BindingKey::RuntimeFused(f) if *f == fid)
1513                })
1514                .map(|((_, d, _), _)| d.to_vec())
1515                .collect();
1516            // HashMap order is per-process, so the report would otherwise vary run to
1517            // run. `DType` is not `Ord`, so key on arity then the debug spelling —
1518            // a presentation order only; nothing depends on which comes first.
1519            signatures.sort_by_key(|d| (d.len(), format!("{d:?}")));
1520            return RuntimeFusedLookup::Ambiguous { signatures };
1521        }
1522        match alts.first() {
1523            Some(e) => RuntimeFusedLookup::Unique(dtypes.as_slice(), e),
1524            None => RuntimeFusedLookup::NotBound,
1525        }
1526    }
1527
1528    /// **TEST-ONLY.** Drop every runtime-fused entry (static entries retained).
1529    /// The runtime-fused reset story: the metadata sidecar's Vec length is the
1530    /// id allocator, so clearing it MUST clear these bindings in the same
1531    /// breath or a reused id resolves a stale kernel
1532    /// (`runtime_fused_kernels::clear_runtime_fused_for_tests` does both).
1533    #[doc(hidden)]
1534    pub fn remove_runtime_fused_for_tests(&mut self) {
1535        self.bindings
1536            .retain(|(k, _, _), _| matches!(k, BindingKey::Static(_)));
1537    }
1538
1539    /// Look up the wrapper for `(op, dtypes, backend)`. Returns the
1540    /// **first alternative**'s [`KernelRef`]; for capability-aware
1541    /// lookup use [`Self::lookup_with_caps`]; for the full alternative
1542    /// set use [`Self::lookup_alternatives`].
1543    pub fn lookup(
1544        &self,
1545        op: impl Into<BindingKey>,
1546        dtypes: &[DType],
1547        backend: BackendId,
1548    ) -> Result<KernelRef> {
1549        self.lookup_with_caps(op, dtypes, backend).map(|(k, _)| k)
1550    }
1551
1552    /// Capability-aware lookup. Returns the first **dense-safe** alternative
1553    /// — the first sibling whose caps do NOT set
1554    /// [`KernelCaps::requires_broadcast`] — paired with its registered
1555    /// [`KernelCaps`]. A baked-broadcast sibling (`broadcast_stride0:
1556    /// required`) is SKIPPED, so a dense operand is never routed into a
1557    /// kernel that reads `in[0]`; the exclusion is order-independent (path
1558    /// 1a). For every existing binding — none set `requires_broadcast` —
1559    /// this is byte-identical to returning the first-registered alternative.
1560    /// Surfaces [`Error::NoBackendForOp`] on a missing binding OR when every
1561    /// sibling is broadcast-baked (production-correct: a surfaced miss, never
1562    /// a wrong read, no panic).
1563    pub fn lookup_with_caps(
1564        &self,
1565        op: impl Into<BindingKey>,
1566        dtypes: &[DType],
1567        backend: BackendId,
1568    ) -> Result<(KernelRef, KernelCaps)> {
1569        let key = (op.into(), SmallVec::from_slice(dtypes), backend);
1570        self.bindings
1571            .get(&key)
1572            .and_then(|alts| alts.iter().find(|e| !e.caps.requires_broadcast))
1573            .map(|e| (e.kernel, e.caps))
1574            .ok_or_else(|| {
1575                let (available_backends, supported_combinations) = self.coverage_diagnostic();
1576                Error::NoBackendForOp {
1577                    op: key.0.static_op().unwrap_or(OpKind::RuntimeFused),
1578                    dtypes: dtypes.to_vec(),
1579                    available_backends,
1580                    supported_combinations,
1581                }
1582                .bt()
1583            })
1584    }
1585
1586    /// Build the truthful `(available_backends, supported_combinations)`
1587    /// diagnostic pair for a [`Error::NoBackendForOp`] miss: the DISTINCT
1588    /// registered backends, and the full static `(backend, op, dtypes)` coverage
1589    /// (the useful "what exists" context). Shared by
1590    /// [`lookup_with_caps`](Self::lookup_with_caps) AND the plan-time miss
1591    /// (`plan::missing_binding_error`) so BOTH surface the same real context. The
1592    /// plan-time path formerly hardcoded `[]` here, which falsely reads as "no
1593    /// backend is registered" and misdirects diagnosis — a mixed-precision
1594    /// `(op, dtypes)` gap is not an empty registry (observed in the wild:
1595    /// `[F32, BF16, F32]` matmul reported as "available backends: []").
1596    pub fn coverage_diagnostic(&self) -> (Vec<BackendId>, Vec<CoverageBinding>) {
1597        let available_backends: Vec<BackendId> = self
1598            .bindings
1599            .keys()
1600            .map(|(_, _, b)| *b)
1601            .collect::<std::collections::HashSet<_>>()
1602            .into_iter()
1603            .collect();
1604        let supported_combinations: Vec<(BackendId, OpKind, Vec<DType>)> = self
1605            .bindings
1606            .keys()
1607            .filter_map(|(k, d, b)| k.static_op().map(|o| (*b, o, d.to_vec())))
1608            .collect();
1609        (available_backends, supported_combinations)
1610    }
1611
1612    /// Does **this specific backend** serve `(op, dtypes)` with a usable
1613    /// (non-broadcast-baked) binding?
1614    ///
1615    /// Mirrors [`lookup_with_caps`](Self::lookup_with_caps)'s success condition
1616    /// exactly — same key, same `!requires_broadcast` admission — but skips
1617    /// constructing the miss diagnostic, so it is cheap enough to call in a
1618    /// per-node loop (`coverage_diagnostic` walks the entire table).
1619    ///
1620    /// Ask this once per *candidate* backend. There is deliberately no
1621    /// "does any backend anywhere serve it?" sibling: a predicate that ignores
1622    /// the key's backend component cannot answer a dispatch question, because a
1623    /// node executes on one backend rather than on the union of them. Such a
1624    /// sibling did exist (`any_backend_supports`) and it caused a real defect —
1625    /// the dtype-reconciliation pass consulted it, so a backend a node could
1626    /// never run on suppressed the `Op::Cast` that node needed and planning
1627    /// dead-ended at `NoBackendForOp`. See `crate::plan::candidate_backends_for`.
1628    pub fn backend_supports(
1629        &self,
1630        op: impl Into<BindingKey>,
1631        dtypes: &[DType],
1632        backend: BackendId,
1633    ) -> bool {
1634        let key = (op.into(), SmallVec::from_slice(dtypes), backend);
1635        self.bindings
1636            .get(&key)
1637            .is_some_and(|alts| alts.iter().any(|e| !e.caps.requires_broadcast))
1638    }
1639
1640    /// Phase 7.6 step 9a: return the full set of registered
1641    /// alternatives at the `(op, dtypes, backend)` decision point.
1642    /// Order is registration order — append-on-register is the step-9a
1643    /// contract. The empty slice means "no kernel registered at this
1644    /// decision point."
1645    ///
1646    /// The route picker (step 9b) consumes this to rank alternatives
1647    /// by cost + precision at plan time. Single-impl callers continue
1648    /// to use [`Self::lookup_with_caps`] (which returns the first
1649    /// alternative — the route picker's job is the longer-term home
1650    /// for selection logic).
1651    pub fn lookup_alternatives(
1652        &self,
1653        op: impl Into<BindingKey>,
1654        dtypes: &[DType],
1655        backend: BackendId,
1656    ) -> &[BindingEntry] {
1657        let key = (op.into(), SmallVec::from_slice(dtypes), backend);
1658        self.bindings
1659            .get(&key)
1660            .map(|alts| alts.as_slice())
1661            .unwrap_or(&[])
1662    }
1663
1664    /// Total number of registered alternatives across all keys (not
1665    /// just unique keys). Step 9a: a key with N siblings counts N.
1666    pub fn len(&self) -> usize {
1667        self.bindings.values().map(|alts| alts.len()).sum()
1668    }
1669
1670    /// Empty binding table.
1671    pub fn is_empty(&self) -> bool {
1672        self.bindings.is_empty()
1673    }
1674
1675    /// Total number of unique `(op, dtypes, backend)` keys. Step 9a:
1676    /// distinct from [`Self::len`], which counts alternatives.
1677    pub fn key_count(&self) -> usize {
1678        self.bindings.len()
1679    }
1680}
1681
1682impl std::fmt::Debug for KernelBindingTable {
1683    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1684        f.debug_struct("KernelBindingTable")
1685            .field("keys", &self.bindings.len())
1686            .field("total_alternatives", &self.len())
1687            .finish_non_exhaustive()
1688    }
1689}
1690
1691#[cfg(test)]
1692mod tests {
1693    use super::*;
1694    use fuel_ir::DType;
1695
1696    /// Smoke: KernelRef can be constructed and stored.
1697    #[test]
1698    fn kernel_ref_stores_function_pointer() {
1699        fn dummy_kernel(
1700            _inputs: &[Arc<RwLock<Storage>>],
1701            _outputs: &mut [Arc<RwLock<Storage>>],
1702            _layouts: &[Layout],
1703            _params: &OpParams,
1704        ) -> Result<()> {
1705            Ok(())
1706        }
1707        let k: KernelRef = dummy_kernel;
1708        // Function pointer is Copy + Clone.
1709        let _k2 = k;
1710        let _k3: KernelRef = k;
1711    }
1712
1713    /// Smoke: OpParams variants construct cleanly and Debug-format.
1714    #[test]
1715    fn op_params_variants_construct() {
1716        let _ = OpParams::None;
1717        let _ = OpParams::Reduce {
1718            dims: vec![0, 1],
1719            keepdim: false,
1720        };
1721        let _ = OpParams::Matmul {
1722            lhs_batch_dims: vec![],
1723            rhs_batch_dims: vec![],
1724            m: 4,
1725            n: 8,
1726            k: 16,
1727            m_compute: MatmulM::All,
1728        };
1729        let _ = OpParams::Slice {
1730            dim: 0,
1731            start: 0,
1732            end: 10,
1733            step: 1,
1734        };
1735        let _ = OpParams::Cast;
1736        let _ = OpParams::Affine { mul: 2.0, add: 1.0 };
1737        // Debug format compiles.
1738        let _ = format!("{:?}", OpParams::None);
1739    }
1740
1741    /// Smoke: a hand-constructed kernel that allocates the output
1742    /// Storage outside this crate would be:
1743    ///   1. allocate output via fuel_memory::alloc_cpu_zeroed
1744    ///   2. wrap inputs as &[Arc<RwLock<Storage>>]
1745    ///   3. call the kernel
1746    ///
1747    /// Phase B5 lands the first such migration; B1 just type-checks
1748    /// the surface.
1749    #[test]
1750    fn kernel_ref_can_be_called() {
1751        fn copy_kernel(
1752            inputs: &[Arc<RwLock<Storage>>],
1753            outputs: &mut [Arc<RwLock<Storage>>],
1754            _layouts: &[Layout],
1755            _params: &OpParams,
1756        ) -> Result<()> {
1757            // Simplest real kernel: copy bytes from inputs[0] to outputs[0].
1758            let in_arc = &inputs[0];
1759            let out_arc = &outputs[0];
1760            let in_guard = in_arc.read().unwrap();
1761            let mut out_guard = out_arc.write().unwrap();
1762            let in_bytes = match &in_guard.inner {
1763                fuel_memory::BackendStorage::Cpu(s) => s.bytes(),
1764                #[allow(unreachable_patterns)]
1765                _ => return Ok(()),
1766            };
1767            if let fuel_memory::BackendStorage::Cpu(s) = &mut out_guard.inner {
1768                s.bytes_mut().copy_from_slice(in_bytes);
1769            }
1770            Ok(())
1771        }
1772
1773        let input = fuel_memory::from_slice_cpu(&[1.0_f32, 2.0, 3.0, 4.0]);
1774        let output = fuel_memory::alloc_cpu_zeroed(DType::F32, 4).unwrap();
1775        let inputs = vec![Arc::new(RwLock::new(input))];
1776        let mut outputs = vec![Arc::new(RwLock::new(output))];
1777
1778        let k: KernelRef = copy_kernel;
1779        k(&inputs, &mut outputs, &[], &OpParams::None).unwrap();
1780
1781        // Output bytes match input.
1782        let out_guard = outputs[0].read().unwrap();
1783        if let fuel_memory::BackendStorage::Cpu(s) = &out_guard.inner {
1784            let typed: &[f32] = s.as_slice().unwrap();
1785            assert_eq!(typed, &[1.0, 2.0, 3.0, 4.0]);
1786        }
1787    }
1788
1789    fn ok_kernel(
1790        _inputs: &[Arc<RwLock<Storage>>],
1791        _outputs: &mut [Arc<RwLock<Storage>>],
1792        _layouts: &[Layout],
1793        _params: &OpParams,
1794    ) -> Result<()> {
1795        Ok(())
1796    }
1797
1798    fn ok_kernel_alt(
1799        _inputs: &[Arc<RwLock<Storage>>],
1800        _outputs: &mut [Arc<RwLock<Storage>>],
1801        _layouts: &[Layout],
1802        _params: &OpParams,
1803    ) -> Result<()> {
1804        Ok(())
1805    }
1806
1807    /// Phase 7.6 step 9a: two distinct `KernelRef`s register against
1808    /// the same `(op, dtypes, backend)` key as sibling alternatives.
1809    /// `lookup_with_caps` returns the first; `lookup_alternatives`
1810    /// returns both in registration order.
1811    #[test]
1812    fn step_9a_two_alternatives_append_then_first_wins_on_legacy_lookup() {
1813        use fuel_ir::probe::BackendId;
1814        let mut table = KernelBindingTable::new();
1815        let dts = [DType::BF16, DType::BF16, DType::BF16];
1816        table.register(OpKind::MatMul, &dts, BackendId::Cuda, ok_kernel);
1817        table.register(OpKind::MatMul, &dts, BackendId::Cuda, ok_kernel_alt);
1818
1819        // Legacy single-impl lookup returns the first-registered.
1820        let (k1, _caps) = table
1821            .lookup_with_caps(OpKind::MatMul, &dts, BackendId::Cuda)
1822            .unwrap();
1823        assert_eq!(k1 as *const () as usize, ok_kernel as *const () as usize);
1824
1825        // Multi-impl lookup returns both in registration order.
1826        let alts = table.lookup_alternatives(OpKind::MatMul, &dts, BackendId::Cuda);
1827        assert_eq!(alts.len(), 2);
1828        assert_eq!(
1829            alts[0].kernel as *const () as usize,
1830            ok_kernel as *const () as usize
1831        );
1832        assert_eq!(
1833            alts[1].kernel as *const () as usize,
1834            ok_kernel_alt as *const () as usize
1835        );
1836
1837        // Step-9a accounting helpers stay consistent.
1838        assert_eq!(table.key_count(), 1, "one decision point");
1839        assert_eq!(table.len(), 2, "two alternatives total");
1840    }
1841
1842    /// FKC gap-closure Task 2.1: hand-written registrations (the
1843    /// `register`/`register_full*` family that never touches the FKC
1844    /// importer) carry no declared cost-expression AST — `cost_expr`
1845    /// defaults to `None`. Only the FKC importer (a later Group-2 task)
1846    /// stamps a real `Some(&CompiledCostExpr)`.
1847    #[test]
1848    fn cost_expr_field_defaults_none_for_hand_written_registration() {
1849        use fuel_ir::probe::BackendId;
1850        let mut table = KernelBindingTable::new();
1851        let dts = [DType::F32, DType::F32, DType::F32];
1852        table.register(OpKind::AddElementwise, &dts, BackendId::Cpu, ok_kernel);
1853        let alts = table.lookup_alternatives(OpKind::AddElementwise, &dts, BackendId::Cpu);
1854        assert_eq!(alts.len(), 1);
1855        assert!(
1856            alts[0].cost_expr.is_none(),
1857            "hand-written registrations carry no declared cost AST"
1858        );
1859    }
1860
1861    /// Task 4.4: `iter_entries` mirrors `iter_precision`/`iter_cost` —
1862    /// yields one `(op, dtypes, backend, &entry)` tuple per registered
1863    /// alternative (both siblings at a shared key show up), and filters
1864    /// out `RuntimeFused` keys just like the precision/cost audit
1865    /// iterators (a "static-kernel audit view").
1866    #[test]
1867    fn iter_entries_yields_full_entries_and_filters_runtime_fused() {
1868        use fuel_ir::probe::BackendId;
1869        let mut table = KernelBindingTable::new();
1870        let dts = [DType::F32, DType::F32, DType::F32];
1871        table.register(OpKind::AddElementwise, &dts, BackendId::Cpu, ok_kernel);
1872        table.register(OpKind::AddElementwise, &dts, BackendId::Cpu, ok_kernel_alt);
1873        // A runtime-fused key — must be excluded from `iter_entries`.
1874        table.register(
1875            BindingKey::RuntimeFused(FusedOpId(FusedOpId::RUNTIME_FUSED_BASE)),
1876            &dts,
1877            BackendId::Cpu,
1878            ok_kernel,
1879        );
1880
1881        let entries: Vec<_> = table.iter_entries().collect();
1882        assert_eq!(
1883            entries.len(),
1884            2,
1885            "both AddElementwise alternatives, RuntimeFused excluded"
1886        );
1887        for (op, dtypes, backend, entry) in &entries {
1888            assert_eq!(*op, OpKind::AddElementwise);
1889            assert_eq!(*dtypes, dts.as_slice());
1890            assert_eq!(*backend, BackendId::Cpu);
1891            // Full entry is reachable, not just one projected field.
1892            let _ = entry.caps;
1893            let _ = entry.kernel_revision_hash;
1894        }
1895        let fns: Vec<usize> = entries
1896            .iter()
1897            .map(|(_, _, _, e)| e.kernel as *const () as usize)
1898            .collect();
1899        assert!(fns.contains(&(ok_kernel as *const () as usize)));
1900        assert!(fns.contains(&(ok_kernel_alt as *const () as usize)));
1901    }
1902
1903    /// Never-panic (option B): registering the same `KernelRef` twice
1904    /// against one key is append-only — it does NOT panic at
1905    /// registration. `finalize()` detects the duplicate and returns
1906    /// `Err`. (Replaces the former `#[should_panic]` guard.)
1907    #[test]
1908    /// FKC-CLAUSE: FKC-10.10-0001 — the boundary itself: registration stays
1909    /// infallible and `finalize()` RETURNS the duplicate as an error rather than
1910    /// panicking. This is why the clause does not require a `Result` signature on
1911    /// `register_full_with_source`.
1912    fn step_9a_duplicate_kernel_ref_detected_by_finalize_not_panic() {
1913        use fuel_ir::probe::BackendId;
1914        let mut table = KernelBindingTable::new();
1915        let dts = [DType::F32, DType::F32, DType::F32];
1916        table.register(OpKind::MatMul, &dts, BackendId::Cpu, ok_kernel);
1917        table.register(OpKind::MatMul, &dts, BackendId::Cpu, ok_kernel); // dup — must NOT panic
1918        let err = table
1919            .finalize()
1920            .expect_err("finalize must report the duplicate KernelRef");
1921        assert!(
1922            format!("{err}").contains("duplicate KernelRef"),
1923            "unexpected error: {err}"
1924        );
1925    }
1926
1927    /// Distinct sibling kernels under one key finalize cleanly.
1928    #[test]
1929    fn finalize_ok_for_distinct_sibling_kernels() {
1930        use fuel_ir::probe::BackendId;
1931        let mut table = KernelBindingTable::new();
1932        let dts = [DType::F32, DType::F32, DType::F32];
1933        table.register(OpKind::MatMul, &dts, BackendId::Cpu, ok_kernel);
1934        table.register(OpKind::MatMul, &dts, BackendId::Cpu, ok_kernel_alt);
1935        table
1936            .finalize()
1937            .expect("distinct sibling kernels must finalize OK");
1938    }
1939
1940    /// Path 1a: a `broadcast_stride0: required` (baked-broadcast) kernel
1941    /// reads its operand as stride-0; a dense operand routed there
1942    /// miscomputes (reads `in[0]` repeatedly). The realize pick MUST skip
1943    /// such a sibling when a generic one exists, and — the load-bearing
1944    /// part — that exclusion must NOT rest on the generic being registered
1945    /// first. Register the broadcast-baked one FIRST (adversarial: `first()`
1946    /// would return it) and assert the pick returns the GENERIC sibling.
1947    #[test]
1948    fn lookup_with_caps_skips_broadcast_baked_sibling_regardless_of_order() {
1949        use fuel_ir::probe::BackendId;
1950        let mut table = KernelBindingTable::new();
1951        let dts = [DType::F32, DType::F32, DType::F32];
1952        // Baked-broadcast sibling registered FIRST.
1953        table.register_with_caps(
1954            OpKind::AddElementwise,
1955            &dts,
1956            BackendId::Cpu,
1957            ok_kernel,
1958            KernelCaps {
1959                strided_input: true,
1960                requires_broadcast: true,
1961            },
1962        );
1963        // Generic dense-safe sibling registered SECOND.
1964        table.register_with_caps(
1965            OpKind::AddElementwise,
1966            &dts,
1967            BackendId::Cpu,
1968            ok_kernel_alt,
1969            KernelCaps {
1970                strided_input: true,
1971                requires_broadcast: false,
1972            },
1973        );
1974
1975        let (picked, caps) = table
1976            .lookup_with_caps(OpKind::AddElementwise, &dts, BackendId::Cpu)
1977            .expect("a dense-safe sibling exists");
1978        assert!(
1979            !caps.requires_broadcast,
1980            "the realize pick must never be a broadcast-baked kernel",
1981        );
1982        assert_eq!(
1983            picked as *const () as usize, ok_kernel_alt as *const () as usize,
1984            "lookup_with_caps must skip the broadcast-baked sibling and return the generic \
1985             one regardless of registration order",
1986        );
1987        // Both siblings coexist — the exclusion is at the pick, not at
1988        // registration (the baked-broadcast contract is still PRESENT).
1989        let alts = table.lookup_alternatives(OpKind::AddElementwise, &dts, BackendId::Cpu);
1990        assert_eq!(
1991            alts.len(),
1992            2,
1993            "both siblings registered; neither is dropped"
1994        );
1995    }
1996
1997    /// The corner that keeps the exclusion honest: if EVERY sibling is
1998    /// broadcast-baked (no dense-safe fallback), the pick is a surfaced
1999    /// miss (`NoBackendForOp`), NOT a silent wrong dense read.
2000    #[test]
2001    fn lookup_with_caps_misses_when_only_broadcast_baked_siblings_exist() {
2002        use fuel_ir::probe::BackendId;
2003        let mut table = KernelBindingTable::new();
2004        let dts = [DType::F32, DType::F32, DType::F32];
2005        table.register_with_caps(
2006            OpKind::AddElementwise,
2007            &dts,
2008            BackendId::Cpu,
2009            ok_kernel,
2010            KernelCaps {
2011                strided_input: true,
2012                requires_broadcast: true,
2013            },
2014        );
2015        assert!(
2016            table
2017                .lookup_with_caps(OpKind::AddElementwise, &dts, BackendId::Cpu)
2018                .is_err(),
2019            "no dense-safe sibling ⇒ honest miss, never a wrong read",
2020        );
2021    }
2022
2023    /// Phase 7.6 step 9a: `fill_unset_cpu_precision` upgrades every
2024    /// alternative under a CPU key, not just the first.
2025    #[test]
2026    fn step_9a_fill_passes_touch_every_alternative() {
2027        use crate::fused::PrecisionGuarantee;
2028        use fuel_ir::probe::BackendId;
2029        let mut table = KernelBindingTable::new();
2030        let dts = [DType::F32, DType::F32, DType::F32];
2031        table.register(OpKind::AddElementwise, &dts, BackendId::Cpu, ok_kernel);
2032        table.register(OpKind::AddElementwise, &dts, BackendId::Cpu, ok_kernel_alt);
2033
2034        // Both start at UNAUDITED.
2035        for e in table.lookup_alternatives(OpKind::AddElementwise, &dts, BackendId::Cpu) {
2036            assert!(!e.precision.bit_stable_on_same_hardware);
2037        }
2038
2039        let bit_stable = PrecisionGuarantee {
2040            bit_stable_on_same_hardware: true,
2041            max_ulp: Some(0),
2042            max_relative: None,
2043            max_absolute: None,
2044            notes: "test default",
2045        };
2046        table.fill_unset_cpu_precision(bit_stable);
2047
2048        // Both got upgraded.
2049        let alts = table.lookup_alternatives(OpKind::AddElementwise, &dts, BackendId::Cpu);
2050        assert_eq!(alts.len(), 2);
2051        for e in alts {
2052            assert!(e.precision.bit_stable_on_same_hardware);
2053            assert_eq!(e.precision.max_ulp, Some(0));
2054        }
2055    }
2056
2057    /// GAP-095: the CPU bulk fill must NOT overwrite an audited
2058    /// "no static bound applies" conclusion.
2059    ///
2060    /// `PrecisionGuarantee::none(reason)` and `PrecisionGuarantee::UNAUDITED`
2061    /// carry identical *values* — all four claim fields at their defaults —
2062    /// and differ only in `notes`. The fill's predicate was purely
2063    /// structural, so it conflated them and rewrote an explicit audit
2064    /// conclusion into `PRIMITIVE_DETERMINISTIC_CPU`: it asserted
2065    /// `bit_stable_on_same_hardware: true` on a kernel whose audit had
2066    /// specifically declined to claim it.
2067    ///
2068    /// FKC-4.8-0001 sanctions the fill for precision blocks left *absent* or
2069    /// `audited: false`. It does not sanction overwriting an `audited: true`
2070    /// + `none(reason)` conclusion.
2071    ///
2072    /// **Both directions asserted.** A test that only checked that
2073    /// `none(reason)` survives would also pass with the fill disabled
2074    /// outright, so the UNAUDITED sibling must still be upgraded in the same
2075    /// run — otherwise the test proves nothing about discrimination.
2076    #[test]
2077    fn gap_095_fill_preserves_audited_none_but_still_upgrades_unaudited() {
2078        use crate::fused::PrecisionGuarantee;
2079        use fuel_ir::probe::BackendId;
2080        let mut table = KernelBindingTable::new();
2081        let dts = [DType::F32, DType::F32, DType::F32];
2082
2083        const REASON: &str = "audited: accumulation order is scheduler-dependent; \
2084                              no static bound applies";
2085        // The audited conclusion — must survive untouched.
2086        table.register_with_precision(
2087            OpKind::MatMul,
2088            &dts,
2089            BackendId::Cpu,
2090            ok_kernel,
2091            PrecisionGuarantee::none(REASON),
2092        );
2093        // The never-audited placeholder — must still be upgraded.
2094        table.register(OpKind::AddElementwise, &dts, BackendId::Cpu, ok_kernel);
2095
2096        table.fill_unset_cpu_precision(PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU);
2097
2098        let audited = table
2099            .lookup_alternatives(OpKind::MatMul, &dts, BackendId::Cpu)
2100            .first()
2101            .expect("audited-none entry registered")
2102            .precision;
2103        assert!(
2104            !audited.bit_stable_on_same_hardware,
2105            "the fill asserted bit-stability on a kernel whose audit \
2106             explicitly concluded that no static bound applies",
2107        );
2108        assert_eq!(
2109            audited.notes, REASON,
2110            "the audit reason was overwritten — the reasoning that lived at \
2111             the registration site is gone",
2112        );
2113
2114        // Opposite direction: the fill must still do its job.
2115        let placeholder = table
2116            .lookup_alternatives(OpKind::AddElementwise, &dts, BackendId::Cpu)
2117            .first()
2118            .expect("unaudited entry registered")
2119            .precision;
2120        assert!(
2121            placeholder.bit_stable_on_same_hardware,
2122            "control FAILED: the UNAUDITED placeholder was not upgraded, so \
2123             this test would pass with the fill disabled entirely and proves \
2124             nothing about the discrimination it is meant to check",
2125        );
2126    }
2127}