1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! Fully connected (linear) layer.
//!
//! Implements the transformation y = xW^T + b.
//!
//! # References
//!
//! - Glorot, X., & Bengio, Y. (2010). Understanding the difficulty of training
//! deep feedforward neural networks. AISTATS.
use super::init::{xavier_uniform, zeros};
use super::module::Module;
use crate::autograd::Tensor;
/// Fully connected layer: y = xW^T + b
///
/// Applies a linear transformation to the incoming data.
/// Weight initialization follows Xavier/Glorot (Glorot & Bengio, 2010).
///
/// # Shape
///
/// - Input: `(*, in_features)` where `*` means any number of batch dimensions
/// - Output: `(*, out_features)`
///
/// # Example
///
/// ```ignore
/// use aprender::nn::{Module, Linear};
/// use aprender::autograd::Tensor;
///
/// let layer = Linear::new(20, 30); // 20 inputs, 30 outputs
/// let x = Tensor::randn(&[128, 20]); // batch of 128
/// let output = layer.forward(&x); // [128, 30]
///
/// assert_eq!(output.shape(), &[128, 30]);
/// ```
pub struct Linear {
/// Weight matrix, shape: [`out_features`, `in_features`]
weight: Tensor,
/// Cached transposed weight [`in_features`, `out_features`] for fast forward
/// Computed once when weight is set, avoids transpose overhead every forward.
weight_t: Option<Tensor>,
/// Bias vector, shape: [`out_features`], or None if bias=false
bias: Option<Tensor>,
/// Number of input features
in_features: usize,
/// Number of output features
out_features: usize,
}
impl Linear {
/// Create a new Linear layer with Xavier initialization.
///
/// # Arguments
///
/// * `in_features` - Number of input features
/// * `out_features` - Number of output features
///
/// # Example
///
/// ```ignore
/// let layer = Linear::new(784, 256);
/// ```
#[must_use]
pub fn new(in_features: usize, out_features: usize) -> Self {
Self::with_seed(in_features, out_features, None)
}
/// Create a Linear layer with a specific random seed.
#[must_use]
pub fn with_seed(in_features: usize, out_features: usize, seed: Option<u64>) -> Self {
let weight = xavier_uniform(
&[out_features, in_features],
in_features,
out_features,
seed,
)
.requires_grad();
let weight_t = Some(weight.transpose());
let bias = zeros(&[out_features]).requires_grad();
Self {
weight,
weight_t,
bias: Some(bias),
in_features,
out_features,
}
}
/// Create a Linear layer without bias.
///
/// Useful when followed by `BatchNorm` which has its own bias.
#[must_use]
pub fn without_bias(in_features: usize, out_features: usize) -> Self {
Self::without_bias_with_seed(in_features, out_features, None)
}
/// Create a Linear layer without bias with a specific random seed.
#[must_use]
pub fn without_bias_with_seed(
in_features: usize,
out_features: usize,
seed: Option<u64>,
) -> Self {
let weight = xavier_uniform(
&[out_features, in_features],
in_features,
out_features,
seed,
)
.requires_grad();
let weight_t = Some(weight.transpose());
Self {
weight,
weight_t,
bias: None,
in_features,
out_features,
}
}
/// Get the input feature dimension.
#[must_use]
pub fn in_features(&self) -> usize {
self.in_features
}
/// Get the output feature dimension.
#[must_use]
pub fn out_features(&self) -> usize {
self.out_features
}
/// Check if this layer has a bias term.
#[must_use]
pub fn has_bias(&self) -> bool {
self.bias.is_some()
}
/// Set weight tensor from external data.
///
/// Used for loading pre-trained weights from `SafeTensors` or other formats.
/// Automatically computes and caches the transposed weight for fast forward.
pub fn set_weight(&mut self, weight: Tensor) {
// Pre-compute transpose once during loading (not every forward pass)
self.weight_t = Some(weight.transpose());
self.weight = weight;
}
/// Set bias tensor from external data.
///
/// Used for loading pre-trained weights.
pub fn set_bias(&mut self, bias: Tensor) {
self.bias = Some(bias);
}
/// Create a placeholder Linear layer with minimal memory allocation.
///
/// Used for lazy initialization when loading pre-trained weights.
/// The placeholder uses 1-element tensors instead of full matrices,
/// reducing memory from O(in*out) to O(1).
///
/// **IMPORTANT**: This layer will NOT work for inference until
/// `set_weight()` is called with real weights.
#[must_use]
pub fn placeholder(in_features: usize, out_features: usize) -> Self {
// Use 1-element placeholder tensors to save memory
Self {
weight: Tensor::new(&[0.0], &[1]),
weight_t: None, // Will be set when set_weight() is called
bias: None,
in_features,
out_features,
}
}
/// Get reference to weight tensor.
#[must_use]
pub fn weight(&self) -> &Tensor {
&self.weight
}
/// Get reference to bias tensor if present.
#[must_use]
pub fn bias(&self) -> Option<&Tensor> {
self.bias.as_ref()
}
/// Check if this layer is ready for inference (`weight_t` is cached).
///
/// Returns false for placeholder layers that haven't had `set_weight()` called.
/// This is useful for verifying all layers are properly initialized before forward.
#[must_use]
pub fn is_ready(&self) -> bool {
self.weight_t.is_some()
}
}
impl Module for Linear {
fn forward(&self, input: &Tensor) -> Tensor {
// y = x @ W^T + b
// Input: [*, in_features] where * is any number of batch dimensions
// Weight: [out_features, in_features]
// Output: [*, out_features]
let input_shape = input.shape();
let ndim = input_shape.len();
// Handle N-dimensional input by flattening batch dimensions
let (reshaped, batch_shape) = if ndim > 2 {
// Flatten all but last dimension
let batch_size: usize = input_shape[..ndim - 1].iter().product();
let in_features = input_shape[ndim - 1];
let batch_shape: Vec<usize> = input_shape[..ndim - 1].to_vec();
(input.view(&[batch_size, in_features]), Some(batch_shape))
} else {
(input.clone(), None)
};
// Use cached transposed weight (computed once during set_weight, not every forward)
// This eliminates ~450M element copies per forward pass for Qwen2-0.5B
let weight_t = self.weight_t.as_ref().unwrap_or_else(|| {
panic!("Linear layer has no cached weight_t. Call set_weight() first or use new().");
});
let output = reshaped.matmul(weight_t);
// Add bias with autograd
let output = match &self.bias {
Some(b) => output.broadcast_add(b),
None => output,
};
// Reshape back to original batch dimensions
match batch_shape {
Some(mut shape) => {
shape.push(self.out_features);
output.view(&shape)
}
None => output,
}
}
fn parameters(&self) -> Vec<&Tensor> {
match &self.bias {
Some(b) => vec![&self.weight, b],
None => vec![&self.weight],
}
}
fn parameters_mut(&mut self) -> Vec<&mut Tensor> {
match &mut self.bias {
Some(b) => vec![&mut self.weight, b],
None => vec![&mut self.weight],
}
}
fn refresh_caches(&mut self) {
// Recompute cached transposed weight after parameters were modified
self.weight_t = Some(self.weight.transpose());
}
}
impl std::fmt::Debug for Linear {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Linear")
.field("in_features", &self.in_features)
.field("out_features", &self.out_features)
.field("bias", &self.bias.is_some())
.finish_non_exhaustive()
}
}
#[cfg(test)]
#[path = "linear_tests.rs"]
mod tests;
#[cfg(test)]
#[path = "tests_linear_contract.rs"]
mod tests_linear_contract;