pub trait AutodiffBackend: Backend {
    type InnerBackend: Backend<Device = Self::Device, FloatElem = Self::FloatElem, IntElem = Self::IntElem, FullPrecisionElem = Self::FullPrecisionElem>;
    type Gradients: Send + Sync;

    // Required methods
    fn backward<const D: usize>(
        tensor: Self::TensorPrimitive<D>
    ) -> Self::Gradients;
    fn grad<const D: usize>(
        tensor: &Self::TensorPrimitive<D>,
        grads: &Self::Gradients
    ) -> Option<<Self::InnerBackend as Backend>::TensorPrimitive<D>>;
    fn grad_remove<const D: usize>(
        tensor: &Self::TensorPrimitive<D>,
        grads: &mut Self::Gradients
    ) -> Option<<Self::InnerBackend as Backend>::TensorPrimitive<D>>;
    fn grad_replace<const D: usize>(
        tensor: &Self::TensorPrimitive<D>,
        grads: &mut Self::Gradients,
        grad: <Self::InnerBackend as Backend>::TensorPrimitive<D>
    );
    fn inner<const D: usize>(
        tensor: Self::TensorPrimitive<D>
    ) -> <Self::InnerBackend as Backend>::TensorPrimitive<D>;
    fn int_inner<const D: usize>(
        tensor: Self::IntTensorPrimitive<D>
    ) -> <Self::InnerBackend as Backend>::IntTensorPrimitive<D>;
    fn bool_inner<const D: usize>(
        tensor: Self::BoolTensorPrimitive<D>
    ) -> <Self::InnerBackend as Backend>::BoolTensorPrimitive<D>;
    fn from_inner<const D: usize>(
        tensor: <Self::InnerBackend as Backend>::TensorPrimitive<D>
    ) -> Self::TensorPrimitive<D>;
    fn int_from_inner<const D: usize>(
        tensor: <Self::InnerBackend as Backend>::IntTensorPrimitive<D>
    ) -> Self::IntTensorPrimitive<D>;
    fn bool_from_inner<const D: usize>(
        tensor: <Self::InnerBackend as Backend>::BoolTensorPrimitive<D>
    ) -> Self::BoolTensorPrimitive<D>;
}
Expand description

Trait that allows a backend to support autodiff.

Required Associated Types§

source

type InnerBackend: Backend<Device = Self::Device, FloatElem = Self::FloatElem, IntElem = Self::IntElem, FullPrecisionElem = Self::FullPrecisionElem>

The inner backend type.

source

type Gradients: Send + Sync

Gradients type.

Required Methods§

source

fn backward<const D: usize>(tensor: Self::TensorPrimitive<D>) -> Self::Gradients

Backward pass.

Arguments
  • tensor - The tensor is the last node of computational graph where the gradients are computed.
Returns

The gradients.

source

fn grad<const D: usize>( tensor: &Self::TensorPrimitive<D>, grads: &Self::Gradients ) -> Option<<Self::InnerBackend as Backend>::TensorPrimitive<D>>

Returns the gradients of a tensor.

Arguments
  • tensor - The tensor to extract the gradients from.
Returns

An optional tensor containing the gradient.

source

fn grad_remove<const D: usize>( tensor: &Self::TensorPrimitive<D>, grads: &mut Self::Gradients ) -> Option<<Self::InnerBackend as Backend>::TensorPrimitive<D>>

Pops the gradients of a tensor and returns them.

Arguments
  • tensor - The tensor to pop the gradients from.
  • grads - The gradients.
Returns

An optional tensor containing the given gradients.

source

fn grad_replace<const D: usize>( tensor: &Self::TensorPrimitive<D>, grads: &mut Self::Gradients, grad: <Self::InnerBackend as Backend>::TensorPrimitive<D> )

Replace the gradients of a tensor with the one provided.

If no gradient existed for the provided tensor, register it.

Arguments
  • tensor - The tensor to pop the gradients from.
  • grads - The gradients.
  • grad - The updated grad tensor.
source

fn inner<const D: usize>( tensor: Self::TensorPrimitive<D> ) -> <Self::InnerBackend as Backend>::TensorPrimitive<D>

Returns the tensor with inner backend type.

Arguments
  • tensor - The tensor to get the inner backend tensor for.
Returns

The inner backend tensor.

source

fn int_inner<const D: usize>( tensor: Self::IntTensorPrimitive<D> ) -> <Self::InnerBackend as Backend>::IntTensorPrimitive<D>

Returns the tensor with inner backend type.

Arguments
  • tensor - The tensor to get the inner backend tensor for.
Returns

The inner backend tensor.

source

fn bool_inner<const D: usize>( tensor: Self::BoolTensorPrimitive<D> ) -> <Self::InnerBackend as Backend>::BoolTensorPrimitive<D>

Returns the tensor with inner backend type.

Arguments
  • tensor - The tensor to get the inner backend tensor for.
Returns

The inner backend tensor.

source

fn from_inner<const D: usize>( tensor: <Self::InnerBackend as Backend>::TensorPrimitive<D> ) -> Self::TensorPrimitive<D>

Converts the inner backend tensor to the autodiff backend tensor.

Arguments
  • tensor - The inner backend tensor to convert.
Returns

The autodiff backend tensor.

source

fn int_from_inner<const D: usize>( tensor: <Self::InnerBackend as Backend>::IntTensorPrimitive<D> ) -> Self::IntTensorPrimitive<D>

Converts the inner backend tensor to the autodiff backend tensor.

Arguments
  • tensor - The inner backend tensor to convert.
Returns

The autodiff backend tensor.

source

fn bool_from_inner<const D: usize>( tensor: <Self::InnerBackend as Backend>::BoolTensorPrimitive<D> ) -> Self::BoolTensorPrimitive<D>

Converts the inner backend tensor to the autodiff backend tensor.

Arguments
  • tensor - The inner backend tensor to convert.
Returns

The autodiff backend tensor.

Object Safety§

This trait is not object safe.

Implementors§