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
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
//! # RusTorch 🚀
//!
//! ```
//! # use rustorch::prelude::*;
//! ```

// Temporary allow attributes for CodeQL compliance
#![allow(clippy::for_kv_map)]
#![allow(clippy::ptr_arg)]
#![allow(clippy::needless_return)]
#![recursion_limit = "256"]
#![allow(clippy::field_reassign_with_default)]
#![allow(clippy::should_implement_trait)]
#![allow(clippy::useless_format)]
#![allow(clippy::box_collection)]
#![allow(clippy::impl_trait_in_params)]
#![allow(clippy::single_char_add_str)]
#![allow(clippy::default_trait_access)]
#![allow(clippy::manual_contains)]
#![allow(clippy::useless_conversion)]
#![allow(clippy::let_and_return)]
#![allow(clippy::missing_safety_doc)]
#![allow(clippy::explicit_iter_loop)]
#![allow(clippy::redundant_field_names)]
#![allow(clippy::manual_range_contains)]
#![allow(clippy::vec_init_then_push)]
#![allow(clippy::collapsible_else_if)]
#![allow(clippy::format_in_format_args)]
#![allow(clippy::manual_clamp)]
#![allow(clippy::borrowed_box)]
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::new_without_default)]
#![allow(clippy::or_fun_call)]
#![allow(clippy::large_enum_variant)]
#![allow(clippy::module_inception)]
#![allow(clippy::clone_on_ref_ptr)]
#![allow(clippy::redundant_closure_call)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::similar_names)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::return_self_not_must_use)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::missing_panics_doc)]
#![allow(unused_imports)]
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(clippy::all)]
#![warn(clippy::correctness)]
// Additional allows for examples, tests, and benches
#![allow(clippy::println_empty_string)]
#![allow(clippy::useless_asref)]
#![allow(clippy::needless_borrow)]
#![allow(clippy::identity_op)]
#![allow(clippy::redundant_closure)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::unnecessary_cast)]
#![allow(clippy::useless_vec)]
#![allow(clippy::unit_arg)]
#![allow(clippy::approx_constant)]
#![allow(clippy::needless_borrows_for_generic_args)]

//!
//! **A production-ready deep learning library in Rust with PyTorch-like API, data validation, debugging tools, and enterprise-grade reliability**
//!
//! RusTorch v0.5.14 is a fully functional deep learning library that leverages Rust's safety and performance,
//! providing comprehensive tensor operations, automatic differentiation, neural network layers,
//! transformer architectures, GPU acceleration, unified error handling system, advanced memory optimization features,
//! data validation & quality assurance, and comprehensive debug & logging systems.
//!
//! ## ✨ Key Features
//!
//! - **🔥 Comprehensive Tensor Operations**: Math operations, broadcasting, indexing, and statistics
//! - **🤖 Transformer Architecture**: Complete transformer implementation with multi-head attention
//! - **⚡ SIMD Optimizations**: AVX2/SSE4.1 vectorized operations for high performance
//! - **🔄 Unified Parallel Operations**: Trait-based parallel tensor operations with intelligent scheduling
//! - **🎮 GPU Integration**: CUDA/Metal/OpenCL support with automatic device selection
//! - **💾 Advanced Memory Management**: Zero-copy operations, SIMD-aligned allocation, and memory pools
//! - **🧠 Automatic Differentiation**: Tape-based computational graph for gradient computation
//! - **🏗️ Neural Network Layers**: Linear, Conv1d/2d/3d, ConvTranspose, RNN/LSTM/GRU, BatchNorm, Dropout, and more
//! - **🛡️ Unified Error Handling**: Single `RusTorchError` type with 61+ specialized helper functions and `RusTorchResult<T>` for cleaner APIs
//! - **🔧 Safe Operations**: Type-safe tensor operations with comprehensive error handling and ReLU activation
//! - **⚙️ Shared Base Traits**: Reusable convolution and pooling base implementations for code efficiency
//! - **🌐 WebAssembly Support**: Browser-compatible WASM bindings with optimized performance
//! - **🔍 Data Validation & Quality Assurance**: Statistical analysis, anomaly detection, consistency checking, real-time monitoring
//! - **🐛 Comprehensive Debug & Logging**: Structured logging, performance profiling, memory tracking, automated alerts
//! - **💾 Phase 9 Serialization**: Model save/load, JIT compilation, PyTorch compatibility, cross-platform format support
//!
//! ## 🚀 Quick Start
//!
//! ```rust
//! use rustorch::prelude::*;
//!
//! // Create tensors
//! let a = Tensor::from_vec(vec![1.0f32, 2.0, 3.0, 4.0], vec![2, 2]);
//! let b = Tensor::from_vec(vec![5.0f32, 6.0, 7.0, 8.0], vec![2, 2]);
//!
//! // Basic operations
//! let c = &a + &b;  // Addition
//! let d = a.matmul(&b);  // Matrix multiplication
//!
//! // Mathematical functions (using methods from tensor ops)
//! let e = a.data.mapv(|x| x.sin());  // Sine function
//! let f = a.data.mapv(|x| x.exp());  // Exponential function
//!
//! println!("Shape: {:?}", c.shape());
//! println!("Result: {:?}", c.as_slice());
//! ```
//!
//! ## 🔧 Safe Operations with Error Handling
//!
//! ```rust
//! use rustorch::nn::safe_ops::SafeOps;
//!
//! // Create variables safely with validation
//! let var = SafeOps::create_variable(vec![-1.0, 0.0, 1.0], vec![3], false).unwrap();
//!
//! // Apply ReLU activation function
//! let relu_result = SafeOps::relu(&var).unwrap();
//! println!("ReLU: {:?}", relu_result.data().read().unwrap().as_array()); // [0.0, 0.0, 1.0]
//!
//! // Get tensor statistics
//! let stats = SafeOps::get_stats(&var).unwrap();
//! println!("Mean: {:.2}, Std: {:.2}", stats.mean, stats.std_dev());
//! ```
//!
//! ## 🏗️ Architecture Overview
//!
//! The library is organized into several key modules:
//!
//! - [`tensor`]: Core tensor operations with parallel and GPU acceleration
//! - [`nn`]: Neural network layers and building blocks
//!   - [`nn::safe_ops`]: Safe tensor operations with error handling and ReLU activation
//!   - [`nn::conv_base`]: Shared base traits for convolution and pooling layers
//! - [`autograd`]: Automatic differentiation system
//! - [`vision`]: Computer vision utilities including transforms and datasets
//! - [`optim`]: Optimization algorithms (SGD, Adam, etc.)
//! - [`gpu`]: GPU acceleration support (CUDA, Metal, OpenCL)
//! - [`simd`]: SIMD vectorized operations
//! - `wasm`: WebAssembly bindings for browser deployment
//! - [`memory`]: Advanced memory management and pooling
//! - [`data`]: Phase 5 data loading API with modern `Dataset` and `DataLoader` traits
//!
//! ## 🔄 Parallel Operations
//!
//! RusTorch provides a unified trait-based system for parallel tensor operations:
//!
//! ```rust
//! use rustorch::tensor::{Tensor, parallel_traits::*};
//!
//! let tensor1 = Tensor::<f32>::ones(&[4, 4]);  // 2D matrices for simplicity
//! let tensor2 = Tensor::<f32>::ones(&[4, 4]);
//!
//! // Basic tensor operations
//! let result = &tensor1 + &tensor2; // Element-wise addition
//!
//! // Matrix multiplication
//! let matmul_result = tensor1.matmul(&tensor2);
//!
//! // Basic reduction operations
//! let sum = tensor1.sum();
//! # assert_eq!(result.shape(), &[4, 4]);
//! # assert_eq!(matmul_result.unwrap().shape(), &[4, 4]);
//! ```
//!
//! ## 🎮 GPU Integration
//!
//! Seamless GPU acceleration with automatic device selection:
//!
//! ```no_run
//! use rustorch::tensor::Tensor;
//!
//! let tensor1 = Tensor::<f32>::ones(&[4, 4]);
//! let tensor2 = Tensor::<f32>::ones(&[4, 4]);
//!
//! // GPU-accelerated operations (when available)
//! let result = &tensor1 + &tensor2;  // Basic tensor operations
//! ```
//!
//! ## 💾 Memory Optimization
//!
//! Advanced memory management for optimal performance:
//!
//! ```rust
//! use rustorch::tensor::Tensor;
//!
//! let tensor = Tensor::<f32>::ones(&[4, 4]);
//!
//! // Basic tensor operations
//! let result = &tensor * &tensor; // Element-wise multiplication
//! # assert_eq!(result.shape(), &[4, 4]);
//! ```
//!
//! ## 🌐 WebAssembly Integration
//!
//! Run neural networks directly in browsers with optimized WASM bindings:
//!
//! ```javascript
//! // Browser usage (JavaScript)
//! import init, * as rustorch from './pkg/rustorch.js';
//!
//! await init();
//!
//! // Create and manipulate tensors
//! const tensor1 = rustorch.WasmTensor.ones([2, 3]);
//! const tensor2 = rustorch.WasmTensor.random([2, 3]);
//! const sum = tensor1.add(tensor2);
//!
//! // Neural network inference
//! const model = new rustorch.WasmModel();
//! model.add_linear(10, 5, true);
//! model.add_relu();
//!
//! const input = rustorch.WasmTensor.random([1, 10]);
//! const output = model.forward(input);
//!
//! console.log('Output:', output.data());
//! ```

#![allow(missing_docs)]
#![warn(rustdoc::missing_crate_level_docs)]

/// Unified error handling system
/// 統一エラーハンドリングシステム
pub mod error;

/// Automatic Mixed Precision (AMP) training support
/// 自動混合精度(AMP)学習サポート
pub mod amp;
/// Automatic differentiation module
/// 自動微分モジュール
pub mod autograd;
/// Unified compute backend abstraction layer  
/// 統一計算バックエンド抽象化レイヤー
pub mod backends;
/// Common utilities and shared functionality
/// 共通ユーティリティと共有機能
pub mod common;
/// PyTorch to RusTorch conversion system
/// PyTorchからRusTorch変換システム
pub mod convert;
/// Data loading and processing utilities (Phase 5 API)
/// データ読み込みと処理のユーティリティ(フェーズ5 API)
///
/// The Phase 5 API provides modern `Dataset` and `DataLoader` traits with improved
/// performance and ergonomics, replacing legacy APIs.
/// フェーズ5 APIは、パフォーマンスと人間工学を改善した現代的な`Dataset`と`DataLoader`トレイトを提供し、
/// レガシーAPIを置き換えます。
pub mod data;
/// Distributed training support for multi-GPU and multi-machine training
/// マルチGPUおよびマルチマシン学習用分散学習サポート
#[cfg(not(target_arch = "wasm32"))]
pub mod distributed;
/// Statistical distributions module providing PyTorch-compatible probability distributions
/// PyTorch互換の確率分布を提供する統計分布モジュール
pub mod distributions;
/// Data types for tensors
/// テンソル用データ型
pub mod dtype;
/// Model format support and conversion utilities
/// モデル形式サポートと変換ユーティリティ
pub mod formats;
/// GPU acceleration support (CUDA, Metal, OpenCL)
/// GPU加速サポート(CUDA、Metal、OpenCL)
#[cfg(not(target_arch = "wasm32"))]
pub mod gpu;
/// High-performance linear algebra with BLAS integration
/// BLAS統合による高性能線形代数
#[cfg(not(target_arch = "wasm32"))]
pub mod linalg;
/// Memory management and pooling utilities
/// メモリ管理とプーリングユーティリティ
#[cfg(not(target_arch = "wasm32"))]
pub mod memory;
/// Model hub for downloading and managing pretrained models
/// 事前学習済みモデルのダウンロードと管理用モデルハブ
#[cfg(feature = "model-hub")]
pub mod model_hub;
/// Model import functionality for PyTorch and ONNX models
/// PyTorchとONNXモデルのインポート機能
pub mod model_import;
/// Pre-built models and architectures
/// 事前構築モデルとアーキテクチャ
pub mod models;
/// Neural network layers and building blocks
/// ニューラルネットワークレイヤーと構成要素
pub mod nn;
/// Optimization algorithms
/// 最適化アルゴリズム
pub mod optim;
/// Parallel processing utilities
/// 並列処理ユーティリティ
pub mod parallel;
/// Performance profiler
/// パフォーマンスプロファイラー
pub mod profiler;
/// SIMD vectorized operations for performance optimization
/// パフォーマンス最適化のためのSIMDベクトル化操作
#[cfg(not(target_arch = "wasm32"))]
pub mod simd;
/// Special mathematical functions (gamma, Bessel, error functions)
/// 特殊数学関数(ガンマ、ベッセル、誤差関数)
pub mod special;
/// Tensor operations and data structures
/// テンソル操作とデータ構造
pub mod tensor;

// Re-export procedural macros
// 手続き型マクロの再エクスポート
pub use rustorch_macros::tensor_nd;
/// TensorBoard integration
/// TensorBoard統合
pub mod tensorboard;
/// Testing utilities and helpers
/// テストユーティリティとヘルパー
#[cfg(test)]
pub mod test_utils;
/// Training loop abstractions and utilities  
/// 学習ループの抽象化とユーティリティ
///
/// This module provides comprehensive training infrastructure including:
/// - Model checkpoint management for saving and restoring training state
/// - Early stopping implementation to prevent overfitting  
/// - Metrics collection and computation for training monitoring
/// - Generic training loop implementation for various model types
/// - Training state management for session persistence
pub mod training;
/// Utility functions
/// ユーティリティ関数
pub mod utils;
/// Computer vision module providing image transforms, data augmentation, and built-in datasets
/// 画像変換、データ拡張、組み込みデータセットを提供するコンピュータビジョンモジュール
pub mod vision;
/// Visualization tools for plots, graphs, and data analysis
/// プロット、グラフ、データ解析用の可視化ツール
pub mod visualization;

/// Data validation and quality assurance system
/// データ検証・品質保証システム
pub mod validation;

/// Serialization and model I/O system (Phase 9)
/// シリアライゼーション・モデルI/Oシステム(フェーズ9)
pub mod serialization;

/// Debug and logging system
/// デバッグ・ログシステム
pub mod debug;

/// Python bindings (modular architecture)
/// Pythonバインディング(モジュラーアーキテクチャ)
#[cfg(feature = "python")]
pub mod python;

/// Dynamic execution engine for runtime graph optimization
/// 実行時グラフ最適化のための動的実行エンジン
#[cfg(not(target_arch = "wasm32"))]
pub mod execution;

/// Cross-platform optimization module
/// クロスプラットフォーム最適化モジュール
pub mod optimization;

/// Quantization support for model compression and acceleration (Phase 11)
/// モデル圧縮・高速化のための量子化サポート(フェーズ11)
pub mod quantization;

/// Sparse tensor support and operations (Phase 12)
/// スパーステンソルサポートと演算(フェーズ12)
pub mod sparse;

/// WebAssembly support and bindings
/// WebAssemblyサポートとバインディング
#[cfg(feature = "wasm")]
pub mod wasm;

// Simple WebAssembly support for basic operations
// 基本操作のためのシンプルなWebAssemblyサポート
// Removed redundant wasm_simple module - functionality integrated into wasm module

/// f32統一ハイブリッドシステム(実験的)
/// f32 Unified Hybrid System (Experimental)
#[cfg(feature = "hybrid-f32")]
pub mod hybrid_f32;

// hybrid_f32実験マクロをクレートルートレベルで定義
// Define hybrid_f32 experimental macros at crate root level
#[cfg(feature = "hybrid-f32")]
#[macro_export]
macro_rules! hybrid_f32_experimental {
    () => {
        #[cfg(feature = "hybrid-f32")]
        #[cfg(debug_assertions)]
        eprintln!("[HYBRID_F32_EXPERIMENTAL] {}", std::module_path!());
    };
}

/// Re-exports of commonly used items
pub mod prelude {
    pub use crate::autograd::Variable;
    pub use crate::convert::{LayerInfo, LayerType, ModelGraph, ModelParser};
    pub use crate::convert::{
        SimpleConversionError, SimplePyTorchConverter, SimplifiedPyTorchModel,
    };
    pub use crate::data::dataloader::DataLoader;
    pub use crate::data::{Dataset, TensorDataset};
    pub use crate::distributions::{
        Bernoulli, Beta, Categorical, Exponential, Gamma, Normal, Uniform,
    };
    pub use crate::distributions::{Distribution, DistributionTrait};
    #[cfg(not(target_arch = "wasm32"))]
    pub use crate::execution::{DynamicOp, GraphBuilder, RuntimeConfig, RuntimeEngine};
    pub use crate::models::{BERTBuilder, TransformerModel, TransformerModelBuilder, BERT};
    pub use crate::models::{
        CNNBuilder, Model, ModelBuilder, ModelMode, ResNet, ResNetBuilder, CNN,
    };
    pub use crate::models::{InferenceEngine, Metrics, Trainer, TrainingConfig, TrainingResult};
    pub use crate::models::{LSTMModel, LSTMModelBuilder, RNNModel, RNNModelBuilder};
    pub use crate::models::{ModelLoader, ModelSaver, SerializationFormat};
    pub use crate::nn::activation::{
        elu, gelu, hardswish, leaky_relu, mish, relu, selu, sigmoid, softmax, swish, tanh,
    };
    pub use crate::nn::loss::{
        binary_cross_entropy, cross_entropy, huber_loss, mse_loss, nll_loss,
    };
    pub use crate::nn::{
        dropout, AlphaDropout, AvgPool2d, BatchNorm1d, BatchNorm2d, Conv2d, Dropout, GRUCell,
        LSTMCell, Linear, MaxPool2d, Module, RNNCell, GRU, LSTM, RNN,
    };
    pub use crate::optim::{AdaGrad, Adam, Optimizer, RMSprop, SGD};
    pub use crate::quantization::{
        FakeQuantize, HistogramObserver, MinMaxObserver, QATConv2d, QATLinear, QATModule,
        QuantizationScheme, QuantizationType, QuantizedTensor, StaticQuantizer, TensorQuantization,
    };
    pub use crate::sparse::pruning::{ModelPruner, PruningConfig, PruningStrategy};
    pub use crate::sparse::sparse_layers::{
        SparseAttention, SparseConv2d, SparseEmbedding, SparseLinear,
    };
    pub use crate::sparse::{SparseFormat, SparseOps, SparseTensor};
    pub use crate::special::SpecialFunctions;
    pub use crate::special::{bessel_i, bessel_j, bessel_k, bessel_y};
    pub use crate::special::{beta, digamma, gamma, lbeta, lgamma};
    pub use crate::special::{erf, erfc, erfcinv, erfinv};
    pub use crate::tensor::Tensor;
    pub use crate::vision::{
        datasets::*, pipeline::*, presets::*, transforms::*, Image, ImageFormat,
    };
    pub use crate::visualization::{ChartType, ColorMap, PlotConfig, PlotStyle, TensorPlotConfig};
    pub use crate::visualization::{GraphVisualizer, TensorVisualizer, TrainingPlotter};
}

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

    #[test]
    fn test_tensor_creation() {
        let t = tensor::Tensor::from_vec(vec![1.0, 2.0, 3.0], vec![3]);
        assert_eq!(t.size(), vec![3]);
    }
}