pub struct ResidualSpec { /* private fields */ }Expand description
Per-rule declaration of which primal inputs/outputs a transpose (VJP) rule reads as full tensor residuals versus which need only shape/dtype metadata.
The AD engine uses this to bound residual retention: an index not declared here may only be accessed through its metadata, never as a tensor operand. Indices are counted in primal-input order (input mask) and primal-output order (output mask).
Individual indices (ResidualSpec::input, ResidualSpec::with_input,
and the output counterparts) are limited to 0..64. The all-index
declarations (ResidualSpec::all_inputs, ResidualSpec::with_all_inputs,
and the output counterparts) cover every index, including indices >= 64,
so a variadic rule such as einsum or concatenate declares its whole arity.
§Examples
use tenferro_ops::ad::ResidualSpec;
// `add` needs no tensor residuals; `mul` keeps both operands.
let add = ResidualSpec::none();
let mul = ResidualSpec::input(0).with_input(1);
assert!(add.is_empty());
assert!(mul.declares_input(0) && mul.declares_input(1));
// Unary ops that reuse the forward output (e.g. exp) declare it.
let exp = ResidualSpec::output(0);
assert!(exp.declares_output(0));
// Variadic rules declare every operand, whatever the arity.
let einsum = ResidualSpec::all_inputs();
assert!(einsum.declares_input(0) && einsum.declares_input(64));Implementations§
Source§impl ResidualSpec
impl ResidualSpec
Sourcepub const fn input(index: usize) -> Self
pub const fn input(index: usize) -> Self
A mask declaring one input index as a tensor residual.
§Panics
Panics if index >= 64; declare such inputs with
ResidualSpec::all_inputs.
Sourcepub const fn output(index: usize) -> Self
pub const fn output(index: usize) -> Self
A mask declaring one output index as a tensor residual.
§Panics
Panics if index >= 64; declare such outputs with
ResidualSpec::all_outputs.
Sourcepub const fn with_input(self, index: usize) -> Self
pub const fn with_input(self, index: usize) -> Self
Add one input index to this mask.
§Panics
Panics if index >= 64; declare such inputs with
ResidualSpec::with_all_inputs.
Sourcepub const fn with_output(self, index: usize) -> Self
pub const fn with_output(self, index: usize) -> Self
Add one output index to this mask.
§Panics
Panics if index >= 64; declare such outputs with
ResidualSpec::with_all_outputs.
Sourcepub const fn with_all_inputs(self) -> Self
pub const fn with_all_inputs(self) -> Self
Add every input index, of any arity, to this mask.
Sourcepub const fn with_all_outputs(self) -> Self
pub const fn with_all_outputs(self) -> Self
Add every output index, of any arity, to this mask.
Sourcepub const fn all_inputs() -> Self
pub const fn all_inputs() -> Self
A mask declaring every input index, of any arity, as a tensor residual.
Used by rules whose required operand set depends on the active-input
configuration (e.g. mul, concatenate, einsum), and by multi-op
families whose individual ops collectively read any operand.
Sourcepub const fn all_outputs() -> Self
pub const fn all_outputs() -> Self
A mask declaring every output index, of any arity, as a tensor residual.
Sourcepub const fn declares_input(&self, index: usize) -> bool
pub const fn declares_input(&self, index: usize) -> bool
Whether input index is declared as a tensor residual.
Sourcepub const fn declares_output(&self, index: usize) -> bool
pub const fn declares_output(&self, index: usize) -> bool
Whether output index is declared as a tensor residual.