trustformers-models 0.1.1

Model implementations for TrustformeRS
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
use serde::{Deserialize, Serialize};
use trustformers_core::{
    errors::{invalid_config, Result},
    traits::Config,
};

/// Mamba model configuration
/// Reference: "Mamba: Linear-Time Sequence Modeling with Selective State Spaces" (Gu & Dao, 2023)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MambaConfig {
    /// Model dimension (d_model)
    pub d_model: usize,
    /// State space dimension (d_state or N)
    pub d_state: usize,
    /// Local convolution width (d_conv)
    pub d_conv: usize,
    /// Expansion factor for inner dimension
    pub expand: usize,
    /// Number of layers
    pub n_layer: usize,
    /// Vocabulary size
    pub vocab_size: usize,
    /// Maximum position embeddings (sequence length)
    pub max_position_embeddings: usize,
    /// RMS norm epsilon
    pub rms_norm_eps: f32,
    /// Initializer range
    pub initializer_range: f32,
    /// Rescale prenorm residual
    pub rescale_prenorm_residual: bool,
    /// Use bias in linear layers
    pub use_bias: bool,
    /// Use conv bias
    pub use_conv_bias: bool,
    /// Time step rank (delta rank)
    pub dt_rank: Option<usize>,
    /// Time step minimum
    pub dt_min: f32,
    /// Time step maximum
    pub dt_max: f32,
    /// Time step initialization
    pub dt_init: String,
    /// Time step scale
    pub dt_scale: f32,
    /// Time step init floor
    pub dt_init_floor: f32,
    /// Pad token ID
    pub pad_token_id: Option<u32>,
    /// Beginning of sequence token ID
    pub bos_token_id: u32,
    /// End of sequence token ID
    pub eos_token_id: u32,
    /// Whether to tie word embeddings
    pub tie_word_embeddings: bool,
    /// Model type identifier
    pub model_type: String,
}

impl Default for MambaConfig {
    fn default() -> Self {
        Self {
            d_model: 768,
            d_state: 16,
            d_conv: 4,
            expand: 2,
            n_layer: 24,
            vocab_size: 50280,
            max_position_embeddings: 2048,
            rms_norm_eps: 1e-5,
            initializer_range: 0.1,
            rescale_prenorm_residual: true,
            use_bias: false,
            use_conv_bias: true,
            dt_rank: None, // Auto-computed: ceil(d_model / 16)
            dt_min: 0.001,
            dt_max: 0.1,
            dt_init: "random".to_string(),
            dt_scale: 1.0,
            dt_init_floor: 1e-4,
            pad_token_id: Some(0),
            bos_token_id: 0,
            eos_token_id: 0,
            tie_word_embeddings: true,
            model_type: "mamba".to_string(),
        }
    }
}

impl MambaConfig {
    /// Mamba-130M configuration
    pub fn mamba_130m() -> Self {
        Self {
            d_model: 768,
            n_layer: 24,
            vocab_size: 50280,
            max_position_embeddings: 2048,
            ..Default::default()
        }
    }

    /// Mamba-370M configuration
    pub fn mamba_370m() -> Self {
        Self {
            d_model: 1024,
            n_layer: 48,
            vocab_size: 50280,
            max_position_embeddings: 2048,
            ..Default::default()
        }
    }

    /// Mamba-790M configuration
    pub fn mamba_790m() -> Self {
        Self {
            d_model: 1536,
            n_layer: 48,
            vocab_size: 50280,
            max_position_embeddings: 2048,
            ..Default::default()
        }
    }

    /// Mamba-1.4B configuration
    pub fn mamba_1_4b() -> Self {
        Self {
            d_model: 2048,
            n_layer: 48,
            vocab_size: 50280,
            max_position_embeddings: 2048,
            ..Default::default()
        }
    }

    /// Mamba-2.8B configuration
    pub fn mamba_2_8b() -> Self {
        Self {
            d_model: 2560,
            n_layer: 64,
            vocab_size: 50280,
            max_position_embeddings: 2048,
            ..Default::default()
        }
    }

    /// Get the computed dt_rank (delta rank) if not explicitly set
    pub fn get_dt_rank(&self) -> usize {
        self.dt_rank.unwrap_or_else(|| {
            // Standard computation: ceil(d_model / 16)
            self.d_model.div_ceil(16)
        })
    }

    /// Get the inner dimension (d_inner)
    pub fn get_d_inner(&self) -> usize {
        self.d_model * self.expand
    }

    /// Create configuration from pretrained model name
    pub fn from_pretrained_name(name: &str) -> Option<Self> {
        match name {
            "state-spaces/mamba-130m" | "mamba-130m" => Some(Self::mamba_130m()),
            "state-spaces/mamba-370m" | "mamba-370m" => Some(Self::mamba_370m()),
            "state-spaces/mamba-790m" | "mamba-790m" => Some(Self::mamba_790m()),
            "state-spaces/mamba-1.4b" | "mamba-1.4b" => Some(Self::mamba_1_4b()),
            "state-spaces/mamba-2.8b" | "mamba-2.8b" => Some(Self::mamba_2_8b()),
            _ => None,
        }
    }
}

impl Config for MambaConfig {
    fn architecture(&self) -> &'static str {
        "mamba"
    }

    fn validate(&self) -> Result<()> {
        if self.d_model == 0 {
            return Err(invalid_config(
                "config_field",
                "d_model must be greater than 0",
            ));
        }
        if self.n_layer == 0 {
            return Err(invalid_config(
                "config_field",
                "n_layer must be greater than 0",
            ));
        }
        if self.vocab_size == 0 {
            return Err(invalid_config(
                "config_field",
                "vocab_size must be greater than 0",
            ));
        }
        Ok(())
    }
}

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

    #[test]
    fn test_default_config() {
        let config = MambaConfig::default();
        assert_eq!(config.d_model, 768);
        assert_eq!(config.d_state, 16);
        assert_eq!(config.d_conv, 4);
        assert_eq!(config.expand, 2);
        assert_eq!(config.n_layer, 24);
    }

    #[test]
    fn test_dt_rank_computation() {
        let config = MambaConfig::default();
        assert_eq!(config.get_dt_rank(), 48); // ceil(768 / 16) = 48

        let config_with_dt_rank = MambaConfig {
            dt_rank: Some(32),
            ..Default::default()
        };
        assert_eq!(config_with_dt_rank.get_dt_rank(), 32);
    }

    #[test]
    fn test_d_inner_computation() {
        let config = MambaConfig::default();
        assert_eq!(config.get_d_inner(), 1536); // 768 * 2
    }

    #[test]
    fn test_predefined_configs() {
        let config_130m = MambaConfig::mamba_130m();
        assert_eq!(config_130m.d_model, 768);
        assert_eq!(config_130m.n_layer, 24);

        let config_2_8b = MambaConfig::mamba_2_8b();
        assert_eq!(config_2_8b.d_model, 2560);
        assert_eq!(config_2_8b.n_layer, 64);
    }

    #[test]
    fn test_from_pretrained_name() {
        let config = MambaConfig::from_pretrained_name("state-spaces/mamba-130m");
        assert!(config.is_some());
        assert_eq!(config.expect("operation failed").d_model, 768);

        let config = MambaConfig::from_pretrained_name("unknown-model");
        assert!(config.is_none());
    }

    #[test]
    fn test_config_trait() {
        let config = MambaConfig::default();
        assert_eq!(config.architecture(), "mamba");
        assert!(config.validate().is_ok());
    }

    // ---- Mamba-2.8B specific defaults ----

    #[test]
    fn test_mamba_2_8b_d_model() {
        let config = MambaConfig::mamba_2_8b();
        assert_eq!(config.d_model, 2560, "Mamba-2.8B d_model must be 2560");
    }

    #[test]
    fn test_mamba_2_8b_n_layers() {
        let config = MambaConfig::mamba_2_8b();
        assert_eq!(config.n_layer, 64, "Mamba-2.8B must have 64 layers");
    }

    #[test]
    fn test_mamba_2_8b_d_state() {
        let config = MambaConfig::mamba_2_8b();
        assert_eq!(config.d_state, 16, "Mamba-2.8B d_state must be 16");
    }

    #[test]
    fn test_mamba_2_8b_expand() {
        let config = MambaConfig::mamba_2_8b();
        assert_eq!(config.expand, 2, "Mamba-2.8B expand must be 2");
    }

    // ---- dt_rank "auto" = ceil(d_model/16) ----

    #[test]
    fn test_dt_rank_auto_130m() {
        let config = MambaConfig::mamba_130m();
        // ceil(768 / 16) = 48
        assert_eq!(config.get_dt_rank(), 48);
    }

    #[test]
    fn test_dt_rank_auto_370m() {
        let config = MambaConfig::mamba_370m();
        // ceil(1024 / 16) = 64
        assert_eq!(config.get_dt_rank(), 64);
    }

    #[test]
    fn test_dt_rank_auto_790m() {
        let config = MambaConfig::mamba_790m();
        // ceil(1536 / 16) = 96
        assert_eq!(config.get_dt_rank(), 96);
    }

    #[test]
    fn test_dt_rank_auto_1_4b() {
        let config = MambaConfig::mamba_1_4b();
        // ceil(2048 / 16) = 128
        assert_eq!(config.get_dt_rank(), 128);
    }

    #[test]
    fn test_dt_rank_auto_2_8b() {
        let config = MambaConfig::mamba_2_8b();
        // ceil(2560 / 16) = 160
        assert_eq!(config.get_dt_rank(), 160);
    }

    #[test]
    fn test_dt_rank_explicit_overrides_auto() {
        let config = MambaConfig {
            d_model: 768,
            dt_rank: Some(100),
            ..Default::default()
        };
        assert_eq!(
            config.get_dt_rank(),
            100,
            "Explicit dt_rank should override auto"
        );
    }

    // ---- d_inner = expand * d_model ----

    #[test]
    fn test_d_inner_2_8b() {
        let config = MambaConfig::mamba_2_8b();
        assert_eq!(
            config.get_d_inner(),
            config.expand * config.d_model,
            "d_inner = expand * d_model"
        );
    }

    #[test]
    fn test_d_inner_370m() {
        let config = MambaConfig::mamba_370m();
        // 1024 * 2 = 2048
        assert_eq!(config.get_d_inner(), 2048);
    }

    // ---- d_conv = 4 (default SSM conv width) ----

    #[test]
    fn test_d_conv_default_is_4() {
        let config = MambaConfig::default();
        assert_eq!(config.d_conv, 4);
    }

    #[test]
    fn test_d_conv_preserved_in_presets() {
        for d_model in [768_usize, 1024, 1536, 2048, 2560] {
            let config = MambaConfig {
                d_model,
                ..Default::default()
            };
            assert_eq!(config.d_conv, 4, "d_conv must be 4 for d_model={}", d_model);
        }
    }

    // ---- conv_bias ----

    #[test]
    fn test_use_conv_bias_default_true() {
        let config = MambaConfig::default();
        assert!(
            config.use_conv_bias,
            "use_conv_bias must be true by default"
        );
    }

    #[test]
    fn test_use_bias_default_false() {
        let config = MambaConfig::default();
        assert!(
            !config.use_bias,
            "use_bias must be false by default (no linear bias)"
        );
    }

    // ---- Validation ----

    #[test]
    fn test_validation_fails_zero_d_model() {
        let config = MambaConfig {
            d_model: 0,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_validation_fails_zero_n_layer() {
        let config = MambaConfig {
            n_layer: 0,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_validation_fails_zero_vocab_size() {
        let config = MambaConfig {
            vocab_size: 0,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_all_presets_validate() {
        for config in [
            MambaConfig::mamba_130m(),
            MambaConfig::mamba_370m(),
            MambaConfig::mamba_790m(),
            MambaConfig::mamba_1_4b(),
            MambaConfig::mamba_2_8b(),
        ] {
            assert!(
                config.validate().is_ok(),
                "Preset with d_model={} failed validation",
                config.d_model
            );
        }
    }

    // ---- from_pretrained short names ----

    #[test]
    fn test_from_pretrained_short_name_2_8b() {
        let config = MambaConfig::from_pretrained_name("mamba-2.8b");
        assert!(config.is_some());
        assert_eq!(config.expect("mamba-2.8b config").d_model, 2560);
    }

    #[test]
    fn test_from_pretrained_short_name_790m() {
        let config = MambaConfig::from_pretrained_name("mamba-790m");
        assert!(config.is_some());
        assert_eq!(config.expect("mamba-790m config").d_model, 1536);
    }

    // ---- Parameter ordering: larger model → larger d_model ----

    #[test]
    fn test_d_model_increases_with_model_size() {
        let configs = [
            MambaConfig::mamba_130m(),
            MambaConfig::mamba_370m(),
            MambaConfig::mamba_790m(),
            MambaConfig::mamba_1_4b(),
            MambaConfig::mamba_2_8b(),
        ];
        for i in 1..configs.len() {
            assert!(
                configs[i].d_model >= configs[i - 1].d_model,
                "d_model must be non-decreasing with model size"
            );
        }
    }

    // ---- Serialization ----

    #[test]
    fn test_config_serialization_roundtrip() {
        let config = MambaConfig::mamba_2_8b();
        let json = serde_json::to_string(&config).expect("serialize MambaConfig");
        let restored: MambaConfig = serde_json::from_str(&json).expect("deserialize MambaConfig");
        assert_eq!(config.d_model, restored.d_model);
        assert_eq!(config.n_layer, restored.n_layer);
        assert_eq!(config.d_state, restored.d_state);
    }

    #[test]
    fn test_config_clone_preserves_fields() {
        let config = MambaConfig::mamba_2_8b();
        let cloned = config.clone();
        assert_eq!(config.d_model, cloned.d_model);
        assert_eq!(config.expand, cloned.expand);
        assert_eq!(config.d_conv, cloned.d_conv);
    }
}