pub struct ExtensionRuleSet { /* private fields */ }Expand description
Explicit, owned set of extension AD rules.
This is the rule container used by higher-level AD contexts. Extension AD intentionally has no process-global fallback; callers must pass the rule set that their graph needs.
Implementations§
Source§impl ExtensionRuleSet
impl ExtensionRuleSet
Sourcepub fn new() -> ExtensionRuleSet
pub fn new() -> ExtensionRuleSet
Create an empty extension rule set.
§Examples
use tenferro_ops::ExtensionRuleSet;
let rules = ExtensionRuleSet::new();
assert!(!rules.is_linearize_registered("example.missing.v1"));Sourcepub fn register_linearize(
&mut self,
rule: Arc<dyn ExtensionLinearizeRule>,
) -> Result<(), ExtensionRegistryError>
pub fn register_linearize( &mut self, rule: Arc<dyn ExtensionLinearizeRule>, ) -> Result<(), ExtensionRegistryError>
Add one linearize rule to this owned set.
§Examples
use std::sync::Arc;
use tidu::ADRuleResult;
use computegraph::types::{LocalValueId, ValueKey};
use tenferro_ops::ad::PrimitiveRuleBuilder;
use tenferro_ops::ext_op::{ExtensionLinearizeRule, ExtensionOp};
use tenferro_ops::{ExtensionRuleSet, ShapeGuardContext};
use tenferro_ops::std_tensor_op::StdTensorOp;
#[derive(Debug)]
struct Rule;
impl ExtensionLinearizeRule for Rule {
fn family_id(&self) -> &'static str { "example.register_linearize.v1" }
fn linearize(
&self,
_op: &dyn ExtensionOp,
_builder: &mut dyn PrimitiveRuleBuilder,
_primal_in: &[ValueKey<StdTensorOp>],
_primal_out: &[ValueKey<StdTensorOp>],
tangent_in: &[Option<LocalValueId>],
_ctx: &mut ShapeGuardContext,
) -> ADRuleResult<Vec<Option<LocalValueId>>> {
Ok(tangent_in.to_vec())
}
}
let mut rules = ExtensionRuleSet::new();
rules.register_linearize(Arc::new(Rule)).unwrap();
assert!(rules.lookup_linearize("example.register_linearize.v1").is_some());Sourcepub fn register_linear_transpose(
&mut self,
rule: Arc<dyn ExtensionLinearTransposeRule>,
) -> Result<(), ExtensionRegistryError>
pub fn register_linear_transpose( &mut self, rule: Arc<dyn ExtensionLinearTransposeRule>, ) -> Result<(), ExtensionRegistryError>
Add one linear-transpose rule to this owned set.
Sourcepub fn register_primal_vjp(
&mut self,
rule: Arc<dyn ExtensionPrimalVjpRule>,
) -> Result<(), ExtensionRegistryError>
pub fn register_primal_vjp( &mut self, rule: Arc<dyn ExtensionPrimalVjpRule>, ) -> Result<(), ExtensionRegistryError>
Add one primal-VJP rule to this owned set.
Sourcepub fn with_linearize(
self,
rule: Arc<dyn ExtensionLinearizeRule>,
) -> Result<ExtensionRuleSet, ExtensionRegistryError>
pub fn with_linearize( self, rule: Arc<dyn ExtensionLinearizeRule>, ) -> Result<ExtensionRuleSet, ExtensionRegistryError>
Return a new rule set containing a linearize rule.
§Examples
use std::sync::Arc;
use tidu::ADRuleResult;
use computegraph::types::{LocalValueId, ValueKey};
use tenferro_ops::ad::PrimitiveRuleBuilder;
use tenferro_ops::ext_op::{ExtensionLinearizeRule, ExtensionOp};
use tenferro_ops::{ExtensionRuleSet, ShapeGuardContext};
use tenferro_ops::std_tensor_op::StdTensorOp;
#[derive(Debug)]
struct Rule;
impl ExtensionLinearizeRule for Rule {
fn family_id(&self) -> &'static str { "example.with_linearize.v1" }
fn linearize(
&self,
_op: &dyn ExtensionOp,
_builder: &mut dyn PrimitiveRuleBuilder,
_primal_in: &[ValueKey<StdTensorOp>],
_primal_out: &[ValueKey<StdTensorOp>],
tangent_in: &[Option<LocalValueId>],
_ctx: &mut ShapeGuardContext,
) -> ADRuleResult<Vec<Option<LocalValueId>>> {
Ok(tangent_in.to_vec())
}
}
let rules = ExtensionRuleSet::new().with_linearize(Arc::new(Rule)).unwrap();
assert!(rules.is_linearize_registered("example.with_linearize.v1"));Sourcepub fn with_linear_transpose(
self,
rule: Arc<dyn ExtensionLinearTransposeRule>,
) -> Result<ExtensionRuleSet, ExtensionRegistryError>
pub fn with_linear_transpose( self, rule: Arc<dyn ExtensionLinearTransposeRule>, ) -> Result<ExtensionRuleSet, ExtensionRegistryError>
Return a new rule set containing a linear-transpose rule.
Sourcepub fn with_primal_vjp(
self,
rule: Arc<dyn ExtensionPrimalVjpRule>,
) -> Result<ExtensionRuleSet, ExtensionRegistryError>
pub fn with_primal_vjp( self, rule: Arc<dyn ExtensionPrimalVjpRule>, ) -> Result<ExtensionRuleSet, ExtensionRegistryError>
Return a new rule set containing a primal-VJP rule.
Sourcepub fn merge(
&mut self,
other: ExtensionRuleSet,
) -> Result<(), ExtensionRegistryError>
pub fn merge( &mut self, other: ExtensionRuleSet, ) -> Result<(), ExtensionRegistryError>
Merge another owned rule set into this one.
The merge is atomic: if any rule in other is invalid or duplicates an
existing family, self is left unchanged.
§Examples
use tenferro_ops::ExtensionRuleSet;
let mut rules = ExtensionRuleSet::new();
rules.merge(ExtensionRuleSet::new()).unwrap();
assert!(!rules.is_linearize_registered("example.missing.v1"));Sourcepub fn lookup_linearize(
&self,
family_id: &str,
) -> Option<Arc<dyn ExtensionLinearizeRule>>
pub fn lookup_linearize( &self, family_id: &str, ) -> Option<Arc<dyn ExtensionLinearizeRule>>
Look up a linearize rule in this set.
§Examples
use tenferro_ops::ExtensionRuleSet;
let rules = ExtensionRuleSet::new();
assert!(rules.lookup_linearize("example.missing.v1").is_none());Sourcepub fn lookup_linear_transpose(
&self,
family_id: &str,
) -> Option<Arc<dyn ExtensionLinearTransposeRule>>
pub fn lookup_linear_transpose( &self, family_id: &str, ) -> Option<Arc<dyn ExtensionLinearTransposeRule>>
Look up a linear-transpose rule in this set.
Sourcepub fn lookup_primal_vjp(
&self,
family_id: &str,
) -> Option<Arc<dyn ExtensionPrimalVjpRule>>
pub fn lookup_primal_vjp( &self, family_id: &str, ) -> Option<Arc<dyn ExtensionPrimalVjpRule>>
Look up a primal-VJP rule in this set.
Sourcepub fn is_linearize_registered(&self, family_id: &str) -> bool
pub fn is_linearize_registered(&self, family_id: &str) -> bool
Return whether family_id has a linearize rule.
§Examples
use tenferro_ops::ExtensionRuleSet;
let rules = ExtensionRuleSet::new();
assert!(!rules.is_linearize_registered("example.missing.v1"));Sourcepub fn is_linear_transpose_registered(&self, family_id: &str) -> bool
pub fn is_linear_transpose_registered(&self, family_id: &str) -> bool
Return whether family_id has a linear-transpose rule.
Sourcepub fn is_primal_vjp_registered(&self, family_id: &str) -> bool
pub fn is_primal_vjp_registered(&self, family_id: &str) -> bool
Return whether family_id has a primal-VJP rule.
Trait Implementations§
Source§impl Clone for ExtensionRuleSet
impl Clone for ExtensionRuleSet
Source§fn clone(&self) -> ExtensionRuleSet
fn clone(&self) -> ExtensionRuleSet
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 ExtensionRuleSet
Available on crate feature autodiff only.
impl Debug for ExtensionRuleSet
autodiff only.Source§impl Default for ExtensionRuleSet
impl Default for ExtensionRuleSet
Source§fn default() -> ExtensionRuleSet
fn default() -> ExtensionRuleSet
Auto Trait Implementations§
impl Freeze for ExtensionRuleSet
impl !RefUnwindSafe for ExtensionRuleSet
impl Send for ExtensionRuleSet
impl Sync for ExtensionRuleSet
impl Unpin for ExtensionRuleSet
impl UnsafeUnpin for ExtensionRuleSet
impl !UnwindSafe for ExtensionRuleSet
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,
§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> Twhere
Self: Distribution<T>,
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