Skip to main content

incin_core/nn/
max_pool2d.rs

1use crate::backend_authoring::Execute;
2use crate::err::{Error, Result};
3use crate::exec::Capabilities;
4use crate::nn::{Module, TrainMode};
5use crate::shapes::{DynShape, Shape};
6use crate::tensor::base::Tensor;
7
8use typenum::Unsigned;
9
10#[derive(Debug, Clone)]
11/// `MaxPool2d`.
12pub struct MaxPool2d<K: Unsigned, S: Unsigned, P: Unsigned = typenum::U0, D: Unsigned = typenum::U1>
13{
14    _phantom: core::marker::PhantomData<(K, S, P, D)>,
15}
16
17impl<K: Unsigned, S: Unsigned, P: Unsigned, D: Unsigned, B: crate::tensor::backend::VariableBackend>
18    crate::nn::VisitParameters<B> for MaxPool2d<K, S, P, D>
19{
20    fn visit_parameters<V: crate::nn::ParameterVisitor<B>>(
21        &self,
22        _: &crate::nn::StatePath,
23        _: &mut V,
24    ) -> Result<()> {
25        Ok(())
26    }
27}
28
29impl<K: Unsigned, S: Unsigned, P: Unsigned, D: Unsigned> MaxPool2d<K, S, P, D> {
30    /// Creates a new instance with default (statically inferred) shape arguments.
31    pub fn new() -> Result<Self> {
32        Ok(Self {
33            _phantom: core::marker::PhantomData,
34        })
35    }
36}
37
38/// Stateless - no training-dependent behavior, opts in with the trait's
39/// default no-op so it can appear inside a `Sequential` alongside layers
40/// that do have one (e.g. `Dropout`).
41impl<K: Unsigned, S: Unsigned, P: Unsigned, D: Unsigned> TrainMode for MaxPool2d<K, S, P, D> {}
42
43impl<
44    I: Shape + DynShape + crate::shapes::Pool2dShape<K, S, P, D>,
45    K: Unsigned,
46    S: Unsigned,
47    P: Unsigned,
48    D: Unsigned,
49    B: crate::tensor::backend::VariableBackend + Execute<crate::exec::catalog::op::MaxPool2d>,
50> Module<Tensor<I, B>> for MaxPool2d<K, S, P, D>
51where
52    B: Capabilities,
53    <B as Execute<crate::exec::catalog::op::MaxPool2d>>::Output: Into<B::Storage<f32>>,
54{
55    /// The output tensor type produced by this module's forward pass.
56    type Output = Tensor<I::Output, B>;
57    /// The error type returned if the forward pass fails.
58    type Error = Error;
59
60    #[inline]
61    /// Runs the forward pass of this module on the given input.
62    fn forward(&self, x: Tensor<I, B>) -> core::result::Result<Self::Output, Error> {
63        x.max_pool2d::<K, S, P, D>()
64    }
65}