Files
rustytorch/crates/specialized/rtx-synthesis/tests/aot_test.rs
T
2026-03-04 00:08:42 +00:00

117 lines
3.7 KiB
Rust

//! Tests for AOT compilation functionality
//!
//! Following TDD methodology - write tests first, then fix implementation
#![cfg(feature = "disabled_tests")]
use anyhow::Result;
use rtx_synthesis::aot::{AotCompiler, CompiledGraph, GraphOperation};
#[test]
fn test_aot_compiler_creation() {
// RED: Test that AotCompiler can be created
let compiler = AotCompiler::new();
// Should have default configuration
assert_eq!(compiler.optimization_level(), 2);
assert!(compiler.enable_fusion());
}
#[test]
fn test_graph_operation_creation() {
// RED: Test GraphOperation construction
let op = GraphOperation::new("matmul", vec!["input_a", "input_b"]);
assert_eq!(op.name(), "matmul");
assert_eq!(op.inputs().len(), 2);
assert_eq!(op.inputs()[0], "input_a");
assert_eq!(op.inputs()[1], "input_b");
}
#[test]
fn test_simple_graph_compilation() {
// RED: Test compilation of a simple graph
let compiler = AotCompiler::new();
// Create a simple graph with a few operations
let mut graph = Vec::new();
graph.push(GraphOperation::new("input", vec![]));
graph.push(GraphOperation::new("linear", vec!["input"]));
graph.push(GraphOperation::new("relu", vec!["linear"]));
graph.push(GraphOperation::new("output", vec!["relu"]));
// Should be able to compile
let result = compiler.compile(&graph);
assert!(result.is_ok());
if let Ok(compiled) = result {
// Should have the compiled graph
assert_eq!(compiled.num_operations(), 4);
assert!(compiled.is_optimized());
}
}
#[test]
fn test_graph_optimization() {
// RED: Test graph optimization features
let mut compiler = AotCompiler::new();
compiler.set_optimization_level(3); // Maximum optimization
// Create graph with fusable operations
let mut graph = Vec::new();
graph.push(GraphOperation::new("conv2d", vec!["input"]));
graph.push(GraphOperation::new("batch_norm", vec!["conv2d"]));
graph.push(GraphOperation::new("relu", vec!["batch_norm"]));
let result = compiler.compile(&graph);
assert!(result.is_ok());
if let Ok(compiled) = result {
// Should have fused operations
assert!(compiled.has_fused_operations());
// Fused graph should have fewer operations
assert!(compiled.num_operations() < 3);
}
}
#[test]
fn test_compiled_graph_execution() {
// RED: Test that compiled graphs can estimate performance
let compiler = AotCompiler::new();
let mut graph = Vec::new();
graph.push(GraphOperation::new("matmul", vec!["a", "b"]));
let compiled = compiler
.compile(&graph)
.expect("Compilation should succeed");
// Should be able to estimate performance
let perf_estimate = compiled.estimate_performance();
assert!(perf_estimate.flops() > 0.0);
assert!(perf_estimate.memory_bandwidth() > 0.0);
assert!(perf_estimate.estimated_time_ms() > 0.0);
}
#[test]
fn test_memory_optimization() {
// RED: Test memory reuse optimization
let mut compiler = AotCompiler::new();
compiler.enable_memory_reuse(true);
// Create graph with intermediate results
let mut graph = Vec::new();
graph.push(GraphOperation::new("linear1", vec!["input"]));
graph.push(GraphOperation::new("relu1", vec!["linear1"]));
graph.push(GraphOperation::new("linear2", vec!["relu1"]));
graph.push(GraphOperation::new("relu2", vec!["linear2"]));
let compiled = compiler
.compile(&graph)
.expect("Compilation should succeed");
// Should have optimized memory usage
let memory_stats = compiled.memory_statistics();
assert!(memory_stats.peak_memory() > 0);
assert!(memory_stats.reuse_ratio() > 0.0);
}