multiscreen-rs 0.2.5

A Rust implementation of the Multiscreen neural language model — training and inference powered by Burn.
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
//! High-level training API with a builder pattern.
//!
//! The [`Trainer`] struct wraps model construction, token-sequence training,
//! and checkpoint management behind a single ergonomic interface with sensible
//! defaults.
//!
//! Users encode their own text into token IDs using any tokenizer they choose,
//! then pass the `Vec<Vec<u32>>` sequences to [`Trainer::train_on_token_sequences`].
//!
//! # Example
//!
//! ```rust,no_run
//! use multiscreen_rs::prelude::*;
//!
//! fn main() -> multiscreen_rs::Result<()> {
//!     let mut trainer = Trainer::builder()
//!         .vocab_size(1000)
//!         .budget(ParameterBudget::Params10M)
//!         .batch_size(4)
//!         .seq_len(64)
//!         .steps(100)
//!         .device(auto_device()?)
//!         .build()?;
//!
//!     // Token sequences from YOUR tokenizer
//!     let sequences = vec![
//!         vec![1, 2, 3, 4, 5],
//!         vec![1, 2, 6, 7, 5],
//!     ];
//!
//!     let report = trainer.train_on_token_sequences(&sequences)?;
//!     println!("trained {} steps, final loss {:.4}", report.steps, report.final_loss);
//!     Ok(())
//! }
//! ```

use crate::error::{Error, Result};
use crate::model::{
    DefaultMultiscreenModel, ModelTrainingConfig, ModelTrainingReport, MultiscreenModelConfig,
};
use crate::runtime::{default_device, Device};
use std::fs;
use std::path::Path;

/// Re-export of the parameter budget enum for convenience.
pub use crate::model::MultiscreenParameterBudget as ParameterBudget;

// ---------------------------------------------------------------------------
// TrainingReport
// ---------------------------------------------------------------------------

/// Summary returned after training via the high-level [`Trainer`].
#[derive(Clone, Debug)]
pub struct TrainingReport {
    /// Number of training steps completed.
    pub steps: usize,
    /// Final training loss.
    pub final_loss: f32,
    /// Total number of parameters in the model.
    pub parameter_count: usize,
    /// Path the checkpoint was saved to, if any.
    pub checkpoint_path: Option<String>,
}

impl TrainingReport {
    fn from_model_report(report: &ModelTrainingReport, checkpoint_path: Option<String>) -> Self {
        Self {
            steps: report.steps,
            final_loss: report.final_loss,
            parameter_count: report.parameter_count,
            checkpoint_path,
        }
    }
}

// ---------------------------------------------------------------------------
// Trainer
// ---------------------------------------------------------------------------

/// High-level trainer that bundles a model and training config.
///
/// Construct via [`Trainer::builder()`] and the [`TrainerBuilder`] struct.
/// Users provide token sequences (`Vec<Vec<u32>>`) from their own tokenizer.
pub struct Trainer {
    model: DefaultMultiscreenModel,
    training_config: ModelTrainingConfig,
    checkpoint_dir: Option<String>,
    #[allow(dead_code)]
    checkpoint_interval: usize,
    #[allow(dead_code)]
    run_dir: Option<String>,
}

impl std::fmt::Debug for Trainer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Trainer")
            .field("training_config", &self.training_config)
            .field("checkpoint_dir", &self.checkpoint_dir)
            .field("checkpoint_interval", &self.checkpoint_interval)
            .field("run_dir", &self.run_dir)
            .finish_non_exhaustive()
    }
}

impl Trainer {
    /// Returns a new [`TrainerBuilder`] with sensible defaults.
    pub fn builder() -> TrainerBuilder {
        TrainerBuilder::new()
    }

    /// Trains the model on the provided token sequences.
    ///
    /// Each inner `Vec<u32>` is a tokenized text sample. Use your own
    /// tokenizer to produce these sequences before calling this method.
    ///
    /// The `on_step` callback is invoked after each optimizer step with
    /// `(step_index, loss_value)`.
    pub fn train_on_token_sequences_with_callback(
        &mut self,
        sequences: &[Vec<u32>],
        on_step: impl FnMut(usize, f32),
    ) -> Result<TrainingReport> {
        if sequences.is_empty() {
            return Err(Error::Training("no training sequences provided".into()));
        }

        let device = self.model_device();
        let report =
            self.model
                .train_token_sequences(sequences, &self.training_config, &device, on_step)?;

        let checkpoint_path = match &self.checkpoint_dir {
            Some(dir) => {
                let dir_path = Path::new(dir);
                fs::create_dir_all(dir_path).map_err(|e| {
                    Error::Io(format!(
                        "failed to create checkpoint directory {:?}: {}",
                        dir, e
                    ))
                })?;
                let path = dir_path.join("checkpoint.mpk");
                self.model.save_parameters(&path)?;
                Some(path.to_string_lossy().into_owned())
            }
            None => None,
        };

        Ok(TrainingReport::from_model_report(&report, checkpoint_path))
    }

    /// Convenience wrapper that trains without a per-step callback.
    pub fn train_on_token_sequences(&mut self, sequences: &[Vec<u32>]) -> Result<TrainingReport> {
        self.train_on_token_sequences_with_callback(sequences, |_, _| {})
    }

    /// Trains the model on chat-style (prompt, response) token-ID pairs.
    ///
    /// Each element of `chat_pairs` is `(prompt_token_ids, response_token_ids)`.
    /// The model sees the full context but loss is computed only on response
    /// tokens, preventing the model from learning to generate role labels.
    ///
    /// The `on_step` callback is invoked after each optimizer step with
    /// `(step_index, loss_value)`.
    pub fn train_on_chat_sequences_with_callback(
        &mut self,
        chat_pairs: &[(Vec<u32>, Vec<u32>)],
        on_step: impl FnMut(usize, f32),
    ) -> Result<TrainingReport> {
        if chat_pairs.is_empty() {
            return Err(Error::Training("no training chat pairs provided".into()));
        }

        let device = self.model_device();
        let report =
            self.model
                .train_chat_sequences(chat_pairs, &self.training_config, &device, on_step)?;

        let checkpoint_path = match &self.checkpoint_dir {
            Some(dir) => {
                let dir_path = Path::new(dir);
                fs::create_dir_all(dir_path).map_err(|e| {
                    Error::Io(format!(
                        "failed to create checkpoint directory {:?}: {}",
                        dir, e
                    ))
                })?;
                let path = dir_path.join("checkpoint.mpk");
                self.model.save_parameters(&path)?;
                Some(path.to_string_lossy().into_owned())
            }
            None => None,
        };

        Ok(TrainingReport::from_model_report(&report, checkpoint_path))
    }

    /// Convenience wrapper that trains on chat pairs without a per-step callback.
    pub fn train_on_chat_sequences(
        &mut self,
        chat_pairs: &[(Vec<u32>, Vec<u32>)],
    ) -> Result<TrainingReport> {
        self.train_on_chat_sequences_with_callback(chat_pairs, |_, _| {})
    }

    /// Saves a model checkpoint to the given path.
    pub fn save_checkpoint(&self, path: &str) -> Result<()> {
        if let Some(parent) = Path::new(path).parent() {
            fs::create_dir_all(parent).map_err(|e| {
                Error::Io(format!(
                    "failed to create checkpoint directory {:?}: {}",
                    parent, e
                ))
            })?;
        }
        self.model.save_parameters(path)
    }

    /// Returns a reference to the underlying model.
    pub fn model(&self) -> &DefaultMultiscreenModel {
        &self.model
    }

    /// Returns a mutable reference to the underlying model.
    pub fn model_mut(&mut self) -> &mut DefaultMultiscreenModel {
        &mut self.model
    }

    /// Returns a reference to the training configuration.
    pub fn training_config(&self) -> &ModelTrainingConfig {
        &self.training_config
    }

    /// Obtains the device the model is currently on.
    fn model_device(&self) -> Device {
        Device::default()
    }
}

// ---------------------------------------------------------------------------
// TrainerBuilder
// ---------------------------------------------------------------------------

/// Builder for constructing a [`Trainer`] with sensible defaults.
pub struct TrainerBuilder {
    vocab_size: Option<usize>,
    budget: ParameterBudget,
    device: Option<Device>,
    batch_size: usize,
    seq_len: usize,
    steps: usize,
    learning_rate: f64,
    weight_decay: f64,
    grad_clip_norm: Option<f64>,
    checkpoint_dir: Option<String>,
    checkpoint_interval: usize,
    run_dir: Option<String>,
}

impl TrainerBuilder {
    /// Creates a new builder with default values.
    fn new() -> Self {
        Self {
            vocab_size: None,
            budget: ParameterBudget::Params10M,
            device: None,
            batch_size: 4,
            seq_len: 128,
            steps: 1000,
            learning_rate: 2e-4,
            weight_decay: 0.01,
            grad_clip_norm: Some(1.0),
            checkpoint_dir: None,
            checkpoint_interval: 1000,
            run_dir: None,
        }
    }

    /// Sets the vocabulary size (required).
    ///
    /// This is the number of distinct token IDs your tokenizer produces.
    pub fn vocab_size(mut self, size: usize) -> Self {
        self.vocab_size = Some(size);
        self
    }

    /// Sets the parameter budget. Defaults to [`ParameterBudget::Params10M`].
    pub fn budget(mut self, budget: ParameterBudget) -> Self {
        self.budget = budget;
        self
    }

    /// Sets the compute device. Defaults to [`default_device()`].
    pub fn device(mut self, device: Device) -> Self {
        self.device = Some(device);
        self
    }

    /// Sets the batch size. Defaults to 4.
    pub fn batch_size(mut self, size: usize) -> Self {
        self.batch_size = size;
        self
    }

    /// Sets the sequence length. Defaults to 128.
    pub fn seq_len(mut self, len: usize) -> Self {
        self.seq_len = len;
        self
    }

    /// Sets the number of training steps. Defaults to 1000.
    pub fn steps(mut self, steps: usize) -> Self {
        self.steps = steps;
        self
    }

    /// Sets the learning rate. Defaults to `2e-4`.
    pub fn learning_rate(mut self, lr: f64) -> Self {
        self.learning_rate = lr;
        self
    }

    /// Sets the weight decay. Defaults to `0.01`.
    pub fn weight_decay(mut self, wd: f64) -> Self {
        self.weight_decay = wd;
        self
    }

    /// Sets gradient clip norm. Defaults to `Some(1.0)`.
    pub fn grad_clip_norm(mut self, norm: Option<f64>) -> Self {
        self.grad_clip_norm = norm;
        self
    }

    /// Sets the directory where checkpoints are saved.
    pub fn checkpoint_dir(mut self, dir: impl Into<String>) -> Self {
        self.checkpoint_dir = Some(dir.into());
        self
    }

    /// Sets how often (in steps) to save checkpoints. Defaults to 1000.
    pub fn checkpoint_interval(mut self, steps: usize) -> Self {
        self.checkpoint_interval = steps;
        self
    }

    /// Overrides the run directory.
    pub fn run_dir(mut self, dir: impl Into<String>) -> Self {
        self.run_dir = Some(dir.into());
        self
    }

    /// Builds the [`Trainer`].
    ///
    /// This constructs the model config from the parameter budget + vocab size,
    /// and creates the model.
    pub fn build(self) -> Result<Trainer> {
        let vocab_size = self.vocab_size.ok_or_else(|| {
            Error::Config("vocab_size is required; call .vocab_size(n) before .build()".into())
        })?;

        let device = match self.device {
            Some(d) => d,
            None => default_device()?,
        };

        let config =
            MultiscreenModelConfig::for_parameter_budget(self.budget, vocab_size, self.seq_len);
        let model = DefaultMultiscreenModel::new(config, &device)?;

        let training_config = ModelTrainingConfig {
            steps: self.steps,
            batch_size: self.batch_size,
            learning_rate: self.learning_rate,
            weight_decay: self.weight_decay,
            grad_clip_norm: self.grad_clip_norm,
            pad_token_id: 0,
        };

        let run_dir = self.run_dir.or_else(|| Some("runs/latest".to_string()));

        Ok(Trainer {
            model,
            training_config,
            checkpoint_dir: self.checkpoint_dir,
            checkpoint_interval: self.checkpoint_interval,
            run_dir,
        })
    }
}

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

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn builder_requires_vocab_size() {
        let result = Trainer::builder().build();
        assert!(result.is_err(), "build should fail without vocab_size");
        let msg = format!("{}", result.unwrap_err());
        assert!(
            msg.contains("vocab_size"),
            "error should mention vocab_size: {}",
            msg
        );
    }

    #[test]
    fn training_report_from_model_report() {
        let model_report = ModelTrainingReport {
            steps: 500,
            final_loss: 0.123,
            training_window_count: 100,
            parameter_count: 10_000_000,
        };
        let report =
            TrainingReport::from_model_report(&model_report, Some("runs/checkpoint.mpk".into()));
        assert_eq!(report.steps, 500);
        assert!((report.final_loss - 0.123).abs() < f32::EPSILON);
        assert_eq!(report.parameter_count, 10_000_000);
        assert_eq!(
            report.checkpoint_path.as_deref(),
            Some("runs/checkpoint.mpk")
        );
    }

    #[test]
    fn builder_defaults() {
        let builder = TrainerBuilder::new();
        assert!(builder.vocab_size.is_none());
        assert!(matches!(builder.budget, ParameterBudget::Params10M));
        assert!(builder.device.is_none());
        assert_eq!(builder.batch_size, 4);
        assert_eq!(builder.seq_len, 128);
        assert_eq!(builder.steps, 1000);
        assert!((builder.learning_rate - 2e-4).abs() < f64::EPSILON);
        assert_eq!(builder.checkpoint_interval, 1000);
    }
}