Skip to main content

GradStore

Struct GradStore 

Source
pub struct GradStore<R: Runtime> { /* private fields */ }
Expand description

Storage for gradients computed during backward pass

Gradients are stored by tensor ID and accumulated when a tensor is used multiple times in the computation graph.

Implementations§

Source§

impl<R: Runtime> GradStore<R>

Source

pub fn new() -> Self

Create a new empty gradient store

Source

pub fn get(&self, id: TensorId) -> Option<&Tensor<R>>

Get the gradient for a tensor

Source

pub fn insert(&mut self, id: TensorId, grad: Tensor<R>)

Insert a gradient (overwrites if exists)

Source

pub fn contains(&self, id: TensorId) -> bool

Check if a gradient exists

Source

pub fn remove(&mut self, id: TensorId) -> Option<Tensor<R>>

Remove and return a gradient

Source

pub fn keys(&self) -> impl Iterator<Item = &TensorId>

Get all tensor IDs with gradients

Source

pub fn len(&self) -> usize

Number of stored gradients

Source

pub fn is_empty(&self) -> bool

Check if empty

Source

pub fn clear(&mut self)

Clear all gradients

Source

pub fn accumulate<F>(&mut self, id: TensorId, grad: Tensor<R>, add_fn: F)
where F: FnOnce(Tensor<R>, Tensor<R>) -> Tensor<R>,

Accumulate a gradient for a tensor

If no gradient exists for this tensor, stores the gradient. If a gradient already exists, adds the new gradient to the existing one.

§Arguments
  • id - The tensor ID to accumulate gradient for
  • grad - The gradient tensor to accumulate
  • add_fn - Function to add two tensors: fn(existing, new) -> sum

This is used when a tensor is used multiple times in the computation graph, requiring its gradients to be summed according to the chain rule.

Source

pub fn try_accumulate<F>( &mut self, id: TensorId, grad: Tensor<R>, add_fn: F, ) -> Result<()>
where F: FnOnce(Tensor<R>, Tensor<R>) -> Result<Tensor<R>>,

Accumulate a gradient with a fallible addition function

Like accumulate, but the addition function can fail and return a Result. This is the preferred method for use in backward passes where tensor operations may fail.

Source

pub fn accumulate_or_insert(&mut self, id: TensorId, grad: Tensor<R>)

Insert a gradient, overwriting any existing value

This is intentionally simpler than accumulate - it directly inserts the gradient without addition. Use this when you don’t have access to a TensorOps client, or when overwriting semantics are desired.

For proper gradient accumulation (adding to existing gradients), use accumulate with an add function instead.

Trait Implementations§

Source§

impl<R: Runtime> Default for GradStore<R>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<R> Freeze for GradStore<R>

§

impl<R> RefUnwindSafe for GradStore<R>

§

impl<R> Send for GradStore<R>

§

impl<R> Sync for GradStore<R>

§

impl<R> Unpin for GradStore<R>

§

impl<R> UnsafeUnpin for GradStore<R>

§

impl<R> UnwindSafe for GradStore<R>

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> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
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.