Skip to main content

GraphCompiler

Struct GraphCompiler 

Source
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::{GraphCompiler, TracedTensor};

let x = TracedTensor::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

Source

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());
Source

pub fn with_compiler_options(compiler_options: CompilerOptions) -> Self

Create a compiler with explicit lowering and optimizer options.

§Examples
use tenferro_runtime::{CompilerOptions, OptimizerConfig};
use tenferro_runtime::GraphCompiler;

let compiler = GraphCompiler::with_compiler_options(CompilerOptions {
    optimizer: OptimizerConfig {
        dot_decomposer: true,
        ..OptimizerConfig::default()
    },
});
assert!(compiler.compiler_options().optimizer.dot_decomposer);
Source

pub fn compile(&mut self, output: &TracedTensor) -> Result<CompiledGraph>

Compile one traced output into a graph program.

§Examples
use tenferro_runtime::{GraphCompiler, TracedTensor};

let x = TracedTensor::from_vec_col_major(vec![1], vec![2.0_f64]).unwrap();
let mut compiler = GraphCompiler::new();
let y = x.neg().unwrap();
let program = compiler.compile(&y).unwrap();
assert_eq!(program.input_count(), 1);
§Errors

Returns Error::Validation with ShapeMismatch, RankMismatch, DTypeMismatch, or InvalidArgument for invalid graph metadata or shape constraints, Error::RuntimeState for missing/inconsistent metadata or cache state, and Error::Internal when the graph violates a compiler invariant. Extension lowering failures retain their typed Error::Extension source.

Source

pub fn compile_traced_graph( &mut self, graph: &TracedGraph, ) -> Result<CompiledGraph>

Compile an immutable semantic trace without consulting a backend.

This is the forward-only trace boundary. The compiler preserves the frozen semantic program and bindings without preparing backend/runtime staging. Runtime preparation owns backend-private staging and plan caches.

§Errors

Returns Error::Validation for invalid metadata or shape constraints, Error::Extension when extension lowering fails, Error::RuntimeState for inconsistent staging state, or Error::Internal when compilation encounters an invariant violation.

Source

pub fn compile_frozen_program( &mut self, frozen: &FrozenProgram, ) -> Result<CompiledGraph>

Compile an immutable semantic program for ordered execution.

This entry is used by validation-preserving semantic transforms such as whole-program AD. Tensor bindings remain outside semantic identity and are preserved in the returned CompiledGraph.

§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, Error::Extension when extension lowering fails, Error::RuntimeState for inconsistent staging state, or Error::Internal when compilation encounters an invariant violation.

Source

pub fn compile_many( &mut self, outputs: &[&TracedTensor], ) -> Result<CompiledGraph>

Compile multiple traced outputs into one graph program.

§Examples
use tenferro_runtime::{GraphCompiler, TracedTensor};

let x = TracedTensor::from_vec_col_major(vec![1], vec![2.0_f64]).unwrap();
let y = x.neg().unwrap();
let mut compiler = GraphCompiler::new();
let program = compiler.compile_many(&[&x, &y]).unwrap();
assert_eq!(program.output_count(), 2);
§Errors

Returns Error::Validation with ShapeMismatch, RankMismatch, DTypeMismatch, or InvalidArgument for invalid graph metadata or shape constraints, Error::RuntimeState for missing/inconsistent metadata or cache state, and Error::Internal when the graph violates a compiler invariant. Extension lowering failures retain their typed Error::Extension source.

Source

pub fn compile_with_input_specs( &mut self, output: &TracedTensor, bindings: &[(&TracedTensor, DType, &[usize])], ) -> Result<CompiledGraph>

Compile one traced output with concrete placeholder specs.

§Examples
use tenferro_runtime::{DType, GraphCompiler, TracedTensor};

let x = TracedTensor::input_symbolic_shape(DType::F64, 1).unwrap();
let mut compiler = GraphCompiler::new();
let y = x.neg().unwrap();
let program = compiler
    .compile_with_input_specs(&y, &[(&x, DType::F64, &[3])])
    .unwrap();
assert_eq!(program.input_count(), 1);
§Errors

Returns Error::UnexpectedBinding for a data-carrying tensor, Error::DuplicateBinding for repeated placeholders, Error::PlaceholderDtypeMismatch, Error::PlaceholderShapeMismatch, or Error::PlaceholderRankMismatch for incompatible specs, and Error::Validation with ShapeMismatch, RankMismatch, DTypeMismatch, or InvalidArgument / Error::RuntimeState when compilation or metadata lowering fails.

Source

pub fn compiler_options(&self) -> CompilerOptions

Return the compiler options used for future graph lowerings.

§Examples
use tenferro_runtime::CompilerOptions;
use tenferro_runtime::GraphCompiler;

let compiler = GraphCompiler::new();
assert_eq!(compiler.compiler_options(), CompilerOptions::default());
Source

pub fn set_compiler_options(&mut self, compiler_options: CompilerOptions)

Replace compiler options and clear compiler-owned extension cache entries.

§Examples
use tenferro_runtime::{CompilerOptions, OptimizerConfig};
use tenferro_runtime::GraphCompiler;

let mut compiler = GraphCompiler::new();
let options = CompilerOptions {
    optimizer: OptimizerConfig {
        dot_decomposer: true,
        ..OptimizerConfig::default()
    },
};
compiler.set_compiler_options(options);
assert_eq!(compiler.compiler_options(), options);
assert_eq!(compiler.cache_stats().entries, 0);
Source

pub fn clear_extension_caches(&mut self)

Clear generic extension compile-time cache entries.

§Examples
use tenferro_runtime::GraphCompiler;

let mut compiler = GraphCompiler::new();
compiler.clear_extension_caches();
assert_eq!(compiler.cache_stats().entries, 0);
Source

pub fn clear_caches(&mut self)

Clear every cache owned by the compiler.

§Examples
use tenferro_runtime::GraphCompiler;

let mut compiler = GraphCompiler::new();
compiler.clear_caches();
assert_eq!(compiler.cache_stats().entries, 0);
Source

pub fn cache_stats(&self) -> CacheStats

Return compiler-owned extension cache-entry and retained-byte stats.

§Examples
use tenferro_runtime::GraphCompiler;

let compiler = GraphCompiler::new();
let stats = compiler.cache_stats();
assert_eq!(stats.entries, 0);
Source

pub fn extension_caches(&self) -> &ExtensionCacheStore

Borrow generic compiler-owned extension cache storage.

§Examples
use tenferro_runtime::GraphCompiler;

let compiler = GraphCompiler::new();
assert!(compiler.extension_caches().is_empty());
Source

pub fn extension_caches_mut(&mut self) -> &mut ExtensionCacheStore

Mutably borrow generic compiler-owned extension cache storage.

§Examples
use tenferro_runtime::GraphCompiler;

let mut compiler = GraphCompiler::new();
compiler.extension_caches_mut().clear();

Trait Implementations§

Source§

impl Debug for GraphCompiler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for GraphCompiler

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> MaybeSend for T
where T: Send,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,

§

impl<T> MaybeSync for T
where T: Sync,