Skip to main content

AutodiffBackend

Trait AutodiffBackend 

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

    // Required methods
    fn backward(tensor: Self::FloatTensorPrimitive) -> Self::Gradients;
    fn grad(
        tensor: &Self::FloatTensorPrimitive,
        grads: &Self::Gradients,
    ) -> Option<<Self::InnerBackend as BackendTypes>::FloatTensorPrimitive>;
    fn grad_remove(
        tensor: &Self::FloatTensorPrimitive,
        grads: &mut Self::Gradients,
    ) -> Option<<Self::InnerBackend as BackendTypes>::FloatTensorPrimitive>;
    fn grad_replace(
        tensor: &Self::FloatTensorPrimitive,
        grads: &mut Self::Gradients,
        grad: <Self::InnerBackend as BackendTypes>::FloatTensorPrimitive,
    );
    fn inner(
        tensor: Self::FloatTensorPrimitive,
    ) -> <Self::InnerBackend as BackendTypes>::FloatTensorPrimitive;
    fn int_inner(
        tensor: Self::IntTensorPrimitive,
    ) -> <Self::InnerBackend as BackendTypes>::IntTensorPrimitive;
    fn bool_inner(
        tensor: Self::BoolTensorPrimitive,
    ) -> <Self::InnerBackend as BackendTypes>::BoolTensorPrimitive;
    fn q_inner(
        tensor: Self::QuantizedTensorPrimitive,
    ) -> <Self::InnerBackend as BackendTypes>::QuantizedTensorPrimitive;
    fn from_inner(
        tensor: <Self::InnerBackend as BackendTypes>::FloatTensorPrimitive,
    ) -> Self::FloatTensorPrimitive;
    fn int_from_inner(
        tensor: <Self::InnerBackend as BackendTypes>::IntTensorPrimitive,
    ) -> Self::IntTensorPrimitive;
    fn bool_from_inner(
        tensor: <Self::InnerBackend as BackendTypes>::BoolTensorPrimitive,
    ) -> Self::BoolTensorPrimitive;
    fn q_from_inner(
        tensor: <Self::InnerBackend as BackendTypes>::QuantizedTensorPrimitive,
    ) -> Self::QuantizedTensorPrimitive;
}
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>

The inner backend type.

Source

type Gradients: Send

Gradients type.

Required Methods§

Source

fn backward(tensor: Self::FloatTensorPrimitive) -> 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( tensor: &Self::FloatTensorPrimitive, grads: &Self::Gradients, ) -> Option<<Self::InnerBackend as BackendTypes>::FloatTensorPrimitive>

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( tensor: &Self::FloatTensorPrimitive, grads: &mut Self::Gradients, ) -> Option<<Self::InnerBackend as BackendTypes>::FloatTensorPrimitive>

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( tensor: &Self::FloatTensorPrimitive, grads: &mut Self::Gradients, grad: <Self::InnerBackend as BackendTypes>::FloatTensorPrimitive, )

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( tensor: Self::FloatTensorPrimitive, ) -> <Self::InnerBackend as BackendTypes>::FloatTensorPrimitive

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( tensor: Self::IntTensorPrimitive, ) -> <Self::InnerBackend as BackendTypes>::IntTensorPrimitive

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( tensor: Self::BoolTensorPrimitive, ) -> <Self::InnerBackend as BackendTypes>::BoolTensorPrimitive

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 q_inner( tensor: Self::QuantizedTensorPrimitive, ) -> <Self::InnerBackend as BackendTypes>::QuantizedTensorPrimitive

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( tensor: <Self::InnerBackend as BackendTypes>::FloatTensorPrimitive, ) -> Self::FloatTensorPrimitive

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( tensor: <Self::InnerBackend as BackendTypes>::IntTensorPrimitive, ) -> Self::IntTensorPrimitive

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( tensor: <Self::InnerBackend as BackendTypes>::BoolTensorPrimitive, ) -> Self::BoolTensorPrimitive

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 q_from_inner( tensor: <Self::InnerBackend as BackendTypes>::QuantizedTensorPrimitive, ) -> Self::QuantizedTensorPrimitive

Converts the inner backend tensor to the autodiff backend tensor.

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

The autodiff backend tensor.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<B, C> AutodiffBackend for Autodiff<B, C>

Available on non-crate feature distributed only.
Source§

type InnerBackend = B

Source§

type Gradients = Gradients

Source§

fn backward(tensor: AutodiffTensor<B>) -> Gradients

Source§

fn grad( tensor: &AutodiffTensor<B>, grads: &Gradients, ) -> Option<<B as BackendTypes>::FloatTensorPrimitive>

Source§

fn grad_remove( tensor: &AutodiffTensor<B>, grads: &mut Gradients, ) -> Option<<B as BackendTypes>::FloatTensorPrimitive>

Source§

fn inner(tensor: AutodiffTensor<B>) -> <B as BackendTypes>::FloatTensorPrimitive

Source§

fn from_inner( tensor: <B as BackendTypes>::FloatTensorPrimitive, ) -> AutodiffTensor<B>

Source§

fn grad_replace( tensor: &AutodiffTensor<B>, grads: &mut <Autodiff<B, C> as AutodiffBackend>::Gradients, grad: <B as BackendTypes>::FloatTensorPrimitive, )

Source§

fn int_inner( tensor: <Autodiff<B, C> as BackendTypes>::IntTensorPrimitive, ) -> <<Autodiff<B, C> as AutodiffBackend>::InnerBackend as BackendTypes>::IntTensorPrimitive

Source§

fn bool_inner( tensor: <Autodiff<B, C> as BackendTypes>::BoolTensorPrimitive, ) -> <<Autodiff<B, C> as AutodiffBackend>::InnerBackend as BackendTypes>::BoolTensorPrimitive

Source§

fn int_from_inner( tensor: <<Autodiff<B, C> as AutodiffBackend>::InnerBackend as BackendTypes>::IntTensorPrimitive, ) -> <Autodiff<B, C> as BackendTypes>::IntTensorPrimitive

Source§

fn bool_from_inner( tensor: <<Autodiff<B, C> as AutodiffBackend>::InnerBackend as BackendTypes>::BoolTensorPrimitive, ) -> <Autodiff<B, C> as BackendTypes>::BoolTensorPrimitive

Source§

fn q_inner( tensor: <Autodiff<B, C> as BackendTypes>::QuantizedTensorPrimitive, ) -> <<Autodiff<B, C> as AutodiffBackend>::InnerBackend as BackendTypes>::QuantizedTensorPrimitive

Source§

fn q_from_inner( tensor: <<Autodiff<B, C> as AutodiffBackend>::InnerBackend as BackendTypes>::QuantizedTensorPrimitive, ) -> <Autodiff<B, C> as BackendTypes>::QuantizedTensorPrimitive

Implementors§