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
//! Complex architecture support for PyTorch conversion
//! PyTorch変換の複雑アーキテクチャサポート

use crate::formats::pytorch::TensorData;
use crate::convert::SimpleLayerInfo;
use crate::error::{RusTorchError, RusTorchResult};
use crate::tensor::Tensor;
use std::collections::HashMap;

/// Transformer architecture components
/// Transformerアーキテクチャコンポーネント
#[derive(Debug, Clone)]
pub struct TransformerComponents {
    /// Multi-head attention layers
    /// マルチヘッドアテンションレイヤー
    pub attention_layers: Vec<MultiheadAttentionInfo>,
    /// Feed-forward network layers
    /// フィードフォワードネットワークレイヤー
    pub ffn_layers: Vec<FeedForwardInfo>,
    /// Layer normalization components
    /// レイヤー正規化コンポーネント
    pub layer_norms: Vec<LayerNormInfo>,
    /// Position embeddings
    /// 位置埋め込み
    pub position_embeddings: Option<PositionEmbeddingInfo>,
    /// Token embeddings
    /// トークン埋め込み
    pub token_embeddings: Option<TokenEmbeddingInfo>,
}

/// Multi-head attention layer information (Phase 6 - PyTorch compatible)
/// マルチヘッドアテンションレイヤー情報(フェーズ6 - PyTorch互換)
#[derive(Debug, Clone)]
pub struct MultiheadAttentionInfo {
    /// Layer name
    /// レイヤー名
    pub name: String,
    /// Number of attention heads
    /// アテンションヘッド数
    pub num_heads: usize,
    /// Embedding dimension
    /// 埋め込み次元
    pub embed_dim: usize,
    /// Dimension of each head
    /// 各ヘッドの次元
    pub head_dim: usize,
    /// Dropout probability
    /// ドロップアウト確率
    pub dropout: f32,
    /// Whether to use bias
    /// バイアス使用フラグ
    pub bias: bool,
    /// Batch first flag
    /// バッチファーストフラグ
    pub batch_first: bool,
    /// Query projection weights
    /// クエリ射影重み
    pub query_weights: Tensor<f32>,
    /// Key projection weights
    /// キー射影重み
    pub key_weights: Tensor<f32>,
    /// Value projection weights
    /// 値射影重み
    pub value_weights: Tensor<f32>,
    /// Output projection weights
    /// 出力射影重み
    pub output_weights: Tensor<f32>,
    /// Bias terms
    /// バイアス項
    pub biases: Option<Vec<Tensor<f32>>>,
}

/// Feed-forward network information
/// フィードフォワードネットワーク情報
#[derive(Debug, Clone)]
pub struct FeedForwardInfo {
    /// Layer name
    /// レイヤー名
    pub name: String,
    /// Input dimension
    /// 入力次元
    pub input_dim: usize,
    /// Hidden dimension
    /// 隠れ次元
    pub hidden_dim: usize,
    /// First linear layer weights
    /// 第一線形層重み
    pub linear1_weights: Tensor<f32>,
    /// Second linear layer weights
    /// 第二線形層重み
    pub linear2_weights: Tensor<f32>,
    /// Bias terms
    /// バイアス項
    pub biases: Option<Vec<Tensor<f32>>>,
    /// Activation function type
    /// 活性化関数タイプ
    pub activation: String,
}

/// Layer normalization information
/// レイヤー正規化情報
#[derive(Debug, Clone)]
pub struct LayerNormInfo {
    /// Layer name
    /// レイヤー名
    pub name: String,
    /// Normalized shape
    /// 正規化形状
    pub normalized_shape: Vec<usize>,
    /// Scale parameters (gamma)
    /// スケールパラメータ(ガンマ)
    pub weight: Tensor<f32>,
    /// Shift parameters (beta)
    /// シフトパラメータ(ベータ)
    pub bias: Option<Tensor<f32>>,
    /// Epsilon value
    /// イプシロン値
    pub eps: f32,
}

/// Position embedding information
/// 位置埋め込み情報
#[derive(Debug, Clone)]
pub struct PositionEmbeddingInfo {
    /// Maximum sequence length
    /// 最大シーケンス長
    pub max_length: usize,
    /// Embedding dimension
    /// 埋め込み次元
    pub embed_dim: usize,
    /// Embedding weights
    /// 埋め込み重み
    pub weights: Tensor<f32>,
}

/// Token embedding information
/// トークン埋め込み情報
#[derive(Debug, Clone)]
pub struct TokenEmbeddingInfo {
    /// Vocabulary size
    /// 語彙サイズ
    pub vocab_size: usize,
    /// Embedding dimension
    /// 埋め込み次元
    pub embed_dim: usize,
    /// Embedding weights
    /// 埋め込み重み
    pub weights: Tensor<f32>,
}

/// CNN architecture components
/// CNNアーキテクチャコンポーネント
#[derive(Debug, Clone)]
pub struct CNNComponents {
    /// Convolutional layers
    /// 畳み込みレイヤー
    pub conv_layers: Vec<ConvLayerInfo>,
    /// Pooling layers
    /// プーリングレイヤー
    pub pool_layers: Vec<PoolLayerInfo>,
    /// Batch normalization layers
    /// バッチ正規化レイヤー
    pub batch_norms: Vec<BatchNormInfo>,
    /// Classification head
    /// 分類ヘッド
    pub classifier: Option<ClassifierInfo>,
}

/// Convolutional layer information
/// 畳み込みレイヤー情報
#[derive(Debug, Clone)]
pub struct ConvLayerInfo {
    /// Layer name
    /// レイヤー名
    pub name: String,
    /// Input channels
    /// 入力チャンネル
    pub in_channels: usize,
    /// Output channels
    /// 出力チャンネル
    pub out_channels: usize,
    /// Kernel size
    /// カーネルサイズ
    pub kernel_size: (usize, usize),
    /// Stride
    /// ストライド
    pub stride: (usize, usize),
    /// Padding
    /// パディング
    pub padding: (usize, usize),
    /// Dilation
    /// 膨張
    pub dilation: (usize, usize),
    /// Groups
    /// グループ
    pub groups: usize,
    /// Weights
    /// 重み
    pub weights: Tensor<f32>,
    /// Bias
    /// バイアス
    pub bias: Option<Tensor<f32>>,
}

/// Pooling layer information
/// プーリングレイヤー情報
#[derive(Debug, Clone)]
pub struct PoolLayerInfo {
    /// Layer name
    /// レイヤー名
    pub name: String,
    /// Pooling type (Max, Average, Adaptive)
    /// プーリングタイプ(最大、平均、適応)
    pub pool_type: PoolType,
    /// Kernel size
    /// カーネルサイズ
    pub kernel_size: (usize, usize),
    /// Stride
    /// ストライド
    pub stride: Option<(usize, usize)>,
    /// Padding
    /// パディング
    pub padding: Option<(usize, usize)>,
}

/// Pooling types
/// プーリングタイプ
#[derive(Debug, Clone)]
pub enum PoolType {
    /// Max pooling
    /// 最大プーリング
    MaxPool,
    /// Average pooling
    /// 平均プーリング
    AvgPool,
    /// Adaptive max pooling
    /// 適応最大プーリング
    AdaptiveMaxPool(Vec<usize>),
    /// Adaptive average pooling
    /// 適応平均プーリング
    AdaptiveAvgPool(Vec<usize>),
}

/// Batch normalization information
/// バッチ正規化情報
#[derive(Debug, Clone)]
pub struct BatchNormInfo {
    /// Layer name
    /// レイヤー名
    pub name: String,
    /// Number of features
    /// 特徴数
    pub num_features: usize,
    /// Scale parameters
    /// スケールパラメータ
    pub weight: Tensor<f32>,
    /// Shift parameters
    /// シフトパラメータ
    pub bias: Tensor<f32>,
    /// Running mean
    /// 実行中平均
    pub running_mean: Tensor<f32>,
    /// Running variance
    /// 実行中分散
    pub running_var: Tensor<f32>,
    /// Momentum for running statistics
    /// 実行統計のモメンタム
    pub momentum: f32,
    /// Epsilon for numerical stability
    /// 数値安定性のためのイプシロン
    pub eps: f32,
}

/// Classifier information
/// 分類器情報
#[derive(Debug, Clone)]
pub struct ClassifierInfo {
    /// Layer name
    /// レイヤー名
    pub name: String,
    /// Input features
    /// 入力特徴
    pub in_features: usize,
    /// Number of classes
    /// クラス数
    pub num_classes: usize,
    /// Weights
    /// 重み
    pub weights: Tensor<f32>,
    /// Bias
    /// バイアス
    pub bias: Option<Tensor<f32>>,
}

/// Complex architecture parser
/// 複雑アーキテクチャパーサー
pub struct ComplexArchitectureParser;

impl ComplexArchitectureParser {
    /// Parse transformer architecture from layer parameters
    /// レイヤーパラメータからtransformerアーキテクチャを解析
    pub fn parse_transformer(
        layers: &HashMap<String, SimpleLayerInfo>
    ) -> Result<TransformerComponents, RusTorchError> {
        let mut attention_layers = Vec::new();
        let mut ffn_layers = Vec::new();
        let mut layer_norms = Vec::new();
        let mut position_embeddings = None;
        let mut token_embeddings = None;

        for (layer_name, layer_info) in layers {
            if Self::is_attention_layer(layer_name) {
                if let Ok(attention) = Self::parse_attention_layer(layer_name, layer_info) {
                    attention_layers.push(attention);
                }
            } else if Self::is_ffn_layer(layer_name) {
                if let Ok(ffn) = Self::parse_ffn_layer(layer_name, layer_info) {
                    ffn_layers.push(ffn);
                }
            } else if Self::is_layer_norm(layer_name) {
                if let Ok(ln) = Self::parse_layer_norm(layer_name, layer_info) {
                    layer_norms.push(ln);
                }
            } else if Self::is_position_embedding(layer_name) {
                position_embeddings = Some(Self::parse_position_embedding(layer_name, layer_info)?);
            } else if Self::is_token_embedding(layer_name) {
                token_embeddings = Some(Self::parse_token_embedding(layer_name, layer_info)?);
            }
        }

        Ok(TransformerComponents {
            attention_layers,
            ffn_layers,
            layer_norms,
            position_embeddings,
            token_embeddings,
        })
    }

    /// Parse CNN architecture from layer parameters
    /// レイヤーパラメータからCNNアーキテクチャを解析
    pub fn parse_cnn(
        layers: &HashMap<String, SimpleLayerInfo>
    ) -> Result<CNNComponents, RusTorchError> {
        let mut conv_layers = Vec::new();
        let mut pool_layers = Vec::new();
        let mut batch_norms = Vec::new();
        let mut classifier = None;

        for (layer_name, layer_info) in layers {
            if Self::is_conv_layer(layer_name, layer_info) {
                if let Ok(conv) = Self::parse_conv_layer(layer_name, layer_info) {
                    conv_layers.push(conv);
                }
            } else if Self::is_pool_layer(layer_name) {
                if let Ok(pool) = Self::parse_pool_layer(layer_name, layer_info) {
                    pool_layers.push(pool);
                }
            } else if Self::is_batch_norm(layer_name, layer_info) {
                if let Ok(bn) = Self::parse_batch_norm(layer_name, layer_info) {
                    batch_norms.push(bn);
                }
            } else if Self::is_classifier(layer_name, layer_info) {
                classifier = Some(Self::parse_classifier(layer_name, layer_info)?);
            }
        }

        Ok(CNNComponents {
            conv_layers,
            pool_layers,
            batch_norms,
            classifier,
        })
    }

    /// Check if layer is attention layer
    /// レイヤーがアテンションレイヤーかチェック
    fn is_attention_layer(layer_name: &str) -> bool {
        layer_name.contains("attention") || 
        layer_name.contains("attn") ||
        layer_name.contains("self_attn") ||
        layer_name.contains("multi_head")
    }

    /// Parse attention layer
    /// アテンションレイヤーを解析
    fn parse_attention_layer(
        layer_name: &str,
        layer_info: &SimpleLayerInfo
    ) -> Result<MultiheadAttentionInfo, RusTorchError> {
        // Extract attention parameters from tensors
        let q_proj = layer_info.tensors.get("q_proj.weight")
            .or_else(|| layer_info.tensors.get("query.weight"))
            .or_else(|| layer_info.tensors.get("wq.weight"))
            .ok_or_else(|| RusTorchError::import_error("query projection".to_string()))?;

        let k_proj = layer_info.tensors.get("k_proj.weight")
            .or_else(|| layer_info.tensors.get("key.weight"))
            .or_else(|| layer_info.tensors.get("wk.weight"))
            .ok_or_else(|| RusTorchError::import_error("key projection".to_string()))?;

        let v_proj = layer_info.tensors.get("v_proj.weight")
            .or_else(|| layer_info.tensors.get("value.weight"))
            .or_else(|| layer_info.tensors.get("wv.weight"))
            .ok_or_else(|| RusTorchError::import_error("value projection".to_string()))?;

        let out_proj = layer_info.tensors.get("out_proj.weight")
            .or_else(|| layer_info.tensors.get("output.weight"))
            .or_else(|| layer_info.tensors.get("wo.weight"))
            .ok_or_else(|| RusTorchError::import_error("output projection".to_string()))?;

        // Infer dimensions from weight shapes
        let embed_dim = q_proj.shape()[1];
        let total_head_dim = q_proj.shape()[0];
        
        // Common head configurations
        let num_heads = Self::infer_num_heads(embed_dim, total_head_dim);
        let head_dim = total_head_dim / num_heads;

        Ok(MultiheadAttentionInfo {
            name: layer_name.to_string(),
            num_heads,
            embed_dim,
            head_dim,
            dropout: 0.1, // Default dropout
            bias: true,   // Default bias
            batch_first: true, // Default batch_first
            query_weights: q_proj.clone(),
            key_weights: k_proj.clone(),
            value_weights: v_proj.clone(),
            output_weights: out_proj.clone(),
            biases: None, // Could be extracted if present
        })
    }

    /// Infer number of attention heads
    /// アテンションヘッド数を推論
    fn infer_num_heads(embed_dim: usize, total_head_dim: usize) -> usize {
        // Common head configurations
        if total_head_dim % embed_dim == 0 {
            total_head_dim / embed_dim
        } else if embed_dim % 64 == 0 && total_head_dim % 64 == 0 {
            embed_dim / 64 // Assume 64-dim heads
        } else if embed_dim % 32 == 0 && total_head_dim % 32 == 0 {
            embed_dim / 32 // Assume 32-dim heads
        } else {
            8 // Default fallback
        }
    }

    /// Check if layer is feed-forward network
    /// レイヤーがフィードフォワードネットワークかチェック
    fn is_ffn_layer(layer_name: &str) -> bool {
        layer_name.contains("ffn") ||
        layer_name.contains("feed_forward") ||
        layer_name.contains("mlp") ||
        (layer_name.contains("linear") && (layer_name.contains("1") || layer_name.contains("2")))
    }

    /// Parse feed-forward network layer
    /// フィードフォワードネットワークレイヤーを解析
    fn parse_ffn_layer(
        layer_name: &str,
        layer_info: &SimpleLayerInfo
    ) -> Result<FeedForwardInfo, RusTorchError> {
        // Look for linear layer pairs
        let linear1 = layer_info.tensors.get("linear1.weight")
            .or_else(|| layer_info.tensors.get("fc1.weight"))
            .or_else(|| layer_info.tensors.get("w1.weight"))
            .ok_or_else(|| RusTorchError::import_error("first linear layer".to_string()))?;

        let linear2 = layer_info.tensors.get("linear2.weight")
            .or_else(|| layer_info.tensors.get("fc2.weight"))
            .or_else(|| layer_info.tensors.get("w2.weight"))
            .ok_or_else(|| RusTorchError::import_error("second linear layer".to_string()))?;

        let input_dim = linear1.shape()[1];
        let hidden_dim = linear1.shape()[0];

        Ok(FeedForwardInfo {
            name: layer_name.to_string(),
            input_dim,
            hidden_dim,
            linear1_weights: linear1.clone(),
            linear2_weights: linear2.clone(),
            biases: None,
            activation: "relu".to_string(), // Default, could be inferred
        })
    }

    /// Check if layer is layer normalization
    /// レイヤーがレイヤー正規化かチェック
    fn is_layer_norm(layer_name: &str) -> bool {
        layer_name.contains("layer_norm") ||
        layer_name.contains("ln") ||
        layer_name.contains("norm")
    }

    /// Parse layer normalization
    /// レイヤー正規化を解析
    fn parse_layer_norm(
        layer_name: &str,
        layer_info: &SimpleLayerInfo
    ) -> Result<LayerNormInfo, RusTorchError> {
        let weight = layer_info.tensors.get("weight")
            .ok_or_else(|| RusTorchError::import_error("layer norm weight".to_string()))?;

        let bias = layer_info.tensors.get("bias");
        let normalized_shape = weight.shape().to_vec();

        Ok(LayerNormInfo {
            name: layer_name.to_string(),
            normalized_shape,
            weight: weight.clone(),
            bias: bias.cloned(),
            eps: 1e-5, // Default epsilon
        })
    }

    /// Check if layer is position embedding
    /// レイヤーが位置埋め込みかチェック
    fn is_position_embedding(layer_name: &str) -> bool {
        layer_name.contains("pos_emb") ||
        layer_name.contains("position") ||
        layer_name.contains("positional")
    }

    /// Parse position embedding
    /// 位置埋め込みを解析
    fn parse_position_embedding(
        _layer_name: &str,
        layer_info: &SimpleLayerInfo
    ) -> Result<PositionEmbeddingInfo, RusTorchError> {
        let weight = layer_info.tensors.get("weight")
            .ok_or_else(|| RusTorchError::import_error("position embedding weight".to_string()))?;

        let shape = weight.shape();
        let max_length = shape[0];
        let embed_dim = shape[1];

        Ok(PositionEmbeddingInfo {
            max_length,
            embed_dim,
            weights: weight.clone(),
        })
    }

    /// Check if layer is token embedding
    /// レイヤーがトークン埋め込みかチェック
    fn is_token_embedding(layer_name: &str) -> bool {
        layer_name.contains("token_emb") ||
        layer_name.contains("word_emb") ||
        layer_name.contains("embedding") ||
        layer_name == "embeddings"
    }

    /// Parse token embedding
    /// トークン埋め込みを解析
    fn parse_token_embedding(
        _layer_name: &str,
        layer_info: &SimpleLayerInfo
    ) -> Result<TokenEmbeddingInfo, RusTorchError> {
        let weight = layer_info.tensors.get("weight")
            .ok_or_else(|| RusTorchError::import_error("token embedding weight".to_string()))?;

        let shape = weight.shape();
        let vocab_size = shape[0];
        let embed_dim = shape[1];

        Ok(TokenEmbeddingInfo {
            vocab_size,
            embed_dim,
            weights: weight.clone(),
        })
    }

    /// Check if layer is convolutional
    /// レイヤーが畳み込みかチェック
    fn is_conv_layer(layer_name: &str, layer_info: &SimpleLayerInfo) -> bool {
        layer_name.contains("conv") && layer_info.layer_type == "Conv2d"
    }

    /// Parse convolutional layer
    /// 畳み込みレイヤーを解析
    fn parse_conv_layer(
        layer_name: &str,
        layer_info: &SimpleLayerInfo
    ) -> Result<ConvLayerInfo, RusTorchError> {
        let weight = layer_info.tensors.get("weight")
            .ok_or_else(|| RusTorchError::import_error("conv weight".to_string()))?;

        let shape = weight.shape();
        let out_channels = shape[0];
        let in_channels = shape[1];
        let kernel_h = shape[2];
        let kernel_w = shape[3];

        let bias = layer_info.tensors.get("bias");

        Ok(ConvLayerInfo {
            name: layer_name.to_string(),
            in_channels,
            out_channels,
            kernel_size: (kernel_h, kernel_w),
            stride: (1, 1), // Default - could be inferred from layer name
            padding: (0, 0), // Default
            dilation: (1, 1), // Default
            groups: 1, // Default
            weights: weight.clone(),
            bias: bias.cloned(),
        })
    }

    /// Check if layer is pooling
    /// レイヤーがプーリングかチェック
    fn is_pool_layer(layer_name: &str) -> bool {
        layer_name.contains("pool") || layer_name.contains("avgpool") || layer_name.contains("maxpool")
    }

    /// Parse pooling layer
    /// プーリングレイヤーを解析
    fn parse_pool_layer(
        layer_name: &str,
        _layer_info: &SimpleLayerInfo
    ) -> Result<PoolLayerInfo, RusTorchError> {
        let pool_type = if layer_name.contains("max") {
            PoolType::MaxPool
        } else if layer_name.contains("avg") {
            PoolType::AvgPool
        } else if layer_name.contains("adaptive") {
            if layer_name.contains("max") {
                PoolType::AdaptiveMaxPool(vec![1, 1]) // Default output size
            } else {
                PoolType::AdaptiveAvgPool(vec![1, 1]) // Default output size
            }
        } else {
            PoolType::MaxPool // Default
        };

        Ok(PoolLayerInfo {
            name: layer_name.to_string(),
            pool_type,
            kernel_size: (2, 2), // Default
            stride: Some((2, 2)), // Default
            padding: Some((0, 0)), // Default
        })
    }

    /// Check if layer is batch normalization
    /// レイヤーがバッチ正規化かチェック
    fn is_batch_norm(layer_name: &str, layer_info: &SimpleLayerInfo) -> bool {
        (layer_name.contains("bn") || layer_name.contains("batch_norm")) && 
        layer_info.layer_type == "BatchNorm2d"
    }

    /// Parse batch normalization layer
    /// バッチ正規化レイヤーを解析
    fn parse_batch_norm(
        layer_name: &str,
        layer_info: &SimpleLayerInfo
    ) -> Result<BatchNormInfo, RusTorchError> {
        let weight = layer_info.tensors.get("weight")
            .ok_or_else(|| RusTorchError::import_error("batch norm weight".to_string()))?;

        let bias = layer_info.tensors.get("bias")
            .ok_or_else(|| RusTorchError::import_error("batch norm bias".to_string()))?;

        let running_mean = layer_info.tensors.get("running_mean")
            .ok_or_else(|| RusTorchError::import_error("batch norm running mean".to_string()))?;

        let running_var = layer_info.tensors.get("running_var")
            .ok_or_else(|| RusTorchError::import_error("batch norm running var".to_string()))?;

        let num_features = weight.shape()[0];

        Ok(BatchNormInfo {
            name: layer_name.to_string(),
            num_features,
            weight: weight.clone(),
            bias: bias.clone(),
            running_mean: running_mean.clone(),
            running_var: running_var.clone(),
            momentum: 0.1, // Default
            eps: 1e-5, // Default
        })
    }

    /// Check if layer is classifier
    /// レイヤーが分類器かチェック
    fn is_classifier(layer_name: &str, layer_info: &SimpleLayerInfo) -> bool {
        (layer_name.contains("classifier") || layer_name.contains("fc") || layer_name == "head") &&
        layer_info.layer_type == "Linear"
    }

    /// Parse classifier layer
    /// 分類器レイヤーを解析
    fn parse_classifier(
        layer_name: &str,
        layer_info: &SimpleLayerInfo
    ) -> Result<ClassifierInfo, RusTorchError> {
        let weight = layer_info.tensors.get("weight")
            .ok_or_else(|| RusTorchError::import_error("classifier weight".to_string()))?;

        let shape = weight.shape();
        let num_classes = shape[0];
        let in_features = shape[1];
        let bias = layer_info.tensors.get("bias");

        Ok(ClassifierInfo {
            name: layer_name.to_string(),
            in_features,
            num_classes,
            weights: weight.clone(),
            bias: bias.cloned(),
        })
    }
}

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

    #[test]
    fn test_attention_layer_detection() {
        assert!(ComplexArchitectureParser::is_attention_layer("encoder.layer.0.attention"));
        assert!(ComplexArchitectureParser::is_attention_layer("transformer.h.0.attn"));
        assert!(ComplexArchitectureParser::is_attention_layer("layers.0.self_attn"));
        assert!(!ComplexArchitectureParser::is_attention_layer("layers.0.linear"));
    }

    #[test]
    fn test_ffn_layer_detection() {
        assert!(ComplexArchitectureParser::is_ffn_layer("encoder.layer.0.ffn"));
        assert!(ComplexArchitectureParser::is_ffn_layer("transformer.h.0.mlp"));
        assert!(ComplexArchitectureParser::is_ffn_layer("layers.0.feed_forward"));
        assert!(!ComplexArchitectureParser::is_ffn_layer("layers.0.attention"));
    }

    #[test]
    fn test_conv_layer_detection() {
        let mut layer_info = SimpleLayerInfo {
            name: "features.0".to_string(),
            layer_type: "Conv2d".to_string(),
            parameter_shapes: HashMap::new(),
            num_parameters: 0,
            tensors: HashMap::new(),
        };
        
        assert!(ComplexArchitectureParser::is_conv_layer("features.0.conv", &layer_info));
        
        layer_info.layer_type = "Linear".to_string();
        assert!(!ComplexArchitectureParser::is_conv_layer("features.0.conv", &layer_info));
    }

    #[test]
    fn test_num_heads_inference() {
        assert_eq!(ComplexArchitectureParser::infer_num_heads(512, 512), 8); // 512/64 = 8
        assert_eq!(ComplexArchitectureParser::infer_num_heads(768, 768), 12); // 768/64 = 12
        assert_eq!(ComplexArchitectureParser::infer_num_heads(1024, 1024), 16); // 1024/64 = 16
    }
}