teeny_core/nn/linear.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/// Fully-connected linear layer: `output = input × Wᵀ [+ b]`
25///
26/// Operates on the last dimension of an N-D tensor, passing all leading
27/// dimensions through unchanged (e.g. a batch dimension).
28///
29/// Type parameters:
30/// - `D` — element dtype
31/// - `IT` — input tensor type (any rank, last dim = `in_features`)
32/// - `OT` — output tensor type (same rank, last dim = `out_features`)
33/// - `RANK` — tensor rank (1 = unbatched, 2 = batched, etc.)
34///
35/// Tensor bounds are on impls, not the struct, so `SymTensor` can have its
36/// own `Layer` impl without a coherence conflict.
37pub struct Linear<D: Dtype, IT, OT, const RANK: usize> {
38 /// Size of the last input dimension.
39 pub in_features: usize,
40 /// Size of the last output dimension.
41 pub out_features: usize,
42 /// Whether to add a learned bias.
43 pub has_bias: bool,
44 _pd: PhantomData<(D, IT, OT)>,
45}
46
47impl<D: Dtype, IT, OT, const RANK: usize> Linear<D, IT, OT, RANK> {
48 /// Creates a new `Linear` layer.
49 pub fn new(in_features: usize, out_features: usize, has_bias: bool) -> Self {
50 Self {
51 in_features,
52 out_features,
53 has_bias,
54 _pd: PhantomData,
55 }
56 }
57}
58
59impl<D: Dtype, IT: Tensor<D, RANK> + EagerTensor, OT: Tensor<D, RANK>, const RANK: usize> Layer<IT>
60 for Linear<D, IT, OT, RANK>
61{
62 type Output = OT;
63
64 fn call(&self, _input: IT) -> Self::Output {
65 todo!()
66 }
67}