teeny_core/nn/flatten.rs
1/*
2 * Copyright (c) 2026 Teenygrad.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use core::marker::PhantomData;
18
19use crate::{
20 dtype::{Dtype, EagerTensor, Tensor},
21 nn::Layer,
22};
23
24/// Flatten layer: collapses all spatial dimensions into a single feature vector.
25///
26/// Input shape: `[N, C, H, W]` (rank 4)
27/// Output shape: `[N, C * H * W]` (rank 2)
28///
29/// Type parameters:
30/// - `D` — element dtype
31/// - `IT` — input tensor type (rank 4: `[N, C, H, W]`)
32/// - `OT` — output tensor type (rank 2: `[N, C*H*W]`)
33///
34/// Tensor bounds are on impls, not the struct, so `SymTensor` can have its
35/// own `Layer` impl without a coherence conflict.
36pub struct Flatten<D: Dtype, IT, OT> {
37 _pd: PhantomData<(D, IT, OT)>,
38}
39
40impl<D: Dtype, IT, OT> Flatten<D, IT, OT> {
41 /// Creates a new `Flatten` layer.
42 pub fn new() -> Self {
43 Self { _pd: PhantomData }
44 }
45}
46
47impl<D: Dtype, IT, OT> Default for Flatten<D, IT, OT> {
48 fn default() -> Self {
49 Self::new()
50 }
51}
52
53impl<D: Dtype, IT: Tensor<D, 4> + EagerTensor, OT: Tensor<D, 2>> Layer<IT> for Flatten<D, IT, OT> {
54 type Output = OT;
55
56 fn call(&self, _input: IT) -> Self::Output {
57 todo!()
58 }
59}