pub struct GraphCompiler { /* private fields */ }Expand description
Compiler for traced tensor graphs.
A graph compiler lowers one or more TracedTensor outputs to a reusable
CompiledGraph without requiring a backend.
§Examples
use tenferro_runtime::{TraceContext, GraphCompiler, TracedTensor};
let trace = TraceContext::new();
let x = trace.from_vec_col_major(vec![2], vec![1.0_f64, 2.0]).unwrap();
let y = (&x + &x).unwrap();
let mut compiler = GraphCompiler::new();
let program = compiler.compile(&y).unwrap();
assert_eq!(program.output_count(), 1);Implementations§
Source§impl GraphCompiler
impl GraphCompiler
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a compiler with bounded default caches.
§Examples
use tenferro_runtime::GraphCompiler;
let compiler = GraphCompiler::new();
assert!(compiler.extension_caches().is_empty());Sourcepub fn with_compiler_options(compiler_options: CompilerOptions) -> Self
pub fn with_compiler_options(compiler_options: CompilerOptions) -> Self
Create a compiler with explicit lowering and optimizer options.
§Examples
use tenferro_runtime::{CompilerOptions, GraphCompiler, OptimizerConfig};
let compiler = GraphCompiler::with_compiler_options(CompilerOptions {
optimizer: OptimizerConfig {
dot_decomposer: true,
..OptimizerConfig::default()
},
});
assert!(compiler.compiler_options().optimizer.dot_decomposer);Sourcepub fn compile(&mut self, output: &TracedTensor) -> Result<CompiledGraph>
pub fn compile(&mut self, output: &TracedTensor) -> Result<CompiledGraph>
Compile one traced output by snapshotting its context.
The output’s reachable closure is frozen into one semantic program; the compile leaves the context usable for further tracing.
§Examples
use tenferro_runtime::{GraphCompiler, TraceContext};
let trace = TraceContext::new();
let x = trace.input_concrete_shape(tenferro_tensor::DType::F64, &[1])?;
let y = x.neg()?;
let mut compiler = GraphCompiler::new();
let program = compiler.compile(&y)?;
assert_eq!(program.output_count(), 1);§Errors
Returns Error::Validation for an invalid snapshot or bound shape
guard, or Error::RuntimeState for inconsistent compiler state.
Sourcepub fn compile_traced_graph(
&mut self,
graph: &TracedGraph,
) -> Result<CompiledGraph>
pub fn compile_traced_graph( &mut self, graph: &TracedGraph, ) -> Result<CompiledGraph>
Compile an immutable semantic trace without consulting a backend.
§Errors
Returns Error::Validation for invalid metadata or shape constraints,
or Error::RuntimeState for inconsistent staging state.
Sourcepub fn compile_frozen_program(
&mut self,
frozen: &FrozenProgram,
) -> Result<CompiledGraph>
pub fn compile_frozen_program( &mut self, frozen: &FrozenProgram, ) -> Result<CompiledGraph>
Compile an immutable semantic program for ordered execution.
§Examples
use tenferro_ops::dim_expr::DimExpr;
use tenferro_runtime::program::{
CoreSemanticOp, ProgramInputSpec, SemanticProgramBuilder,
};
use tenferro_runtime::{DType, GraphCompiler};
let mut builder = SemanticProgramBuilder::new();
let input = builder
.input(ProgramInputSpec::new(DType::F64, [DimExpr::Const(2)]))
.unwrap();
let output = builder.add_op(CoreSemanticOp::Neg, &[input]).unwrap()[0];
let frozen = builder.finish(&[output]).unwrap();
let compiled = GraphCompiler::new()
.compile_frozen_program(&frozen)
.unwrap();
assert_eq!(compiled.input_count(), 1);§Errors
Returns Error::Validation for invalid metadata or shape constraints,
or Error::RuntimeState for inconsistent staging state.
Sourcepub fn compile_many(
&mut self,
outputs: &[&TracedTensor],
) -> Result<CompiledGraph>
pub fn compile_many( &mut self, outputs: &[&TracedTensor], ) -> Result<CompiledGraph>
Compile multiple traced outputs into one program.
Every output must belong to the same trace context.
§Errors
Returns Error::Validation with ValidationError::InvalidArgument when
the output list is empty (argument: "outputs") or when an output belongs
to a different trace context, and the typed snapshot error from
compile when the program cannot be frozen.
Sourcepub fn compile_with_input_specs(
&mut self,
output: &TracedTensor,
bindings: &[(&TracedTensor, DType, &[usize])],
) -> Result<CompiledGraph>
pub fn compile_with_input_specs( &mut self, output: &TracedTensor, bindings: &[(&TracedTensor, DType, &[usize])], ) -> Result<CompiledGraph>
Compile one traced output with concrete placeholder specs.
Every declared placeholder must be a data-less input of the output’s context, with the declared dtype and shape. The compiled program lists the declared placeholders first, in the order given here, followed by the remaining live inputs in context order.
§Errors
Returns Error::UnexpectedBinding for a data-carrying tensor,
Error::DuplicateBinding for repeated placeholders,
Error::PlaceholderDtypeMismatch,
Error::PlaceholderShapeMismatch, or
Error::PlaceholderRankMismatch for incompatible specs,
Error::Validation with InvalidArgument for a declared placeholder the
output does not depend on, and the snapshot failures of
compile.
Sourcepub fn compiler_options(&self) -> CompilerOptions
pub fn compiler_options(&self) -> CompilerOptions
Return the compiler options used for future lowerings.
§Examples
use tenferro_runtime::{CompilerOptions, GraphCompiler};
let compiler = GraphCompiler::new();
assert_eq!(compiler.compiler_options(), CompilerOptions::default());Sourcepub fn set_compiler_options(&mut self, compiler_options: CompilerOptions)
pub fn set_compiler_options(&mut self, compiler_options: CompilerOptions)
Replace compiler options and clear compiler-owned extension cache entries.
Sourcepub fn clear_extension_caches(&mut self)
pub fn clear_extension_caches(&mut self)
Drop every extension cache entry owned by this compiler.
§Examples
use tenferro_runtime::GraphCompiler;
let mut compiler = GraphCompiler::new();
compiler.clear_extension_caches();
assert!(compiler.extension_caches().is_empty());Sourcepub fn clear_caches(&mut self)
pub fn clear_caches(&mut self)
Drop every cache owned by this compiler.
Sourcepub fn cache_stats(&self) -> CacheStats
pub fn cache_stats(&self) -> CacheStats
Return aggregate cache statistics for this compiler.
§Examples
use tenferro_runtime::GraphCompiler;
let compiler = GraphCompiler::new();
let stats = compiler.cache_stats();
assert_eq!(stats.entries, 0);Sourcepub fn extension_caches(&self) -> &ExtensionCacheStore
pub fn extension_caches(&self) -> &ExtensionCacheStore
Borrow the extension cache store owned by this compiler.
Sourcepub fn extension_caches_mut(&mut self) -> &mut ExtensionCacheStore
pub fn extension_caches_mut(&mut self) -> &mut ExtensionCacheStore
Borrow the extension cache store mutably.