aprender-gpu 0.30.0

Pure Rust PTX generation for NVIDIA CUDA - no LLVM, no nvcc
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
# Complete CUDA Runtime Specification

**Version:** 2.0.0
**Date:** 2025-12-14
**Status:** Draft - Post Design Review (NO EXTERNAL DEPENDENCIES)
**Methodology:** Toyota Production System (TPS) + Popperian Falsification
**Review:** Formal Popperian Design Review incorporated
**Philosophy:** **OWN THE STACK** - Zero external dependencies, pure Rust + minimal FFI

---

## Executive Summary

trueno-gpu currently generates PTX assembly code but cannot execute it. This specification defines **every missing component** required for end-to-end GPU kernel execution, designed with Toyota Way principles and Popperian falsification methodology.

**Current State:**
```
┌────────────────────────────────────────────────────────────────────┐
│  Rust Code → PTX Builder → PTX String → ??? (NO EXECUTION)        │
│                                                                    │
│  ✅ kernels/attention.rs  (459 lines) - FlashAttention PTX        │
│  ✅ kernels/gemm.rs       (643 lines) - Tiled GEMM PTX            │
│  ✅ kernels/quantize.rs   (364 lines) - Q4_K dequant PTX          │
│  ✅ kernels/softmax.rs    (358 lines) - Warp-shuffle softmax PTX  │
│  ✅ kernels/layernorm.rs  (509 lines) - Fused LayerNorm PTX       │
│  ✅ ptx/builder.rs        (1000 lines) - PTX code generation      │
│  ❌ driver/sys.rs         (0 lines)   - MISSING: OUR OWN FFI      │
│  ❌ driver/context.rs     (0 lines)   - MISSING: CUDA context     │
│  ❌ driver/module.rs      (0 lines)   - MISSING: PTX loader       │
│  ❌ driver/stream.rs      (0 lines)   - MISSING: Async execution  │
└────────────────────────────────────────────────────────────────────┘
```

**V2.0 Philosophy Change:**
```
┌─────────────────────────────────────────────────────────────────────┐
│  REJECTED: cudarc (external dependency by Corey Lowman)             │
│  ACCEPTED: driver/sys.rs (OUR OWN ~400 lines of FFI)                │
│                                                                     │
│  Rationale: We built 5,500 lines of PTX generation from scratch.    │
│  We can build 400 lines of CUDA FFI. OWN THE STACK.                 │
└─────────────────────────────────────────────────────────────────────┘
```

---

## 1. Toyota Way Design Principles

### 1.1 The 14 Principles Applied

| Principle | Application to trueno-gpu |
|-----------|--------------------------|
| **1. Long-term philosophy** | Build reusable CUDA abstractions, not one-off hacks |
| **2. Create continuous flow** | PTX generation → Load → Execute → Synchronize (no batching waste) |
| **3. Use pull systems** | Lazy module loading - only load kernels when needed |
| **4. Level the workload (Heijunka)** | Memory pool to smooth allocation spikes |
| **5. Build quality in (Jidoka)** | Compile-time state machine prevents invalid GPU operations |
| **6. Standardized tasks** | Consistent API for all kernel types |
| **7. Visual control** | Clear error messages with GPU state context |
| **8. Reliable technology** | Use stable CUDA Driver API (since CUDA 3.0), wrapped in our own RAII FFI |
| **9. Grow leaders** | Document every design decision for future maintainers |
| **10. Develop people** | Extensive examples and tutorials |
| **11. Help partners** | Clean API for realizar integration |
| **12. Go see (Genchi Genbutsu)** | Profile actual GPU execution, not estimates |
| **13. Decide slowly, act fast** | This spec reviews before implementation |
| **14. Become learning org** | Falsifiable benchmarks track real progress |

### 1.2 Muda (Waste) Identification

| Waste Type | Current Manifestation | Elimination Strategy |
|------------|----------------------|---------------------|
| **Overproduction** | Generating PTX that's never executed | Execute what we generate |
| **Waiting** | CPU idle during GPU execution | Async streams + overlap |
| **Transport** | Unnecessary H2D/D2H copies | Persistent GPU buffers |
| **Extra Processing** | Full precision when Q4 suffices | Fused dequant+compute |
| **Inventory** | Unbounded memory allocation | Pool with limits |
| **Motion** | Context switching overhead | Single context per device |
| **Defects** | Silent failures, wrong results | Checksums + validation |

---

## 2. Popperian Falsification Framework

### 2.1 Philosophy

> "A theory which is not refutable by any conceivable event is non-scientific."
> — Karl Popper, *Conjectures and Refutations* (1963)

Every claim in this specification is **falsifiable**. We define specific tests that would DISPROVE our claims if they fail. We do NOT attempt to prove correctness—we attempt to find bugs.

### 2.2 Design Review Critiques (Addressed in V1.1)

#### Critique 1: The "Happy Path" Trap
* **Problem:** Original hypotheses H1, H2, H4 focused on success states (verification, not falsification).
* **Resolution:** Shift tests to boundary conditions with quantitative thresholds.
  * *Weak:* "Streams provide async overlap."
  * *Strong:* "The abstraction overhead of `CudaStream` does not exceed 2µs per launch. If overhead > 2µs, the abstraction is 'leaky' and must be discarded."

#### Critique 2: The "Black Swan" of Latency
* **Problem:** Assumed `cudarc` (safe Rust wrapper) has negligible overhead—a bold HPC claim.
* **Resolution:** Added H11 "Zero-Cost Safety" test comparing vs raw C++ CUDA benchmark.
  * *Threshold:* If `trueno` is >5% slower than C++, the "zero-cost safety" hypothesis is falsified.

#### Critique 3: Memory Fragmentation
* **Problem:** H8 checked allocation call count, but not long-term fragmentation.
* **Resolution:** Added H12 "Fragmentation Resistance" test.
  * *Test:* "Thundering Herd" random alloc/free for 10,000 iterations.
  * *Falsification:* If memory usage grows >1.1x ideal, allocator design is falsified.

### 2.3 Falsifiable Hypotheses

| ID | Hypothesis | Falsification Test | Threshold |
|----|------------|-------------------|-----------|
| **H1** | Our FFI loads PTX modules | `CudaModule::from_ptx()` succeeds | Module handle ≠ null |
| **H2** | Kernel launches execute | Write known value, read back, compare | Exact match |
| **H3** | Memory transfers are correct | H2D followed by D2H returns original | Byte-exact equality |
| **H4** | Stream overhead is acceptable | Measure launch latency overhead | **Overhead ≤ 2µs per launch** |
| **H5** | GEMM produces correct output | Compare vs CPU reference | Max |error| < 1e-3 |
| **H6** | Attention matches reference | Compare vs PyTorch FlashAttention | Max |error| < 1e-2 |
| **H7** | Q4_K dequant is accurate | Compare vs llama.cpp dequant | Max |error| < 1e-4 |
| **H8** | Memory pool reduces allocs | Count cuMemAlloc calls | Pool: <10, No pool: >100 |
| **H9** | Error handling catches failures | Intentionally trigger OOM | Error returned, no panic |
| **H10** | Multi-stream parallelism works | Launch N independent kernels | Speedup > 1.5x vs sequential |

### 2.4 Crucial Experiments (*Experimentum Crucis*)

These "Killer Tests" are designed to **actively break** the design. If any fails, the corresponding design decision must be reconsidered.

| ID | Hypothesis | Falsification Test (The "Killer") | Threshold |
|----|------------|-----------------------------------|-----------|
| **H11** | **Zero-Cost Safety** | Microbenchmark launch latency: `trueno` vs raw C++ CUDA | **Latency > 1.05x C++ → FALSIFIED** |
| **H12** | **Fragmentation Resistance** | Random alloc/free loop (10k iters) with varying sizes (1KB-10MB) | **Mem Usage > 1.1x Ideal → FALSIFIED** |
| **H13** | **Concurrency Safety** | Launch kernels from 4 threads sharing 1 context simultaneously | **Any Race/Panic → FALSIFIED** |

#### H11 Implementation: Zero-Cost Safety Benchmark

```rust
/// Crucial Experiment: Does Rust safety have a cost?
///
/// Methodology:
/// 1. Benchmark 10,000 kernel launches via trueno-gpu
/// 2. Benchmark 10,000 kernel launches via raw C++ CUDA (extern "C")
/// 3. Compare median latency
///
/// Falsification: If trueno > 1.05x C++, the "zero-cost" claim is false.
#[test]
#[cfg(feature = "cuda")]
fn crucial_h11_zero_cost_safety() {
    const ITERATIONS: usize = 10_000;
    const THRESHOLD: f64 = 1.05; // 5% tolerance

    // Measure trueno-gpu launch latency
    let ctx = CudaContext::new(0).unwrap();
    let stream = CudaStream::new(ctx.device().clone()).unwrap();
    let kernel = trivial_kernel(); // Minimal work kernel

    let start = std::time::Instant::now();
    for _ in 0..ITERATIONS {
        execute_kernel(&ctx, &stream, &kernel, LaunchConfig::linear(1, 1), &[]).unwrap();
        stream.synchronize().unwrap();
    }
    let trueno_duration = start.elapsed();

    // Compare against C++ baseline (from external benchmark)
    let cpp_baseline_us = 2.0; // Documented C++ baseline in microseconds
    let trueno_us = trueno_duration.as_micros() as f64 / ITERATIONS as f64;
    let ratio = trueno_us / cpp_baseline_us;

    assert!(
        ratio <= THRESHOLD,
        "H11 FALSIFIED: trueno latency {:.2}µs is {:.2}x C++ baseline {:.2}µs (threshold: {:.2}x)",
        trueno_us, ratio, cpp_baseline_us, THRESHOLD
    );
}
```

#### H12 Implementation: Fragmentation Resistance

```rust
/// Crucial Experiment: Does the pool fragment under stress?
///
/// Methodology:
/// 1. Allocate/free random sizes (1KB - 10MB) for 10,000 iterations
/// 2. Track actual vs ideal memory usage
///
/// Falsification: If actual > 1.1x ideal, pool design is flawed.
#[test]
#[cfg(feature = "cuda")]
fn crucial_h12_fragmentation_resistance() {
    use rand::{Rng, SeedableRng};

    const ITERATIONS: usize = 10_000;
    const MIN_SIZE: usize = 1024;        // 1 KB
    const MAX_SIZE: usize = 10 * 1024 * 1024; // 10 MB
    const FRAGMENTATION_THRESHOLD: f64 = 1.1;

    let ctx = CudaContext::new(0).unwrap();
    let mut pool = MemoryPool::new(ctx.device().clone(), PoolConfig::default());
    let mut rng = rand::rngs::StdRng::seed_from_u64(42);
    let mut allocations: Vec<GpuAllocation<u8>> = Vec::new();
    let mut total_requested: usize = 0;

    for i in 0..ITERATIONS {
        // 70% allocate, 30% free
        if rng.gen_bool(0.7) || allocations.is_empty() {
            let size = rng.gen_range(MIN_SIZE..MAX_SIZE);
            let alloc = pool.alloc::<u8>(size).unwrap();
            total_requested += size;
            allocations.push(alloc);
        } else {
            let idx = rng.gen_range(0..allocations.len());
            let freed = allocations.swap_remove(idx);
            total_requested -= freed.len();
        }
    }

    let actual_usage = pool.allocated_bytes();
    let ideal_usage = total_requested;
    let ratio = actual_usage as f64 / ideal_usage as f64;

    assert!(
        ratio <= FRAGMENTATION_THRESHOLD,
        "H12 FALSIFIED: Fragmentation ratio {:.2}x exceeds threshold {:.2}x \
         (actual: {} bytes, ideal: {} bytes)",
        ratio, FRAGMENTATION_THRESHOLD, actual_usage, ideal_usage
    );
}
```

#### H13 Implementation: Concurrency Safety

```rust
/// Crucial Experiment: Is the context thread-safe?
///
/// Methodology:
/// 1. Share one CudaContext across 4 threads
/// 2. Each thread launches 1000 kernels concurrently
///
/// Falsification: Any race condition, panic, or data corruption.
#[test]
#[cfg(feature = "cuda")]
fn crucial_h13_concurrency_safety() {
    use std::sync::Arc;
    use std::thread;

    const NUM_THREADS: usize = 4;
    const KERNELS_PER_THREAD: usize = 1000;

    let ctx = Arc::new(CudaContext::new(0).unwrap());
    let mut handles = Vec::new();

    for thread_id in 0..NUM_THREADS {
        let ctx = Arc::clone(&ctx);
        let handle = thread::spawn(move || {
            let stream = CudaStream::new(ctx.device().clone()).unwrap();
            let kernel = trivial_kernel();

            for i in 0..KERNELS_PER_THREAD {
                execute_kernel(&ctx, &stream, &kernel, LaunchConfig::linear(1, 1), &[])
                    .expect(&format!("Thread {} kernel {} failed", thread_id, i));
            }
            stream.synchronize().unwrap();
        });
        handles.push(handle);
    }

    // All threads must complete without panic
    for (i, handle) in handles.into_iter().enumerate() {
        handle.join().expect(&format!("Thread {} panicked", i));
    }
}
```

### 2.5 Falsification Protocol

```rust
/// Every test follows this structure:
fn falsification_test<T: PartialEq>(
    hypothesis: &str,
    operation: impl FnOnce() -> T,
    expected: T,
    tolerance: Option<f64>,
) -> Result<(), FalsificationError> {
    let result = operation();

    // Attempt to DISPROVE the hypothesis
    if result != expected {
        return Err(FalsificationError::Falsified {
            hypothesis: hypothesis.to_string(),
            expected: format!("{:?}", expected),
            actual: format!("{:?}", result),
        });
    }

    // Hypothesis survives this test (NOT proven, just not disproven)
    Ok(())
}
```

---

## 3. Missing Components Inventory

### 3.1 Component Dependency Graph

```
┌─────────────────────────────────────────────────────────────────────────┐
│                    MISSING COMPONENTS (NO EXTERNAL DEPS)                 │
├─────────────────────────────────────────────────────────────────────────┤
│                                                                          │
│  Cargo.toml                                                              │
│     └── NO NEW DEPENDENCIES (only libc for FFI)                         │
│                                                                          │
│  src/driver/                                                             │
│     ├── sys.rs       ←── OUR OWN CUDA FFI (~400 lines)                  │
│     ├── context.rs   ←── CudaContext (device init, primary context)     │
│     ├── module.rs    ←── CudaModule (PTX loading, JIT compilation)      │
│     ├── stream.rs    ←── CudaStream (async execution, sync)             │
│     ├── memory.rs    ←── CudaMemory (alloc, free, copy)                 │
│     └── function.rs  ←── CudaFunction (kernel handle, launch)           │
│                                                                          │
│  src/executor/                                                           │
│     ├── mod.rs       ←── High-level kernel execution                    │
│     ├── gemm.rs      ←── GEMM kernel executor                           │
│     ├── attention.rs ←── Attention kernel executor                      │
│     └── quantize.rs  ←── Q4_K kernel executor                           │
│                                                                          │
│  src/memory/                                                             │
│     ├── allocator.rs ←── Actual GPU allocation (currently stubbed)      │
│     └── transfer.rs  ←── H2D/D2H with pinned memory                     │
│                                                                          │
└─────────────────────────────────────────────────────────────────────────┘
```

### 3.2 Detailed Component Specifications

#### 3.2.1 CRT-001: Our Own CUDA FFI (driver/sys.rs)

**File:** `src/driver/sys.rs`

**Philosophy:** We reject external dependencies like `cudarc`. We built 5,500 lines of PTX
generation; we can build 400 lines of CUDA FFI. **OWN THE STACK.**

```rust
//! Minimal CUDA Driver API FFI Bindings
//!
//! Hand-written FFI for the ~20 CUDA driver functions we actually need.
//! No external dependencies. Dynamic loading via libcuda.so/nvcuda.dll.
//!
//! Design: Toyota Principle #8 (Reliable Technology) - Minimal surface area,
//! maximum control. We only bind what we use.
//!
//! Reference: NVIDIA CUDA Driver API v12.3 [5]

#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use std::ffi::{c_char, c_int, c_uint, c_void};
use std::os::raw::c_ulong;

// ============================================================================
// CUDA Types (from cuda.h)
// ============================================================================

/// CUDA result code
pub type CUresult = c_int;

/// CUDA device handle
pub type CUdevice = c_int;

/// CUDA context handle (opaque pointer)
pub type CUcontext = *mut c_void;

/// CUDA module handle (opaque pointer)
pub type CUmodule = *mut c_void;

/// CUDA function handle (opaque pointer)
pub type CUfunction = *mut c_void;

/// CUDA stream handle (opaque pointer)
pub type CUstream = *mut c_void;

/// Device pointer (GPU memory address)
pub type CUdeviceptr = c_ulong;

// ============================================================================
// CUDA Error Codes (subset we handle)
// ============================================================================

pub const CUDA_SUCCESS: CUresult = 0;
pub const CUDA_ERROR_INVALID_VALUE: CUresult = 1;
pub const CUDA_ERROR_OUT_OF_MEMORY: CUresult = 2;
pub const CUDA_ERROR_NOT_INITIALIZED: CUresult = 3;
pub const CUDA_ERROR_DEINITIALIZED: CUresult = 4;
pub const CUDA_ERROR_NO_DEVICE: CUresult = 100;
pub const CUDA_ERROR_INVALID_DEVICE: CUresult = 101;
pub const CUDA_ERROR_INVALID_PTX: CUresult = 218;
pub const CUDA_ERROR_INVALID_HANDLE: CUresult = 400;

// ============================================================================
// CUDA Driver API Functions (dynamic loading)
// ============================================================================

/// CUDA driver function table - loaded dynamically at runtime
///
/// We use dynamic loading so the library can be built without CUDA installed.
/// The functions are resolved when `CudaDriver::load()` is called.
#[derive(Debug)]
pub struct CudaDriver {
    // Initialization
    pub cuInit: unsafe extern "C" fn(flags: c_uint) -> CUresult,

    // Device management
    pub cuDeviceGetCount: unsafe extern "C" fn(count: *mut c_int) -> CUresult,
    pub cuDeviceGet: unsafe extern "C" fn(device: *mut CUdevice, ordinal: c_int) -> CUresult,
    pub cuDeviceGetName: unsafe extern "C" fn(
        name: *mut c_char,
        len: c_int,
        dev: CUdevice
    ) -> CUresult,
    pub cuDeviceGetAttribute: unsafe extern "C" fn(
        pi: *mut c_int,
        attrib: c_int,
        dev: CUdevice,
    ) -> CUresult,

    // Context management (Primary Context API - recommended)
    pub cuDevicePrimaryCtxRetain: unsafe extern "C" fn(
        pctx: *mut CUcontext,
        dev: CUdevice
    ) -> CUresult,
    pub cuDevicePrimaryCtxRelease: unsafe extern "C" fn(dev: CUdevice) -> CUresult,
    pub cuCtxSetCurrent: unsafe extern "C" fn(ctx: CUcontext) -> CUresult,

    // Module management (PTX loading)
    pub cuModuleLoadData: unsafe extern "C" fn(
        module: *mut CUmodule,
        image: *const c_void
    ) -> CUresult,
    pub cuModuleUnload: unsafe extern "C" fn(hmod: CUmodule) -> CUresult,
    pub cuModuleGetFunction: unsafe extern "C" fn(
        hfunc: *mut CUfunction,
        hmod: CUmodule,
        name: *const c_char,
    ) -> CUresult,

    // Memory management
    pub cuMemAlloc_v2: unsafe extern "C" fn(
        dptr: *mut CUdeviceptr,
        bytesize: usize
    ) -> CUresult,
    pub cuMemFree_v2: unsafe extern "C" fn(dptr: CUdeviceptr) -> CUresult,
    pub cuMemcpyHtoD_v2: unsafe extern "C" fn(
        dstDevice: CUdeviceptr,
        srcHost: *const c_void,
        ByteCount: usize,
    ) -> CUresult,
    pub cuMemcpyDtoH_v2: unsafe extern "C" fn(
        dstHost: *mut c_void,
        srcDevice: CUdeviceptr,
        ByteCount: usize,
    ) -> CUresult,
    pub cuMemGetInfo_v2: unsafe extern "C" fn(
        free: *mut usize,
        total: *mut usize
    ) -> CUresult,

    // Stream management
    pub cuStreamCreate: unsafe extern "C" fn(
        phStream: *mut CUstream,
        Flags: c_uint
    ) -> CUresult,
    pub cuStreamDestroy_v2: unsafe extern "C" fn(hStream: CUstream) -> CUresult,
    pub cuStreamSynchronize: unsafe extern "C" fn(hStream: CUstream) -> CUresult,

    // Kernel launch
    pub cuLaunchKernel: unsafe extern "C" fn(
        f: CUfunction,
        gridDimX: c_uint, gridDimY: c_uint, gridDimZ: c_uint,
        blockDimX: c_uint, blockDimY: c_uint, blockDimZ: c_uint,
        sharedMemBytes: c_uint,
        hStream: CUstream,
        kernelParams: *mut *mut c_void,
        extra: *mut *mut c_void,
    ) -> CUresult,
}

impl CudaDriver {
    /// Load CUDA driver dynamically
    ///
    /// Searches for libcuda.so (Linux) or nvcuda.dll (Windows).
    /// Returns None if CUDA driver is not installed.
    ///
    /// # Safety
    /// This function loads a shared library and resolves function pointers.
    /// The library must remain loaded for the lifetime of the returned struct.
    #[cfg(feature = "cuda")]
    pub unsafe fn load() -> Option<Self> {
        // Platform-specific library loading
        #[cfg(target_os = "linux")]
        let lib = libloading::Library::new("libcuda.so.1")
            .or_else(|_| libloading::Library::new("libcuda.so"))
            .ok()?;

        #[cfg(target_os = "windows")]
        let lib = libloading::Library::new("nvcuda.dll").ok()?;

        #[cfg(target_os = "macos")]
        return None; // No CUDA on macOS

        // Resolve all function pointers
        macro_rules! load_fn {
            ($name:ident) => {
                *lib.get::<unsafe extern "C" fn() -> CUresult>(
                    concat!(stringify!($name), "\0").as_bytes()
                ).ok()? as _
            };
        }

        Some(Self {
            cuInit: load_fn!(cuInit),
            cuDeviceGetCount: load_fn!(cuDeviceGetCount),
            cuDeviceGet: load_fn!(cuDeviceGet),
            cuDeviceGetName: load_fn!(cuDeviceGetName),
            cuDeviceGetAttribute: load_fn!(cuDeviceGetAttribute),
            cuDevicePrimaryCtxRetain: load_fn!(cuDevicePrimaryCtxRetain),
            cuDevicePrimaryCtxRelease: load_fn!(cuDevicePrimaryCtxRelease),
            cuCtxSetCurrent: load_fn!(cuCtxSetCurrent),
            cuModuleLoadData: load_fn!(cuModuleLoadData),
            cuModuleUnload: load_fn!(cuModuleUnload),
            cuModuleGetFunction: load_fn!(cuModuleGetFunction),
            cuMemAlloc_v2: load_fn!(cuMemAlloc_v2),
            cuMemFree_v2: load_fn!(cuMemFree_v2),
            cuMemcpyHtoD_v2: load_fn!(cuMemcpyHtoD_v2),
            cuMemcpyDtoH_v2: load_fn!(cuMemcpyDtoH_v2),
            cuMemGetInfo_v2: load_fn!(cuMemGetInfo_v2),
            cuStreamCreate: load_fn!(cuStreamCreate),
            cuStreamDestroy_v2: load_fn!(cuStreamDestroy_v2),
            cuStreamSynchronize: load_fn!(cuStreamSynchronize),
            cuLaunchKernel: load_fn!(cuLaunchKernel),
        })
    }

    /// Convert CUDA result to our error type
    pub fn check(result: CUresult) -> Result<(), crate::GpuError> {
        if result == CUDA_SUCCESS {
            Ok(())
        } else {
            Err(crate::GpuError::CudaDriver(
                cuda_error_name(result).to_string(),
                result,
            ))
        }
    }
}

/// Get human-readable error name
fn cuda_error_name(code: CUresult) -> &'static str {
    match code {
        CUDA_SUCCESS => "CUDA_SUCCESS",
        CUDA_ERROR_INVALID_VALUE => "CUDA_ERROR_INVALID_VALUE",
        CUDA_ERROR_OUT_OF_MEMORY => "CUDA_ERROR_OUT_OF_MEMORY",
        CUDA_ERROR_NOT_INITIALIZED => "CUDA_ERROR_NOT_INITIALIZED",
        CUDA_ERROR_DEINITIALIZED => "CUDA_ERROR_DEINITIALIZED",
        CUDA_ERROR_NO_DEVICE => "CUDA_ERROR_NO_DEVICE",
        CUDA_ERROR_INVALID_DEVICE => "CUDA_ERROR_INVALID_DEVICE",
        CUDA_ERROR_INVALID_PTX => "CUDA_ERROR_INVALID_PTX",
        CUDA_ERROR_INVALID_HANDLE => "CUDA_ERROR_INVALID_HANDLE",
        _ => "CUDA_ERROR_UNKNOWN",
    }
}

// Device attribute constants
pub const CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR: c_int = 75;
pub const CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR: c_int = 76;
pub const CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_BLOCK: c_int = 1;
pub const CU_DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_BLOCK: c_int = 8;
```

**Falsification Tests:**
```rust
#[test]
fn test_cuda_types_sizes() {
    // Falsify: Types must match CUDA driver ABI
    assert_eq!(std::mem::size_of::<CUdevice>(), std::mem::size_of::<c_int>());
    assert_eq!(std::mem::size_of::<CUdeviceptr>(), std::mem::size_of::<usize>());
}

#[test]
fn test_error_codes_match_cuda_h() {
    // Falsify: Error codes must match cuda.h definitions
    assert_eq!(CUDA_SUCCESS, 0);
    assert_eq!(CUDA_ERROR_OUT_OF_MEMORY, 2);
    assert_eq!(CUDA_ERROR_NO_DEVICE, 100);
    assert_eq!(CUDA_ERROR_INVALID_PTX, 218);
}

#[test]
#[cfg(feature = "cuda")]
fn falsify_driver_loads() {
    // H1: CUDA driver can be loaded on systems with NVIDIA GPU
    unsafe {
        let driver = CudaDriver::load();
        // This test passes if driver loads OR if no CUDA installed (returns None)
        // It FAILS if loading panics or corrupts memory
        if let Some(drv) = driver {
            let mut count: c_int = 0;
            let result = (drv.cuInit)(0);
            // cuInit may fail if no GPU, but should not crash
            if result == CUDA_SUCCESS {
                (drv.cuDeviceGetCount)(&mut count);
                assert!(count >= 0, "Device count should be non-negative");
            }
        }
    }
}
```

#### 3.2.2 CRT-002: CUDA Context Management

**File:** `src/driver/context.rs`

```rust
//! CUDA Context Management
//!
//! Implements the Primary Context pattern for efficient multi-module usage.
//! Uses OUR OWN FFI from driver/sys.rs - no external dependencies.
//!
//! Design follows Toyota Principle #5 (Jidoka): Build quality in through
//! typestate pattern that prevents invalid operations at compile time.

use crate::driver::sys::*;
use crate::GpuError;
use std::ptr;
use std::sync::Arc;

/// Global CUDA driver instance (loaded once)
static DRIVER: std::sync::OnceLock<Option<CudaDriver>> = std::sync::OnceLock::new();

/// Get or initialize the CUDA driver
fn get_driver() -> Result<&'static CudaDriver, GpuError> {
    let driver = DRIVER.get_or_init(|| unsafe { CudaDriver::load() });
    driver.as_ref().ok_or_else(|| {
        GpuError::CudaNotAvailable("CUDA driver not found (libcuda.so)".to_string())
    })
}

/// CUDA context wrapper with automatic cleanup (RAII)
///
/// Uses Primary Context API for efficient multi-module usage.
/// The context is automatically released when dropped.
pub struct CudaContext {
    device: CUdevice,
    context: CUcontext,
    device_id: i32,
}

// SAFETY: CudaContext can be sent between threads.
// CUDA contexts are thread-safe when using Primary Context API.
unsafe impl Send for CudaContext {}
unsafe impl Sync for CudaContext {}

impl CudaContext {
    /// Initialize CUDA context for device
    ///
    /// # Errors
    /// Returns error if:
    /// - No CUDA driver installed
    /// - Device ordinal out of range
    /// - Device initialization fails
    pub fn new(device_id: i32) -> Result<Self, GpuError> {
        let driver = get_driver()?;

        unsafe {
            // Initialize CUDA (safe to call multiple times)
            CudaDriver::check((driver.cuInit)(0))?;

            // Check device count
            let mut count: i32 = 0;
            CudaDriver::check((driver.cuDeviceGetCount)(&mut count))?;

            if device_id >= count {
                return Err(GpuError::DeviceNotFound(device_id, count as usize));
            }

            // Get device handle
            let mut device: CUdevice = 0;
            CudaDriver::check((driver.cuDeviceGet)(&mut device, device_id))?;

            // Retain primary context (preferred over cuCtxCreate)
            let mut context: CUcontext = ptr::null_mut();
            CudaDriver::check((driver.cuDevicePrimaryCtxRetain)(&mut context, device))?;

            // Set as current context
            CudaDriver::check((driver.cuCtxSetCurrent)(context))?;

            Ok(Self {
                device,
                context,
                device_id,
            })
        }
    }

    /// Get device compute capability (major, minor)
    pub fn compute_capability(&self) -> Result<(u32, u32), GpuError> {
        let driver = get_driver()?;
        unsafe {
            let mut major: i32 = 0;
            let mut minor: i32 = 0;
            CudaDriver::check((driver.cuDeviceGetAttribute)(
                &mut major,
                CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR,
                self.device,
            ))?;
            CudaDriver::check((driver.cuDeviceGetAttribute)(
                &mut minor,
                CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR,
                self.device,
            ))?;
            Ok((major as u32, minor as u32))
        }
    }

    /// Get device memory info (free, total) in bytes
    pub fn memory_info(&self) -> Result<(usize, usize), GpuError> {
        let driver = get_driver()?;
        unsafe {
            let mut free: usize = 0;
            let mut total: usize = 0;
            CudaDriver::check((driver.cuMemGetInfo_v2)(&mut free, &mut total))?;
            Ok((free, total))
        }
    }

    /// Get device ordinal
    pub fn device_id(&self) -> i32 {
        self.device_id
    }

    /// Get raw device handle (for advanced usage)
    pub fn raw_device(&self) -> CUdevice {
        self.device
    }

    /// Get raw context handle (for advanced usage)
    pub fn raw_context(&self) -> CUcontext {
        self.context
    }
}

impl Drop for CudaContext {
    fn drop(&mut self) {
        if let Ok(driver) = get_driver() {
            unsafe {
                // Release primary context
                let _ = (driver.cuDevicePrimaryCtxRelease)(self.device);
            }
        }
    }
}
```

**Falsification Tests:**
```rust
#[test]
#[cfg(feature = "cuda")]
fn falsify_context_creation_succeeds() {
    // H1: CUDA context can be created on device 0
    let result = CudaContext::new(0);
    assert!(result.is_ok(), "Context creation failed: {:?}", result.err());
}

#[test]
#[cfg(feature = "cuda")]
fn falsify_invalid_device_errors() {
    // Negative test: Invalid device should error, not panic
    let result = CudaContext::new(999);
    assert!(result.is_err(), "Expected error for invalid device");
    match result {
        Err(GpuError::DeviceNotFound(999, _)) => (), // Expected
        Err(e) => panic!("Wrong error type: {:?}", e),
        Ok(_) => panic!("Should have failed"),
    }
}

#[test]
#[cfg(feature = "cuda")]
fn falsify_compute_capability_valid() {
    // H1: Compute capability should be >= 7.0 (our minimum target)
    let ctx = CudaContext::new(0).unwrap();
    let (major, minor) = ctx.compute_capability().unwrap();
    assert!(major >= 7, "Compute capability {}.{} < 7.0", major, minor);
}

#[test]
#[cfg(feature = "cuda")]
fn falsify_memory_info_sane() {
    // Memory info should return non-zero values
    let ctx = CudaContext::new(0).unwrap();
    let (free, total) = ctx.memory_info().unwrap();
    assert!(total > 0, "Total memory should be > 0");
    assert!(free <= total, "Free memory should be <= total");
}
```

#### 3.2.3 CRT-003: PTX Module Loading

**File:** `src/driver/module.rs`

```rust
//! PTX Module Loading and JIT Compilation
//!
//! Loads PTX source into GPU-executable modules.
//! Uses OUR OWN FFI from driver/sys.rs - no external dependencies.
//!
//! Citation: NVIDIA CUDA C Programming Guide, Section 3.3 "Modules" [5]

use crate::driver::sys::*;
use crate::driver::context::CudaContext;
use crate::GpuError;
use std::collections::HashMap;
use std::ffi::CString;
use std::ptr;

/// Compiled CUDA module containing kernels
pub struct CudaModule {
    module: CUmodule,
    functions: HashMap<String, CUfunction>,
}

// SAFETY: CUmodule handles are thread-safe for read-only operations
unsafe impl Send for CudaModule {}
unsafe impl Sync for CudaModule {}

impl CudaModule {
    /// Load PTX source and JIT compile to device code
    ///
    /// # Arguments
    /// * `_ctx` - CUDA context (must be current)
    /// * `ptx` - PTX assembly source code (null-terminated)
    ///
    /// # JIT Compilation Notes
    /// The PTX is compiled to SASS (device assembly) at load time.
    /// This incurs one-time cost but enables runtime architecture targeting.
    pub fn from_ptx(_ctx: &CudaContext, ptx: &str) -> Result<Self, GpuError> {
        let driver = super::context::get_driver()?;

        // Ensure PTX is null-terminated
        let ptx_cstring = CString::new(ptx)
            .map_err(|_| GpuError::ModuleLoad("PTX contains null bytes".to_string()))?;

        unsafe {
            let mut module: CUmodule = ptr::null_mut();
            CudaDriver::check((driver.cuModuleLoadData)(
                &mut module,
                ptx_cstring.as_ptr() as *const _,
            ))?;

            Ok(Self {
                module,
                functions: HashMap::new(),
            })
        }
    }

    /// Get kernel function handle by name
    pub fn get_function(&mut self, name: &str) -> Result<CUfunction, GpuError> {
        if let Some(&func) = self.functions.get(name) {
            return Ok(func);
        }

        let driver = super::context::get_driver()?;
        let name_cstring = CString::new(name)
            .map_err(|_| GpuError::FunctionNotFound(name.to_string()))?;

        unsafe {
            let mut func: CUfunction = ptr::null_mut();
            CudaDriver::check((driver.cuModuleGetFunction)(
                &mut func,
                self.module,
                name_cstring.as_ptr(),
            ))?;

            self.functions.insert(name.to_string(), func);
            Ok(func)
        }
    }

    /// Get raw module handle
    pub fn raw(&self) -> CUmodule {
        self.module
    }
}

impl Drop for CudaModule {
    fn drop(&mut self) {
        if let Ok(driver) = super::context::get_driver() {
            unsafe {
                let _ = (driver.cuModuleUnload)(self.module);
            }
        }
    }
}
```

**Falsification Tests:**
```rust
#[test]
#[cfg(feature = "cuda")]
fn falsify_ptx_loading_works() {
    // H1: PTX generated by trueno-gpu can be loaded
    use crate::kernels::{SoftmaxKernel, Kernel};

    let ctx = CudaContext::new(0).unwrap();
    let kernel = SoftmaxKernel::new(128);
    let ptx = kernel.emit_ptx();

    let module = CudaModule::from_ptx(&ctx, &ptx);
    assert!(module.is_ok(), "PTX loading failed: {:?}", module.err());
}

#[test]
#[cfg(feature = "cuda")]
fn falsify_invalid_ptx_errors() {
    // Negative test: Invalid PTX should error gracefully
    let ctx = CudaContext::new(0).unwrap();
    let invalid_ptx = "this is not valid PTX";

    let result = CudaModule::from_ptx(&ctx, invalid_ptx);
    assert!(result.is_err(), "Expected error for invalid PTX");
}

#[test]
#[cfg(feature = "cuda")]
fn falsify_function_lookup_works() {
    use crate::kernels::{SoftmaxKernel, Kernel};

    let ctx = CudaContext::new(0).unwrap();
    let kernel = SoftmaxKernel::new(128);
    let ptx = kernel.emit_ptx();

    let mut module = CudaModule::from_ptx(&ctx, &ptx).unwrap();
    let func = module.get_function(kernel.name());
    assert!(func.is_ok(), "Function lookup failed: {:?}", func.err());
}
```

#### 3.2.4 CRT-004: Async Stream Execution

**File:** `src/driver/stream.rs`

```rust
//! CUDA Stream Management
//!
//! Provides async execution and synchronization primitives.
//! Uses OUR OWN FFI from driver/sys.rs - no external dependencies.
//!
//! Citation: Sourouri et al. [2] demonstrates stream overlap essential for PCIe hiding.

use crate::driver::sys::*;
use crate::driver::context::get_driver;
use crate::GpuError;
use std::ptr;

/// CUDA execution stream with RAII cleanup
///
/// Streams enable:
/// 1. Async H2D/D2H transfers overlapped with compute
/// 2. Concurrent kernel execution (different streams)
/// 3. Ordered execution within a stream
pub struct CudaStream {
    stream: CUstream,
}

// SAFETY: CUstream handles are thread-safe
unsafe impl Send for CudaStream {}
unsafe impl Sync for CudaStream {}

impl CudaStream {
    /// Create a new CUDA stream
    pub fn new() -> Result<Self, GpuError> {
        let driver = get_driver()?;
        unsafe {
            let mut stream: CUstream = ptr::null_mut();
            CudaDriver::check((driver.cuStreamCreate)(&mut stream, 0))?;
            Ok(Self { stream })
        }
    }

    /// Synchronize stream (block until all operations complete)
    pub fn synchronize(&self) -> Result<(), GpuError> {
        let driver = get_driver()?;
        unsafe {
            CudaDriver::check((driver.cuStreamSynchronize)(self.stream))
        }
    }

    /// Get raw stream handle for kernel launches
    pub fn raw(&self) -> CUstream {
        self.stream
    }
}

impl Drop for CudaStream {
    fn drop(&mut self) {
        if let Ok(driver) = get_driver() {
            unsafe {
                let _ = (driver.cuStreamDestroy_v2)(self.stream);
            }
        }
    }
}
```

**Falsification Tests:**
```rust
#[test]
#[cfg(feature = "cuda")]
fn falsify_stream_creation() {
    let _ctx = CudaContext::new(0).unwrap(); // Must have context
    let stream = CudaStream::new();
    assert!(stream.is_ok(), "Stream creation failed");
}

#[test]
#[cfg(feature = "cuda")]
fn falsify_stream_sync_completes() {
    // H4: Stream synchronize returns without deadlock
    let _ctx = CudaContext::new(0).unwrap();
    let stream = CudaStream::new().unwrap();

    let result = stream.synchronize();
    assert!(result.is_ok(), "Stream sync failed: {:?}", result.err());
}

#[test]
#[cfg(feature = "cuda")]
fn falsify_multiple_streams() {
    // H10: Multiple streams can be created and used
    let _ctx = CudaContext::new(0).unwrap();
    let streams: Vec<_> = (0..4)
        .map(|_| CudaStream::new().unwrap())
        .collect();

    for stream in &streams {
        stream.synchronize().unwrap();
    }
}
```

#### 3.2.5 CRT-005: GPU Memory Operations

**File:** `src/driver/memory.rs`

```rust
//! GPU Memory Management
//!
//! Provides allocation, deallocation, and transfer operations.
//! Uses OUR OWN FFI from driver/sys.rs - no external dependencies.
//!
//! Citation: Oden & Fröning [4] show cudaMalloc latency (1-10ms) necessitates pooling.

use crate::driver::sys::*;
use crate::driver::context::get_driver;
use crate::GpuError;
use std::marker::PhantomData;

/// GPU memory allocation with automatic cleanup (RAII)
///
/// Wraps a device pointer with type-safe access and automatic deallocation.
pub struct GpuBuffer<T> {
    ptr: CUdeviceptr,
    len: usize,
    _marker: PhantomData<T>,
}

// SAFETY: GPU memory can be accessed from any thread
unsafe impl<T: Send> Send for GpuBuffer<T> {}
unsafe impl<T: Sync> Sync for GpuBuffer<T> {}

impl<T> GpuBuffer<T> {
    /// Allocate uninitialized GPU memory
    pub fn alloc(len: usize) -> Result<Self, GpuError> {
        let driver = get_driver()?;
        let byte_size = len * std::mem::size_of::<T>();

        unsafe {
            let mut ptr: CUdeviceptr = 0;
            CudaDriver::check((driver.cuMemAlloc_v2)(&mut ptr, byte_size))?;

            Ok(Self {
                ptr,
                len,
                _marker: PhantomData,
            })
        }
    }

    /// Allocate and copy from host
    pub fn from_host(data: &[T]) -> Result<Self, GpuError> {
        let buffer = Self::alloc(data.len())?;
        buffer.copy_from_host(data)?;
        Ok(buffer)
    }

    /// Copy data from host to device
    pub fn copy_from_host(&self, data: &[T]) -> Result<(), GpuError> {
        if data.len() != self.len {
            return Err(GpuError::Transfer(format!(
                "Size mismatch: buffer={}, data={}",
                self.len, data.len()
            )));
        }

        let driver = get_driver()?;
        let byte_size = self.len * std::mem::size_of::<T>();

        unsafe {
            CudaDriver::check((driver.cuMemcpyHtoD_v2)(
                self.ptr,
                data.as_ptr() as *const _,
                byte_size,
            ))
        }
    }

    /// Copy data from device to host
    pub fn to_host(&self) -> Result<Vec<T>, GpuError>
    where
        T: Default + Clone,
    {
        let driver = get_driver()?;
        let byte_size = self.len * std::mem::size_of::<T>();
        let mut result = vec![T::default(); self.len];

        unsafe {
            CudaDriver::check((driver.cuMemcpyDtoH_v2)(
                result.as_mut_ptr() as *mut _,
                self.ptr,
                byte_size,
            ))?;
        }

        Ok(result)
    }

    /// Get length in elements
    pub fn len(&self) -> usize {
        self.len
    }

    /// Check if buffer is empty
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Get size in bytes
    pub fn byte_size(&self) -> usize {
        self.len * std::mem::size_of::<T>()
    }

    /// Get raw device pointer
    pub fn as_ptr(&self) -> CUdeviceptr {
        self.ptr
    }
}

impl<T> Drop for GpuBuffer<T> {
    fn drop(&mut self) {
        if self.ptr != 0 {
            if let Ok(driver) = get_driver() {
                unsafe {
                    let _ = (driver.cuMemFree_v2)(self.ptr);
                }
            }
        }
    }
}
```

**Falsification Tests:**
```rust
#[test]
#[cfg(feature = "cuda")]
fn falsify_memory_roundtrip() {
    // H3: Data survives H2D → D2H roundtrip exactly
    let _ctx = CudaContext::new(0).unwrap();
    let original: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0];

    let gpu_data = GpuBuffer::from_host(&original).unwrap();
    let returned = gpu_data.to_host().unwrap();

    assert_eq!(original, returned, "Memory roundtrip corrupted data");
}

#[test]
#[cfg(feature = "cuda")]
fn falsify_large_allocation() {
    // Test 1GB allocation doesn't panic
    let _ctx = CudaContext::new(0).unwrap();
    let size = 256 * 1024 * 1024; // 256M floats = 1GB

    let result = GpuBuffer::<f32>::alloc(size);
    // Either succeeds or returns OOM error (not panic)
    match result {
        Ok(_) => (),
        Err(GpuError::CudaDriver(msg, _)) if msg.contains("OUT_OF_MEMORY") => (),
        Err(e) => panic!("Unexpected error type: {:?}", e),
    }
}

#[test]
#[cfg(feature = "cuda")]
fn falsify_buffer_size_tracking() {
    let _ctx = CudaContext::new(0).unwrap();
    let buffer = GpuBuffer::<f32>::alloc(100).unwrap();

    assert_eq!(buffer.len(), 100);
    assert_eq!(buffer.byte_size(), 400); // 100 * 4 bytes
    assert!(!buffer.is_empty());
}
```

#### 3.2.6 CRT-006: Kernel Execution

**File:** `src/executor/mod.rs`

```rust
//! Kernel Execution Framework
//!
//! High-level API for launching GPU kernels.
//! Uses OUR OWN FFI from driver/sys.rs - no external dependencies.
//!
//! Citation: FlashAttention [6] demonstrates fused kernel execution
//! reduces memory bandwidth by 5-7x.

use crate::driver::sys::*;
use crate::driver::{CudaContext, CudaModule, CudaStream, GpuBuffer, get_driver};
use crate::GpuError;
use crate::kernels::Kernel;
use std::ffi::c_void;

/// Launch configuration for kernel execution
#[derive(Debug, Clone, Copy)]
pub struct LaunchConfig {
    pub grid: (u32, u32, u32),
    pub block: (u32, u32, u32),
    pub shared_mem: u32,
}

impl LaunchConfig {
    /// Create 1D launch config
    pub fn linear(elements: u32, block_size: u32) -> Self {
        let grid_x = (elements + block_size - 1) / block_size;
        Self {
            grid: (grid_x, 1, 1),
            block: (block_size, 1, 1),
            shared_mem: 0,
        }
    }
}

/// Execute a kernel with the given parameters
pub fn launch_kernel(
    ctx: &CudaContext,
    stream: &CudaStream,
    module: &mut CudaModule,
    kernel_name: &str,
    config: LaunchConfig,
    params: &mut [*mut c_void],
) -> Result<(), GpuError> {
    let driver = get_driver()?;
    let func = module.get_function(kernel_name)?;

    unsafe {
        CudaDriver::check((driver.cuLaunchKernel)(
            func,
            config.grid.0, config.grid.1, config.grid.2,
            config.block.0, config.block.1, config.block.2,
            config.shared_mem,
            stream.raw(),
            params.as_mut_ptr(),
            std::ptr::null_mut(),
        ))
    }
}

/// Execute a kernel from PTX source (convenience wrapper)
pub fn execute_kernel<K: Kernel>(
    ctx: &CudaContext,
    stream: &CudaStream,
    kernel: &K,
    config: LaunchConfig,
    params: &mut [*mut c_void],
) -> Result<(), GpuError> {
    // 1. Generate PTX
    let ptx = kernel.emit_ptx();

    // 2. Load module
    let mut module = CudaModule::from_ptx(ctx, &ptx)?;

    // 3. Launch kernel
    launch_kernel(ctx, stream, &mut module, kernel.name(), config, params)
}
```

**Falsification Tests:**
```rust
#[test]
#[cfg(feature = "cuda")]
fn falsify_gemm_correctness() {
    // H5: GEMM produces mathematically correct results
    use crate::kernels::{GemmKernel, Kernel};

    let ctx = CudaContext::new(0).unwrap();
    let stream = CudaStream::new().unwrap();

    // Small test case: 4x4 matrices
    let a: Vec<f32> = vec![
        1.0, 2.0, 3.0, 4.0,
        5.0, 6.0, 7.0, 8.0,
        9.0, 10.0, 11.0, 12.0,
        13.0, 14.0, 15.0, 16.0,
    ];
    let b: Vec<f32> = vec![
        1.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0,
        0.0, 0.0, 0.0, 1.0,
    ]; // Identity matrix

    // A × I = A
    let expected = a.clone();

    let gpu_a = GpuBuffer::from_host(&a).unwrap();
    let gpu_b = GpuBuffer::from_host(&b).unwrap();
    let gpu_c = GpuBuffer::<f32>::alloc(16).unwrap();

    let kernel = GemmKernel::new(4, 4, 4);
    let config = LaunchConfig::linear(16, 256);

    // Build params array (pointers to device pointers)
    let mut ptr_a = gpu_a.as_ptr();
    let mut ptr_b = gpu_b.as_ptr();
    let mut ptr_c = gpu_c.as_ptr();
    let mut params: Vec<*mut c_void> = vec![
        &mut ptr_a as *mut _ as *mut c_void,
        &mut ptr_b as *mut _ as *mut c_void,
        &mut ptr_c as *mut _ as *mut c_void,
    ];

    execute_kernel(&ctx, &stream, &kernel, config, &mut params).unwrap();
    stream.synchronize().unwrap();

    let result = gpu_c.to_host().unwrap();

    for (i, (r, e)) in result.iter().zip(expected.iter()).enumerate() {
        let diff = (r - e).abs();
        assert!(diff < 1e-3, "GEMM mismatch at {}: {} vs {} (diff={})", i, r, e, diff);
    }
}
```

---

## 4. Complete File List

### 4.1 Files to Create (NO EXTERNAL DEPENDENCIES)

| File | Lines (Est.) | Priority | Description |
|------|--------------|----------|-------------|
| `Cargo.toml` (modify) | +3 | P0 | Add libloading for dynamic loading |
| `src/driver/sys.rs` | ~400 | P0 | **OUR OWN CUDA FFI** |
| `src/driver/context.rs` | ~120 | P0 | CUDA context management |
| `src/driver/module.rs` | ~100 | P0 | PTX loading |
| `src/driver/stream.rs` | ~70 | P0 | Async execution |
| `src/driver/memory.rs` | ~150 | P0 | GPU memory operations |
| `src/executor/mod.rs` | ~100 | P1 | High-level execution API |
| `src/executor/launch.rs` | ~80 | P1 | Kernel launch wrapper |
| `src/memory/pool.rs` | ~200 | P2 | Pool allocator (extend existing) |
| `tests/integration/cuda_e2e.rs` | ~300 | P0 | End-to-end falsification tests |

**Total New Code:** ~1,808 lines

### 4.2 Files to Modify

| File | Changes | Description |
|------|---------|-------------|
| `Cargo.toml` | +3 lines | Add libloading (dynamic library loading) |
| `src/driver/mod.rs` | +30 lines | Export sys, context, module, stream, memory |
| `src/memory/mod.rs` | +30 lines | Wire up allocator |
| `src/lib.rs` | +10 lines | Feature gates for cuda |
| `src/backend/mod.rs` | +20 lines | Wire up CudaBackend.is_available() |

### 4.3 Dependency Philosophy

```
┌─────────────────────────────────────────────────────────────────────────┐
│                    DEPENDENCY AUDIT (V2.0)                               │
├─────────────────────────────────────────────────────────────────────────┤
│                                                                          │
│  REQUIRED (minimal):                                                     │
│    - thiserror (already present) - Error derive macros                  │
│    - libloading (new) - Dynamic library loading for libcuda.so          │
│                                                                          │
│  REJECTED:                                                               │
│    - cudarc - External CUDA bindings (replaced by driver/sys.rs)        │
│    - cuda-sys - Raw FFI (we write our own)                              │
│    - rustacuda - Alternative bindings (unnecessary)                     │
│                                                                          │
│  PHILOSOPHY: We control 100% of the CUDA interface.                     │
│  If libcuda.so changes, WE decide how to adapt.                         │
│                                                                          │
└─────────────────────────────────────────────────────────────────────────┘
```

---

## 5. Peer-Reviewed Citations

### [1] RustBelt: Securing the Foundations of the Rust Programming Language
**Authors:** Jung, R., Jourdan, J. H., Krebbers, R., & Dreyer, D.
**Venue:** Proceedings of the ACM on Programming Languages (POPL), 2017
**DOI:** 10.1145/3158154
**Relevance:** Formally proves that Rust's type system (ownership/borrowing) can safely encapsulate unsafe FFI operations. Validates the strategy of wrapping raw CUDA pointers in RAII structs (`GpuAllocation`, `CudaContext`). Supports Section 3.2.1 design decision to use our own `driver/sys.rs` FFI layer with safe RAII wrappers.

### [2] Effective Multi-GPU Communication Using Multiple CUDA Streams and Threads
**Authors:** Sourouri, M., Gillberg, T., Baden, S. B., & Cai, X.
**Venue:** IEEE International Conference on Parallel and Distributed Systems (ICPADS), 2014
**DOI:** 10.1109/PADSW.2014.7097819
**Relevance:** Demonstrates empirically that overlapping computation with communication via CUDA streams is essential for hiding PCIe latency. Directly supports H4 hypothesis ("Stream overhead is acceptable") and the `CudaStream` design in Section 3.2.4.

### [3] Kernel Fusion: An Effective Method for Better Power Efficiency on Multithreaded GPU
**Authors:** Wang, G., Lin, Y., & Yi, W.
**Venue:** IEEE/ACM International Conference on Green Computing and Communications, 2010
**DOI:** 10.1109/GreenCom-CPSCom.2010.102
**Relevance:** Provides empirical data showing that fusing kernels (reducing global memory roundtrips) significantly lowers energy consumption and improves performance. Validates the "Muda" (waste) elimination strategy in Section 1.2, particularly the "Extra Processing" row justifying fused dequant+compute kernels.

### [4] MallocMC: Allocation of Memory on the GPU
**Authors:** Oden, L., & Fröning, H.
**Venue:** IEEE International Conference on High Performance Computing (HiPC), 2013
**DOI:** 10.1109/HiPC.2013.6799100
**Relevance:** Analyzes the high latency of `cudaMalloc` and `cudaFree` (typically 1-10ms), providing scientific basis for why a pool allocator is not just an optimization, but a **requirement** for high-throughput inference. Supports H8 ("Memory pool reduces allocs") and H12 ("Fragmentation Resistance").

### [5] NVIDIA CUDA C++ Programming Guide v12.3
**Authors:** NVIDIA Corporation
**Venue:** NVIDIA Developer Documentation, 2024
**URL:** https://docs.nvidia.com/cuda/cuda-c-programming-guide/
**Relevance:** The primary source of truth for Driver API vs Runtime API behavior. Supports the decision to target Driver API (via our own `driver/sys.rs` FFI) for finer control over contexts and module loading. Essential reference for PTX JIT compilation semantics (Section 3.2.3).

### [6] FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness
**Authors:** Dao, T., Fu, D., Ermon, S., Rudra, A., & Ré, C.
**Venue:** Advances in Neural Information Processing Systems (NeurIPS), 2022
**DOI:** 10.48550/arXiv.2205.14135
**Relevance:** The foundational paper for IO-aware attention algorithms. Validates the architectural choice in `kernels/attention.rs` (fused attention) and explains why simple matrix multiplication is insufficient for Transformer performance. Supports the "Memory Traffic" reduction strategy in Section 3.2.6.

### [7] QLoRA: Efficient Finetuning of Quantized LLMs
**Authors:** Dettmers, T., Pagnoni, A., Holtzman, A., & Zettlemoyer, L.
**Venue:** Advances in Neural Information Processing Systems (NeurIPS), 2023
**DOI:** 10.48550/arXiv.2305.14314
**Relevance:** Establishes the scientific validity of 4-bit quantization (NormalFloat4) for maintaining LLM accuracy while drastically reducing memory footprint. Directly supports the correctness targets in H7 ("Q4_K dequant is accurate") and the "Extra Processing" waste reduction strategy in Section 1.2.

---

## 6. Implementation Milestones

### 6.1 Phase 1: Foundation (P0)

**Goal:** Execute a simple kernel end-to-end

| Milestone | Deliverable | Falsification Test |
|-----------|-------------|-------------------|
| M1 | CUDA FFI compiles | `cargo build --features cuda` succeeds |
| M2 | Context creates | `CudaContext::new(0)` returns `Ok` |
| M3 | Memory roundtrip | H2D → D2H returns exact input |
| M4 | PTX loads | `CudaModule::from_ptx()` returns `Ok` |
| M5 | Kernel launches | Write pattern, read back, verify |

### 6.2 Phase 2: Kernels (P1)

**Goal:** Execute all existing PTX kernels

| Milestone | Deliverable | Falsification Test |
|-----------|-------------|-------------------|
| M6 | GEMM works | 4×4 identity multiply = input |
| M7 | Softmax works | Sum to 1.0, max element highest |
| M8 | LayerNorm works | Output mean ≈ 0, variance ≈ 1 |
| M9 | Attention works | Compare vs CPU reference |
| M10 | Q4_K works | Compare vs llama.cpp dequant |

### 6.3 Phase 3: Performance (P2)

**Goal:** Match Ollama/llama.cpp performance

| Milestone | Deliverable | Falsification Test |
|-----------|-------------|-------------------|
| M11 | Memory pool | <10 cuMemAlloc calls for 100 ops |
| M12 | Async overlap | H2D overlaps with compute |
| M13 | Multi-stream | Parallel kernels faster |
| M14 | Benchmark parity | Within 2x of llama.cpp |

---

## 7. Risk Analysis

### 7.1 Technical Risks

| Risk | Probability | Impact | Mitigation |
|------|-------------|--------|------------|
| CUDA Driver API changes | Very Low | Medium | Driver API is stable since CUDA 3.0 |
| PTX compatibility | Medium | High | Test on sm_70, sm_80, sm_89 |
| Memory corruption | Medium | Critical | Address sanitizer in tests |
| OOM handling | High | Medium | Graceful degradation |
| Driver version mismatch | Medium | High | Document minimum driver (525.60+) |
| libloading failure | Low | Medium | Graceful fallback to CPU path |

### 7.2 Falsifiable Risk Thresholds

```rust
/// Risk is realized if any of these fail:
const RISK_THRESHOLDS: &[(&str, f64)] = &[
    ("gemm_max_error", 1e-3),      // Numerical precision
    ("attention_max_error", 1e-2), // Attention tolerance
    ("memory_leak_bytes", 0.0),    // Zero tolerance for leaks
    ("kernel_launch_failures", 0.0), // Zero launch failures
    ("sync_deadlock_timeout_ms", 5000.0), // 5 second max wait
];
```

---

## 8. Acceptance Criteria

### 8.1 Specification Approval

- [ ] All 13 falsifiable hypotheses (H1-H13) have test implementations
- [ ] All 7 citations are peer-reviewed or authoritative technical docs
- [ ] Toyota Way principles are traceable to design decisions
- [ ] No SATD (Self-Admitted Technical Debt) in specification
- [ ] Memory safety verified (no raw pointers without RAII wrapper)
- [ ] Crucial Experiments (H11-H13) have quantitative thresholds defined

### 8.2 Implementation Approval

- [ ] `cargo build --features cuda` succeeds
- [ ] All falsification tests pass
- [ ] `cargo clippy --features cuda -- -D warnings` clean
- [ ] Coverage > 80% on new code
- [ ] No `unsafe` blocks outside `driver/` module

---

## Appendix A: Current Source Audit

### A.1 Existing TODOs (Technical Debt)

```
src/driver/mod.rs:30:        // TODO: Check for cuInit success
src/backend/mod.rs:35:       // TODO: Query actual device count
src/memory/mod.rs:64:        // TODO: Implement with CUDA feature
src/memory/mod.rs:70:        // TODO: Implement with CUDA feature
```

**Resolution:** All 4 TODOs will be eliminated by this specification's implementation.

### A.2 Line Count Summary

| Module | Current Lines | After Implementation |
|--------|--------------|---------------------|
| `driver/` | 271 | ~680 |
| `memory/` | 412 | ~700 |
| `kernels/` | 2,733 | 2,733 (unchanged) |
| `executor/` | 0 | ~500 |
| `ptx/` | 2,136 | 2,136 (unchanged) |
| **Total** | 5,552 | ~6,769 |

---

## Appendix B: Glossary

| Term | Definition |
|------|------------|
| **PTX** | Parallel Thread Execution - NVIDIA's intermediate assembly |
| **SASS** | Shader Assembly - GPU-specific machine code |
| **JIT** | Just-In-Time compilation (PTX → SASS at load time) |
| **H2D** | Host-to-Device memory transfer |
| **D2H** | Device-to-Host memory transfer |
| **Poka-Yoke** | Japanese for "mistake-proofing" (Toyota term) |
| **Muda** | Japanese for "waste" (Toyota term) |
| **Genchi Genbutsu** | Japanese for "go and see" (observe actual conditions) |
| **SATD** | Self-Admitted Technical Debt |

---

**Document Version:** 2.0.0
**Last Updated:** 2025-12-14
**Author:** Claude Code
**Review Status:** POST-DESIGN REVIEW - AWAITING FINAL APPROVAL
**Philosophy:** OWN THE STACK - Zero External Dependencies

---

## Changelog

### V2.0.0 (2025-12-14) - **MAJOR: OWN THE STACK**
- **REMOVED:** cudarc dependency (external CUDA bindings by Corey Lowman)
- **ADDED:** CRT-001 `driver/sys.rs` - Our own CUDA FFI (~400 lines hand-written)
- **ADDED:** Philosophy section: "OWN THE STACK - Zero External Dependencies"
- **CHANGED:** All components now use our own FFI instead of cudarc
- **CHANGED:** Dependency from `cudarc` to `libloading` (dynamic library loading only)
- **CHANGED:** Citations [1], [5] updated to reference our own FFI, not cudarc
- **RATIONALE:** We built 5,500 lines of PTX generation from scratch. We can build
  400 lines of CUDA FFI. Total control of the stack, no third-party surprises.
- **UPDATED:** Risk analysis to reflect CUDA Driver API stability (not cudarc API)
- **UPDATED:** Implementation milestones to reference "CUDA FFI" not "cudarc"
- **REDUCED:** Estimated new code from 1,855 to 1,520 lines (sys.rs replaces abstraction)

### V1.2.0 (2025-12-14)
- **Added:** Citation [6] FlashAttention (Dao et al.)
- **Added:** Citation [7] QLoRA (Dettmers et al.) for quantization context
- **Fixed:** Mismatched citation index in Section 3.2.6
- **Updated:** Acceptance criteria to require all 7 citations
- **Refined:** Aligned FlashAttention and Quantization descriptions with new citations

### V1.1.0 (2025-12-14)
- **Added:** Section 2.2 "Design Review Critiques" addressing Happy Path Trap, Black Swan of Latency, Memory Fragmentation
- **Added:** Section 2.4 "Crucial Experiments" with H11, H12, H13 killer tests
- **Added:** Full implementation code for H11 (Zero-Cost Safety), H12 (Fragmentation Resistance), H13 (Concurrency Safety)
- **Changed:** H4 threshold from "Total time < sequential" to "Overhead ≤ 2µs per launch"
- **Changed:** Citations [1]-[4] replaced with proper peer-reviewed academic papers
- **Changed:** Acceptance criteria updated for 13 hypotheses

### V1.0.0 (2025-12-14)
- Initial specification draft