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
//! CAD-LLM Topology Optimization Module
//!
//! This module implements the CAD-inspired topology optimization framework for LLMs,
//! combining Lie group decomposition, tensor ring compression, and topology-preserving
//! transformations to achieve lossless, controllable, and interpretable LLM optimization.
//!
//! ## Core Philosophy
//!
//! Transform LLM optimization from "alchemy trial-and-error" to "engineering design"
//! by establishing topology isomorphism between CAD feature trees and LLM computation graphs.
//!
//! ## Design Principles
//!
//! - **P1 No Blackbox**: Avoid traditional pruning/quantization/distillation blackbox optimization
//! - **P2 Topology Preserving**: Maintain computation graph topology, only optimize node weights
//! - **P3 No Parameter Bloat**: No new learnable parameters, prevent parameter inflation
//! - **P4 Engineering Paradigm**: Transform LLM optimization to engineering design modifications
//! - **P5 Mathematically Rigorous**: Based on Lie group decomposition, tensor ring compression
//! - **P6 CAD Inspired**: Reuse CAD feature tree editing, constraint solving, assembly validation
//!
//! ## Modules
//!
//! - [`switch`]: Bidirectional lossless conversion between Safetensors and GodGraph
//! - [`lie_group`]: Lie group decomposition and orthogonalization
//! - [`tensor_ring`]: Tensor ring compression for parameter reduction
//! - [`cad_editor`]: CAD-style topology editor for defect detection and constraint solving
//! - [`constraints`]: Topology constraints and validation
//!
//! ## Example
//!
//! ```no_run
//! # #[cfg(feature = "safetensors")]
//! use god_gragh::transformer::optimization::{ModelSwitch, LieGroupOptimizer, LieGroupConfig};
//! use god_gragh::transformer::optimization::{TensorRingCompressor, CompressionConfig};
//! use god_gragh::transformer::optimization::{CadStyleEditor, TopologyConstraint};
//!
//! # #[cfg(feature = "safetensors")]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // 1. Load model from Safetensors
//! let mut graph = ModelSwitch::load_from_safetensors("model.safetensors")?;
//!
//! // 2. Validate topology
//! let report = ModelSwitch::validate_topology(&graph)?;
//! assert!(report.is_valid);
//!
//! // 3. Apply Lie group orthogonalization
//! let config = LieGroupConfig::new()
//! .with_block_size(64)
//! .with_orthogonalize(true);
//! let optimizer = LieGroupOptimizer::new(config);
//! optimizer.orthogonalize_weights(&mut graph)?;
//!
//! // 4. Apply tensor ring compression
//! let config = CompressionConfig::new()
//! .with_target_ranks(vec![32, 64])
//! .with_layers(vec!["qkv".to_string(), "mlp".to_string()]);
//! let compressor = TensorRingCompressor::new(config);
//! let compressed = compressor.compress_graph(&graph)?;
//!
//! // 5. CAD-style topology editing
//! let mut editor = CadStyleEditor::new(&mut graph);
//! let defects = editor.detect_defects()?;
//! editor.add_constraint(TopologyConstraint::ResidualConnection {
//! from_layer: "layer.0".to_string(),
//! to_layer: "layer.1".to_string(),
//! })?;
//! editor.solve_constraints()?;
//! # Ok(())
//! # }
//! # #[cfg(not(feature = "safetensors"))]
//! # fn main() {}
//! ```
// switch module is always available when tensor feature is enabled
// safetensors feature is only needed for actual Safetensors file I/O
// Re-export main types
pub use ModelSwitch;
pub use ;
pub use ;
pub use CadStyleEditor;
pub use ;
pub use ;
// Re-export TensorRing from decomposition module
pub use crateTensorRing;