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
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
// テンソルプール - メモリ再利用による高速化
// Tensor pool - acceleration through memory reuse

use crate::common::RusTorchResult;
use crate::hybrid_f32::tensor::core::F32Tensor;
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant};

/// プールサイズ設定
/// Pool size configuration
#[derive(Debug, Clone)]
pub struct PoolConfig {
    /// 最大プールサイズ
    /// Maximum pool size
    pub max_pool_size: usize,

    /// 形状ごとの最大保持数
    /// Maximum retained per shape
    pub max_per_shape: usize,

    /// プールアイテムの最大生存時間
    /// Maximum lifetime for pool items
    pub max_lifetime: Duration,

    /// ガベージコレクション間隔
    /// Garbage collection interval
    pub gc_interval: Duration,

    /// メモリ圧縮しきい値
    /// Memory compression threshold
    pub compression_threshold: usize,
}

impl Default for PoolConfig {
    fn default() -> Self {
        PoolConfig {
            max_pool_size: 10000,
            max_per_shape: 100,
            max_lifetime: Duration::from_secs(300), // 5分
            gc_interval: Duration::from_secs(60),   // 1分
            compression_threshold: 1000,
        }
    }
}

/// プールされたテンソル
/// Pooled tensor
#[derive(Debug)]
pub struct PooledTensor {
    /// テンソル本体
    /// Tensor body
    pub tensor: F32Tensor,

    /// プールされた時刻
    /// Time when pooled
    pub pooled_at: Instant,

    /// 使用回数
    /// Usage count
    pub usage_count: usize,

    /// 最後に使用された時刻
    /// Last used time
    pub last_used: Instant,
}

impl PooledTensor {
    pub fn new(tensor: F32Tensor) -> Self {
        let now = Instant::now();
        PooledTensor {
            tensor,
            pooled_at: now,
            usage_count: 0,
            last_used: now,
        }
    }

    /// テンソルを使用としてマーク
    /// Mark tensor as used
    pub fn mark_used(&mut self) {
        self.usage_count += 1;
        self.last_used = Instant::now();
    }

    /// 期限切れかチェック
    /// Check if expired
    pub fn is_expired(&self, max_lifetime: Duration) -> bool {
        self.last_used.elapsed() > max_lifetime
    }
}

/// プール統計情報
/// Pool statistics
#[derive(Debug, Clone)]
pub struct PoolStats {
    /// 現在のプールサイズ
    /// Current pool size
    pub current_size: usize,

    /// 形状別アイテム数
    /// Items per shape
    pub shapes: HashMap<Vec<usize>, usize>,

    /// 総ヒット数
    /// Total hits
    pub total_hits: usize,

    /// 総ミス数
    /// Total misses
    pub total_misses: usize,

    /// ヒット率
    /// Hit rate
    pub hit_rate: f64,

    /// 最後のGC時刻
    /// Last GC time
    pub last_gc: Instant,

    /// GC回数
    /// GC count
    pub gc_count: usize,

    /// 解放されたアイテム数
    /// Released items
    pub items_released: usize,
}

impl Default for PoolStats {
    fn default() -> Self {
        PoolStats {
            current_size: 0,
            shapes: HashMap::new(),
            total_hits: 0,
            total_misses: 0,
            hit_rate: 0.0,
            last_gc: Instant::now(),
            gc_count: 0,
            items_released: 0,
        }
    }
}

impl PoolStats {
    /// ヒット率を更新
    /// Update hit rate
    pub fn update_hit_rate(&mut self) {
        let total = self.total_hits + self.total_misses;
        self.hit_rate = if total > 0 {
            self.total_hits as f64 / total as f64
        } else {
            0.0
        };
    }
}

/// テンソルプール
/// Tensor pool
#[derive(Debug)]
pub struct TensorPool {
    /// 設定
    /// Configuration
    config: PoolConfig,

    /// 形状別プール
    /// Pools by shape
    pools: Arc<Mutex<HashMap<Vec<usize>, VecDeque<PooledTensor>>>>,

    /// 統計情報
    /// Statistics
    stats: Arc<RwLock<PoolStats>>,

    /// 最後のGC時刻
    /// Last GC time
    last_gc: Arc<Mutex<Instant>>,
}

impl TensorPool {
    /// 新しいプールを作成
    /// Create a new pool
    pub fn new(config: PoolConfig) -> Self {
        TensorPool {
            config,
            pools: Arc::new(Mutex::new(HashMap::new())),
            stats: Arc::new(RwLock::new(PoolStats::default())),
            last_gc: Arc::new(Mutex::new(Instant::now())),
        }
    }

    /// デフォルト設定でプールを作成
    /// Create pool with default config
    pub fn with_default_config() -> Self {
        Self::new(PoolConfig::default())
    }

    /// テンソルを取得(ヒットした場合)またはNone(ミスした場合)
    /// Get tensor (if hit) or None (if miss)
    pub fn get(&self, shape: &[usize]) -> Option<F32Tensor> {
        let mut pools = self.pools.lock().unwrap();
        let mut stats = self.stats.write().unwrap();

        if let Some(pool) = pools.get_mut(shape) {
            if let Some(mut pooled) = pool.pop_front() {
                pooled.mark_used();
                stats.total_hits += 1;
                stats.update_hit_rate();

                // 統計の形状カウントを更新
                if let Some(count) = stats.shapes.get_mut(shape) {
                    *count = pool.len();
                }

                return Some(pooled.tensor);
            }
        }

        stats.total_misses += 1;
        stats.update_hit_rate();
        None
    }

    /// テンソルをプールに戻す
    /// Return tensor to pool
    pub fn put(&self, tensor: F32Tensor) -> RusTorchResult<()> {
        let shape = tensor.shape().to_vec();
        let mut pools = self.pools.lock().unwrap();
        let mut stats = self.stats.write().unwrap();

        // プールサイズ制限チェック
        if stats.current_size >= self.config.max_pool_size {
            return Ok(()); // プールが満杯の場合は破棄
        }

        // 形状別プール取得または作成
        let pool = pools.entry(shape.clone()).or_insert_with(VecDeque::new);

        // 形状別制限チェック
        if pool.len() >= self.config.max_per_shape {
            pool.pop_back(); // 古いものを削除
        } else {
            stats.current_size += 1;
        }

        // 新しいプールアイテムを追加
        pool.push_front(PooledTensor::new(tensor));

        // 統計更新
        stats.shapes.insert(shape, pool.len());

        // 定期的なガベージコレクション
        if self.should_gc() {
            drop(stats);
            drop(pools);
            self.garbage_collect()?;
        }

        Ok(())
    }

    /// テンソルを取得(なければ作成)
    /// Get tensor (create if not found)
    pub fn get_or_create<F>(&self, shape: &[usize], creator: F) -> RusTorchResult<F32Tensor>
    where
        F: FnOnce(&[usize]) -> RusTorchResult<F32Tensor>,
    {
        if let Some(tensor) = self.get(shape) {
            Ok(tensor)
        } else {
            creator(shape)
        }
    }

    /// ガベージコレクションが必要かチェック
    /// Check if garbage collection is needed
    fn should_gc(&self) -> bool {
        let last_gc = self.last_gc.lock().unwrap();
        last_gc.elapsed() > self.config.gc_interval
    }

    /// ガベージコレクションを実行
    /// Perform garbage collection
    pub fn garbage_collect(&self) -> RusTorchResult<usize> {
        let mut pools = self.pools.lock().unwrap();
        let mut stats = self.stats.write().unwrap();
        let mut last_gc = self.last_gc.lock().unwrap();

        let mut released_count = 0;
        let now = Instant::now();

        // 期限切れアイテムを削除
        for (shape, pool) in pools.iter_mut() {
            let original_len = pool.len();
            pool.retain(|item| !item.is_expired(self.config.max_lifetime));
            let removed = original_len - pool.len();
            released_count += removed;

            // 統計更新
            stats.shapes.insert(shape.clone(), pool.len());
        }

        // 空のプールを削除
        pools.retain(|_, pool| !pool.is_empty());

        // 統計更新
        stats.current_size = stats.current_size.saturating_sub(released_count);
        stats.last_gc = now;
        stats.gc_count += 1;
        stats.items_released += released_count;

        *last_gc = now;

        Ok(released_count)
    }

    /// プールを完全にクリア
    /// Clear pool completely
    pub fn clear(&self) -> RusTorchResult<()> {
        let mut pools = self.pools.lock().unwrap();
        let mut stats = self.stats.write().unwrap();

        let total_items: usize = pools.values().map(|pool| pool.len()).sum();

        pools.clear();
        stats.current_size = 0;
        stats.shapes.clear();
        stats.items_released += total_items;

        Ok(())
    }

    /// 統計情報を取得
    /// Get statistics
    pub fn stats(&self) -> PoolStats {
        self.stats.read().unwrap().clone()
    }

    /// プール設定を取得
    /// Get pool config
    pub fn config(&self) -> &PoolConfig {
        &self.config
    }

    /// メモリ使用量を推定(バイト)
    /// Estimate memory usage (bytes)
    pub fn estimated_memory_usage(&self) -> usize {
        let pools = self.pools.lock().unwrap();
        let mut total_memory = 0;

        for (shape, pool) in pools.iter() {
            let elements_per_tensor: usize = shape.iter().product();
            let bytes_per_tensor = elements_per_tensor * std::mem::size_of::<f32>();
            total_memory += pool.len() * bytes_per_tensor;
        }

        total_memory
    }

    /// 最も使用頻度の高い形状を取得
    /// Get most frequently used shapes
    pub fn top_shapes(&self, limit: usize) -> Vec<(Vec<usize>, usize)> {
        let stats = self.stats.read().unwrap();
        let mut shapes: Vec<_> = stats
            .shapes
            .iter()
            .map(|(shape, count)| (shape.clone(), *count))
            .collect();

        shapes.sort_by(|a, b| b.1.cmp(&a.1));
        shapes.truncate(limit);
        shapes
    }

    /// プールの健全性をチェック
    /// Check pool health
    pub fn health_check(&self) -> PoolHealthReport {
        let stats = self.stats();
        let config = &self.config;

        PoolHealthReport {
            is_healthy: stats.hit_rate > 0.5 && stats.current_size < config.max_pool_size,
            hit_rate: stats.hit_rate,
            memory_usage: self.estimated_memory_usage(),
            fragmentation_ratio: self.calculate_fragmentation(),
            recommendations: self.generate_recommendations(&stats),
        }
    }

    /// フラグメンテーション率を計算
    /// Calculate fragmentation ratio
    fn calculate_fragmentation(&self) -> f64 {
        let pools = self.pools.lock().unwrap();
        if pools.is_empty() {
            return 0.0;
        }

        let shape_count = pools.len() as f64;
        let avg_per_shape = pools.values().map(|p| p.len()).sum::<usize>() as f64 / shape_count;
        let variance = pools
            .values()
            .map(|p| (p.len() as f64 - avg_per_shape).powi(2))
            .sum::<f64>()
            / shape_count;

        variance.sqrt() / avg_per_shape.max(1.0)
    }

    /// 最適化の推奨事項を生成
    /// Generate optimization recommendations
    fn generate_recommendations(&self, stats: &PoolStats) -> Vec<String> {
        let mut recommendations = Vec::new();

        if stats.hit_rate < 0.3 {
            recommendations.push(
                "ヒット率が低いです。プールサイズを増やすかGC間隔を調整してください".to_string(),
            );
        }

        if stats.current_size > self.config.max_pool_size * 9 / 10 {
            recommendations.push(
                "プールが満杯に近いです。最大サイズを増やすかGCを頻繁に実行してください"
                    .to_string(),
            );
        }

        let fragmentation = self.calculate_fragmentation();
        if fragmentation > 2.0 {
            recommendations
                .push("フラグメンテーションが高いです。形状の分散を見直してください".to_string());
        }

        if recommendations.is_empty() {
            recommendations.push("プールは健全に動作しています".to_string());
        }

        recommendations
    }
}

/// プールの健全性レポート
/// Pool health report
#[derive(Debug)]
pub struct PoolHealthReport {
    pub is_healthy: bool,
    pub hit_rate: f64,
    pub memory_usage: usize,
    pub fragmentation_ratio: f64,
    pub recommendations: Vec<String>,
}

// グローバルテンソルプール / Global tensor pool
lazy_static::lazy_static! {
    static ref GLOBAL_POOL: TensorPool = TensorPool::with_default_config();
}

/// グローバルプールを取得
/// Get global pool
pub fn global_pool() -> &'static TensorPool {
    &GLOBAL_POOL
}

/// プールヘルパー関数
/// Pool helper functions
pub mod helpers {
    use super::*;

    /// プールからテンソルを取得またはゼロテンソルを作成
    /// Get tensor from pool or create zeros
    pub fn get_or_zeros(shape: &[usize]) -> RusTorchResult<F32Tensor> {
        global_pool().get_or_create(shape, |shape| F32Tensor::zeros(shape))
    }

    /// プールからテンソルを取得またはワンテンソルを作成
    /// Get tensor from pool or create ones
    pub fn get_or_ones(shape: &[usize]) -> RusTorchResult<F32Tensor> {
        global_pool().get_or_create(shape, |shape| F32Tensor::ones(shape))
    }

    /// プールからテンソルを取得またはランダムテンソルを作成
    /// Get tensor from pool or create random
    pub fn get_or_randn(shape: &[usize]) -> RusTorchResult<F32Tensor> {
        global_pool().get_or_create(shape, |shape| F32Tensor::randn(shape))
    }

    /// テンソルをグローバルプールに戻す
    /// Return tensor to global pool
    pub fn put_tensor(tensor: F32Tensor) -> RusTorchResult<()> {
        global_pool().put(tensor)
    }

    /// グローバルプールの統計を取得
    /// Get global pool statistics
    pub fn pool_stats() -> PoolStats {
        global_pool().stats()
    }

    /// グローバルプールのガベージコレクション
    /// Garbage collect global pool
    pub fn gc_global_pool() -> RusTorchResult<usize> {
        global_pool().garbage_collect()
    }
}

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

    #[test]
    fn test_pool_basic_operations() {
        let pool = TensorPool::with_default_config();
        let shape = vec![2, 3];

        // ミス(最初は空)
        assert!(pool.get(&shape).is_none());

        // テンソルを作成してプールに入れる
        let tensor = F32Tensor::zeros(&shape).unwrap();
        pool.put(tensor).unwrap();

        // ヒット
        let retrieved = pool.get(&shape);
        assert!(retrieved.is_some());

        let stats = pool.stats();
        assert_eq!(stats.total_hits, 1);
        assert_eq!(stats.total_misses, 1);
        assert_eq!(stats.hit_rate, 0.5);
    }

    #[test]
    fn test_pool_gc() {
        let mut config = PoolConfig::default();
        config.max_lifetime = Duration::from_millis(1); // 短い生存時間
        let pool = TensorPool::new(config);

        // テンソルを追加
        let tensor = F32Tensor::zeros(&[2, 2]).unwrap();
        pool.put(tensor).unwrap();

        // 少し待つ
        std::thread::sleep(Duration::from_millis(10));

        // GC実行
        let released = pool.garbage_collect().unwrap();
        assert_eq!(released, 1);

        let stats = pool.stats();
        assert_eq!(stats.current_size, 0);
    }

    #[test]
    fn test_pool_size_limits() {
        let mut config = PoolConfig::default();
        config.max_pool_size = 2;
        config.max_per_shape = 1;
        let pool = TensorPool::new(config);

        // 制限を超えてテンソルを追加
        for i in 0..5 {
            let tensor = F32Tensor::zeros(&[i + 1]).unwrap();
            pool.put(tensor).unwrap();
        }

        let stats = pool.stats();
        assert!(stats.current_size <= 2);
    }

    #[test]
    fn test_helper_functions() {
        let zeros = helpers::get_or_zeros(&[2, 2]).unwrap();
        assert_eq!(zeros.shape(), &[2, 2]);

        let ones = helpers::get_or_ones(&[3, 3]).unwrap();
        assert_eq!(ones.shape(), &[3, 3]);

        // プールに戻す
        helpers::put_tensor(zeros).unwrap();
        helpers::put_tensor(ones).unwrap();

        let stats = helpers::pool_stats();
        assert!(stats.current_size > 0);
    }

    #[test]
    fn test_health_check() {
        let pool = TensorPool::with_default_config();

        // いくつかの操作を実行
        for _ in 0..10 {
            let tensor = F32Tensor::zeros(&[2, 2]).unwrap();
            pool.put(tensor).unwrap();
        }

        for _ in 0..5 {
            pool.get(&[2, 2]);
        }

        let health = pool.health_check();
        assert!(health.hit_rate >= 0.0);
        assert!(!health.recommendations.is_empty());
    }
}