god_graph/transformer/optimization/mod.rs
1//! CAD-LLM Topology Optimization Module
2//!
3//! This module implements the CAD-inspired topology optimization framework for LLMs,
4//! combining Lie group decomposition, tensor ring compression, and topology-preserving
5//! transformations to achieve lossless, controllable, and interpretable LLM optimization.
6//!
7//! ## Core Philosophy
8//!
9//! Transform LLM optimization from "alchemy trial-and-error" to "engineering design"
10//! by establishing topology isomorphism between CAD feature trees and LLM computation graphs.
11//!
12//! ## Design Principles
13//!
14//! - **P1 No Blackbox**: Avoid traditional pruning/quantization/distillation blackbox optimization
15//! - **P2 Topology Preserving**: Maintain computation graph topology, only optimize node weights
16//! - **P3 No Parameter Bloat**: No new learnable parameters, prevent parameter inflation
17//! - **P4 Engineering Paradigm**: Transform LLM optimization to engineering design modifications
18//! - **P5 Mathematically Rigorous**: Based on Lie group decomposition, tensor ring compression
19//! - **P6 CAD Inspired**: Reuse CAD feature tree editing, constraint solving, assembly validation
20//!
21//! ## Modules
22//!
23//! - [`switch`]: Bidirectional lossless conversion between Safetensors and GodGraph
24//! - [`lie_group`]: Lie group decomposition and orthogonalization
25//! - [`tensor_ring`]: Tensor ring compression for parameter reduction
26//! - [`cad_editor`]: CAD-style topology editor for defect detection and constraint solving
27//! - [`constraints`]: Topology constraints and validation
28//!
29//! ## Example
30//!
31//! ```no_run
32//! # #[cfg(feature = "safetensors")]
33//! use god_gragh::transformer::optimization::{ModelSwitch, LieGroupOptimizer, LieGroupConfig};
34//! use god_gragh::transformer::optimization::{TensorRingCompressor, CompressionConfig};
35//! use god_gragh::transformer::optimization::{CadStyleEditor, TopologyConstraint};
36//!
37//! # #[cfg(feature = "safetensors")]
38//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
39//! // 1. Load model from Safetensors
40//! let mut graph = ModelSwitch::load_from_safetensors("model.safetensors")?;
41//!
42//! // 2. Validate topology
43//! let report = ModelSwitch::validate_topology(&graph)?;
44//! assert!(report.is_valid);
45//!
46//! // 3. Apply Lie group orthogonalization
47//! let config = LieGroupConfig::new()
48//! .with_block_size(64)
49//! .with_orthogonalize(true);
50//! let optimizer = LieGroupOptimizer::new(config);
51//! optimizer.orthogonalize_weights(&mut graph)?;
52//!
53//! // 4. Apply tensor ring compression
54//! let config = CompressionConfig::new()
55//! .with_target_ranks(vec![32, 64])
56//! .with_layers(vec!["qkv".to_string(), "mlp".to_string()]);
57//! let compressor = TensorRingCompressor::new(config);
58//! let compressed = compressor.compress_graph(&graph)?;
59//!
60//! // 5. CAD-style topology editing
61//! let mut editor = CadStyleEditor::new(&mut graph);
62//! let defects = editor.detect_defects()?;
63//! editor.add_constraint(TopologyConstraint::ResidualConnection {
64//! from_layer: "layer.0".to_string(),
65//! to_layer: "layer.1".to_string(),
66//! })?;
67//! editor.solve_constraints()?;
68//! # Ok(())
69//! # }
70//! # #[cfg(not(feature = "safetensors"))]
71//! # fn main() {}
72//! ```
73
74// switch module is always available when tensor feature is enabled
75// safetensors feature is only needed for actual Safetensors file I/O
76pub mod switch;
77
78#[cfg(feature = "tensor")]
79pub mod lie_group;
80
81#[cfg(feature = "tensor")]
82pub mod tensor_ring;
83
84#[cfg(feature = "tensor")]
85pub mod cad_editor;
86
87#[cfg(feature = "tensor")]
88pub mod constraints;
89
90#[cfg(feature = "tensor")]
91pub mod error_analysis;
92
93// Re-export main types
94#[cfg(feature = "safetensors")]
95pub use switch::ModelSwitch;
96
97#[cfg(feature = "tensor")]
98pub use lie_group::{LieGroupConfig, LieGroupOptimizer, SOkBlock, decompose_into_so_blocks};
99
100#[cfg(feature = "tensor")]
101pub use tensor_ring::{CompressionConfig, TensorRingCompressor, mixed_precision_compress, adaptive_rank_selection};
102
103#[cfg(feature = "tensor")]
104pub use cad_editor::CadStyleEditor;
105
106#[cfg(feature = "tensor")]
107pub use constraints::{ConstraintReport, TopologyConstraint, TopologyDefect, TopologyValidator};
108
109#[cfg(feature = "tensor")]
110pub use error_analysis::{ErrorAccumulator, ErrorReport, ErrorStatistics, LayerErrorStats};
111
112// Re-export TensorRing from decomposition module
113#[cfg(feature = "tensor")]
114pub use crate::tensor::decomposition::tensor_ring::TensorRing;