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
//! StableLM Model Implementation
//!
//! StableLM is a family of open-source language models developed by Stability AI.
//! Based on the LLaMA architecture with some optimizations and different configurations.
//!
//! Key features:
//! - RMSNorm for layer normalization
//! - SwiGLU/SiLU activation functions
//! - Rotary Position Embeddings (RoPE) with partial rotary factor
//! - Grouped-query attention in newer versions
//! - Various model sizes: 1.6B, 3B, 7B, 12B parameters
//!
//! References:
//! - StableLM models: <https://github.com/Stability-AI/StableLM>
//! - Based on LLaMA architecture innovations

pub mod config;
pub mod model;

pub use config::{RopeScaling, StableLMConfig};
pub use model::{
    StableLMAttention, StableLMCausalLMOutputs, StableLMDecoderLayer, StableLMEmbeddings,
    StableLMForCausalLM, StableLMMLP, StableLMModel, StableLMOutputs,
};

use trustformers_core::errors::TrustformersError;

/// Re-export common types for convenience
pub type StableLM3B = StableLMForCausalLM;
pub type StableLM7B = StableLMForCausalLM;
pub type StableLMZephyr = StableLMForCausalLM;
pub type StableLMCode = StableLMForCausalLM;

/// Model variant identifiers
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StableLMVariant {
    /// StableLM-3B base model
    Base3B,
    /// StableLM-7B base model
    Base7B,
    /// StableLM-Zephyr (instruction-tuned)
    Zephyr3B,
    /// StableLM-Code (code-specialized)
    Code3B,
    /// StableLM-2-1.6B (second generation)
    V2_1_6B,
    /// StableLM-2-12B (second generation)
    V2_12B,
}

impl StableLMVariant {
    /// Get the default configuration for this variant
    pub fn config(self) -> StableLMConfig {
        match self {
            StableLMVariant::Base3B => StableLMConfig::stablelm_3b(),
            StableLMVariant::Base7B => StableLMConfig::stablelm_7b(),
            StableLMVariant::Zephyr3B => StableLMConfig::stablelm_zephyr_3b(),
            StableLMVariant::Code3B => StableLMConfig::stablelm_code_3b(),
            StableLMVariant::V2_1_6B => StableLMConfig::stablelm_2_1_6b(),
            StableLMVariant::V2_12B => StableLMConfig::stablelm_2_12b(),
        }
    }

    /// Get the HuggingFace model name for this variant
    pub fn model_name(self) -> &'static str {
        match self {
            StableLMVariant::Base3B => "stabilityai/stablelm-3b-4e1t",
            StableLMVariant::Base7B => "stabilityai/stablelm-base-alpha-7b",
            StableLMVariant::Zephyr3B => "stabilityai/stablelm-zephyr-3b",
            StableLMVariant::Code3B => "stabilityai/stable-code-3b",
            StableLMVariant::V2_1_6B => "stabilityai/stablelm-2-1_6b",
            StableLMVariant::V2_12B => "stabilityai/stablelm-2-12b",
        }
    }

    /// Get approximate parameter count
    pub fn parameter_count(self) -> usize {
        match self {
            StableLMVariant::V2_1_6B => 1_600_000_000,
            StableLMVariant::Base3B | StableLMVariant::Zephyr3B | StableLMVariant::Code3B => {
                3_000_000_000
            },
            StableLMVariant::Base7B => 7_000_000_000,
            StableLMVariant::V2_12B => 12_000_000_000,
        }
    }

    /// Check if this variant supports grouped-query attention
    pub fn has_grouped_query_attention(self) -> bool {
        matches!(self, StableLMVariant::V2_1_6B | StableLMVariant::V2_12B)
    }
}

use trustformers_core::device::Device;

/// Helper function to create a StableLM model from a variant
pub fn create_model(variant: StableLMVariant) -> Result<StableLMForCausalLM, TrustformersError> {
    let config = variant.config();
    StableLMForCausalLM::new(config)
}

/// Helper function to create a StableLM model from a variant with device support
pub fn create_model_with_device(
    variant: StableLMVariant,
    device: Device,
) -> Result<StableLMForCausalLM, TrustformersError> {
    let config = variant.config();
    StableLMForCausalLM::new_with_device(config, device)
}

/// Helper function to create a StableLM model from a HuggingFace model name
pub fn from_pretrained_name(
    model_name: &str,
) -> Option<Result<StableLMForCausalLM, TrustformersError>> {
    StableLMConfig::from_pretrained_name(model_name).map(StableLMForCausalLM::new)
}

/// Helper function to create a StableLM model from a HuggingFace model name with device support
pub fn from_pretrained_name_with_device(
    model_name: &str,
    device: Device,
) -> Option<Result<StableLMForCausalLM, TrustformersError>> {
    StableLMConfig::from_pretrained_name(model_name)
        .map(|config| StableLMForCausalLM::new_with_device(config, device))
}

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

    #[test]
    fn test_variant_configs() {
        let variant = StableLMVariant::Base3B;
        let config = variant.config();
        assert_eq!(config.hidden_size, 2560);
        assert_eq!(variant.model_name(), "stabilityai/stablelm-3b-4e1t");
        assert!(!variant.has_grouped_query_attention());

        let variant = StableLMVariant::V2_12B;
        let config = variant.config();
        assert_eq!(config.hidden_size, 5120);
        assert!(variant.has_grouped_query_attention());
    }

    #[test]
    #[ignore] // Heavy test - creates StableLM 3B model, run with --ignored
    fn test_create_model() -> Result<(), TrustformersError> {
        let model = create_model(StableLMVariant::Base3B)?;
        assert_eq!(model.model.config.hidden_size, 2560);
        Ok(())
    }

    #[test]
    #[ignore] // Heavy test - creates StableLM 3B model, run with --ignored
    fn test_from_pretrained_name() {
        let model = from_pretrained_name("stabilityai/stablelm-3b-4e1t");
        assert!(model.is_some());

        let model = from_pretrained_name("nonexistent/model");
        assert!(model.is_none());
    }

    // ---- StableLM variant identity ----

    #[test]
    fn test_base3b_hidden_size() {
        let config = StableLMVariant::Base3B.config();
        assert_eq!(
            config.hidden_size, 2560,
            "StableLM-3B hidden_size must be 2560"
        );
    }

    #[test]
    fn test_base3b_num_layers() {
        let config = StableLMVariant::Base3B.config();
        assert_eq!(
            config.num_hidden_layers, 32,
            "StableLM-3B must have 32 layers"
        );
    }

    #[test]
    fn test_base7b_hidden_size() {
        let config = StableLMVariant::Base7B.config();
        assert_eq!(
            config.hidden_size, 4096,
            "StableLM-7B hidden_size must be 4096"
        );
    }

    #[test]
    fn test_zephyr3b_model_type() {
        let config = StableLMVariant::Zephyr3B.config();
        assert!(
            config.model_type.contains("zephyr"),
            "Zephyr model_type must contain 'zephyr'"
        );
    }

    #[test]
    fn test_code3b_vocab_size_differs() {
        let base_config = StableLMVariant::Base3B.config();
        let code_config = StableLMVariant::Code3B.config();
        // Code model uses different (smaller) vocab for code tokens
        assert_ne!(
            base_config.vocab_size, code_config.vocab_size,
            "Code variant must have a different vocab size"
        );
    }

    #[test]
    fn test_v2_1_6b_gqa() {
        let variant = StableLMVariant::V2_1_6B;
        assert!(
            variant.has_grouped_query_attention(),
            "V2_1_6B must support GQA"
        );
        let config = variant.config();
        assert!(
            config.num_key_value_heads.is_some(),
            "V2_1_6B must set num_key_value_heads"
        );
    }

    #[test]
    fn test_v2_12b_hidden_size() {
        let config = StableLMVariant::V2_12B.config();
        assert_eq!(
            config.hidden_size, 5120,
            "StableLM-2-12B hidden_size must be 5120"
        );
    }

    #[test]
    fn test_v2_12b_layers() {
        let config = StableLMVariant::V2_12B.config();
        assert_eq!(
            config.num_hidden_layers, 40,
            "StableLM-2-12B must have 40 layers"
        );
    }

    // ---- Rotary embedding (partial_rotary_factor) ----

    #[test]
    fn test_partial_rotary_factor_3b() {
        let config = StableLMVariant::Base3B.config();
        assert!(
            (config.partial_rotary_factor - 0.25).abs() < 1e-6,
            "StableLM-3B partial_rotary_factor must be 0.25, got {}",
            config.partial_rotary_factor
        );
    }

    #[test]
    fn test_partial_rotary_factor_in_range() {
        for variant in [
            StableLMVariant::Base3B,
            StableLMVariant::Base7B,
            StableLMVariant::Zephyr3B,
            StableLMVariant::Code3B,
            StableLMVariant::V2_1_6B,
            StableLMVariant::V2_12B,
        ] {
            let config = variant.config();
            assert!(
                config.partial_rotary_factor > 0.0 && config.partial_rotary_factor <= 1.0,
                "{:?} has out-of-range partial_rotary_factor: {}",
                variant,
                config.partial_rotary_factor
            );
        }
    }

    #[test]
    fn test_rotary_dim_derived_from_factor() {
        // rotary_dim = head_dim * partial_rotary_factor  (must be integer)
        let config = StableLMVariant::Base3B.config();
        let head_dim = config.hidden_size / config.num_attention_heads;
        let rotary_dim = (head_dim as f32 * config.partial_rotary_factor).round() as usize;
        assert!(rotary_dim > 0, "Rotary dimension must be positive");
        assert!(
            rotary_dim <= head_dim,
            "Rotary dimension cannot exceed head_dim"
        );
    }

    // ---- Validation ----

    #[test]
    fn test_all_variants_validate() {
        for variant in [
            StableLMVariant::Base3B,
            StableLMVariant::Base7B,
            StableLMVariant::Zephyr3B,
            StableLMVariant::Code3B,
            StableLMVariant::V2_1_6B,
            StableLMVariant::V2_12B,
        ] {
            let config = variant.config();
            assert!(
                config.validate().is_ok(),
                "{:?} failed validation: {:?}",
                variant,
                config.validate()
            );
        }
    }

    #[test]
    fn test_variant_parameter_counts_ordered() {
        let p_1_6b = StableLMVariant::V2_1_6B.parameter_count();
        let p_3b = StableLMVariant::Base3B.parameter_count();
        let p_7b = StableLMVariant::Base7B.parameter_count();
        let p_12b = StableLMVariant::V2_12B.parameter_count();
        assert!(p_1_6b < p_3b, "1.6B must have fewer params than 3B");
        assert!(p_3b < p_7b, "3B must have fewer params than 7B");
        assert!(p_7b < p_12b, "7B must have fewer params than 12B");
    }

    // ---- Context length ----

    #[test]
    fn test_context_length_at_least_4096() {
        for variant in [
            StableLMVariant::Base3B,
            StableLMVariant::Base7B,
            StableLMVariant::V2_1_6B,
        ] {
            let config = variant.config();
            assert!(
                config.max_position_embeddings >= 4096,
                "{:?} context length {} < 4096",
                variant,
                config.max_position_embeddings
            );
        }
    }

    // ---- Vocab size ----

    #[test]
    fn test_vocab_size_nonzero() {
        for variant in [
            StableLMVariant::Base3B,
            StableLMVariant::V2_1_6B,
            StableLMVariant::V2_12B,
        ] {
            let config = variant.config();
            assert!(config.vocab_size > 0, "{:?} has zero vocab size", variant);
        }
    }

    #[test]
    fn test_v2_vocab_larger_than_v1() {
        // V2 models use a larger vocabulary (100352 vs 50432)
        let v1_config = StableLMVariant::Base3B.config();
        let v2_config = StableLMVariant::V2_1_6B.config();
        assert!(
            v2_config.vocab_size > v1_config.vocab_size,
            "V2 vocab ({}) must exceed V1 vocab ({})",
            v2_config.vocab_size,
            v1_config.vocab_size
        );
    }

    // ---- Model name lookup ----

    #[test]
    fn test_from_pretrained_name_returns_none_for_unknown() {
        assert!(from_pretrained_name("this-model-does-not-exist").is_none());
    }

    #[test]
    fn test_model_names_are_unique() {
        let names = [
            StableLMVariant::Base3B.model_name(),
            StableLMVariant::Base7B.model_name(),
            StableLMVariant::Zephyr3B.model_name(),
            StableLMVariant::Code3B.model_name(),
            StableLMVariant::V2_1_6B.model_name(),
            StableLMVariant::V2_12B.model_name(),
        ];
        let mut deduped = names.to_vec();
        deduped.sort_unstable();
        deduped.dedup();
        assert_eq!(deduped.len(), names.len(), "All model names must be unique");
    }

    // ---- Equality & Clone ----

    #[test]
    fn test_variant_eq() {
        assert_eq!(StableLMVariant::Base3B, StableLMVariant::Base3B);
        assert_ne!(StableLMVariant::Base3B, StableLMVariant::Base7B);
    }

    #[test]
    fn test_config_clone() {
        let config = StableLMVariant::Base3B.config();
        let cloned = config.clone();
        assert_eq!(config.hidden_size, cloned.hidden_size);
        assert_eq!(config.partial_rotary_factor, cloned.partial_rotary_factor);
    }
}