use crate::graph::{ComputationGraph, NodeId, Operation};
use anyhow::Result;
use scirs2_core::ndarray_ext::ScalarOperand;
use scirs2_core::numeric::{Float, FromPrimitive};
use std::collections::HashSet;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OptimizationPass {
OperationFusion,
DeadCodeElimination,
ConstantFolding,
All,
}
#[derive(Debug, Clone)]
pub struct OptimizationConfig {
pub passes: Vec<OptimizationPass>,
pub run_until_convergence: bool,
pub max_iterations: usize,
pub verbose: bool,
}
impl Default for OptimizationConfig {
fn default() -> Self {
Self {
passes: vec![OptimizationPass::All],
run_until_convergence: true,
max_iterations: 10,
verbose: false,
}
}
}
impl OptimizationConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_pass(mut self, pass: OptimizationPass) -> Self {
self.passes.push(pass);
self
}
pub fn verbose(mut self, verbose: bool) -> Self {
self.verbose = verbose;
self
}
pub fn max_iterations(mut self, max: usize) -> Self {
self.max_iterations = max;
self
}
}
#[derive(Debug, Clone)]
pub enum FusedOperation {
MatMulBias {
lhs: NodeId,
rhs: NodeId,
bias: NodeId,
},
MatMulBiasReLU {
lhs: NodeId,
rhs: NodeId,
bias: NodeId,
},
MulAdd { x: NodeId, y: NodeId, c: NodeId },
AddReLU { lhs: NodeId, rhs: NodeId },
}
#[derive(Debug, Clone, Default)]
pub struct OptimizationStats {
pub nodes_before: usize,
pub nodes_after: usize,
pub fusions_applied: usize,
pub dead_nodes_removed: usize,
pub constants_folded: usize,
pub iterations: usize,
}
impl OptimizationStats {
pub fn reduction_percent(&self) -> f64 {
if self.nodes_before == 0 {
0.0
} else {
100.0 * (1.0 - self.nodes_after as f64 / self.nodes_before as f64)
}
}
}
impl std::fmt::Display for OptimizationStats {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Graph Optimization Statistics:")?;
writeln!(f, " Nodes before: {}", self.nodes_before)?;
writeln!(f, " Nodes after: {}", self.nodes_after)?;
writeln!(f, " Reduction: {:.1}%", self.reduction_percent())?;
writeln!(f, " Operations fused: {}", self.fusions_applied)?;
writeln!(f, " Dead nodes removed: {}", self.dead_nodes_removed)?;
writeln!(f, " Constants folded: {}", self.constants_folded)?;
writeln!(f, " Iterations: {}", self.iterations)?;
Ok(())
}
}
pub struct GraphOptimizer {
config: OptimizationConfig,
}
impl Default for GraphOptimizer {
fn default() -> Self {
Self::new()
}
}
impl GraphOptimizer {
pub fn new() -> Self {
Self {
config: OptimizationConfig::default(),
}
}
pub fn with_config(config: OptimizationConfig) -> Self {
Self { config }
}
pub fn with_pass(mut self, pass: OptimizationPass) -> Self {
self.config.passes.push(pass);
self
}
pub fn verbose(mut self, verbose: bool) -> Self {
self.config.verbose = verbose;
self
}
pub fn optimize<T: Float + ScalarOperand + FromPrimitive>(
&self,
_graph: &ComputationGraph<T>,
) -> Result<OptimizationStats> {
Ok(OptimizationStats {
nodes_before: 0,
nodes_after: 0,
fusions_applied: 0,
dead_nodes_removed: 0,
constants_folded: 0,
iterations: 0,
})
}
pub fn detect_fusion_patterns(
&self,
ops: &[(NodeId, Operation)],
) -> Vec<(usize, FusedOperation)> {
let mut patterns = Vec::new();
let mut skip_until = 0;
for i in 0..ops.len() {
if i < skip_until {
continue; }
if i + 2 < ops.len() {
let (node_id_i, op_i) = &ops[i];
let (node_id_i1, op_i1) = &ops[i + 1];
let (_node_id_i2, op_i2) = &ops[i + 2];
if let (
Operation::MatMul { lhs, rhs },
Operation::Add {
lhs: add_lhs,
rhs: add_rhs,
},
Operation::ReLU { input: relu_input },
) = (op_i, op_i1, op_i2)
{
if relu_input == node_id_i1 {
let bias = if add_lhs == node_id_i {
*add_rhs
} else if add_rhs == node_id_i {
*add_lhs
} else {
continue;
};
patterns.push((
i,
FusedOperation::MatMulBiasReLU {
lhs: *lhs,
rhs: *rhs,
bias,
},
));
skip_until = i + 3; continue;
}
}
}
if i + 1 < ops.len() {
let (node_id_i, op_i) = &ops[i];
let (_node_id_i1, op_i1) = &ops[i + 1];
if let (
Operation::MatMul { lhs, rhs },
Operation::Add {
lhs: add_lhs,
rhs: add_rhs,
},
) = (op_i, op_i1)
{
if let Some(bias) = if add_lhs == node_id_i {
Some(*add_rhs)
} else if add_rhs == node_id_i {
Some(*add_lhs)
} else {
None
} {
patterns.push((
i,
FusedOperation::MatMulBias {
lhs: *lhs,
rhs: *rhs,
bias,
},
));
skip_until = i + 2;
continue;
}
}
if let (Operation::Add { lhs, rhs }, Operation::ReLU { input: relu_input }) =
(op_i, op_i1)
{
if relu_input == node_id_i {
patterns.push((
i,
FusedOperation::AddReLU {
lhs: *lhs,
rhs: *rhs,
},
));
skip_until = i + 2;
continue;
}
}
if let (
Operation::Mul { lhs, rhs },
Operation::Add {
lhs: add_lhs,
rhs: add_rhs,
},
) = (op_i, op_i1)
{
if let Some(c) = if add_lhs == node_id_i {
Some(*add_rhs)
} else if add_rhs == node_id_i {
Some(*add_lhs)
} else {
None
} {
patterns.push((
i,
FusedOperation::MulAdd {
x: *lhs,
y: *rhs,
c,
},
));
skip_until = i + 2;
continue;
}
}
}
}
patterns
}
pub fn eliminate_dead_code(
&self,
ops: &[(NodeId, Operation)],
output_nodes: &HashSet<NodeId>,
) -> Vec<NodeId> {
let mut live_nodes = output_nodes.clone();
let mut changed = true;
while changed {
changed = false;
for (node_id, op) in ops {
if !live_nodes.contains(node_id) {
continue;
}
let parents = self.get_operation_inputs(op);
for parent in parents {
if live_nodes.insert(parent) {
changed = true;
}
}
}
}
ops.iter()
.map(|(id, _)| *id)
.filter(|id| !live_nodes.contains(id))
.collect()
}
fn get_operation_inputs(&self, op: &Operation) -> Vec<NodeId> {
match op {
Operation::Input => vec![],
Operation::Add { lhs, rhs }
| Operation::Sub { lhs, rhs }
| Operation::Mul { lhs, rhs }
| Operation::Div { lhs, rhs }
| Operation::MatMul { lhs, rhs } => vec![*lhs, *rhs],
Operation::Neg { input }
| Operation::Exp { input }
| Operation::Log { input }
| Operation::Pow { input, .. }
| Operation::Sum { input, .. }
| Operation::Mean { input, .. }
| Operation::Reshape { input, .. }
| Operation::Transpose { input, .. }
| Operation::Broadcast { input, .. }
| Operation::ReLU { input }
| Operation::Sigmoid { input }
| Operation::Tanh { input } => vec![*input],
Operation::Slice { input, .. } => vec![*input],
}
}
pub fn estimate_memory_savings(
&self,
stats: &OptimizationStats,
avg_tensor_size_bytes: usize,
) -> usize {
let nodes_removed = stats.nodes_before.saturating_sub(stats.nodes_after);
nodes_removed * avg_tensor_size_bytes
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::graph::Operation;
#[test]
fn test_optimizer_creation() {
let optimizer = GraphOptimizer::new();
assert_eq!(optimizer.config.passes.len(), 1);
}
#[test]
fn test_optimizer_with_passes() {
let optimizer = GraphOptimizer::new()
.with_pass(OptimizationPass::OperationFusion)
.with_pass(OptimizationPass::DeadCodeElimination);
assert_eq!(optimizer.config.passes.len(), 3); }
#[test]
fn test_config_builder() {
let config = OptimizationConfig::new()
.with_pass(OptimizationPass::OperationFusion)
.verbose(true)
.max_iterations(5);
assert!(config.verbose);
assert_eq!(config.max_iterations, 5);
assert_eq!(config.passes.len(), 2);
}
#[test]
fn test_fusion_pattern_matmul_bias() {
let optimizer = GraphOptimizer::new();
let ops = vec![
(
NodeId(0),
Operation::MatMul {
lhs: NodeId(1),
rhs: NodeId(2),
},
),
(
NodeId(3),
Operation::Add {
lhs: NodeId(0),
rhs: NodeId(4),
},
),
];
let patterns = optimizer.detect_fusion_patterns(&ops);
assert_eq!(patterns.len(), 1);
match &patterns[0].1 {
FusedOperation::MatMulBias { lhs, rhs, bias } => {
assert_eq!(*lhs, NodeId(1));
assert_eq!(*rhs, NodeId(2));
assert_eq!(*bias, NodeId(4));
}
_ => panic!("Expected MatMulBias fusion"),
}
}
#[test]
fn test_fusion_pattern_matmul_bias_relu() {
let optimizer = GraphOptimizer::new();
let ops = vec![
(
NodeId(0),
Operation::MatMul {
lhs: NodeId(1),
rhs: NodeId(2),
},
),
(
NodeId(3),
Operation::Add {
lhs: NodeId(0),
rhs: NodeId(4),
},
),
(NodeId(5), Operation::ReLU { input: NodeId(3) }),
];
let patterns = optimizer.detect_fusion_patterns(&ops);
assert_eq!(patterns.len(), 1);
match &patterns[0].1 {
FusedOperation::MatMulBiasReLU { lhs, rhs, bias } => {
assert_eq!(*lhs, NodeId(1));
assert_eq!(*rhs, NodeId(2));
assert_eq!(*bias, NodeId(4));
}
_ => panic!("Expected MatMulBiasReLU fusion"),
}
}
#[test]
fn test_fusion_pattern_add_relu() {
let optimizer = GraphOptimizer::new();
let ops = vec![
(
NodeId(0),
Operation::Add {
lhs: NodeId(1),
rhs: NodeId(2),
},
),
(NodeId(3), Operation::ReLU { input: NodeId(0) }),
];
let patterns = optimizer.detect_fusion_patterns(&ops);
assert_eq!(patterns.len(), 1);
match &patterns[0].1 {
FusedOperation::AddReLU { lhs, rhs } => {
assert_eq!(*lhs, NodeId(1));
assert_eq!(*rhs, NodeId(2));
}
_ => panic!("Expected AddReLU fusion"),
}
}
#[test]
fn test_fusion_pattern_mul_add() {
let optimizer = GraphOptimizer::new();
let ops = vec![
(
NodeId(0),
Operation::Mul {
lhs: NodeId(1),
rhs: NodeId(2),
},
),
(
NodeId(3),
Operation::Add {
lhs: NodeId(0),
rhs: NodeId(4),
},
),
];
let patterns = optimizer.detect_fusion_patterns(&ops);
assert_eq!(patterns.len(), 1);
match &patterns[0].1 {
FusedOperation::MulAdd { x, y, c } => {
assert_eq!(*x, NodeId(1));
assert_eq!(*y, NodeId(2));
assert_eq!(*c, NodeId(4));
}
_ => panic!("Expected MulAdd fusion"),
}
}
#[test]
fn test_dead_code_elimination() {
let optimizer = GraphOptimizer::new();
let ops = vec![
(NodeId(0), Operation::Input),
(NodeId(1), Operation::Input),
(
NodeId(2),
Operation::Add {
lhs: NodeId(0),
rhs: NodeId(1),
},
),
(
NodeId(3),
Operation::Mul {
lhs: NodeId(0),
rhs: NodeId(1),
},
), (
NodeId(4),
Operation::Add {
lhs: NodeId(2),
rhs: NodeId(0),
},
),
];
let mut outputs = HashSet::new();
outputs.insert(NodeId(4));
let dead_nodes = optimizer.eliminate_dead_code(&ops, &outputs);
assert_eq!(dead_nodes.len(), 1);
assert!(dead_nodes.contains(&NodeId(3))); }
#[test]
fn test_get_operation_inputs() {
let optimizer = GraphOptimizer::new();
let add_op = Operation::Add {
lhs: NodeId(1),
rhs: NodeId(2),
};
assert_eq!(
optimizer.get_operation_inputs(&add_op),
vec![NodeId(1), NodeId(2)]
);
let relu_op = Operation::ReLU { input: NodeId(3) };
assert_eq!(optimizer.get_operation_inputs(&relu_op), vec![NodeId(3)]);
let input_op = Operation::Input;
assert_eq!(optimizer.get_operation_inputs(&input_op), vec![]);
}
#[test]
fn test_optimization_stats_reduction() {
let stats = OptimizationStats {
nodes_before: 100,
nodes_after: 80,
fusions_applied: 5,
dead_nodes_removed: 15,
constants_folded: 0,
iterations: 3,
};
assert!((stats.reduction_percent() - 20.0).abs() < 1e-10);
}
#[test]
fn test_memory_savings_estimation() {
let optimizer = GraphOptimizer::new();
let stats = OptimizationStats {
nodes_before: 100,
nodes_after: 80,
fusions_applied: 0,
dead_nodes_removed: 20,
constants_folded: 0,
iterations: 1,
};
let savings = optimizer.estimate_memory_savings(&stats, 1024); assert_eq!(savings, 20 * 1024); }
#[test]
fn test_stats_display() {
let stats = OptimizationStats {
nodes_before: 50,
nodes_after: 40,
fusions_applied: 3,
dead_nodes_removed: 7,
constants_folded: 0,
iterations: 2,
};
let output = format!("{}", stats);
assert!(output.contains("Nodes before: 50"));
assert!(output.contains("Nodes after: 40"));
assert!(output.contains("Operations fused: 3"));
}
}