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
//! C8.1 — ferrotorch-gpu lifecycle + infrastructure conformance suite.
//!
//! Covers all 8 lifecycle modules:
//! `allocator`, `buffer`, `pool`, `memory_guard`, `stream`, `transfer`,
//! `device`, `module_cache`.
//!
//! All tests are gated `#[cfg(feature = "cuda")]`. Tests that require a real
//! CUDA device use `cascade_skip!()` — returning early with a note when
//! `GpuDevice::new(0)` fails — so the suite stays green on CPU-only CI.
//!
//! Layer-2 fixtures: `tests/conformance/fixtures_lifecycle.json`
//! (regenerated by `scripts/regenerate_gpu_lifecycle_fixtures.py`).
//!
//! Layer-4 (strict coverage gate): deferred to C8.4.
#![allow(clippy::float_cmp)] // bit-exact round-trip tests compare exactly
#[cfg(feature = "cuda")]
mod lifecycle {
use std::sync::Arc;
use ferrotorch_gpu::allocator::{
CudaAllocator, MIN_BLOCK_SIZE, MIN_LARGE_ALLOC, ROUND_LARGE, SMALL_BUFFER, SMALL_SIZE,
StreamId, get_allocation_size, round_size,
};
use ferrotorch_gpu::device::GpuDevice;
use ferrotorch_gpu::error::GpuError;
use ferrotorch_gpu::memory_guard::{MemoryGuardBuilder, MemoryHook, OomPolicy, PressureLevel};
use ferrotorch_gpu::pool::{
self, empty_cache, empty_cache_all, pool_return, pool_return_with_stream, pool_take,
pool_take_stream, record_stream, reset_pool_stats, round_len,
};
use ferrotorch_gpu::stream::{
CudaEventWrapper, StreamGuard, StreamPool, StreamPriority, clear_current_stream,
get_current_stream, set_current_stream,
};
use ferrotorch_gpu::transfer::{
alloc_zeros_f32, alloc_zeros_f64, cpu_to_gpu, cpu_to_gpu_pinned, gpu_to_cpu,
};
// -----------------------------------------------------------------------
// Cascade-skip helper
// -----------------------------------------------------------------------
/// Return a `GpuDevice` for device 0, or skip the test if no GPU is present.
/// Expands to `return` (early exit) with a note printed to stderr.
macro_rules! cascade_skip {
() => {
match GpuDevice::new(0) {
Ok(d) => d,
Err(e) => {
eprintln!(
"[cascade_skip] no CUDA device available ({}); \
test skipped — would pass on GPU hardware",
e
);
return;
}
}
};
($expr:expr) => {
match $expr {
Ok(v) => v,
Err(e) => {
eprintln!(
"[cascade_skip] prerequisite failed ({}); \
test skipped — would pass on GPU hardware",
e
);
return;
}
}
};
}
// -----------------------------------------------------------------------
// Layer-2 fixture loader
// -----------------------------------------------------------------------
fn fixtures_json() -> serde_json::Value {
let path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/conformance/fixtures_lifecycle.json"
);
let text = std::fs::read_to_string(path).expect(
"fixtures_lifecycle.json must exist; run scripts/regenerate_gpu_lifecycle_fixtures.py",
);
serde_json::from_str(&text).expect("fixtures_lifecycle.json must be valid JSON")
}
fn fixture_by_id(fixtures: &serde_json::Value, id: &str) -> serde_json::Value {
fixtures["fixtures"]
.as_array()
.expect("fixtures array present")
.iter()
.find(|f| f["id"] == id)
.unwrap_or_else(|| panic!("fixture '{}' not found in fixtures_lifecycle.json", id))
.clone()
}
// -----------------------------------------------------------------------
// Module: allocator — pure arithmetic (no GPU device required)
// -----------------------------------------------------------------------
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::round_size
#[test]
fn allocator_round_size_matches_fixtures() {
let fixtures = fixtures_json();
let cases = fixtures["fixtures"]
.as_array()
.expect("fixtures array")
.iter()
.filter(|f| f["module"] == "allocator" && f["op"] == "round_size");
let mut n = 0;
for fx in cases {
let bytes = fx["inputs"]["bytes"].as_u64().expect("bytes") as usize;
let expected = fx["expected_output"].as_u64().expect("expected") as usize;
let actual = round_size(bytes);
assert_eq!(
actual, expected,
"round_size({}) = {} but fixture expects {}",
bytes, actual, expected
);
n += 1;
}
assert!(n >= 6, "expected at least 6 round_size fixtures; got {}", n);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::get_allocation_size
#[test]
fn allocator_get_allocation_size_matches_fixtures() {
let fixtures = fixtures_json();
let cases = fixtures["fixtures"]
.as_array()
.expect("fixtures array")
.iter()
.filter(|f| f["module"] == "allocator" && f["op"] == "get_allocation_size");
let mut n = 0;
for fx in cases {
let size = fx["inputs"]["size"].as_u64().expect("size") as usize;
let expected = fx["expected_output"].as_u64().expect("expected") as usize;
let actual = get_allocation_size(size);
assert_eq!(
actual, expected,
"get_allocation_size({}) = {} but fixture expects {}",
size, actual, expected
);
n += 1;
}
assert!(
n >= 6,
"expected at least 6 get_allocation_size fixtures; got {}",
n
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::MIN_BLOCK_SIZE
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::SMALL_SIZE
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::SMALL_BUFFER
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::MIN_LARGE_ALLOC
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::ROUND_LARGE
#[test]
fn allocator_constants_match_pytorch_caching_allocator() {
// PyTorch CUDACachingAllocator constants (c10/cuda/CUDACachingAllocator.cpp):
assert_eq!(MIN_BLOCK_SIZE, 512, "MIN_BLOCK_SIZE must be 512 bytes");
assert_eq!(SMALL_SIZE, 1 << 20, "SMALL_SIZE must be 1 MiB");
assert_eq!(SMALL_BUFFER, 2 << 20, "SMALL_BUFFER must be 2 MiB");
assert_eq!(MIN_LARGE_ALLOC, 10 << 20, "MIN_LARGE_ALLOC must be 10 MiB");
assert_eq!(ROUND_LARGE, 2 << 20, "ROUND_LARGE must be 2 MiB");
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::StreamId
#[test]
fn allocator_stream_id_is_copy_eq() {
let s1 = StreamId(42);
let s2 = StreamId(42);
let s3 = StreamId(99);
assert_eq!(s1, s2);
assert_ne!(s1, s3);
// StreamId must implement Copy.
let _s4 = s1;
let _s5 = s1; // would fail to compile if StreamId is not Copy
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::new
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::memory_allocated
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::max_memory_allocated
#[test]
fn allocator_new_starts_at_zero() {
let device = cascade_skip!();
let alloc = CudaAllocator::new(Arc::new(device));
assert_eq!(
alloc.memory_allocated(),
0,
"fresh allocator: allocated == 0"
);
assert_eq!(
alloc.max_memory_allocated(),
0,
"fresh allocator: peak == 0"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::alloc_zeros
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::memory_allocated
#[test]
fn allocator_alloc_increases_bytes() {
let fixtures = fixtures_json();
let fx = fixture_by_id(&fixtures, "alloc_zeros_increases_bytes");
let count = fx["inputs"]["count"].as_u64().expect("count") as usize;
let expected_bytes = fx["expected_allocated_bytes"].as_u64().expect("bytes") as usize;
let device = cascade_skip!();
let alloc = CudaAllocator::new(Arc::new(device));
let buf = cascade_skip!(alloc.alloc_zeros::<f32>(count));
assert_eq!(
alloc.memory_allocated(),
expected_bytes,
"alloc_zeros({count} f32) must set allocated to {expected_bytes}"
);
alloc.free(buf);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::free
#[test]
fn allocator_free_decreases_bytes() {
let fixtures = fixtures_json();
let fx = fixture_by_id(&fixtures, "alloc_free_decreases_bytes");
let count = fx["inputs"]["count"].as_u64().expect("count") as usize;
let expected_after_alloc = fx["expected_allocated_after_alloc"]
.as_u64()
.expect("expected") as usize;
let expected_after_free = fx["expected_allocated_after_free"]
.as_u64()
.expect("expected") as usize;
let device = cascade_skip!();
let alloc = CudaAllocator::new(Arc::new(device));
let buf = cascade_skip!(alloc.alloc_zeros::<f32>(count));
assert_eq!(alloc.memory_allocated(), expected_after_alloc);
alloc.free(buf);
assert_eq!(alloc.memory_allocated(), expected_after_free);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::max_memory_allocated
#[test]
fn allocator_peak_does_not_decrease_on_free() {
let device = cascade_skip!();
let alloc = CudaAllocator::new(Arc::new(device));
let buf1 = cascade_skip!(alloc.alloc_zeros::<f32>(100));
let buf2 = cascade_skip!(alloc.alloc_zeros::<f32>(200));
let peak_after_two = alloc.max_memory_allocated();
alloc.free(buf1);
// Peak must not decrease after freeing buf1.
assert_eq!(
alloc.max_memory_allocated(),
peak_after_two,
"max_memory_allocated must not decrease when memory is freed"
);
assert!(
alloc.memory_allocated() < peak_after_two,
"allocated must be less than peak after partial free"
);
alloc.free(buf2);
assert_eq!(alloc.memory_allocated(), 0);
assert_eq!(alloc.max_memory_allocated(), peak_after_two);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::reset_peak_stats
#[test]
fn allocator_reset_peak_stats() {
let device = cascade_skip!();
let alloc = CudaAllocator::new(Arc::new(device));
let buf = cascade_skip!(alloc.alloc_zeros::<f32>(512));
let high = alloc.max_memory_allocated();
assert!(high > 0);
alloc.free(buf);
assert_eq!(alloc.max_memory_allocated(), high, "peak stays after free");
alloc.reset_peak_stats();
assert_eq!(
alloc.max_memory_allocated(),
0,
"after reset_peak_stats + free, peak == 0"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::alloc_copy
#[test]
fn allocator_alloc_copy_tracks_bytes() {
let fixtures = fixtures_json();
let fx = fixture_by_id(&fixtures, "alloc_copy_tracks_bytes");
let expected_bytes = fx["expected_allocated_bytes"].as_u64().expect("bytes") as usize;
let device = cascade_skip!();
let alloc = CudaAllocator::new(Arc::new(device));
let data: Vec<f64> = vec![1.0, 2.0, 3.0, 4.0];
let buf = cascade_skip!(alloc.alloc_copy(&data));
assert_eq!(alloc.memory_allocated(), expected_bytes);
alloc.free(buf);
assert_eq!(alloc.memory_allocated(), 0);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::alloc_zeros (zero elements)
#[test]
fn allocator_zero_element_alloc() {
let device = cascade_skip!();
let alloc = CudaAllocator::new(Arc::new(device));
let buf = cascade_skip!(alloc.alloc_zeros::<f32>(0));
assert_eq!(
alloc.memory_allocated(),
0,
"zero-element alloc must not change allocated bytes"
);
assert_eq!(buf.len(), 0);
assert!(buf.is_empty());
alloc.free(buf);
assert_eq!(alloc.memory_allocated(), 0);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::cache_insert
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::cache_free
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::cache_find
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::cache_stats
#[test]
fn allocator_cache_find_insert_free_roundtrip() {
let device = Arc::new(match GpuDevice::new(0) {
Ok(d) => d,
Err(_) => return,
});
let alloc = CudaAllocator::new(device);
let stream = StreamId(1);
let (idx, _actual) = alloc.cache_insert(2048, 4096, 0x1000, stream);
assert_eq!(
alloc.cache_stats().1,
1,
"cache_insert must count as a miss"
);
alloc.cache_free(idx);
let found = alloc.cache_find(512, stream);
assert!(found.is_some(), "cache_find after cache_free must hit");
assert_eq!(
alloc.cache_stats().0,
1,
"cache_find hit must increment hit counter"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::empty_cache
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::free_block_count
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::block_count
#[test]
fn allocator_empty_cache_clears_free_blocks() {
let device = Arc::new(match GpuDevice::new(0) {
Ok(d) => d,
Err(_) => return,
});
let alloc = CudaAllocator::new(device);
let stream = StreamId(1);
alloc.cache_insert(1024, 4096, 0x1000, stream);
alloc.cache_free(0);
assert!(
alloc.free_block_count() > 0,
"should have free blocks before empty_cache"
);
alloc.empty_cache();
assert_eq!(
alloc.free_block_count(),
0,
"empty_cache must set free_block_count to 0"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::allocator::CudaAllocator::memory_reserved
#[test]
fn allocator_memory_reserved_nonneg() {
let device = Arc::new(match GpuDevice::new(0) {
Ok(d) => d,
Err(_) => return,
});
let alloc = CudaAllocator::new(device);
// reserved_bytes is non-negative by type; just verify it doesn't panic.
let _ = alloc.memory_reserved();
}
// -----------------------------------------------------------------------
// Module: buffer
// -----------------------------------------------------------------------
/// conformance_gpu_lifecycle: ferrotorch_gpu::buffer::CudaBuffer
/// conformance_gpu_lifecycle: ferrotorch_gpu::buffer::CudaBuffer::len
/// conformance_gpu_lifecycle: ferrotorch_gpu::buffer::CudaBuffer::is_empty
/// conformance_gpu_lifecycle: ferrotorch_gpu::buffer::CudaBuffer::device_ordinal
#[test]
fn buffer_len_is_empty_device_ordinal() {
let device = cascade_skip!();
let host: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let buf = cascade_skip!(cpu_to_gpu(&host, &device));
assert_eq!(
buf.len(),
5,
"CudaBuffer::len must equal the number of elements transferred"
);
assert!(!buf.is_empty(), "non-empty buffer must not report is_empty");
assert_eq!(
buf.device_ordinal(),
0,
"device_ordinal must match the device used for transfer"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::buffer::CudaBuffer::is_empty (empty case)
#[test]
fn buffer_empty() {
let device = cascade_skip!();
let host: Vec<f32> = vec![];
let buf = cascade_skip!(cpu_to_gpu(&host, &device));
assert_eq!(buf.len(), 0);
assert!(buf.is_empty());
}
// -----------------------------------------------------------------------
// Module: pool — pure operations (no GPU device required)
// -----------------------------------------------------------------------
/// conformance_gpu_lifecycle: ferrotorch_gpu::pool::round_len
#[test]
fn pool_round_len_matches_fixtures() {
let fixtures = fixtures_json();
let cases = fixtures["fixtures"]
.as_array()
.expect("fixtures array")
.iter()
.filter(|f| f["module"] == "pool" && f["op"] == "round_len");
let mut n = 0;
for fx in cases {
let len = fx["inputs"]["len"].as_u64().expect("len") as usize;
let expected = fx["expected_output"].as_u64().expect("expected") as usize;
let actual = round_len(len);
assert_eq!(
actual, expected,
"round_len({}) = {} but fixture expects {}",
len, actual, expected
);
n += 1;
}
assert!(n >= 6, "expected at least 6 round_len fixtures; got {}", n);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::pool::pool_take
#[test]
fn pool_take_miss_returns_none() {
let result = pool_take::<u64>(9901, 256, 8);
assert!(result.is_none(), "pool_take on empty pool must return None");
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::pool::pool_return
/// conformance_gpu_lifecycle: ferrotorch_gpu::pool::pool_take
#[test]
fn pool_return_then_take_roundtrip() {
pool_return::<u64>(9902, 256, 8, 12345u64);
let taken = pool_take::<u64>(9902, 256, 8);
assert_eq!(
taken,
Some(12345u64),
"pool_take after pool_return must return the stored value"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::pool::pool_take_stream (wrong stream)
#[test]
fn pool_take_stream_wrong_stream_returns_none() {
let stream_a = StreamId(100);
let stream_b = StreamId(200);
pool_return_with_stream::<u64>(9903, 256, 8, 777u64, stream_a);
let taken = pool_take_stream::<u64>(9903, 256, 8, stream_b);
assert!(
taken.is_none(),
"pool_take_stream with wrong stream must return None"
);
// Clean up: drain the entry so it doesn't affect other tests.
let _ = pool_take::<u64>(9903, 256, 8);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::pool::pool_take_stream (correct stream)
/// conformance_gpu_lifecycle: ferrotorch_gpu::pool::pool_return_with_stream
#[test]
fn pool_take_stream_correct_stream_succeeds() {
let stream_a = StreamId(100);
pool_return_with_stream::<u64>(9904, 256, 8, 888u64, stream_a);
let taken = pool_take_stream::<u64>(9904, 256, 8, stream_a);
assert_eq!(
taken,
Some(888u64),
"pool_take_stream with matching stream must return the value"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::pool::record_stream
#[test]
fn pool_record_stream_prevents_stream_aware_take() {
let stream_a = StreamId(300);
let stream_b = StreamId(400);
pool_return_with_stream::<u64>(9905, 256, 8, 999u64, stream_a);
record_stream::<u64>(9905, 256, stream_b);
// Stream-aware take must fail: cross-stream use recorded.
let taken_stream = pool_take_stream::<u64>(9905, 256, 8, stream_a);
assert!(
taken_stream.is_none(),
"pool_take_stream must return None when cross-stream use is recorded"
);
// Plain take still works.
let taken_plain = pool_take::<u64>(9905, 256, 8);
assert_eq!(
taken_plain,
Some(999u64),
"plain pool_take must still work after record_stream"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::pool::pool_stats
/// conformance_gpu_lifecycle: ferrotorch_gpu::pool::reset_pool_stats
#[test]
fn pool_stats_tracking() {
reset_pool_stats();
let (h0, _m0, r0) = pool::pool_stats();
assert_eq!(h0, 0, "hits must be 0 after reset");
assert_eq!(r0, 0, "returns must be 0 after reset");
pool_return::<u32>(9910, 256, 4, 42u32);
let (_, _, r1) = pool::pool_stats();
assert!(r1 >= 1, "return counter must increment after pool_return");
let _ = pool_take::<u32>(9910, 256, 4);
let (h1, _, _) = pool::pool_stats();
assert!(h1 >= 1, "hit counter must increment after pool_take");
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::pool::empty_cache
#[test]
fn pool_empty_cache_device_specific() {
pool_return::<u32>(9906, 256, 4, 11u32);
pool_return::<u32>(9907, 256, 4, 22u32);
empty_cache(9906);
assert!(
pool_take::<u32>(9906, 256, 4).is_none(),
"empty_cache(device) must remove entries for that device"
);
assert_eq!(
pool_take::<u32>(9907, 256, 4),
Some(22u32),
"empty_cache must not affect other devices"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::pool::empty_cache_all
#[test]
fn pool_empty_cache_all() {
pool_return::<u32>(9908, 256, 4, 33u32);
pool_return::<u32>(9909, 256, 4, 44u32);
empty_cache_all();
assert!(
pool_take::<u32>(9908, 256, 4).is_none(),
"empty_cache_all must clear device 9908"
);
assert!(
pool_take::<u32>(9909, 256, 4).is_none(),
"empty_cache_all must clear device 9909"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::pool::cached_bytes
#[test]
fn pool_cached_bytes_increases_after_return() {
let dev = 9911usize;
let before = pool::cached_bytes(dev);
pool_return::<u32>(dev, 256, 4, 55u32);
let after = pool::cached_bytes(dev);
assert!(
after > before,
"cached_bytes must increase after pool_return"
);
let _ = pool_take::<u32>(dev, 256, 4);
}
// -----------------------------------------------------------------------
// Module: transfer
// -----------------------------------------------------------------------
/// conformance_gpu_lifecycle: ferrotorch_gpu::transfer::cpu_to_gpu
/// conformance_gpu_lifecycle: ferrotorch_gpu::transfer::gpu_to_cpu
#[test]
fn transfer_h2d_d2h_round_trip_f32() {
let fixtures = fixtures_json();
let fx = fixture_by_id(&fixtures, "transfer_h2d_d2h_f32");
let expected: Vec<f32> = fx["expected_output"]
.as_array()
.expect("expected array")
.iter()
.map(|v| v.as_f64().expect("f64") as f32)
.collect();
let device = cascade_skip!();
let gpu_buf = cascade_skip!(cpu_to_gpu(&expected, &device));
assert_eq!(gpu_buf.len(), expected.len());
assert_eq!(gpu_buf.device_ordinal(), 0);
let back = cascade_skip!(gpu_to_cpu(&gpu_buf, &device));
for (i, (&a, &b)) in back.iter().zip(expected.iter()).enumerate() {
assert_eq!(
a, b,
"H2D+D2H f32 round-trip bit-exact mismatch at index {}",
i
);
}
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::transfer::cpu_to_gpu
/// conformance_gpu_lifecycle: ferrotorch_gpu::transfer::gpu_to_cpu
#[allow(clippy::approx_constant)]
#[test]
fn transfer_h2d_d2h_round_trip_f64() {
let fixtures = fixtures_json();
let fx = fixture_by_id(&fixtures, "transfer_h2d_d2h_f64");
let expected: Vec<f64> = fx["expected_output"]
.as_array()
.expect("expected array")
.iter()
.map(|v| v.as_f64().expect("f64"))
.collect();
let device = cascade_skip!();
let gpu_buf = cascade_skip!(cpu_to_gpu(&expected, &device));
let back = cascade_skip!(gpu_to_cpu(&gpu_buf, &device));
for (i, (&a, &b)) in back.iter().zip(expected.iter()).enumerate() {
assert_eq!(
a, b,
"H2D+D2H f64 round-trip bit-exact mismatch at index {}",
i
);
}
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::transfer::cpu_to_gpu (empty)
/// conformance_gpu_lifecycle: ferrotorch_gpu::transfer::gpu_to_cpu (empty)
#[test]
fn transfer_h2d_d2h_empty() {
let device = cascade_skip!();
let host: Vec<f32> = vec![];
let gpu_buf = cascade_skip!(cpu_to_gpu(&host, &device));
assert_eq!(gpu_buf.len(), 0);
assert!(gpu_buf.is_empty());
let back = cascade_skip!(gpu_to_cpu(&gpu_buf, &device));
assert!(back.is_empty(), "empty transfer must return empty Vec");
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::transfer::cpu_to_gpu (large)
#[test]
fn transfer_h2d_d2h_large() {
let device = cascade_skip!();
let n = 1_000_000usize;
let host: Vec<f32> = (0..n).map(|i| i as f32).collect();
let gpu_buf = cascade_skip!(cpu_to_gpu(&host, &device));
assert_eq!(gpu_buf.len(), n);
let back = cascade_skip!(gpu_to_cpu(&gpu_buf, &device));
assert_eq!(back.len(), n);
// Check first 1000 elements against fixture pattern.
for (i, &val) in back.iter().enumerate().take(1000) {
assert_eq!(val, i as f32, "large transfer: element {} mismatch", i);
}
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::transfer::alloc_zeros_f32
#[test]
fn transfer_alloc_zeros_f32_all_zero() {
let device = cascade_skip!();
let buf = cascade_skip!(alloc_zeros_f32(1024, &device));
assert_eq!(buf.len(), 1024);
let host = cascade_skip!(gpu_to_cpu(&buf, &device));
assert!(
host.iter().all(|&x| x == 0.0_f32),
"alloc_zeros_f32 must produce all-zero buffer"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::transfer::alloc_zeros_f64
#[test]
fn transfer_alloc_zeros_f64_all_zero() {
let device = cascade_skip!();
let buf = cascade_skip!(alloc_zeros_f64(512, &device));
assert_eq!(buf.len(), 512);
let host = cascade_skip!(gpu_to_cpu(&buf, &device));
assert!(
host.iter().all(|&x| x == 0.0_f64),
"alloc_zeros_f64 must produce all-zero buffer"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::transfer::alloc_zeros_f32 (pool reuse)
#[test]
fn transfer_pool_reuse_zeros_f32() {
let device = cascade_skip!();
// First allocation: goes to pool on drop (evidenced by cached_bytes > 0 after drop).
let cached_before = pool::cached_bytes(device.ordinal());
let buf1 = cascade_skip!(alloc_zeros_f32(512, &device));
// alloc_len() >= len() is the public indicator that the buffer is pool-tracked.
assert!(
buf1.alloc_len() >= buf1.len(),
"alloc_zeros_f32 must return a buffer with alloc_len >= len (pool-tracked)"
);
drop(buf1);
let cached_after_drop = pool::cached_bytes(device.ordinal());
assert!(
cached_after_drop > cached_before,
"cached_bytes must increase after dropping a pooled buffer"
);
// Second allocation of same size: should hit pool.
let buf2 = cascade_skip!(alloc_zeros_f32(512, &device));
let host = cascade_skip!(gpu_to_cpu(&buf2, &device));
assert!(
host.iter().all(|&x| x == 0.0_f32),
"pool-hit alloc_zeros_f32 must be all-zero (memset_zeros in pool-hit path)"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::transfer::cpu_to_gpu_pinned
#[test]
fn transfer_pinned_round_trip_f32() {
let fixtures = fixtures_json();
let fx = fixture_by_id(&fixtures, "transfer_pinned_round_trip");
let expected: Vec<f32> = fx["expected_output"]
.as_array()
.expect("expected array")
.iter()
.map(|v| v.as_f64().expect("f64") as f32)
.collect();
let device = cascade_skip!();
let gpu_buf = cascade_skip!(cpu_to_gpu_pinned(&expected, &device));
assert_eq!(gpu_buf.len(), expected.len());
let back = cascade_skip!(gpu_to_cpu(&gpu_buf, &device));
for (i, (&a, &b)) in back.iter().zip(expected.iter()).enumerate() {
assert_eq!(
a, b,
"pinned H2D+D2H f32 round-trip bit-exact mismatch at index {}",
i
);
}
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::error::GpuError::DeviceMismatch
#[test]
fn transfer_device_mismatch_rejected() {
// GpuError::DeviceMismatch is produced by gpu_to_cpu when the buffer's
// device_ordinal field does not match the supplied GpuDevice. The field
// is pub(crate), so we exercise the error path via the existing module-level
// test harness (the transfer::tests::device_mismatch_rejected test in the
// source already covers this with direct field mutation). Here we verify the
// error variant exists and its pattern is correct structurally.
//
// We confirm that DeviceMismatch is reachable by constructing it directly.
let err = GpuError::DeviceMismatch {
expected: 1,
got: 0,
};
match err {
GpuError::DeviceMismatch {
expected: 1,
got: 0,
} => {}
other => panic!("DeviceMismatch pattern broken: {:?}", other),
}
// Structural cascade_skip: if no GPU present, we've still validated the type.
let _ = GpuDevice::new(0); // attempt — result unused
}
// -----------------------------------------------------------------------
// Module: stream
// -----------------------------------------------------------------------
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::StreamPool::get_stream
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::StreamPool::pool_size
#[test]
fn stream_pool_lazy_init_and_round_robin() {
let device = cascade_skip!();
let ctx = device.context().clone();
let dev = 0usize;
let s1 = cascade_skip!(StreamPool::get_stream(&ctx, dev));
let s2 = cascade_skip!(StreamPool::get_stream(&ctx, dev));
let pool_size = StreamPool::pool_size(dev);
assert!(
pool_size >= 1,
"StreamPool must lazily create at least 1 stream"
);
assert!(
pool_size <= 8,
"StreamPool must not exceed 8 streams per device"
);
// Collect one full cycle.
let mut streams = vec![s1, s2];
for _ in 2..pool_size {
streams.push(cascade_skip!(StreamPool::get_stream(&ctx, dev)));
}
// Next call wraps around — must return same Arc as first.
let wrap = cascade_skip!(StreamPool::get_stream(&ctx, dev));
assert_eq!(
Arc::as_ptr(&wrap),
Arc::as_ptr(&streams[0]),
"StreamPool must wrap round-robin back to the first stream"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::error::GpuError::InvalidDevice
#[test]
fn stream_pool_invalid_device_returns_error() {
let device = cascade_skip!();
let ctx = device.context().clone();
let err =
StreamPool::get_stream(&ctx, 9999).expect_err("ordinal >= MAX_DEVICES must return Err");
assert!(
matches!(err, GpuError::InvalidDevice { .. }),
"expected InvalidDevice, got: {:?}",
err
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::StreamGuard::new
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::get_current_stream
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::set_current_stream
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::clear_current_stream
#[test]
fn stream_guard_set_restore() {
let device = cascade_skip!();
let ctx = device.context().clone();
let dev = 0usize;
// Ensure clean state.
clear_current_stream(dev);
assert!(
get_current_stream(dev).is_none(),
"should start with no current stream"
);
let s1 = cascade_skip!(ctx.new_stream());
let s2 = cascade_skip!(ctx.new_stream());
let s1_ptr = Arc::as_ptr(&s1);
let s2_ptr = Arc::as_ptr(&s2);
set_current_stream(dev, Arc::clone(&s1));
assert_eq!(
Arc::as_ptr(&get_current_stream(dev).unwrap()),
s1_ptr,
"set_current_stream must update the thread-local"
);
{
let _guard = StreamGuard::new(dev, Arc::clone(&s2));
assert_eq!(
Arc::as_ptr(&get_current_stream(dev).unwrap()),
s2_ptr,
"StreamGuard must set the new stream"
);
}
assert_eq!(
Arc::as_ptr(&get_current_stream(dev).unwrap()),
s1_ptr,
"StreamGuard drop must restore the previous stream"
);
clear_current_stream(dev);
assert!(
get_current_stream(dev).is_none(),
"clear_current_stream must remove the entry"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::StreamGuard::new (no previous)
#[test]
fn stream_guard_clears_when_no_previous() {
let device = cascade_skip!();
let ctx = device.context().clone();
let dev = 0usize;
clear_current_stream(dev);
assert!(get_current_stream(dev).is_none());
let s1 = cascade_skip!(ctx.new_stream());
{
let _guard = StreamGuard::new(dev, Arc::clone(&s1));
assert!(get_current_stream(dev).is_some());
}
assert!(
get_current_stream(dev).is_none(),
"StreamGuard with no previous must clear current stream on drop"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::CudaEventWrapper::new
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::CudaEventWrapper::record
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::CudaEventWrapper::synchronize
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::CudaEventWrapper::query
#[test]
fn stream_event_record_synchronize_query() {
let device = cascade_skip!();
let ctx = device.context().clone();
let stream = device.stream();
let event = cascade_skip!(CudaEventWrapper::new(&ctx));
cascade_skip!(event.record(&stream));
cascade_skip!(event.synchronize());
let complete = cascade_skip!(event.query());
assert!(
complete,
"event.query() after synchronize() must return true"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::CudaEventWrapper::new_with_timing
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::CudaEventWrapper::elapsed_us
#[test]
fn stream_event_elapsed_us_nonnegative() {
let device = cascade_skip!();
let ctx = device.context().clone();
let stream = device.stream();
let start = cascade_skip!(CudaEventWrapper::new_with_timing(&ctx));
cascade_skip!(start.record(&stream));
// Do some trivial work: alloc + free.
let _buf = cascade_skip!(alloc_zeros_f32(256, &device));
let end = cascade_skip!(CudaEventWrapper::new_with_timing(&ctx));
cascade_skip!(end.record(&stream));
cascade_skip!(end.synchronize());
// elapsed_us returns u64, which is always >= 0 by type; we just verify no panic.
let elapsed = cascade_skip!(start.elapsed_us(&end));
let _ = elapsed; // u64 is always non-negative; presence of Ok(_) is the assertion
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::CudaEventWrapper::wait_on
#[test]
fn stream_event_wait_on() {
let device = cascade_skip!();
let ctx = device.context().clone();
let stream1 = device.stream();
let stream2 = cascade_skip!(ctx.new_stream());
let event = cascade_skip!(CudaEventWrapper::new(&ctx));
cascade_skip!(event.record(&stream1));
cascade_skip!(event.wait_on(&stream2));
cascade_skip!(stream2.synchronize());
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::StreamPriority
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::StreamPriority::to_cuda_priority
#[test]
fn stream_priority_to_cuda_priority_within_range() {
// Synthetic range with three distinct levels.
let range = (5_i32, -5_i32); // CUDA: lower int = higher priority
assert_eq!(
StreamPriority::High.to_cuda_priority(range),
-5,
"High priority must resolve to greatest (numerically smallest)"
);
assert_eq!(
StreamPriority::Low.to_cuda_priority(range),
5,
"Low priority must resolve to least (numerically largest)"
);
let normal = StreamPriority::Normal.to_cuda_priority(range);
assert!(
(-5..=5).contains(&normal),
"Normal priority must be within [greatest, least]"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::get_stream_priority_range
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::new_stream_with_priority
#[test]
fn stream_priority_range_and_create() {
use ferrotorch_gpu::stream::{get_stream_priority_range, new_stream_with_priority};
let device = cascade_skip!();
let ctx = device.context().clone();
let (least, greatest) = cascade_skip!(get_stream_priority_range(&ctx));
// CUDA convention: lower int = higher priority.
assert!(
greatest <= least,
"priority range invariant: greatest ({}) <= least ({})",
greatest,
least
);
let high = cascade_skip!(new_stream_with_priority(&ctx, StreamPriority::High));
let normal = cascade_skip!(new_stream_with_priority(&ctx, StreamPriority::Normal));
let low = cascade_skip!(new_stream_with_priority(&ctx, StreamPriority::Low));
assert_ne!(Arc::as_ptr(&high), Arc::as_ptr(&normal));
assert_ne!(Arc::as_ptr(&normal), Arc::as_ptr(&low));
assert_ne!(Arc::as_ptr(&high), Arc::as_ptr(&low));
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::StreamPool::get_priority_stream
/// conformance_gpu_lifecycle: ferrotorch_gpu::stream::StreamPool::priority_pool_size
#[test]
fn stream_priority_pool_populates() {
let device = cascade_skip!();
let ctx = device.context().clone();
let dev = 0usize;
let _ = cascade_skip!(StreamPool::get_priority_stream(
&ctx,
dev,
StreamPriority::High
));
let _ = cascade_skip!(StreamPool::get_priority_stream(
&ctx,
dev,
StreamPriority::Low
));
let high_size = StreamPool::priority_pool_size(dev, StreamPriority::High);
let low_size = StreamPool::priority_pool_size(dev, StreamPriority::Low);
assert!(
high_size > 0,
"High priority pool must be populated after first access"
);
assert!(
low_size > 0,
"Low priority pool must be populated after first access"
);
}
// -----------------------------------------------------------------------
// Module: memory_guard
// -----------------------------------------------------------------------
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuardBuilder::new
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuardBuilder::build
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::stats
#[test]
fn memory_guard_starts_at_zero() {
let device = cascade_skip!();
let guard = cascade_skip!(MemoryGuardBuilder::new(Arc::new(device)).build());
let stats = guard.stats();
assert_eq!(
stats.used_bytes, 0,
"fresh MemoryGuard must have used_bytes == 0"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::safe_alloc
#[test]
fn memory_guard_safe_alloc_increases_used_bytes() {
let fixtures = fixtures_json();
let fx = fixture_by_id(&fixtures, "guard_alloc_increases_used");
let count = fx["inputs"]["count"].as_u64().expect("count") as usize;
let expected_bytes = fx["expected_used_bytes"].as_u64().expect("bytes") as usize;
let device = cascade_skip!();
let guard = cascade_skip!(MemoryGuardBuilder::new(Arc::new(device)).build());
let buf = cascade_skip!(guard.safe_alloc::<f32>(count));
let stats = guard.stats();
assert_eq!(
stats.used_bytes, expected_bytes,
"safe_alloc({count} f32) must set used_bytes to {expected_bytes}"
);
guard.free(buf);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::free
#[test]
fn memory_guard_free_decreases_used_bytes() {
let device = cascade_skip!();
let guard = cascade_skip!(MemoryGuardBuilder::new(Arc::new(device)).build());
let buf = cascade_skip!(guard.safe_alloc::<f32>(64));
assert!(guard.stats().used_bytes > 0);
guard.free(buf);
assert_eq!(
guard.stats().used_bytes,
0,
"free() must set used_bytes back to 0"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::OomPolicy
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuardBuilder::budget_bytes
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::safe_alloc (over budget)
/// conformance_gpu_lifecycle: ferrotorch_gpu::error::GpuError::BudgetExceeded
#[test]
fn memory_guard_budget_enforced() {
let device = cascade_skip!();
let guard = cascade_skip!(
MemoryGuardBuilder::new(Arc::new(device))
.budget_bytes(1024)
.build()
);
// 1000 f32 = 4000 bytes > 1024 budget
let err = guard
.safe_alloc::<f32>(1000)
.expect_err("allocation over budget must return Err");
assert!(
matches!(err, GpuError::BudgetExceeded { .. }),
"expected BudgetExceeded, got: {:?}",
err
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::PressureLevel
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::pressure_level
#[test]
fn memory_guard_pressure_none_with_no_budget() {
let device = cascade_skip!();
let guard = cascade_skip!(MemoryGuardBuilder::new(Arc::new(device)).build());
assert_eq!(
guard.pressure_level(),
PressureLevel::None,
"pressure_level must be None when no budget is set"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::PressureLevel::Critical
#[test]
fn memory_guard_pressure_critical_when_budget_exhausted() {
let device = cascade_skip!();
// Set budget to exactly 512 bytes (128 f32 elements).
let guard = cascade_skip!(
MemoryGuardBuilder::new(Arc::new(device))
.budget_bytes(512)
.build()
);
// Allocate exactly 128 f32 = 512 bytes to fill the budget.
let buf = match guard.safe_alloc::<f32>(128) {
Ok(b) => b,
Err(_) => return, // driver alloc failure on constrained systems — skip
};
assert_eq!(
guard.pressure_level(),
PressureLevel::Critical,
"pressure_level must be Critical when used_bytes >= budget_bytes"
);
guard.free(buf);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::register_hook
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::remove_hook
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryHook
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryHook::new
#[test]
fn memory_guard_hook_register_remove() {
let device = cascade_skip!();
let guard = cascade_skip!(MemoryGuardBuilder::new(Arc::new(device)).build());
let hook = MemoryHook::new("test_hook", 1024, 0, 10, || 1024usize);
guard.register_hook(hook);
let removed = guard.remove_hook("test_hook");
assert!(removed, "remove_hook must return true when hook is found");
let not_found = guard.remove_hook("test_hook");
assert!(
!not_found,
"remove_hook must return false when hook is not found"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::safe_alloc_with_hooks
#[test]
fn memory_guard_hook_fires_before_budget_error() {
// #891 fixed: hook-freed headroom now unblocks alloc even when used_bytes=0.
// The fix tracks hook-freed bytes as an independent headroom accumulator
// rather than relying solely on used_bytes decrement (which saturates at 0).
let device = cascade_skip!();
// Budget = 2048 bytes, used_bytes starts at 0. Hook claims to free 2048
// bytes of externally-managed memory. Request 2048 bytes (512 f32) — this
// would exceed the budget without the hook, but the hook frees enough.
let guard = cascade_skip!(
MemoryGuardBuilder::new(Arc::new(device))
.budget_bytes(2048)
.build()
);
let hook = MemoryHook::new("free_2kib", 2048, 0, 10, || 2048usize);
guard.register_hook(hook);
// used_bytes=0; alloc_bytes=2048=budget => shortfall=2048; hook frees 2048.
// After fix: headroom = (2048-0) + 2048 = 4096 >= 2048 => proceeds.
let result = guard.safe_alloc_with_hooks::<f32>(512); // 512*4 = 2048 bytes
match result {
Ok(buf) => guard.free(buf),
Err(GpuError::Driver(_)) => {} // no GPU — acceptable skip
Err(e) => panic!(
"#891 regression: hook should have unblocked alloc with used_bytes=0, got {:?}",
e
),
}
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::set_budget
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::budget
#[test]
fn memory_guard_set_budget() {
let device = cascade_skip!();
let guard = cascade_skip!(MemoryGuardBuilder::new(Arc::new(device)).build());
guard.set_budget(1 << 20);
assert_eq!(
guard.budget(),
1 << 20,
"budget() must reflect set_budget()"
);
// Unlimited.
guard.set_budget(0);
assert_eq!(guard.budget(), 0);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::set_oom_policy
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::OomPolicy::RetryAfterFree
#[test]
fn memory_guard_set_oom_policy() {
let device = cascade_skip!();
let guard = cascade_skip!(MemoryGuardBuilder::new(Arc::new(device)).build());
guard.set_oom_policy(OomPolicy::RetryAfterFree);
guard.set_oom_policy(OomPolicy::Fail);
guard.set_oom_policy(OomPolicy::WaitAndRetry { timeout_secs: 5 });
guard.set_oom_policy(OomPolicy::CheckpointAndFail);
// No assertion needed — verifies the method accepts all variants without panic.
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::reset_peak_stats
#[test]
fn memory_guard_reset_peak_stats() {
let device = cascade_skip!();
let guard = cascade_skip!(MemoryGuardBuilder::new(Arc::new(device)).build());
let buf = cascade_skip!(guard.safe_alloc::<f32>(512));
assert!(guard.stats().peak_bytes > 0);
guard.free(buf);
guard.reset_peak_stats();
assert_eq!(
guard.stats().peak_bytes,
0,
"reset_peak_stats must set peak to 0 after all memory is freed"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::device
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::device_arc
#[test]
fn memory_guard_device_accessors() {
let device = cascade_skip!();
let guard = cascade_skip!(MemoryGuardBuilder::new(Arc::new(device)).build());
assert_eq!(guard.device().ordinal(), 0);
assert_eq!(guard.device_arc().ordinal(), 0);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryStats
#[test]
fn memory_guard_stats_struct_fields_accessible() {
let device = cascade_skip!();
let guard = cascade_skip!(MemoryGuardBuilder::new(Arc::new(device)).build());
let stats = guard.stats();
// All fields must be readable; total_device_bytes > 0 on a real GPU.
let _ = stats.used_bytes;
let _ = stats.budget_bytes;
let _ = stats.peak_bytes;
let _ = stats.num_allocations;
let _ = stats.num_oom_recoveries;
assert!(
stats.total_device_bytes > 0,
"total_device_bytes must be > 0 on a real GPU"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::safe_alloc_copy
#[test]
fn memory_guard_safe_alloc_copy() {
let device = cascade_skip!();
let guard = cascade_skip!(MemoryGuardBuilder::new(Arc::new(device)).build());
let data: Vec<f32> = vec![1.0, 2.0, 3.0];
let buf = cascade_skip!(guard.safe_alloc_copy(&data));
assert_eq!(buf.len(), 3);
guard.free(buf);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryReservation
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryReservation::reserved_bytes
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryReservation::device_ordinal
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuardBuilder::reserve_bytes
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::release_reservation
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuard::has_reservation
#[test]
fn memory_guard_reservation_lifecycle() {
let device = cascade_skip!();
let guard = cascade_skip!(
MemoryGuardBuilder::new(Arc::new(device))
.reserve_bytes(1 << 20) // 1 MiB reservation
.build()
);
assert!(
guard.has_reservation(),
"has_reservation must be true after reserve_bytes in builder"
);
let released = guard.release_reservation();
assert!(
released >= 1 << 20,
"release_reservation must return >= 1 MiB"
);
assert!(
!guard.has_reservation(),
"has_reservation must be false after release_reservation"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuardedDevice
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuardedDevice::device
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuardedDevice::guard
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryGuardedDevice::memory_info
#[test]
fn memory_guard_guarded_device_memory_info() {
use ferrotorch_gpu::memory_guard::MemoryGuardedDevice;
let device = cascade_skip!();
// MemoryGuardedDevice has a single public field `guard: MemoryGuard`.
// It delegates device() and guard() through the inner MemoryGuard.
let guard = cascade_skip!(MemoryGuardBuilder::new(Arc::new(device)).build());
let guarded = MemoryGuardedDevice { guard };
assert_eq!(guarded.device().ordinal(), 0);
// memory_info is on GpuDevice (exposed via the guard's device()).
let (free, total) = cascade_skip!(guarded.device().memory_info());
assert!(total > 0, "total device memory must be > 0 on a real GPU");
assert!(
free <= total,
"free device memory must be <= total ({} <= {})",
free,
total
);
// Also verify the guard accessor.
assert_eq!(guarded.guard().device().ordinal(), 0);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryWatchdog
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryWatchdog::new
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryWatchdog::check_pressure
/// conformance_gpu_lifecycle: ferrotorch_gpu::memory_guard::MemoryWatchdog::pressure_threshold_bytes
#[test]
fn memory_guard_watchdog_construction() {
use ferrotorch_gpu::memory_guard::MemoryWatchdog;
// MemoryWatchdog::new takes Arc<GpuDevice> (not Arc<MemoryGuard>).
let device = cascade_skip!();
let device_arc = Arc::new(device);
let threshold_bytes = 512 * 1024 * 1024; // 512 MiB
let watchdog = Arc::new(MemoryWatchdog::new(
Arc::clone(&device_arc),
threshold_bytes,
std::time::Duration::from_millis(100),
));
assert_eq!(watchdog.pressure_threshold_bytes(), threshold_bytes);
// check_pressure returns true if free VRAM < threshold; just verify no panic.
let _ = watchdog.check_pressure();
}
// -----------------------------------------------------------------------
// Module: device
// -----------------------------------------------------------------------
/// conformance_gpu_lifecycle: ferrotorch_gpu::device::GpuDevice::new
/// conformance_gpu_lifecycle: ferrotorch_gpu::device::GpuDevice::ordinal
#[test]
fn device_new_ordinal_zero() {
let device = cascade_skip!();
assert_eq!(
device.ordinal(),
0,
"GpuDevice::new(0) must report ordinal == 0"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::device::GpuDevice::stream
#[test]
fn device_stream_returns_arc() {
let device = cascade_skip!();
// stream() must not panic and must return a valid Arc.
let s = device.stream();
// Verify the stream Arc was actually returned (non-dangling ptr from Arc).
let _ = Arc::strong_count(&s); // exercises Arc without a null-check lint
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::device::GpuDevice::default_stream
#[test]
fn device_default_stream_stable() {
let device = cascade_skip!();
let p1 = Arc::as_ptr(device.default_stream());
let p2 = Arc::as_ptr(device.default_stream());
assert_eq!(
p1, p2,
"default_stream must return the same Arc on repeated calls"
);
}
/// conformance_gpu_lifecycle: ferrotorch_gpu::device::GpuDevice::new (invalid ordinal)
#[test]
fn device_invalid_ordinal_returns_error() {
// Ordinal 9999 must not be a real device on any reasonable system.
let err = GpuDevice::new(9999);
assert!(
err.is_err(),
"GpuDevice::new(9999) must return Err on systems with < 9999 GPUs"
);
}
// -----------------------------------------------------------------------
// Module: module_cache
// -----------------------------------------------------------------------
/// conformance_gpu_lifecycle: ferrotorch_gpu::module_cache::get_or_compile
#[test]
fn module_cache_repeated_calls_produce_identical_results() {
let fixtures = fixtures_json();
let fx = fixture_by_id(&fixtures, "module_cache_repeated_calls_identity");
let a_data: Vec<f32> = fx["inputs"]["a"]
.as_array()
.expect("a array")
.iter()
.map(|v| v.as_f64().expect("f64") as f32)
.collect();
let b_data: Vec<f32> = fx["inputs"]["b"]
.as_array()
.expect("b array")
.iter()
.map(|v| v.as_f64().expect("f64") as f32)
.collect();
let expected: Vec<f32> = fx["expected_output"]
.as_array()
.expect("expected array")
.iter()
.map(|v| v.as_f64().expect("f64") as f32)
.collect();
let tol = fx["tolerance"].as_f64().expect("tolerance") as f32;
let device = cascade_skip!();
let a = cascade_skip!(cpu_to_gpu(&a_data, &device));
let b = cascade_skip!(cpu_to_gpu(&b_data, &device));
// First call: compiles PTX.
let r1 = cascade_skip!(ferrotorch_gpu::kernels::gpu_add(&a, &b, &device));
// Second call: uses cache.
let r2 = cascade_skip!(ferrotorch_gpu::kernels::gpu_add(&a, &b, &device));
let h1 = cascade_skip!(gpu_to_cpu(&r1, &device));
let h2 = cascade_skip!(gpu_to_cpu(&r2, &device));
assert_eq!(
h1.len(),
expected.len(),
"first-call result length must match fixture"
);
assert_eq!(
h1.len(),
h2.len(),
"both calls must produce the same number of elements"
);
for (i, ((&v1, &v2), &exp)) in h1.iter().zip(h2.iter()).zip(expected.iter()).enumerate() {
assert!(
(v1 - exp).abs() <= tol,
"module_cache 1st call: element {i}: {v1} vs expected {exp} (tol {tol})"
);
assert_eq!(
v1, v2,
"module_cache: 1st and 2nd call must produce identical results at index {i}"
);
}
}
// -----------------------------------------------------------------------
// Layer-3 fixture integrity: verify the fixture file is loadable and
// contains the expected number of entries.
// -----------------------------------------------------------------------
/// conformance_gpu_lifecycle: fixture file integrity
#[test]
fn fixtures_lifecycle_json_is_valid() {
let fixtures = fixtures_json();
let count = fixtures["fixtures"]
.as_array()
.expect("fixtures must be an array")
.len();
// The script generates >= 70 fixtures; bump the floor if more are added.
assert!(
count >= 70,
"fixtures_lifecycle.json must contain at least 70 fixtures (found {})",
count
);
}
}