abaddon 0.2.0-rc.1

Blazingly fast LLM inference engine - The Destroyer renders judgment
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
//! Integration tests for the cuda_inference module.
//!
//! These tests require a CUDA device to run. They're marked with `#[ignore]`
//! by default and can be run with `cargo test --features cuda -- --ignored`.

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use cudarc::driver::CudaDevice;

    use crate::cuda_inference::arch::{Activation, ModelArch, ModelConfig};
    use crate::cuda_inference::cublas::CublasHandle;
    use crate::cuda_inference::tensor::{GpuDType, GpuTensor};

    /// Helper to get CUDA device if available.
    fn get_cuda_device() -> Option<Arc<CudaDevice>> {
        CudaDevice::new(0).ok()
    }

    // ==================== GpuTensor Tests ====================

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_gpu_tensor_zeros() {
        let device = get_cuda_device().expect("CUDA device required");

        let tensor = GpuTensor::zeros(vec![4, 128], GpuDType::F16, device.clone())
            .expect("Failed to create tensor");

        assert_eq!(tensor.shape(), &[4, 128]);
        assert_eq!(tensor.dtype(), GpuDType::F16);
        assert_eq!(tensor.numel(), 512);
        assert_eq!(tensor.size_bytes(), 1024); // 512 elements * 2 bytes
        assert_eq!(tensor.ndim(), 2);
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_gpu_tensor_strides() {
        let device = get_cuda_device().expect("CUDA device required");

        let tensor = GpuTensor::zeros(vec![2, 3, 4], GpuDType::F32, device.clone())
            .expect("Failed to create tensor");

        // Row-major strides: [12, 4, 1]
        assert_eq!(tensor.strides(), &[12, 4, 1]);
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_gpu_tensor_reshape() {
        let device = get_cuda_device().expect("CUDA device required");

        let tensor = GpuTensor::zeros(vec![4, 8], GpuDType::F16, device.clone())
            .expect("Failed to create tensor");

        // Reshape to [2, 16]
        let reshaped = tensor.reshape(vec![2, 16]).expect("Reshape failed");
        assert_eq!(reshaped.shape(), &[2, 16]);
        assert_eq!(reshaped.numel(), 32);

        // Invalid reshape should fail
        let result = tensor.reshape(vec![3, 10]);
        assert!(result.is_err());
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_gpu_tensor_slice() {
        let device = get_cuda_device().expect("CUDA device required");

        let tensor = GpuTensor::zeros(vec![8, 64], GpuDType::F16, device.clone())
            .expect("Failed to create tensor");

        // Slice first 4 rows
        let sliced = tensor.slice_dim0(0, 4).expect("Slice failed");
        assert_eq!(sliced.shape(), &[4, 64]);

        // Slice middle rows
        let sliced = tensor.slice_dim0(2, 6).expect("Slice failed");
        assert_eq!(sliced.shape(), &[4, 64]);

        // Invalid slice should fail
        let result = tensor.slice_dim0(5, 10);
        assert!(result.is_err());
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_gpu_tensor_host_copy() {
        let device = get_cuda_device().expect("CUDA device required");

        // Create tensor and copy data to it
        let mut tensor = GpuTensor::zeros(vec![4], GpuDType::F32, device.clone())
            .expect("Failed to create tensor");

        // Source data: [1.0, 2.0, 3.0, 4.0]
        let src_data: Vec<u8> = [1.0f32, 2.0, 3.0, 4.0]
            .iter()
            .flat_map(|f| f.to_le_bytes())
            .collect();

        tensor
            .copy_from_host(&src_data)
            .expect("Copy to GPU failed");

        // Copy back and verify
        let mut dst_data = vec![0u8; 16];
        tensor
            .copy_to_host(&mut dst_data)
            .expect("Copy from GPU failed");

        assert_eq!(src_data, dst_data);
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_gpu_tensor_to_host() {
        let device = get_cuda_device().expect("CUDA device required");

        let mut tensor = GpuTensor::zeros(vec![4], GpuDType::F32, device.clone())
            .expect("Failed to create tensor");

        let src_data: Vec<u8> = [1.0f32, 2.0, 3.0, 4.0]
            .iter()
            .flat_map(|f| f.to_le_bytes())
            .collect();

        tensor
            .copy_from_host(&src_data)
            .expect("Copy to GPU failed");

        // Use to_host convenience method
        let dst_data = tensor.to_host().expect("to_host failed");
        assert_eq!(src_data, dst_data);
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_gpu_tensor_clone() {
        let device = get_cuda_device().expect("CUDA device required");

        let mut tensor = GpuTensor::zeros(vec![8], GpuDType::F32, device.clone())
            .expect("Failed to create tensor");

        // Write some data
        let src: Vec<u8> = (0..8u32).flat_map(|i| (i as f32).to_le_bytes()).collect();
        tensor.copy_from_host(&src).expect("Copy failed");

        // Clone the tensor
        let cloned = tensor.clone_tensor().expect("Clone failed");

        // Verify they have same data
        let mut original_data = vec![0u8; 32];
        let mut cloned_data = vec![0u8; 32];

        tensor
            .copy_to_host(&mut original_data)
            .expect("Copy failed");
        cloned.copy_to_host(&mut cloned_data).expect("Copy failed");

        assert_eq!(original_data, cloned_data);

        // Verify they have different device pointers
        assert_ne!(tensor.device_ptr(), cloned.device_ptr());
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_gpu_tensor_i32() {
        let device = get_cuda_device().expect("CUDA device required");

        let mut tensor = GpuTensor::zeros(vec![4], GpuDType::I32, device.clone())
            .expect("Failed to create I32 tensor");

        assert_eq!(tensor.dtype(), GpuDType::I32);
        assert_eq!(tensor.size_bytes(), 16); // 4 elements * 4 bytes

        // Write position indices
        let positions: Vec<u8> = [0i32, 1, 2, 3]
            .iter()
            .flat_map(|p| p.to_le_bytes())
            .collect();
        tensor.copy_from_host(&positions).expect("Copy failed");

        let result = tensor.to_host().expect("to_host failed");
        assert_eq!(positions, result);
    }

    // ==================== cuBLAS Tests ====================

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_cublas_handle() {
        let device = get_cuda_device().expect("CUDA device required");

        let handle = CublasHandle::new(device.clone()).expect("Failed to create cuBLAS handle");

        handle.set_math_mode(true).expect("Failed to set math mode");
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_cublas_axpy() {
        let device = get_cuda_device().expect("CUDA device required");
        let handle = CublasHandle::new(device.clone()).expect("Failed to create cuBLAS handle");

        let x =
            GpuTensor::zeros(vec![128], GpuDType::F16, device.clone()).expect("Failed to create x");
        let mut y =
            GpuTensor::zeros(vec![128], GpuDType::F16, device.clone()).expect("Failed to create y");

        // This is a placeholder test - actual result depends on implementation
        handle.axpy(1.0, &x, &mut y).expect("axpy failed");
    }

    // ==================== Kernel Tests ====================

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_rmsnorm_kernel() {
        use crate::cuda_inference::kernels::RMSNormKernel;

        let device = get_cuda_device().expect("CUDA device required");
        let mut kernel = RMSNormKernel::new(device.clone()).expect("Failed to create kernel");

        // Create test tensors
        let seq_len = 4;
        let hidden_size = 128;

        let mut input = GpuTensor::zeros(vec![seq_len, hidden_size], GpuDType::F16, device.clone())
            .expect("Failed to create input");

        let weight = GpuTensor::zeros(vec![hidden_size], GpuDType::F16, device.clone())
            .expect("Failed to create weight");

        let mut output =
            GpuTensor::zeros(vec![seq_len, hidden_size], GpuDType::F16, device.clone())
                .expect("Failed to create output");

        // Initialize weight to 1.0
        let ones: Vec<u8> = (0..hidden_size)
            .flat_map(|_| half::f16::from_f32(1.0).to_le_bytes())
            .collect();
        let mut weight_mut = weight;
        weight_mut.copy_from_host(&ones).expect("Copy failed");

        // Run kernel
        kernel
            .forward(&input, &weight_mut, &mut output, 1e-5)
            .expect("RMSNorm failed");
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_rope_kernel() {
        use crate::cuda_inference::kernels::RoPEKernel;

        let device = get_cuda_device().expect("CUDA device required");
        let mut kernel = RoPEKernel::new(device.clone()).expect("Failed to create kernel");

        // Create test tensors [batch, heads, head_dim]
        let batch = 4;
        let heads = 8;
        let head_dim = 64;

        let mut x = GpuTensor::zeros(vec![batch, heads, head_dim], GpuDType::F16, device.clone())
            .expect("Failed to create x");

        let mut positions = GpuTensor::zeros(vec![batch], GpuDType::I32, device.clone())
            .expect("Failed to create positions");

        // Set positions [0, 1, 2, 3]
        let pos_data: Vec<u8> = (0..batch as i32).flat_map(|p| p.to_le_bytes()).collect();
        positions.copy_from_host(&pos_data).expect("Copy failed");

        // Run kernel
        kernel
            .forward(&mut x, &positions, 10000.0, 1.0)
            .expect("RoPE failed");
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_activation_silu() {
        use crate::cuda_inference::kernels::{ActivationKernel, ActivationType};

        let device = get_cuda_device().expect("CUDA device required");
        let mut kernel = ActivationKernel::new(device.clone()).expect("Failed to create kernel");

        let n = 256;
        let mut x =
            GpuTensor::zeros(vec![n], GpuDType::F16, device.clone()).expect("Failed to create x");
        let mut out =
            GpuTensor::zeros(vec![n], GpuDType::F16, device.clone()).expect("Failed to create out");

        // Initialize with some values
        let data: Vec<u8> = (0..n)
            .map(|i| half::f16::from_f32((i as f32 - 128.0) / 64.0))
            .flat_map(|f| f.to_le_bytes())
            .collect();
        x.copy_from_host(&data).expect("Copy failed");

        // Run SiLU
        kernel
            .forward(&x, &mut out, ActivationType::SiLU)
            .expect("SiLU failed");
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_activation_silu_mul() {
        use crate::cuda_inference::kernels::ActivationKernel;

        let device = get_cuda_device().expect("CUDA device required");
        let mut kernel = ActivationKernel::new(device.clone()).expect("Failed to create kernel");

        let n = 256;
        let gate = GpuTensor::zeros(vec![n], GpuDType::F16, device.clone())
            .expect("Failed to create gate");
        let up =
            GpuTensor::zeros(vec![n], GpuDType::F16, device.clone()).expect("Failed to create up");
        let mut out =
            GpuTensor::zeros(vec![n], GpuDType::F16, device.clone()).expect("Failed to create out");

        // Run fused SiLU * up
        kernel
            .silu_mul(&gate, &up, &mut out)
            .expect("SiLU mul failed");
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_fused_gemm_f16() {
        use crate::cuda_inference::kernels::FusedGemmKernel;

        let device = get_cuda_device().expect("CUDA device required");
        let mut kernel = FusedGemmKernel::new(device.clone()).expect("Failed to create kernel");

        // Matrix sizes
        let m = 32; // batch/seq
        let k = 128; // in features
        let n = 256; // out features

        let a = GpuTensor::zeros(vec![m, k], GpuDType::F16, device.clone())
            .expect("Failed to create A");
        let b = GpuTensor::zeros(vec![k, n], GpuDType::F16, device.clone())
            .expect("Failed to create B");
        let mut c = GpuTensor::zeros(vec![m, n], GpuDType::F16, device.clone())
            .expect("Failed to create C");

        // Run F16 GEMM
        kernel.forward_f16(&a, &b, &mut c).expect("F16 GEMM failed");
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_flash_attention() {
        use crate::cuda_inference::kernels::FlashAttentionKernel;

        let device = get_cuda_device().expect("CUDA device required");
        let kernel = FlashAttentionKernel::new(device.clone()).expect("Failed to create kernel");

        // Attention dimensions (4D: [batch, heads, seq, head_dim])
        let batch = 1;
        let seq_len = 16;
        let num_heads = 8;
        let num_kv_heads = 2; // GQA
        let head_dim = 64;

        let q = GpuTensor::zeros(
            vec![batch, num_heads, seq_len, head_dim],
            GpuDType::F16,
            device.clone(),
        )
        .expect("Failed to create Q");

        let k = GpuTensor::zeros(
            vec![batch, num_kv_heads, seq_len, head_dim],
            GpuDType::F16,
            device.clone(),
        )
        .expect("Failed to create K");

        let v = GpuTensor::zeros(
            vec![batch, num_kv_heads, seq_len, head_dim],
            GpuDType::F16,
            device.clone(),
        )
        .expect("Failed to create V");

        let mut out = GpuTensor::zeros(
            vec![batch, num_heads, seq_len, head_dim],
            GpuDType::F16,
            device.clone(),
        )
        .expect("Failed to create output");

        // Run attention (causal)
        kernel
            .forward(&q, &k, &v, &mut out, true)
            .expect("Flash attention failed");
    }

    // ==================== ComputeEngine Tests ====================

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_compute_engine_creation() {
        use crate::cuda_inference::compute::ComputeEngine;

        let device = get_cuda_device().expect("CUDA device required");

        let config = ModelConfig {
            arch: ModelArch::Llama,
            hidden_size: 256,
            intermediate_size: 512,
            num_layers: 2,
            num_attention_heads: 4,
            num_kv_heads: 2,
            head_dim: 64,
            vocab_size: 1000,
            max_seq_len: 128,
            rms_norm_eps: 1e-5,
            rope_theta: 10000.0,
            rope_scaling: None,
            attention_bias: false,
            mlp_bias: false,
            hidden_act: Activation::SiLU,
            tie_word_embeddings: true,
            sliding_window: None,
            bos_token_id: 1,
            eos_token_id: 2,
            pad_token_id: None,
        };

        let engine = ComputeEngine::new(config, 128, device.clone());
        assert!(
            engine.is_ok(),
            "Failed to create ComputeEngine: {:?}",
            engine.err()
        );

        let engine = engine.unwrap();
        assert_eq!(engine.config().hidden_size, 256);
        assert_eq!(engine.current_position(), 0);
    }

    #[test]
    #[ignore = "requires CUDA device"]
    fn test_kv_cache() {
        use crate::cuda_inference::kv_cache::KvCache;

        let device = get_cuda_device().expect("CUDA device required");

        let config = ModelConfig {
            arch: ModelArch::Llama,
            hidden_size: 256,
            intermediate_size: 512,
            num_layers: 2,
            num_attention_heads: 4,
            num_kv_heads: 2,
            head_dim: 64,
            vocab_size: 1000,
            max_seq_len: 128,
            rms_norm_eps: 1e-5,
            rope_theta: 10000.0,
            rope_scaling: None,
            attention_bias: false,
            mlp_bias: false,
            hidden_act: Activation::SiLU,
            tie_word_embeddings: true,
            sliding_window: None,
            bos_token_id: 1,
            eos_token_id: 2,
            pad_token_id: None,
        };

        let mut cache =
            KvCache::new(&config, 128, device.clone()).expect("Failed to create KV cache");

        assert_eq!(cache.seq_len(), 0);
        assert_eq!(cache.max_seq_len(), 128);

        // Reset
        cache.reset();
        assert_eq!(cache.seq_len(), 0);
    }

    // ==================== Model Config Tests ====================

    #[test]
    fn test_model_config_llama() {
        let config = ModelConfig {
            arch: ModelArch::Llama,
            hidden_size: 4096,
            intermediate_size: 11008,
            num_layers: 32,
            num_attention_heads: 32,
            num_kv_heads: 8,
            head_dim: 128,
            vocab_size: 32000,
            max_seq_len: 4096,
            rms_norm_eps: 1e-5,
            rope_theta: 10000.0,
            rope_scaling: None,
            attention_bias: false,
            mlp_bias: false,
            hidden_act: Activation::SiLU,
            tie_word_embeddings: false,
            sliding_window: None,
            bos_token_id: 1,
            eos_token_id: 2,
            pad_token_id: None,
        };

        assert_eq!(config.arch, ModelArch::Llama);
        assert_eq!(config.hidden_size, 4096);
        assert_eq!(config.num_kv_heads, 8);
    }

    #[test]
    fn test_model_arch_equality() {
        assert_eq!(ModelArch::Llama, ModelArch::Llama);
        assert_ne!(ModelArch::Llama, ModelArch::Mistral);
        assert_ne!(ModelArch::Qwen, ModelArch::Phi);
    }

    #[test]
    fn test_gpu_dtype_sizes() {
        assert_eq!(GpuDType::F16.size_bytes(), 2);
        assert_eq!(GpuDType::BF16.size_bytes(), 2);
        assert_eq!(GpuDType::F32.size_bytes(), 4);
        assert_eq!(GpuDType::I32.size_bytes(), 4);
        assert_eq!(GpuDType::I8.size_bytes(), 1);
        assert_eq!(GpuDType::U8.size_bytes(), 1);
        assert_eq!(GpuDType::I4.size_bytes(), 1);
    }

    #[test]
    fn test_gpu_dtype_packed() {
        assert!(!GpuDType::F16.is_packed());
        assert!(!GpuDType::F32.is_packed());
        assert!(!GpuDType::I32.is_packed());
        assert!(GpuDType::I4.is_packed());

        assert_eq!(GpuDType::F16.pack_factor(), 1);
        assert_eq!(GpuDType::I4.pack_factor(), 2);
    }

    // ==================== RoPE Scaling Tests ====================

    #[test]
    fn test_rope_scaling_functions() {
        use crate::cuda_inference::kernels::rope::{linear_scaling, ntk_aware_scaling};

        // No extension
        let theta = ntk_aware_scaling(4096, 4096, 10000.0);
        assert!((theta - 10000.0).abs() < 0.01);

        // 2x extension
        let theta = ntk_aware_scaling(4096, 8192, 10000.0);
        assert!(theta > 10000.0);

        // Linear scaling
        let scale = linear_scaling(4096, 4096);
        assert!((scale - 1.0).abs() < 0.01);

        let scale = linear_scaling(4096, 8192);
        assert!((scale - 0.5).abs() < 0.01);
    }

    // ==================== Activation Type Tests ====================

    #[test]
    fn test_activation_type_equality() {
        use crate::cuda_inference::kernels::ActivationType;

        assert_eq!(ActivationType::SiLU, ActivationType::SiLU);
        assert_ne!(ActivationType::SiLU, ActivationType::GELUFast);
        assert_ne!(ActivationType::GELUFast, ActivationType::GELUTanh);
        assert_ne!(ActivationType::GELUTanh, ActivationType::ReLU);
    }

    // ==================== E2E Integration Tests ====================

    /// Test loading HCT model weights.
    ///
    /// This test requires:
    /// 1. A CUDA device
    /// 2. SmolLM2-135M converted to HCT format at test_models/smollm2-135m-int4/
    #[test]
    #[ignore = "requires CUDA device and HCT model files"]
    fn test_load_hct_weights() {
        use crate::cuda_inference::weight_store::WeightStore;

        let model_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../../test_models/smollm2-135m-int4");

        if !model_dir.exists() {
            eprintln!(
                "Skipping test: model directory not found at {:?}",
                model_dir
            );
            return;
        }

        let weights =
            WeightStore::load_hct(&model_dir, None, 0).expect("Failed to load HCT weights");

        // Verify config was parsed correctly
        assert_eq!(weights.config.hidden_size, 576);
        assert_eq!(weights.config.num_layers, 30);
        assert_eq!(weights.config.num_attention_heads, 9);
        assert_eq!(weights.config.num_kv_heads, 3);
        assert_eq!(weights.config.vocab_size, 49152);

        // Verify layers were loaded
        assert_eq!(weights.layers.len(), 30);

        // Verify embedding shape
        assert_eq!(weights.embed_tokens.shape()[0], 49152);
        assert_eq!(weights.embed_tokens.shape()[1], 576);

        // Verify layer 0 weight shapes and formats
        eprintln!("Layer 0 shapes and formats:");
        eprintln!(
            "  q_proj: {:?}, format: {:?}",
            weights.layers[0].q_proj.shape, weights.layers[0].q_proj.format
        );
        eprintln!(
            "  k_proj: {:?}, format: {:?}",
            weights.layers[0].k_proj.shape, weights.layers[0].k_proj.format
        );
        eprintln!(
            "  v_proj: {:?}, format: {:?}",
            weights.layers[0].v_proj.shape, weights.layers[0].v_proj.format
        );
        eprintln!(
            "  o_proj: {:?}, format: {:?}",
            weights.layers[0].o_proj.shape, weights.layers[0].o_proj.format
        );
        eprintln!(
            "  gate_proj: {:?}, format: {:?}",
            weights.layers[0].gate_proj.shape, weights.layers[0].gate_proj.format
        );
        eprintln!(
            "  up_proj: {:?}, format: {:?}",
            weights.layers[0].up_proj.shape, weights.layers[0].up_proj.format
        );
        eprintln!(
            "  down_proj: {:?}, format: {:?}",
            weights.layers[0].down_proj.shape, weights.layers[0].down_proj.format
        );
        eprintln!(
            "  embed_tokens data shape: {:?}",
            weights.embed_tokens.shape()
        );

        // Verify o_proj shape is [hidden_size, hidden_size] = [576, 576]
        assert_eq!(weights.layers[0].o_proj.shape, (576, 576));

        // Verify weights are INT4
        use crate::cuda_inference::weight_store::QuantFormat;
        assert!(matches!(weights.layers[0].o_proj.format, QuantFormat::Int4));

        eprintln!(
            "Successfully loaded SmolLM2-135M: {:.2} MB GPU memory",
            weights.memory_used as f64 / 1024.0 / 1024.0
        );
    }

    /// Test creating ComputeEngine from loaded HCT weights.
    #[test]
    #[ignore = "requires CUDA device and HCT model files"]
    fn test_compute_engine_from_hct() {
        use crate::cuda_inference::compute::ComputeEngine;
        use crate::cuda_inference::weight_store::WeightStore;

        let model_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../../test_models/smollm2-135m-int4");

        if !model_dir.exists() {
            eprintln!("Skipping test: model directory not found");
            return;
        }

        let weights = WeightStore::load_hct(&model_dir, None, 0).expect("Failed to load weights");

        let _engine = ComputeEngine::new(
            weights.config.clone(),
            512, // max_seq_len
            weights.device().clone(),
        )
        .expect("Failed to create ComputeEngine");

        eprintln!("ComputeEngine created from HCT weights successfully");
    }

    /// Test full forward pass with sample tokens.
    ///
    /// This is the main E2E integration test that:
    /// 1. Loads the model from HCT files
    /// 2. Creates a ComputeEngine
    /// 3. Runs a forward pass with sample input
    /// 4. Verifies output logits
    #[test]
    #[ignore = "requires CUDA device and HCT model files"]
    fn test_e2e_forward_pass() {
        use crate::cuda_inference::compute::ComputeEngine;
        use crate::cuda_inference::weight_store::WeightStore;

        let model_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../../test_models/smollm2-135m-int4");

        if !model_dir.exists() {
            eprintln!("Skipping test: model directory not found");
            return;
        }

        // Load weights
        let weights = WeightStore::load_hct(&model_dir, None, 0).expect("Failed to load weights");

        eprintln!(
            "Loaded weights: {} layers, {} MB",
            weights.config.num_layers,
            weights.memory_used as f64 / 1024.0 / 1024.0
        );

        // Create compute engine
        let mut engine = ComputeEngine::new(
            weights.config.clone(),
            128, // max_seq_len for testing
            weights.device().clone(),
        )
        .expect("Failed to create engine");

        // Sample input tokens (e.g., "Hello" might be token 9906 in some tokenizers)
        // Using simple test tokens that are valid for vocab_size = 49152
        let input_tokens: Vec<u32> = vec![1, 7592, 2]; // BOS, "Hello", EOS

        // Run forward pass
        let start = std::time::Instant::now();
        let output = engine
            .forward(&input_tokens, &weights, 0)
            .expect("Forward pass failed");
        let elapsed = start.elapsed();

        // Verify output shape
        let output_shape = output.shape();
        assert_eq!(output_shape.len(), 2);
        assert_eq!(output_shape[0], input_tokens.len()); // seq_len
        assert_eq!(output_shape[1], 49152); // vocab_size

        eprintln!(
            "Forward pass successful: output shape {:?}, time: {:?}",
            output_shape, elapsed
        );

        // Get logits for sampling
        let logits = engine.get_logits().expect("Failed to get logits");

        // Verify logits shape (last token's logits)
        assert_eq!(logits.shape().len(), 1);
        assert_eq!(logits.shape()[0], 49152);

        eprintln!("E2E test passed!");
    }

    /// Performance benchmark for inference throughput.
    ///
    /// Measures:
    /// - Prefill latency (processing prompt)
    /// - Decode latency (per-token generation)
    /// - Tokens per second
    #[test]
    #[ignore = "requires CUDA device and HCT model files"]
    fn test_performance_benchmark() {
        use crate::cuda_inference::compute::ComputeEngine;
        use crate::cuda_inference::weight_store::WeightStore;

        let model_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../../test_models/smollm2-135m-int4");

        if !model_dir.exists() {
            eprintln!("Skipping benchmark: model directory not found");
            return;
        }

        // Load weights
        let weights = WeightStore::load_hct(&model_dir, None, 0).expect("Failed to load weights");

        eprintln!("\n=== Performance Benchmark ===");
        eprintln!("Model: SmolLM2-135M (INT4)");
        eprintln!("Layers: {}", weights.config.num_layers);
        eprintln!("Hidden size: {}", weights.config.hidden_size);
        eprintln!(
            "GPU Memory: {:.2} MB",
            weights.memory_used as f64 / 1024.0 / 1024.0
        );
        eprintln!();

        // Create compute engine
        let mut engine = ComputeEngine::new(
            weights.config.clone(),
            512, // max_seq_len for benchmark
            weights.device().clone(),
        )
        .expect("Failed to create engine");

        // Warmup run
        let warmup_tokens: Vec<u32> = vec![1, 100, 200, 300];
        let _ = engine
            .prefill(&warmup_tokens, &weights)
            .expect("Warmup failed");

        // === Prefill Benchmark ===
        let prompt_lengths = [8, 16, 32, 64, 128];
        eprintln!("--- Prefill Latency ---");

        for &seq_len in &prompt_lengths {
            let tokens: Vec<u32> = (0..seq_len).map(|i| (i % 1000 + 1) as u32).collect();

            // Run multiple iterations for stable timing
            let iterations = 5;
            let mut total_time = std::time::Duration::ZERO;

            for _ in 0..iterations {
                engine.reset();
                let start = std::time::Instant::now();
                let _ = engine.prefill(&tokens, &weights).expect("Prefill failed");
                // Sync to ensure GPU work completes
                weights.device().synchronize().ok();
                total_time += start.elapsed();
            }

            let avg_time = total_time / iterations as u32;
            let tokens_per_sec = seq_len as f64 / avg_time.as_secs_f64();

            eprintln!(
                "  seq_len={:3}: {:6.2}ms ({:.0} tok/s)",
                seq_len,
                avg_time.as_secs_f64() * 1000.0,
                tokens_per_sec
            );
        }

        // === Decode Benchmark ===
        eprintln!("\n--- Decode Latency (single token) ---");

        // Prefill a prompt first
        let prompt: Vec<u32> = (0..32).map(|i| (i % 1000 + 1) as u32).collect();
        engine.reset();
        let _ = engine.prefill(&prompt, &weights).expect("Prefill failed");

        // Measure decode latency
        let decode_iterations = 20;
        let mut decode_times = Vec::with_capacity(decode_iterations);

        for i in 0..decode_iterations {
            let token = (i % 1000 + 500) as u32;

            let start = std::time::Instant::now();
            let _ = engine.decode(token, &weights).expect("Decode failed");
            weights.device().synchronize().ok();
            decode_times.push(start.elapsed());
        }

        // Calculate statistics
        decode_times.sort();
        let median_decode = decode_times[decode_iterations / 2];
        let min_decode = decode_times[0];
        let max_decode = decode_times[decode_iterations - 1];
        let avg_decode: std::time::Duration =
            decode_times.iter().sum::<std::time::Duration>() / decode_iterations as u32;

        let tokens_per_sec = 1.0 / avg_decode.as_secs_f64();

        eprintln!(
            "  Avg:    {:6.2}ms ({:.0} tok/s)",
            avg_decode.as_secs_f64() * 1000.0,
            tokens_per_sec
        );
        eprintln!("  Median: {:6.2}ms", median_decode.as_secs_f64() * 1000.0);
        eprintln!("  Min:    {:6.2}ms", min_decode.as_secs_f64() * 1000.0);
        eprintln!("  Max:    {:6.2}ms", max_decode.as_secs_f64() * 1000.0);

        eprintln!("\n=== Benchmark Complete ===");
    }
}