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
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
//! hybrid_f32専用エラー処理
//! hybrid_f32-specific error handling

use std::fmt;
use thiserror::Error;

/// hybrid_f32のResult型エイリアス
/// Result type alias for hybrid_f32
pub type F32Result<T> = Result<T, F32Error>;

/// hybrid_f32専用エラー型
/// hybrid_f32-specific error type
#[derive(Error, Debug, Clone)]
pub enum F32Error {
    /// 形状不一致エラー
    /// Shape mismatch error
    #[error("Shape mismatch: {message}")]
    ShapeMismatch { message: String },

    /// インデックスエラー
    /// Index error
    #[error("Index error: {message}")]
    IndexError { message: String },

    /// 次元エラー
    /// Dimension error
    #[error("Dimension error: expected {expected}, got {actual}")]
    DimensionError { expected: usize, actual: usize },

    /// 数値演算エラー
    /// Numerical operation error
    #[error("Numerical error: {message}")]
    NumericalError { message: String },

    /// デバイスエラー
    /// Device error
    #[error("Device error: {message}")]
    DeviceError { message: String },

    /// メモリエラー
    /// Memory error
    #[error("Memory error: {message}")]
    MemoryError { message: String },

    /// 無効な操作
    /// Invalid operation
    #[error("Invalid operation: {message}")]
    InvalidOperation { message: String },

    /// GPU操作エラー
    /// GPU operation error
    #[error("GPU error: {message}")]
    GpuError { message: String },

    /// Neural Engineエラー
    /// Neural Engine error
    #[error("Neural Engine error: {message}")]
    NeuralEngineError { message: String },

    /// 変換エラー
    /// Conversion error
    #[error("Conversion error: {message}")]
    ConversionError { message: String },

    /// データ検証エラー
    /// Data validation error
    #[error("Validation error: {message}")]
    ValidationError { message: String },
}

impl F32Error {
    /// 形状不一致エラーを作成
    /// Create shape mismatch error
    pub fn shape_mismatch<S: Into<String>>(message: S) -> Self {
        Self::ShapeMismatch {
            message: message.into(),
        }
    }

    /// インデックスエラーを作成
    /// Create index error
    pub fn index_error<S: Into<String>>(message: S) -> Self {
        Self::IndexError {
            message: message.into(),
        }
    }

    /// 次元エラーを作成
    /// Create dimension error
    pub fn dimension_error(expected: usize, actual: usize) -> Self {
        Self::DimensionError { expected, actual }
    }

    /// 数値演算エラーを作成
    /// Create numerical error
    pub fn numerical_error<S: Into<String>>(message: S) -> Self {
        Self::NumericalError {
            message: message.into(),
        }
    }

    /// デバイスエラーを作成
    /// Create device error
    pub fn device_error<S: Into<String>>(message: S) -> Self {
        Self::DeviceError {
            message: message.into(),
        }
    }

    /// メモリエラーを作成
    /// Create memory error
    pub fn memory_error<S: Into<String>>(message: S) -> Self {
        Self::MemoryError {
            message: message.into(),
        }
    }

    /// 無効な操作エラーを作成
    /// Create invalid operation error
    pub fn invalid_operation<S: Into<String>>(message: S) -> Self {
        Self::InvalidOperation {
            message: message.into(),
        }
    }

    /// GPUエラーを作成
    /// Create GPU error
    pub fn gpu_error<S: Into<String>>(message: S) -> Self {
        Self::GpuError {
            message: message.into(),
        }
    }

    /// Neural Engineエラーを作成
    /// Create Neural Engine error
    pub fn neural_engine_error<S: Into<String>>(message: S) -> Self {
        Self::NeuralEngineError {
            message: message.into(),
        }
    }

    /// 変換エラーを作成
    /// Create conversion error
    pub fn conversion_error<S: Into<String>>(message: S) -> Self {
        Self::ConversionError {
            message: message.into(),
        }
    }

    /// データ検証エラーを作成
    /// Create validation error
    pub fn validation_error<S: Into<String>>(message: S) -> Self {
        Self::ValidationError {
            message: message.into(),
        }
    }

    /// エラーがリカバリ可能かどうか判定
    /// Determine if error is recoverable
    pub fn is_recoverable(&self) -> bool {
        match self {
            F32Error::ShapeMismatch { .. } => false,
            F32Error::IndexError { .. } => false,
            F32Error::DimensionError { .. } => false,
            F32Error::NumericalError { .. } => true, // 一部回復可能
            F32Error::DeviceError { .. } => true,    // デバイス切替可能
            F32Error::MemoryError { .. } => true,    // メモリ解放後再試行可能
            F32Error::InvalidOperation { .. } => false,
            F32Error::GpuError { .. } => true, // CPU fallback可能
            F32Error::NeuralEngineError { .. } => true, // GPU fallback可能
            F32Error::ConversionError { .. } => false,
            F32Error::ValidationError { .. } => false,
        }
    }

    /// エラーの重要度を取得
    /// Get error severity
    pub fn severity(&self) -> ErrorSeverity {
        match self {
            F32Error::ShapeMismatch { .. } => ErrorSeverity::High,
            F32Error::IndexError { .. } => ErrorSeverity::High,
            F32Error::DimensionError { .. } => ErrorSeverity::High,
            F32Error::NumericalError { .. } => ErrorSeverity::Medium,
            F32Error::DeviceError { .. } => ErrorSeverity::Medium,
            F32Error::MemoryError { .. } => ErrorSeverity::High,
            F32Error::InvalidOperation { .. } => ErrorSeverity::High,
            F32Error::GpuError { .. } => ErrorSeverity::Low, // fallback可能
            F32Error::NeuralEngineError { .. } => ErrorSeverity::Low, // fallback可能
            F32Error::ConversionError { .. } => ErrorSeverity::Medium,
            F32Error::ValidationError { .. } => ErrorSeverity::High,
        }
    }

    /// ユーザー向けメッセージを取得
    /// Get user-friendly message
    pub fn user_message(&self) -> String {
        match self {
            F32Error::ShapeMismatch { .. } =>
                "テンソルの形状が一致しません。演算を行うには同じ形状である必要があります。".to_string(),
            F32Error::IndexError { .. } =>
                "インデックスが範囲外です。有効な範囲内のインデックスを指定してください。".to_string(),
            F32Error::DimensionError { expected, actual } =>
                format!("次元数が不正です。{}次元が必要ですが、{}次元が指定されました。", expected, actual),
            F32Error::NumericalError { .. } =>
                "数値計算でエラーが発生しました。入力値を確認してください。".to_string(),
            F32Error::DeviceError { .. } =>
                "デバイス操作でエラーが発生しました。別のデバイスを試してください。".to_string(),
            F32Error::MemoryError { .. } =>
                "メモリ不足です。データサイズを小さくするか、メモリを解放してください。".to_string(),
            F32Error::InvalidOperation { .. } =>
                "無効な操作です。操作の前提条件を確認してください。".to_string(),
            F32Error::GpuError { .. } =>
                "GPU操作でエラーが発生しました。CPUモードで再試行します。".to_string(),
            F32Error::NeuralEngineError { .. } =>
                "Neural Engine操作でエラーが発生しました。GPUモードで再試行します。".to_string(),
            F32Error::ConversionError { .. } =>
                "データ変換でエラーが発生しました。入力データの形式を確認してください。".to_string(),
            F32Error::ValidationError { .. } =>
                "データ検証でエラーが発生しました。入力データが仕様に適合しているか確認してください。".to_string(),
        }
    }
}

/// エラーの重要度
/// Error severity
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ErrorSeverity {
    Low = 1,
    Medium = 2,
    High = 3,
}

impl fmt::Display for ErrorSeverity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ErrorSeverity::Low => write!(f, "LOW"),
            ErrorSeverity::Medium => write!(f, "MEDIUM"),
            ErrorSeverity::High => write!(f, "HIGH"),
        }
    }
}

/// メインRusTorchErrorからの変換
/// Conversion from main RusTorchError
impl From<crate::error::RusTorchError> for F32Error {
    fn from(error: crate::error::RusTorchError) -> Self {
        use crate::error::RusTorchError;

        match error {
            // 既存のエラータイプマッピング
            RusTorchError::TensorOp { message, .. } => F32Error::invalid_operation(message),
            RusTorchError::ShapeMismatch { expected, actual } => {
                F32Error::shape_mismatch(format!("expected {:?}, got {:?}", expected, actual))
            }
            RusTorchError::Device { device, message } => {
                F32Error::device_error(format!("{}: {}", device, message))
            }
            RusTorchError::BackendUnavailable { backend } => {
                F32Error::device_error(format!("Backend unavailable: {}", backend))
            }
            RusTorchError::MemoryAllocation { size, device } => {
                F32Error::memory_error(format!("Failed to allocate {} bytes on {}", size, device))
            }
            RusTorchError::InvalidParameters { operation, message } => {
                F32Error::invalid_operation(format!("{}: {}", operation, message))
            }
            RusTorchError::InvalidOperation { operation, message } => {
                F32Error::invalid_operation(format!("{}: {}", operation, message))
            }
            RusTorchError::NeuralNetwork { layer, message } => {
                F32Error::neural_engine_error(format!("{}: {}", layer, message))
            }
            RusTorchError::Autograd { message } => F32Error::numerical_error(message),
            RusTorchError::ModelIo { message, .. } => F32Error::conversion_error(message),
            RusTorchError::Import { message, .. } => F32Error::conversion_error(message),
            RusTorchError::DataLoading { message, .. } => F32Error::validation_error(message),
            RusTorchError::Gpu { message, .. } => F32Error::gpu_error(message),
            RusTorchError::Vision { message, .. } => F32Error::invalid_operation(message),
            RusTorchError::Distributed { message, .. } => F32Error::device_error(message),
            RusTorchError::Visualization { message, .. } => F32Error::invalid_operation(message),
            RusTorchError::Profiling { message } => F32Error::invalid_operation(message),
            RusTorchError::Validation { message } => F32Error::validation_error(message),
            RusTorchError::Debug { message } => F32Error::invalid_operation(message),
            RusTorchError::KernelCompilation { message } => F32Error::gpu_error(message),
            RusTorchError::Dataloader { message } => F32Error::validation_error(message),
            RusTorchError::SerializationError { operation, message } => {
                F32Error::conversion_error(format!("{}: {}", operation, message))
            }
            RusTorchError::IO(err) => F32Error::invalid_operation(err.to_string()),
            RusTorchError::NotImplemented { feature } => {
                F32Error::invalid_operation(format!("Not implemented: {}", feature))
            }
            RusTorchError::OutOfMemory {
                requested,
                available,
            } => F32Error::memory_error(format!(
                "Out of memory: requested {} bytes, available {} bytes",
                requested, available
            )),
            RusTorchError::Serialization { message } => F32Error::conversion_error(message),
        }
    }
}

/// F32ErrorからメインRusTorchErrorへの変換
/// Conversion to main RusTorchError
impl From<F32Error> for crate::error::RusTorchError {
    fn from(error: F32Error) -> Self {
        use crate::error::RusTorchError;

        match error {
            F32Error::ShapeMismatch { message } => RusTorchError::ShapeMismatch {
                expected: vec![], // デフォルト値として空のベクター
                actual: vec![],   // 詳細は message に含まれる
            },
            F32Error::IndexError { message } => RusTorchError::InvalidOperation {
                operation: "index".to_string(),
                message,
            },
            F32Error::DimensionError { expected, actual } => RusTorchError::InvalidOperation {
                operation: "dimension_check".to_string(),
                message: format!("expected {}, got {}", expected, actual),
            },
            F32Error::NumericalError { message } => RusTorchError::TensorOp {
                message,
                source: None,
            },
            F32Error::DeviceError { message } => RusTorchError::Device {
                device: "hybrid_f32".to_string(),
                message,
            },
            F32Error::MemoryError { message } => RusTorchError::OutOfMemory {
                requested: 0, // 詳細情報は message に含まれる
                available: 0,
            },
            F32Error::InvalidOperation { message } => RusTorchError::InvalidOperation {
                operation: "hybrid_f32".to_string(),
                message,
            },
            F32Error::GpuError { message } => RusTorchError::Gpu {
                message,
                source: None,
            },
            F32Error::NeuralEngineError { message } => RusTorchError::NeuralNetwork {
                layer: "neural_engine".to_string(),
                message,
            },
            F32Error::ConversionError { message } => RusTorchError::Serialization { message },
            F32Error::ValidationError { message } => RusTorchError::Validation { message },
        }
    }
}

/// エラーコンテキスト用のトレイト
/// Trait for error context
pub trait F32ErrorContext<T> {
    /// エラーにコンテキストを追加
    /// Add context to error
    fn with_context<F, S>(self, f: F) -> F32Result<T>
    where
        F: FnOnce() -> S,
        S: Into<String>;

    /// エラーにコンテキスト文字列を追加
    /// Add context string to error
    fn context<S: Into<String>>(self, msg: S) -> F32Result<T>;
}

impl<T> F32ErrorContext<T> for F32Result<T> {
    fn with_context<F, S>(self, f: F) -> F32Result<T>
    where
        F: FnOnce() -> S,
        S: Into<String>,
    {
        self.map_err(|err| {
            let context = f().into();
            match err {
                F32Error::ShapeMismatch { message } => {
                    F32Error::shape_mismatch(format!("{}: {}", context, message))
                }
                F32Error::IndexError { message } => {
                    F32Error::index_error(format!("{}: {}", context, message))
                }
                F32Error::InvalidOperation { message } => {
                    F32Error::invalid_operation(format!("{}: {}", context, message))
                }
                other => other,
            }
        })
    }

    fn context<S: Into<String>>(self, msg: S) -> F32Result<T> {
        let msg = msg.into();
        self.with_context(|| msg)
    }
}

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

    #[test]
    fn test_error_creation() {
        let err = F32Error::shape_mismatch("test message");
        assert!(matches!(err, F32Error::ShapeMismatch { .. }));
        assert_eq!(err.to_string(), "Shape mismatch: test message");
    }

    #[test]
    fn test_error_severity() {
        assert_eq!(
            F32Error::shape_mismatch("test").severity(),
            ErrorSeverity::High
        );
        assert_eq!(F32Error::gpu_error("test").severity(), ErrorSeverity::Low);
    }

    #[test]
    fn test_error_recoverability() {
        assert!(!F32Error::shape_mismatch("test").is_recoverable());
        assert!(F32Error::gpu_error("test").is_recoverable());
    }

    #[test]
    fn test_error_context() {
        let result: F32Result<i32> = Err(F32Error::shape_mismatch("original"));
        let with_context = result.context("tensor operation");

        assert!(with_context.is_err());
        let err_msg = with_context.unwrap_err().to_string();
        assert!(err_msg.contains("tensor operation"));
        assert!(err_msg.contains("original"));
    }
}