tenrso_planner/
lib.rs

1//! # tenrso-planner
2//!
3//! Contraction order planning and optimization for TenRSo.
4//!
5//! **Version:** 0.1.0-alpha.2
6//! **Tests:** 271 passing (264 + 7 ignored) - 100%
7//! **Status:** M4 Complete with advanced enhancements and production features
8//!
9//! This crate provides algorithms and tools for finding efficient execution plans
10//! for tensor network contractions, particularly those expressed in Einstein summation notation.
11//!
12//! ## Features
13//!
14//! - **6 Planning Algorithms**: Greedy, DP, Beam Search, Simulated Annealing, Genetic Algorithm, Adaptive
15//! - **Parallel Planning**: Ensemble planner runs multiple algorithms concurrently for best quality
16//! - **ML-Based Calibration**: Learn from execution history to improve cost predictions over time
17//! - **Cost Estimation**: FLOPs and memory prediction for dense and sparse tensors
18//! - **Plan Caching**: LRU/LFU/ARC eviction policies for memoization
19//! - **Execution Simulation**: Hardware-specific cost modeling (CPU + 6 GPU models with Tensor Cores)
20//! - **Quality Tracking**: Real-time execution feedback and adaptive tuning
21//! - **Representation Selection**: Automatic choice between dense, sparse, and low-rank formats
22//! - **Tiling and Blocking**: Multi-level cache-aware strategies for efficient memory access
23//! - **Production Features**: Profiling, validation, visualization, serialization
24//! - **SciRS2-Core Integration**: Professional-grade RNG for reproducible stochastic algorithms
25//!
26//! ## Quick Start
27//!
28//! ```
29//! use tenrso_planner::{greedy_planner, EinsumSpec, PlanHints};
30//!
31//! // Parse Einstein summation notation
32//! let spec = EinsumSpec::parse("ij,jk->ik").unwrap();
33//!
34//! // Define tensor shapes
35//! let shapes = vec![vec![100, 200], vec![200, 300]];
36//!
37//! // Create a plan with default hints
38//! let hints = PlanHints::default();
39//! let plan = greedy_planner(&spec, &shapes, &hints).unwrap();
40//!
41//! // Inspect plan details
42//! println!("Estimated FLOPs: {:.2e}", plan.estimated_flops);
43//! println!("Peak memory: {} bytes", plan.estimated_memory);
44//! ```
45//!
46//! ## Planners
47//!
48//! TenRSo-Planner provides **6 production-grade planning algorithms**:
49//!
50//! ### 1. Adaptive Planner (⭐ Recommended)
51//!
52//! The [`AdaptivePlanner`] automatically selects the best algorithm based on problem size.
53//! - Small networks (n ≤ 5): Uses DP (optimal)
54//! - Medium networks (5 < n ≤ 20): Uses Beam Search or DP
55//! - Large networks (n > 20): Uses Greedy, SA, or GA based on time budget
56//!
57//! **Use when:** You want optimal results without manual algorithm selection
58//!
59//! ### 2. Greedy Planner
60//!
61//! The [`greedy_planner`] repeatedly contracts the pair of tensors with minimum cost
62//! at each step. Fast (O(n³)) and produces good results for most tensor networks.
63//!
64//! **Use when:** Planning time is critical, many tensors (> 10)
65//!
66//! ### 3. Beam Search Planner
67//!
68//! The [`BeamSearchPlanner`] explores multiple paths simultaneously. Better quality
69//! than greedy while remaining tractable.
70//!
71//! **Use when:** Medium networks (8-20 tensors), want better than greedy quality
72//!
73//! ### 4. Dynamic Programming Planner
74//!
75//! The [`dp_planner`] uses bitmask DP to find globally optimal contraction order.
76//! More expensive (O(3ⁿ) time, O(2ⁿ) space) but guarantees optimality.
77//!
78//! **Use when:** Few tensors (≤ 20), need provably optimal plans
79//!
80//! ### 5. Simulated Annealing Planner
81//!
82//! The [`SimulatedAnnealingPlanner`] uses stochastic search to escape local minima.
83//!
84//! **Use when:** Large networks (> 20), quality more important than planning speed
85//!
86//! ### 6. Genetic Algorithm Planner
87//!
88//! The [`GeneticAlgorithmPlanner`] uses evolutionary search with population-based
89//! exploration, crossover, and mutation to find high-quality contraction orders.
90//!
91//! **Use when:** Very large networks (> 20), need best quality with enough time budget
92//!
93//! ## Cost Model
94//!
95//! The planner estimates costs using:
96//! - **FLOPs**: Multiply-add operations (2 FLOPs per element)
97//! - **Memory**: Bytes for intermediate results and outputs
98//! - **Sparsity**: Density-based predictions for sparse operations
99//!
100//! ## Examples
101//!
102//! See the `examples/` directory for comprehensive usage demonstrations:
103//! - `basic_matmul.rs`: Simple matrix multiplication planning
104//! - `matrix_chain.rs`: Comparing greedy vs DP for chain contractions
105//! - `planning_hints.rs`: Using hints to control optimization
106//!
107//! ## Performance
108//!
109//! Planning overhead is typically negligible compared to execution:
110//! - **Adaptive**: Auto-selects best algorithm (varies)
111//! - **Greedy**: ~1ms for 10 tensors, ~10ms for 100 tensors
112//! - **Beam Search (k=5)**: ~5ms for 10 tensors, ~50ms for 100 tensors
113//! - **DP**: ~1ms for 5 tensors, ~100ms for 10 tensors, ~10s for 15 tensors
114//! - **Simulated Annealing**: ~10-100ms (configurable iterations)
115//! - **Genetic Algorithm**: ~50-500ms (depends on population and generations)
116//!
117//! ## References
118//!
119//! - Matrix chain multiplication: Cormen et al., "Introduction to Algorithms"
120//! - Tensor network contraction: Pfeifer et al., "Faster identification of optimal contraction sequences" (2014)
121//! - Einsum optimization: opt_einsum library (Python)
122
123#![deny(warnings)]
124
125pub mod api;
126pub mod cache;
127pub mod cost;
128pub mod ml_cost;
129pub mod order;
130pub mod parallel;
131pub mod parser;
132pub mod profiling;
133#[cfg(test)]
134mod property_tests;
135pub mod quality;
136pub mod repr;
137pub mod simulator;
138pub mod tiling;
139
140// Re-exports
141pub use api::*;
142pub use cache::*;
143pub use cost::*;
144pub use ml_cost::*;
145pub use order::{
146    beam_search_planner, dp_planner, genetic_algorithm_planner, greedy_planner, refine_plan,
147    simulated_annealing_planner, AdaptivePlanner, BeamSearchPlanner, DPPlanner,
148    GeneticAlgorithmPlanner, GreedyPlanner, SimulatedAnnealingPlanner,
149};
150pub use parallel::*;
151pub use parser::*;
152pub use profiling::*;
153pub use quality::*;
154pub use repr::*;
155pub use simulator::*;
156pub use tiling::*;