Skip to main content

incin_core/nn/
flatten.rs

1use crate::err::Result;
2use crate::nn::Module;
3use crate::shapes::Dyn;
4use crate::shapes::FlattenAt;
5use crate::shapes::idx::StaticCursor;
6use crate::shapes::{DynShape, Shape};
7use crate::tensor::base::Tensor;
8use crate::tensor::grad::RequiresGrad;
9use crate::tensor::ops::manipulation::FlattenSelector;
10use core::marker::PhantomData;
11
12/// Ergonomic flatten module driven by two axis selectors.
13#[derive(Debug, Clone, Copy, Default)]
14pub struct Flatten<Start, End> {
15    start: Start,
16    end: End,
17}
18
19impl<Start, End> Flatten<Start, End> {
20    /// Creates a flattening module for an inclusive axis range.
21    #[must_use]
22    pub const fn new(start: Start, end: End) -> Self {
23        Self { start, end }
24    }
25}
26
27/// Advanced structural flatten module for internal shape-proof code.
28#[derive(Debug, Clone, Copy, Default)]
29pub struct StructuralFlatten<Start, End>(PhantomData<fn() -> (Start, End)>);
30
31/// Runtime-axis flattening module for ordinary model code.
32///
33/// The inclusive axis range uses signed indices, so `FlattenAxes::new(1, -1)`
34/// flattens every dimension after a leading batch axis without exposing proof
35/// cursor types. The output shape is `Dyn` because the selected positions are
36/// runtime values.
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub struct FlattenAxes {
39    start: isize,
40    end: isize,
41}
42
43impl FlattenAxes {
44    /// Creates a checked runtime-axis flattening module.
45    #[must_use]
46    pub const fn new(start: isize, end: isize) -> Self {
47        Self { start, end }
48    }
49}
50
51impl<Start, End, B: crate::tensor::backend::VariableBackend> crate::nn::VisitParameters<B>
52    for Flatten<Start, End>
53{
54    fn visit_parameters<V: crate::nn::ParameterVisitor<B>>(
55        &self,
56        _: &crate::nn::StatePath,
57        _: &mut V,
58    ) -> Result<()> {
59        Ok(())
60    }
61}
62
63impl<B: crate::tensor::backend::VariableBackend> crate::nn::VisitParameters<B> for FlattenAxes {
64    fn visit_parameters<V: crate::nn::ParameterVisitor<B>>(
65        &self,
66        _: &crate::nn::StatePath,
67        _: &mut V,
68    ) -> Result<()> {
69        Ok(())
70    }
71}
72
73impl<S, B, K, G> Module<Tensor<S, B, K, G>> for FlattenAxes
74where
75    S: Shape + DynShape,
76    B: crate::tensor::backend::VariableBackend
77        + crate::backend_authoring::Execute<
78            crate::backend_authoring::op::FlattenExact,
79            Output = <B as crate::backend_authoring::StorageBackend>::Storage<K>,
80        > + crate::exec::Capabilities,
81    K: crate::tensor::dtype::DType,
82    G: RequiresGrad,
83{
84    type Output = Tensor<Dyn, B, K, G>;
85    type Error = crate::err::Error;
86
87    fn forward(&self, x: Tensor<S, B, K, G>) -> Result<Self::Output> {
88        x.flatten_runtime(self.start, self.end)
89    }
90}
91
92impl<S, B, K, G, Start: Copy, End: Copy> Module<Tensor<S, B, K, G>> for Flatten<Start, End>
93where
94    S: Shape + DynShape,
95    (): FlattenSelector<S, Start, End>,
96    <() as FlattenSelector<S, Start, End>>::Output: Shape + DynShape,
97    B: crate::tensor::backend::VariableBackend
98        + crate::backend_authoring::Execute<
99            crate::backend_authoring::op::FlattenExact,
100            Output = <B as crate::tensor::backend::StorageBackend>::Storage<K>,
101        > + crate::exec::Capabilities,
102    K: crate::tensor::dtype::DType,
103    G: RequiresGrad,
104{
105    type Output = Tensor<<() as FlattenSelector<S, Start, End>>::Output, B, K, G>;
106    type Error = crate::err::Error;
107
108    fn forward(&self, x: Tensor<S, B, K, G>) -> Result<Self::Output> {
109        x.flatten(self.start, self.end)
110    }
111}
112
113impl<S, B, K, G, Start, End> Module<Tensor<S, B, K, G>> for StructuralFlatten<Start, End>
114where
115    Start: StaticCursor,
116    End: StaticCursor,
117    S: Shape + DynShape + FlattenAt<Start, End>,
118    <S as FlattenAt<Start, End>>::Output: Shape + DynShape,
119    B: crate::tensor::backend::VariableBackend
120        + crate::backend_authoring::Execute<
121            crate::backend_authoring::op::FlattenExact,
122            Output = <B as crate::tensor::backend::StorageBackend>::Storage<K>,
123        > + crate::exec::Capabilities,
124    K: crate::tensor::dtype::DType,
125    G: RequiresGrad,
126{
127    type Output = Tensor<<S as FlattenAt<Start, End>>::Output, B, K, G>;
128    type Error = crate::err::Error;
129
130    fn forward(&self, x: Tensor<S, B, K, G>) -> Result<Self::Output> {
131        x.flatten_structural::<Start, End>()
132    }
133}