rusttorch 0.4.0

Build, train, and run neural networks in Rust with LibTorch
Documentation
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
//! Trainable layers and model composition for tensors.
//!
//! Build image feature extractors with [`ConvConfig`], token models with
//! [`EmbeddingConfig`] and [`LayerNormConfig`], and feed-forward networks with
//! [`Sequential`]. A [`VarStore`] holds parameters and persistent buffers for
//! custom models; [`Module`] lets their fallible forward passes share the same
//! interface. Use [`SequentialBuilder::layer`] to register additional modules.
//!
//! | Model task | Useful building blocks |
//! | --- | --- |
//! | Image features | [`Conv2d`], [`BatchNorm2d`], [`MaxPool2d`], [`AdaptiveAvgPool2d`] |
//! | Upsampling | [`ConvTransposeConfig`] |
//! | Time series | [`RnnConfig`], [`Rnn`], [`Gru`], [`Lstm`] |
//! | Token relationships | [`MultiheadAttentionConfig`], [`AttentionMask`] |
//! | Sequence-to-sequence learning | [`TransformerConfig`], [`TransformerMasks`] |
//! | Small-batch normalization | [`GroupNormConfig`], [`InstanceNormConfig`] |
//!
//! Pass `training` explicitly to mode-dependent layers. Recurrent APIs can
//! return hidden state for the next chunk; attention and Transformer APIs accept
//! masks for padding and causal prediction. Each configuration documents its
//! input layout and includes a runnable Rust example.
//!
//! ```
//! use rusttorch::{DeviceSpec, Kind, Tensor, nn::{ConvConfig, Sequential}};
//! let model = Sequential::builder()
//!     .conv2d(ConvConfig::new(3, 8, [3, 3]).padding([1, 1]))
//!     .relu()
//!     .flatten(1, -1)
//!     .linear(8 * 8 * 8, 10)
//!     .build(DeviceSpec::Cpu)?;
//! let images = Tensor::zeros([2, 3, 8, 8], (Kind::Float, model.device()));
//! assert_eq!(model.forward(&images)?.size(), [2, 10]);
//! # Ok::<(), rusttorch::RustTorchError>(())
//! ```

mod activations;
pub mod functional;
mod layers;
mod loss;
mod normalization;
mod sequence;
mod spatial;

pub use activations::*;
pub use normalization::*;
pub use sequence::*;
pub use spatial::*;

pub use layers::{
    Conv, Conv1d, Conv2d, Conv3d, ConvConfig, Embedding, EmbeddingConfig, LayerNorm,
    LayerNormConfig,
};

/// The parameter store shared by a model's layers and optimizer.
///
/// ```
/// let store = rusttorch::nn::VarStore::new(rusttorch::Device::Cpu);
/// let layer = rusttorch::nn::LinearConfig::new(4, 2).build(&store.root())?;
/// # Ok::<(), rusttorch::RustTorchError>(())
/// ```
pub use tch::nn::VarStore;

/// A named location in a [`VarStore`] used to register layer parameters.
///
/// ```
/// let store = rusttorch::nn::VarStore::new(rusttorch::Device::Cpu);
/// let path: rusttorch::nn::ParameterPath<'_> = store.root() / "encoder";
/// let layer = rusttorch::nn::LinearConfig::new(4, 2).build(&path)?;
/// # Ok::<(), rusttorch::RustTorchError>(())
/// ```
pub use tch::nn::Path as ParameterPath;

use std::{fmt, path::Path};

use tch::{Device, Tensor};

use crate::{
    DeviceSpec, Result, RustTorchError,
    device::{ensure_device, resolve_device},
    interop::{
        LoadOptions, LoadReport, StateDictMapping, load_state_dict, load_state_dict_with_mapping,
        save_state_dict,
    },
};

/// A tensor transformation with a fallible forward pass.
///
/// Implement this trait to compose custom layers, residual branches, or shared
/// parameters. Override [`Module::forward_t`] when behavior depends on training
/// mode. Otherwise it calls [`Module::forward`] and ignores the mode flag.
///
/// ```
/// use rusttorch::{Result, Tensor, nn::Module};
/// struct ResidualRelu;
/// impl Module for ResidualRelu {
///     fn forward(&self, input: &Tensor) -> Result<Tensor> {
///         Ok(input.f_relu()?.f_add(input)?)
///     }
/// }
/// let output = ResidualRelu.forward(&Tensor::from_slice(&[-1_f32, 2.]))?;
/// assert_eq!(Vec::<f32>::try_from(&output)?, [-1., 4.]);
/// # Ok::<(), rusttorch::RustTorchError>(())
/// ```
pub trait Module: Send {
    /// Computes the module output using its default execution mode.
    fn forward(&self, input: &Tensor) -> Result<Tensor>;

    /// Computes the module output with an explicit training flag.
    ///
    /// Mode-independent modules use [`Module::forward`] by default.
    fn forward_t(&self, input: &Tensor, _training: bool) -> Result<Tensor> {
        self.forward(input)
    }
}

/// Configuration for a fully connected layer.
///
/// A linear layer converts each input feature vector into an output feature
/// vector, preserving all leading dimensions. Use it as a regression head,
/// classifier, or hidden projection. Bias is enabled by default.
///
/// ```
/// use rusttorch::{Device, Kind, Tensor, nn::{LinearConfig, VarStore}};
/// let store = VarStore::new(Device::Cpu);
/// let head = LinearConfig::new(8, 3).build(&(store.root() / "head"))?;
/// let features = Tensor::ones([4, 8], (Kind::Float, Device::Cpu));
/// assert_eq!(head.forward(&features)?.size(), [4, 3]);
/// # Ok::<(), rusttorch::RustTorchError>(())
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LinearConfig {
    in_features: i64,
    out_features: i64,
    bias: bool,
}

impl LinearConfig {
    /// Creates a biased linear-layer configuration.
    pub const fn new(in_features: i64, out_features: i64) -> Self {
        Self {
            in_features,
            out_features,
            bias: true,
        }
    }

    #[must_use]
    /// Enables or disables the additive bias parameter.
    pub const fn bias(mut self, bias: bool) -> Self {
        self.bias = bias;
        self
    }

    /// Registers the layer parameters under `path` and creates the layer.
    ///
    /// Returns an error for negative feature counts or a parameter-store dtype
    /// that cannot track gradients (integer, boolean, or quantized types).
    pub fn build(self, path: &ParameterPath<'_>) -> Result<Linear> {
        layers::validate_parameter_kind(path)?;
        if self.in_features < 0 {
            return Err(RustTorchError::InvalidConfiguration {
                field: "in_features",
                reason: "must be non-negative".to_owned(),
            });
        }
        if self.out_features < 0 {
            return Err(RustTorchError::InvalidConfiguration {
                field: "out_features",
                reason: "must be non-negative".to_owned(),
            });
        }
        // PyTorch v2.13.0 torch/nn/modules/linear.py initializes weight before
        // bias, uniformly with fan-in bound. tch's convenience default uses
        // a different gain and allocation order. See THIRD_PARTY_NOTICES.md.
        let bound = if self.in_features == 0 {
            0.0
        } else {
            1.0 / (self.in_features as f64).sqrt()
        };
        let init = tch::nn::Init::Uniform {
            lo: -bound,
            up: bound,
        };
        let ws = path.f_var("weight", &[self.out_features, self.in_features], init)?;
        let bs = self
            .bias
            .then(|| path.f_var("bias", &[self.out_features], init))
            .transpose()?;
        let inner = tch::nn::Linear { ws, bs };
        Ok(Linear { inner })
    }
}

/// A trainable affine transformation of the last input dimension.
///
/// Construct with [`linear`] or [`LinearConfig`]. Its parameters are registered
/// as `weight` and optional `bias` beneath the chosen [`ParameterPath`].
/// [`LinearConfig`]'s example shows a classification head.
#[derive(Debug)]
pub struct Linear {
    inner: tch::nn::Linear,
}

impl Linear {
    /// Returns the weight parameter with shape `[out_features, in_features]`.
    pub fn weight(&self) -> &Tensor {
        &self.inner.ws
    }

    /// Returns the bias parameter, or `None` when bias was disabled.
    pub fn bias(&self) -> Option<&Tensor> {
        self.inner.bs.as_ref()
    }

    /// Applies the linear transformation to the last input dimension.
    pub fn forward(&self, input: &Tensor) -> Result<Tensor> {
        <Self as Module>::forward(self, input)
    }
}

impl Module for Linear {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        functional::linear(input, &self.inner.ws, self.inner.bs.as_ref())
    }
}

/// Creates a biased linear layer and registers it under `path`.
pub fn linear(path: &ParameterPath<'_>, in_features: i64, out_features: i64) -> Result<Linear> {
    LinearConfig::new(in_features, out_features).build(path)
}

/// A module that returns a shallow clone of its input.
#[derive(Debug, Default)]
pub struct Identity;

impl Module for Identity {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        Ok(input.shallow_clone())
    }
}

/// An element-wise rectified linear unit module.
#[derive(Debug, Default)]
pub struct ReLU;

impl Module for ReLU {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        functional::relu(input)
    }
}

/// Approximation mode for Gaussian error linear units.
///
/// New backend-supported approximation modes may be added in future releases.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum GeluApproximation {
    /// Uses the exact formulation.
    #[default]
    None,
    /// Uses the tanh approximation.
    Tanh,
}

impl GeluApproximation {
    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Tanh => "tanh",
        }
    }
}

/// An element-wise Gaussian error linear unit module.
#[derive(Debug, Default)]
pub struct Gelu {
    approximation: GeluApproximation,
}

impl Gelu {
    /// Creates a GELU module with the requested approximation.
    pub const fn new(approximation: GeluApproximation) -> Self {
        Self { approximation }
    }
}

impl Module for Gelu {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        functional::gelu_with_approximation(input, self.approximation)
    }
}

/// Randomly masks activations during training to regularize a model.
///
/// Standalone [`Module::forward`] uses evaluation behavior. Call
/// [`Module::forward_t`] with `true` to apply dropout, or put this layer in a
/// [`Sequential`] model whose training flag controls execution.
///
/// ```
/// use rusttorch::{Tensor, nn::{Dropout, Module}};
/// let dropout = Dropout::new(1.0)?;
/// let input = Tensor::from_slice(&[1_f32, 2.]);
/// assert_eq!(Vec::<f32>::try_from(&dropout.forward_t(&input, true)?)?, [0., 0.]);
/// assert_eq!(Vec::<f32>::try_from(&dropout.forward(&input)?)?, [1., 2.]);
/// # Ok::<(), rusttorch::RustTorchError>(())
/// ```
#[derive(Debug)]
pub struct Dropout {
    probability: f64,
}

impl Dropout {
    /// Creates dropout with a probability in the inclusive range `[0, 1]`.
    pub fn new(probability: f64) -> Result<Self> {
        functional::validate_dropout(probability)?;
        Ok(Self { probability })
    }
}

impl Module for Dropout {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        self.forward_t(input, false)
    }

    fn forward_t(&self, input: &Tensor, training: bool) -> Result<Tensor> {
        functional::dropout(input, self.probability, training)
    }
}

/// Flattens a contiguous range of tensor dimensions.
#[derive(Debug)]
pub struct Flatten {
    start_dim: i64,
    end_dim: i64,
}

impl Flatten {
    /// Creates a flatten module over the inclusive dimension range.
    pub const fn new(start_dim: i64, end_dim: i64) -> Self {
        Self { start_dim, end_dim }
    }
}

impl Default for Flatten {
    fn default() -> Self {
        Self::new(1, -1)
    }
}

impl Module for Flatten {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        functional::flatten(input, self.start_dim, self.end_dim)
    }
}

enum LayerSpec {
    Custom(LayerFactory),
    Conv1d(ConvConfig<1>),
    Conv2d(ConvConfig<2>),
    Conv3d(ConvConfig<3>),
    LayerNorm(LayerNormConfig),
    Embedding(EmbeddingConfig),
    Linear(LinearConfig),
    Identity,
    ReLU,
    Gelu(GeluApproximation),
    Dropout(f64),
    Flatten(i64, i64),
}

type LayerFactory = Box<dyn FnOnce(&ParameterPath<'_>) -> Result<Box<dyn Module>> + Send>;

/// Builder for an owned eager model and its `VarStore`.
#[derive(Default)]
pub struct SequentialBuilder {
    layers: Vec<LayerSpec>,
}

impl SequentialBuilder {
    /// Creates an empty sequential model builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds any module using the model's parameter path at this layer index.
    ///
    /// Use this for custom layers and configurations without a shorthand builder
    /// method. The factory runs on the resolved model device at build time, so
    /// parameters and persistent buffers belong to the same store and checkpoint.
    ///
    /// ```
    /// use rusttorch::{DeviceSpec, nn::{Sequential, LinearConfig, ReLU}};
    /// let model = Sequential::builder()
    ///     .layer(|path| LinearConfig::new(4, 2).build(path))
    ///     .layer(|_| Ok(ReLU))
    ///     .build(DeviceSpec::Cpu)?;
    /// assert!(model.var_store().variables().contains_key("0.weight"));
    /// # Ok::<(), rusttorch::RustTorchError>(())
    /// ```
    #[must_use]
    pub fn layer<M, F>(mut self, factory: F) -> Self
    where
        M: Module + 'static,
        F: FnOnce(&ParameterPath<'_>) -> Result<M> + Send + 'static,
    {
        self.layers.push(LayerSpec::Custom(Box::new(move |path| {
            Ok(Box::new(factory(path)?))
        })));
        self
    }

    /// Appends a sequence convolution; validates the configuration when building.
    ///
    /// ```
    /// use rusttorch::nn::{Sequential, ConvConfig};
    /// let builder = Sequential::builder().conv1d(ConvConfig::new(1, 4, [3]));
    /// ```
    #[must_use]
    pub fn conv1d(mut self, config: ConvConfig<1>) -> Self {
        self.layers.push(LayerSpec::Conv1d(config));
        self
    }

    /// Appends an image convolution; validates the configuration when building.
    ///
    /// ```
    /// use rusttorch::nn::{Sequential, ConvConfig};
    /// let builder = Sequential::builder().conv2d(ConvConfig::new(3, 8, [3, 3]));
    /// ```
    #[must_use]
    pub fn conv2d(mut self, config: ConvConfig<2>) -> Self {
        self.layers.push(LayerSpec::Conv2d(config));
        self
    }

    /// Appends a volume convolution; validates the configuration when building.
    ///
    /// ```
    /// use rusttorch::nn::{Sequential, ConvConfig};
    /// let builder = Sequential::builder().conv3d(ConvConfig::new(1, 4, [3, 3, 3]));
    /// ```
    #[must_use]
    pub fn conv3d(mut self, config: ConvConfig<3>) -> Self {
        self.layers.push(LayerSpec::Conv3d(config));
        self
    }

    /// Appends normalization over trailing feature dimensions; validates the configuration when building.
    ///
    /// ```
    /// use rusttorch::nn::{Sequential, LayerNormConfig};
    /// let builder = Sequential::builder().layer_norm(LayerNormConfig::new([8]));
    /// ```
    #[must_use]
    pub fn layer_norm(mut self, config: LayerNormConfig) -> Self {
        self.layers.push(LayerSpec::LayerNorm(config));
        self
    }

    /// Appends a token lookup table; validates the configuration when building.
    ///
    /// ```
    /// use rusttorch::nn::{Sequential, EmbeddingConfig};
    /// let builder = Sequential::builder().embedding(EmbeddingConfig::new(100, 8));
    /// ```
    #[must_use]
    pub fn embedding(mut self, config: EmbeddingConfig) -> Self {
        self.layers.push(LayerSpec::Embedding(config));
        self
    }

    #[must_use]
    /// Appends a biased linear layer.
    pub fn linear(mut self, in_features: i64, out_features: i64) -> Self {
        self.layers.push(LayerSpec::Linear(LinearConfig::new(
            in_features,
            out_features,
        )));
        self
    }

    #[must_use]
    /// Appends a configured linear layer.
    pub fn linear_config(mut self, config: LinearConfig) -> Self {
        self.layers.push(LayerSpec::Linear(config));
        self
    }

    #[must_use]
    /// Appends an identity layer.
    pub fn identity(mut self) -> Self {
        self.layers.push(LayerSpec::Identity);
        self
    }

    #[must_use]
    /// Appends an element-wise ReLU layer.
    pub fn relu(mut self) -> Self {
        self.layers.push(LayerSpec::ReLU);
        self
    }

    #[must_use]
    /// Appends an exact GELU layer.
    pub fn gelu(mut self) -> Self {
        self.layers.push(LayerSpec::Gelu(GeluApproximation::None));
        self
    }

    #[must_use]
    /// Appends a GELU layer with an explicit approximation.
    pub fn gelu_approximate(mut self, approximation: GeluApproximation) -> Self {
        self.layers.push(LayerSpec::Gelu(approximation));
        self
    }

    #[must_use]
    /// Appends dropout with the given probability.
    ///
    /// Probability validation occurs when [`SequentialBuilder::build`] is called.
    pub fn dropout(mut self, probability: f64) -> Self {
        self.layers.push(LayerSpec::Dropout(probability));
        self
    }

    #[must_use]
    /// Appends a flatten layer over the inclusive dimension range.
    pub fn flatten(mut self, start_dim: i64, end_dim: i64) -> Self {
        self.layers.push(LayerSpec::Flatten(start_dim, end_dim));
        self
    }

    /// Builds the model and allocates all parameters on the resolved device.
    pub fn build(self, device: DeviceSpec) -> Result<Sequential> {
        let device = resolve_device(device)?;
        let var_store = VarStore::new(device);
        let mut layers: Vec<Box<dyn Module>> = Vec::with_capacity(self.layers.len());
        for (index, layer) in self.layers.into_iter().enumerate() {
            let path = var_store.root() / index.to_string();
            let layer: Box<dyn Module> = match layer {
                LayerSpec::Custom(factory) => factory(&path)?,
                LayerSpec::Conv1d(config) => Box::new(config.build(&path)?),
                LayerSpec::Conv2d(config) => Box::new(config.build(&path)?),
                LayerSpec::Conv3d(config) => Box::new(config.build(&path)?),
                LayerSpec::LayerNorm(config) => Box::new(config.build(&path)?),
                LayerSpec::Embedding(config) => Box::new(config.build(&path)?),
                LayerSpec::Linear(config) => Box::new(config.build(&path)?),
                LayerSpec::Identity => Box::new(Identity),
                LayerSpec::ReLU => Box::new(ReLU),
                LayerSpec::Gelu(approximation) => Box::new(Gelu::new(approximation)),
                LayerSpec::Dropout(probability) => Box::new(Dropout::new(probability)?),
                LayerSpec::Flatten(start_dim, end_dim) => {
                    Box::new(Flatten::new(start_dim, end_dim))
                }
            };
            layers.push(layer);
        }
        Ok(Sequential {
            var_store,
            layers,
            training: true,
        })
    }
}

/// A sequence of layers with an owned parameter store.
///
/// Create a model with [`Sequential::builder`], pass its [`Self::var_store`] to
/// an optimizer, and use [`Self::save_weights`] to persist trained parameters.
/// Layers register parameters under their numeric position (for example,
/// `0.weight` and `2.bias`). New models start in training mode; call
/// [`Self::eval`] for inference. Evaluation changes dropout behavior but does
/// not disable gradient tracking; use [`crate::no_grad`] when needed.
///
/// ```
/// use rusttorch::{DeviceSpec, Kind, Tensor, nn::Sequential};
/// let mut model = Sequential::builder().linear(4, 8).relu().dropout(0.2)
///     .linear(8, 2).build(DeviceSpec::Cpu)?;
/// model.eval();
/// let features = Tensor::zeros([3, 4], (Kind::Float, model.device()));
/// let scores = rusttorch::no_grad(|| model.forward(&features))?;
/// assert_eq!(scores.size(), [3, 2]);
/// # Ok::<(), rusttorch::RustTorchError>(())
/// ```
pub struct Sequential {
    var_store: VarStore,
    layers: Vec<Box<dyn Module>>,
    training: bool,
}

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

impl Sequential {
    /// Creates an empty sequential model builder.
    pub fn builder() -> SequentialBuilder {
        SequentialBuilder::new()
    }

    /// Runs every layer using the model's current training state.
    pub fn forward(&self, input: &Tensor) -> Result<Tensor> {
        self.forward_t(input, self.training)
    }

    /// Runs every layer with an explicit training flag without changing model state.
    pub fn forward_t(&self, input: &Tensor, training: bool) -> Result<Tensor> {
        ensure_device("Sequential input", input, self.device())?;
        self.layers
            .iter()
            .try_fold(input.shallow_clone(), |value, layer| {
                layer.forward_t(&value, training)
            })
    }

    /// Enables training behavior for subsequent [`Sequential::forward`] calls.
    pub fn train(&mut self) {
        self.training = true;
    }

    /// Enables evaluation behavior for subsequent [`Sequential::forward`] calls.
    pub fn eval(&mut self) {
        self.training = false;
    }

    /// Returns whether default forward calls use training behavior.
    pub const fn is_training(&self) -> bool {
        self.training
    }

    /// Returns the device holding this model's parameters.
    pub fn device(&self) -> Device {
        self.var_store.device()
    }

    /// Returns the parameter store used by the model.
    pub const fn var_store(&self) -> &VarStore {
        &self.var_store
    }

    /// Moves all model parameters to the resolved device.
    pub fn to_device(&mut self, device: DeviceSpec) -> Result<()> {
        self.var_store.set_device(resolve_device(device)?);
        Ok(())
    }

    /// Saves the model state as a device-neutral SafeTensors file.
    pub fn save_weights(&self, path: impl AsRef<Path>) -> Result<()> {
        save_state_dict(path, &self.var_store)
    }

    /// Strictly loads model state from a SafeTensors file.
    pub fn load_weights(&self, path: impl AsRef<Path>) -> Result<LoadReport> {
        load_state_dict(path, &self.var_store)
    }

    /// Loads model state with explicit key mapping and strictness options.
    pub fn load_weights_with_mapping(
        &self,
        path: impl AsRef<Path>,
        mapping: &StateDictMapping,
        options: LoadOptions,
    ) -> Result<LoadReport> {
        load_state_dict_with_mapping(path, &self.var_store, mapping, options)
    }
}

impl Module for Sequential {
    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        Sequential::forward(self, input)
    }

    fn forward_t(&self, input: &Tensor, training: bool) -> Result<Tensor> {
        Sequential::forward_t(self, input, training)
    }
}