Skip to main content

teeny_core/nn/
conv1d.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/// 1-D convolution layer: `output = conv1d(input, weight) [+ b]`
25///
26/// Input shape:  `[N, C_in,  L_in ]`
27/// Output shape: `[N, C_out, L_out]`
28///
29/// where: `L_out = (L_in + 2*padding - kernel_l) / stride + 1`
30pub struct Conv1d<D: Dtype, IT, OT, const RANK: usize> {
31    /// Number of input channels.
32    pub in_channels: usize,
33    /// Number of output channels.
34    pub out_channels: usize,
35    /// Convolution kernel length.
36    pub kernel_l: usize,
37    /// Stride.
38    pub stride: usize,
39    /// Zero-padding applied to both sides of the input.
40    pub padding: usize,
41    /// Whether to add a learned bias.
42    pub has_bias: bool,
43    _pd: PhantomData<(D, IT, OT)>,
44}
45
46impl<D: Dtype, IT, OT, const RANK: usize> Conv1d<D, IT, OT, RANK> {
47    /// Creates a new `Conv1d` layer.
48    pub fn new(
49        in_channels: usize,
50        out_channels: usize,
51        kernel_size: usize,
52        stride: usize,
53        padding: usize,
54        has_bias: bool,
55    ) -> Self {
56        Self {
57            in_channels,
58            out_channels,
59            kernel_l: kernel_size,
60            stride,
61            padding,
62            has_bias,
63            _pd: PhantomData,
64        }
65    }
66}
67
68impl<D: Dtype, IT: Tensor<D, RANK> + EagerTensor, OT: Tensor<D, RANK>, const RANK: usize> Layer<IT>
69    for Conv1d<D, IT, OT, RANK>
70{
71    type Output = OT;
72    fn call(&self, _input: IT) -> Self::Output {
73        todo!()
74    }
75}