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
impl ShapeGuardContext
Sourcepub fn with_global_metadata() -> Self
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());pub fn with_linearize_active_values( self, keys: Arc<HashSet<ValueKey<StdTensorOp>>>, ) -> Self
Sourcepub fn is_value_active_in_linearize(&self, key: &ValueKey<StdTensorOp>) -> bool
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.
Sourcepub fn set_transpose_primal_outputs(
&mut self,
keys: Option<Vec<ValueKey<StdTensorOp>>>,
)
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.
Sourcepub fn transpose_primal_outputs(&mut self) -> Option<&[ValueKey<StdTensorOp>]>
pub fn transpose_primal_outputs(&mut self) -> Option<&[ValueKey<StdTensorOp>]>
Return the current primal outputs and mark them as consumed by this rule.
pub fn transpose_primal_outputs_were_used(&self) -> bool
Sourcepub fn guards(&self) -> &[ShapeGuard]
pub fn guards(&self) -> &[ShapeGuard]
Returns the guards recorded so far.
§Examples
use tenferro_ops::ShapeGuardContext;
let ctx = ShapeGuardContext::default();
assert_eq!(ctx.guards(), &[]);Sourcepub fn clear_guards(&mut self)
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());Sourcepub fn shape_of(
&mut self,
val: &ValueRef<StdTensorOp>,
) -> ShapeGuardResult<Vec<SymDim>>
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.
Sourcepub fn rank_of(
&mut self,
val: &ValueRef<StdTensorOp>,
) -> ShapeGuardResult<usize>
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.
Sourcepub fn extents_of(
&mut self,
val: &ValueRef<StdTensorOp>,
) -> ShapeGuardResult<&[ShapeExtent<SymDim>]>
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.
Sourcepub fn exact_shape_of(
&mut self,
val: &ValueRef<StdTensorOp>,
) -> ShapeGuardResult<Option<Vec<SymDim>>>
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.
Sourcepub fn dtype_of(
&mut self,
val: &ValueRef<StdTensorOp>,
) -> ShapeGuardResult<DType>
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.
Sourcepub fn metadata_of(
&mut self,
val: &ValueRef<StdTensorOp>,
) -> ShapeGuardResult<&TensorMeta>
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
impl Clone for ShapeGuardContext
Source§fn clone(&self) -> ShapeGuardContext
fn clone(&self) -> ShapeGuardContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ShapeGuardContext
impl Debug for ShapeGuardContext
Source§impl Default for ShapeGuardContext
impl Default for ShapeGuardContext
Source§fn default() -> ShapeGuardContext
fn default() -> ShapeGuardContext
Auto Trait Implementations§
impl Freeze for ShapeGuardContext
impl !RefUnwindSafe for ShapeGuardContext
impl Send for ShapeGuardContext
impl Sync for ShapeGuardContext
impl Unpin for ShapeGuardContext
impl UnsafeUnpin for ShapeGuardContext
impl !UnwindSafe for ShapeGuardContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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