rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
//! Enhanced CUDA cuBLAS integration for high-performance matrix operations
//! 高性能行列演算のための強化されたCUDA cuBLAS統合

use crate::error::{RusTorchError, RusTorchResult};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

#[cfg(feature = "cuda")]
use cudarc::{
    cublas::{CudaBlas, Gemm},
    driver::{CudaDevice, CudaSlice, DevicePtr, LaunchAsync, LaunchConfig},
    nvrtc::compile_ptx,
};

/// Enhanced CUDA matrix operations with cuBLAS optimization
/// cuBLAS最適化による強化されたCUDA行列演算
#[cfg(feature = "cuda")]
pub struct CudaMatrixExecutor {
    device: Arc<CudaDevice>,
    cublas: CudaBlas,
    device_id: usize,
    streams: Vec<cudarc::driver::CudaStream>,
    memory_pool: Arc<Mutex<CudaMemoryPool>>,
}

#[cfg(feature = "cuda")]
impl CudaMatrixExecutor {
    /// Create new CUDA matrix executor with cuBLAS
    /// cuBLASを使用した新しいCUDA行列実行器を作成
    pub fn new(device_id: usize) -> RusTorchResult<Self> {
        let device = CudaDevice::new(device_id).map_err(|e| {
            RusTorchError::tensor_op(format!(
                "Failed to initialize CUDA device {}: {}",
                device_id, e
            ))
        })?;

        // CudaDevice::new() now returns Arc<CudaDevice> in cudarc 0.11+
        let cublas = CudaBlas::new(device.clone())
            .map_err(|e| RusTorchError::tensor_op(format!("Failed to initialize cuBLAS: {}", e)))?;

        // Create multiple streams for concurrent execution
        let mut streams = Vec::new();
        for i in 0..4 {
            let stream = device.fork_default_stream().map_err(|e| {
                RusTorchError::tensor_op(format!("Failed to create CUDA stream {}: {}", i, e))
            })?;
            streams.push(stream);
        }

        let memory_pool = Arc::new(Mutex::new(CudaMemoryPool::new()));

        Ok(Self {
            device,
            cublas,
            device_id,
            streams,
            memory_pool,
        })
    }

    /// Optimized matrix multiplication using cuBLAS SGEMM
    /// cuBLAS SGEMMを使用した最適化された行列乗算
    pub fn matmul_f32(
        &self,
        a: &[f32],
        b: &[f32],
        c: &mut [f32],
        m: usize,
        n: usize,
        k: usize,
        use_tensor_cores: bool,
    ) -> RusTorchResult<()> {
        // Validate dimensions
        if a.len() != m * k || b.len() != k * n || c.len() != m * n {
            return Err(RusTorchError::shape_mismatch(
                &[m, k, k, n, m, n],
                &[a.len(), 0, b.len(), 0, c.len(), 0],
            ));
        }

        // Allocate GPU memory for matrices using new cudarc 0.11+ API
        let a_dev = self.device.htod_copy(a.to_vec()).map_err(|e| {
            RusTorchError::tensor_op(format!("Failed to copy matrix A to GPU: {}", e))
        })?;
        let b_dev = self.device.htod_copy(b.to_vec()).map_err(|e| {
            RusTorchError::tensor_op(format!("Failed to copy matrix B to GPU: {}", e))
        })?;
        let mut c_dev = self.device.alloc_zeros(m * n).map_err(|e| {
            RusTorchError::tensor_op(format!("Failed to allocate GPU memory for result: {}", e))
        })?;

        // Perform GPU matrix multiplication using cuBLAS
        self.perform_standard_gemm(&a_dev, &b_dev, &mut c_dev, m, n, k)?;

        // Copy result back to CPU
        self.device.dtoh_sync_copy_into(&c_dev, c).map_err(|e| {
            RusTorchError::tensor_op(format!("Failed to copy result from GPU: {}", e))
        })?;
        Ok(())
    }

    /// Tensor Core optimized GEMM for Volta+ architectures
    /// Volta+アーキテクチャ向けTensor Core最適化GEMM
    fn perform_tensor_core_gemm(
        &self,
        a: &CudaSlice<f32>,
        b: &CudaSlice<f32>,
        c: &mut CudaSlice<f32>,
        m: usize,
        n: usize,
        k: usize,
    ) -> RusTorchResult<()> {
        // Use mixed precision with Tensor Cores when available
        // This requires converting f32 to f16, performing computation, then back to f32
        // For simplicity, fall back to standard GEMM for now
        self.perform_standard_gemm(a, b, c, m, n, k)
    }

    /// Standard cuBLAS SGEMM implementation
    /// 標準cuBLAS SGEMM実装
    fn perform_standard_gemm(
        &self,
        a: &CudaSlice<f32>,
        b: &CudaSlice<f32>,
        c: &mut CudaSlice<f32>,
        m: usize,
        n: usize,
        k: usize,
    ) -> RusTorchResult<()> {
        // cuBLAS uses column-major order, so we need to transpose
        // C = alpha * A * B + beta * C
        // In column-major: C = alpha * B^T * A^T + beta * C
        let alpha = 1.0f32;
        let beta = 0.0f32;

        // Use new cuBLAS GEMM API with GemmConfig
        use cudarc::cublas::GemmConfig;
        let cfg = GemmConfig {
            transa: cudarc::cublas::sys::cublasOperation_t::CUBLAS_OP_N,
            transb: cudarc::cublas::sys::cublasOperation_t::CUBLAS_OP_N,
            m: m as i32,
            n: n as i32,
            k: k as i32,
            lda: k as i32, // Leading dimension of A
            ldb: n as i32, // Leading dimension of B
            ldc: n as i32, // Leading dimension of C
            alpha,
            beta,
        };

        unsafe {
            self.cublas
                .gemm(cfg, a, b, c)
                .map_err(|e| RusTorchError::tensor_op(format!("cuBLAS SGEMM failed: {}", e)))?;
        }

        Ok(())
    }

    /// Batch matrix multiplication for multiple matrices
    /// 複数行列のバッチ行列乗算
    pub fn batch_matmul_f32(
        &self,
        a_batch: &[Vec<f32>],
        b_batch: &[Vec<f32>],
        c_batch: &mut [Vec<f32>],
        m: usize,
        n: usize,
        k: usize,
    ) -> RusTorchResult<()> {
        if a_batch.len() != b_batch.len() || a_batch.len() != c_batch.len() {
            return Err(RusTorchError::shape_mismatch(
                &[a_batch.len()],
                &[b_batch.len(), c_batch.len()],
            ));
        }

        let batch_size = a_batch.len();
        let stream_count = self.streams.len().min(batch_size);

        // Process batches in parallel using multiple streams
        for (batch_idx, ((a, b), c)) in a_batch
            .iter()
            .zip(b_batch.iter())
            .zip(c_batch.iter_mut())
            .enumerate()
        {
            let stream_idx = batch_idx % stream_count;
            let _stream = &self.streams[stream_idx];

            // For now, process synchronously
            // In production, we'd use asynchronous kernel launches with streams
            self.matmul_f32(a, b, c, m, n, k, false)?;
        }

        // Synchronize all streams
        self.device.synchronize().map_err(|e| {
            RusTorchError::tensor_op(format!("Failed to synchronize CUDA device: {}", e))
        })?;

        Ok(())
    }

    /// Get device memory information
    /// デバイスメモリ情報を取得
    pub fn get_memory_info(&self) -> RusTorchResult<(usize, usize)> {
        // Use device synchronize to ensure context is available, then estimate memory
        // In cudarc 0.11, direct memory querying requires more setup
        let _ = self.device.synchronize();

        // Return a reasonable default - actual memory querying would require
        // additional CUDA context management in cudarc 0.11+
        let total = 8usize * 1024 * 1024 * 1024; // Default to 8GB
        let free = total / 2; // Assume half available

        Ok((free, total))
    }

    /// Get compute capability
    /// 計算能力を取得
    pub fn get_compute_capability(&self) -> RusTorchResult<(i32, i32)> {
        // Get major and minor compute capability
        let major = self.device.attribute(
            cudarc::driver::sys::CUdevice_attribute::CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR,
        ).map_err(|e| {
            RusTorchError::tensor_op(format!("Failed to get compute capability major: {}", e))
        })? as i32;

        let minor = self.device.attribute(
            cudarc::driver::sys::CUdevice_attribute::CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR,
        ).map_err(|e| {
            RusTorchError::tensor_op(format!("Failed to get compute capability minor: {}", e))
        })? as i32;

        Ok((major, minor))
    }
}

/// CUDA memory pool for efficient memory management (future implementation)
/// 効率的なメモリ管理のためのCUDAメモリプール(将来実装)
#[allow(dead_code)]
struct CudaMemoryPool {
    buffers: HashMap<usize, Vec<*mut u8>>,
}

#[allow(dead_code)]
impl CudaMemoryPool {
    fn new() -> Self {
        Self {
            buffers: HashMap::new(),
        }
    }

    fn get_buffer(&mut self, size: usize) -> Option<*mut u8> {
        self.buffers.get_mut(&size)?.pop()
    }

    fn return_buffer(&mut self, ptr: *mut u8, size: usize) {
        self.buffers.entry(size).or_default().push(ptr);
    }
}

/// Non-CUDA fallback implementation
/// CUDA非対応時のフォールバック実装
#[cfg(not(feature = "cuda"))]
pub struct CudaMatrixExecutor;

#[cfg(not(feature = "cuda"))]
impl CudaMatrixExecutor {
    /// Create a new CUDA matrix executor for the specified device
    /// 指定されたデバイス用の新しいCUDA行列実行器を作成
    pub fn new(_device_id: usize) -> RusTorchResult<Self> {
        Err(RusTorchError::UnsupportedDevice(
            "CUDA not available".to_string(),
        ))
    }

    /// Perform matrix multiplication using CUDA cuBLAS
    /// CUDA cuBLASを使用して行列乗算を実行
    #[allow(clippy::too_many_arguments)]
    pub fn matmul_f32(
        &self,
        _a: &[f32],
        _b: &[f32],
        _c: &mut [f32],
        _m: usize,
        _n: usize,
        _k: usize,
        _use_tensor_cores: bool,
    ) -> RusTorchResult<()> {
        Err(RusTorchError::UnsupportedDevice(
            "CUDA not available".to_string(),
        ))
    }

    /// Perform batch matrix multiplication using CUDA cuBLAS
    /// CUDA cuBLASを使用してバッチ行列乗算を実行
    pub fn batch_matmul_f32(
        &self,
        _a_batch: &[Vec<f32>],
        _b_batch: &[Vec<f32>],
        _c_batch: &mut [Vec<f32>],
        _m: usize,
        _n: usize,
        _k: usize,
    ) -> RusTorchResult<()> {
        Err(RusTorchError::UnsupportedDevice(
            "CUDA not available".to_string(),
        ))
    }

    /// Get CUDA device memory information (free, total)
    /// CUDAデバイスメモリ情報を取得(空き容量、総容量)
    pub fn get_memory_info(&self) -> RusTorchResult<(usize, usize)> {
        Err(RusTorchError::UnsupportedDevice(
            "CUDA not available".to_string(),
        ))
    }

    /// Get CUDA compute capability (major, minor)
    /// CUDAコンピュート能力を取得(メジャー、マイナー)
    pub fn get_compute_capability(&self) -> RusTorchResult<(i32, i32)> {
        Err(RusTorchError::UnsupportedDevice(
            "CUDA not available".to_string(),
        ))
    }
}

/// Public interface functions for CUDA operations
/// CUDA演算のためのパブリックインターフェース関数
///
/// Execute CUDA matrix multiplication with cuBLAS
/// cuBLASを使用したCUDA行列乗算を実行
#[allow(clippy::too_many_arguments, clippy::many_single_char_names)] // Matrix dimensions m,n,k and matrices a,b,c are standard in linear algebra
pub fn cuda_matmul_f32(
    a: &[f32],
    b: &[f32],
    c: &mut [f32],
    m: usize,
    n: usize,
    k: usize,
    device_id: usize,
    use_tensor_cores: bool,
) -> RusTorchResult<()> {
    #[cfg(feature = "cuda")]
    {
        let executor = CudaMatrixExecutor::new(device_id)?;
        executor.matmul_f32(a, b, c, m, n, k, use_tensor_cores)
    }
    #[cfg(not(feature = "cuda"))]
    {
        let _ = (a, b, c, m, n, k, device_id, use_tensor_cores);
        Err(RusTorchError::UnsupportedDevice(
            "CUDA not available".to_string(),
        ))
    }
}

/// Execute batch CUDA matrix multiplication
/// バッチCUDA行列乗算を実行
pub fn cuda_batch_matmul_f32(
    a_batch: &[Vec<f32>],
    b_batch: &[Vec<f32>],
    c_batch: &mut [Vec<f32>],
    m: usize,
    n: usize,
    k: usize,
    device_id: usize,
) -> RusTorchResult<()> {
    #[cfg(feature = "cuda")]
    {
        let executor = CudaMatrixExecutor::new(device_id)?;
        executor.batch_matmul_f32(a_batch, b_batch, c_batch, m, n, k)
    }
    #[cfg(not(feature = "cuda"))]
    {
        let _ = (a_batch, b_batch, c_batch, m, n, k, device_id);
        Err(RusTorchError::UnsupportedDevice(
            "CUDA not available".to_string(),
        ))
    }
}

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

    #[test]
    fn test_cuda_executor_creation() {
        let result = CudaMatrixExecutor::new(0);
        #[cfg(not(feature = "cuda"))]
        assert!(result.is_err());
    }

    #[test]
    fn test_cuda_matmul_interface() {
        let a = vec![1.0f32; 64];
        let b = vec![2.0f32; 64];
        let mut c = vec![0.0f32; 64];

        let result = cuda_matmul_f32(&a, &b, &mut c, 8, 8, 8, 0, false);
        #[cfg(not(feature = "cuda"))]
        assert!(result.is_err());
    }
}