vyre-driver-cuda 0.7.2

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
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
//! `CompiledPipeline` implementation for precompiled CUDA pipelines.
//!
//! The parent `pipeline` module owns construction and static launch state. This
//! module owns dispatch entrypoints, CUDA graph replay selection, dynamic GPU
//! dispatch when runtime policy changes, and persistent-resource output routing.

use std::sync::MutexGuard;

use smallvec::SmallVec;
use vyre_driver::{
    borrowed_input_batch_shapes_match, dispatch_configs_share_launch_shape, BackendError,
    BindingRole, CompiledPipeline, DispatchConfig, OutputBuffers, Resource,
};

use crate::backend::cuda_graph_replay::{CudaGraphReplayInputState, CudaGraphReplayStats};
use crate::backend::resident::CudaDispatchBinding;
use crate::backend::resident_dispatch::{next_dispatch_binding, CudaResidentDispatch};
use crate::backend::staging_reserve::{reserve_smallvec, reserved_vec, resize_vec_slots};
use crate::backend::CachedCudaGraph;
use crate::numeric::CUDA_NUMERIC;
use crate::pipeline::materialized_cache::{materialized_input_key, MaterializedInputKey};
use crate::pipeline::{
    cuda_graph_lane_count_for_batch, cuda_graph_replay_enabled, CudaCompiledPipeline,
    MaterializedPipelineOutputCache, MaterializedPipelineOutputCacheEntry,
    MAX_GRAPH_CACHE_ENTRIES_PER_PIPELINE,
};

fn compiled_graph_batch_inputs<'a>(
    batches: &'a [&[&[u8]]],
    batch_index: usize,
    context: &'static str,
) -> Result<&'a [&'a [u8]], BackendError> {
    batches
        .get(batch_index)
        .copied()
        .ok_or_else(|| BackendError::InvalidProgram {
            fix: format!(
                "Fix: compiled CUDA graph replay {context} expected batch index {batch_index} but only {} batch input set(s) were supplied. Rebuild the materialized batch partition before replay.",
                batches.len()
            ),
        })
}

fn compiled_graph_output<'a>(
    outputs: &'a [OutputBuffers],
    batch_index: usize,
    context: &'static str,
) -> Result<&'a OutputBuffers, BackendError> {
    outputs
        .get(batch_index)
        .ok_or_else(|| BackendError::InvalidProgram {
            fix: format!(
                "Fix: compiled CUDA graph replay {context} expected output batch index {batch_index} but only {} output slot(s) were available. Resize output slots before replay.",
                outputs.len()
            ),
        })
}

fn compiled_graph_output_mut<'a>(
    outputs: &'a mut [OutputBuffers],
    batch_index: usize,
    context: &'static str,
) -> Result<&'a mut OutputBuffers, BackendError> {
    let output_count = outputs.len();
    outputs
        .get_mut(batch_index)
        .ok_or_else(|| BackendError::InvalidProgram {
            fix: format!(
                "Fix: compiled CUDA graph replay {context} expected mutable output batch index {batch_index} but only {output_count} output slot(s) were available. Resize output slots before replay.",
            ),
        })
}

fn compiled_graph_lane<'a>(
    lanes: &'a [CachedCudaGraph],
    lane: usize,
    context: &'static str,
) -> Result<&'a CachedCudaGraph, BackendError> {
    let lane_count = lanes.len();
    lanes.get(lane).ok_or_else(|| BackendError::InvalidProgram {
        fix: format!(
            "Fix: compiled CUDA graph replay {context} expected lane {lane} but only {lane_count} cached graph lane(s) were available. Rebuild the graph replay lane plan.",
        ),
    })
}

fn compiled_graph_lane_mut<'a>(
    lanes: &'a mut [CachedCudaGraph],
    lane: usize,
    context: &'static str,
) -> Result<&'a mut CachedCudaGraph, BackendError> {
    let lane_count = lanes.len();
    lanes
        .get_mut(lane)
        .ok_or_else(|| BackendError::InvalidProgram {
            fix: format!(
                "Fix: compiled CUDA graph replay {context} expected lane {lane} but only {lane_count} cached graph lane(s) were available. Rebuild the graph replay lane plan.",
            ),
        })
}

#[derive(Clone, Copy, Debug)]
struct MaterializedBatchMiss {
    batch_index: usize,
    input_key: MaterializedInputKey,
}

#[derive(Clone, Copy, Debug)]
struct LaunchedMaterializedBatch {
    lane: usize,
    batch_index: usize,
    input_key: MaterializedInputKey,
    replay_stats: CudaGraphReplayStats,
}

#[derive(Debug)]
struct CachedGraphReplaySelection {
    graph: CachedCudaGraph,
    input_state: CudaGraphReplayInputState,
}

impl CompiledPipeline for CudaCompiledPipeline {
    fn id(&self) -> &str {
        &self.id
    }

    fn dispatch(
        &self,
        inputs: &[Vec<u8>],
        config: &DispatchConfig,
    ) -> Result<Vec<Vec<u8>>, BackendError> {
        let borrowed = vyre_driver::borrowed_input_slices(inputs, "cuda compiled borrowed input")?;
        self.dispatch_borrowed(&borrowed, config)
    }

    fn dispatch_borrowed(
        &self,
        inputs: &[&[u8]],
        config: &DispatchConfig,
    ) -> Result<Vec<Vec<u8>>, BackendError> {
        if !dispatch_configs_share_launch_shape(&self.compiled_config, config) {
            return self
                .backend
                .dispatch_borrowed_async_with_ptx_key(
                    &self.program,
                    inputs,
                    config,
                    &self.ptx_src,
                    self.module_key,
                )?
                .await_result();
        }
        let mut outputs = reserved_vec(self.prepared.output_binding_indices.len(), "output")?;
        self.dispatch_borrowed_into(inputs, config, &mut outputs)?;
        Ok(outputs)
    }

    fn dispatch_borrowed_timed(
        &self,
        inputs: &[&[u8]],
        config: &DispatchConfig,
    ) -> Result<vyre_driver::TimedDispatchResult, BackendError> {
        let _profiler_range =
            crate::profiler::cuda_profiler_range(crate::profiler::CUDA_PIPELINE_DISPATCH_RANGE);
        if !dispatch_configs_share_launch_shape(&self.compiled_config, config) {
            return self.backend.dispatch_borrowed_timed_with_ptx_key(
                &self.program,
                inputs,
                config,
                &self.ptx_src,
                self.module_key,
            );
        }
        if !cuda_graph_replay_enabled() || self.prepared.cooperative {
            return self.backend.dispatch_borrowed_timed_with_ptx_key(
                &self.program,
                inputs,
                config,
                &self.ptx_src,
                self.module_key,
            );
        }
        let started = std::time::Instant::now();
        let mut outputs = reserved_vec(self.prepared.output_binding_indices.len(), "timed output")?;
        let input_key = materialized_input_key(inputs)?;
        if self.materialized_output_cache_hit_with_key_into(inputs, &input_key, &mut outputs)? {
            let wall_ns = CUDA_NUMERIC
                .elapsed_nanos_u64(started, "cuda graph materialized timed hit wall latency")?;
            // Materialized output cache hit: no kernel was launched, so the
            // device performed exactly zero work.  Report Some(0) -- the exact,
            // non-fabricated device time for a hit.  None would mean "device
            // time unknown" and route this to timed_dispatches_missing_device_time,
            // which is wrong: we know the device did nothing.  Some(0) also lets
            // the release perf gate see the cache eliminate device work (0 ns).
            self.backend
                .telemetry
                .record_timed_dispatch(wall_ns, Some(0), None, None);
            return Ok(vyre_driver::TimedDispatchResult {
                outputs,
                wall_ns,
                device_ns: Some(0),
                enqueue_ns: None,
                wait_ns: None,
            });
        }
        let (mut cached, input_state) = match self
            .take_cached_graph_with_replay_state(inputs, &input_key)?
        {
            Some(selection) => (selection.graph, selection.input_state),
            None => {
                let cached =
                    self.backend
                        .record_cuda_graph_borrowed(&self.program, inputs, config)?;
                let input_state = self
                    .backend
                    .prepare_cuda_graph_replay_input_state_with_key(&cached, inputs, input_key)?;
                (cached, input_state)
            }
        };
        let replay_result = self
            .backend
            .dispatch_via_cuda_graph_timed_with_input_state_into(
                &mut cached,
                inputs,
                &input_state,
                &mut outputs,
            );
        if replay_result.is_ok() {
            self.return_cached_graph(cached)?;
            self.remember_materialized_output_cache_with_key(inputs, input_key, &outputs)?;
        } else {
            std::mem::forget(cached);
        }
        let device_ns = replay_result?;
        let wall_ns = CUDA_NUMERIC.elapsed_nanos_u64(started, "cuda graph replay wall latency")?;
        self.backend
            .telemetry
            .record_timed_dispatch(wall_ns, device_ns, None, None);
        Ok(vyre_driver::TimedDispatchResult {
            outputs,
            wall_ns,
            device_ns,
            enqueue_ns: None,
            wait_ns: None,
        })
    }

    fn dispatch_borrowed_into(
        &self,
        inputs: &[&[u8]],
        config: &DispatchConfig,
        outputs: &mut OutputBuffers,
    ) -> Result<(), BackendError> {
        let _profiler_range =
            crate::profiler::cuda_profiler_range(crate::profiler::CUDA_PIPELINE_DISPATCH_RANGE);
        if !dispatch_configs_share_launch_shape(&self.compiled_config, config) {
            self.backend
                .dispatch_borrowed_async_with_ptx_key(
                    &self.program,
                    inputs,
                    config,
                    &self.ptx_src,
                    self.module_key,
                )?
                .await_result_into(outputs)?;
            return Ok(());
        }
        if !cuda_graph_replay_enabled() || self.prepared.cooperative {
            self.backend
                .dispatch_borrowed_async_with_ptx_key(
                    &self.program,
                    inputs,
                    config,
                    &self.ptx_src,
                    self.module_key,
                )?
                .await_result_into(outputs)?;
            return Ok(());
        }
        let input_key = materialized_input_key(inputs)?;
        if self.materialized_output_cache_hit_with_key_into(inputs, &input_key, outputs)? {
            return Ok(());
        }
        let (mut cached, input_state) = match self
            .take_cached_graph_with_replay_state(inputs, &input_key)?
        {
            Some(selection) => (selection.graph, selection.input_state),
            None => {
                let cached =
                    self.backend
                        .record_cuda_graph_borrowed(&self.program, inputs, config)?;
                let input_state = self
                    .backend
                    .prepare_cuda_graph_replay_input_state_with_key(&cached, inputs, input_key)?;
                (cached, input_state)
            }
        };
        let replay_result = self.backend.dispatch_via_cuda_graph_with_input_state_into(
            &mut cached,
            inputs,
            &input_state,
            outputs,
        );
        if replay_result.is_ok() {
            self.return_cached_graph(cached)?;
            self.remember_materialized_output_cache_with_key(inputs, input_key, outputs)?;
        } else {
            std::mem::forget(cached);
        }
        replay_result
    }

    fn dispatch_borrowed_batched(
        &self,
        batches: &[&[&[u8]]],
        config: &DispatchConfig,
    ) -> Result<Vec<OutputBuffers>, BackendError> {
        let mut outputs = reserved_vec(batches.len(), "batched output")?;
        self.dispatch_borrowed_batched_into(batches, config, &mut outputs)?;
        Ok(outputs)
    }

    fn dispatch_borrowed_batched_into(
        &self,
        batches: &[&[&[u8]]],
        config: &DispatchConfig,
        outputs: &mut Vec<OutputBuffers>,
    ) -> Result<(), BackendError> {
        let _profiler_range = crate::profiler::cuda_profiler_range(
            crate::profiler::CUDA_PIPELINE_BATCH_DISPATCH_RANGE,
        );
        if batches.is_empty() {
            outputs.clear();
            return Ok(());
        }
        if cuda_graph_replay_enabled()
            && !self.prepared.cooperative
            && dispatch_configs_share_launch_shape(&self.compiled_config, config)
            && borrowed_input_batch_shapes_match(batches)
        {
            return self.dispatch_borrowed_batched_via_cuda_graph_lanes(batches, config, outputs);
        }
        let mut pending = SmallVec::<[_; 8]>::new();
        reserve_smallvec(&mut pending, batches.len(), "pending dispatch")?;
        if dispatch_configs_share_launch_shape(&self.compiled_config, config) {
            for inputs in batches {
                pending.push(self.backend.dispatch_prepared_borrowed_async_with_ptx_key(
                    &self.program,
                    inputs,
                    &self.ptx_src,
                    self.module_key,
                    &self.prepared,
                )?);
            }
        } else {
            for inputs in batches {
                pending.push(self.backend.dispatch_borrowed_async_with_ptx_key(
                    &self.program,
                    inputs,
                    config,
                    &self.ptx_src,
                    self.module_key,
                )?);
            }
        }

        resize_vec_slots(outputs, pending.len(), "batched output")?;
        for (dispatch, item_outputs) in pending.into_iter().zip(outputs.iter_mut()) {
            dispatch.await_result_into(item_outputs)?;
        }
        Ok(())
    }

    fn dispatch_persistent_handles(
        &self,
        inputs: &[Resource],
        config: &DispatchConfig,
    ) -> Result<OutputBuffers, BackendError> {
        let mut outputs = reserved_vec(
            self.prepared.output_binding_indices.len(),
            "persistent output",
        )?;
        self.dispatch_persistent_handles_into(inputs, config, &mut outputs)?;
        Ok(outputs)
    }

    fn dispatch_persistent_handles_timed(
        &self,
        inputs: &[Resource],
        config: &DispatchConfig,
    ) -> Result<vyre_driver::TimedDispatchResult, BackendError> {
        let _profiler_range =
            crate::profiler::cuda_profiler_range(crate::profiler::CUDA_PIPELINE_DISPATCH_RANGE);
        if crate::instrumentation::cuda_resident_borrowed_fallback_enabled() {
            let started = std::time::Instant::now();
            let outputs = self.dispatch_persistent_handles(inputs, config)?;
            let wall_ns = crate::numeric::CUDA_NUMERIC
                .elapsed_nanos_u64(started, "compiled persistent fallback wall latency")?;
            self.backend
                .telemetry
                .record_timed_dispatch(wall_ns, None, None, None);
            return Ok(vyre_driver::TimedDispatchResult {
                outputs,
                wall_ns,
                device_ns: None,
                enqueue_ns: None,
                wait_ns: None,
            });
        }
        if !dispatch_configs_share_launch_shape(&self.compiled_config, config) {
            let started = std::time::Instant::now();
            let enqueue_started = std::time::Instant::now();
            let bindings = self.backend.resident_bindings_from_resources(inputs)?;
            let prepared =
                self.backend
                    .prepare_resident_dispatch(&self.program, &bindings, config)?;
            let dispatch = self.backend.dispatch_resident_async_concrete_with_ptx_key(
                &self.program,
                &bindings,
                config,
                &self.ptx_src,
                self.module_key,
                true,
                None,
                true,
                &prepared,
            )?;
            let enqueue_ns = crate::numeric::CUDA_NUMERIC
                .elapsed_nanos_u64(enqueue_started, "compiled persistent enqueue latency")?;
            let wait_started = std::time::Instant::now();
            let (outputs, device_ns) = dispatch.pending.await_timed_result()?;
            let wait_ns = crate::numeric::CUDA_NUMERIC
                .elapsed_nanos_u64(wait_started, "compiled persistent wait latency")?;
            let wall_ns = crate::numeric::CUDA_NUMERIC
                .elapsed_nanos_u64(started, "compiled persistent wall latency")?;
            self.backend.telemetry.record_timed_dispatch(
                wall_ns,
                device_ns,
                Some(enqueue_ns),
                Some(wait_ns),
            );
            return Ok(vyre_driver::TimedDispatchResult {
                outputs,
                wall_ns,
                device_ns,
                enqueue_ns: Some(enqueue_ns),
                wait_ns: Some(wait_ns),
            });
        }

        let started = std::time::Instant::now();
        let enqueue_started = std::time::Instant::now();
        let bindings = self.backend.resident_bindings_from_resources(inputs)?;
        let dispatch = self.backend.dispatch_resident_async_concrete_with_ptx_key(
            &self.program,
            &bindings,
            config,
            &self.ptx_src,
            self.module_key,
            true,
            (self.static_params.ptr != 0).then_some(self.static_params.ptr),
            true,
            &self.prepared,
        )?;
        let enqueue_ns = crate::numeric::CUDA_NUMERIC
            .elapsed_nanos_u64(enqueue_started, "compiled persistent enqueue latency")?;
        let wait_started = std::time::Instant::now();
        let (outputs, device_ns) = dispatch.pending.await_timed_result()?;
        let wait_ns = crate::numeric::CUDA_NUMERIC
            .elapsed_nanos_u64(wait_started, "compiled persistent wait latency")?;
        let wall_ns = crate::numeric::CUDA_NUMERIC
            .elapsed_nanos_u64(started, "compiled persistent wall latency")?;
        self.backend.telemetry.record_timed_dispatch(
            wall_ns,
            device_ns,
            Some(enqueue_ns),
            Some(wait_ns),
        );
        Ok(vyre_driver::TimedDispatchResult {
            outputs,
            wall_ns,
            device_ns,
            enqueue_ns: Some(enqueue_ns),
            wait_ns: Some(wait_ns),
        })
    }

    fn dispatch_persistent_handles_into(
        &self,
        inputs: &[Resource],
        config: &DispatchConfig,
        outputs: &mut OutputBuffers,
    ) -> Result<(), BackendError> {
        let _profiler_range =
            crate::profiler::cuda_profiler_range(crate::profiler::CUDA_PIPELINE_DISPATCH_RANGE);
        let bindings = self.backend.resident_bindings_from_resources(inputs)?;
        if dispatch_configs_share_launch_shape(&self.compiled_config, config)
            && !crate::instrumentation::cuda_resident_borrowed_fallback_enabled()
        {
            let dispatch = self.backend.dispatch_resident_async_concrete_with_ptx_key(
                &self.program,
                &bindings,
                config,
                &self.ptx_src,
                self.module_key,
                false,
                (self.static_params.ptr != 0).then_some(self.static_params.ptr),
                true,
                &self.prepared,
            )?;
            let (dispatch_outputs, _) = dispatch.pending.await_timed_result()?;
            vyre_driver::replace_output_buffers_preserving_slots(dispatch_outputs, outputs);
            return Ok(());
        }
        self.backend.dispatch_resident_outputs_with_ptx_key_into(
            &self.program,
            &bindings,
            config,
            &self.ptx_src,
            self.module_key,
            outputs,
        )
    }

    fn dispatch_persistent_handles_batched(
        &self,
        batches: &[&[Resource]],
        config: &DispatchConfig,
    ) -> Result<Vec<OutputBuffers>, BackendError> {
        let mut outputs = reserved_vec(batches.len(), "persistent batched output")?;
        self.dispatch_persistent_handles_batched_into(batches, config, &mut outputs)?;
        Ok(outputs)
    }

    fn dispatch_persistent_handles_batched_into(
        &self,
        batches: &[&[Resource]],
        config: &DispatchConfig,
        outputs: &mut Vec<OutputBuffers>,
    ) -> Result<(), BackendError> {
        let _profiler_range = crate::profiler::cuda_profiler_range(
            crate::profiler::CUDA_PIPELINE_BATCH_DISPATCH_RANGE,
        );
        if batches.is_empty() {
            outputs.clear();
            return Ok(());
        }
        let mut resident_batches =
            SmallVec::<[SmallVec<[crate::backend::CudaResidentBuffer; 8]>; 8]>::new();
        reserve_smallvec(&mut resident_batches, batches.len(), "resident batch")?;
        for batch in batches {
            resident_batches.push(self.backend.resident_handles_from_resources(batch)?);
        }

        self.dispatch_resident_batches_into(&resident_batches, config, outputs)
    }

    fn dispatch_persistent_handle_rows_into(
        &self,
        rows: &[[Resource; 4]],
        config: &DispatchConfig,
        outputs: &mut Vec<OutputBuffers>,
    ) -> Result<(), BackendError> {
        let _profiler_range = crate::profiler::cuda_profiler_range(
            crate::profiler::CUDA_PIPELINE_BATCH_DISPATCH_RANGE,
        );
        if rows.is_empty() {
            outputs.clear();
            return Ok(());
        }
        let mut resident_batches =
            SmallVec::<[SmallVec<[crate::backend::CudaResidentBuffer; 8]>; 8]>::new();
        reserve_smallvec(&mut resident_batches, rows.len(), "resident row batch")?;
        for row in rows {
            resident_batches.push(
                self.backend
                    .resident_handles_from_resources(row.as_slice())?,
            );
        }

        self.dispatch_resident_batches_into(&resident_batches, config, outputs)
    }

    fn dispatch_persistent_resource_outputs(
        &self,
        inputs: &[Resource],
        config: &DispatchConfig,
    ) -> Result<Vec<Resource>, BackendError> {
        let _profiler_range =
            crate::profiler::cuda_profiler_range(crate::profiler::CUDA_PIPELINE_DISPATCH_RANGE);
        let bindings = self.backend.resident_bindings_from_resources(inputs)?;
        let borrowed_fallback = crate::instrumentation::cuda_resident_borrowed_fallback_enabled();
        let same_shape = dispatch_configs_share_launch_shape(&self.compiled_config, config);
        let prepared_storage;
        let (prepared, static_params_ptr) = if same_shape {
            (
                &self.prepared,
                (self.static_params.ptr != 0).then_some(self.static_params.ptr),
            )
        } else {
            prepared_storage =
                self.backend
                    .prepare_resident_dispatch(&self.program, &bindings, config)?;
            (&prepared_storage, None)
        };
        let mut output_handles = SmallVec::<[crate::backend::CudaResidentBuffer; 8]>::new();
        reserve_smallvec(
            &mut output_handles,
            prepared.output_binding_indices.len(),
            "compiled resident resource output handle",
        )?;
        let mut next_binding = 0usize;
        for binding in &prepared.bindings.bindings {
            if binding.role == BindingRole::Shared {
                continue;
            }
            let source = next_dispatch_binding(
                &bindings,
                &mut next_binding,
                "compiled resident resource output routing",
            )?;
            if binding.output_index.is_some() {
                // This entry point hands back a Resource::Resident per output,
                // naming device memory that outlives the call. A borrowed
                // output is staged from the transient pool and recycled when
                // the dispatch ends, so there is no lasting id to return.
                let CudaDispatchBinding::Resident(handle) = source else {
                    return Err(BackendError::InvalidProgram {
                        fix: format!(
                            "Fix: CUDA compiled resident resource output `{}` was given a borrowed resource, but this entry point returns Resource::Resident outputs that outlive the dispatch. Bind a resident buffer for that output, or use a dispatch entry point that returns output bytes.",
                            binding.name
                        ),
                    });
                };
                output_handles.push(handle);
            }
        }
        if borrowed_fallback {
            self.backend
                .dispatch_resident_via_borrowed(&self.program, &bindings, config)?;
        } else {
            self.backend
                .dispatch_resident_async_concrete_with_ptx_key(
                    &self.program,
                    &bindings,
                    config,
                    &self.ptx_src,
                    self.module_key,
                    false,
                    static_params_ptr,
                    false,
                    prepared,
                )?
                .pending
                .await_timed_result()?;
        }
        let mut resources = reserved_vec(output_handles.len(), "resource output")?;
        for handle in output_handles {
            resources.push(Resource::Resident(handle.handle));
        }
        Ok(resources)
    }
}

impl CudaCompiledPipeline {
    fn dispatch_resident_batches_into(
        &self,
        resident_batches: &[SmallVec<[crate::backend::CudaResidentBuffer; 8]>],
        config: &DispatchConfig,
        outputs: &mut Vec<OutputBuffers>,
    ) -> Result<(), BackendError> {
        if resident_batches.is_empty() {
            outputs.clear();
            return Ok(());
        }
        if !dispatch_configs_share_launch_shape(&self.compiled_config, config) {
            return self.dispatch_dynamic_persistent_batches_concurrently(
                resident_batches,
                config,
                outputs,
            );
        }

        let resident_dispatch = self
            .backend
            .dispatch_resident_batch_async_concrete_with_ptx_key(
                &self.program,
                resident_batches,
                config,
                &self.ptx_src,
                self.module_key,
                (self.static_params.ptr != 0).then_some(self.static_params.ptr),
                &self.prepared,
            );
        let resident_dispatch = resident_dispatch?;
        let output_handles = resident_dispatch.output_handles;
        let output_readbacks = resident_dispatch.output_readbacks;
        resident_dispatch.pending.await_timed_result()?;
        self.backend.download_resident_readback_batches_many_into(
            &output_handles,
            &output_readbacks,
            outputs,
        )
    }

    fn dispatch_dynamic_persistent_batches_concurrently(
        &self,
        resident_batches: &[SmallVec<[crate::backend::CudaResidentBuffer; 8]>],
        config: &DispatchConfig,
        outputs: &mut Vec<OutputBuffers>,
    ) -> Result<(), BackendError> {
        let mut dispatches = SmallVec::<[CudaResidentDispatch; 8]>::new();
        reserve_smallvec(
            &mut dispatches,
            resident_batches.len(),
            "dynamic resident dispatch",
        )?;
        for handles in resident_batches {
            let batch_bindings = crate::backend::resident_bindings_from_handles(handles)?;
            let prepared =
                self.backend
                    .prepare_resident_dispatch(&self.program, &batch_bindings, config)?;
            dispatches.push(self.backend.dispatch_resident_async_concrete_with_ptx_key(
                &self.program,
                &batch_bindings,
                config,
                &self.ptx_src,
                self.module_key,
                false,
                None,
                true,
                &prepared,
            )?);
        }

        resize_vec_slots(outputs, dispatches.len(), "dynamic resident output")?;
        for (dispatch, item_outputs) in dispatches.into_iter().zip(outputs.iter_mut()) {
            let output_handles =
                dispatch.resident_output_handles("dynamic persistent batch readback")?;
            let output_readbacks = dispatch.output_readbacks;
            dispatch.pending.await_timed_result()?;
            self.backend.download_resident_readbacks_many_into(
                &output_handles,
                &output_readbacks,
                item_outputs,
            )?;
        }
        Ok(())
    }

    fn dispatch_borrowed_batched_via_cuda_graph_lanes(
        &self,
        batches: &[&[&[u8]]],
        config: &DispatchConfig,
        outputs: &mut Vec<OutputBuffers>,
    ) -> Result<(), BackendError> {
        let miss_entries = self.materialized_output_batch_cache_partition_into(batches, outputs)?;
        if miss_entries.is_empty() {
            return Ok(());
        }

        let mut miss_batches = SmallVec::<[&[&[u8]]; MAX_GRAPH_CACHE_ENTRIES_PER_PIPELINE]>::new();
        reserve_smallvec(
            &mut miss_batches,
            miss_entries.len(),
            "cuda graph miss batch",
        )?;
        for miss in miss_entries.iter().copied() {
            miss_batches.push(compiled_graph_batch_inputs(
                batches,
                miss.batch_index,
                "miss partition",
            )?);
        }
        let lane_count =
            cuda_graph_lane_count_for_batch(&self.backend.caps, &self.prepared, &miss_batches)?;
        let first_miss =
            miss_entries
                .first()
                .copied()
                .ok_or_else(|| BackendError::InvalidProgram {
                    fix: "Fix: compiled CUDA graph replay produced zero miss entry after a non-empty miss partition. Rebuild the materialized batch partition before lane replay."
                        .to_string(),
                })?;
        let first_miss_batch =
            miss_batches
                .first()
                .copied()
                .ok_or_else(|| BackendError::InvalidProgram {
                    fix: "Fix: compiled CUDA graph replay produced zero miss batch input sets after a non-empty miss partition. Rebuild the materialized batch partition before lane replay."
                        .to_string(),
                })?;
        let mut lanes = SmallVec::<[CachedCudaGraph; MAX_GRAPH_CACHE_ENTRIES_PER_PIPELINE]>::new();
        reserve_smallvec(&mut lanes, lane_count, "cuda graph lane")?;
        for _ in 0..lane_count {
            lanes.push(
                match self.take_cached_graph_with_key(first_miss_batch, &first_miss.input_key)? {
                    Some(cached) => cached,
                    None => self.backend.record_cuda_graph_borrowed(
                        &self.program,
                        first_miss_batch,
                        config,
                    )?,
                },
            );
        }

        for (chunk_index, chunk) in miss_entries.chunks(lane_count).enumerate() {
            let mut launched = SmallVec::<
                [LaunchedMaterializedBatch; MAX_GRAPH_CACHE_ENTRIES_PER_PIPELINE],
            >::new();
            let chunk_start = match chunk_index.checked_mul(lane_count).ok_or_else(|| {
                BackendError::InvalidProgram {
                    fix: format!(
                        "Fix: CUDA graph replay chunk {chunk_index} with {lane_count} lane(s) overflowed miss-entry indexing; split the replay batch."
                    ),
                }
            }) {
                Ok(chunk_start) => chunk_start,
                Err(error) => return self.return_cached_graph_lanes_after_error(lanes, error),
            };
            for lane in 0..chunk.len() {
                let miss_entry_index =
                    match chunk_start.checked_add(lane).ok_or_else(|| {
                        BackendError::InvalidProgram {
                            fix: format!(
                                "Fix: CUDA graph replay chunk {chunk_index} lane {lane} overflowed miss-entry indexing; split the replay batch."
                            ),
                        }
                    }) {
                        Ok(miss_entry_index) => miss_entry_index,
                        Err(error) => {
                            return self.finish_and_return_cuda_graph_lanes_after_error(
                                lanes, &launched, outputs, error,
                            );
                        }
                    };
                let miss = match miss_entries.get(miss_entry_index).copied().ok_or_else(|| {
                    BackendError::InvalidProgram {
                        fix: format!(
                            "Fix: CUDA graph replay chunk {chunk_index} lane {lane} resolved outside the miss-entry table; rebuild the compiled pipeline."
                        ),
                    }
                }) {
                    Ok(miss) => miss,
                    Err(error) => {
                        return self.finish_and_return_cuda_graph_lanes_after_error(
                            lanes, &launched, outputs, error,
                        );
                    }
                };
                let batch_index = miss.batch_index;
                let inputs = match compiled_graph_batch_inputs(batches, batch_index, "lane replay")
                {
                    Ok(inputs) => inputs,
                    Err(error) => {
                        return self.finish_and_return_cuda_graph_lanes_after_error(
                            lanes, &launched, outputs, error,
                        );
                    }
                };
                let lane_graph = match compiled_graph_lane(&lanes, lane, "prepare replay input") {
                    Ok(lane_graph) => lane_graph,
                    Err(error) => {
                        return self.finish_and_return_cuda_graph_lanes_after_error(
                            lanes, &launched, outputs, error,
                        );
                    }
                };
                let input_state = match self.backend.prepare_cuda_graph_replay_input_state_with_key(
                    lane_graph,
                    inputs,
                    miss.input_key,
                ) {
                    Ok(input_state) => input_state,
                    Err(error) => {
                        return self.finish_and_return_cuda_graph_lanes_after_error(
                            lanes, &launched, outputs, error,
                        );
                    }
                };
                let lane_graph =
                    match compiled_graph_lane_mut(&mut lanes, lane, "materialized cache probe") {
                        Ok(lane_graph) => lane_graph,
                        Err(error) => {
                            return self.finish_and_return_cuda_graph_lanes_after_error(
                                lanes, &launched, outputs, error,
                            );
                        }
                    };
                let output_slot = match compiled_graph_output_mut(
                    outputs,
                    batch_index,
                    "materialized cache probe",
                ) {
                    Ok(output_slot) => output_slot,
                    Err(error) => {
                        return self.finish_and_return_cuda_graph_lanes_after_error(
                            lanes, &launched, outputs, error,
                        );
                    }
                };
                match self
                    .backend
                    .try_cuda_graph_materialized_cache_with_input_state_into(
                        lane_graph,
                        inputs,
                        &input_state,
                        output_slot,
                    ) {
                    Ok(true) => {
                        let output_slot = match compiled_graph_output(
                            outputs,
                            batch_index,
                            "materialized cache remember",
                        ) {
                            Ok(output_slot) => output_slot,
                            Err(error) => {
                                return self.finish_and_return_cuda_graph_lanes_after_error(
                                    lanes, &launched, outputs, error,
                                );
                            }
                        };
                        if let Err(error) = self.remember_materialized_output_cache_with_key(
                            inputs,
                            miss.input_key,
                            output_slot,
                        ) {
                            return self.finish_and_return_cuda_graph_lanes_after_error(
                                lanes, &launched, outputs, error,
                            );
                        }
                        continue;
                    }
                    Ok(false) => {}
                    Err(error) => {
                        return self.finish_and_return_cuda_graph_lanes_after_error(
                            lanes, &launched, outputs, error,
                        );
                    }
                }
                let lane_graph =
                    match compiled_graph_lane_mut(&mut lanes, lane, "enqueue lane replay") {
                        Ok(lane_graph) => lane_graph,
                        Err(error) => {
                            return self.finish_and_return_cuda_graph_lanes_after_error(
                                lanes, &launched, outputs, error,
                            );
                        }
                    };
                match self.backend.enqueue_cuda_graph_replay_with_input_state(
                    lane_graph,
                    inputs,
                    &input_state,
                ) {
                    Ok(replay_stats) => launched.push(LaunchedMaterializedBatch {
                        lane,
                        batch_index,
                        input_key: miss.input_key,
                        replay_stats,
                    }),
                    Err(error) => {
                        return self.finish_and_return_cuda_graph_lanes_after_error(
                            lanes, &launched, outputs, error,
                        );
                    }
                }
            }
            if !launched.is_empty() {
                match CUDA_NUMERIC.usize_to_u64(launched.len(), "cuda graph replay lane count") {
                    Ok(lanes) => self.backend.record_cuda_graph_batched_replay_chunk(lanes),
                    Err(error) => {
                        return self.finish_and_return_cuda_graph_lanes_after_error(
                            lanes, &launched, outputs, error,
                        );
                    }
                }
            }
            if let Err(error) =
                self.finish_cuda_graph_indexed_lane_replays(&mut lanes, &launched, outputs)
            {
                std::mem::forget(lanes);
                return Err(error);
            }
            for launched_batch in launched.iter().copied() {
                let inputs = match compiled_graph_batch_inputs(
                    batches,
                    launched_batch.batch_index,
                    "materialized replay remember",
                ) {
                    Ok(inputs) => inputs,
                    Err(error) => {
                        return self.return_cached_graph_lanes_after_error(lanes, error);
                    }
                };
                let output_slot = match compiled_graph_output(
                    outputs,
                    launched_batch.batch_index,
                    "materialized replay remember",
                ) {
                    Ok(output_slot) => output_slot,
                    Err(error) => {
                        return self.return_cached_graph_lanes_after_error(lanes, error);
                    }
                };
                if let Err(error) = self.remember_materialized_output_cache_with_key(
                    inputs,
                    launched_batch.input_key,
                    output_slot,
                ) {
                    return self.return_cached_graph_lanes_after_error(lanes, error);
                }
            }
        }

        self.return_cached_graph_lanes(lanes)
    }

    fn materialized_output_batch_cache_partition_into(
        &self,
        batches: &[&[&[u8]]],
        outputs: &mut Vec<OutputBuffers>,
    ) -> Result<SmallVec<[MaterializedBatchMiss; MAX_GRAPH_CACHE_ENTRIES_PER_PIPELINE]>, BackendError>
    {
        let mut input_keys = SmallVec::<
            [(usize, MaterializedInputKey); MAX_GRAPH_CACHE_ENTRIES_PER_PIPELINE],
        >::new();
        reserve_smallvec(
            &mut input_keys,
            batches.len(),
            "cuda graph materialized batch input key",
        )?;
        for (batch_index, inputs) in batches.iter().enumerate() {
            input_keys.push((batch_index, materialized_input_key(inputs)?));
        }
        let mut miss_entries =
            SmallVec::<[MaterializedBatchMiss; MAX_GRAPH_CACHE_ENTRIES_PER_PIPELINE]>::new();
        reserve_smallvec(
            &mut miss_entries,
            batches.len(),
            "cuda graph materialized batch miss index",
        )?;
        let mut hit_snapshots = SmallVec::<[_; MAX_GRAPH_CACHE_ENTRIES_PER_PIPELINE]>::new();
        reserve_smallvec(
            &mut hit_snapshots,
            batches.len(),
            "cuda graph materialized batch hit snapshot",
        )?;
        {
            let cache = self.lock_materialized_output_cache("during batch cache replay")?;
            for (batch_index, input_key) in input_keys.iter().copied() {
                let inputs =
                    compiled_graph_batch_inputs(batches, batch_index, "batch cache lookup")?;
                if let Some(snapshot) = cache.snapshot_with_key(inputs, &input_key) {
                    hit_snapshots.push((batch_index, snapshot));
                } else {
                    miss_entries.push(MaterializedBatchMiss {
                        batch_index,
                        input_key,
                    });
                }
            }
        }
        resize_vec_slots(
            outputs,
            batches.len(),
            "cuda graph materialized batch output",
        )?;
        for (batch_index, snapshot) in hit_snapshots {
            snapshot.copy_into(compiled_graph_output_mut(
                outputs,
                batch_index,
                "batch cache hit copy",
            )?)?;
            self.backend
                .telemetry
                .record_cuda_graph_materialized_cache_hit();
        }
        Ok(miss_entries)
    }

    fn materialized_output_cache_hit_with_key_into(
        &self,
        inputs: &[&[u8]],
        input_key: &MaterializedInputKey,
        outputs: &mut OutputBuffers,
    ) -> Result<bool, BackendError> {
        let snapshot = {
            let cache = self.lock_materialized_output_cache("during single-dispatch replay")?;
            cache.snapshot_with_key(inputs, input_key)
        };
        let Some(snapshot) = snapshot else {
            return Ok(false);
        };
        snapshot.copy_into(outputs)?;
        self.backend
            .telemetry
            .record_cuda_graph_materialized_cache_hit();
        Ok(true)
    }

    fn remember_materialized_output_cache_with_key(
        &self,
        inputs: &[&[u8]],
        input_key: MaterializedInputKey,
        outputs: &OutputBuffers,
    ) -> Result<(), BackendError> {
        let Some(entry) = MaterializedPipelineOutputCacheEntry::new_with_key_if_cacheable(
            inputs, &input_key, outputs,
        )?
        else {
            return Ok(());
        };
        let mut cache = self.lock_materialized_output_cache("while storing keyed replay output")?;
        cache.remember_entry(entry)
    }

    fn lock_materialized_output_cache(
        &self,
        action: &'static str,
    ) -> Result<MutexGuard<'_, MaterializedPipelineOutputCache>, BackendError> {
        self.materialized_output_cache.lock().map_err(|_| {
            BackendError::DispatchFailed {
                code: None,
                message: format!(
                    "CUDA compiled-pipeline materialized output cache lock poisoned {action}. Fix: rebuild the compiled pipeline after a panic during materialized cache access."
                ),
            }
        })
    }

    fn finish_cuda_graph_indexed_lane_replays(
        &self,
        lanes: &mut [CachedCudaGraph],
        launched: &[LaunchedMaterializedBatch],
        outputs: &mut [OutputBuffers],
    ) -> Result<(), BackendError> {
        let mut finish_error = None;
        for launched_batch in launched.iter().copied() {
            let lane = match compiled_graph_lane_mut(
                lanes,
                launched_batch.lane,
                "finish indexed lane replay",
            ) {
                Ok(lane) => lane,
                Err(error) => {
                    if finish_error.is_none() {
                        finish_error = Some(error);
                    }
                    continue;
                }
            };
            let output = match compiled_graph_output_mut(
                outputs,
                launched_batch.batch_index,
                "finish indexed lane replay",
            ) {
                Ok(output) => output,
                Err(error) => {
                    if let Err(cleanup_error) = self
                        .finish_cuda_graph_lane_replay_discarding_outputs(
                            lane,
                            launched_batch.replay_stats,
                        )
                    {
                        if finish_error.is_none() {
                            finish_error = Some(cleanup_error);
                        }
                    }
                    if finish_error.is_none() {
                        finish_error = Some(error);
                    }
                    continue;
                }
            };
            if let Err(error) = self.backend.finish_cuda_graph_replay_into(
                lane,
                launched_batch.replay_stats,
                output,
            ) {
                if finish_error.is_none() {
                    finish_error = Some(error);
                }
            }
        }
        if let Some(error) = finish_error {
            return Err(error);
        }
        Ok(())
    }

    fn finish_cuda_graph_lane_replay_discarding_outputs(
        &self,
        lane: &mut CachedCudaGraph,
        replay_stats: CudaGraphReplayStats,
    ) -> Result<(), BackendError> {
        let mut discard_outputs = reserved_vec(
            lane.output_host_bufs.len(),
            "discarded cuda graph lane output",
        )?;
        self.backend
            .finish_cuda_graph_replay_into(lane, replay_stats, &mut discard_outputs)
    }

    fn finish_and_return_cuda_graph_lanes_after_error(
        &self,
        mut lanes: SmallVec<[CachedCudaGraph; MAX_GRAPH_CACHE_ENTRIES_PER_PIPELINE]>,
        launched: &[LaunchedMaterializedBatch],
        outputs: &mut [OutputBuffers],
        error: BackendError,
    ) -> Result<(), BackendError> {
        if let Err(finish_error) =
            self.finish_cuda_graph_indexed_lane_replays(&mut lanes, launched, outputs)
        {
            std::mem::forget(lanes);
            return Err(finish_error);
        }
        self.return_cached_graph_lanes_after_error(lanes, error)
    }

    fn return_cached_graph_lanes_after_error(
        &self,
        lanes: SmallVec<[CachedCudaGraph; MAX_GRAPH_CACHE_ENTRIES_PER_PIPELINE]>,
        error: BackendError,
    ) -> Result<(), BackendError> {
        self.return_cached_graph_lanes(lanes)?;
        Err(error)
    }

    fn take_cached_graph_with_key(
        &self,
        inputs: &[&[u8]],
        input_key: &MaterializedInputKey,
    ) -> Result<Option<CachedCudaGraph>, BackendError> {
        Ok(self
            .take_cached_graph_with_replay_state(inputs, input_key)?
            .map(|selection| selection.graph))
    }

    fn take_cached_graph_with_replay_state(
        &self,
        inputs: &[&[u8]],
        input_key: &MaterializedInputKey,
    ) -> Result<Option<CachedGraphReplaySelection>, BackendError> {
        let mut graphs = self.graph_cache.lock().map_err(|_| {
            BackendError::DispatchFailed {
                code: None,
                message: "CUDA compiled-pipeline graph cache lock poisoned. Fix: rebuild the compiled pipeline after a panic during graph replay.".to_string(),
            }
        })?;
        let mut first_shape_match = None;
        for (index, cached) in graphs.iter().enumerate() {
            if !cached.input_shape_matches(inputs) {
                continue;
            }
            let input_state = self
                .backend
                .prepare_cuda_graph_replay_input_state_with_key(cached, inputs, *input_key)?;
            if cached.materialized_output_cache_matches_with_input_state(inputs, &input_state)? {
                return Ok(Some(CachedGraphReplaySelection {
                    graph: graphs.swap_remove(index),
                    input_state,
                }));
            }
            if first_shape_match.is_none() {
                first_shape_match = Some((index, input_state));
            }
        }
        Ok(
            first_shape_match.map(|(index, input_state)| CachedGraphReplaySelection {
                graph: graphs.swap_remove(index),
                input_state,
            }),
        )
    }

    fn return_cached_graph(&self, cached: CachedCudaGraph) -> Result<(), BackendError> {
        let mut graphs = self.graph_cache.lock().map_err(|_| {
            BackendError::DispatchFailed {
                code: None,
                message: "CUDA compiled-pipeline graph cache lock poisoned while returning a graph. Fix: rebuild the compiled pipeline after a panic during graph replay.".to_string(),
            }
        })?;
        if graphs.len() < MAX_GRAPH_CACHE_ENTRIES_PER_PIPELINE {
            graphs.push(cached);
        }
        Ok(())
    }

    fn return_cached_graph_lanes(
        &self,
        lanes: SmallVec<[CachedCudaGraph; MAX_GRAPH_CACHE_ENTRIES_PER_PIPELINE]>,
    ) -> Result<(), BackendError> {
        let mut graphs = self.graph_cache.lock().map_err(|_| {
            BackendError::DispatchFailed {
                code: None,
                message: "CUDA compiled-pipeline graph cache lock poisoned while returning graph lanes. Fix: rebuild the compiled pipeline after a panic during batched graph replay.".to_string(),
            }
        })?;
        for lane in lanes {
            if graphs.len() >= MAX_GRAPH_CACHE_ENTRIES_PER_PIPELINE {
                break;
            }
            graphs.push(lane);
        }
        Ok(())
    }
}