Skip to main content

TraceContext

Struct TraceContext 

Source
pub struct TraceContext { /* private fields */ }
Expand description

Shared, cloneable handle to one mutable semantic trace.

Cloning the handle does not copy the trace. Values issued by one context cannot be used by another, and the handle is deliberately not Send: the trace is a single-threaded construction value, while eager execution keeps a frozen program instead of a live handle.

§Examples

use tenferro_runtime::{DType, TraceContext};

let trace = TraceContext::new();
let x = trace.from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
let y = x.reshape([2])?;

// A clone is the same trace; a second context is a different one.
let same = trace.clone();
assert!(same.same_context(&trace));
assert!(!TraceContext::new().same_context(&trace));

Implementations§

Source§

impl TraceContext

Source

pub fn new() -> Self

Construct an empty trace context.

§Examples
use tenferro_runtime::TraceContext;

let mut trace = TraceContext::new();
assert!(trace.finish(&[]).is_ok());
Source

pub fn input( &self, spec: ProgramInputSpec, ) -> Result<TraceValue, ProgramBuildError>

Add one ordered external input.

§Errors

Returns ProgramBuildError::TooManyValues if another value cannot be represented, or ProgramBuildError::ReentrantContextBorrow when the context is already borrowed by an operation in progress.

Source

pub fn input_with_default( &self, spec: ProgramInputSpec, tensor: Tensor, ) -> Result<TraceValue, ProgramBuildError>

Add one ordered input and attach its default tensor.

§Errors

Returns ProgramBuildError::TooManyValues when another input cannot be represented, ProgramBuildError::BindingTargetNotInput if the new value is not accepted as an input, or ProgramBuildError::DuplicateBinding if it is already bound.

Source

pub fn bind_input( &self, input: TraceValue, tensor: Tensor, ) -> Result<BindingKey, ProgramBuildError>

Attach a tensor default to an existing trace input.

§Errors

Returns ProgramBuildError::ForeignValue for another context’s value, ProgramBuildError::BindingTargetNotInput for a computed value, or ProgramBuildError::DuplicateBinding for a repeated binding.

Source

pub fn add_op( &self, op: CoreSemanticOp, inputs: &[TraceValue], ) -> Result<Box<[TraceValue]>, ProgramBuildError>

Add one canonical core semantic operation.

§Errors

Returns ProgramBuildError::ForeignValue for an input from another context, ProgramBuildError::Arity for the wrong input count, ProgramBuildError::Metadata / ProgramBuildError::OutputMetadataCount when inference returns invalid metadata, or ProgramBuildError::ReentrantContextBorrow when the context is already borrowed.

Source

pub fn add_extension( &self, op: Arc<dyn ExtensionOp>, inputs: &[TraceValue], ) -> Result<Box<[TraceValue]>, ProgramBuildError>

Add one extension semantic operation.

§Errors

Returns ProgramBuildError::ForeignValue or ProgramBuildError::Arity for invalid inputs, ProgramBuildError::UndeclaredExtensionEffects / ProgramBuildError::UndeclaredExtensionAliases for an incomplete extension contract, ProgramBuildError::Metadata when inference fails, or ProgramBuildError::ReentrantContextBorrow when the context is already borrowed.

Source

pub fn value_metadata( &self, value: TraceValue, ) -> Result<ProgramValueMetadata, ProgramBuildError>

Return metadata for a context-owned value.

§Errors

Returns ProgramBuildError::ForeignValue for a value from another context, or ProgramBuildError::ReentrantContextBorrow when the context is already borrowed.

Source

pub fn with_extension_caches<R>( &self, f: impl FnOnce(&mut ExtensionCacheStore) -> R, ) -> Result<R, ProgramBuildError>

Run f with the generic trace-time extension cache store.

The store is borrowed only for the call: a live context shares interior mutability, so a &mut borrow cannot escape.

§Errors

Returns ProgramBuildError::ReentrantContextBorrow when the context is already borrowed.

Source

pub fn finish( &self, outputs: &[TraceValue], ) -> Result<TracedGraph, ProgramFinishError>

Freeze the ordered closure of outputs without consuming the context.

Later appends do not change the returned graph, and the context stays usable for further tracing.

§Errors

Returns ProgramFinishError::ForeignOutput if an output belongs to another context, ProgramFinishError::ReentrantContextBorrow when the context is already borrowed, or a typed structural/binding finalization failure.

Source§

impl TraceContext

Source

pub fn input_concrete_shape( &self, dtype: DType, shape: &[usize], ) -> Result<TracedTensor>

Add a placeholder input with a fixed concrete shape.

§Examples
use tenferro_runtime::TraceContext;
use tenferro_tensor::DType;

let trace = TraceContext::new();
let x = trace.input_concrete_shape(DType::F64, &[2, 3])?;
assert_eq!(x.rank(), 2);
assert!(x.is_concrete_shape());
§Errors

Returns Error::RuntimeState when the trace cannot accept another input or a binding already exists for it.

Source

pub fn input_concrete_shape_declaring_scalar( &self, dtype: DType, shape: &[usize], identity: &'static str, ) -> Result<TracedTensor>

Add a placeholder input of concrete shape declaring an external scalar.

§Errors

Returns Error::RuntimeState when the trace cannot accept another input or a binding already exists for it, and Error::RuntimeStateSource wrapping ProgramBuildError::ExternalScalarWithoutIdentity when an externally defined scalar dtype reaches the builder without the declared identity.

Source

pub fn input_symbolic_shape( &self, dtype: DType, rank: usize, ) -> Result<TracedTensor>

Add a placeholder input with symbolic (rank-only) extents.

§Examples
use tenferro_runtime::TraceContext;
use tenferro_tensor::DType;

let trace = TraceContext::new();
let x = trace.input_symbolic_shape(DType::F64, 2)?;
assert_eq!(x.rank(), 2);
assert!(!x.is_concrete_shape());
§Errors

Returns Error::RuntimeState when the trace cannot accept another input.

Source

pub fn from_tensor_concrete_shape(&self, tensor: Tensor) -> Result<TracedTensor>

Add a constant leaf holding tensor with its concrete shape.

§Examples
use tenferro_runtime::{Tensor, TraceContext};

let trace = TraceContext::new();
let x = trace.from_tensor_concrete_shape(
    Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?,
)?;
assert!(x.attached_value().is_some());
§Errors

Returns Error::Validation for a malformed tensor or Error::RuntimeState when the trace cannot bind the constant.

Source

pub fn from_tensor_concrete_shape_declaring_scalar( &self, tensor: Tensor, identity: &'static str, ) -> Result<TracedTensor>

Add a constant leaf of concrete shape declaring an external scalar.

§Errors

Returns Error::Validation for a malformed tensor, Error::RuntimeState when the trace cannot bind the constant, and Error::RuntimeStateSource wrapping ProgramBuildError::ExternalScalarWithoutIdentity when an externally defined scalar dtype reaches the builder without the declared identity.

Source

pub fn from_tensor_symbolic_shape(&self, tensor: Tensor) -> Result<TracedTensor>

Add a constant leaf holding tensor but advertising symbolic extents.

§Errors

Returns Error::RuntimeState when the trace cannot bind the constant.

Source

pub fn from_vec_col_major<T: TensorScalar>( &self, shape: impl IntoShapeVec, data: Vec<T>, ) -> Result<TracedTensor>

Add a constant leaf from compact column-major data.

§Errors

Returns Error::TensorRuntime with a typed validation error when the shape and data length disagree.

Trait Implementations§

Source§

impl Clone for TraceContext

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TraceContext

Source§

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

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

impl Default for TraceContext

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.