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_extension_rules(self, rules: ExtensionRuleSet) -> Self

Use an explicit extension AD rule set for this context.

Extension AD lookup is context-owned: a context without an attached rule set has no extension AD rules.

§Examples
use tenferro_ops::{ExtensionRuleSet, ShapeGuardContext};

let _ctx = ShapeGuardContext::default().with_extension_rules(ExtensionRuleSet::new());
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)]);
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);
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)));
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);
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);
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);

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> 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.