Trait burn::tensor::backend::AutodiffBackend

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

    // Required methods
    fn backward<const D: usize>(
        tensor: Self::FloatTensorPrimitive<D>,
    ) -> Self::Gradients;
    fn grad<const D: usize>(
        tensor: &Self::FloatTensorPrimitive<D>,
        grads: &Self::Gradients,
    ) -> Option<<Self::InnerBackend as Backend>::FloatTensorPrimitive<D>>;
    fn grad_remove<const D: usize>(
        tensor: &Self::FloatTensorPrimitive<D>,
        grads: &mut Self::Gradients,
    ) -> Option<<Self::InnerBackend as Backend>::FloatTensorPrimitive<D>>;
    fn grad_replace<const D: usize>(
        tensor: &Self::FloatTensorPrimitive<D>,
        grads: &mut Self::Gradients,
        grad: <Self::InnerBackend as Backend>::FloatTensorPrimitive<D>,
    );
    fn inner<const D: usize>(
        tensor: Self::FloatTensorPrimitive<D>,
    ) -> <Self::InnerBackend as Backend>::FloatTensorPrimitive<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 q_inner<const D: usize>(
        tensor: Self::QuantizedTensorPrimitive<D>,
    ) -> <Self::InnerBackend as Backend>::QuantizedTensorPrimitive<D>;
    fn from_inner<const D: usize>(
        tensor: <Self::InnerBackend as Backend>::FloatTensorPrimitive<D>,
    ) -> Self::FloatTensorPrimitive<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>;
    fn q_from_inner<const D: usize>(
        tensor: <Self::InnerBackend as Backend>::QuantizedTensorPrimitive<D>,
    ) -> Self::QuantizedTensorPrimitive<D>;
}
Expand description

Trait that allows a backend to support autodiff.

Required Associated Types§

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

The inner backend type.

type Gradients: Send

Gradients type.

Required Methods§

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

Backward pass.

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

The gradients.

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

Returns the gradients of a tensor.

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

An optional tensor containing the gradient.

fn grad_remove<const D: usize>( tensor: &Self::FloatTensorPrimitive<D>, grads: &mut Self::Gradients, ) -> Option<<Self::InnerBackend as Backend>::FloatTensorPrimitive<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.

fn grad_replace<const D: usize>( tensor: &Self::FloatTensorPrimitive<D>, grads: &mut Self::Gradients, grad: <Self::InnerBackend as Backend>::FloatTensorPrimitive<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.

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

Returns the tensor with inner backend type.

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

The inner backend tensor.

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.

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.

fn q_inner<const D: usize>( tensor: Self::QuantizedTensorPrimitive<D>, ) -> <Self::InnerBackend as Backend>::QuantizedTensorPrimitive<D>

Returns the tensor with inner backend type.

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

The inner backend tensor.

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

Converts the inner backend tensor to the autodiff backend tensor.

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

The autodiff backend tensor.

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.

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.

fn q_from_inner<const D: usize>( tensor: <Self::InnerBackend as Backend>::QuantizedTensorPrimitive<D>, ) -> Self::QuantizedTensorPrimitive<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§