aprender-core 0.29.3

Next-generation machine learning library in pure Rust
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Container modules for composing neural networks.
//!
//! These containers allow building complex networks from simpler modules.

use super::module::Module;
use crate::autograd::Tensor;
use std::collections::HashMap;

/// Sequential container for chaining modules.
///
/// Modules are executed in order, with each module's output
/// becoming the next module's input.
///
/// # Example
///
/// ```ignore
/// use aprender::nn::{Sequential, Linear, ReLU, Dropout};
///
/// let model = Sequential::new()
///     .add(Linear::new(784, 256))
///     .add(ReLU::new())
///     .add(Dropout::new(0.5))
///     .add(Linear::new(256, 10));
///
/// let x = Tensor::randn(&[32, 784]);
/// let output = model.forward(&x);  // [32, 10]
/// ```
pub struct Sequential {
    modules: Vec<Box<dyn Module>>,
    training: bool,
}

impl Sequential {
    /// Create an empty Sequential container.
    #[must_use]
    pub fn new() -> Self {
        Self {
            modules: Vec::new(),
            training: true,
        }
    }

    /// Add a module to the sequence.
    ///
    /// Returns self for method chaining.
    #[allow(clippy::should_implement_trait)]
    pub fn add<M: Module + 'static>(mut self, module: M) -> Self {
        self.modules.push(Box::new(module));
        self
    }

    /// Add a module by boxed trait object.
    #[must_use]
    pub fn add_boxed(mut self, module: Box<dyn Module>) -> Self {
        self.modules.push(module);
        self
    }

    /// Get the number of modules.
    #[must_use]
    pub fn len(&self) -> usize {
        self.modules.len()
    }

    /// Check if the container is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.modules.is_empty()
    }
}

impl Default for Sequential {
    fn default() -> Self {
        Self::new()
    }
}

impl Module for Sequential {
    fn forward(&self, input: &Tensor) -> Tensor {
        self.modules
            .iter()
            .fold(input.clone(), |x, module| module.forward(&x))
    }

    fn parameters(&self) -> Vec<&Tensor> {
        self.modules.iter().flat_map(|m| m.parameters()).collect()
    }

    fn parameters_mut(&mut self) -> Vec<&mut Tensor> {
        self.modules
            .iter_mut()
            .flat_map(|m| m.parameters_mut())
            .collect()
    }

    fn train(&mut self) {
        self.training = true;
        for module in &mut self.modules {
            module.train();
        }
    }

    fn eval(&mut self) {
        self.training = false;
        for module in &mut self.modules {
            module.eval();
        }
    }

    fn training(&self) -> bool {
        self.training
    }
}

impl std::fmt::Debug for Sequential {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Sequential")
            .field("num_modules", &self.modules.len())
            .field("training", &self.training)
            .finish()
    }
}

/// List of modules with index-based access.
///
/// Unlike Sequential, `ModuleList` doesn't define a forward pass.
/// It's useful for holding submodules that need custom control flow.
///
/// # Example
///
/// ```ignore
/// use aprender::nn::{ModuleList, Linear, Module};
///
/// let layers = ModuleList::new()
///     .add(Linear::new(10, 10))
///     .add(Linear::new(10, 10))
///     .add(Linear::new(10, 10));
///
/// // Custom forward with residual connections
/// fn forward(layers: &ModuleList, x: &Tensor) -> Tensor {
///     let mut out = x.clone();
///     for i in 0..layers.len() {
///         let residual = out.clone();
///         out = layers.get(i).expect("layer index in bounds").forward(&out);
///         out = out.add(&residual);  // residual connection
///     }
///     out
/// }
/// ```
pub struct ModuleList {
    modules: Vec<Box<dyn Module>>,
    training: bool,
}

impl ModuleList {
    /// Create an empty `ModuleList`.
    #[must_use]
    pub fn new() -> Self {
        Self {
            modules: Vec::new(),
            training: true,
        }
    }

    /// Add a module to the list.
    #[allow(clippy::should_implement_trait)]
    pub fn add<M: Module + 'static>(mut self, module: M) -> Self {
        self.modules.push(Box::new(module));
        self
    }

    /// Add a boxed module to the list.
    #[must_use]
    pub fn add_boxed(mut self, module: Box<dyn Module>) -> Self {
        self.modules.push(module);
        self
    }

    /// Get a module by index.
    pub fn get(&self, index: usize) -> Option<&dyn Module> {
        self.modules.get(index).map(AsRef::as_ref)
    }

    /// Get a mutable module by index.
    pub fn get_mut(&mut self, index: usize) -> Option<&mut Box<dyn Module>> {
        self.modules.get_mut(index)
    }

    /// Get the number of modules.
    #[must_use]
    pub fn len(&self) -> usize {
        self.modules.len()
    }

    /// Check if the list is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.modules.is_empty()
    }

    /// Iterate over modules.
    pub fn iter(&self) -> impl Iterator<Item = &dyn Module> {
        self.modules.iter().map(AsRef::as_ref)
    }
}

impl Default for ModuleList {
    fn default() -> Self {
        Self::new()
    }
}

// ModuleList doesn't implement forward - use get() for custom control flow
impl ModuleList {
    /// Get all parameters from all modules.
    #[must_use]
    pub fn parameters(&self) -> Vec<&Tensor> {
        self.modules.iter().flat_map(|m| m.parameters()).collect()
    }

    /// Get all mutable parameters.
    pub fn parameters_mut(&mut self) -> Vec<&mut Tensor> {
        self.modules
            .iter_mut()
            .flat_map(|m| m.parameters_mut())
            .collect()
    }

    /// Set all modules to training mode.
    pub fn train(&mut self) {
        self.training = true;
        for module in &mut self.modules {
            module.train();
        }
    }

    /// Set all modules to evaluation mode.
    pub fn eval(&mut self) {
        self.training = false;
        for module in &mut self.modules {
            module.eval();
        }
    }
}

impl std::fmt::Debug for ModuleList {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ModuleList")
            .field("num_modules", &self.modules.len())
            .field("training", &self.training)
            .finish()
    }
}

/// Dictionary of named modules with string-based access.
///
/// Useful for building networks with multiple branches or when you need
/// to access modules by name rather than index.
///
/// # Example
///
/// ```ignore
/// use aprender::nn::{ModuleDict, Linear, Module};
/// use aprender::autograd::Tensor;
///
/// let modules = ModuleDict::new()
///     .insert("encoder", Linear::new(784, 256))
///     .insert("decoder", Linear::new(256, 784))
///     .insert("classifier", Linear::new(256, 10));
///
/// let x = Tensor::randn(&[32, 784]);
/// let encoded = modules.get("encoder").expect("encoder module exists").forward(&x);
/// let decoded = modules.get("decoder").expect("decoder module exists").forward(&encoded);
/// let logits = modules.get("classifier").expect("classifier module exists").forward(&encoded);
/// ```
pub struct ModuleDict {
    modules: HashMap<String, Box<dyn Module>>,
    /// Insertion order for deterministic iteration
    keys: Vec<String>,
    training: bool,
}

impl ModuleDict {
    /// Create an empty `ModuleDict`.
    #[must_use]
    pub fn new() -> Self {
        Self {
            modules: HashMap::new(),
            keys: Vec::new(),
            training: true,
        }
    }

    /// Insert a module with the given name.
    ///
    /// If a module with the same name exists, it will be replaced.
    pub fn insert<S: Into<String>, M: Module + 'static>(mut self, name: S, module: M) -> Self {
        let name = name.into();
        if !self.modules.contains_key(&name) {
            self.keys.push(name.clone());
        }
        self.modules.insert(name, Box::new(module));
        self
    }

    /// Insert a boxed module.
    pub fn insert_boxed<S: Into<String>>(mut self, name: S, module: Box<dyn Module>) -> Self {
        let name = name.into();
        if !self.modules.contains_key(&name) {
            self.keys.push(name.clone());
        }
        self.modules.insert(name, module);
        self
    }

    /// Get a module by name.
    pub fn get(&self, name: &str) -> Option<&dyn Module> {
        self.modules.get(name).map(AsRef::as_ref)
    }

    /// Get a mutable module by name.
    pub fn get_mut(&mut self, name: &str) -> Option<&mut Box<dyn Module>> {
        self.modules.get_mut(name)
    }

    /// Check if a module with the given name exists.
    #[must_use]
    pub fn contains(&self, name: &str) -> bool {
        self.modules.contains_key(name)
    }

    /// Remove a module by name.
    pub fn remove(&mut self, name: &str) -> Option<Box<dyn Module>> {
        if let Some(module) = self.modules.remove(name) {
            self.keys.retain(|k| k != name);
            Some(module)
        } else {
            None
        }
    }

    /// Get the number of modules.
    #[must_use]
    pub fn len(&self) -> usize {
        self.modules.len()
    }

    /// Check if the dictionary is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.modules.is_empty()
    }

    /// Get all module names in insertion order.
    pub fn keys(&self) -> impl Iterator<Item = &str> {
        self.keys.iter().map(String::as_str)
    }

    /// Iterate over (name, module) pairs in insertion order.
    pub fn iter(&self) -> impl Iterator<Item = (&str, &dyn Module)> {
        self.keys.iter().map(|k| {
            let module = self
                .modules
                .get(k)
                .map(AsRef::as_ref)
                .expect("key must exist");
            (k.as_str(), module)
        })
    }

    /// Get all parameters from all modules.
    #[must_use]
    pub fn parameters(&self) -> Vec<&Tensor> {
        self.keys
            .iter()
            .filter_map(|k| self.modules.get(k))
            .flat_map(|m| m.parameters())
            .collect()
    }

    /// Get all mutable parameters.
    pub fn parameters_mut(&mut self) -> Vec<&mut Tensor> {
        self.modules
            .values_mut()
            .flat_map(|m| m.parameters_mut())
            .collect()
    }

    /// Set all modules to training mode.
    pub fn train(&mut self) {
        self.training = true;
        for module in self.modules.values_mut() {
            module.train();
        }
    }

    /// Set all modules to evaluation mode.
    pub fn eval(&mut self) {
        self.training = false;
        for module in self.modules.values_mut() {
            module.eval();
        }
    }

    /// Check if in training mode.
    #[must_use]
    pub fn training(&self) -> bool {
        self.training
    }
}

impl Default for ModuleDict {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for ModuleDict {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ModuleDict")
            .field("keys", &self.keys)
            .field("training", &self.training)
            .finish_non_exhaustive()
    }
}

#[cfg(test)]
#[path = "container_tests.rs"]
mod tests;