ferrum-kernels 0.8.6

Unified compute kernels (CUDA/Metal/CPU) and model runner for Ferrum inference
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
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
//! CUDA providers for backend-neutral vNext operation contracts.

use std::collections::BTreeSet;
use std::sync::Arc;

use cudarc::driver::{CudaFunction, LaunchConfig, PushKernelArg};
use cudarc::nvrtc::Ptx;
use ferrum_interfaces::vnext::{
    causal_paged_attention_contract, constant_scale_contract, dense_geglu_tanh_contract,
    dense_linear_contract, dense_swiglu_contract, gated_delta_recurrent_attention_contract,
    gpt_oss_causal_paged_attention_contract, hybrid_vnorm_causal_paged_attention_contract,
    last_token_dense_linear_contract, last_token_masked_argmax_contract, logit_softcap_contract,
    residual_add_contract, rms_norm_contract, token_embedding_contract, AttributeId,
    BatchedOperationInvocation, CapabilityCatalog, CapabilityId, ContractVersion,
    DeviceBatchingForm, DeviceId, DeviceReusableExecutionTopologyFingerprint, DeviceRuntime,
    DynamicStorageAllocator, DynamicStorageProfile, DynamicStorageRequirement, DynamicStorageView,
    ElementType, EncodedDeviceOperation, EngineProviderDescriptor, OperationContract,
    OperationFailure, OperationInvocation, OperationProvider, OperationProviderDescriptor,
    OperationResourceEstimate, OperationResourceEstimateRequest, OperationResourceEstimator,
    OperationRuntimeRegistry, PreparedModelFamily, ProfilePhase, ProviderId,
    ProviderStorageBindingRequirement, ProviderWorkspaceRequirement, ProviderWorkspaceReusePolicy,
    ProviderWorkspaceScope, ProviderWorkspaceSizeFormula, QuantizationFormatId,
    ResolvedTensorLayout, ResolvedValueBinding, ResolvedValueRole, ReusableExecutionTopology,
    ReusableExecutionTopologyRequest, SemanticValue, VNextError, WeightFormatId,
    WeightMaterializerId, WeightMaterializerRegistry, WeightMaterializerSelection,
    CAUSAL_PAGED_ATTENTION_F16_CAPABILITY_ID, CONSTANT_SCALE_F16_CAPABILITY_ID,
    DENSE_GEGLU_TANH_F16_CAPABILITY_ID, DENSE_LINEAR_F16_CAPABILITY_ID,
    DENSE_SWIGLU_F16_CAPABILITY_ID, DEVICE_NATIVE_ADAPTIVE_ATTENTION_CAPABILITY_ID,
    DEVICE_REUSABLE_EXECUTION_CAPABILITY_ID, GATED_DELTA_RECURRENT_ATTENTION_F16_CAPABILITY_ID,
    GPT_OSS_CAUSAL_PAGED_ATTENTION_F16_CAPABILITY_ID,
    HYBRID_VNORM_CAUSAL_PAGED_ATTENTION_F16_CAPABILITY_ID, IDENTITY_WEIGHT_MATERIALIZER_ID,
    LAST_TOKEN_DENSE_LINEAR_F16_CAPABILITY_ID, LAST_TOKEN_DENSE_LINEAR_OPERATION_ID,
    LAST_TOKEN_MASKED_ARGMAX_F16_CAPABILITY_ID, LAST_TOKEN_MASKED_ARGMAX_OPERATION_ID,
    LOGIT_SOFTCAP_F16_CAPABILITY_ID, RESIDUAL_ADD_F16_CAPABILITY_ID, RMS_NORM_F16_CAPABILITY_ID,
    TOKEN_EMBEDDING_F16_CAPABILITY_ID, TOKEN_EMBEDDING_OPERATION_ID,
};
#[cfg(feature = "vllm-moe-marlin")]
use ferrum_interfaces::vnext::{
    gpt_oss_routed_clamped_swiglu_moe_contract, routed_shared_swiglu_moe_contract,
    routed_swiglu_moe_contract, GPT_OSS_ROUTED_CLAMPED_SWIGLU_MOE_MXFP4_BF16_CAPABILITY_ID,
    ROUTED_SHARED_SWIGLU_MOE_F16_CAPABILITY_ID, ROUTED_SWIGLU_MOE_F16_CAPABILITY_ID,
};
use ferrum_types::{
    AttentionExecutionPolicy, NativeOperatorBackend, NativeOperatorProviderCatalog,
};
use sha2::{Digest, Sha256};

use super::vnext_replay::CudaCommandReplayKeyBuilder;
use super::vnext_runtime::{
    CudaBufferRegion, CudaDeviceBuffer, CudaDeviceCommand, CudaDeviceRuntime,
    CudaDeviceRuntimeConfig, CudaDeviceRuntimeError,
};

mod transformer;

const TOKEN_EMBEDDING_PROVIDER_ID: &str = "provider.cuda.token_embedding.f16";
const TOKEN_EMBEDDING_ESTIMATOR_ID: &str = "resource-estimator.cuda.token_embedding.f16";
const LAST_TOKEN_DENSE_LINEAR_PROVIDER_ID: &str =
    "provider.cuda.last_token_dense_linear.f16.cublas";
const LAST_TOKEN_DENSE_LINEAR_ESTIMATOR_ID: &str =
    "resource-estimator.cuda.last_token_dense_linear.f16.cublas";
const LAST_TOKEN_MASKED_ARGMAX_PROVIDER_ID: &str = "provider.cuda.last_token_masked_argmax.f16";
const LAST_TOKEN_MASKED_ARGMAX_ESTIMATOR_ID: &str =
    "resource-estimator.cuda.last_token_masked_argmax.f16";
const CUDA_ENGINE_PROVIDER_ID: &str = "provider.engine.cuda.vnext";
const DENSE_SAFETENSORS_FORMAT_ID: &str = "weight-format.safetensors.dense";
const BLOCK_FP8_SAFETENSORS_FORMAT_ID: &str =
    "weight-format.safetensors.fp8-e4m3-block-grid-inverse-scale";
const BLOCK_FP8_SOURCE_QUANTIZATION_FORMAT_ID: &str =
    "quantization.safetensors.fp8-e4m3-block-grid-inverse-scale";
const EMBEDDING_FUNCTION_NAME: &str = "vnext_embedding_lookup_f16";
const MASKED_ARGMAX_PRESERVING_LOGITS_FUNCTION_NAME: &str =
    "last_token_masked_argmax_preserving_logits_f16";
const VALUE_ALIGNMENT_BYTES: u64 = 16;
const THREADS_PER_BLOCK: u32 = 256;
const MAXIMUM_TOKENS_PER_LAUNCH: u64 = u16::MAX as u64;
pub(super) const VNEXT_KV_PAGE_BYTES: u64 = 64 * 1024;

/// Typed CUDA runtime input for the currently installed vNext provider bundle.
pub fn cuda_vnext_runtime_config(
    ordinal: usize,
    device_id: DeviceId,
    requested_attention_policy: AttentionExecutionPolicy,
) -> Result<CudaDeviceRuntimeConfig, VNextError> {
    let fingerprint_parts: Vec<&[u8]> = vec![
        include_str!("vnext_runtime.rs").as_bytes(),
        include_str!("vnext_replay.rs").as_bytes(),
        include_str!("vnext_ops.rs").as_bytes(),
        include_str!("vnext_ops/transformer.rs").as_bytes(),
        include_str!("vnext_ops/transformer/attention.rs").as_bytes(),
        include_str!("vnext_ops/transformer/causal_attention.rs").as_bytes(),
        include_str!("vnext_ops/transformer/gpt_oss_attention.rs").as_bytes(),
        crate::ptx::EMBEDDING_LOOKUP.as_bytes(),
        crate::ptx::ARGMAX_ROWS.as_bytes(),
        crate::ptx::RMS_NORM.as_bytes(),
        crate::ptx::FUSED_SILU_MUL.as_bytes(),
        crate::ptx::RESIDUAL_ADD.as_bytes(),
        crate::ptx::SANDWICH_NORM.as_bytes(),
        crate::ptx::LINEAR_ATTENTION.as_bytes(),
        crate::ptx::GATED_DELTA_RULE.as_bytes(),
        crate::ptx::VNEXT_CAUSAL_ATTENTION.as_bytes(),
        crate::ptx::GPT_OSS_ATTENTION.as_bytes(),
    ];
    #[cfg(feature = "vllm-moe-marlin")]
    let fingerprint_parts = {
        let mut fingerprint_parts = fingerprint_parts;
        fingerprint_parts.extend([
            include_str!("vnext_ops/transformer/moe.rs").as_bytes(),
            include_str!("vnext_ops/transformer/moe_launch.rs").as_bytes(),
            include_str!("vnext_ops/transformer/moe_routed.rs").as_bytes(),
            include_str!("vnext_ops/transformer/moe_weights.rs").as_bytes(),
            include_str!("vnext_ops/transformer/moe_workspace.rs").as_bytes(),
            include_str!("vnext_ops/transformer/gpt_oss_moe.rs").as_bytes(),
            crate::ptx::MOE_ROUTER.as_bytes(),
            crate::ptx::MOE_ALIGN_BLOCK_SIZE_PAIR_IDS.as_bytes(),
            crate::ptx::MOE_COMBINE.as_bytes(),
            crate::ptx::GPT_OSS_MOE.as_bytes(),
        ]);
        fingerprint_parts
    };
    #[cfg(feature = "vllm-marlin")]
    let fingerprint_parts = {
        let mut fingerprint_parts = fingerprint_parts;
        fingerprint_parts.extend([
            include_str!("../../marlin_fp8_materializer.rs").as_bytes(),
            include_str!("../../mxfp4_marlin_materializer.rs").as_bytes(),
            crate::ptx::MXFP4_MARLIN_PREPARE.as_bytes(),
        ]);
        fingerprint_parts
    };
    let capabilities = cuda_vnext_capabilities()?;
    let attention_execution_policy = requested_attention_policy
        .resolve(capabilities.iter().any(|capability| {
            capability.as_str() == DEVICE_NATIVE_ADAPTIVE_ATTENTION_CAPABILITY_ID
        }))
        .map_err(|reason| VNextError::InvalidExecutionPlan { reason })?;
    Ok(CudaDeviceRuntimeConfig {
        ordinal,
        device_id,
        attention_execution_policy,
        runtime_implementation_fingerprint: implementation_fingerprint(&fingerprint_parts),
        capabilities,
        dynamic_storage_profiles: BTreeSet::from([
            DynamicStorageProfile::new(
                DynamicStorageAllocator::LinearArena,
                DynamicStorageView::Contiguous,
            )?,
            DynamicStorageProfile::new(
                DynamicStorageAllocator::FixedBlockArena {
                    block_bytes: VNEXT_KV_PAGE_BYTES,
                },
                DynamicStorageView::PagedRegions {
                    block_bytes: VNEXT_KV_PAGE_BYTES,
                },
            )?,
        ]),
    })
}

pub fn cuda_vnext_capabilities() -> Result<BTreeSet<CapabilityId>, VNextError> {
    let capabilities = [
        TOKEN_EMBEDDING_F16_CAPABILITY_ID,
        LAST_TOKEN_DENSE_LINEAR_F16_CAPABILITY_ID,
        LAST_TOKEN_MASKED_ARGMAX_F16_CAPABILITY_ID,
        RMS_NORM_F16_CAPABILITY_ID,
        DENSE_LINEAR_F16_CAPABILITY_ID,
        DENSE_SWIGLU_F16_CAPABILITY_ID,
        DENSE_GEGLU_TANH_F16_CAPABILITY_ID,
        CONSTANT_SCALE_F16_CAPABILITY_ID,
        LOGIT_SOFTCAP_F16_CAPABILITY_ID,
        RESIDUAL_ADD_F16_CAPABILITY_ID,
        GATED_DELTA_RECURRENT_ATTENTION_F16_CAPABILITY_ID,
        CAUSAL_PAGED_ATTENTION_F16_CAPABILITY_ID,
        HYBRID_VNORM_CAUSAL_PAGED_ATTENTION_F16_CAPABILITY_ID,
        GPT_OSS_CAUSAL_PAGED_ATTENTION_F16_CAPABILITY_ID,
        DEVICE_REUSABLE_EXECUTION_CAPABILITY_ID,
    ]
    .into_iter()
    .map(CapabilityId::new)
    .collect::<Result<BTreeSet<_>, _>>()?;
    #[cfg(feature = "vllm-moe-marlin")]
    let capabilities = {
        let mut capabilities = capabilities;
        capabilities.insert(CapabilityId::new(
            ROUTED_SHARED_SWIGLU_MOE_F16_CAPABILITY_ID,
        )?);
        capabilities.insert(CapabilityId::new(ROUTED_SWIGLU_MOE_F16_CAPABILITY_ID)?);
        capabilities.insert(CapabilityId::new(
            GPT_OSS_ROUTED_CLAMPED_SWIGLU_MOE_MXFP4_BF16_CAPABILITY_ID,
        )?);
        capabilities
    };
    #[cfg(feature = "vllm-marlin")]
    let capabilities = {
        let mut capabilities = capabilities;
        capabilities.insert(CapabilityId::new(
            crate::marlin_fp8_materializer::MARLIN_FP8_CAPABILITY_ID,
        )?);
        capabilities.insert(CapabilityId::new(transformer::GPTQ_MARLIN_CAPABILITY_ID)?);
        capabilities.insert(CapabilityId::new(
            transformer::COMPRESSED_TENSORS_MARLIN_CAPABILITY_ID,
        )?);
        capabilities.insert(CapabilityId::new(
            transformer::COMPRESSED_TENSORS_MARLIN_SYMMETRIC_CAPABILITY_ID,
        )?);
        capabilities
    };
    #[cfg(feature = "vllm-paged-attn-v2")]
    let capabilities = {
        let mut capabilities = capabilities;
        capabilities.insert(CapabilityId::new(
            DEVICE_NATIVE_ADAPTIVE_ATTENTION_CAPABILITY_ID,
        )?);
        capabilities
    };
    Ok(capabilities)
}

fn cuda_weight_materializer_selection(
    family: &PreparedModelFamily,
) -> Result<WeightMaterializerSelection, VNextError> {
    let block_fp8_weight_format = WeightFormatId::new(BLOCK_FP8_SAFETENSORS_FORMAT_ID)?;
    let block_fp8_quantization =
        QuantizationFormatId::new(BLOCK_FP8_SOURCE_QUANTIZATION_FORMAT_ID)?;
    let quantization_formats = family.weight_schema().quantization_formats();
    let has_block_fp8_quantization = quantization_formats.contains(&block_fp8_quantization);
    let has_block_fp8_weight_format = family.weight_schema().format_id == block_fp8_weight_format;
    let mxfp4_weight_format = WeightFormatId::new(
        crate::mxfp4_marlin_materializer::GPT_OSS_MXFP4_SOURCE_WEIGHT_FORMAT_ID,
    )?;
    let mxfp4_quantization = QuantizationFormatId::new(
        crate::mxfp4_marlin_materializer::GPT_OSS_MXFP4_SOURCE_QUANTIZATION_FORMAT_ID,
    )?;
    let has_mxfp4_quantization = quantization_formats.contains(&mxfp4_quantization);
    let has_mxfp4_weight_format = family.weight_schema().format_id == mxfp4_weight_format;

    if has_block_fp8_weight_format != has_block_fp8_quantization
        || (has_block_fp8_weight_format
            && quantization_formats != BTreeSet::from([block_fp8_quantization]))
    {
        return Err(VNextError::InvalidExecutionPlan {
            reason: "CUDA block-FP8 source format and typed quantization schema disagree"
                .to_owned(),
        });
    }
    if has_mxfp4_weight_format != has_mxfp4_quantization
        || (has_mxfp4_weight_format && quantization_formats != BTreeSet::from([mxfp4_quantization]))
    {
        return Err(VNextError::InvalidExecutionPlan {
            reason: "CUDA GPT-OSS MXFP4 source format and typed quantization schema disagree"
                .to_owned(),
        });
    }
    if has_block_fp8_weight_format && has_mxfp4_weight_format {
        return Err(VNextError::InvalidExecutionPlan {
            reason: "CUDA source schema cannot require block-FP8 and GPT-OSS MXFP4 materializers together"
                .to_owned(),
        });
    }

    if !has_block_fp8_weight_format && !has_mxfp4_weight_format {
        return Ok(WeightMaterializerSelection::exact(
            WeightMaterializerId::new(IDENTITY_WEIGHT_MATERIALIZER_ID)?,
        ));
    }

    if has_mxfp4_weight_format {
        #[cfg(feature = "vllm-moe-marlin")]
        {
            return Ok(WeightMaterializerSelection::exact(
                WeightMaterializerId::new(
                    crate::mxfp4_marlin_materializer::GPT_OSS_MXFP4_TO_MARLIN_WEIGHT_MATERIALIZER_ID,
                )?,
            ));
        }
        #[cfg(not(feature = "vllm-moe-marlin"))]
        {
            return Err(VNextError::InvalidExecutionPlan {
                reason:
                    "CUDA GPT-OSS MXFP4 source requires the compiled vllm-moe-marlin materializer"
                        .to_owned(),
            });
        }
    }

    #[cfg(feature = "vllm-marlin")]
    {
        Ok(WeightMaterializerSelection::exact(
            WeightMaterializerId::new(
                crate::marlin_fp8_materializer::BLOCK_FP8_TO_MARLIN_FP8_WEIGHT_MATERIALIZER_ID,
            )?,
        ))
    }
    #[cfg(not(feature = "vllm-marlin"))]
    {
        Err(VNextError::InvalidExecutionPlan {
            reason: "CUDA block-FP8 source requires the compiled vllm-marlin materializer"
                .to_owned(),
        })
    }
}

#[cfg(all(test, feature = "vllm-marlin"))]
mod block_fp8_exact_materializer_tests {
    use super::*;

    #[test]
    fn block_fp8_selection_requires_the_live_exact_materializer() {
        let materializer =
            crate::marlin_fp8_materializer::block_fp8_to_marlin_fp8_weight_materializer()
                .expect("live block-FP8 materializer");
        assert_eq!(
            materializer.descriptor().fidelity(),
            ferrum_interfaces::vnext::WeightMaterializationFidelity::Exact
        );
        let selection = WeightMaterializerSelection::exact(
            WeightMaterializerId::new(
                crate::marlin_fp8_materializer::BLOCK_FP8_TO_MARLIN_FP8_WEIGHT_MATERIALIZER_ID,
            )
            .expect("block-FP8 materializer id"),
        );
        assert!(!selection.has_numeric_quality_artifact());
    }
}

/// Build the exact composition root used for both planning and dispatch.
pub fn cuda_vnext_operation_registry(
    runtime: &CudaDeviceRuntime,
) -> Result<OperationRuntimeRegistry<CudaDeviceRuntime>, CudaDeviceRuntimeError> {
    let contracts: Vec<Box<dyn OperationContract>> = vec![
        Box::new(token_embedding_contract().map_err(contract_error)?),
        Box::new(last_token_dense_linear_contract().map_err(contract_error)?),
        Box::new(last_token_masked_argmax_contract().map_err(contract_error)?),
        Box::new(rms_norm_contract().map_err(contract_error)?),
        Box::new(dense_linear_contract().map_err(contract_error)?),
        Box::new(dense_swiglu_contract().map_err(contract_error)?),
        Box::new(dense_geglu_tanh_contract().map_err(contract_error)?),
        Box::new(constant_scale_contract().map_err(contract_error)?),
        Box::new(logit_softcap_contract().map_err(contract_error)?),
        Box::new(residual_add_contract().map_err(contract_error)?),
        Box::new(gated_delta_recurrent_attention_contract().map_err(contract_error)?),
        Box::new(causal_paged_attention_contract().map_err(contract_error)?),
        Box::new(hybrid_vnorm_causal_paged_attention_contract().map_err(contract_error)?),
        Box::new(gpt_oss_causal_paged_attention_contract().map_err(contract_error)?),
    ];
    #[cfg(feature = "vllm-moe-marlin")]
    let contracts = {
        let mut contracts = contracts;
        contracts.push(Box::new(
            routed_shared_swiglu_moe_contract().map_err(contract_error)?,
        ));
        contracts.push(Box::new(
            routed_swiglu_moe_contract().map_err(contract_error)?,
        ));
        contracts.push(Box::new(
            gpt_oss_routed_clamped_swiglu_moe_contract().map_err(contract_error)?,
        ));
        contracts
    };
    let providers: Vec<Box<dyn OperationProvider<CudaDeviceRuntime>>> = vec![
        Box::new(CudaTokenEmbeddingProvider::new(runtime)?),
        Box::new(CudaLastTokenDenseLinearProvider::new(runtime)?),
        Box::new(CudaLastTokenMaskedArgmaxProvider::new(runtime)?),
        Box::new(transformer::CudaRmsNormProvider::new(runtime)?),
        Box::new(transformer::CudaDenseLinearProvider::new(runtime)?),
        Box::new(transformer::CudaDenseSwiGluProvider::new(runtime)?),
        Box::new(transformer::CudaDenseGeGluTanhProvider::new(runtime)?),
        Box::new(transformer::CudaConstantScaleProvider::new(runtime)?),
        Box::new(transformer::CudaLogitSoftcapProvider::new(runtime)?),
        Box::new(transformer::CudaResidualAddProvider::new(runtime)?),
        Box::new(transformer::CudaGatedDeltaRecurrentAttentionProvider::new(
            runtime,
        )?),
        Box::new(transformer::CudaCausalPagedAttentionProvider::new(
            runtime,
            runtime.attention_execution_policy(),
        )?),
        Box::new(transformer::CudaCausalPagedAttentionProvider::new_gemma4(
            runtime,
            runtime.attention_execution_policy(),
            &hybrid_vnorm_causal_paged_attention_contract().map_err(contract_error)?,
            HYBRID_VNORM_CAUSAL_PAGED_ATTENTION_F16_CAPABILITY_ID,
        )?),
        Box::new(transformer::CudaGptOssCausalPagedAttentionProvider::new(
            runtime,
        )?),
    ];
    #[cfg(feature = "vllm-marlin")]
    let providers = {
        let mut providers = providers;
        providers.push(Box::new(
            transformer::CudaMarlinFp8DenseLinearProvider::new(runtime)?,
        ));
        providers
    };
    #[cfg(feature = "vllm-moe-marlin")]
    let providers = {
        let mut providers = providers;
        providers.push(Box::new(
            transformer::CudaRoutedSharedSwiGluMoeProvider::new(runtime)?,
        ));
        providers.push(Box::new(
            transformer::CudaRoutedSharedSwiGluMoeProvider::new_marlin_fp8(runtime)?,
        ));
        providers.push(Box::new(transformer::CudaRoutedSwiGluMoeProvider::new(
            runtime,
        )?));
        providers.push(Box::new(
            transformer::CudaGptOssRoutedClampedSwiGluMoeProvider::new(runtime)?,
        ));
        providers
    };
    OperationRuntimeRegistry::new(contracts, providers).map_err(contract_error)
}

/// One CUDA composition root shared by planning, provisioning, and dispatch.
/// The capability catalog is derived from the retained registry objects rather
/// than reconstructed from a second descriptor list.
pub struct CudaVNextComposition {
    runtime: Arc<CudaDeviceRuntime>,
    registry: OperationRuntimeRegistry<CudaDeviceRuntime>,
    weight_materializers: WeightMaterializerRegistry,
    weight_materializer_selection: WeightMaterializerSelection,
    catalog: CapabilityCatalog,
}

impl CudaVNextComposition {
    fn prepare(
        ordinal: usize,
        device_id: DeviceId,
        requested_attention_policy: AttentionExecutionPolicy,
        family: Option<&PreparedModelFamily>,
    ) -> Result<Self, CudaDeviceRuntimeError> {
        let weight_materializer_selection = match family {
            Some(family) => cuda_weight_materializer_selection(family).map_err(contract_error)?,
            None => WeightMaterializerSelection::exact(
                WeightMaterializerId::new(IDENTITY_WEIGHT_MATERIALIZER_ID)
                    .map_err(contract_error)?,
            ),
        };
        let config = cuda_vnext_runtime_config(ordinal, device_id, requested_attention_policy)
            .map_err(contract_error)?;
        let runtime = Arc::new(CudaDeviceRuntime::new(config)?);
        let registry = cuda_vnext_operation_registry(&runtime)?;
        #[cfg(feature = "vllm-marlin")]
        let weight_materializers = vec![
            crate::marlin_fp8_materializer::marlin_fp8_weight_materializer()
                .map_err(contract_error)?,
            crate::marlin_fp8_materializer::block_fp8_to_marlin_fp8_weight_materializer()
                .map_err(contract_error)?,
        ];
        #[cfg(feature = "vllm-moe-marlin")]
        let weight_materializers = {
            let mut weight_materializers = weight_materializers;
            weight_materializers.push(
                crate::mxfp4_marlin_materializer::gpt_oss_mxfp4_to_marlin_weight_materializer()
                    .map_err(contract_error)?,
            );
            weight_materializers
        };
        #[cfg(feature = "vllm-marlin")]
        let weight_materializers =
            WeightMaterializerRegistry::new(weight_materializers).map_err(contract_error)?;
        #[cfg(not(feature = "vllm-marlin"))]
        let weight_materializers =
            WeightMaterializerRegistry::identity_only().map_err(contract_error)?;
        let engine = EngineProviderDescriptor::new(
            ProviderId::new(CUDA_ENGINE_PROVIDER_ID).map_err(contract_error)?,
            ContractVersion::new(1, 0),
            implementation_fingerprint(&[
                include_str!("vnext_ops.rs").as_bytes(),
                include_str!("vnext_runtime.rs").as_bytes(),
                CUDA_ENGINE_PROVIDER_ID.as_bytes(),
            ]),
            runtime.descriptor().id.clone(),
            runtime.descriptor().capabilities.clone(),
        )
        .map_err(contract_error)?;
        let catalog = registry
            .capability_catalog(runtime.descriptor().clone(), vec![engine])
            .map_err(contract_error)?;
        let catalog = weight_materializers
            .augment_catalog(catalog)
            .map_err(contract_error)?;
        Ok(Self {
            runtime,
            registry,
            weight_materializers,
            weight_materializer_selection,
            catalog,
        })
    }

    pub fn create(
        ordinal: usize,
        device_id: DeviceId,
        requested_attention_policy: AttentionExecutionPolicy,
        family: &PreparedModelFamily,
    ) -> Result<Self, CudaDeviceRuntimeError> {
        let composition =
            Self::prepare(ordinal, device_id, requested_attention_policy, Some(family))?;
        composition.validate_compiled_native_operators()?;
        Ok(composition)
    }

    fn validate_compiled_native_operators(&self) -> Result<(), CudaDeviceRuntimeError> {
        let catalog = &self.catalog;
        let native_provider_catalog = catalog
            .native_operator_provider_catalog(NativeOperatorBackend::Cuda)
            .map_err(contract_error)?;
        crate::native_ops::validate_compiled_native_operator_provider_catalog(
            &native_provider_catalog,
            crate::native_ops::compiled_native_operator_artifacts(),
        )
        .map_err(CudaDeviceRuntimeError::contract)
    }

    pub fn runtime(&self) -> &Arc<CudaDeviceRuntime> {
        &self.runtime
    }

    pub fn registry(&self) -> &OperationRuntimeRegistry<CudaDeviceRuntime> {
        &self.registry
    }

    pub fn catalog(&self) -> &CapabilityCatalog {
        &self.catalog
    }

    pub fn into_parts(
        self,
    ) -> (
        Arc<CudaDeviceRuntime>,
        OperationRuntimeRegistry<CudaDeviceRuntime>,
        WeightMaterializerRegistry,
        WeightMaterializerSelection,
        CapabilityCatalog,
    ) {
        (
            self.runtime,
            self.registry,
            self.weight_materializers,
            self.weight_materializer_selection,
            self.catalog,
        )
    }
}

/// Immutable catalog input used to rebuild native artifacts after provider
/// identity changes. It deliberately exposes no executable runtime.
pub struct CudaNativeOperatorCatalogInput {
    provider_catalog: NativeOperatorProviderCatalog,
    capability_catalog: CapabilityCatalog,
}

impl CudaNativeOperatorCatalogInput {
    pub fn provider_catalog(&self) -> &NativeOperatorProviderCatalog {
        &self.provider_catalog
    }

    pub fn capability_catalog(&self) -> &CapabilityCatalog {
        &self.capability_catalog
    }

    pub fn into_parts(self) -> (NativeOperatorProviderCatalog, CapabilityCatalog) {
        (self.provider_catalog, self.capability_catalog)
    }
}

fn cuda_native_operator_catalog_input_from_composition(
    composition: CudaVNextComposition,
) -> Result<CudaNativeOperatorCatalogInput, CudaDeviceRuntimeError> {
    let provider_catalog = composition
        .catalog
        .native_operator_provider_catalog(NativeOperatorBackend::Cuda)
        .map_err(contract_error)?;
    Ok(CudaNativeOperatorCatalogInput {
        provider_catalog,
        capability_catalog: composition.catalog,
    })
}

/// Capture the exact provider identities and validate the installed native
/// artifact set without exposing an executable family-less composition.
pub fn cuda_validated_native_operator_catalog_input(
    ordinal: usize,
    device_id: DeviceId,
    requested_attention_policy: AttentionExecutionPolicy,
) -> Result<CudaNativeOperatorCatalogInput, CudaDeviceRuntimeError> {
    let composition =
        CudaVNextComposition::prepare(ordinal, device_id, requested_attention_policy, None)?;
    composition.validate_compiled_native_operators()?;
    cuda_native_operator_catalog_input_from_composition(composition)
}

/// Capture the exact provider identities needed to package a new native
/// artifact set. Product composition uses [`CudaVNextComposition::create`]
/// with a typed model family and cannot bypass compiled-artifact validation.
pub fn cuda_native_operator_catalog_input(
    ordinal: usize,
    device_id: DeviceId,
    requested_attention_policy: AttentionExecutionPolicy,
) -> Result<CudaNativeOperatorCatalogInput, CudaDeviceRuntimeError> {
    let composition =
        CudaVNextComposition::prepare(ordinal, device_id, requested_attention_policy, None)?;
    cuda_native_operator_catalog_input_from_composition(composition)
}

pub struct CudaTokenEmbeddingProvider {
    descriptor: OperationProviderDescriptor,
    function: CudaFunction,
}

impl CudaTokenEmbeddingProvider {
    pub fn new(runtime: &CudaDeviceRuntime) -> Result<Self, CudaDeviceRuntimeError> {
        let contract = token_embedding_contract().map_err(contract_error)?;
        let capability =
            CapabilityId::new(TOKEN_EMBEDDING_F16_CAPABILITY_ID).map_err(contract_error)?;
        if !runtime.descriptor().capabilities.contains(&capability) {
            return Err(CudaDeviceRuntimeError::contract(
                "CUDA runtime does not advertise the token embedding capability",
            ));
        }

        let provider_fingerprint = implementation_fingerprint(&[
            include_str!("vnext_ops.rs").as_bytes(),
            crate::ptx::EMBEDDING_LOOKUP.as_bytes(),
            EMBEDDING_FUNCTION_NAME.as_bytes(),
        ]);
        let estimator_fingerprint = implementation_fingerprint(&[
            include_str!("vnext_ops.rs").as_bytes(),
            TOKEN_EMBEDDING_ESTIMATOR_ID.as_bytes(),
        ]);
        let descriptor = OperationProviderDescriptor::new(
            ProviderId::new(TOKEN_EMBEDDING_PROVIDER_ID).map_err(contract_error)?,
            contract.descriptor().id.clone(),
            contract
                .descriptor()
                .fingerprint()
                .map_err(contract_error)?,
            provider_fingerprint,
            ferrum_interfaces::vnext::ProviderExecutionSemantics::bitwise_eager_and_replay(),
            contract.descriptor().version,
            runtime.descriptor().id.clone(),
            BTreeSet::from([capability]),
            BTreeSet::from([
                WeightFormatId::new(DENSE_SAFETENSORS_FORMAT_ID).map_err(contract_error)?
            ]),
            BTreeSet::new(),
            contiguous_bindings(),
            TOKEN_EMBEDDING_ESTIMATOR_ID,
            ContractVersion::new(1, 0),
            estimator_fingerprint,
        )
        .map_err(contract_error)?;
        let module = runtime
            .context()
            .load_module(Ptx::from_src(crate::ptx::EMBEDDING_LOOKUP.to_owned()))
            .map_err(|error| CudaDeviceRuntimeError::driver("embedding module load", error))?;
        let function = module
            .load_function(EMBEDDING_FUNCTION_NAME)
            .map_err(|error| CudaDeviceRuntimeError::driver("embedding function load", error))?;
        Ok(Self {
            descriptor,
            function,
        })
    }
}

impl OperationResourceEstimator for CudaTokenEmbeddingProvider {
    fn descriptor(&self) -> &OperationProviderDescriptor {
        &self.descriptor
    }

    fn estimate_resources(
        &self,
        request: OperationResourceEstimateRequest<'_>,
    ) -> Result<OperationResourceEstimate, VNextError> {
        if request.operation().id.as_str() != TOKEN_EMBEDDING_OPERATION_ID
            || request.operation().fingerprint()? != self.descriptor.operation_fingerprint()
        {
            return Err(VNextError::InvalidExecutionPlan {
                reason: "CUDA token embedding estimator received another operation".to_owned(),
            });
        }
        Ok(OperationResourceEstimate::new(
            self.descriptor.resource_estimator_id(),
            self.descriptor.resource_estimator_version(),
            self.descriptor
                .resource_estimator_implementation_fingerprint(),
            request.input_fingerprint(),
            VALUE_ALIGNMENT_BYTES,
            None,
            None,
        ))
    }
}

impl OperationProvider<CudaDeviceRuntime> for CudaTokenEmbeddingProvider {
    fn reusable_execution_topology(
        &self,
        request: ReusableExecutionTopologyRequest<'_>,
    ) -> Result<ReusableExecutionTopology, VNextError> {
        reusable_token_topology(
            &request,
            b"ferrum.cuda.token-embedding.reusable-topology.v2\0",
        )
    }

    fn encode_selected(
        &self,
        invocation: BatchedOperationInvocation<'_, CudaDeviceBuffer>,
    ) -> Result<EncodedDeviceOperation<CudaDeviceCommand>, OperationFailure> {
        let identity = invocation.participants()[0].identity().clone();
        encode_token_embedding(
            &self.function,
            self.descriptor.provider_implementation_fingerprint(),
            invocation,
        )
        .map(EncodedDeviceOperation::compute)
        .map_err(|message| provider_failure(identity, "cuda.token_embedding.encode", message))
    }
}

pub struct CudaLastTokenDenseLinearProvider {
    descriptor: OperationProviderDescriptor,
}

impl CudaLastTokenDenseLinearProvider {
    pub fn new(runtime: &CudaDeviceRuntime) -> Result<Self, CudaDeviceRuntimeError> {
        let contract = last_token_dense_linear_contract().map_err(contract_error)?;
        let descriptor = transformer::provider_descriptor(
            runtime,
            &contract,
            LAST_TOKEN_DENSE_LINEAR_PROVIDER_ID,
            LAST_TOKEN_DENSE_LINEAR_F16_CAPABILITY_ID,
            LAST_TOKEN_DENSE_LINEAR_ESTIMATOR_ID,
            transformer::contiguous_bindings(2),
            implementation_fingerprint(&[
                include_str!("vnext_ops.rs").as_bytes(),
                LAST_TOKEN_DENSE_LINEAR_PROVIDER_ID.as_bytes(),
            ]),
        )?;
        Ok(Self { descriptor })
    }
}

impl OperationResourceEstimator for CudaLastTokenDenseLinearProvider {
    fn descriptor(&self) -> &OperationProviderDescriptor {
        &self.descriptor
    }

    fn estimate_resources(
        &self,
        request: OperationResourceEstimateRequest<'_>,
    ) -> Result<OperationResourceEstimate, VNextError> {
        transformer::ensure_estimator_request(
            &self.descriptor,
            &request,
            LAST_TOKEN_DENSE_LINEAR_OPERATION_ID,
        )?;
        Ok(transformer::estimate(
            &self.descriptor,
            request.input_fingerprint(),
            None,
        ))
    }
}

impl OperationProvider<CudaDeviceRuntime> for CudaLastTokenDenseLinearProvider {
    fn reusable_execution_topology(
        &self,
        request: ReusableExecutionTopologyRequest<'_>,
    ) -> Result<ReusableExecutionTopology, VNextError> {
        reusable_token_topology(
            &request,
            b"ferrum.cuda.last-token-linear.reusable-topology.v2\0",
        )
    }

    fn encode_selected(
        &self,
        invocation: BatchedOperationInvocation<'_, CudaDeviceBuffer>,
    ) -> Result<EncodedDeviceOperation<CudaDeviceCommand>, OperationFailure> {
        let identity = invocation.participants()[0].identity().clone();
        encode_last_token_dense_linear(
            self.descriptor.provider_implementation_fingerprint(),
            invocation,
        )
        .map(EncodedDeviceOperation::compute)
        .map_err(|message| {
            provider_failure(identity, "cuda.last_token_dense_linear.encode", message)
        })
    }
}

pub struct CudaLastTokenMaskedArgmaxProvider {
    descriptor: OperationProviderDescriptor,
    function: CudaFunction,
}

impl CudaLastTokenMaskedArgmaxProvider {
    pub fn new(runtime: &CudaDeviceRuntime) -> Result<Self, CudaDeviceRuntimeError> {
        let contract = last_token_masked_argmax_contract().map_err(contract_error)?;
        let descriptor = transformer::provider_descriptor(
            runtime,
            &contract,
            LAST_TOKEN_MASKED_ARGMAX_PROVIDER_ID,
            LAST_TOKEN_MASKED_ARGMAX_F16_CAPABILITY_ID,
            LAST_TOKEN_MASKED_ARGMAX_ESTIMATOR_ID,
            transformer::contiguous_bindings(5),
            implementation_fingerprint(&[
                include_str!("vnext_ops.rs").as_bytes(),
                crate::ptx::ARGMAX_ROWS.as_bytes(),
                MASKED_ARGMAX_PRESERVING_LOGITS_FUNCTION_NAME.as_bytes(),
            ]),
        )?;
        let module = runtime
            .context()
            .load_module(Ptx::from_src(crate::ptx::ARGMAX_ROWS.to_owned()))
            .map_err(|error| CudaDeviceRuntimeError::driver("masked argmax module load", error))?;
        let function = module
            .load_function(MASKED_ARGMAX_PRESERVING_LOGITS_FUNCTION_NAME)
            .map_err(|error| {
                CudaDeviceRuntimeError::driver(
                    "masked argmax preserving-logits function load",
                    error,
                )
            })?;
        Ok(Self {
            descriptor,
            function,
        })
    }
}

impl OperationResourceEstimator for CudaLastTokenMaskedArgmaxProvider {
    fn descriptor(&self) -> &OperationProviderDescriptor {
        &self.descriptor
    }

    fn estimate_resources(
        &self,
        request: OperationResourceEstimateRequest<'_>,
    ) -> Result<OperationResourceEstimate, VNextError> {
        transformer::ensure_estimator_request(
            &self.descriptor,
            &request,
            LAST_TOKEN_MASKED_ARGMAX_OPERATION_ID,
        )?;
        let vocabulary_size = unsigned_attribute(request.attributes(), "vocab_size")
            .map_err(|reason| VNextError::InvalidExecutionPlan { reason })?;
        let scratch_bytes = masked_argmax_scratch_stride(vocabulary_size)
            .map_err(|reason| VNextError::InvalidExecutionPlan { reason })?;
        let scratch = ProviderWorkspaceRequirement::from_formula(
            ProviderWorkspaceSizeFormula::actual_sequences(scratch_bytes)?,
            VALUE_ALIGNMENT_BYTES,
            ProviderWorkspaceScope::Invocation,
            ProviderWorkspaceReusePolicy::OverwriteBeforeRead,
            DynamicStorageRequirement::contiguous(),
        )?;
        Ok(transformer::estimate(
            &self.descriptor,
            request.input_fingerprint(),
            Some(scratch),
        ))
    }
}

impl OperationProvider<CudaDeviceRuntime> for CudaLastTokenMaskedArgmaxProvider {
    fn reusable_execution_topology(
        &self,
        request: ReusableExecutionTopologyRequest<'_>,
    ) -> Result<ReusableExecutionTopology, VNextError> {
        transformer::static_contiguous_reusable_topology(
            &request,
            5,
            &[transformer::CapturedProviderWorkspace::Scratch],
        )
    }

    fn encode_selected(
        &self,
        invocation: BatchedOperationInvocation<'_, CudaDeviceBuffer>,
    ) -> Result<EncodedDeviceOperation<CudaDeviceCommand>, OperationFailure> {
        let identity = invocation.participants()[0].identity().clone();
        encode_last_token_masked_argmax(
            &self.function,
            self.descriptor.provider_implementation_fingerprint(),
            invocation,
        )
        .map(EncodedDeviceOperation::compute)
        .map_err(|message| {
            provider_failure(identity, "cuda.last_token_masked_argmax.encode", message)
        })
    }
}

#[derive(Debug, Clone, Copy)]
struct MaskedArgmaxLaunch {
    first_region: usize,
    scratch_offset_bytes: u64,
    vocabulary_size: i32,
    repetition_capacity: i32,
}

fn encode_last_token_masked_argmax(
    function: &CudaFunction,
    provider_fingerprint: &str,
    invocation: BatchedOperationInvocation<'_, CudaDeviceBuffer>,
) -> Result<CudaDeviceCommand, String> {
    if invocation.operation().id.as_str() != LAST_TOKEN_MASKED_ARGMAX_OPERATION_ID
        || invocation.participants().is_empty()
    {
        return Err("CUDA masked argmax received another or empty operation".to_owned());
    }

    let first_vocabulary_size =
        unsigned_attribute(invocation.participants()[0].attributes(), "vocab_size")?;
    let scratch_stride = masked_argmax_scratch_stride(first_vocabulary_size)?;
    let required_scratch_bytes = scratch_stride
        .checked_mul(invocation.participants().len() as u64)
        .ok_or_else(|| "CUDA masked argmax scratch size overflows".to_owned())?;
    let mut regions = Vec::with_capacity(invocation.participants().len() * 6 + 1);
    let mut launches = Vec::with_capacity(invocation.participants().len());
    for (participant_index, participant) in invocation.participants().iter().enumerate() {
        let logits = binding(participant.bindings(), ResolvedValueRole::Input, 0)?;
        let valid_mask = binding(participant.bindings(), ResolvedValueRole::Input, 1)?;
        let repetition_token_ids = binding(participant.bindings(), ResolvedValueRole::Input, 2)?;
        let repetition_offsets = binding(participant.bindings(), ResolvedValueRole::Input, 3)?;
        let repetition_penalty = binding(participant.bindings(), ResolvedValueRole::Input, 4)?;
        let output = binding(participant.bindings(), ResolvedValueRole::Output, 0)?;
        let vocabulary_size = unsigned_attribute(participant.attributes(), "vocab_size")?;
        if vocabulary_size != first_vocabulary_size {
            return Err("CUDA masked argmax participants disagree on vocabulary size".to_owned());
        }
        let repetition_capacity = validate_masked_argmax_signature(
            logits,
            valid_mask,
            repetition_token_ids,
            repetition_offsets,
            repetition_penalty,
            output,
            vocabulary_size,
        )?;

        let first_region = regions.len();
        regions.push(contiguous_region(participant, logits, ElementType::F16)?);
        regions.push(contiguous_region(participant, valid_mask, ElementType::U8)?);
        regions.push(contiguous_region(
            participant,
            repetition_token_ids,
            ElementType::U32,
        )?);
        regions.push(contiguous_region(
            participant,
            repetition_offsets,
            ElementType::U32,
        )?);
        regions.push(contiguous_region(
            participant,
            repetition_penalty,
            ElementType::F32,
        )?);
        regions.push(contiguous_region(participant, output, ElementType::U32)?);
        launches.push(MaskedArgmaxLaunch {
            first_region,
            scratch_offset_bytes: scratch_stride
                .checked_mul(participant_index as u64)
                .ok_or_else(|| "CUDA masked argmax scratch offset overflows".to_owned())?,
            vocabulary_size: i32::try_from(vocabulary_size)
                .map_err(|_| "masked argmax vocabulary exceeds i32".to_owned())?,
            repetition_capacity,
        });
    }
    let scratch_region = regions.len();
    regions.push(transformer::shared_scratch_region(
        &invocation,
        required_scratch_bytes,
    )?);

    let participant_count = u32::try_from(invocation.participants().len())
        .map_err(|_| "masked argmax participant count exceeds u32".to_owned())?;
    let mut replay_key =
        CudaCommandReplayKeyBuilder::new(provider_fingerprint, "vnext_last_token_masked_argmax")
            .u64(launches.len() as u64);
    for launch in &launches {
        replay_key = replay_key
            .u64(launch.first_region as u64)
            .u64(launch.scratch_offset_bytes)
            .i32(launch.vocabulary_size)
            .i32(launch.repetition_capacity);
    }
    let function = function.clone();
    CudaDeviceCommand::replayable_operation(
        "vnext_last_token_masked_argmax",
        regions,
        replay_key.finish(),
        move |stream, regions| {
            for launch in &launches {
                let logits = regions[launch.first_region].device_ptr();
                let valid_mask = regions[launch.first_region + 1].device_ptr();
                let repetition_token_ids = regions[launch.first_region + 2].device_ptr();
                let repetition_offsets = regions[launch.first_region + 3].device_ptr();
                let repetition_penalty = regions[launch.first_region + 4].device_ptr();
                let output = regions[launch.first_region + 5].device_ptr();
                let scratch = regions[scratch_region]
                    .device_ptr()
                    .checked_add(launch.scratch_offset_bytes)
                    .ok_or_else(|| {
                        CudaDeviceRuntimeError::contract(
                            "vNext masked argmax scratch pointer overflows",
                        )
                    })?;
                let mut builder = stream.launch_builder(&function);
                builder.arg(&logits);
                builder.arg(&scratch);
                builder.arg(&launch.vocabulary_size);
                builder.arg(&valid_mask);
                builder.arg(&launch.vocabulary_size);
                builder.arg(&repetition_offsets);
                builder.arg(&repetition_token_ids);
                builder.arg(&repetition_penalty);
                builder.arg(&launch.repetition_capacity);
                builder.arg(&output);
                unsafe {
                    builder.launch(LaunchConfig {
                        grid_dim: (1, 1, 1),
                        block_dim: (THREADS_PER_BLOCK, 1, 1),
                        shared_mem_bytes: 0,
                    })
                }
                .map_err(|error| {
                    CudaDeviceRuntimeError::driver("vNext masked argmax launch", error)
                })?;
            }
            Ok(())
        },
    )
    .and_then(|command| {
        command.with_work_attribution(
            if participant_count == 1 {
                DeviceBatchingForm::Scalar
            } else {
                DeviceBatchingForm::ParticipantLoop
            },
            participant_count,
            u64::from(participant_count),
            u64::from(participant_count),
            0,
        )
    })
    .map_err(|error| error.to_string())
}

fn masked_argmax_scratch_stride(vocabulary_size: u64) -> Result<u64, String> {
    let bytes = vocabulary_size
        .checked_mul(ElementType::F16.size_bytes())
        .ok_or_else(|| "CUDA masked argmax scratch size overflows".to_owned())?;
    bytes
        .checked_add(VALUE_ALIGNMENT_BYTES - 1)
        .map(|value| value & !(VALUE_ALIGNMENT_BYTES - 1))
        .filter(|value| *value != 0)
        .ok_or_else(|| "CUDA masked argmax scratch alignment overflows".to_owned())
}

fn validate_masked_argmax_signature(
    logits: &ResolvedValueBinding,
    valid_mask: &ResolvedValueBinding,
    repetition_token_ids: &ResolvedValueBinding,
    repetition_offsets: &ResolvedValueBinding,
    repetition_penalty: &ResolvedValueBinding,
    output: &ResolvedValueBinding,
    vocabulary_size: u64,
) -> Result<i32, String> {
    let contiguous = |binding: &ResolvedValueBinding| {
        matches!(binding.tensor().layout(), ResolvedTensorLayout::Contiguous)
    };
    if logits.tensor().element_type() != ElementType::F16
        || valid_mask.tensor().element_type() != ElementType::U8
        || repetition_token_ids.tensor().element_type() != ElementType::U32
        || repetition_offsets.tensor().element_type() != ElementType::U32
        || repetition_penalty.tensor().element_type() != ElementType::F32
        || output.tensor().element_type() != ElementType::U32
        || logits.tensor().dimensions() != [1, vocabulary_size]
        || valid_mask.tensor().dimensions() != [vocabulary_size]
        || repetition_token_ids.tensor().dimensions().len() != 1
        || repetition_token_ids.tensor().dimensions()[0] == 0
        || repetition_offsets.tensor().dimensions() != [2]
        || repetition_penalty.tensor().dimensions() != [1]
        || output.tensor().dimensions() != [1]
        || !contiguous(logits)
        || !contiguous(valid_mask)
        || !contiguous(repetition_token_ids)
        || !contiguous(repetition_offsets)
        || !contiguous(repetition_penalty)
        || !contiguous(output)
    {
        return Err("CUDA masked argmax invocation differs from its resolved signature".to_owned());
    }
    i32::try_from(repetition_token_ids.tensor().dimensions()[0])
        .map_err(|_| "CUDA masked argmax repetition capacity exceeds i32".to_owned())
}

#[derive(Debug, Clone, Copy)]
struct LastTokenDenseLinearLaunch {
    input_region: usize,
    output_region: usize,
}

fn encode_last_token_dense_linear(
    provider_fingerprint: &str,
    invocation: BatchedOperationInvocation<'_, CudaDeviceBuffer>,
) -> Result<CudaDeviceCommand, String> {
    if invocation.operation().id.as_str() != LAST_TOKEN_DENSE_LINEAR_OPERATION_ID
        || invocation.participants().is_empty()
    {
        return Err("CUDA last-token dense-linear received another or empty operation".to_owned());
    }
    let token_ranges = invocation.participant_token_ranges();
    if token_ranges.len() != invocation.participants().len() {
        return Err("CUDA last-token dense-linear participant ranges are incomplete".to_owned());
    }
    let first = &invocation.participants()[0];
    let hidden_size = unsigned_attribute(first.attributes(), "hidden_size")?;
    let out_features = unsigned_attribute(first.attributes(), "out_features")?;
    let input_packed =
        transformer::token_binding_is_packed(&invocation, ResolvedValueRole::Input, 0)?;
    let mut regions = vec![transformer::shared_full_region(
        &invocation,
        ResolvedValueRole::Input,
        1,
        ElementType::F16,
    )?];
    let mut launches = Vec::with_capacity(invocation.participants().len());
    for (participant, token_range) in invocation.participants().iter().zip(token_ranges) {
        let input = binding(participant.bindings(), ResolvedValueRole::Input, 0)?;
        let participant_weight = binding(participant.bindings(), ResolvedValueRole::Input, 1)?;
        let output = binding(participant.bindings(), ResolvedValueRole::Output, 0)?;
        if unsigned_attribute(participant.attributes(), "hidden_size")? != hidden_size
            || unsigned_attribute(participant.attributes(), "out_features")? != out_features
        {
            return Err("CUDA last-token dense-linear participant attributes disagree".to_owned());
        }
        validate_last_token_dense_linear_signature(
            input,
            participant_weight,
            output,
            hidden_size,
            out_features,
        )?;
        let source_range = token_range.source_token_range();
        let packed_range = token_range.immediate_token_range();
        let selected_range = if input_packed {
            packed_range
        } else {
            source_range
        };
        if selected_range.is_empty() {
            return Err("CUDA last-token dense-linear cannot select from an empty span".to_owned());
        }
        let last_token = selected_range.end - 1;
        let input_region = regions.len();
        let source = contiguous_token_region(participant, input, ElementType::F16, last_token, 1)?;
        regions.push(source);
        let output_region = regions.len();
        let destination = contiguous_region(participant, output, ElementType::F16)?;
        regions.push(destination);
        launches.push(LastTokenDenseLinearLaunch {
            input_region,
            output_region,
        });
    }

    let participant_count = u32::try_from(invocation.participants().len())
        .map_err(|_| "last-token dense-linear participant count exceeds u32".to_owned())?;
    let token_count = u64::from(participant_count);
    let compute_dispatch_count = launches.len() as u64;
    let rows = 1_i32;
    let hidden_size = i32::try_from(hidden_size)
        .map_err(|_| "last-token dense-linear hidden size exceeds i32".to_owned())?;
    let out_features = i32::try_from(out_features)
        .map_err(|_| "last-token dense-linear output width exceeds i32".to_owned())?;
    let mut replay_key =
        CudaCommandReplayKeyBuilder::new(provider_fingerprint, "vnext_last_token_dense_linear")
            .i32(rows)
            .i32(hidden_size)
            .i32(out_features)
            .u64(launches.len() as u64);
    for launch in &launches {
        replay_key = replay_key
            .u64(launch.input_region as u64)
            .u64(launch.output_region as u64);
    }
    CudaDeviceCommand::replayable_operation_with_blas(
        "vnext_last_token_dense_linear",
        regions,
        replay_key.finish(),
        move |_stream, blas, regions| {
            let weight = regions[0].device_ptr();
            for launch in &launches {
                transformer::launch_gemm_f16(
                    blas,
                    regions[launch.input_region].device_ptr(),
                    weight,
                    regions[launch.output_region].device_ptr(),
                    rows,
                    out_features,
                    hidden_size,
                    "vNext last-token dense-linear GEMM",
                )?;
            }
            Ok(())
        },
    )
    .and_then(|command| {
        command.with_work_attribution(
            DeviceBatchingForm::ParticipantLoop,
            participant_count,
            token_count,
            compute_dispatch_count,
            0,
        )
    })
    .map_err(|error| error.to_string())
}

fn validate_last_token_dense_linear_signature(
    input: &ResolvedValueBinding,
    weight: &ResolvedValueBinding,
    output: &ResolvedValueBinding,
    hidden_size: u64,
    out_features: u64,
) -> Result<(), String> {
    let contiguous = |binding: &ResolvedValueBinding| {
        matches!(binding.tensor().layout(), ResolvedTensorLayout::Contiguous)
    };
    let input_dimensions = input.tensor().dimensions();
    if input.tensor().element_type() != ElementType::F16
        || weight.tensor().element_type() != ElementType::F16
        || output.tensor().element_type() != ElementType::F16
        || input_dimensions.len() != 2
        || input_dimensions[0] == 0
        || input_dimensions[1] != hidden_size
        || weight.tensor().dimensions() != [out_features, hidden_size]
        || output.tensor().dimensions() != [1, out_features]
        || !contiguous(input)
        || !contiguous(weight)
        || !contiguous(output)
    {
        return Err(
            "CUDA last-token dense-linear invocation differs from its resolved signature"
                .to_owned(),
        );
    }
    Ok(())
}

#[derive(Debug, Clone, Copy)]
struct EmbeddingLaunch {
    first_region: usize,
    token_count: u64,
    vocabulary_size: u32,
    hidden_size: i32,
    grid_x: u32,
}

fn encode_token_embedding(
    function: &CudaFunction,
    provider_fingerprint: &str,
    invocation: BatchedOperationInvocation<'_, CudaDeviceBuffer>,
) -> Result<CudaDeviceCommand, String> {
    if invocation.operation().id.as_str() != TOKEN_EMBEDDING_OPERATION_ID
        || invocation.participants().is_empty()
    {
        return Err("CUDA token embedding received another or empty operation".to_owned());
    }

    let token_ranges = invocation.participant_token_ranges();
    if token_ranges.len() != invocation.participants().len() {
        return Err("CUDA token embedding participant ranges are incomplete".to_owned());
    }
    let input_packed =
        transformer::token_binding_is_packed(&invocation, ResolvedValueRole::Input, 0)?;
    let mut regions = Vec::with_capacity(invocation.participants().len() * 3);
    let mut launches = Vec::with_capacity(invocation.participants().len());
    for (participant, token_range) in invocation.participants().iter().zip(token_ranges) {
        let token_ids = binding(participant.bindings(), ResolvedValueRole::Input, 0)?;
        let table = binding(participant.bindings(), ResolvedValueRole::Input, 1)?;
        let output = binding(participant.bindings(), ResolvedValueRole::Output, 0)?;
        let hidden_size = unsigned_attribute(participant.attributes(), "hidden_size")?;
        let vocabulary_size = unsigned_attribute(participant.attributes(), "vocab_size")?;
        validate_signature(token_ids, table, output, vocabulary_size, hidden_size)?;
        let source_range = token_range.source_token_range();
        let packed_range = token_range.immediate_token_range();
        let token_count = token_range.immediate_tokens();
        let grid_x = hidden_size
            .div_ceil(THREADS_PER_BLOCK as u64)
            .try_into()
            .map_err(|_| "embedding launch grid exceeds u32".to_owned())?;

        let first_region = regions.len();
        regions.push(contiguous_region(participant, table, ElementType::F16)?);
        regions.push(contiguous_token_region(
            participant,
            token_ids,
            ElementType::U32,
            if input_packed {
                packed_range.start
            } else {
                source_range.start
            },
            token_count,
        )?);
        regions.push(contiguous_token_region(
            participant,
            output,
            ElementType::F16,
            packed_range.start,
            token_count,
        )?);
        launches.push(EmbeddingLaunch {
            first_region,
            token_count,
            vocabulary_size: vocabulary_size
                .try_into()
                .map_err(|_| "embedding vocabulary size exceeds u32".to_owned())?,
            hidden_size: hidden_size
                .try_into()
                .map_err(|_| "embedding hidden size exceeds i32".to_owned())?,
            grid_x,
        });
    }

    let participant_count = u32::try_from(invocation.participants().len())
        .map_err(|_| "embedding participant count exceeds u32".to_owned())?;
    let token_count = invocation.work_shape().immediate_tokens();
    let compute_dispatch_count = launches
        .iter()
        .map(|launch| launch.token_count.div_ceil(MAXIMUM_TOKENS_PER_LAUNCH))
        .sum();
    let mut replay_key =
        CudaCommandReplayKeyBuilder::new(provider_fingerprint, "vnext_token_embedding")
            .u64(launches.len() as u64);
    for launch in &launches {
        replay_key = replay_key
            .u64(launch.first_region as u64)
            .u64(launch.token_count)
            .u32(launch.vocabulary_size)
            .i32(launch.hidden_size)
            .u32(launch.grid_x);
    }
    let function = function.clone();
    CudaDeviceCommand::replayable_operation(
        "vnext_token_embedding",
        regions,
        replay_key.finish(),
        move |stream, regions| {
            for launch in &launches {
                let table = regions[launch.first_region].device_ptr();
                let token_ids_base = regions[launch.first_region + 1].device_ptr();
                let output_base = regions[launch.first_region + 2].device_ptr();
                let mut token_offset = 0_u64;
                while token_offset < launch.token_count {
                    let chunk_tokens =
                        (launch.token_count - token_offset).min(MAXIMUM_TOKENS_PER_LAUNCH);
                    let token_ids = checked_pointer_offset(
                        token_ids_base,
                        token_offset,
                        ElementType::U32.size_bytes(),
                        "token id",
                    )?;
                    let output_element_offset = token_offset
                        .checked_mul(launch.hidden_size as u64)
                        .ok_or_else(|| {
                            CudaDeviceRuntimeError::contract(
                                "vNext embedding output element offset overflows",
                            )
                        })?;
                    let output = checked_pointer_offset(
                        output_base,
                        output_element_offset,
                        ElementType::F16.size_bytes(),
                        "embedding output",
                    )?;
                    let batch = chunk_tokens as i32;
                    let mut builder = stream.launch_builder(&function);
                    builder.arg(&table);
                    builder.arg(&token_ids);
                    builder.arg(&output);
                    builder.arg(&batch);
                    builder.arg(&launch.hidden_size);
                    builder.arg(&launch.vocabulary_size);
                    unsafe {
                        builder.launch(LaunchConfig {
                            grid_dim: (launch.grid_x, chunk_tokens as u32, 1),
                            block_dim: (THREADS_PER_BLOCK, 1, 1),
                            shared_mem_bytes: 0,
                        })
                    }
                    .map_err(|error| {
                        CudaDeviceRuntimeError::driver("vNext token embedding launch", error)
                    })?;
                    token_offset += chunk_tokens;
                }
            }
            Ok(())
        },
    )
    .and_then(|command| {
        command.with_work_attribution(
            DeviceBatchingForm::ParticipantLoop,
            participant_count,
            token_count,
            compute_dispatch_count,
            0,
        )
    })
    .map_err(|error| error.to_string())
}

fn checked_pointer_offset(
    base: cudarc::driver::sys::CUdeviceptr,
    elements: u64,
    element_bytes: u64,
    context: &'static str,
) -> Result<cudarc::driver::sys::CUdeviceptr, CudaDeviceRuntimeError> {
    elements
        .checked_mul(element_bytes)
        .and_then(|bytes| base.checked_add(bytes))
        .ok_or_else(|| CudaDeviceRuntimeError::contract(format!("{context} pointer overflows")))
}

fn validate_signature(
    token_ids: &ResolvedValueBinding,
    table: &ResolvedValueBinding,
    output: &ResolvedValueBinding,
    vocabulary_size: u64,
    hidden_size: u64,
) -> Result<u64, String> {
    let token_dimensions = token_ids.tensor().dimensions();
    let table_dimensions = table.tensor().dimensions();
    let output_dimensions = output.tensor().dimensions();
    let contiguous = |binding: &ResolvedValueBinding| {
        matches!(binding.tensor().layout(), ResolvedTensorLayout::Contiguous)
    };
    if token_ids.tensor().element_type() != ElementType::U32
        || table.tensor().element_type() != ElementType::F16
        || output.tensor().element_type() != ElementType::F16
        || token_dimensions.len() != 1
        || table_dimensions != [vocabulary_size, hidden_size]
        || output_dimensions != [token_dimensions[0], hidden_size]
        || !contiguous(token_ids)
        || !contiguous(table)
        || !contiguous(output)
    {
        return Err(
            "CUDA token embedding invocation differs from its resolved signature".to_owned(),
        );
    }
    Ok(token_dimensions[0])
}

fn binding(
    bindings: &[ResolvedValueBinding],
    role: ResolvedValueRole,
    ordinal: u32,
) -> Result<&ResolvedValueBinding, String> {
    bindings
        .iter()
        .find(|binding| binding.role() == role && binding.ordinal() == ordinal)
        .ok_or_else(|| format!("CUDA operation lacks {role:?} binding {ordinal}"))
}

fn unsigned_attribute(
    attributes: &std::collections::BTreeMap<AttributeId, SemanticValue>,
    name: &str,
) -> Result<u64, String> {
    match attributes
        .iter()
        .find(|(attribute, _)| attribute.as_str() == name)
        .map(|(_, value)| value)
    {
        Some(SemanticValue::Unsigned(value)) => Ok(*value),
        _ => Err(format!("CUDA operation lacks unsigned attribute {name:?}")),
    }
}

fn reusable_token_topology(
    request: &ReusableExecutionTopologyRequest<'_>,
    domain: &'static [u8],
) -> Result<ReusableExecutionTopology, VNextError> {
    for (role, ordinal) in [
        (ResolvedValueRole::Input, 0),
        (ResolvedValueRole::Input, 1),
        (ResolvedValueRole::Output, 0),
    ] {
        if request
            .binding_reusable_address_scope(role, ordinal)?
            .is_none()
        {
            return Ok(ReusableExecutionTopology::EagerBoundary);
        }
    }

    let bind_source_ranges =
        !request.binding_uses_packed_batch_coordinates(ResolvedValueRole::Input, 0)?;
    let ranges = request.work_shape().participant_token_ranges();
    let mut digest = Sha256::new();
    digest.update(domain);
    digest.update((ranges.len() as u64).to_le_bytes());
    digest.update(request.work_shape().immediate_tokens().to_le_bytes());
    for range in ranges {
        let source = range.source_token_range();
        let packed = range.immediate_token_range();
        digest.update(range.immediate_tokens().to_le_bytes());
        if bind_source_ranges {
            digest.update(source.start.to_le_bytes());
            digest.update(source.end.to_le_bytes());
        }
        digest.update(packed.start.to_le_bytes());
        digest.update(packed.end.to_le_bytes());
    }
    Ok(ReusableExecutionTopology::Dynamic(
        DeviceReusableExecutionTopologyFingerprint::from_sha256(digest.finalize().into()),
    ))
}

fn contiguous_region(
    participant: &OperationInvocation<'_, CudaDeviceBuffer>,
    binding: &ResolvedValueBinding,
    element_type: ElementType,
) -> Result<CudaBufferRegion, String> {
    let [component] = binding.storage().components() else {
        return Err("CUDA operation requires one storage component per value".to_owned());
    };
    if component.element_type() != element_type {
        return Err("CUDA operation storage element type differs from its contract".to_owned());
    }
    contiguous_region_range(
        participant,
        binding,
        element_type,
        component.offset_bytes(),
        component.length_bytes(),
    )
}

fn contiguous_token_region(
    participant: &OperationInvocation<'_, CudaDeviceBuffer>,
    binding: &ResolvedValueBinding,
    element_type: ElementType,
    token_start: u64,
    token_count: u64,
) -> Result<CudaBufferRegion, String> {
    let [component] = binding.storage().components() else {
        return Err("CUDA operation requires one storage component per value".to_owned());
    };
    let projection = participant
        .work()
        .token_projection(binding.role(), binding.ordinal())
        .ok_or_else(|| "CUDA operation binding has no token work projection".to_owned())?;
    let dimensions = binding.tensor().dimensions();
    if projection.axis() != 0
        || projection.rank() as usize != dimensions.len()
        || dimensions.first() != Some(&projection.canonical_extent())
        || component.offset_bytes() != 0
        || component.length_bytes() % projection.canonical_extent() != 0
    {
        return Err(
            "CUDA contiguous token projection is not a canonical leading-axis tensor".to_owned(),
        );
    }
    let bytes_per_token = component.length_bytes() / projection.canonical_extent();
    let logical_offset = token_start
        .checked_mul(bytes_per_token)
        .ok_or_else(|| "CUDA token region offset overflows".to_owned())?;
    let logical_length = token_count
        .checked_mul(bytes_per_token)
        .ok_or_else(|| "CUDA token region length overflows".to_owned())?;
    contiguous_region_range(
        participant,
        binding,
        element_type,
        logical_offset,
        logical_length,
    )
}

fn contiguous_region_range(
    participant: &OperationInvocation<'_, CudaDeviceBuffer>,
    binding: &ResolvedValueBinding,
    element_type: ElementType,
    logical_offset_bytes: u64,
    logical_length_bytes: u64,
) -> Result<CudaBufferRegion, String> {
    let [component] = binding.storage().components() else {
        return Err("CUDA operation requires one storage component per value".to_owned());
    };
    if component.element_type() != element_type {
        return Err("CUDA operation storage element type differs from its contract".to_owned());
    }
    let view = participant
        .views()
        .iter()
        .find(|view| view.resource_id() == component.resource_id())
        .ok_or_else(|| "CUDA operation value has no resource view".to_owned())?;
    // The invocation construction has already proved the resource view and
    // component describe the same allocation. The provider still translates
    // the logical slice instead of assuming an arena base pointer.
    let translated = view
        .translate(logical_offset_bytes, logical_length_bytes)
        .map_err(|error| error.to_string())?;
    let mut physical = translated.iter();
    let region = physical
        .next()
        .ok_or_else(|| "CUDA operation translated to no physical region".to_owned())?;
    if physical.next().is_some() {
        return Err("CUDA operation requires contiguous physical storage".to_owned());
    }
    let (buffer, range, retention) = region.buffer_and_physical_range();
    let region = buffer
        .retained_region(range, retention)
        .map_err(|error| error.to_string())?;
    if region.element_type() != element_type || region.length_bytes() != logical_length_bytes {
        return Err(
            "CUDA operation physical region differs from its resolved component".to_owned(),
        );
    }
    Ok(region)
}

fn same_physical_region(left: &CudaBufferRegion, right: &CudaBufferRegion) -> bool {
    left.device_ptr() == right.device_ptr()
        && left.length_bytes() == right.length_bytes()
        && left.element_type() == right.element_type()
}

fn contiguous_bindings() -> Vec<ProviderStorageBindingRequirement> {
    [
        (ResolvedValueRole::Input, 0),
        (ResolvedValueRole::Input, 1),
        (ResolvedValueRole::Output, 0),
    ]
    .into_iter()
    .map(|(role, ordinal)| {
        ProviderStorageBindingRequirement::new(
            role,
            ordinal,
            DynamicStorageRequirement::contiguous(),
        )
    })
    .collect()
}

fn provider_failure(
    identity: ferrum_interfaces::vnext::ExecutionIdentityEnvelope,
    stage: &'static str,
    message: String,
) -> OperationFailure {
    let message = message.chars().take(2048).collect::<String>();
    OperationFailure::new(identity, ProfilePhase::Forward, stage, message, false)
        .expect("core-issued CUDA operation identity must form a valid provider failure")
}

fn implementation_fingerprint(parts: &[&[u8]]) -> String {
    let mut digest = Sha256::new();
    for part in parts {
        digest.update((part.len() as u64).to_le_bytes());
        digest.update(part);
    }
    format!("{:x}", digest.finalize())
}

fn contract_error(error: VNextError) -> CudaDeviceRuntimeError {
    CudaDeviceRuntimeError::contract(error.to_string())
}