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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Cost composition + ranking for the picker. Phase 1.4 of the
//! picker-work arc.
//!
//! Layer-1 static cost composition only. The scoring function
//! converts a `CostEstimate { flops, bytes_moved, kernel_overhead_ns }`
//! into a composite nanosecond figure the ranker sorts by. Layer-2
//! (Judge empirical refinement) lands in Phase 3 and either
//! refines `static_cost` before this composition happens or
//! provides its own rank.
//!
//! # The composition model
//!
//! For each candidate, the picker estimates wall-clock by combining
//! three terms:
//!
//! - **Compute time** = `flops / cap.peak_flops_per_s` (seconds)
//! - **Memory time** = `bytes_moved / cap.peak_bandwidth_bytes_per_s`
//! - **Overhead** = `kernel_overhead_ns / 1e9` (seconds)
//!
//! Returned as `u64` nanoseconds (saturating). The arithmetic
//! intentionally treats compute and memory as **parallel** — a
//! kernel that's memory-bound spends `max(compute, memory)` rather
//! than `compute + memory`. This is the classic roofline shape and
//! matches how every modern accelerator's pipelines actually behave.
//!
//! For Phase 1.4 the picker doesn't yet read live BackendCapabilities
//! (the populate-costs step is the caller's responsibility once it
//! has them). The standalone [`composite_ns`] takes a `CostEstimate`
//! already calibrated against capabilities and reduces it to a
//! sortable scalar.

use fuel_ir::backend::BackendCapabilities;
use fuel_ir::dispatch::{OpKind, SizeClass};
use fuel_ir::probe::BackendId;
use fuel_ir::{DType, DeviceLocation, Shape};

use super::alternative_set::AlternativeSet;
use super::judge::JudgeOracle;
use crate::fused::CostEstimate;
use crate::kernel::KernelBindingTable;
// `OpParams` appears only in the test module's `KernelRef`-shaped stub
// signatures, so scope the import to `cfg(test)` rather than warn in the lib.
#[cfg(test)]
use crate::kernel::OpParams;

/// Plan-time transfer-cost oracle — planner Stage 2
/// (`docs/session-prompts/load-time-incremental-planner.md`).
///
/// Prices moving `bytes` from `src` to `dst` in wall-clock
/// nanoseconds. `fuel-dispatch` must not depend on `fuel-core`
/// (dependency direction), so the production numbers — Stage 1's
/// `SystemTopology::estimate_transfer_ns`, backed by the lazily
/// probed per-generation `TransferCalibration` — thread in through
/// this trait on `PlanOptions`, mirroring how [`JudgeOracle`] was
/// threaded in Phase 3. Tests use synthetic implementations; unit
/// tests must never depend on live calibration.
///
/// Contract:
///
/// - `src == dst` must cost zero (no bytes move).
/// - Never panics; conservative estimates for unknown paths.
/// - Monotonic in `bytes` for a fixed path.
pub trait TransferEstimator: Send + Sync {
    /// Estimated wall-clock nanoseconds to move `bytes` from `src`
    /// to `dst`. Zero when `src == dst`.
    fn estimate_transfer_ns(&self, src: DeviceLocation, dst: DeviceLocation, bytes: u64) -> u64;
}

/// Populate [`super::Candidate::inbound_transfer_ns`] on every
/// candidate in `set`: the sum over `inputs` of
/// `estimator.estimate_transfer_ns(src, candidate.device, bytes)`.
///
/// `inputs` carries one `(resident device, byte size)` pair per
/// decision-point input whose residency is KNOWN at plan time
/// (committed producer placements + caller-supplied residency for
/// graph inputs). Inputs with unknown residency are simply absent —
/// no term fires for them, which is the conservative direction (an
/// unpriceable edge never justifies *or* penalizes a move).
///
/// Inputs already resident on the candidate's device contribute
/// zero by the [`TransferEstimator`] contract, so co-located
/// candidates rank purely on kernel cost. Saturating arithmetic
/// throughout.
pub fn apply_inbound_transfer_costs(
    set: &mut AlternativeSet,
    inputs: &[(DeviceLocation, u64)],
    estimator: &dyn TransferEstimator,
) {
    for i in 0..set.len() {
        let dst = set.alternatives()[i].device;
        let mut total: u64 = 0;
        for &(src, bytes) in inputs {
            total = total.saturating_add(estimator.estimate_transfer_ns(src, dst, bytes));
        }
        set.set_inbound_transfer_ns(i, total);
    }
}

/// CPU baseline compute throughput prior, in FLOPs per nanosecond.
/// Reproduces `composite_ns`'s historical neutral prior (1 FLOP ≈ 1 ns).
pub const CPU_COMPUTE_FLOPS_PER_NS: f64 = 1.0;
/// CPU baseline memory bandwidth prior, in bytes per nanosecond.
/// Reproduces the historical prior (4 bytes ≈ 1 ns, i.e. `bytes / 4`).
pub const CPU_MEM_BYTES_PER_NS: f64 = 4.0;
/// GPU-class compute throughput prior, in FLOPs per nanosecond.
/// Deliberately conservative — ~30× the CPU baseline models the real
/// parallel-throughput advantage a GPU has on large ops well enough for
/// the placement DP to prefer it there, while a small op's transfer
/// cost still keeps it local. Directionally correct, not calibrated;
/// the Judge's Layer-2 measurements refine the effective figure.
pub const GPU_COMPUTE_FLOPS_PER_NS: f64 = 30.0;
/// GPU-class memory bandwidth prior, in bytes per nanosecond (~10× the
/// CPU baseline).
pub const GPU_MEM_BYTES_PER_NS: f64 = 40.0;

/// The per-backend Layer-1 throughput prior for a consumer that does
/// NOT have the registered [`BackendCapabilities`] in hand — the
/// candidate rank (`CostVector::from_candidate`) and the dispatch-time
/// selectors, which see only `Candidate::backend`.
///
/// The AUTHORITATIVE source is the registered caps'
/// `compute_throughput_flops_per_ns` / `mem_bandwidth_bytes_per_ns`,
/// which the placement DP (`compile_plan`) reads directly. This fallback
/// is kept consistent by deriving from the SAME constants that
/// `default_cpu_caps` / `derive_backend_caps` populate those caps with,
/// so a candidate ranks with the same prior the DP would price it at
/// until the Judge refines a specific cell.
///
/// Returns `(compute_flops_per_ns, mem_bytes_per_ns)`.
pub fn default_backend_rates(backend: BackendId) -> (f64, f64) {
    match backend {
        BackendId::Cpu => (CPU_COMPUTE_FLOPS_PER_NS, CPU_MEM_BYTES_PER_NS),
        // CUDA / Vulkan / Metal — GPU-class parallel throughput.
        _ => (GPU_COMPUTE_FLOPS_PER_NS, GPU_MEM_BYTES_PER_NS),
    }
}

/// Convert a `CostEstimate` into a sortable composite nanosecond
/// figure using a backend's throughput. Lower is better. Treats
/// compute + memory as parallel (roofline-style) and adds launch
/// overhead serially.
///
/// `compute_rate_flops_per_ns` / `mem_bandwidth_bytes_per_ns` are the
/// candidate backend's throughput priors (from its registered
/// [`BackendCapabilities`], or [`default_backend_rates`] when caps
/// aren't in hand). A faster backend divides the same FLOP/byte count by
/// a larger rate → fewer ns → the cross-backend placement DP can prefer
/// it for large parallel work. Passing the CPU baseline (`1.0`, `4.0`)
/// reproduces the pre-throughput neutral prior exactly.
///
/// Never-panic: a non-finite or non-positive rate falls back to the CPU
/// baseline (so a mis-registered backend degrades to the old prior
/// rather than dividing by zero). `f64 → u64` casts saturate (Rust
/// float-to-int semantics: `+inf`/overflow → `u64::MAX`, `NaN` → 0), so
/// extreme inputs clamp to `u64::MAX` rather than overflowing.
///
/// `kernel_overhead_ns` is added serially and NOT scaled — the Judge's
/// Layer-2 refinement packs a measured latency there (with `flops` =
/// `bytes_moved` = 0), so this returns that measurement unchanged
/// regardless of the rates.
pub fn composite_ns(
    cost: &CostEstimate,
    compute_rate_flops_per_ns: f64,
    mem_bandwidth_bytes_per_ns: f64,
) -> u64 {
    let compute_rate = if compute_rate_flops_per_ns.is_finite() && compute_rate_flops_per_ns > 0.0 {
        compute_rate_flops_per_ns
    } else {
        CPU_COMPUTE_FLOPS_PER_NS
    };
    let bandwidth = if mem_bandwidth_bytes_per_ns.is_finite() && mem_bandwidth_bytes_per_ns > 0.0 {
        mem_bandwidth_bytes_per_ns
    } else {
        CPU_MEM_BYTES_PER_NS
    };
    // f64 → u64 casts saturate in Rust (no panic / no UB).
    let compute_ns = (cost.flops as f64 / compute_rate) as u64;
    let memory_ns = (cost.bytes_moved as f64 / bandwidth) as u64;
    let parallel_ns = compute_ns.max(memory_ns);
    parallel_ns.saturating_add(cost.kernel_overhead_ns as u64)
}

/// Closure type for "give me the BackendCapabilities for this
/// backend." The caller (Phase 1.5's compile_plan) reads from
/// `SystemTopology::capabilities(...)` or the CapabilityRegistry;
/// this module accepts a callback so it stays ignorant of where
/// the capabilities come from.
pub type CapabilitiesLookup<'a> = dyn Fn(BackendId) -> Option<&'a BackendCapabilities> + 'a;

/// Compute and populate `static_cost` on every candidate in `set`.
///
/// For each candidate, looks up the binding-table entry that
/// produced it (matched by `kernel` function pointer identity) and
/// invokes the entry's `CostFn(shapes, dtypes, op_params, caps)`
/// to compute the Layer-1 cost. Candidates whose backend has no
/// capabilities entry, or whose kernel pointer is no longer
/// findable (defensive — the table shouldn't change between
/// enumeration and costing within one plan), retain the default
/// zero-cost.
///
/// When `judge` is `Some`, runs the Layer-2 refinement after
/// Layer-1: for each candidate, queries the oracle for a measured
/// latency at this (op, dtype, size_class, backend) cell. If the
/// measurement exists, **replaces** the Layer-1 estimate with a
/// Layer-2-equivalent: zero FLOPs + zero bytes + `kernel_overhead_ns
/// = saturating_cast(latency_ns)`. The composite scoring then
/// returns that latency directly. Cells without measurements keep
/// the Layer-1 estimate (silent fallback — no measurement is NOT
/// the same as "this kernel is fast").
///
/// The size_class is derived via [`SizeClass::for_op`] from the input
/// operand `shapes` — the single shared derivation the Judge profiler
/// (producer) also uses, so a profiled cell is found here. A matmul
/// keys on `(m,n,k)` read from the operand shapes (aspect-aware, so
/// non-square matmuls agree with the producer); every other op keys on
/// `shapes[0]`'s element count. Empty `shapes` (nullary op) →
/// `SizeClass(0)`.
///
/// The caller supplies `shapes` (input operand shapes for the
/// decision point), the capabilities lookup closure, and optional
/// judge oracle.
pub fn compute_static_costs(
    set: &mut AlternativeSet,
    op_kind: OpKind,
    dtypes: &[DType],
    shapes: &[Shape],
    bindings: &KernelBindingTable,
    capabilities_for: &CapabilitiesLookup<'_>,
    judge: Option<&dyn JudgeOracle>,
) {
    // Layer-1: static cost via the binding-table's CostFn.
    for i in 0..set.len() {
        let (kernel_ptr, backend, op_params) = {
            let c = &set.alternatives()[i];
            (
                c.kernel as *const () as usize,
                c.backend,
                c.op_params.clone(),
            )
        };
        let entries = bindings.lookup_alternatives(op_kind, dtypes, backend);
        let Some(entry) = entries
            .iter()
            .find(|e| (e.kernel as *const () as usize) == kernel_ptr)
        else {
            continue;
        };
        let Some(caps) = capabilities_for(backend) else {
            continue;
        };
        let cost = match entry.cost_expr {
            Some(expr) => crate::fkc::cost_estimate(expr, op_kind, shapes, dtypes, &op_params)
                .unwrap_or_else(|_| (entry.cost)(shapes, dtypes, &op_params, caps)),
            None => (entry.cost)(shapes, dtypes, &op_params, caps),
        };
        set.set_static_cost(i, cost);
    }

    // Layer-2 refinement: if the Judge has data for this cell,
    // replace the Layer-1 estimate with a latency-equivalent shape
    // so composite_ns returns the measurement.
    let Some(judge) = judge else {
        return;
    };
    // Pick the principal dtype — by convention the first input's
    // dtype, which matches the Judge profiler's keying. For
    // mixed-dtype ops (Cast etc.) this picks the source dtype;
    // future refinement could thread the destination as a separate
    // axis if a measurable wins/losses pattern emerges.
    let principal_dtype = match dtypes.first() {
        Some(&dt) => dt,
        None => return,
    };
    // Derive the Judge lookup key through the shared `for_op` helper —
    // the SAME derivation the producer (`fuel-core` Judge) uses. For a
    // matmul this reads `(m,n,k)` from the operand shapes so the key
    // agrees with the profiled cell even when the matmul is non-square
    // (the pre-v4 bug: this consumer keyed on `shapes[0].elem_count() =
    // m·k` while the producer keyed on the output `m·n`, so non-square
    // lookups missed). Every other op keys on `shapes[0]`'s element
    // count exactly as before.
    let size_class = SizeClass::for_op(op_kind, shapes);
    for i in 0..set.len() {
        let backend = set.alternatives()[i].backend;
        let kernel_source = set.alternatives()[i].kernel_source;
        let kernel_revision_hash = set.alternatives()[i].kernel_revision_hash;
        let Some(latency_ns) = judge.measured_latency_ns(
            op_kind,
            principal_dtype,
            size_class,
            backend,
            kernel_source,
            kernel_revision_hash,
        ) else {
            continue;
        };
        // Convert latency to a CostEstimate that composite_ns will
        // return as-is. Saturate u64 → u32 for kernel_overhead_ns;
        // anything above u32::MAX ns (~4.3 seconds) is in
        // practice a degenerate case we still want correctly
        // ordered.
        let overhead = latency_ns.min(u32::MAX as u64) as u32;
        set.set_static_cost(
            i,
            CostEstimate {
                flops: 0,
                bytes_moved: 0,
                kernel_overhead_ns: overhead,
            },
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fused::PrecisionGuarantee;
    use crate::kernel::{KernelBindingTable, KernelCaps, unknown_cost};
    use crate::ranker::alternative_set::AlternativeSet;
    use crate::ranker::candidate::Candidate;
    use fuel_ir::backend::{BackendCapabilities, SubstrateClass, TransferPath};
    use fuel_ir::dispatch::OpKind;
    use fuel_ir::probe::BackendId;
    use fuel_ir::{DType, DeviceLocation, Layout, Result, Shape};
    use fuel_memory::Storage;
    use std::collections::{HashMap, HashSet};
    use std::sync::{Arc, RwLock};

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

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

    fn caps_for_test(backend_id: BackendId) -> BackendCapabilities {
        BackendCapabilities {
            backend_id,
            device_location: DeviceLocation::Cpu,
            op_dtype_support: HashSet::new(),
            required_alignment: 64,
            access_granularity_bits: 8,
            transfer_paths: vec![(DeviceLocation::Cpu, TransferPath::SameDevice)],
            storage_substrate: SubstrateClass::HostBytes,
            compute_throughput_flops_per_ns: default_backend_rates(backend_id).0,
            mem_bandwidth_bytes_per_ns: default_backend_rates(backend_id).1,
        }
    }

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

    /// Synthetic estimator: zero same-device, otherwise
    /// `latency + bytes * ns_per_byte`. Deterministic — no live
    /// calibration anywhere near unit tests.
    struct FlatEstimator {
        ns_per_byte: u64,
        latency_ns: u64,
    }

    impl TransferEstimator for FlatEstimator {
        fn estimate_transfer_ns(
            &self,
            src: DeviceLocation,
            dst: DeviceLocation,
            bytes: u64,
        ) -> u64 {
            if src == dst {
                return 0;
            }
            self.latency_ns
                .saturating_add(bytes.saturating_mul(self.ns_per_byte))
        }
    }

    #[test]
    fn composite_ns_zero_cost_is_zero() {
        assert_eq!(
            composite_ns(
                &CostEstimate::default(),
                CPU_COMPUTE_FLOPS_PER_NS,
                CPU_MEM_BYTES_PER_NS
            ),
            0
        );
    }

    #[test]
    fn composite_ns_flops_dominant() {
        // 1000 FLOPs, 0 bytes, 0 overhead → 1000 ns.
        let c = CostEstimate {
            flops: 1000,
            bytes_moved: 0,
            kernel_overhead_ns: 0,
        };
        assert_eq!(
            composite_ns(&c, CPU_COMPUTE_FLOPS_PER_NS, CPU_MEM_BYTES_PER_NS),
            1000
        );
    }

    #[test]
    fn composite_ns_memory_dominant() {
        // 0 FLOPs, 4000 bytes, 0 overhead → 1000 ns (4 bytes/ns).
        let c = CostEstimate {
            flops: 0,
            bytes_moved: 4000,
            kernel_overhead_ns: 0,
        };
        assert_eq!(
            composite_ns(&c, CPU_COMPUTE_FLOPS_PER_NS, CPU_MEM_BYTES_PER_NS),
            1000
        );
    }

    #[test]
    fn composite_ns_takes_max_of_compute_and_memory() {
        // Compute = 500 ns, memory = 1000 ns → max is 1000 (parallel).
        let c = CostEstimate {
            flops: 500,
            bytes_moved: 4000,
            kernel_overhead_ns: 0,
        };
        assert_eq!(
            composite_ns(&c, CPU_COMPUTE_FLOPS_PER_NS, CPU_MEM_BYTES_PER_NS),
            1000
        );
    }

    #[test]
    fn composite_ns_overhead_serial_after_parallel() {
        // Parallel work = max(500, 800) = 800. Overhead = 200.
        // Total = 1000.
        let c = CostEstimate {
            flops: 500,
            bytes_moved: 3200,
            kernel_overhead_ns: 200,
        };
        assert_eq!(
            composite_ns(&c, CPU_COMPUTE_FLOPS_PER_NS, CPU_MEM_BYTES_PER_NS),
            1000
        );
    }

    #[test]
    fn composite_ns_saturates_at_u64_max() {
        let c = CostEstimate {
            flops: u64::MAX,
            bytes_moved: u64::MAX,
            kernel_overhead_ns: u32::MAX,
        };
        // max() of two saturated values is u64::MAX; saturating_add
        // pins to u64::MAX. (flops / 1.0 saturates the f64→u64 cast;
        // bytes / 4.0 does not, but the max + overhead still pin.)
        assert_eq!(
            composite_ns(&c, CPU_COMPUTE_FLOPS_PER_NS, CPU_MEM_BYTES_PER_NS),
            u64::MAX
        );
    }

    #[test]
    fn rank_by_composite_cost_orders_ascending() {
        let mut set = AlternativeSet::from_candidates(vec![
            candidate_with_cost(
                noop_a,
                CostEstimate {
                    flops: 300,
                    bytes_moved: 0,
                    kernel_overhead_ns: 0,
                },
            ),
            candidate_with_cost(
                noop_b,
                CostEstimate {
                    flops: 100,
                    bytes_moved: 0,
                    kernel_overhead_ns: 0,
                },
            ),
            candidate_with_cost(
                noop_a,
                CostEstimate {
                    flops: 200,
                    bytes_moved: 0,
                    kernel_overhead_ns: 0,
                },
            ),
        ]);
        set.rank_by_composite_cost();
        let costs: Vec<u64> = set
            .alternatives()
            .iter()
            .map(|c| c.static_cost.flops)
            .collect();
        assert_eq!(costs, vec![100, 200, 300]);
    }

    /// PR-B2: the per-ending-device Pareto frontier replaces the fixed
    /// top-N. Five same-(device, backend) candidates that differ ONLY
    /// in time are all dominated by the cheapest (equal on every other
    /// axis, strictly worse on time), so retention keeps exactly the
    /// fastest — the arm-0 winner — and drops the rest.
    #[test]
    fn rank_then_retain_keeps_undominated_only() {
        let mut set = AlternativeSet::from_candidates(
            (0..5)
                .map(|i| {
                    candidate_with_cost(
                        noop_a,
                        CostEstimate {
                            flops: (5 - i) * 100,
                            bytes_moved: 0,
                            kernel_overhead_ns: 0,
                        },
                    )
                })
                .collect(),
        );
        set.rank_by_composite_cost();
        set.retain_per_device_frontier(crate::ranker::KEEP_PER_DEVICE);
        assert_eq!(
            set.len(),
            1,
            "all but the time-best are dominated on one device"
        );
        let costs: Vec<u64> = set
            .alternatives()
            .iter()
            .map(|c| c.static_cost.flops)
            .collect();
        assert_eq!(costs, vec![100], "the fastest (arm-0 winner) survives");
    }

    fn make_cost_fn(flops: u64, bytes: u64, overhead: u32) -> crate::kernel::CostFn {
        // Function pointers can't capture state, so we use distinct
        // functions per test scenario. For the populate test below,
        // a simple two-tier setup is enough.
        // Hack: define a closure-like family by using nested fn defs.
        let _ = (flops, bytes, overhead);
        |_, _, _, _| CostEstimate {
            flops: 1000,
            bytes_moved: 0,
            kernel_overhead_ns: 0,
        }

    }

    #[test]
    fn compute_static_costs_populates_via_binding_lookup() {
        fn cost_a(_: &[Shape], _: &[DType], _: &OpParams, _: &BackendCapabilities) -> CostEstimate {
            CostEstimate {
                flops: 500,
                bytes_moved: 0,
                kernel_overhead_ns: 0,
            }
        }
        let mut bindings = KernelBindingTable::new();
        let dtypes = [DType::F32, DType::F32, DType::F32];
        bindings.register_full(
            OpKind::AddElementwise,
            &dtypes,
            BackendId::Cpu,
            noop_a,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cost_a,
        );

        let mut set = AlternativeSet::from_candidates(vec![candidate_with_cost(
            noop_a,
            CostEstimate::default(),
        )]);

        let cpu_caps = caps_for_test(BackendId::Cpu);
        let lookup: HashMap<BackendId, BackendCapabilities> =
            [(BackendId::Cpu, cpu_caps)].into_iter().collect();
        let lookup_fn = |b: BackendId| lookup.get(&b);

        compute_static_costs(
            &mut set,
            OpKind::AddElementwise,
            &dtypes,
            &[Shape::from(vec![4])],
            &bindings,
            &lookup_fn,
            None,
        );

        assert_eq!(set.alternatives()[0].static_cost.flops, 500);
        let _ = make_cost_fn; // silence unused-warning
    }

    /// Task 2.3: when a binding carries a declared `cost_expr` AST, the
    /// ranking site prices the cell via [`crate::fkc::cost_estimate`]
    /// instead of the registered `CostFn`. `entry.cost` is deliberately
    /// `unknown_cost` (all-zero) here — if the assertion below passes,
    /// the AST (not the fn pointer) priced the cell.
    #[test]
    fn compute_static_costs_prefers_declared_cost_expr() {
        let expr = crate::fkc::cost_compile::intern_cost_expr(
            &crate::fkc::cost_expr::compile_field(Some("2 * n")).unwrap(),
        )
        .expect("expr interns");

        let mut bindings = KernelBindingTable::new();
        let dtypes = [DType::F32, DType::F32, DType::F32];
        bindings.register_full_with_source_generic(
            OpKind::AddElementwise,
            &dtypes,
            BackendId::Cpu,
            noop_a,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            unknown_cost,
            "",
            false,
            0,
            Some(expr),
        );

        let mut set = AlternativeSet::from_candidates(vec![candidate_with_cost(
            noop_a,
            CostEstimate::default(),
        )]);

        let cpu_caps = caps_for_test(BackendId::Cpu);
        let lookup: HashMap<BackendId, BackendCapabilities> =
            [(BackendId::Cpu, cpu_caps)].into_iter().collect();

        compute_static_costs(
            &mut set,
            OpKind::AddElementwise,
            &dtypes,
            &[Shape::from(vec![3])],
            &bindings,
            &|b| lookup.get(&b),
            None,
        );

        assert_eq!(
            set.alternatives()[0].static_cost.flops,
            6,
            "2 * n with n = elem_count([3]) = 3",
        );
    }

    /// Sentinel `CostFn` returning a fixed non-zero, non-`unknown_cost`
    /// flops count — used to discriminate "the fn-pointer fallback fired"
    /// from both "the AST priced it" (would differ from 42 for any
    /// realistic shape) and "the code panicked" (a passing assertion here
    /// proves execution reached and returned from this fn).
    fn sentinel_cost_42(
        _s: &[Shape],
        _d: &[DType],
        _p: &OpParams,
        _c: &BackendCapabilities,
    ) -> CostEstimate {
        CostEstimate {
            flops: 42,
            ..Default::default()
        }
    }

    /// Task 2.3 fallback safety property: when `cost_expr` is `Some` but
    /// evaluating it errors (an undefined symbol — see below), the eval
    /// site's `.unwrap_or_else(|_| (entry.cost)(...))` must degrade to the
    /// registered `CostFn` rather than panicking.
    ///
    /// `"undefined_symbol_xyz"` is a bare identifier, so `compile_field`
    /// parses it to `CompiledCostExpr::Expr(CostNode::Sym("undefined_symbol_xyz"))`
    /// without error — per `cost_expr.rs`'s module doc, "any identifier
    /// resolves at eval time from the supplied bindings (a missing symbol
    /// is an eval error, not a parse error)". At eval time,
    /// `bind_cost_symbols` for a non-`Matmul` op (this test uses
    /// `AddElementwise`) only ever inserts `"dtype_bytes"` and `"n"`
    /// (`cost_expr.rs:498-526`) — `"undefined_symbol_xyz"` is in neither,
    /// so `eval_node`'s `CostNode::Sym` arm (`cost_expr.rs:440-443`) takes
    /// the `bindings.get(name)` `None` branch and returns
    /// `Err(CostEvalError("undefined cost symbol \`undefined_symbol_xyz\`"))`.
    /// That error is exactly what `cost_estimate` (`cost_expr.rs:534-547`)
    /// propagates up through `crate::fkc::cost_estimate(...)` at the eval
    /// site, so `.unwrap_or_else` is guaranteed to run its closure here.
    ///
    /// Had the eval site instead used `.unwrap()` (the pre-Task-2.3-fix
    /// shape this test pins against regressing to), this exact test would
    /// panic — `cost_estimate(...)` returns `Err(..)` here, not `Ok(..)`,
    /// so `.unwrap()` unwinds instead of falling through to
    /// `sentinel_cost_42`, and the `flops == 42` assertion would never be
    /// reached.
    #[test]
    fn compute_static_costs_degrades_to_fn_pointer_on_cost_expr_eval_error() {
        let expr = crate::fkc::cost_compile::intern_cost_expr(
            &crate::fkc::cost_expr::compile_field(Some("undefined_symbol_xyz")).unwrap(),
        )
        .expect("expr interns (a bare Sym node is never CompiledCostExpr::Unknown)");

        let mut bindings = KernelBindingTable::new();
        let dtypes = [DType::F32, DType::F32, DType::F32];
        bindings.register_full_with_source_generic(
            OpKind::AddElementwise,
            &dtypes,
            BackendId::Cpu,
            noop_a,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            sentinel_cost_42,
            "",
            false,
            0,
            Some(expr),
        );

        let mut set = AlternativeSet::from_candidates(vec![candidate_with_cost(
            noop_a,
            CostEstimate::default(),
        )]);

        let cpu_caps = caps_for_test(BackendId::Cpu);
        let lookup: HashMap<BackendId, BackendCapabilities> =
            [(BackendId::Cpu, cpu_caps)].into_iter().collect();

        compute_static_costs(
            &mut set,
            OpKind::AddElementwise,
            &dtypes,
            &[Shape::from(vec![3])],
            &bindings,
            &|b| lookup.get(&b),
            None,
        );

        assert_eq!(
            set.alternatives()[0].static_cost.flops,
            42,
            "undefined symbol => cost_estimate errors => .unwrap_or_else falls back \
             to sentinel_cost_42, never panics",
        );
    }

    #[test]
    fn compute_static_costs_leaves_default_when_no_capabilities() {
        let mut bindings = KernelBindingTable::new();
        let dtypes = [DType::F32, DType::F32, DType::F32];
        bindings.register_full(
            OpKind::AddElementwise,
            &dtypes,
            BackendId::Cpu,
            noop_a,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            unknown_cost,
        );
        let mut set = AlternativeSet::from_candidates(vec![candidate_with_cost(
            noop_a,
            CostEstimate::default(),
        )]);
        // Empty lookup → no caps → cost not computed.
        let empty_lookup = |_: BackendId| -> Option<&BackendCapabilities> { None };
        compute_static_costs(
            &mut set,
            OpKind::AddElementwise,
            &dtypes,
            &[Shape::from(vec![4])],
            &bindings,
            &empty_lookup,
            None,
        );
        assert_eq!(set.alternatives()[0].static_cost, CostEstimate::default());
    }

    #[test]
    fn compute_static_costs_skips_candidates_without_matching_binding() {
        // Candidate's kernel pointer doesn't match any binding-table
        // entry (defensive case). Cost stays default.
        let mut bindings = KernelBindingTable::new();
        let dtypes = [DType::F32, DType::F32, DType::F32];
        bindings.register_full(
            OpKind::AddElementwise,
            &dtypes,
            BackendId::Cpu,
            noop_a,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            unknown_cost,
        );
        let mut set = AlternativeSet::from_candidates(
            // noop_b isn't registered.
            vec![candidate_with_cost(noop_b, CostEstimate::default())],
        );
        let cpu_caps = caps_for_test(BackendId::Cpu);
        let lookup: HashMap<BackendId, BackendCapabilities> =
            [(BackendId::Cpu, cpu_caps)].into_iter().collect();
        compute_static_costs(
            &mut set,
            OpKind::AddElementwise,
            &dtypes,
            &[Shape::from(vec![4])],
            &bindings,
            &|b| lookup.get(&b),
            None,
        );
        assert_eq!(set.alternatives()[0].static_cost, CostEstimate::default());
    }

    // ===== Phase 3: Layer-2 refinement via JudgeOracle =====

    #[test]
    fn judge_refinement_replaces_layer1_with_measured_latency() {
        use crate::ranker::judge::HashMapJudge;

        // Layer 1 says this kernel is 1000 ns; Judge measured 250 ns.
        // After refinement composite_ns should report the measurement.
        fn layer1(_: &[Shape], _: &[DType], _: &OpParams, _: &BackendCapabilities) -> CostEstimate {
            CostEstimate {
                flops: 1000,
                bytes_moved: 0,
                kernel_overhead_ns: 0,
            }
        }
        let mut bindings = KernelBindingTable::new();
        let dtypes = [DType::F32, DType::F32, DType::F32];
        bindings.register_full(
            OpKind::AddElementwise,
            &dtypes,
            BackendId::Cpu,
            noop_a,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            layer1,
        );

        let mut set = AlternativeSet::from_candidates(vec![candidate_with_cost(
            noop_a,
            CostEstimate::default(),
        )]);
        let cpu_caps = caps_for_test(BackendId::Cpu);
        let lookup: HashMap<BackendId, BackendCapabilities> =
            [(BackendId::Cpu, cpu_caps)].into_iter().collect();
        let shapes = [Shape::from(vec![4])];
        let sc = SizeClass::from_elem_count(4);

        let mut judge = HashMapJudge::new();
        judge.insert(
            OpKind::AddElementwise,
            DType::F32,
            sc,
            BackendId::Cpu,
            "",
            250,
            0,
        );

        compute_static_costs(
            &mut set,
            OpKind::AddElementwise,
            &dtypes,
            &shapes,
            &bindings,
            &|b| lookup.get(&b),
            Some(&judge),
        );

        let c = &set.alternatives()[0];
        assert_eq!(c.static_cost.flops, 0, "Layer-2 zeroes FLOPs");
        assert_eq!(c.static_cost.bytes_moved, 0, "Layer-2 zeroes bytes");
        assert_eq!(
            c.static_cost.kernel_overhead_ns, 250,
            "Layer-2 stamps latency"
        );
        // The Judge packs its latency into kernel_overhead_ns with
        // flops = bytes = 0, so composite_ns returns it unchanged for
        // ANY throughput — verified here with a GPU rate to prove the
        // per-backend scaling never touches the measured leg.
        let (cr, bw) = default_backend_rates(BackendId::Cuda);
        assert_eq!(
            composite_ns(&c.static_cost, cr, bw),
            250,
            "composite returns measurement"
        );
    }

    #[test]
    fn judge_missing_measurement_leaves_layer1_intact() {
        // Cell isn't in the Judge map → Layer-1 estimate stays.
        fn layer1(_: &[Shape], _: &[DType], _: &OpParams, _: &BackendCapabilities) -> CostEstimate {
            CostEstimate {
                flops: 1000,
                bytes_moved: 4000,
                kernel_overhead_ns: 50,
            }
        }
        let mut bindings = KernelBindingTable::new();
        let dtypes = [DType::F32, DType::F32, DType::F32];
        bindings.register_full(
            OpKind::AddElementwise,
            &dtypes,
            BackendId::Cpu,
            noop_a,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            layer1,
        );
        let mut set = AlternativeSet::from_candidates(vec![candidate_with_cost(
            noop_a,
            CostEstimate::default(),
        )]);
        let cpu_caps = caps_for_test(BackendId::Cpu);
        let lookup: HashMap<BackendId, BackendCapabilities> =
            [(BackendId::Cpu, cpu_caps)].into_iter().collect();
        let empty_judge = crate::ranker::judge::HashMapJudge::new();
        compute_static_costs(
            &mut set,
            OpKind::AddElementwise,
            &dtypes,
            &[Shape::from(vec![4])],
            &bindings,
            &|b| lookup.get(&b),
            Some(&empty_judge),
        );
        let c = &set.alternatives()[0];
        assert_eq!(c.static_cost.flops, 1000, "Layer-1 FLOPs survive");
        assert_eq!(c.static_cost.bytes_moved, 4000);
        assert_eq!(c.static_cost.kernel_overhead_ns, 50);
    }

    #[test]
    fn judge_refinement_per_backend_can_flip_winner() {
        // Two backends. Layer-1 says CPU cheap, Aocl expensive.
        // Judge measured opposite: Aocl 100ns, CPU 500ns.
        // After refinement + rank, Aocl wins.
        fn cpu_layer1(
            _: &[Shape],
            _: &[DType],
            _: &OpParams,
            _: &BackendCapabilities,
        ) -> CostEstimate {
            CostEstimate {
                flops: 100,
                bytes_moved: 0,
                kernel_overhead_ns: 0,
            }
        }
        fn aocl_layer1(
            _: &[Shape],
            _: &[DType],
            _: &OpParams,
            _: &BackendCapabilities,
        ) -> CostEstimate {
            CostEstimate {
                flops: 1000,
                bytes_moved: 0,
                kernel_overhead_ns: 0,
            }
        }
        let mut bindings = KernelBindingTable::new();
        let dtypes = [DType::F32, DType::F32, DType::F32];
        bindings.register_full(
            OpKind::AddElementwise,
            &dtypes,
            BackendId::Cpu,
            noop_a,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cpu_layer1,
        );
        bindings.register_full(
            OpKind::AddElementwise,
            &dtypes,
            BackendId::Cuda,
            noop_b,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            aocl_layer1,
        );
        let mut set = AlternativeSet::from_candidates(vec![
            Candidate {
                backend: BackendId::Cpu,
                ..candidate_with_cost(noop_a, CostEstimate::default())
            },
            Candidate {
                backend: BackendId::Cuda,
                ..candidate_with_cost(noop_b, CostEstimate::default())
            },
        ]);
        let lookup: HashMap<BackendId, BackendCapabilities> = [
            (BackendId::Cpu, caps_for_test(BackendId::Cpu)),
            (BackendId::Cuda, caps_for_test(BackendId::Cuda)),
        ]
        .into_iter()
        .collect();
        let sc = SizeClass::from_elem_count(4);
        let mut judge = crate::ranker::judge::HashMapJudge::new();
        judge.insert(
            OpKind::AddElementwise,
            DType::F32,
            sc,
            BackendId::Cpu,
            "",
            500,
            0,
        );
        judge.insert(
            OpKind::AddElementwise,
            DType::F32,
            sc,
            BackendId::Cuda,
            "",
            100,
            0,
        );

        compute_static_costs(
            &mut set,
            OpKind::AddElementwise,
            &dtypes,
            &[Shape::from(vec![4])],
            &bindings,
            &|b| lookup.get(&b),
            Some(&judge),
        );
        set.rank_by_composite_cost();
        assert_eq!(
            set.winner().unwrap().backend,
            BackendId::Cuda,
            "Layer-2 measurement reverses Layer-1 verdict",
        );
    }

    #[test]
    fn judge_partial_coverage_mixes_layer1_and_layer2() {
        // Two backends. Judge measured ONE of them; the other keeps
        // Layer-1. Ranking has to handle the mixed-shape cost.
        fn cheap(_: &[Shape], _: &[DType], _: &OpParams, _: &BackendCapabilities) -> CostEstimate {
            CostEstimate {
                flops: 50,
                bytes_moved: 0,
                kernel_overhead_ns: 0,
            }
        }
        fn expensive(
            _: &[Shape],
            _: &[DType],
            _: &OpParams,
            _: &BackendCapabilities,
        ) -> CostEstimate {
            CostEstimate {
                flops: 10_000,
                bytes_moved: 0,
                kernel_overhead_ns: 0,
            }
        }
        let mut bindings = KernelBindingTable::new();
        let dtypes = [DType::F32, DType::F32, DType::F32];
        bindings.register_full(
            OpKind::AddElementwise,
            &dtypes,
            BackendId::Cpu,
            noop_a,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cheap,
        );
        bindings.register_full(
            OpKind::AddElementwise,
            &dtypes,
            BackendId::Cuda,
            noop_b,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            expensive,
        );
        let mut set = AlternativeSet::from_candidates(vec![
            Candidate {
                backend: BackendId::Cpu,
                ..candidate_with_cost(noop_a, CostEstimate::default())
            },
            Candidate {
                backend: BackendId::Cuda,
                ..candidate_with_cost(noop_b, CostEstimate::default())
            },
        ]);
        let lookup: HashMap<BackendId, BackendCapabilities> = [
            (BackendId::Cpu, caps_for_test(BackendId::Cpu)),
            (BackendId::Cuda, caps_for_test(BackendId::Cuda)),
        ]
        .into_iter()
        .collect();
        let sc = SizeClass::from_elem_count(4);
        let mut judge = crate::ranker::judge::HashMapJudge::new();
        // Only measure Aocl (Judge said it's 20ns — way better than
        // Layer-1's 10000-FLOP estimate).
        judge.insert(
            OpKind::AddElementwise,
            DType::F32,
            sc,
            BackendId::Cuda,
            "",
            20,
            0,
        );

        compute_static_costs(
            &mut set,
            OpKind::AddElementwise,
            &dtypes,
            &[Shape::from(vec![4])],
            &bindings,
            &|b| lookup.get(&b),
            Some(&judge),
        );
        set.rank_by_composite_cost();
        // CPU = Layer-1 cost = 50 ns; Aocl = Layer-2 = 20 ns → Aocl wins.
        assert_eq!(
            set.winner().unwrap().backend,
            BackendId::Cuda,
            "partial Judge coverage still influences ranking",
        );
    }

    #[test]
    fn judge_saturates_above_u32_max_ns() {
        fn layer1(_: &[Shape], _: &[DType], _: &OpParams, _: &BackendCapabilities) -> CostEstimate {
            CostEstimate {
                flops: 1,
                bytes_moved: 0,
                kernel_overhead_ns: 0,
            }
        }
        let mut bindings = KernelBindingTable::new();
        let dtypes = [DType::F32, DType::F32, DType::F32];
        bindings.register_full(
            OpKind::AddElementwise,
            &dtypes,
            BackendId::Cpu,
            noop_a,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            layer1,
        );
        let mut set = AlternativeSet::from_candidates(vec![candidate_with_cost(
            noop_a,
            CostEstimate::default(),
        )]);
        let lookup: HashMap<BackendId, BackendCapabilities> =
            [(BackendId::Cpu, caps_for_test(BackendId::Cpu))]
                .into_iter()
                .collect();
        let mut judge = crate::ranker::judge::HashMapJudge::new();
        // Latency exceeds u32::MAX ns (~4.3 s).
        judge.insert(
            OpKind::AddElementwise,
            DType::F32,
            SizeClass::from_elem_count(4),
            BackendId::Cpu,
            "",
            u64::MAX,
            0,
        );
        compute_static_costs(
            &mut set,
            OpKind::AddElementwise,
            &dtypes,
            &[Shape::from(vec![4])],
            &bindings,
            &|b| lookup.get(&b),
            Some(&judge),
        );
        assert_eq!(
            set.alternatives()[0].static_cost.kernel_overhead_ns,
            u32::MAX,
            "u64 → u32 saturating cast pins at u32::MAX",
        );
    }

    // ===== Planner Stage 2: inbound-transfer pricing =====

    /// Per-candidate term = sum over inputs; co-resident inputs
    /// contribute zero.
    #[test]
    fn inbound_transfer_sums_over_offdevice_inputs_only() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut set = AlternativeSet::from_candidates(vec![
            // Local CPU candidate.
            candidate_with_cost(noop_a, CostEstimate::default()),
            // Off-device CUDA candidate.
            Candidate {
                backend: BackendId::Cuda,
                device: cuda0,
                ..candidate_with_cost(noop_b, CostEstimate::default())
            },
        ]);
        let est = FlatEstimator {
            ns_per_byte: 1,
            latency_ns: 100,
        };
        // Two inputs resident on CPU: 12 bytes and 8 bytes.
        let inputs = [(DeviceLocation::Cpu, 12_u64), (DeviceLocation::Cpu, 8_u64)];
        apply_inbound_transfer_costs(&mut set, &inputs, &est);
        assert_eq!(
            set.alternatives()[0].inbound_transfer_ns,
            0,
            "co-resident inputs price zero",
        );
        assert_eq!(
            set.alternatives()[1].inbound_transfer_ns,
            (100 + 12) + (100 + 8),
            "off-device candidate pays latency + bytes per input",
        );
    }

    /// Unknown-residency inputs are absent from the slice — no term
    /// fires, candidates keep zero.
    #[test]
    fn inbound_transfer_empty_inputs_prices_zero() {
        let mut set = AlternativeSet::from_candidates(vec![candidate_with_cost(
            noop_a,
            CostEstimate::default(),
        )]);
        let est = FlatEstimator {
            ns_per_byte: 1_000_000,
            latency_ns: u64::MAX,
        };
        apply_inbound_transfer_costs(&mut set, &[], &est);
        assert_eq!(set.alternatives()[0].inbound_transfer_ns, 0);
    }

    /// Saturating accumulation — absurd per-input estimates pin at
    /// u64::MAX instead of overflowing.
    #[test]
    fn inbound_transfer_saturates() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut set = AlternativeSet::from_candidates(vec![Candidate {
            backend: BackendId::Cuda,
            device: cuda0,
            ..candidate_with_cost(noop_a, CostEstimate::default())
        }]);
        let est = FlatEstimator {
            ns_per_byte: 0,
            latency_ns: u64::MAX,
        };
        let inputs = [(DeviceLocation::Cpu, 1_u64), (DeviceLocation::Cpu, 1_u64)];
        apply_inbound_transfer_costs(&mut set, &inputs, &est);
        assert_eq!(set.alternatives()[0].inbound_transfer_ns, u64::MAX);
    }

    /// The transfer term composes with ranking: equal kernel costs,
    /// the co-resident candidate wins; a big-enough kernel gap still
    /// outranks the transfer.
    #[test]
    fn inbound_transfer_flips_rank_only_when_it_dominates() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let local = |ns: u64| {
            candidate_with_cost(
                noop_a,
                CostEstimate {
                    flops: ns,
                    bytes_moved: 0,
                    kernel_overhead_ns: 0,
                },
            )
        };
        let remote = |ns: u64| Candidate {
            backend: BackendId::Cuda,
            device: cuda0,
            ..candidate_with_cost(
                noop_b,
                CostEstimate {
                    flops: ns,
                    bytes_moved: 0,
                    kernel_overhead_ns: 0,
                },
            )
        };
        let est = FlatEstimator {
            ns_per_byte: 0,
            latency_ns: 1_000,
        };
        let inputs = [(DeviceLocation::Cpu, 4_u64)];

        // Tiny op: remote kernel "faster" (500 vs 600) but the 1µs
        // crossing dominates → local wins.
        let mut tiny = AlternativeSet::from_candidates(vec![remote(500), local(600)]);
        apply_inbound_transfer_costs(&mut tiny, &inputs, &est);
        tiny.rank_by_composite_cost();
        assert_eq!(tiny.winner().unwrap().device, DeviceLocation::Cpu);

        // Huge op: kernel gap (10µs) dwarfs the crossing → remote
        // wins despite paying the transfer.
        let mut huge = AlternativeSet::from_candidates(vec![local(20_000), remote(10_000)]);
        apply_inbound_transfer_costs(&mut huge, &inputs, &est);
        huge.rank_by_composite_cost();
        assert_eq!(huge.winner().unwrap().device, cuda0);
    }
}