Skip to main content

Module

Trait Module 

Source
pub trait Module<Input> {
    type Output;
    type Error;

    // Required method
    fn forward(&self, input: Input) -> Result<Self::Output, Self::Error>;
}
Expand description

A generic Neural Network Layer or Module. Capable of taking an input and returning an output or error.

Module is the fundamental building block of neural networks in Incin. Any layer, from a simple ReLU to an entire ResNet architecture, implements Module.

§Deriving Modules

While you can implement Module manually, the recommended approach is to use the #[module] attribute macro provided by incin-macros. This automatically derives typed visitors and structural boilerplate.

use incin::backend_authoring::VariableBackend;
use incin::prelude::*;

#[module]
pub struct MyLayer<B: VariableBackend> {
    weight: Param<s![128, 128], B>,
    bias: Param<s![128], B>,
}

impl<B: VariableBackend> Module<Tensor<s![1, 128], B>> for MyLayer<B> {
    type Output = Tensor<s![1, 128], B>;
    type Error = Error;

    fn forward(&self, x: Tensor<s![1, 128], B>) -> Result<Self::Output> {
        // Custom logic here
        Ok(x)
    }
}

Required Associated Types§

Source

type Output

The output tensor type produced by this module’s forward pass.

Source

type Error

The error type returned if the forward pass fails.

Required Methods§

Source

fn forward(&self, input: Input) -> Result<Self::Output, Self::Error>

Runs the forward pass of this module on the given input.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<B, K, Train, G> Module<Tensor<Dyn, B, K, G>> for Linear<Dyn, B, False, K, Train>

Source§

type Output = Tensor<Dyn, B, K, <G as GradJoin<<Train as TrainState>::TensorGrad>>::Output>

Source§

type Error = Error

Source§

impl<B, K, Train, G> Module<Tensor<Dyn, B, K, G>> for Linear<Dyn, B, True, K, Train>

Source§

type Output = Tensor<Dyn, B, K, <G as GradJoin<<Train as TrainState>::TensorGrad>>::Output>

Source§

type Error = Error

Source§

impl<B, K, Train> Module<Tensor<Dyn, B, K>> for Linear<Dyn, B, Dyn, K, Train>

Source§

impl<I, HOut, WOut, B> Module<Tensor<I, B>> for AdaptiveAvgPool2d<HOut, WOut>

Source§

impl<I, K, S, P, D, B> Module<Tensor<I, B>> for AvgPool2d<K, S, P, D>

Source§

type Output = Tensor<<I as Pool2dShape<K, S, P, D>>::Output, B>

Source§

type Error = Error

Source§

impl<I, K, S, P, D, B> Module<Tensor<I, B>> for MaxPool2d<K, S, P, D>

Source§

type Output = Tensor<<I as Pool2dShape<K, S, P, D>>::Output, B>

Source§

type Error = Error

Source§

impl<I, L1, L2> Module<I> for Sequential<L1, L2>
where L1: Module<I>, L2: Module<<L1 as Module<I>>::Output, Error = <L1 as Module<I>>::Error>,

Source§

type Output = <L2 as Module<<L1 as Module<I>>::Output>>::Output

Source§

type Error = <L1 as Module<I>>::Error

Source§

impl<I, S, B, COut, CIn, K, Train> Module<Tensor<I, B, K>> for Conv1d<S, B, Dyn, K, Train>
where COut: Dim, CIn: Dim, K: DType, Train: TrainState, S: Conv1dShape<OutC = COut, InC = CIn>, I: Shape + DynShape + SpatialConv1d<COut, <S as Conv1dShape>::K, <S as Conv1dShape>::S, <S as Conv1dShape>::P, <S as Conv1dShape>::D> + HasChannels1D<CIn>, B: VariableBackend + Execute<Conv1dExact> + Execute<ReshapeExact>, <B as Execute<Conv1dExact>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<ReshapeExact>>::Output: Into<<B as StorageBackend>::Storage<K>>,

Source§

type Output = Tensor<<I as SpatialConv1d<COut, <S as Conv1dShape>::K, <S as Conv1dShape>::S, <S as Conv1dShape>::P, <S as Conv1dShape>::D>>::Output, B, K>

Source§

type Error = Error

Source§

impl<I, S, B, COut, CIn, K, Train> Module<Tensor<I, B, K>> for Conv1d<S, B, False, K, Train>
where COut: Dim, CIn: Dim, K: DType, Train: TrainState, S: Conv1dShape<OutC = COut, InC = CIn>, I: Shape + DynShape + SpatialConv1d<COut, <S as Conv1dShape>::K, <S as Conv1dShape>::S, <S as Conv1dShape>::P, <S as Conv1dShape>::D> + HasChannels1D<CIn>, B: VariableBackend + Execute<Conv1dExact> + Execute<ReshapeExact>, <B as Execute<Conv1dExact>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<ReshapeExact>>::Output: Into<<B as StorageBackend>::Storage<K>>,

Source§

type Output = Tensor<<I as SpatialConv1d<COut, <S as Conv1dShape>::K, <S as Conv1dShape>::S, <S as Conv1dShape>::P, <S as Conv1dShape>::D>>::Output, B, K>

Source§

type Error = Error

Source§

impl<I, S, B, COut, CIn, K, Train> Module<Tensor<I, B, K>> for Conv1d<S, B, True, K, Train>
where COut: Dim, CIn: Dim, K: DType, Train: TrainState, S: Conv1dShape<OutC = COut, InC = CIn>, I: Shape + DynShape + SpatialConv1d<COut, <S as Conv1dShape>::K, <S as Conv1dShape>::S, <S as Conv1dShape>::P, <S as Conv1dShape>::D> + HasChannels1D<CIn>, B: VariableBackend + Execute<Conv1dExact> + Execute<ReshapeExact>, <B as Execute<Conv1dExact>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<ReshapeExact>>::Output: Into<<B as StorageBackend>::Storage<K>>,

Source§

type Output = Tensor<<I as SpatialConv1d<COut, <S as Conv1dShape>::K, <S as Conv1dShape>::S, <S as Conv1dShape>::P, <S as Conv1dShape>::D>>::Output, B, K>

Source§

type Error = Error

Source§

impl<I, S, B, COut, CIn, K, Train> Module<Tensor<I, B, K>> for Conv2d<S, B, Dyn, K, Train>
where COut: Dim, CIn: Dim, K: DType, Train: TrainState, S: Conv2dShape<OutC = COut, InC = CIn>, I: Shape + DynShape + SpatialConv2d<COut, <S as Conv2dShape>::K, <S as Conv2dShape>::S, <S as Conv2dShape>::P, <S as Conv2dShape>::D> + HasChannels2D<CIn>, B: VariableBackend + Execute<Conv2dExact> + Execute<ReshapeExact>, <B as Execute<Conv2dExact>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<ReshapeExact>>::Output: Into<<B as StorageBackend>::Storage<K>>,

Source§

type Output = Tensor<<I as SpatialConv2d<COut, <S as Conv2dShape>::K, <S as Conv2dShape>::S, <S as Conv2dShape>::P, <S as Conv2dShape>::D>>::Output, B, K>

Source§

type Error = Error

Source§

impl<I, S, B, COut, CIn, K, Train> Module<Tensor<I, B, K>> for Conv2d<S, B, False, K, Train>
where COut: Dim, CIn: Dim, K: DType, Train: TrainState, S: Conv2dShape<OutC = COut, InC = CIn>, I: Shape + DynShape + SpatialConv2d<COut, <S as Conv2dShape>::K, <S as Conv2dShape>::S, <S as Conv2dShape>::P, <S as Conv2dShape>::D> + HasChannels2D<CIn>, B: VariableBackend + Execute<Conv2dExact> + Execute<ReshapeExact>, <B as Execute<Conv2dExact>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<ReshapeExact>>::Output: Into<<B as StorageBackend>::Storage<K>>,

Source§

type Output = Tensor<<I as SpatialConv2d<COut, <S as Conv2dShape>::K, <S as Conv2dShape>::S, <S as Conv2dShape>::P, <S as Conv2dShape>::D>>::Output, B, K>

Source§

type Error = Error

Source§

impl<I, S, B, COut, CIn, K, Train> Module<Tensor<I, B, K>> for Conv2d<S, B, True, K, Train>
where COut: Dim, CIn: Dim, K: DType, Train: TrainState, S: Conv2dShape<OutC = COut, InC = CIn>, I: Shape + DynShape + SpatialConv2d<COut, <S as Conv2dShape>::K, <S as Conv2dShape>::S, <S as Conv2dShape>::P, <S as Conv2dShape>::D> + HasChannels2D<CIn>, B: VariableBackend + Execute<Conv2dExact> + Execute<ReshapeExact>, <B as Execute<Conv2dExact>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<ReshapeExact>>::Output: Into<<B as StorageBackend>::Storage<K>>,

Source§

type Output = Tensor<<I as SpatialConv2d<COut, <S as Conv2dShape>::K, <S as Conv2dShape>::S, <S as Conv2dShape>::P, <S as Conv2dShape>::D>>::Output, B, K>

Source§

type Error = Error

Source§

impl<In, Out, Batch, B, BiasIh, BiasHh, K, Train> Module<(Tensor<DimCons<Batch, DimCons<In, Nil>>, B, K>, (Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>, Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>))> for LSTMCell<DimCons<In, DimCons<Out, Nil>>, B, BiasIh, BiasHh, K, Train>
where In: Dim, Out: Dim, Batch: Dim, B: VariableBackend + Execute<Add> + Execute<Mul> + Execute<Sigmoid> + Execute<Tanh>, BiasIh: OptionalField, BiasHh: OptionalField, K: DType, Train: TrainState, Linear<DimCons<In, DimCons<Out, Nil>>, B, BiasIh, K, Train>: Module<Tensor<DimCons<Batch, DimCons<In, Nil>>, B, K>, Output = Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>, Error = Error>, Linear<DimCons<Out, DimCons<Out, Nil>>, B, BiasHh, K, Train>: Module<Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>, Output = Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>, Error = Error>, <B as Execute<Add>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<Mul>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<Sigmoid>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<Tanh>>::Output: Into<<B as StorageBackend>::Storage<K>>,

Source§

type Output = (Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>, Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>)

Source§

type Error = Error

Source§

impl<In, Out, Batch, Seq, B, BiasIh, BiasHh, K, Train> Module<(Tensor<DimCons<Batch, DimCons<Seq, DimCons<In, Nil>>>, B, K>, (Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>, Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>))> for LSTM<DimCons<In, DimCons<Out, Nil>>, B, BiasIh, BiasHh, K, Train>
where In: Dim<Arg = ()>, Out: Dim<Arg = ()>, Batch: Dim<Arg = ()>, Seq: Dim<Arg = ()>, B: VariableBackend + Execute<StackExact> + Execute<Narrow> + Execute<SqueezeExact> + Capabilities, BiasIh: OptionalField, BiasHh: OptionalField, K: DType, Train: TrainState, <B as Execute<StackExact>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<Narrow>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<SqueezeExact>>::Output: Into<<B as StorageBackend>::Storage<K>>, LSTMCell<DimCons<In, DimCons<Out, Nil>>, B, BiasIh, BiasHh, K, Train>: Module<(Tensor<DimCons<Batch, DimCons<In, Nil>>, B, K>, (Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>, Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>)), Output = (Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>, Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>), Error = Error>,

Source§

type Output = (Tensor<DimCons<Batch, DimCons<Seq, DimCons<Out, Nil>>>, B, K>, (Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>, Tensor<DimCons<Batch, DimCons<Out, Nil>>, B, K>))

Source§

type Error = Error

Source§

impl<InF, OutF, InShape, B, K, Train, G> Module<Tensor<InShape, B, K, G>> for Linear<DimCons<InF, DimCons<OutF, Nil>>, B, False, K, Train>

Source§

type Output = Tensor<<InShape as ReplaceLastDim<OutF>>::Output, B, K, <G as GradJoin<<Train as TrainState>::TensorGrad>>::Output>

Source§

type Error = Error

Source§

impl<InF, OutF, InShape, B, K, Train, G> Module<Tensor<InShape, B, K, G>> for Linear<DimCons<InF, DimCons<OutF, Nil>>, B, True, K, Train>

Source§

type Output = Tensor<<InShape as ReplaceLastDim<OutF>>::Output, B, K, <G as GradJoin<<Train as TrainState>::TensorGrad>>::Output>

Source§

type Error = Error

Source§

impl<InF, OutF, InShape, B, K, Train> Module<Tensor<InShape, B, K>> for Linear<DimCons<InF, DimCons<OutF, Nil>>, B, Dyn, K, Train>

Source§

type Output = Tensor<<InShape as ReplaceLastDim<OutF>>::Output, B, K>

Source§

type Error = Error

Source§

impl<S, B, G> Module<Tensor<S, B, f32, G>> for ReLU

Source§

impl<S, B, K, G, Start, End> Module<Tensor<S, B, K, G>> for Flatten<Start, End>
where Start: Copy, End: Copy, S: Shape + DynShape, (): FlattenSelector<S, Start, End>, <() as FlattenSelector<S, Start, End>>::Output: Shape + DynShape, B: VariableBackend + Execute<FlattenExact, Output = <B as StorageBackend>::Storage<K>> + Capabilities, K: DType, G: RequiresGrad,

Source§

type Output = Tensor<<() as FlattenSelector<S, Start, End>>::Output, B, K, G>

Source§

type Error = Error

Source§

impl<S, B, K, G, Start, End> Module<Tensor<S, B, K, G>> for StructuralFlatten<Start, End>
where Start: StaticCursor, End: StaticCursor, S: Shape + DynShape + FlattenAt<Start, End>, <S as FlattenAt<Start, End>>::Output: Shape + DynShape, B: VariableBackend + Execute<FlattenExact, Output = <B as StorageBackend>::Storage<K>> + Capabilities, K: DType, G: RequiresGrad,

Source§

type Output = Tensor<<S as FlattenAt<Start, End>>::Output, B, K, G>

Source§

type Error = Error

Source§

impl<S, B, K, G> Module<Tensor<S, B, K, G>> for Dropout

Source§

type Output = Tensor<S, B, K, G>

Source§

type Error = Error

Source§

impl<S, B, K, G> Module<Tensor<S, B, K, G>> for FlattenAxes

Source§

impl<S, B> Module<Tensor<S, B>> for ELU

Source§

impl<S, B> Module<Tensor<S, B>> for GELU

Source§

impl<S, B> Module<Tensor<S, B>> for Mish

Source§

impl<S, B> Module<Tensor<S, B>> for Sigmoid

Source§

impl<S, B> Module<Tensor<S, B>> for Softmax

Source§

impl<S, B> Module<Tensor<S, B>> for Swish

Source§

impl<S, B> Module<Tensor<S, B>> for Tanh

Source§

impl<S, Batch, B, BiasIh, BiasHh, K, Train, G> Module<(Tensor<DimCons<Batch, DimCons<<S as RnnShape>::In, Nil>>, B, K, G>, Tensor<DimCons<Batch, DimCons<<S as RnnShape>::Out, Nil>>, B, K, G>)> for RNNCell<S, B, BiasIh, BiasHh, K, Train>
where S: RnnShape, Batch: Dim, B: VariableBackend + Execute<Add> + Execute<Tanh>, BiasIh: OptionalField, BiasHh: OptionalField, K: DType, Train: TrainState, G: RequiresGrad + GradJoin<<Train as TrainState>::TensorGrad>, Linear<<S as RnnShape>::IhShape, B, BiasIh, K, Train>: Module<Tensor<DimCons<Batch, DimCons<<S as RnnShape>::In, Nil>>, B, K, G>, Output = Tensor<DimCons<Batch, DimCons<<S as RnnShape>::Out, Nil>>, B, K, <G as GradJoin<<Train as TrainState>::TensorGrad>>::Output>, Error = Error>, Linear<<S as RnnShape>::HhShape, B, BiasHh, K, Train>: Module<Tensor<DimCons<Batch, DimCons<<S as RnnShape>::Out, Nil>>, B, K, G>, Output = Tensor<DimCons<Batch, DimCons<<S as RnnShape>::Out, Nil>>, B, K, <G as GradJoin<<Train as TrainState>::TensorGrad>>::Output>, Error = Error>, <B as Execute<Add>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<Tanh>>::Output: Into<<B as StorageBackend>::Storage<K>>,

Source§

type Output = Tensor<DimCons<Batch, DimCons<<S as RnnShape>::Out, Nil>>, B, K, <G as GradJoin<<Train as TrainState>::TensorGrad>>::Output>

Source§

type Error = Error

Source§

impl<S, Batch, Seq, B, BiasIh, BiasHh, K, G, Train> Module<(Tensor<DimCons<Batch, DimCons<Seq, DimCons<<S as RnnShape>::In, Nil>>>, B, K, G>, Tensor<DimCons<Batch, DimCons<<S as RnnShape>::Out, Nil>>, B, K, G>)> for RNN<S, B, BiasIh, BiasHh, K, Train, G>
where S: RnnShape, Batch: Dim<Arg = ()>, Seq: Dim<Arg = ()>, B: VariableBackend + Execute<StackExact> + Execute<Narrow> + Execute<SqueezeExact> + Capabilities, BiasIh: OptionalField, BiasHh: OptionalField, K: ConstDType, G: RequiresGrad<Output = G> + GradJoin<<Train as TrainState>::TensorGrad>, Train: TrainState, <B as Execute<StackExact>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<Narrow>>::Output: Into<<B as StorageBackend>::Storage<K>>, <B as Execute<SqueezeExact>>::Output: Into<<B as StorageBackend>::Storage<K>>, <S as RnnShape>::In: Dim<Arg = ()>, <S as RnnShape>::Out: Dim<Arg = ()>, <B as StorageBackend>::Device: ConstDevice, RNNCell<S, B, BiasIh, BiasHh, K, Train>: Module<(Tensor<DimCons<Batch, DimCons<<S as RnnShape>::In, Nil>>, B, K, G>, Tensor<DimCons<Batch, DimCons<<S as RnnShape>::Out, Nil>>, B, K, G>), Output = Tensor<DimCons<Batch, DimCons<<S as RnnShape>::Out, Nil>>, B, K, G>, Error = Error>,

Source§

type Output = (Tensor<DimCons<Batch, DimCons<Seq, DimCons<<S as RnnShape>::Out, Nil>>>, B, K, G>, Tensor<DimCons<Batch, DimCons<<S as RnnShape>::Out, Nil>>, B, K, G>)

Source§

type Error = Error

Source§

impl<S, InS, B, InK, K, Train> Module<Tensor<InS, B, InK>> for Embedding<S, B, K, Train>

Source§

impl<S, InS, B, K, Train> Module<Tensor<InS, B, K>> for BatchNorm2d<S, B, K, Train>

Source§

type Output = Tensor<InS, B, K>

Source§

type Error = Error

Source§

impl<S, InS, B, K, Train> Module<Tensor<InS, B, K>> for LayerNorm<S, B, K, Train>

Source§

type Output = Tensor<InS, B, K>

Source§

type Error = Error

Source§

impl<S, InS, B, K, Train> Module<Tensor<InS, B, K>> for RMSNorm<S, B, K, Train>

Source§

type Output = Tensor<InS, B, K, <Train as TrainState>::TensorGrad>

Source§

type Error = Error