Skip to main content

ShapeGuardContext

Struct ShapeGuardContext 

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

AD context providing dimension resolution, guard recording, and value metadata.

§Examples

use tenferro_ops::ShapeGuardContext;

let ctx = ShapeGuardContext::default();
assert!(ctx.guards().is_empty());

Implementations§

Source§

impl ShapeGuardContext

Source

pub fn with_global_metadata() -> Self

Create a context backed by the global metadata registry.

Instead of cloning the entire global registry up-front (which used to be O(N) per AD pass and quadratic across oracle_replay), the context keeps a flag and lazily fetches entries from the shared lookup_global_metadata on first miss, caching into its local metadata map for subsequent reads within the same pass.

§Examples
let ctx = tenferro_ops::ShapeGuardContext::with_global_metadata();
assert!(ctx.guards().is_empty());
Source

pub fn with_linearize_active_values( self, keys: Arc<HashSet<ValueKey<StdTensorOp>>>, ) -> Self

Source

pub fn is_value_active_in_linearize(&self, key: &ValueKey<StdTensorOp>) -> bool

Whether a primal value lies on a path from the current linearize targets.

When no active set was attached, every value is treated as active so existing callers keep the conservative full JVP graphs.

Source

pub fn set_transpose_primal_outputs( &mut self, keys: Option<Vec<ValueKey<StdTensorOp>>>, )

Primal output keys for the operation currently being transposed.

Primary-mode extension transpose rules such as Eigh use these to reuse forward eigenvectors instead of recomputing a decomposition.

Source

pub fn transpose_primal_outputs(&mut self) -> Option<&[ValueKey<StdTensorOp>]>

Return the current primal outputs and mark them as consumed by this rule.

Source

pub fn transpose_primal_outputs_were_used(&self) -> bool

Source

pub fn guards(&self) -> &[ShapeGuard]

Returns the guards recorded so far.

§Examples
use tenferro_ops::ShapeGuardContext;

let ctx = ShapeGuardContext::default();
assert_eq!(ctx.guards(), &[]);
Source

pub fn clear_guards(&mut self)

Clears all recorded guards.

§Examples
use tenferro_ops::ShapeGuardContext;

let mut ctx = ShapeGuardContext::default();
ctx.clear_guards();
assert!(ctx.guards().is_empty());
Source

pub fn shape_of( &mut self, val: &ValueRef<StdTensorOp>, ) -> ShapeGuardResult<Vec<SymDim>>

Return the shape metadata for a value reference.

§Examples
use computegraph::types::{ValueKey, ValueRef};
use tenferro_ops::input_key::TensorInputKey;
use tenferro_ops::std_tensor_op::StdTensorOp;
use tenferro_ops::{ShapeGuardContext, SymDim, TensorMeta};
use tenferro_tensor::DType;

let key = ValueKey::<StdTensorOp>::Input(TensorInputKey::User { id: 1 });
let value = ValueRef::External(key.clone());
let mut ctx = ShapeGuardContext::default();
ctx.insert_metadata(key, TensorMeta::exact(DType::F64, vec![SymDim::from(4usize)]));

let shape = ctx.shape_of(&value).unwrap();
assert_eq!(shape, &[SymDim::from(4usize)]);
§Errors

Returns ShapeGuardError when the value cannot be resolved, metadata is missing, or the metadata does not describe an exact shape.

Source

pub fn rank_of( &mut self, val: &ValueRef<StdTensorOp>, ) -> ShapeGuardResult<usize>

Return the rank for a value reference without requiring exact extents.

Use this when an AD rule only needs axis count or needs to build runtime-shape references. Calling ShapeGuardContext::shape_of in those cases would reject valid values such as DynamicTruncate outputs whose runtime extent is known only as an upper bound.

§Examples
use computegraph::types::{ValueKey, ValueRef};
use tenferro_ops::input_key::TensorInputKey;
use tenferro_ops::std_tensor_op::StdTensorOp;
use tenferro_ops::{ShapeExtent, ShapeGuardContext, SymDim, TensorMeta};
use tenferro_tensor::DType;

let key = ValueKey::<StdTensorOp>::Input(TensorInputKey::User { id: 1 });
let value = ValueRef::External(key.clone());
let mut ctx = ShapeGuardContext::default();
ctx.insert_metadata(
    key,
    TensorMeta::with_extents(DType::F64, vec![ShapeExtent::upper_bound(SymDim::from(8usize))]),
);

assert_eq!(ctx.rank_of(&value).unwrap(), 1);
§Errors

Returns ShapeGuardError when the value cannot be resolved or its metadata is unavailable.

Source

pub fn extents_of( &mut self, val: &ValueRef<StdTensorOp>, ) -> ShapeGuardResult<&[ShapeExtent<SymDim>]>

Return per-axis shape guarantees for a value reference.

§Examples
use computegraph::types::{ValueKey, ValueRef};
use tenferro_ops::input_key::TensorInputKey;
use tenferro_ops::std_tensor_op::StdTensorOp;
use tenferro_ops::{ShapeExtent, ShapeGuardContext, SymDim, TensorMeta};
use tenferro_tensor::DType;

let key = ValueKey::<StdTensorOp>::Input(TensorInputKey::User { id: 1 });
let value = ValueRef::External(key.clone());
let mut ctx = ShapeGuardContext::default();
ctx.insert_metadata(
    key,
    TensorMeta::with_extents(DType::F64, vec![ShapeExtent::upper_bound(SymDim::from(8usize))]),
);

let extents = ctx.extents_of(&value).unwrap();
assert_eq!(extents[0], ShapeExtent::upper_bound(SymDim::from(8usize)));
§Errors

Returns ShapeGuardError when the value cannot be resolved or its metadata is unavailable.

Source

pub fn exact_shape_of( &mut self, val: &ValueRef<StdTensorOp>, ) -> ShapeGuardResult<Option<Vec<SymDim>>>

Return the exact shape for a value reference, if all axes are exact.

§Examples
use computegraph::types::{ValueKey, ValueRef};
use tenferro_ops::input_key::TensorInputKey;
use tenferro_ops::std_tensor_op::StdTensorOp;
use tenferro_ops::{ShapeExtent, ShapeGuardContext, SymDim, TensorMeta};
use tenferro_tensor::DType;

let key = ValueKey::<StdTensorOp>::Input(TensorInputKey::User { id: 1 });
let value = ValueRef::External(key.clone());
let mut ctx = ShapeGuardContext::default();
ctx.insert_metadata(
    key,
    TensorMeta::with_extents(DType::F64, vec![ShapeExtent::upper_bound(SymDim::from(8usize))]),
);

let maybe_shape = ctx.exact_shape_of(&value).unwrap();
assert_eq!(maybe_shape, None);
§Errors

Returns ShapeGuardError when the value cannot be resolved or its metadata is unavailable.

Source

pub fn dtype_of( &mut self, val: &ValueRef<StdTensorOp>, ) -> ShapeGuardResult<DType>

Return the dtype metadata for a value reference.

§Examples
use computegraph::types::{ValueKey, ValueRef};
use tenferro_ops::input_key::TensorInputKey;
use tenferro_ops::std_tensor_op::StdTensorOp;
use tenferro_ops::{ShapeGuardContext, SymDim, TensorMeta};
use tenferro_tensor::DType;

let key = ValueKey::<StdTensorOp>::Input(TensorInputKey::User { id: 1 });
let value = ValueRef::External(key.clone());
let mut ctx = ShapeGuardContext::default();
ctx.insert_metadata(key, TensorMeta::exact(DType::F64, vec![SymDim::from(4usize)]));

let dtype = ctx.dtype_of(&value).unwrap();
assert_eq!(dtype, DType::F64);
§Errors

Returns ShapeGuardError when the value cannot be resolved or its metadata is unavailable.

Source

pub fn metadata_of( &mut self, val: &ValueRef<StdTensorOp>, ) -> ShapeGuardResult<&TensorMeta>

Return the complete metadata record for a value reference.

§Examples
use computegraph::types::{ValueKey, ValueRef};
use tenferro_ops::input_key::TensorInputKey;
use tenferro_ops::std_tensor_op::StdTensorOp;
use tenferro_ops::{ShapeGuardContext, SymDim, TensorMeta};
use tenferro_tensor::DType;

let key = ValueKey::<StdTensorOp>::Input(TensorInputKey::User { id: 1 });
let value = ValueRef::External(key.clone());
let mut ctx = ShapeGuardContext::default();
ctx.insert_metadata(key, TensorMeta::exact(DType::F64, vec![SymDim::from(4usize)]));

let meta = ctx.metadata_of(&value).unwrap();
assert_eq!(meta.dtype, DType::F64);
§Errors

Returns ShapeGuardError when the value cannot be resolved or its metadata is unavailable.

Trait Implementations§

Source§

impl Clone for ShapeGuardContext

Source§

fn clone(&self) -> ShapeGuardContext

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 ShapeGuardContext

Source§

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

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

impl Default for ShapeGuardContext

Source§

fn default() -> ShapeGuardContext

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> 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> 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 = 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,