pub struct GradientTape { /* private fields */ }Expand description
Gradient tape for reverse-mode automatic differentiation.
§Usage
- Create a tape.
- Record operations during the forward pass using
record_*methods. Each method returns an output tensor index. - After computing the scalar loss, call
backwardwith the gradient of the loss w.r.t. the output to accumulate gradients into all upstream tensors.
Implementations§
Source§impl GradientTape
impl GradientTape
Sourcepub fn record_add(&mut self, a: usize, b: usize) -> usize
pub fn record_add(&mut self, a: usize, b: usize) -> usize
Record an elementwise add: out = tensors[a] + tensors[b].
Returns the output tensor index.
Sourcepub fn record_mul(
&mut self,
a: usize,
a_data: &Array1<f32>,
b: usize,
b_data: &Array1<f32>,
) -> usize
pub fn record_mul( &mut self, a: usize, a_data: &Array1<f32>, b: usize, b_data: &Array1<f32>, ) -> usize
Record an elementwise multiply: out = tensors[a] * tensors[b].
a_data and b_data are the forward values saved for gradient
computation (product rule).
Returns the output tensor index.
Sourcepub fn record_matmul(
&mut self,
a: usize,
a_mat: &Array2<f32>,
b: usize,
b_mat: &Array2<f32>,
) -> usize
pub fn record_matmul( &mut self, a: usize, a_mat: &Array2<f32>, b: usize, b_mat: &Array2<f32>, ) -> usize
Record a matrix-matrix multiply: out = A @ B (result flattened to 1-D).
Returns the output tensor index.
Sourcepub fn record_silu(&mut self, input: usize, input_data: &Array1<f32>) -> usize
pub fn record_silu(&mut self, input: usize, input_data: &Array1<f32>) -> usize
Record a SiLU activation: out = x * sigmoid(x).
input_data is the pre-activation tensor value.
Returns the output tensor index.
Sourcepub fn record_layer_norm(
&mut self,
input: usize,
mean: f32,
var: f32,
scale: &Array1<f32>,
) -> usize
pub fn record_layer_norm( &mut self, input: usize, mean: f32, var: f32, scale: &Array1<f32>, ) -> usize
Record a layer-norm operation.
Returns the output tensor index.
Sourcepub fn record_ssm_scan(
&mut self,
input: usize,
a_vals: &Array1<f32>,
b_vals: &Array1<f32>,
) -> usize
pub fn record_ssm_scan( &mut self, input: usize, a_vals: &Array1<f32>, b_vals: &Array1<f32>, ) -> usize
Record a simplified SSM scan step.
Returns the output tensor index.
Sourcepub fn backward(
&self,
loss_grad: Array1<f32>,
tensors: &mut Vec<Array1<f32>>,
) -> ModelResult<()>
pub fn backward( &self, loss_grad: Array1<f32>, tensors: &mut Vec<Array1<f32>>, ) -> ModelResult<()>
Run reverse-mode backpropagation.
§Parameters
loss_grad: gradient of the scalar loss w.r.t. the final output (shape must match the last recorded output).tensors: gradient buffers, oneArray1<f32>per allocated tensor slot. The caller is responsible for initialising these to zeros and ensuringtensors.len() >= self.num_tensors. After the call each buffer holds the accumulated gradient for that tensor slot.
§Errors
Returns ModelError::numerical_instability if any gradient contains
NaN or Inf values.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for GradientTape
impl RefUnwindSafe for GradientTape
impl Send for GradientTape
impl Sync for GradientTape
impl Unpin for GradientTape
impl UnsafeUnpin for GradientTape
impl UnwindSafe for GradientTape
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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