vyre-driver-cuda 0.6.1

CUDA/PTX backend for vyre through the CUDA driver API.
Documentation
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
//! CUDA megakernel barrier planning for dependency-typed dataflow waves.
//!
//! The planner is pure and deterministic: it converts a wave dependency DAG
//! into the minimum number of global-synchronization layers implied by those
//! dependencies. Waves inside one layer are independent and can be fused into
//! one cooperative megakernel phase without inserting a host-side barrier.

use crate::backend::accounting::{
    checked_add_u64_count as checked_add, checked_mul_u64_count as checked_mul,
    CudaArithmeticOverflow,
};
use crate::backend::staging_reserve::CudaStorageReserveFailure;
use crate::megakernel_plan_cache::{
    CudaMegakernelAnalysisKind, CudaMegakernelDeviceKey, CudaMegakernelPlanCache,
};
use crate::megakernel_scheduler::{
    CudaMegakernelExecutionPlan, CudaMegakernelGraphShape, CudaMegakernelMemoryError,
    CudaMegakernelScheduleSample,
};
use vyre_driver::megakernel_barrier::{
    plan_megakernel_barriers, plan_megakernel_barriers_with_scratch, MegakernelBarrierGroup,
    MegakernelBarrierPlan, MegakernelBarrierPlanError, MegakernelBarrierScratch,
    MegakernelWaveDependency,
};
use vyre_driver::megakernel_frontier::{
    plan_megakernel_frontier_memory_with_scratch, MegakernelFrontierMemoryPlanError,
    MegakernelFrontierWave,
};

/// Directed dependency between two CUDA megakernel dataflow waves.
pub type CudaMegakernelWaveDependency = MegakernelWaveDependency;

/// One barrier-free group of independent CUDA megakernel waves.
pub type CudaMegakernelBarrierGroup = MegakernelBarrierGroup;

/// Barrier plan for CUDA megakernel execution.
pub type CudaMegakernelBarrierPlan = MegakernelBarrierPlan;

/// Caller-owned scratch for repeated CUDA megakernel barrier planning.
pub type CudaMegakernelBarrierScratch = MegakernelBarrierScratch;

/// Barrier planning failure.
pub type CudaMegakernelBarrierPlanError = MegakernelBarrierPlanError;

/// Plan minimum global barriers for a CUDA megakernel wave dependency DAG.
pub fn plan_cuda_megakernel_barriers(
    wave_count: usize,
    dependencies: &[CudaMegakernelWaveDependency],
) -> Result<CudaMegakernelBarrierPlan, CudaMegakernelBarrierPlanError> {
    plan_megakernel_barriers(wave_count, dependencies)
}

/// Plan minimum global barriers using caller-owned temporary storage.
pub fn plan_cuda_megakernel_barriers_with_scratch(
    wave_count: usize,
    dependencies: &[CudaMegakernelWaveDependency],
    scratch: &mut CudaMegakernelBarrierScratch,
) -> Result<CudaMegakernelBarrierPlan, CudaMegakernelBarrierPlanError> {
    plan_megakernel_barriers_with_scratch(wave_count, dependencies, scratch)
}

/// Frontier-typed CUDA megakernel wave memory envelope.
pub type CudaMegakernelFrontierWave = MegakernelFrontierWave;

/// Dependency-aware CUDA megakernel execution plan for frontier waves.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CudaMegakernelFrontierExecutionPlan {
    /// Cache-backed topology and memory-budget plan.
    pub execution: CudaMegakernelExecutionPlan,
    /// Minimum global-barrier grouping for the wave dependencies.
    pub barriers: CudaMegakernelBarrierPlan,
    /// Peak frontier bytes across any fused barrier-free group.
    pub peak_frontier_bytes: u64,
    /// Peak scratch bytes across any fused barrier-free group.
    pub peak_scratch_bytes: u64,
    /// Peak output bytes across any fused barrier-free group.
    pub peak_output_bytes: u64,
    /// Readback pressure fed into topology selection after combining runtime
    /// telemetry with static fused-wave output volume.
    pub amortized_readback_bytes: u64,
    /// Widest barrier-free group in wave count.
    pub max_group_width: usize,
}

/// Dependency-aware frontier execution planning failure.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CudaMegakernelFrontierExecutionPlanError {
    /// Dependency graph cannot be barrier-planned.
    Barrier(CudaMegakernelBarrierPlanError),
    /// Peak wave bytes overflowed while grouping a barrier-free phase.
    ByteCountOverflow {
        /// Field being accumulated.
        field: &'static str,
    },
    /// Static graph or fused frontier bytes exceed the caller-approved budget.
    GroupOverBudget {
        /// Required bytes before topology selection.
        required_bytes: u64,
        /// Caller-provided budget.
        budget_bytes: u64,
        /// Budget region being checked.
        field: &'static str,
    },
    /// Cache-backed execution memory planning failed.
    Memory(CudaMegakernelMemoryError),
    /// Frontier planning result storage could not be reserved.
    StorageReserveFailed {
        /// Field being reserved.
        field: &'static str,
        /// Number of elements requested.
        requested: usize,
        /// Allocator error text.
        message: String,
    },
}

impl CudaArithmeticOverflow for CudaMegakernelFrontierExecutionPlanError {
    fn arithmetic_overflow(field: &'static str) -> Self {
        Self::ByteCountOverflow { field }
    }
}

impl CudaStorageReserveFailure for CudaMegakernelFrontierExecutionPlanError {
    fn storage_reserve_failed(field: &'static str, requested: usize, message: String) -> Self {
        Self::StorageReserveFailed {
            field,
            requested,
            message,
        }
    }
}

impl std::fmt::Display for CudaMegakernelFrontierExecutionPlanError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Barrier(error) => error.fmt(f),
            Self::ByteCountOverflow { field } => write!(
                f,
                "CUDA megakernel frontier execution planner overflowed while accumulating {field}. Fix: shard the frontier wave group or split the fused phase."
            ),
            Self::GroupOverBudget {
                required_bytes,
                budget_bytes,
                field,
            } => write!(
                f,
                "CUDA megakernel frontier execution planner requires {required_bytes} bytes for {field} but budget allows {budget_bytes}. Fix: shard the graph/frontier waves or raise the explicit CUDA megakernel budget."
            ),
            Self::Memory(error) => error.fmt(f),
            Self::StorageReserveFailed {
                field,
                requested,
                message,
            } => write!(
                f,
                "CUDA megakernel frontier execution planner could not reserve {requested} {field} entries: {message}. Fix: shard the frontier waves before planning."
            ),
        }
    }
}

impl std::error::Error for CudaMegakernelFrontierExecutionPlanError {}

impl From<CudaMegakernelBarrierPlanError> for CudaMegakernelFrontierExecutionPlanError {
    fn from(error: CudaMegakernelBarrierPlanError) -> Self {
        Self::Barrier(error)
    }
}

impl From<CudaMegakernelMemoryError> for CudaMegakernelFrontierExecutionPlanError {
    fn from(error: CudaMegakernelMemoryError) -> Self {
        Self::Memory(error)
    }
}

impl From<MegakernelFrontierMemoryPlanError> for CudaMegakernelFrontierExecutionPlanError {
    fn from(error: MegakernelFrontierMemoryPlanError) -> Self {
        match error {
            MegakernelFrontierMemoryPlanError::Barrier(error) => Self::Barrier(error),
            MegakernelFrontierMemoryPlanError::ByteCountOverflow { field } => {
                Self::ByteCountOverflow { field }
            }
            MegakernelFrontierMemoryPlanError::GroupOverBudget {
                required_bytes,
                budget_bytes,
                field,
            } => Self::GroupOverBudget {
                required_bytes,
                budget_bytes,
                field,
            },
            MegakernelFrontierMemoryPlanError::StorageReserveFailed {
                field,
                requested,
                message,
            } => Self::StorageReserveFailed {
                field,
                requested,
                message,
            },
        }
    }
}

/// Plan dependency-aware CUDA megakernel execution for frontier-typed waves.
///
/// The planner first minimizes global barriers from wave dependencies, then
/// computes the peak memory envelope of any barrier-free fused group, and
/// finally asks the CUDA plan cache for a memory-validated execution topology.
pub fn plan_cuda_frontier_megakernel_execution(
    cache: &mut CudaMegakernelPlanCache,
    graph_layout_hash: u64,
    analysis_kind: CudaMegakernelAnalysisKind,
    device: CudaMegakernelDeviceKey,
    sample: CudaMegakernelScheduleSample,
    graph: CudaMegakernelGraphShape,
    bytes_per_node: u64,
    bytes_per_edge: u64,
    waves: &[CudaMegakernelFrontierWave],
    dependencies: &[CudaMegakernelWaveDependency],
    budget_bytes: u64,
    launch_overhead_ns: f64,
    fusion_pressure: f64,
) -> Result<CudaMegakernelFrontierExecutionPlan, CudaMegakernelFrontierExecutionPlanError> {
    let mut scratch =
        CudaMegakernelBarrierScratch::try_with_capacity(waves.len(), dependencies.len())?;
    plan_cuda_frontier_megakernel_execution_with_scratch(
        cache,
        graph_layout_hash,
        analysis_kind,
        device,
        sample,
        graph,
        bytes_per_node,
        bytes_per_edge,
        waves,
        dependencies,
        budget_bytes,
        launch_overhead_ns,
        fusion_pressure,
        &mut scratch,
    )
}

/// Plan dependency-aware CUDA megakernel execution using caller-owned barrier scratch.
pub fn plan_cuda_frontier_megakernel_execution_with_scratch(
    cache: &mut CudaMegakernelPlanCache,
    graph_layout_hash: u64,
    analysis_kind: CudaMegakernelAnalysisKind,
    device: CudaMegakernelDeviceKey,
    sample: CudaMegakernelScheduleSample,
    graph: CudaMegakernelGraphShape,
    bytes_per_node: u64,
    bytes_per_edge: u64,
    waves: &[CudaMegakernelFrontierWave],
    dependencies: &[CudaMegakernelWaveDependency],
    budget_bytes: u64,
    launch_overhead_ns: f64,
    fusion_pressure: f64,
    scratch: &mut CudaMegakernelBarrierScratch,
) -> Result<CudaMegakernelFrontierExecutionPlan, CudaMegakernelFrontierExecutionPlanError> {
    let graph_bytes = graph_resident_bytes(graph, bytes_per_node, bytes_per_edge)?;
    let memory_plan = plan_megakernel_frontier_memory_with_scratch(
        waves,
        dependencies,
        graph_bytes,
        budget_bytes,
        sample.readback_bytes,
        scratch,
    )?;

    let topology_sample = CudaMegakernelScheduleSample {
        readback_bytes: memory_plan.amortized_readback_bytes,
        ..sample
    };
    let execution = cache.get_or_plan_execution(
        graph_layout_hash,
        analysis_kind,
        device,
        topology_sample,
        graph,
        bytes_per_node,
        bytes_per_edge,
        memory_plan.peak_frontier_bytes,
        memory_plan.peak_scratch_bytes,
        memory_plan.peak_output_bytes,
        budget_bytes,
        launch_overhead_ns,
        fusion_pressure,
    )?;

    Ok(CudaMegakernelFrontierExecutionPlan {
        execution,
        barriers: memory_plan.barriers,
        peak_frontier_bytes: memory_plan.peak_frontier_bytes,
        peak_scratch_bytes: memory_plan.peak_scratch_bytes,
        peak_output_bytes: memory_plan.peak_output_bytes,
        amortized_readback_bytes: topology_sample.readback_bytes,
        max_group_width: memory_plan.max_group_width,
    })
}

fn graph_resident_bytes(
    graph: CudaMegakernelGraphShape,
    bytes_per_node: u64,
    bytes_per_edge: u64,
) -> Result<u64, CudaMegakernelFrontierExecutionPlanError> {
    let node_bytes = checked_mul::<CudaMegakernelFrontierExecutionPlanError>(
        graph.node_count,
        bytes_per_node,
        "node layout bytes",
    )?;
    let edge_bytes = checked_mul::<CudaMegakernelFrontierExecutionPlanError>(
        graph.edge_count,
        bytes_per_edge,
        "edge layout bytes",
    )?;
    checked_add::<CudaMegakernelFrontierExecutionPlanError>(
        node_bytes,
        edge_bytes,
        "graph layout bytes",
    )
}

#[cfg(test)]
mod tests {
    use super::{
        plan_cuda_frontier_megakernel_execution,
        plan_cuda_frontier_megakernel_execution_with_scratch, plan_cuda_megakernel_barriers,
        plan_cuda_megakernel_barriers_with_scratch, CudaMegakernelBarrierPlanError,
        CudaMegakernelBarrierScratch, CudaMegakernelFrontierExecutionPlanError,
        CudaMegakernelFrontierWave, CudaMegakernelWaveDependency,
    };
    use crate::megakernel_plan_cache::{
        CudaMegakernelAnalysisKind, CudaMegakernelDeviceKey, CudaMegakernelPlanCache,
    };
    use crate::megakernel_scheduler::{
        CudaMegakernelGraphShape, CudaMegakernelScheduleSample, CudaMegakernelTopology,
    };

    #[test]
    fn independent_waves_share_one_barrier_free_group() {
        let plan = plan_cuda_megakernel_barriers(4, &[])
            .expect("Fix: independent CUDA megakernel waves should not need barriers.");

        assert_eq!(plan.global_barriers, 0);
        assert_eq!(plan.groups.len(), 1);
        assert_eq!(plan.groups[0].waves, vec![0, 1, 2, 3]);
    }

    #[test]
    fn dependency_chain_requires_one_barrier_between_each_wave() {
        let plan = plan_cuda_megakernel_barriers(
            4,
            &[
                CudaMegakernelWaveDependency {
                    before: 0,
                    after: 1,
                },
                CudaMegakernelWaveDependency {
                    before: 1,
                    after: 2,
                },
                CudaMegakernelWaveDependency {
                    before: 2,
                    after: 3,
                },
            ],
        )
        .expect("Fix: acyclic CUDA megakernel wave chain should be schedulable.");

        assert_eq!(plan.global_barriers, 3);
        assert_eq!(plan.groups[0].waves, vec![0]);
        assert_eq!(plan.groups[1].waves, vec![1]);
        assert_eq!(plan.groups[2].waves, vec![2]);
        assert_eq!(plan.groups[3].waves, vec![3]);
    }

    #[test]
    fn diamond_dependencies_fuse_middle_waves() {
        let plan = plan_cuda_megakernel_barriers(
            4,
            &[
                CudaMegakernelWaveDependency {
                    before: 0,
                    after: 1,
                },
                CudaMegakernelWaveDependency {
                    before: 0,
                    after: 2,
                },
                CudaMegakernelWaveDependency {
                    before: 1,
                    after: 3,
                },
                CudaMegakernelWaveDependency {
                    before: 2,
                    after: 3,
                },
            ],
        )
        .expect("Fix: diamond CUDA megakernel dependencies should preserve middle-wave fusion.");

        assert_eq!(plan.global_barriers, 2);
        assert_eq!(plan.groups[0].waves, vec![0]);
        assert_eq!(plan.groups[1].waves, vec![1, 2]);
        assert_eq!(plan.groups[2].waves, vec![3]);
    }

    #[test]
    fn barrier_planner_uses_csr_adjacency_for_wide_wave_graphs() {
        let dependencies = (1..1_025)
            .map(|after| CudaMegakernelWaveDependency { before: 0, after })
            .collect::<Vec<_>>();
        let plan = plan_cuda_megakernel_barriers(1_025, &dependencies)
            .expect("Fix: wide CUDA megakernel dependency fanout must schedule without per-wave adjacency allocation.");

        assert_eq!(plan.global_barriers, 1);
        assert_eq!(plan.groups[0].waves, vec![0]);
        assert_eq!(plan.groups[1].waves.len(), 1_024);
        let src = include_str!("../../vyre-driver/src/megakernel_barrier.rs");
        assert!(
            !src.contains(concat!("vec![", "Vec::new(); wave_count]")),
            "Fix: CUDA megakernel barrier planner must use contiguous CSR adjacency instead of allocating one Vec per wave."
        );
        assert!(
            !src.contains(concat!("outgoing_offsets[..wave_count]", ".to_vec()")),
            "Fix: CUDA megakernel barrier planner must reuse the counts buffer as the CSR write cursor instead of allocating an O(wave_count) cursor Vec."
        );
        assert!(
            !src.contains(concat!("Vec", "Deque")),
            "Fix: CUDA megakernel barrier planner should use contiguous current/next ready vectors, not deque queue mechanics, for wide wave layers."
        );
        assert!(
            !src.contains(concat!("saturating", "_add")),
            "Fix: CUDA megakernel barrier dependency accounting is bounded by the validated graph shape and must not hide invariant violations with saturating arithmetic."
        );
        assert!(
            src.contains("field: \"outgoing dependency count\"")
                && src.contains("field: \"incoming dependency count\"")
                && src.contains("field: \"outgoing dependency offsets\"")
                && src.contains("field: \"outgoing target cursor\""),
            "Fix: CUDA megakernel barrier CSR construction must use checked arithmetic for dependency counters, offsets, and cursors."
        );
        assert!(
            src.contains("reserve_typed_vec_to_capacity as reserve_vec_to_capacity")
                && src.contains("fn fill_barrier_vec_zeroed(")
                && src.contains("StorageReserveFailed"),
            "Fix: CUDA megakernel barrier and frontier group staging must reserve through shared fallible CUDA staging instead of panicking under scale pressure."
        );

        assert!(
            !src.contains(concat!("Vec::with_capacity", "(wave_count)"))
                && !src.contains(concat!("Vec::with_capacity", "(barriers.groups.len())"))
                && !src.contains(concat!("Vec::with_capacity", "(group.waves.len().min(8))"))
                && !src.contains(concat!(".reserve", "(wave_count)"))
                && !src.contains(concat!("scratch.outgoing_counts", ".resize"))
                && !src.contains(concat!("scratch.indegree", ".resize"))
                && !src.contains(concat!("scratch.outgoing_targets", ".resize")),
            "Fix: CUDA megakernel barrier planner must not use infallible capacity growth in release topology planning."
        );
        assert!(
            !src.contains(concat!(
                "scratch.outgoing_counts[dependency.before]",
                " += 1"
            ))
                && !src.contains(concat!("scratch.indegree[dependency.after]", " += 1"))
                && !src.contains(concat!(
                    "let next = scratch.outgoing_offsets.last().copied().unwrap_or(0)",
                    " + *count"
                )),
            "Fix: CUDA megakernel barrier planning must not use unchecked usize arithmetic for CSR construction."
        );
    }

    #[test]
    fn barrier_planner_reuses_caller_owned_csr_scratch_across_shapes() {
        let mut scratch = CudaMegakernelBarrierScratch::try_with_capacity(1_025, 1_024)
            .expect("Fix: wide reusable CUDA megakernel barrier scratch should fit");
        let wide_dependencies = (1..1_025)
            .map(|after| CudaMegakernelWaveDependency { before: 0, after })
            .collect::<Vec<_>>();
        let wide =
            plan_cuda_megakernel_barriers_with_scratch(1_025, &wide_dependencies, &mut scratch)
                .expect(
                    "Fix: wide CUDA megakernel dependency fanout should plan with reusable scratch",
                );
        let wave_capacity = scratch.wave_capacity();
        let dependency_capacity = scratch.dependency_capacity();

        assert_eq!(wide.groups[1].waves.len(), 1_024);

        let narrow = plan_cuda_megakernel_barriers_with_scratch(
            4,
            &[
                CudaMegakernelWaveDependency {
                    before: 0,
                    after: 1,
                },
                CudaMegakernelWaveDependency {
                    before: 1,
                    after: 2,
                },
                CudaMegakernelWaveDependency {
                    before: 2,
                    after: 3,
                },
            ],
            &mut scratch,
        )
        .expect("Fix: narrow CUDA megakernel dependency chain should reuse larger scratch");

        assert_eq!(narrow.global_barriers, 3);
        assert!(scratch.wave_capacity() >= wave_capacity);
        assert!(scratch.dependency_capacity() >= dependency_capacity);
    }

    #[test]
    fn frontier_execution_planner_accepts_reusable_barrier_scratch() {
        let mut cache = CudaMegakernelPlanCache::new();
        let mut scratch = CudaMegakernelBarrierScratch::try_with_capacity(3, 2)
            .expect("Fix: frontier reusable CUDA megakernel barrier scratch should fit");
        let waves = [
            CudaMegakernelFrontierWave {
                frontier_bytes: 128,
                scratch_bytes: 64,
                output_bytes: 32,
            },
            CudaMegakernelFrontierWave {
                frontier_bytes: 256,
                scratch_bytes: 128,
                output_bytes: 64,
            },
            CudaMegakernelFrontierWave {
                frontier_bytes: 512,
                scratch_bytes: 256,
                output_bytes: 128,
            },
        ];
        let dependencies = [
            CudaMegakernelWaveDependency {
                before: 0,
                after: 1,
            },
            CudaMegakernelWaveDependency {
                before: 1,
                after: 2,
            },
        ];

        let plan = plan_cuda_frontier_megakernel_execution_with_scratch(
            &mut cache,
            77,
            CudaMegakernelAnalysisKind::Dataflow,
            device(),
            CudaMegakernelScheduleSample {
                dispatch_cost_ns: 1_000.0,
                frontier_density: 0.4,
                readback_bytes: 16,
            },
            CudaMegakernelGraphShape {
                node_count: 256,
                edge_count: 512,
            },
            16,
            8,
            &waves,
            &dependencies,
            1 << 20,
            400.0,
            1.0,
            &mut scratch,
        )
        .expect("Fix: frontier megakernel planner should accept caller-owned barrier scratch");

        assert_eq!(plan.barriers.global_barriers, 2);
        assert!(scratch.wave_capacity() >= 3);
        assert!(scratch.dependency_capacity() >= 2);
    }

    #[test]
    fn invalid_or_cyclic_dependencies_fail_loudly() {
        let invalid = plan_cuda_megakernel_barriers(
            2,
            &[CudaMegakernelWaveDependency {
                before: 0,
                after: 2,
            }],
        )
        .expect_err("Fix: invalid CUDA megakernel wave index must fail before planning.");
        assert!(matches!(
            invalid,
            CudaMegakernelBarrierPlanError::InvalidWave { .. }
        ));

        let cycle = plan_cuda_megakernel_barriers(
            2,
            &[
                CudaMegakernelWaveDependency {
                    before: 0,
                    after: 1,
                },
                CudaMegakernelWaveDependency {
                    before: 1,
                    after: 0,
                },
            ],
        )
        .expect_err(
            "Fix: cyclic CUDA megakernel dependencies require explicit fixed-point kernels.",
        );
        assert_eq!(
            cycle,
            CudaMegakernelBarrierPlanError::Cycle {
                unscheduled_waves: 2
            }
        );
    }

    #[test]
    fn frontier_execution_plan_uses_peak_barrier_group_memory() {
        let mut cache = CudaMegakernelPlanCache::new();
        let plan = plan_cuda_frontier_megakernel_execution(
            &mut cache,
            42,
            CudaMegakernelAnalysisKind::Dataflow,
            device(),
            CudaMegakernelScheduleSample {
                dispatch_cost_ns: 1_000.0,
                frontier_density: 0.90,
                readback_bytes: 1 << 20,
            },
            CudaMegakernelGraphShape {
                node_count: 1_000,
                edge_count: 4_000,
            },
            16,
            8,
            &[
                CudaMegakernelFrontierWave {
                    frontier_bytes: 1_024,
                    scratch_bytes: 512,
                    output_bytes: 256,
                },
                CudaMegakernelFrontierWave {
                    frontier_bytes: 2_048,
                    scratch_bytes: 1_024,
                    output_bytes: 512,
                },
                CudaMegakernelFrontierWave {
                    frontier_bytes: 4_096,
                    scratch_bytes: 2_048,
                    output_bytes: 1_024,
                },
                CudaMegakernelFrontierWave {
                    frontier_bytes: 8_192,
                    scratch_bytes: 4_096,
                    output_bytes: 2_048,
                },
            ],
            &[
                CudaMegakernelWaveDependency {
                    before: 0,
                    after: 1,
                },
                CudaMegakernelWaveDependency {
                    before: 0,
                    after: 2,
                },
                CudaMegakernelWaveDependency {
                    before: 1,
                    after: 3,
                },
                CudaMegakernelWaveDependency {
                    before: 2,
                    after: 3,
                },
            ],
            128 * 1024,
            250.0,
            0.95,
        )
        .expect("Fix: frontier-typed CUDA megakernel execution plan should fit the budget.");

        assert_eq!(plan.barriers.global_barriers, 2);
        assert_eq!(plan.barriers.groups[1].waves, vec![1, 2]);
        assert_eq!(plan.peak_frontier_bytes, 8_192);
        assert_eq!(plan.peak_scratch_bytes, 4_096);
        assert_eq!(plan.peak_output_bytes, 2_048);
        assert_eq!(plan.amortized_readback_bytes, 1 << 20);
        assert_eq!(plan.max_group_width, 2);
        assert_eq!(plan.execution.topology, CudaMegakernelTopology::FusedWave);
        assert_eq!(plan.execution.memory.frontier_bytes, 8_192);
    }

    #[test]
    fn frontier_execution_uses_static_group_output_to_trigger_fusion() {
        let mut cache = CudaMegakernelPlanCache::new();
        let plan = plan_cuda_frontier_megakernel_execution(
            &mut cache,
            77,
            CudaMegakernelAnalysisKind::Dataflow,
            device(),
            CudaMegakernelScheduleSample {
                dispatch_cost_ns: 1_000.0,
                frontier_density: 0.50,
                readback_bytes: 0,
            },
            CudaMegakernelGraphShape {
                node_count: 1_000,
                edge_count: 4_000,
            },
            16,
            8,
            &[
                CudaMegakernelFrontierWave {
                    frontier_bytes: 1_024,
                    scratch_bytes: 512,
                    output_bytes: 3_072,
                },
                CudaMegakernelFrontierWave {
                    frontier_bytes: 1_024,
                    scratch_bytes: 512,
                    output_bytes: 3_072,
                },
            ],
            &[],
            128 * 1024,
            250.0,
            0.95,
        )
        .expect("Fix: static output-amortized CUDA frontier plan should fit the budget.");

        assert_eq!(plan.peak_output_bytes, 6_144);
        assert_eq!(plan.amortized_readback_bytes, 6_144);
        assert_eq!(
            plan.execution.topology,
            CudaMegakernelTopology::FusedWave,
            "Fix: high static fused-group output pressure must trigger megakernel fusion even when the previous telemetry interval had no final readback."
        );
    }

    #[test]
    fn frontier_execution_splits_independent_layers_to_fit_fused_memory_budget() {
        let mut cache = CudaMegakernelPlanCache::new();
        let waves = [
            CudaMegakernelFrontierWave {
                frontier_bytes: 10,
                scratch_bytes: 10,
                output_bytes: 10,
            },
            CudaMegakernelFrontierWave {
                frontier_bytes: 10,
                scratch_bytes: 10,
                output_bytes: 10,
            },
            CudaMegakernelFrontierWave {
                frontier_bytes: 10,
                scratch_bytes: 10,
                output_bytes: 10,
            },
        ];
        let plan = plan_cuda_frontier_megakernel_execution(
            &mut cache,
            909,
            CudaMegakernelAnalysisKind::Dataflow,
            device(),
            CudaMegakernelScheduleSample {
                dispatch_cost_ns: 1_000.0,
                frontier_density: 0.50,
                readback_bytes: 4_096,
            },
            CudaMegakernelGraphShape {
                node_count: 1,
                edge_count: 0,
            },
            0,
            0,
            &waves,
            &[],
            100,
            250.0,
            0.95,
        )
        .expect("Fix: independent CUDA frontier waves should split into memory-fit fused chunks instead of failing the release path.");

        assert_eq!(plan.barriers.groups.len(), 3);
        assert_eq!(plan.barriers.global_barriers, 2);
        assert_eq!(plan.max_group_width, 1);
        assert_eq!(plan.peak_frontier_bytes, 10);
        assert_eq!(plan.peak_scratch_bytes, 10);
        assert_eq!(plan.peak_output_bytes, 10);
        assert_eq!(plan.execution.topology, CudaMegakernelTopology::FusedWave);
        assert_eq!(plan.execution.memory.required_bytes, 60);
    }

    #[test]
    fn frontier_execution_rejects_graph_bytes_over_budget_without_zero_budget_default() {
        let mut cache = CudaMegakernelPlanCache::new();
        let error = plan_cuda_frontier_megakernel_execution(
            &mut cache,
            910,
            CudaMegakernelAnalysisKind::Dataflow,
            device(),
            CudaMegakernelScheduleSample {
                dispatch_cost_ns: 1_000.0,
                frontier_density: 0.50,
                readback_bytes: 4_096,
            },
            CudaMegakernelGraphShape {
                node_count: 100,
                edge_count: 100,
            },
            8,
            8,
            &[CudaMegakernelFrontierWave {
                frontier_bytes: 1,
                scratch_bytes: 1,
                output_bytes: 1,
            }],
            &[],
            1_000,
            250.0,
            0.95,
        )
        .expect_err("resident graph bytes above budget must fail before split planning");

        assert_eq!(
            error,
            CudaMegakernelFrontierExecutionPlanError::GroupOverBudget {
                required_bytes: 1_600,
                budget_bytes: 1_000,
                field: "resident graph bytes",
            }
        );
    }

    #[test]
    fn frontier_execution_rejects_single_wave_that_cannot_fit_group_budget() {
        let mut cache = CudaMegakernelPlanCache::new();
        let error = plan_cuda_frontier_megakernel_execution(
            &mut cache,
            911,
            CudaMegakernelAnalysisKind::Dataflow,
            device(),
            CudaMegakernelScheduleSample {
                dispatch_cost_ns: 1_000.0,
                frontier_density: 0.50,
                readback_bytes: 4_096,
            },
            CudaMegakernelGraphShape {
                node_count: 1,
                edge_count: 0,
            },
            0,
            0,
            &[CudaMegakernelFrontierWave {
                frontier_bytes: 100,
                scratch_bytes: 100,
                output_bytes: 100,
            }],
            &[],
            500,
            250.0,
            0.95,
        )
        .expect_err("single fused wave above group budget must fail before topology planning");

        assert_eq!(
            error,
            CudaMegakernelFrontierExecutionPlanError::GroupOverBudget {
                required_bytes: 600,
                budget_bytes: 500,
                field: "single fused frontier wave bytes",
            }
        );
    }

    #[test]
    fn frontier_execution_plan_reuses_cached_topology_for_equivalent_pressure() {
        let mut cache = CudaMegakernelPlanCache::new();
        let waves = [
            CudaMegakernelFrontierWave {
                frontier_bytes: 1_024,
                scratch_bytes: 512,
                output_bytes: 256,
            },
            CudaMegakernelFrontierWave {
                frontier_bytes: 2_048,
                scratch_bytes: 1_024,
                output_bytes: 512,
            },
        ];
        let first = plan_cuda_frontier_megakernel_execution(
            &mut cache,
            42,
            CudaMegakernelAnalysisKind::ParserFrontend,
            device(),
            CudaMegakernelScheduleSample {
                dispatch_cost_ns: 1_000.0,
                frontier_density: 0.90,

                readback_bytes: 1 << 20,
            },
            CudaMegakernelGraphShape {
                node_count: 1_000,
                edge_count: 4_000,
            },
            16,
            8,
            &waves,
            &[],
            128 * 1024,
            250.0,
            0.95,
        )
        .expect("Fix: first frontier execution plan should fit.");
        let second = plan_cuda_frontier_megakernel_execution(
            &mut cache,
            42,
            CudaMegakernelAnalysisKind::ParserFrontend,
            device(),
            CudaMegakernelScheduleSample {
                dispatch_cost_ns: 1_000.0,
                frontier_density: 0.91,
                readback_bytes: 1 << 20,
            },
            CudaMegakernelGraphShape {
                node_count: 1_000,
                edge_count: 4_000,
            },
            16,
            8,
            &waves,
            &[],
            128 * 1024,
            250.0,
            0.95,
        )
        .expect("Fix: equivalent frontier execution pressure should reuse cached topology.");

        assert_eq!(first.execution.topology, CudaMegakernelTopology::FusedWave);
        assert_eq!(second.execution.topology, CudaMegakernelTopology::FusedWave);
        assert_eq!(cache.stats().hits, 1);
    }

    #[test]
    fn frontier_execution_plan_fails_loudly_on_wave_byte_overflow() {
        let mut cache = CudaMegakernelPlanCache::new();
        let error = plan_cuda_frontier_megakernel_execution(
            &mut cache,
            42,
            CudaMegakernelAnalysisKind::Dataflow,
            device(),
            CudaMegakernelScheduleSample {
                dispatch_cost_ns: 1_000.0,
                frontier_density: 0.90,
                readback_bytes: 1 << 20,
            },
            CudaMegakernelGraphShape {
                node_count: 1,
                edge_count: 1,
            },
            1,
            1,
            &[
                CudaMegakernelFrontierWave {
                    frontier_bytes: u64::MAX,
                    scratch_bytes: 1,
                    output_bytes: 1,
                },
                CudaMegakernelFrontierWave {
                    frontier_bytes: 1,
                    scratch_bytes: 1,
                    output_bytes: 1,
                },
            ],
            &[],
            u64::MAX,
            250.0,
            0.95,
        )
        .expect_err("Fix: overflowed frontier wave bytes must fail before CUDA launch planning.");

        assert_eq!(
            error,
            CudaMegakernelFrontierExecutionPlanError::ByteCountOverflow {
                field: "fused wave bytes"
            }
        );
    }

    fn device() -> CudaMegakernelDeviceKey {
        CudaMegakernelDeviceKey {
            sm_major: 12,
            sm_minor: 0,
            warp_size: 32,
            supports_grid_sync: true,
            supports_tensor_cores: true,
            max_workgroup_size: 1024,
        }
    }
}