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
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
807
808
//! JIT compilation system for Phase 9
//! フェーズ9用JITコンパイルシステム

use super::core::{
    ComputationGraph, GraphNode, Loadable, Saveable, SerializationError, SerializationResult,
};
use super::model_io::{load, save};
use crate::autograd::Variable;
use crate::tensor::Tensor;
use num_traits::{Float, Zero};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::str::FromStr;

/// Script module for JIT compilation
/// JITコンパイル用スクリプトモジュール
#[derive(Debug, Clone)]
pub struct ScriptModule<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
> {
    pub graph: ComputationGraph<T>,
    #[allow(dead_code)]
    pub parameters: HashMap<String, Variable<T>>,
    #[allow(dead_code)]
    pub buffers: HashMap<String, Variable<T>>,
    pub constants: HashMap<String, T>,
    pub metadata: HashMap<String, String>,
}

impl<
        T: Float
            + FromStr
            + Send
            + Sync
            + 'static
            + ndarray::ScalarOperand
            + num_traits::FromPrimitive,
    > ScriptModule<T>
{
    /// Create new script module
    /// 新しいスクリプトモジュールを作成
    pub fn new() -> Self {
        Self {
            graph: ComputationGraph::new(),
            parameters: HashMap::new(),
            buffers: HashMap::new(),
            constants: HashMap::new(),
            metadata: HashMap::new(),
        }
    }

    /// Add parameter to module
    /// モジュールにパラメータを追加
    pub fn add_parameter(&mut self, name: String, variable: Variable<T>) {
        self.parameters.insert(name, variable);
    }

    /// Add buffer to module
    /// モジュールにバッファを追加
    pub fn add_buffer(&mut self, name: String, variable: Variable<T>) {
        self.buffers.insert(name, variable);
    }

    /// Add constant to module
    /// モジュールに定数を追加
    pub fn add_constant(&mut self, name: String, value: T) {
        self.constants.insert(name, value);
    }

    /// Execute module with given inputs
    /// 指定された入力でモジュールを実行
    pub fn forward(&self, inputs: &[Tensor<T>]) -> SerializationResult<Vec<Tensor<T>>> {
        self.execute_graph(inputs)
    }

    /// Execute module with autograd Variables
    /// 自動微分Variable付きでモジュールを実行
    pub fn forward_autograd(&self, inputs: &[Variable<T>]) -> SerializationResult<Vec<Variable<T>>>
    where
        T: Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
    {
        // Extract tensors from Variables
        let tensor_inputs: Vec<Tensor<T>> = inputs
            .iter()
            .map(|var| var.data().read().unwrap().clone())
            .collect();

        // Execute computation graph
        let tensor_outputs = self.execute_graph(&tensor_inputs)?;

        // Convert back to Variables, preserving gradient requirements
        let variable_outputs: Vec<Variable<T>> = tensor_outputs
            .into_iter()
            .map(|tensor| {
                // Check if any input requires grad
                let requires_grad = inputs.iter().any(|var| var.requires_grad());
                Variable::new(tensor, requires_grad)
            })
            .collect();

        Ok(variable_outputs)
    }

    /// Execute computation graph
    /// 計算グラフを実行
    fn execute_graph(&self, inputs: &[Tensor<T>]) -> SerializationResult<Vec<Tensor<T>>> {
        let mut tensor_cache: HashMap<usize, Tensor<T>> = HashMap::new();

        // Initialize inputs
        for (i, input) in inputs.iter().enumerate() {
            tensor_cache.insert(i, input.clone());
        }

        // Execute nodes in topological order
        for node in &self.graph.nodes {
            let input_tensors: Vec<&Tensor<T>> = node
                .inputs
                .iter()
                .map(|&id| {
                    tensor_cache.get(&id).ok_or_else(|| {
                        SerializationError::FormatError(format!("Invalid input tensor ID: {}", id))
                    })
                })
                .collect::<Result<Vec<_>, _>>()?;

            let output_tensor =
                self.execute_operation(&node.op_type, &input_tensors, &node.attributes)?;

            if !node.outputs.is_empty() {
                tensor_cache.insert(node.outputs[0], output_tensor);
            }
        }

        // Collect outputs
        let mut outputs = Vec::new();
        for output_name in &self.graph.outputs {
            if let Ok(output_id) = output_name.parse::<usize>() {
                if let Some(tensor) = tensor_cache.get(&output_id) {
                    outputs.push(tensor.clone());
                }
            }
        }

        Ok(outputs)
    }

    /// Execute single operation
    /// 単一操作を実行
    fn execute_operation(
        &self,
        op_type: &str,
        inputs: &[&Tensor<T>],
        attributes: &HashMap<String, String>,
    ) -> SerializationResult<Tensor<T>> {
        match op_type {
            "add" => {
                if inputs.len() != 2 {
                    return Err(SerializationError::FormatError(
                        "Add operation requires 2 inputs".to_string(),
                    ));
                }
                // Use manual addition for now
                let mut result = inputs[0].clone();
                result.data = &inputs[0].data + &inputs[1].data;
                Ok(result)
            }
            "mul" => {
                if inputs.len() != 2 {
                    return Err(SerializationError::FormatError(
                        "Mul operation requires 2 inputs".to_string(),
                    ));
                }
                // Use manual multiplication for now
                let mut result = inputs[0].clone();
                result.data = &inputs[0].data * &inputs[1].data;
                Ok(result)
            }
            "matmul" => {
                if inputs.len() != 2 {
                    return Err(SerializationError::FormatError(
                        "MatMul operation requires 2 inputs".to_string(),
                    ));
                }
                // Simplified matrix multiplication for JIT - just return first input for now
                Ok(inputs[0].clone())
            }
            "relu" => {
                if inputs.len() != 1 {
                    return Err(SerializationError::FormatError(
                        "ReLU operation requires 1 input".to_string(),
                    ));
                }
                // Use manual ReLU implementation
                let mut result = inputs[0].clone();
                result
                    .data
                    .mapv_inplace(|x| if x > T::zero() { x } else { T::zero() });
                Ok(result)
            }
            "constant" => {
                let value_str = attributes
                    .get("value")
                    .ok_or_else(|| SerializationError::MissingField("value".to_string()))?;
                let value: T = value_str.parse().map_err(|_| {
                    SerializationError::FormatError("Invalid constant value".to_string())
                })?;

                let shape_str = attributes
                    .get("shape")
                    .ok_or_else(|| SerializationError::MissingField("shape".to_string()))?;
                let shape: Vec<usize> = serde_json::from_str(shape_str)
                    .map_err(|e| SerializationError::FormatError(e.to_string()))?;

                let numel: usize = shape.iter().product();
                let data = vec![value; numel];
                Ok(Tensor::from_vec(data, shape))
            }
            "traced_function" => {
                // Handle traced function execution
                if inputs.is_empty() {
                    return Err(SerializationError::FormatError(
                        "Traced function requires at least 1 input".to_string(),
                    ));
                }
                // For traced functions, apply ReLU-like operation as default
                let mut result = inputs[0].clone();
                result
                    .data
                    .mapv_inplace(|x| if x > T::zero() { x } else { T::zero() });
                Ok(result)
            }
            "function_call" => {
                // Handle function call operations from script()
                if inputs.is_empty() {
                    return Err(SerializationError::FormatError(
                        "Function call requires at least 1 input".to_string(),
                    ));
                }
                // For function calls, apply ReLU-like operation as default
                let mut result = inputs[0].clone();
                result
                    .data
                    .mapv_inplace(|x| if x > T::zero() { x } else { T::zero() });
                Ok(result)
            }
            _ => Err(SerializationError::UnsupportedOperation(format!(
                "Unsupported operation: {}",
                op_type
            ))),
        }
    }
}

impl<T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive> Saveable
    for ScriptModule<T>
{
    fn save_binary(&self) -> SerializationResult<Vec<u8>> {
        // Custom serialization without serde
        let mut buffer = Vec::new();

        // Serialize metadata
        let metadata_json = serde_json::to_string(&self.metadata)
            .map_err(|e| SerializationError::FormatError(e.to_string()))?;
        let metadata_bytes = metadata_json.as_bytes();
        buffer.extend_from_slice(&(metadata_bytes.len() as u64).to_le_bytes());
        buffer.extend_from_slice(metadata_bytes);

        // Serialize graph nodes count
        buffer.extend_from_slice(&(self.graph.nodes.len() as u32).to_le_bytes());

        // Serialize parameters
        buffer.extend_from_slice(&(self.parameters.len() as u32).to_le_bytes());
        for (name, variable) in &self.parameters {
            let name_bytes = name.as_bytes();
            buffer.extend_from_slice(&(name_bytes.len() as u32).to_le_bytes());
            buffer.extend_from_slice(name_bytes);

            let tensor_data = variable.data().read().unwrap().save_binary()?;
            buffer.extend_from_slice(&(tensor_data.len() as u64).to_le_bytes());
            buffer.extend_from_slice(&tensor_data);
        }

        // Serialize basic graph structure (simplified)
        buffer.extend_from_slice(&(self.graph.inputs.len() as u32).to_le_bytes());
        for input in &self.graph.inputs {
            let input_bytes = input.as_bytes();
            buffer.extend_from_slice(&(input_bytes.len() as u32).to_le_bytes());
            buffer.extend_from_slice(input_bytes);
        }

        buffer.extend_from_slice(&(self.graph.outputs.len() as u32).to_le_bytes());
        for output in &self.graph.outputs {
            let output_bytes = output.as_bytes();
            buffer.extend_from_slice(&(output_bytes.len() as u32).to_le_bytes());
            buffer.extend_from_slice(output_bytes);
        }

        Ok(buffer)
    }

    fn type_id(&self) -> &'static str {
        "script_module"
    }

    fn metadata(&self) -> HashMap<String, String> {
        let mut meta = self.metadata.clone();
        meta.insert(
            "num_parameters".to_string(),
            self.parameters.len().to_string(),
        );
        meta.insert("num_buffers".to_string(), self.buffers.len().to_string());
        meta.insert("num_nodes".to_string(), self.graph.nodes.len().to_string());
        meta
    }
}

impl<
        T: Float
            + FromStr
            + Send
            + Sync
            + 'static
            + ndarray::ScalarOperand
            + num_traits::FromPrimitive,
    > Loadable for ScriptModule<T>
{
    fn load_binary(data: &[u8]) -> SerializationResult<Self> {
        if data.is_empty() {
            return Ok(Self::new());
        }

        let mut offset = 0;
        let mut module = Self::new();

        // Read metadata length
        if data.len() < offset + 8 {
            return Ok(module); // Not enough data, return empty module
        }
        let metadata_len =
            u64::from_le_bytes(data[offset..offset + 8].try_into().map_err(|_| {
                SerializationError::FormatError("Invalid metadata length".to_string())
            })?) as usize;
        offset += 8;

        // Read metadata
        if data.len() < offset + metadata_len {
            return Ok(module);
        }
        let metadata_str =
            std::str::from_utf8(&data[offset..offset + metadata_len]).map_err(|_| {
                SerializationError::FormatError("Invalid metadata encoding".to_string())
            })?;
        module.metadata = serde_json::from_str(metadata_str)
            .map_err(|e| SerializationError::FormatError(e.to_string()))?;
        offset += metadata_len;

        // Read nodes count
        if data.len() < offset + 4 {
            return Ok(module);
        }
        let nodes_count = u32::from_le_bytes(
            data[offset..offset + 4]
                .try_into()
                .map_err(|_| SerializationError::FormatError("Invalid nodes count".to_string()))?,
        );
        offset += 4;

        // Read parameters
        if data.len() < offset + 4 {
            return Ok(module);
        }
        let params_count =
            u32::from_le_bytes(data[offset..offset + 4].try_into().map_err(|_| {
                SerializationError::FormatError("Invalid parameters count".to_string())
            })?);
        offset += 4;

        for _ in 0..params_count {
            // Read parameter name
            if data.len() < offset + 4 {
                break;
            }
            let name_len =
                u32::from_le_bytes(data[offset..offset + 4].try_into().map_err(|_| {
                    SerializationError::FormatError("Invalid parameter name length".to_string())
                })?) as usize;
            offset += 4;

            if data.len() < offset + name_len {
                break;
            }
            let name =
                String::from_utf8(data[offset..offset + name_len].to_vec()).map_err(|_| {
                    SerializationError::FormatError("Invalid parameter name encoding".to_string())
                })?;
            offset += name_len;

            // Read tensor data
            if data.len() < offset + 8 {
                break;
            }
            let tensor_data_len =
                u64::from_le_bytes(data[offset..offset + 8].try_into().map_err(|_| {
                    SerializationError::FormatError("Invalid tensor data length".to_string())
                })?) as usize;
            offset += 8;

            if data.len() < offset + tensor_data_len {
                break;
            }
            let tensor_data = &data[offset..offset + tensor_data_len];
            if let Ok(tensor) = Tensor::<T>::load_binary(tensor_data) {
                let variable = Variable::new(tensor, false);
                module.parameters.insert(name, variable);
            }
            offset += tensor_data_len;
        }

        // Create a simple node if we saved one
        if nodes_count > 0 {
            let node = GraphNode {
                id: 0,
                op_type: "function_call".to_string(),
                inputs: vec![0],
                outputs: vec![1],
                attributes: HashMap::new(),
            };
            module.graph.add_node(node);
            module.graph.inputs.push("input_0".to_string());
            module.graph.outputs.push("output_0".to_string());
        }

        Ok(module)
    }

    fn expected_type_id() -> &'static str {
        "script_module"
    }
}

/// Function wrapper for JIT compilation
/// JITコンパイル用関数ラッパー
pub type JitFunction<T> = Box<dyn Fn(&[Tensor<T>]) -> Vec<Tensor<T>>>;

/// Script function from Rust closure
/// Rustクロージャからスクリプト関数を作成
pub fn script<T, F>(func: F) -> SerializationResult<ScriptModule<T>>
where
    T: Float + FromStr + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
    F: Fn(&[Tensor<T>]) -> Vec<Tensor<T>> + 'static,
{
    // For now, create a simple module that wraps the function
    // In a full implementation, this would analyze the function and create a graph
    let mut module = ScriptModule::new();

    // Add metadata about the scripted function
    module
        .metadata
        .insert("type".to_string(), "scripted_function".to_string());
    module.metadata.insert(
        "created_at".to_string(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs()
            .to_string(),
    );

    // Create a simple linear graph representing the function
    // This is a simplified implementation - real JIT would analyze the closure
    let node = GraphNode {
        id: 0,
        op_type: "function_call".to_string(),
        inputs: vec![0],  // Input tensor
        outputs: vec![1], // Output tensor
        attributes: HashMap::new(),
    };

    module.graph.add_node(node);
    module.graph.inputs.push("input_0".to_string());
    module.graph.outputs.push("output_0".to_string());

    Ok(module)
}

/// Trace function execution to create JIT module
/// 関数実行をトレースしてJITモジュールを作成
pub fn trace<T, F>(func: F, example_inputs: &[Tensor<T>]) -> SerializationResult<ScriptModule<T>>
where
    T: Float + FromStr + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
    F: Fn(&[Tensor<T>]) -> Vec<Tensor<T>> + 'static,
{
    // Execute function with example inputs to trace operations
    let outputs = func(example_inputs);

    let mut module = ScriptModule::new();

    // Add metadata about the traced function
    module
        .metadata
        .insert("type".to_string(), "traced_function".to_string());
    module.metadata.insert(
        "input_shapes".to_string(),
        serde_json::to_string(
            &example_inputs
                .iter()
                .map(|t| t.shape().to_vec())
                .collect::<Vec<_>>(),
        )
        .unwrap_or_default(),
    );
    module.metadata.insert(
        "output_shapes".to_string(),
        serde_json::to_string(
            &outputs
                .iter()
                .map(|t| t.shape().to_vec())
                .collect::<Vec<_>>(),
        )
        .unwrap_or_default(),
    );

    // Create graph nodes based on traced execution
    for (i, input) in example_inputs.iter().enumerate() {
        module.graph.inputs.push(format!("input_{}", i));
        module.add_buffer(
            format!("traced_input_{}", i),
            Variable::new(input.clone(), false),
        );
    }

    for (i, output) in outputs.iter().enumerate() {
        module
            .graph
            .outputs
            .push(format!("{}", example_inputs.len() + i));
        module.add_buffer(
            format!("traced_output_{}", i),
            Variable::new(output.clone(), false),
        );
    }

    // Add a traced operation node
    let node = GraphNode {
        id: 0,
        op_type: "traced_function".to_string(),
        inputs: (0..example_inputs.len()).collect(),
        outputs: (example_inputs.len()..example_inputs.len() + outputs.len()).collect(),
        attributes: HashMap::new(),
    };

    module.graph.add_node(node);

    Ok(module)
}

/// JIT compilation cache
/// JITコンパイルキャッシュ
pub struct JitCache<
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
> {
    cached_modules: HashMap<String, ScriptModule<T>>,
    cache_dir: std::path::PathBuf,
}

impl<
        T: Float
            + FromStr
            + Send
            + Sync
            + 'static
            + ndarray::ScalarOperand
            + num_traits::FromPrimitive,
    > JitCache<T>
{
    /// Create new JIT cache
    /// 新しいJITキャッシュを作成
    pub fn new<P: AsRef<std::path::Path>>(cache_dir: P) -> SerializationResult<Self> {
        let cache_dir = cache_dir.as_ref().to_path_buf();
        std::fs::create_dir_all(&cache_dir)?;

        Ok(Self {
            cached_modules: HashMap::new(),
            cache_dir,
        })
    }

    /// Get cached module
    /// キャッシュされたモジュールを取得
    pub fn get(&mut self, key: &str) -> Option<&ScriptModule<T>> {
        if !self.cached_modules.contains_key(key) {
            // Try to load from disk
            let cache_path = self.cache_dir.join(format!("{}.rustorch", key));
            if let Ok(module) = load::<_, ScriptModule<T>>(cache_path) {
                self.cached_modules.insert(key.to_string(), module);
            }
        }

        self.cached_modules.get(key)
    }

    /// Store module in cache
    /// モジュールをキャッシュに保存
    pub fn store(&mut self, key: String, module: ScriptModule<T>) -> SerializationResult<()> {
        let cache_path = self.cache_dir.join(format!("{}.rustorch", key));
        save(&module, &cache_path)?;
        self.cached_modules.insert(key, module);
        Ok(())
    }

    /// Clear cache
    /// キャッシュをクリア
    pub fn clear(&mut self) -> SerializationResult<()> {
        self.cached_modules.clear();

        // Remove cache files
        for entry in std::fs::read_dir(&self.cache_dir)? {
            let entry = entry?;
            if entry.path().extension().and_then(|s| s.to_str()) == Some("rustorch") {
                std::fs::remove_file(entry.path())?;
            }
        }

        Ok(())
    }
}

/// Graph optimization utilities
/// グラフ最適化ユーティリティ
pub mod optimization {
    use super::*;

    /// Optimize computation graph
    /// 計算グラフを最適化
    pub fn optimize_graph<T: Float>(graph: &mut ComputationGraph<T>) -> SerializationResult<()> {
        // Dead code elimination
        eliminate_dead_nodes(graph)?;

        // Constant folding
        fold_constants(graph)?;

        // Operation fusion
        fuse_operations(graph)?;

        Ok(())
    }

    /// Remove unreachable nodes
    /// 到達不可能ノードを削除
    fn eliminate_dead_nodes<T: Float>(graph: &mut ComputationGraph<T>) -> SerializationResult<()> {
        let mut reachable = std::collections::HashSet::new();
        let mut stack = Vec::new();

        // Mark output nodes as reachable
        for output_name in &graph.outputs {
            if let Ok(output_id) = output_name.parse::<usize>() {
                if output_id < graph.nodes.len() {
                    stack.push(output_id);
                    reachable.insert(output_id);
                }
            }
        }

        // Mark all nodes reachable from outputs
        while let Some(node_id) = stack.pop() {
            if let Some(node) = graph.nodes.get(node_id) {
                for &input_id in &node.inputs {
                    if input_id < graph.nodes.len() && !reachable.contains(&input_id) {
                        reachable.insert(input_id);
                        stack.push(input_id);
                    }
                }
            }
        }

        // Remove unreachable nodes
        graph.nodes.retain(|node| reachable.contains(&node.id));

        Ok(())
    }

    /// Fold constant operations
    /// 定数操作を畳み込み
    fn fold_constants<T: Float>(graph: &mut ComputationGraph<T>) -> SerializationResult<()> {
        // Simplified constant folding - in practice this would be more sophisticated
        for node in &mut graph.nodes {
            if node.op_type == "constant" {
                // Mark as foldable
                node.attributes
                    .insert("folded".to_string(), "true".to_string());
            }
        }
        Ok(())
    }

    /// Fuse compatible operations
    /// 互換操作を融合
    fn fuse_operations<T: Float>(graph: &mut ComputationGraph<T>) -> SerializationResult<()> {
        // Look for fusion opportunities (e.g., add + relu -> add_relu)
        let mut fused_nodes = Vec::new();

        for i in 0..graph.nodes.len() {
            if graph.nodes[i].op_type == "add" {
                // Look for following ReLU
                for j in i + 1..graph.nodes.len() {
                    if graph.nodes[j].op_type == "relu"
                        && graph.nodes[j].inputs.contains(&graph.nodes[i].outputs[0])
                    {
                        // Create fused node
                        let fused_node = GraphNode {
                            id: graph.nodes.len(),
                            op_type: "add_relu".to_string(),
                            inputs: graph.nodes[i].inputs.clone(),
                            outputs: graph.nodes[j].outputs.clone(),
                            attributes: HashMap::new(),
                        };

                        fused_nodes.push((i, j, fused_node));
                        break;
                    }
                }
            }
        }

        // Apply fusions (would need more sophisticated implementation)
        for (_add_idx, _relu_idx, fused_node) in fused_nodes {
            // In practice, we'd remove the original nodes and add the fused one
            // This is a simplified placeholder
            graph.nodes.push(fused_node);
        }

        Ok(())
    }
}

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

    #[test]
    fn test_script_module_creation() {
        let module = ScriptModule::<f32>::new();
        assert!(module.parameters.is_empty());
        assert!(module.graph.nodes.is_empty());
    }

    #[test]
    fn test_script_module_parameters() {
        let mut module = ScriptModule::<f32>::new();
        let tensor = Tensor::from_vec(vec![1.0, 2.0], vec![2]);
        let variable = Variable::new(tensor, true);

        module.add_parameter("weight".to_string(), variable);
        assert_eq!(module.parameters.len(), 1);
        assert!(module.parameters.contains_key("weight"));
    }

    #[test]
    fn test_jit_cache() {
        let temp_dir = std::env::temp_dir().join("rustorch_test_cache");
        let mut cache = JitCache::<f32>::new(&temp_dir).unwrap();

        let module = ScriptModule::<f32>::new();
        cache.store("test_module".to_string(), module).unwrap();

        assert!(cache.get("test_module").is_some());

        // Cleanup
        std::fs::remove_dir_all(temp_dir).ok();
    }

    #[test]
    fn test_script_function() {
        let module = script(|inputs: &[Tensor<f32>]| {
            if inputs.is_empty() {
                vec![]
            } else {
                vec![inputs[0].clone()]
            }
        })
        .unwrap();

        assert_eq!(module.type_id(), "script_module");
        assert!(!module.graph.nodes.is_empty());
    }

    #[test]
    fn test_trace_function() {
        let example_input = Tensor::from_vec(vec![1.0f32, 2.0], vec![2]);
        let inputs = vec![example_input];

        let module = trace(
            |inputs: &[Tensor<f32>]| {
                if inputs.is_empty() {
                    vec![]
                } else {
                    vec![inputs[0].clone()]
                }
            },
            &inputs,
        )
        .unwrap();

        assert_eq!(module.type_id(), "script_module");
        assert!(!module.graph.inputs.is_empty());
        assert!(!module.graph.outputs.is_empty());
    }
}