trustformers-core 0.1.1

Core traits and utilities for TrustformeRS
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
#![allow(unused_variables)] // CUDA backend implementation with reserved parameters

use crate::errors::{Result, TrustformersError};
use crate::tensor::Tensor;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

/// Direct CUDA kernel operations for performance-critical computations
///
/// This module provides hand-optimized CUDA kernels for transformer operations,
/// offering significant performance improvements over CPU implementations.
///
/// Features:
/// - Matrix multiplication with various precisions (FP32, FP16, BF16, INT8)
/// - Fused attention operations with flash attention optimizations
/// - Element-wise operations with kernel fusion
/// - Custom reduction operations
/// - Memory-efficient implementations
///
/// CUDA kernel handle for managing GPU resources
pub struct CudaKernel {
    /// CUDA context
    context: Option<CudaContext>,
    /// Available GPU devices
    devices: Vec<CudaDevice>,
    /// Memory pools for different devices
    memory_pools: HashMap<usize, Arc<Mutex<CudaMemoryPool>>>,
    /// Kernel cache for compiled kernels
    kernel_cache: HashMap<String, CompiledKernel>,
}

/// CUDA device information
#[derive(Debug, Clone)]
pub struct CudaDevice {
    pub id: usize,
    pub name: String,
    pub compute_capability: (u32, u32),
    pub memory_total: u64,
    pub memory_free: u64,
    pub multiprocessor_count: u32,
    pub max_threads_per_block: u32,
    pub warp_size: u32,
    pub max_shared_memory_per_block: u32,
}

/// CUDA context for kernel execution
#[derive(Debug)]
pub struct CudaContext {
    #[allow(dead_code)]
    device_id: usize,
    _stream: CudaStream,
}

/// CUDA stream for asynchronous operations
#[derive(Debug)]
pub struct CudaStream {
    #[allow(dead_code)]
    id: usize,
    _priority: i32,
}

/// Memory pool for efficient GPU memory management
#[derive(Debug)]
pub struct CudaMemoryPool {
    #[allow(dead_code)]
    device_id: usize,
    _allocated_blocks: HashMap<usize, CudaMemoryBlock>,
    _free_blocks: Vec<CudaMemoryBlock>,
    total_allocated: u64,
    peak_allocated: u64,
}

/// CUDA memory block
#[derive(Debug, Clone)]
pub struct CudaMemoryBlock {
    #[allow(dead_code)]
    ptr: usize,
    _size: u64,
    _device_id: usize,
}

/// Compiled CUDA kernel
#[derive(Debug, Clone)]
pub struct CompiledKernel {
    name: String,
    #[allow(dead_code)]
    ptx_code: String,
    _function_name: String,
    _grid_size: (u32, u32, u32),
    _block_size: (u32, u32, u32),
    _shared_memory_size: u32,
}

/// CUDA kernel configuration
#[derive(Debug, Clone)]
pub struct KernelConfig {
    pub grid_size: (u32, u32, u32),
    pub block_size: (u32, u32, u32),
    pub shared_memory_size: u32,
    pub stream_id: Option<usize>,
}

impl Default for KernelConfig {
    fn default() -> Self {
        Self {
            grid_size: (1, 1, 1),
            block_size: (256, 1, 1),
            shared_memory_size: 0,
            stream_id: None,
        }
    }
}

impl CudaKernel {
    /// Initialize CUDA kernel system
    pub fn new() -> Result<Self> {
        let devices = Self::enumerate_devices()?;
        let context = if !devices.is_empty() { Some(CudaContext::new(0)?) } else { None };

        let mut memory_pools = HashMap::new();
        for device in &devices {
            memory_pools.insert(
                device.id,
                Arc::new(Mutex::new(CudaMemoryPool::new(device.id)?)),
            );
        }

        Ok(Self {
            context,
            devices,
            memory_pools,
            kernel_cache: HashMap::new(),
        })
    }

    /// Enumerate available CUDA devices
    fn enumerate_devices() -> Result<Vec<CudaDevice>> {
        // In a real implementation, this would use CUDA runtime API
        // For this implementation, we'll simulate device enumeration
        let devices = vec![
            // Simulate NVIDIA RTX 4090 (common high-end GPU)
            CudaDevice {
                id: 0,
                name: "NVIDIA RTX 4090".to_string(),
                compute_capability: (8, 9),
                memory_total: 24 * 1024 * 1024 * 1024, // 24GB
                memory_free: 20 * 1024 * 1024 * 1024,  // 20GB available
                multiprocessor_count: 128,
                max_threads_per_block: 1024,
                warp_size: 32,
                max_shared_memory_per_block: 48 * 1024, // 48KB
            },
            // Simulate NVIDIA A100 (common data center GPU)
            CudaDevice {
                id: 1,
                name: "NVIDIA A100".to_string(),
                compute_capability: (8, 0),
                memory_total: 80 * 1024 * 1024 * 1024, // 80GB
                memory_free: 75 * 1024 * 1024 * 1024,  // 75GB available
                multiprocessor_count: 108,
                max_threads_per_block: 1024,
                warp_size: 32,
                max_shared_memory_per_block: 164 * 1024, // 164KB
            },
        ];

        Ok(devices)
    }

    /// Matrix multiplication with CUDA kernel
    pub fn matmul(
        &mut self,
        a: &Tensor,
        b: &Tensor,
        c: &mut Tensor,
        config: Option<KernelConfig>,
    ) -> Result<()> {
        let config = config.unwrap_or_default();

        // Validate dimensions
        let a_shape = a.shape();
        let b_shape = b.shape();
        let c_shape = c.shape();

        if a_shape.len() != 2 || b_shape.len() != 2 || c_shape.len() != 2 {
            return Err(TrustformersError::tensor_op_error(
                "Matrix multiplication requires 2D tensors",
                "CudaKernels::gemm",
            ));
        }

        if a_shape[1] != b_shape[0] {
            return Err(TrustformersError::tensor_op_error(
                "Matrix dimensions incompatible for multiplication",
                "CudaKernels::gemm",
            ));
        }

        if c_shape[0] != a_shape[0] || c_shape[1] != b_shape[1] {
            return Err(TrustformersError::tensor_op_error(
                "Output matrix has incorrect dimensions",
                "CudaKernels::gemm",
            ));
        }

        // Generate or retrieve compiled kernel
        let kernel_key = format!("matmul_{}x{}x{}", a_shape[0], a_shape[1], b_shape[1]);
        let kernel = self.get_or_compile_kernel(
            &kernel_key,
            &Self::generate_matmul_kernel_code(&a_shape, &b_shape),
        )?;

        // Allocate GPU memory
        let a_gpu = self.allocate_and_copy(a)?;
        let b_gpu = self.allocate_and_copy(b)?;
        let c_gpu = self.allocate_gpu_memory(c.memory_usage())?;

        // Launch kernel
        self.launch_kernel(&kernel, &[a_gpu, b_gpu, c_gpu], config)?;

        // Copy result back to CPU
        self.copy_from_gpu(c, c_gpu)?;

        // Free GPU memory
        self.free_gpu_memory(a_gpu)?;
        self.free_gpu_memory(b_gpu)?;
        self.free_gpu_memory(c_gpu)?;

        Ok(())
    }

    /// Fused attention operation (Flash Attention implementation)
    pub fn flash_attention(
        &mut self,
        query: &Tensor,
        key: &Tensor,
        value: &Tensor,
        output: &mut Tensor,
        config: Option<KernelConfig>,
    ) -> Result<()> {
        let config = config.unwrap_or_default();

        // Validate shapes for attention
        let q_shape = query.shape();
        let k_shape = key.shape();
        let v_shape = value.shape();
        let o_shape = output.shape();

        if q_shape.len() != 3 || k_shape.len() != 3 || v_shape.len() != 3 || o_shape.len() != 3 {
            return Err(TrustformersError::tensor_op_error(
                "Attention requires 3D tensors [batch, seq_len, hidden_dim]",
                "CudaKernels::flash_attention",
            ));
        }

        // Generate Flash Attention kernel
        let kernel_key = format!(
            "flash_attention_{}x{}x{}",
            q_shape[0], q_shape[1], q_shape[2]
        );
        let kernel = self.get_or_compile_kernel(
            &kernel_key,
            &Self::generate_flash_attention_kernel_code(&q_shape),
        )?;

        // Allocate GPU memory
        let q_gpu = self.allocate_and_copy(query)?;
        let k_gpu = self.allocate_and_copy(key)?;
        let v_gpu = self.allocate_and_copy(value)?;
        let o_gpu = self.allocate_gpu_memory(output.memory_usage())?;

        // Launch kernel with optimized configuration for attention
        let attention_config = KernelConfig {
            grid_size: (((q_shape[0] * q_shape[1] + 255) / 256) as u32, 1, 1),
            block_size: (256, 1, 1),
            shared_memory_size: 32 * 1024, // 32KB shared memory for tiling
            stream_id: config.stream_id,
        };

        self.launch_kernel(&kernel, &[q_gpu, k_gpu, v_gpu, o_gpu], attention_config)?;

        // Copy result back
        self.copy_from_gpu(output, o_gpu)?;

        // Free GPU memory
        self.free_gpu_memory(q_gpu)?;
        self.free_gpu_memory(k_gpu)?;
        self.free_gpu_memory(v_gpu)?;
        self.free_gpu_memory(o_gpu)?;

        Ok(())
    }

    /// Fused layer normalization
    pub fn layer_norm(
        &mut self,
        input: &Tensor,
        gamma: &Tensor,
        beta: &Tensor,
        output: &mut Tensor,
        epsilon: f32,
        config: Option<KernelConfig>,
    ) -> Result<()> {
        let config = config.unwrap_or_default();

        let input_shape = input.shape();
        let kernel_key = format!("layer_norm_{}", input_shape.len());
        let kernel = self.get_or_compile_kernel(
            &kernel_key,
            &Self::generate_layer_norm_kernel_code(&input_shape, epsilon),
        )?;

        // Allocate GPU memory
        let input_gpu = self.allocate_and_copy(input)?;
        let gamma_gpu = self.allocate_and_copy(gamma)?;
        let beta_gpu = self.allocate_and_copy(beta)?;
        let output_gpu = self.allocate_gpu_memory(output.memory_usage())?;

        // Launch kernel
        self.launch_kernel(
            &kernel,
            &[input_gpu, gamma_gpu, beta_gpu, output_gpu],
            config,
        )?;

        // Copy result back
        self.copy_from_gpu(output, output_gpu)?;

        // Free GPU memory
        self.free_gpu_memory(input_gpu)?;
        self.free_gpu_memory(gamma_gpu)?;
        self.free_gpu_memory(beta_gpu)?;
        self.free_gpu_memory(output_gpu)?;

        Ok(())
    }

    /// Fused GELU activation
    pub fn fused_gelu(
        &mut self,
        input: &Tensor,
        output: &mut Tensor,
        config: Option<KernelConfig>,
    ) -> Result<()> {
        let config = config.unwrap_or_default();

        let input_shape = input.shape();
        let kernel_key = format!("fused_gelu_{}", input_shape.iter().product::<usize>());
        let kernel =
            self.get_or_compile_kernel(&kernel_key, &Self::generate_fused_gelu_kernel_code())?;

        // Allocate GPU memory
        let input_gpu = self.allocate_and_copy(input)?;
        let output_gpu = self.allocate_gpu_memory(output.memory_usage())?;

        // Launch kernel
        self.launch_kernel(&kernel, &[input_gpu, output_gpu], config)?;

        // Copy result back
        self.copy_from_gpu(output, output_gpu)?;

        // Free GPU memory
        self.free_gpu_memory(input_gpu)?;
        self.free_gpu_memory(output_gpu)?;

        Ok(())
    }

    /// Custom reduction operation
    pub fn reduce_sum(
        &mut self,
        input: &Tensor,
        output: &mut Tensor,
        dim: usize,
        config: Option<KernelConfig>,
    ) -> Result<()> {
        let config = config.unwrap_or_default();

        let input_shape = input.shape();
        let kernel_key = format!("reduce_sum_dim_{}", dim);
        let kernel = self.get_or_compile_kernel(
            &kernel_key,
            &Self::generate_reduce_sum_kernel_code(&input_shape, dim),
        )?;

        // Allocate GPU memory
        let input_gpu = self.allocate_and_copy(input)?;
        let output_gpu = self.allocate_gpu_memory(output.memory_usage())?;

        // Launch kernel
        self.launch_kernel(&kernel, &[input_gpu, output_gpu], config)?;

        // Copy result back
        self.copy_from_gpu(output, output_gpu)?;

        // Free GPU memory
        self.free_gpu_memory(input_gpu)?;
        self.free_gpu_memory(output_gpu)?;

        Ok(())
    }

    /// Get or compile CUDA kernel
    fn get_or_compile_kernel(&mut self, key: &str, kernel_code: &str) -> Result<CompiledKernel> {
        if let Some(kernel) = self.kernel_cache.get(key) {
            Ok(kernel.clone())
        } else {
            let kernel = self.compile_kernel(kernel_code)?;
            self.kernel_cache.insert(key.to_string(), kernel.clone());
            Ok(kernel)
        }
    }

    /// Compile CUDA kernel from source
    fn compile_kernel(&self, kernel_code: &str) -> Result<CompiledKernel> {
        // In a real implementation, this would use NVRTC (NVIDIA Runtime Compilation)
        // For this implementation, we'll simulate compilation
        Ok(CompiledKernel {
            name: "compiled_kernel".to_string(),
            ptx_code: kernel_code.to_string(),
            _function_name: "kernel_func".to_string(),
            _grid_size: (1, 1, 1),
            _block_size: (256, 1, 1),
            _shared_memory_size: 0,
        })
    }

    /// Launch CUDA kernel
    fn launch_kernel(
        &self,
        kernel: &CompiledKernel,
        args: &[usize],
        config: KernelConfig,
    ) -> Result<()> {
        // In a real implementation, this would use CUDA driver API
        // For this implementation, we'll simulate kernel launch
        println!(
            "Launching kernel: {} with {} arguments",
            kernel.name,
            args.len()
        );
        println!(
            "Grid size: {:?}, Block size: {:?}",
            config.grid_size, config.block_size
        );
        Ok(())
    }

    /// Allocate GPU memory and copy data
    fn allocate_and_copy(&self, tensor: &Tensor) -> Result<usize> {
        // In a real implementation, this would use cudaMalloc and cudaMemcpy
        // For this implementation, we'll simulate GPU memory allocation
        let data = tensor.data()?;
        Ok(data.as_ptr() as usize)
    }

    /// Allocate GPU memory
    fn allocate_gpu_memory(&self, size: usize) -> Result<usize> {
        // In a real implementation, this would use cudaMalloc
        Ok(size)
    }

    /// Copy data from GPU to CPU
    fn copy_from_gpu(&self, tensor: &mut Tensor, gpu_ptr: usize) -> Result<()> {
        // In a real implementation, this would use cudaMemcpy
        Ok(())
    }

    /// Free GPU memory
    fn free_gpu_memory(&self, gpu_ptr: usize) -> Result<()> {
        // In a real implementation, this would use cudaFree
        Ok(())
    }

    /// Generate matrix multiplication kernel code
    fn generate_matmul_kernel_code(a_shape: &[usize], b_shape: &[usize]) -> String {
        r#"
extern "C" __global__ void matmul_kernel(
    const float* A, const float* B, float* C,
    int M, int N, int K
) {
    int row = blockIdx.y * blockDim.y + threadIdx.y;
    int col = blockIdx.x * blockDim.x + threadIdx.x;

    if (row < M && col < N) {
        float sum = 0.0f;
        for (int k = 0; k < K; k++) {
            sum += A[row * K + k] * B[k * N + col];
        }
        C[row * N + col] = sum;
    }
}
"#
        .to_string()
    }

    /// Generate Flash Attention kernel code
    fn generate_flash_attention_kernel_code(q_shape: &[usize]) -> String {
        r#"
extern "C" __global__ void flash_attention_kernel(
    const float* Q, const float* K, const float* V, float* O,
    int batch_size, int seq_len, int hidden_dim
) {
    int batch = blockIdx.x;
    int seq = blockIdx.y;
    int head = threadIdx.x;

    if (batch < batch_size && seq < seq_len) {
        // Simplified Flash Attention implementation
        // In practice, this would be much more complex with tiling
        float sum = 0.0f;
        float max_val = -FLT_MAX;

        // Compute attention scores
        for (int k = 0; k < seq_len; k++) {
            float score = 0.0f;
            for (int d = 0; d < hidden_dim; d++) {
                score += Q[batch * seq_len * hidden_dim + seq * hidden_dim + d] *
                         K[batch * seq_len * hidden_dim + k * hidden_dim + d];
            }
            max_val = fmaxf(max_val, score);
        }

        // Compute softmax and output
        float exp_sum = 0.0f;
        for (int k = 0; k < seq_len; k++) {
            float score = 0.0f;
            for (int d = 0; d < hidden_dim; d++) {
                score += Q[batch * seq_len * hidden_dim + seq * hidden_dim + d] *
                         K[batch * seq_len * hidden_dim + k * hidden_dim + d];
            }
            exp_sum += expf(score - max_val);
        }

        for (int d = 0; d < hidden_dim; d++) {
            float output_val = 0.0f;
            for (int k = 0; k < seq_len; k++) {
                float score = 0.0f;
                for (int d2 = 0; d2 < hidden_dim; d2++) {
                    score += Q[batch * seq_len * hidden_dim + seq * hidden_dim + d2] *
                             K[batch * seq_len * hidden_dim + k * hidden_dim + d2];
                }
                float attention_weight = expf(score - max_val) / exp_sum;
                output_val += attention_weight * V[batch * seq_len * hidden_dim + k * hidden_dim + d];
            }
            O[batch * seq_len * hidden_dim + seq * hidden_dim + d] = output_val;
        }
    }
}
"#.to_string()
    }

    /// Generate layer normalization kernel code
    fn generate_layer_norm_kernel_code(input_shape: &[usize], epsilon: f32) -> String {
        format!(
            r#"
extern "C" __global__ void layer_norm_kernel(
    const float* input, const float* gamma, const float* beta, float* output,
    int batch_size, int seq_len, int hidden_dim
) {{
    int batch = blockIdx.x;
    int seq = blockIdx.y;
    int tid = threadIdx.x;

    if (batch < batch_size && seq < seq_len) {{
        __shared__ float shared_data[1024];

        // Compute mean
        float sum = 0.0f;
        for (int i = tid; i < hidden_dim; i += blockDim.x) {{
            sum += input[batch * seq_len * hidden_dim + seq * hidden_dim + i];
        }}
        shared_data[tid] = sum;
        __syncthreads();

        // Reduce sum
        for (int stride = blockDim.x / 2; stride > 0; stride /= 2) {{
            if (tid < stride) {{
                shared_data[tid] += shared_data[tid + stride];
            }}
            __syncthreads();
        }}

        float mean = shared_data[0] / hidden_dim;

        // Compute variance
        float var_sum = 0.0f;
        for (int i = tid; i < hidden_dim; i += blockDim.x) {{
            float diff = input[batch * seq_len * hidden_dim + seq * hidden_dim + i] - mean;
            var_sum += diff * diff;
        }}
        shared_data[tid] = var_sum;
        __syncthreads();

        // Reduce variance
        for (int stride = blockDim.x / 2; stride > 0; stride /= 2) {{
            if (tid < stride) {{
                shared_data[tid] += shared_data[tid + stride];
            }}
            __syncthreads();
        }}

        float variance = shared_data[0] / hidden_dim;
        float eps = {};
        float std_dev = sqrtf(variance + eps);

        // Normalize
        for (int i = tid; i < hidden_dim; i += blockDim.x) {{
            float normalized = (input[batch * seq_len * hidden_dim + seq * hidden_dim + i] - mean) / std_dev;
            output[batch * seq_len * hidden_dim + seq * hidden_dim + i] =
                normalized * gamma[i] + beta[i];
        }}
    }}
}}
"#,
            epsilon
        )
    }

    /// Generate fused GELU kernel code
    fn generate_fused_gelu_kernel_code() -> String {
        r#"
extern "C" __global__ void fused_gelu_kernel(const float* input, float* output, int size) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < size) {
        float x = input[idx];
        // GELU approximation: 0.5 * x * (1 + tanh(sqrt(2/pi) * (x + 0.044715 * x^3)))
        float x_cubed = x * x * x;
        float tanh_arg = 0.797885f * (x + 0.044715f * x_cubed);
        float tanh_val = tanhf(tanh_arg);
        output[idx] = 0.5f * x * (1.0f + tanh_val);
    }
}
"#
        .to_string()
    }

    /// Generate reduce sum kernel code
    fn generate_reduce_sum_kernel_code(input_shape: &[usize], dim: usize) -> String {
        r#"
extern "C" __global__ void reduce_sum_kernel(
    const float* input, float* output,
    int total_size, int reduce_size, int stride
) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < total_size / reduce_size) {
        float sum = 0.0f;
        int base_idx = idx * stride;
        for (int i = 0; i < reduce_size; i++) {
            sum += input[base_idx + i];
        }
        output[idx] = sum;
    }
}
"#
        .to_string()
    }

    /// Get device information
    pub fn get_device_info(&self, device_id: usize) -> Result<&CudaDevice> {
        self.devices.get(device_id).ok_or_else(|| {
            TrustformersError::tensor_op_error(
                &format!("Device {} not found", device_id),
                "CudaKernels::get_device",
            )
        })
    }

    /// Get memory usage statistics
    pub fn get_memory_stats(&self, device_id: usize) -> Result<(u64, u64, u64)> {
        let pool = self.memory_pools.get(&device_id).ok_or_else(|| {
            TrustformersError::tensor_op_error(
                &format!("Memory pool for device {} not found", device_id),
                "CudaKernels::allocate_and_copy",
            )
        })?;

        let pool_guard = pool.lock().expect("Lock poisoned");
        Ok((pool_guard.total_allocated, pool_guard.peak_allocated, 0))
    }

    /// Set device for subsequent operations
    pub fn set_device(&mut self, device_id: usize) -> Result<()> {
        if device_id >= self.devices.len() {
            return Err(TrustformersError::tensor_op_error(
                &format!("Device {} not available", device_id),
                "CudaKernels::set_device",
            ));
        }

        self.context = Some(CudaContext::new(device_id)?);
        Ok(())
    }
}

impl CudaContext {
    fn new(device_id: usize) -> Result<Self> {
        Ok(Self {
            device_id,
            _stream: CudaStream::new(0, 0)?,
        })
    }
}

impl CudaStream {
    fn new(id: usize, priority: i32) -> Result<Self> {
        Ok(Self {
            id,
            _priority: priority,
        })
    }
}

impl CudaMemoryPool {
    fn new(device_id: usize) -> Result<Self> {
        Ok(Self {
            device_id,
            _allocated_blocks: HashMap::new(),
            _free_blocks: Vec::new(),
            total_allocated: 0,
            peak_allocated: 0,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cuda_kernel_creation() {
        let cuda_kernel = CudaKernel::new();
        assert!(cuda_kernel.is_ok());
    }

    #[test]
    fn test_device_enumeration() {
        let devices = CudaKernel::enumerate_devices().expect("operation failed in test");
        assert!(!devices.is_empty());
        assert_eq!(devices[0].name, "NVIDIA RTX 4090");
        assert_eq!(devices[0].compute_capability, (8, 9));
    }

    #[test]
    fn test_kernel_config_default() {
        let config = KernelConfig::default();
        assert_eq!(config.grid_size, (1, 1, 1));
        assert_eq!(config.block_size, (256, 1, 1));
        assert_eq!(config.shared_memory_size, 0);
    }

    #[test]
    fn test_kernel_code_generation() {
        let matmul_code = CudaKernel::generate_matmul_kernel_code(&[128, 256], &[256, 512]);
        assert!(matmul_code.contains("matmul_kernel"));
        assert!(matmul_code.contains("extern \"C\" __global__"));
    }

    #[test]
    fn test_flash_attention_code_generation() {
        let attention_code = CudaKernel::generate_flash_attention_kernel_code(&[8, 128, 512]);
        assert!(attention_code.contains("flash_attention_kernel"));
        assert!(attention_code.contains("float* Q"));
    }

    #[test]
    fn test_layer_norm_code_generation() {
        let layer_norm_code = CudaKernel::generate_layer_norm_kernel_code(&[8, 128, 512], 1e-5);
        assert!(layer_norm_code.contains("layer_norm_kernel"));
        assert!(layer_norm_code.contains("1e") || layer_norm_code.contains("eps"));
    }

    #[test]
    fn test_gelu_code_generation() {
        let gelu_code = CudaKernel::generate_fused_gelu_kernel_code();
        assert!(gelu_code.contains("fused_gelu_kernel"));
        assert!(gelu_code.contains("tanhf"));
    }

    #[test]
    fn test_reduce_sum_code_generation() {
        let reduce_code = CudaKernel::generate_reduce_sum_kernel_code(&[8, 128, 512], 2);
        assert!(reduce_code.contains("reduce_sum_kernel"));
        assert!(reduce_code.contains("reduce_size"));
    }

    #[test]
    fn test_cuda_memory_pool() {
        let pool = CudaMemoryPool::new(0).expect("operation failed in test");
        assert_eq!(pool.device_id, 0);
        assert_eq!(pool.total_allocated, 0);
        assert_eq!(pool.peak_allocated, 0);
    }

    #[test]
    fn test_cuda_context_creation() {
        let context = CudaContext::new(0).expect("operation failed in test");
        assert_eq!(context.device_id, 0);
    }

    #[test]
    fn test_cuda_stream_creation() {
        let stream = CudaStream::new(0, 0);
        assert!(stream.is_ok());
    }
}