fuel-dispatch 0.20.0

Dispatch infrastructure for fuel: KernelBindingTable, per-backend registration wrappers, CompiledNode + compile_node, ExecutionPlan + compile_plan + resolve_kernel, FusedKernelRegistry, PipelinedExecutor. The layer that takes a graph and runs it against registered backends. Extracted from fuel-memory 2026-05-31.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// SPDX-License-Identifier: MIT OR Apache-2.0
//! `JudgeAwareSelector` — Phase 5.2 runtime selector that re-queries
//! the [`JudgeOracle`] at dispatch time and re-ranks candidates by
//! fresh empirical measurements.
//!
//! # Why this selector exists
//!
//! Picker 1 (the optimizer ranker) consumes Judge data once during
//! `compute_static_costs`, at plan-build time. If new measurements
//! land between plan build and dispatch (the Judge profiles
//! incrementally; a long-running process accumulates entries during
//! its first realizes), the plan's ranking can be stale. The static
//! cost was Layer-1 + whatever Judge entries existed at plan time;
//! a Layer-2 entry that materializes after the plan freezes is not
//! reflected.
//!
//! `JudgeAwareSelector` fixes this by re-querying the Judge at
//! select time for each candidate's `(op, dtype, size_class, backend)`
//! cell. Candidates with a fresh measurement get re-ranked by that
//! measurement; candidates without a measurement keep their static
//! rank position. The selector returns the new top-1.
//!
//! # Selector context
//!
//! The Judge lookup key is
//! `(OpKind, DType, SizeClass, BackendId)`. Only `BackendId` is on
//! the [`Candidate`]; the other three components are per-decision-
//! point context that the trait's `select(&AlternativeSet)` signature
//! deliberately does NOT carry. Phase 5.2 resolves this the same way
//! Phase 5.1 documented as the path forward: the selector is
//! constructed PER DECISION POINT with the op/dtype/size context
//! baked in. The caller (Phase 4's executor migration, when it lands)
//! produces one of these per kernel-bearing node from the plan's
//! enumeration metadata.
//!
//! This shape is deliberately not the future "shared selector for
//! whole realize" — that would require widening the trait. It's the
//! pragmatic Phase 5.2 shape that lets the selector exist before
//! the wider trait change has consensus.
//!
//! # What "kernel_source" does here
//!
//! Phase v0.4 of the backend contract carries `kernel_source` on
//! both [`Candidate`] and [`fuel_ir::dispatch::ProfileEntry`].
//! The [`JudgeOracle`] trait's `measured_latency_ns` doesn't take
//! `kernel_source` today (the trait predates per-alternative
//! measurement). When a future Judge revision adds it, this selector
//! is the first consumer that will benefit — multiple candidates
//! with the same `(op, dtype, size, backend)` but different
//! `kernel_source` will get distinguished. For now the selector
//! re-queries by `(op, dtype, size_class, backend)` and accepts
//! that AOCL vs MKL candidates on `BackendId::Cpu` share a Judge
//! cell.

use std::sync::Arc;

use fuel_ir::DType;
use fuel_ir::dispatch::{OpKind, SizeClass};

use super::{
    AlternativeSet, Candidate, JudgeOracle, RuntimeSelector, composite_ns, default_backend_rates,
};

/// Phase 5.2 runtime selector that re-queries the Judge at dispatch
/// time and re-ranks candidates by the freshest measured latency.
///
/// See module docs for the full rationale.
///
/// # Construction
///
/// ```ignore
/// use std::sync::Arc;
/// use fuel_ir::dispatch::{OpKind, SizeClass};
/// use fuel_ir::DType;
/// use fuel_dispatch::ranker::{HashMapJudge, JudgeAwareSelector};
///
/// let judge = Arc::new(HashMapJudge::new());
/// let selector = JudgeAwareSelector::new(
///     judge,
///     OpKind::MatMul,
///     DType::F32,
///     SizeClass::from_elem_count(1024 * 1024),
/// );
/// ```
#[derive(Clone)]
pub struct JudgeAwareSelector {
    judge: Arc<dyn JudgeOracle>,
    op: OpKind,
    dtype: DType,
    size_class: SizeClass,
}

impl JudgeAwareSelector {
    /// Construct a selector bound to one `(op, dtype, size_class)`
    /// decision point. The selector re-queries `judge` at each
    /// `select` call for each candidate's backend.
    pub fn new(
        judge: Arc<dyn JudgeOracle>,
        op: OpKind,
        dtype: DType,
        size_class: SizeClass,
    ) -> Self {
        Self {
            judge,
            op,
            dtype,
            size_class,
        }
    }

    /// The op this selector queries the Judge under.
    pub fn op(&self) -> OpKind {
        self.op
    }

    /// The dtype this selector queries the Judge under.
    pub fn dtype(&self) -> DType {
        self.dtype
    }

    /// The size class this selector queries the Judge under.
    pub fn size_class(&self) -> SizeClass {
        self.size_class
    }

    /// Look up the candidate's measured latency, if any.
    ///
    /// Threads the candidate's `kernel_source` AND `kernel_revision_hash`
    /// through the Judge so sibling kernels at the same `(backend,
    /// device)` slot get their own measurement, instead of collapsing
    /// onto whichever registered last. `kernel_source` separates
    /// providers (AOCL vs MKL vs portable-cpu under `BackendId::Cpu`);
    /// `kernel_revision_hash` separates variant siblings of one provider
    /// (forty tilings of one generator), which the tag alone cannot.
    fn measured_latency(&self, c: &Candidate) -> Option<u64> {
        self.judge.measured_latency_ns(
            self.op,
            self.dtype,
            self.size_class,
            c.backend,
            c.kernel_source,
            c.kernel_revision_hash,
        )
    }
}

impl std::fmt::Debug for JudgeAwareSelector {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JudgeAwareSelector")
            .field("judge", &"<dyn JudgeOracle>")
            .field("op", &self.op)
            .field("dtype", &self.dtype)
            .field("size_class", &self.size_class)
            .finish()
    }
}

impl RuntimeSelector for JudgeAwareSelector {
    fn select<'a>(&self, set: &'a AlternativeSet) -> Option<&'a Candidate> {
        let alts = set.alternatives();
        if alts.is_empty() {
            return None;
        }

        // Rank every candidate by a single nanosecond figure:
        //
        // - Measured candidates use the Judge's measured latency.
        // - Unmeasured candidates use `composite_ns(static_cost)`,
        //   the same Layer-1 scalar Picker 1 already ranks by.
        //
        // This produces a uniform ordering across both groups: a
        // 100ms measured kernel correctly loses to a 50ns unmeasured
        // one when the Layer-1 estimate is trustworthy. The prior
        // shape ("any measured candidate trumps all unmeasured
        // candidates") was wrong — see the 2026-06-08 adversarial
        // verification of the Phase 5.2 selector. Treating the two
        // groups on equal footing is the principled fix.
        //
        // Tie-break by original index so ties resolve toward
        // Picker 1's static rank — preserving determinism and the
        // `set.winner()` invariant when every candidate scores the
        // same.
        let (best_idx, _) = alts
            .iter()
            .enumerate()
            .map(|(i, c)| {
                // Stage 2: the inbound-transfer term adds serially
                // to both groups — the bytes must land before either
                // a measured or an estimated kernel can run. Plan-
                // produced sets are device-pruned so the term is
                // uniform in practice; adding it keeps the scale
                // consistent with the plan rank.
                let score = self
                    .measured_latency(c)
                    .unwrap_or_else(|| {
                        let (cr, bw) = default_backend_rates(c.backend);
                        composite_ns(&c.static_cost, cr, bw)
                    })
                    .saturating_add(c.inbound_transfer_ns);
                (i, score)
            })
            .min_by_key(|&(idx, score)| (score, idx))
            .expect("non-empty after the early return");
        alts.get(best_idx)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fused::{CostEstimate, PrecisionGuarantee};
    use crate::kernel::{KernelCaps, OpParams};
    use crate::ranker::HashMapJudge;
    use fuel_ir::probe::BackendId;
    use fuel_ir::{DeviceLocation, Layout, Result};
    use fuel_memory::Storage;
    use std::sync::{Arc, RwLock};

    fn noop_kernel(
        _i: &[Arc<RwLock<Storage>>],
        _o: &mut [Arc<RwLock<Storage>>],
        _l: &[Layout],
        _p: &OpParams,
    ) -> Result<()> {
        Ok(())
    }

    fn make_candidate(backend: BackendId, cost: u64) -> Candidate {
        Candidate {
            kernel: noop_kernel,
            caps: KernelCaps::empty(),
            backend,
            device: DeviceLocation::Cpu,
            precision: PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            static_cost: CostEstimate {
                flops: cost,
                bytes_moved: cost,
                kernel_overhead_ns: 0,
            },
            inbound_transfer_ns: 0,
            op_params: OpParams::None,
            coupling: Vec::new(),
            kernel_source: "",
            kernel_revision_hash: 0,
        }
    }

    /// Helper: build a selector against a freshly-populated judge.
    fn make_selector(judge: HashMapJudge) -> JudgeAwareSelector {
        JudgeAwareSelector::new(Arc::new(judge), OpKind::MatMul, DType::F32, SizeClass(16))
    }

    /// No measurements → behaves like WinnerSelector.
    #[test]
    fn empty_judge_falls_back_to_winner() {
        let mut set = AlternativeSet::empty();
        set.push(make_candidate(BackendId::Cuda, 100));
        set.push(make_candidate(BackendId::Cpu, 200));

        let sel = make_selector(HashMapJudge::new());
        let pick = sel.select(&set).expect("non-empty");
        assert_eq!(
            pick.backend,
            BackendId::Cuda,
            "no measurements → static winner",
        );
    }

    /// Judge measurement on the loser → re-rank flips order.
    #[test]
    fn judge_measurement_flips_picker_order() {
        let mut set = AlternativeSet::empty();
        // Cost rank: CUDA (100) wins. CPU (200) loses.
        set.push(make_candidate(BackendId::Cuda, 100));
        set.push(make_candidate(BackendId::Cpu, 200));

        // Judge says CUDA is actually 10ms while CPU is 1ms.
        let mut judge = HashMapJudge::new();
        judge.insert(
            OpKind::MatMul,
            DType::F32,
            SizeClass(16),
            BackendId::Cuda,
            "",
            10_000_000,
            0,
        );
        judge.insert(
            OpKind::MatMul,
            DType::F32,
            SizeClass(16),
            BackendId::Cpu,
            "",
            1_000_000,
            0,
        );

        let sel = make_selector(judge);
        let pick = sel.select(&set).expect("non-empty");
        assert_eq!(
            pick.backend,
            BackendId::Cpu,
            "Judge says CPU is 10x faster → CPU wins despite static cost",
        );
    }

    /// Only one candidate measured → measured and unmeasured rank
    /// uniformly on the same nanosecond axis. A fast (low static
    /// cost) unmeasured candidate beats a slow measured one; a
    /// fast measurement beats a high static cost. The selector no
    /// longer auto-promotes every measured candidate above every
    /// unmeasured one.
    #[test]
    fn single_measurement_competes_uniformly_against_unmeasured() {
        // Case A: the measurement is slow → unmeasured static
        // winner should still win.
        let mut set = AlternativeSet::empty();
        set.push(make_candidate(BackendId::Cuda, 100));
        set.push(make_candidate(BackendId::Cpu, 200));
        set.push(make_candidate(BackendId::Vulkan, 300));

        // CPU measured at 500_000 ns — slow compared to the
        // unmeasured CUDA (composite_ns = 100 ns).
        let mut judge = HashMapJudge::new();
        judge.insert(
            OpKind::MatMul,
            DType::F32,
            SizeClass(16),
            BackendId::Cpu,
            "",
            500_000,
            0,
        );
        let sel = make_selector(judge);
        let pick = sel.select(&set).expect("non-empty");
        assert_eq!(
            pick.backend,
            BackendId::Cuda,
            "slow measurement does NOT trump a faster unmeasured static cost",
        );

        // Case B: the measurement is fast → the measured candidate
        // wins.
        let mut set = AlternativeSet::empty();
        set.push(make_candidate(BackendId::Cuda, 1_000_000));
        set.push(make_candidate(BackendId::Cpu, 2_000_000));
        let mut judge = HashMapJudge::new();
        judge.insert(
            OpKind::MatMul,
            DType::F32,
            SizeClass(16),
            BackendId::Cpu,
            "",
            500,
            0,
        );
        let sel = make_selector(judge);
        let pick = sel.select(&set).expect("non-empty");
        assert_eq!(
            pick.backend,
            BackendId::Cpu,
            "fast measurement beats slower unmeasured static cost",
        );
    }

    /// All candidates measured → lowest latency wins.
    #[test]
    fn all_measured_picks_lowest_latency() {
        let mut set = AlternativeSet::empty();
        set.push(make_candidate(BackendId::Cuda, 100));
        set.push(make_candidate(BackendId::Cpu, 200));
        set.push(make_candidate(BackendId::Vulkan, 300));

        let mut judge = HashMapJudge::new();
        judge.insert(
            OpKind::MatMul,
            DType::F32,
            SizeClass(16),
            BackendId::Cuda,
            "",
            10_000,
            0,
        );
        judge.insert(
            OpKind::MatMul,
            DType::F32,
            SizeClass(16),
            BackendId::Cpu,
            "",
            5_000,
            0,
        );
        judge.insert(
            OpKind::MatMul,
            DType::F32,
            SizeClass(16),
            BackendId::Vulkan,
            "",
            1_000,
            0,
        );

        let sel = make_selector(judge);
        let pick = sel.select(&set).expect("non-empty");
        assert_eq!(
            pick.backend,
            BackendId::Vulkan,
            "lowest latency wins regardless of static cost order",
        );
    }

    /// Different op key in the Judge → no match → falls back to
    /// winner. Proves the selector queries by its constructed
    /// (op, dtype, size_class), not by candidate.
    #[test]
    fn judge_key_must_match_selector_context() {
        let mut set = AlternativeSet::empty();
        set.push(make_candidate(BackendId::Cuda, 100));
        set.push(make_candidate(BackendId::Cpu, 200));

        // Judge has entries — but under a DIFFERENT op.
        let mut judge = HashMapJudge::new();
        judge.insert(
            OpKind::AddElementwise, // selector is configured for MatMul
            DType::F32,
            SizeClass(16),
            BackendId::Cpu,
            "",
            1_000,
            0,
        );

        // Selector configured for MatMul.
        let sel = make_selector(judge);
        let pick = sel.select(&set).expect("non-empty");
        assert_eq!(
            pick.backend,
            BackendId::Cuda,
            "Judge has no entry at our (op,dtype,size) → static winner",
        );
    }

    /// Tie in latency → earlier-original-index wins (stable).
    #[test]
    fn latency_tie_breaks_toward_static_winner() {
        let mut set = AlternativeSet::empty();
        // CUDA at original idx 0; CPU at idx 1; both measured at
        // the same latency.
        set.push(make_candidate(BackendId::Cuda, 100));
        set.push(make_candidate(BackendId::Cpu, 200));

        let mut judge = HashMapJudge::new();
        judge.insert(
            OpKind::MatMul,
            DType::F32,
            SizeClass(16),
            BackendId::Cuda,
            "",
            5_000,
            0,
        );
        judge.insert(
            OpKind::MatMul,
            DType::F32,
            SizeClass(16),
            BackendId::Cpu,
            "",
            5_000,
            0,
        );

        let sel = make_selector(judge);
        let pick = sel.select(&set).expect("non-empty");
        assert_eq!(
            pick.backend,
            BackendId::Cuda,
            "tie on latency → earlier original index wins (static-rank tiebreak)",
        );
    }

    /// Empty set → None.
    #[test]
    fn empty_set_returns_none() {
        let set = AlternativeSet::empty();
        let sel = make_selector(HashMapJudge::new());
        assert!(sel.select(&set).is_none());
    }

    /// Debug impl doesn't panic on the Arc<dyn> field.
    #[test]
    fn debug_does_not_panic() {
        let sel = make_selector(HashMapJudge::new());
        let s = format!("{sel:?}");
        assert!(s.contains("JudgeAwareSelector"));
        assert!(s.contains("MatMul"));
    }

    /// Accessor methods round-trip the construction args.
    #[test]
    fn accessors_round_trip_construction() {
        let sel = JudgeAwareSelector::new(
            Arc::new(HashMapJudge::new()),
            OpKind::AddElementwise,
            DType::BF16,
            SizeClass(7),
        );
        assert_eq!(sel.op(), OpKind::AddElementwise);
        assert_eq!(sel.dtype(), DType::BF16);
        assert_eq!(sel.size_class(), SizeClass(7));
    }

    /// Two `BackendId::Cpu` candidates with distinct `kernel_source`
    /// tags receive DISTINCT measurements from the Judge. The trait
    /// fix that threads `kernel_source` into `measured_latency_ns`
    /// only matters if the selector propagates it correctly — this
    /// test pins that behavior.
    ///
    /// Without the fix, the second `judge.insert(..., "aocl", _)` and
    /// `judge.insert(..., "mkl", _)` would collide on the same key and
    /// last-write-wins would collapse the ranking. With the fix, each
    /// sibling kernel is judged on its own merits and the selector
    /// picks the cheaper of the two.
    #[test]
    fn distinct_kernel_sources_get_distinct_measurements() {
        let mut set = AlternativeSet::empty();
        // Two Cpu candidates: aocl and mkl. Identical backend, identical
        // device, identical static cost — only `kernel_source` differs.
        let mut c_aocl = make_candidate(BackendId::Cpu, 500);
        c_aocl.kernel_source = "aocl";
        let mut c_mkl = make_candidate(BackendId::Cpu, 500);
        c_mkl.kernel_source = "mkl";
        set.push(c_aocl);
        set.push(c_mkl);

        // Judge: aocl is slow (10ms), mkl is fast (1ms).
        let mut judge = HashMapJudge::new();
        judge.insert(
            OpKind::MatMul,
            DType::F32,
            SizeClass(16),
            BackendId::Cpu,
            "aocl",
            10_000_000,
            0,
        );
        judge.insert(
            OpKind::MatMul,
            DType::F32,
            SizeClass(16),
            BackendId::Cpu,
            "mkl",
            1_000_000,
            0,
        );

        let sel = make_selector(judge);
        let pick = sel.select(&set).expect("non-empty");
        assert_eq!(pick.backend, BackendId::Cpu);
        assert_eq!(
            pick.kernel_source, "mkl",
            "selector threads kernel_source into the Judge so siblings rank independently",
        );

        // Flip the measurements and confirm the OTHER sibling wins.
        let mut set = AlternativeSet::empty();
        let mut c_aocl = make_candidate(BackendId::Cpu, 500);
        c_aocl.kernel_source = "aocl";
        let mut c_mkl = make_candidate(BackendId::Cpu, 500);
        c_mkl.kernel_source = "mkl";
        set.push(c_aocl);
        set.push(c_mkl);

        let mut judge = HashMapJudge::new();
        judge.insert(
            OpKind::MatMul,
            DType::F32,
            SizeClass(16),
            BackendId::Cpu,
            "aocl",
            500_000,
            0,
        );
        judge.insert(
            OpKind::MatMul,
            DType::F32,
            SizeClass(16),
            BackendId::Cpu,
            "mkl",
            5_000_000,
            0,
        );
        let sel = make_selector(judge);
        let pick = sel.select(&set).expect("non-empty");
        assert_eq!(
            pick.kernel_source, "aocl",
            "flipped measurements → other sibling wins, proving siblings are not collapsed",
        );
    }
}