rusttorch 0.1.0

Eager PyTorch-style neural networks and graph execution for Rust, backed by 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
//! Eager neural-network modules.

pub mod functional;

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

use tch::{Device, Tensor, nn::VarStore, no_grad};

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 fallible eager module. `forward_t` defaults to mode-independent execution.
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 PyTorch-compatible fully connected layer.
#[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.
    pub fn build(self, path: &tch::nn::Path<'_>) -> Result<Linear> {
        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(),
            });
        }
        let inner = tch::nn::linear(
            path,
            self.in_features,
            self.out_features,
            tch::nn::LinearConfig {
                bias: self.bias,
                ..Default::default()
            },
        );
        // Adapted from PyTorch v2.13.0 torch/nn/modules/linear.py:
        // zero fan-in uses a zero bias bound. See THIRD_PARTY_NOTICES.md.
        if self.in_features == 0
            && let Some(bias) = &inner.bs
        {
            let mut bias = bias.shallow_clone();
            let _ = no_grad(|| bias.f_zero_())?;
        }
        Ok(Linear { inner })
    }
}

/// A fully connected transformation using LibTorch's linear operator.
#[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: &tch::nn::Path<'_>, 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)
    }
}

/// A dropout module that is active only during training execution.
#[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 {
    Linear(LinearConfig),
    Identity,
    ReLU,
    Gelu(GeluApproximation),
    Dropout(f64),
    Flatten(i64, i64),
}

/// 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()
    }

    #[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::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,
        })
    }
}

/// An owned eager sequence with stable PyTorch-style numeric parameter paths.
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)
    }
}