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
use serde::{Deserialize, Serialize};
use trustformers_core::traits::Config;

/// RoPE position scaling strategy for long-context CodeLlama variants
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum RopeScalingType {
    /// Linear position scaling: divide all frequencies by `factor`.
    ///
    /// `freq_scaled = base_freq / factor`
    ///
    /// Used by CodeLlama-34B to extend context to 100K tokens.
    Linear,
    /// Dynamic NTK-aware scaling: increase the RoPE base period.
    ///
    /// `base_freq = (alpha * theta)^(-2i/d)`, where `alpha = factor`
    ///
    /// Maintains quality at long range better than linear scaling.
    Dynamic,
}

impl std::fmt::Display for RopeScalingType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RopeScalingType::Linear => write!(f, "linear"),
            RopeScalingType::Dynamic => write!(f, "dynamic"),
        }
    }
}

/// RoPE scaling configuration for long-context variants
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RopeScalingConfig {
    /// Scaling strategy (linear or dynamic NTK)
    pub scaling_type: RopeScalingType,
    /// Multiplicative scale factor (e.g. 4.0 for 4× context extension)
    pub factor: f32,
}

/// CodeLlama model configuration
///
/// CodeLlama is a family of code-optimised models built by fine-tuning LLaMA-2
/// on code-heavy data (Meta AI, 2023).  The architecture is identical to
/// LLaMA-2, but adds:
///
/// * **Optional RoPE scaling**: CodeLlama-34B supports up to 100K tokens via
///   linear frequency scaling.
/// * **Fill-in-the-Middle (FIM) / infilling**: Models trained with the
///   `<FILL_ME>` sentinel token.
/// * **Programming language metadata**: tracks which languages the model was
///   trained on (informational).
///
/// Reference: "Code Llama: Open Foundation Models for Code" (Rozière et al., 2023)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeLlamaConfig {
    // ── Architecture (identical to LLaMA-2) ──────────────────────────────────
    /// Hidden dimension
    pub hidden_size: usize,
    /// FFN intermediate dimension
    pub intermediate_size: usize,
    /// Number of decoder layers
    pub num_hidden_layers: usize,
    /// Number of query attention heads
    pub num_attention_heads: usize,
    /// Number of KV heads (GQA when < num_attention_heads)
    pub num_key_value_heads: usize,
    /// Vocabulary size (32016 for most CodeLlama variants)
    pub vocab_size: usize,
    /// Maximum sequence length
    pub max_position_embeddings: usize,
    /// RoPE base frequency
    pub rope_theta: f32,
    /// RMSNorm epsilon
    pub rms_norm_eps: f64,
    /// Whether to use attention bias
    pub attention_bias: bool,
    /// Whether to use MLP bias
    pub mlp_bias: bool,
    /// BOS token ID
    pub bos_token_id: u32,
    /// EOS token ID
    pub eos_token_id: u32,
    /// Whether to use KV cache
    pub use_cache: bool,
    /// Optional pad token ID
    pub pad_token_id: Option<u32>,

    // ── CodeLlama-specific ────────────────────────────────────────────────────
    /// Optional RoPE scaling for long-context variants (e.g. 34B / 100K)
    pub rope_scaling: Option<RopeScalingConfig>,
    /// Whether the model supports Fill-in-the-Middle (FIM) infilling
    pub infilling: bool,
    /// Programming languages included in the training mix (informational)
    pub programming_languages: Vec<String>,
}

impl Default for CodeLlamaConfig {
    fn default() -> Self {
        Self {
            hidden_size: 4096,
            intermediate_size: 11008,
            num_hidden_layers: 32,
            num_attention_heads: 32,
            num_key_value_heads: 32,
            vocab_size: 32016, // CodeLlama extends LLaMA-2 vocab slightly
            max_position_embeddings: 16384,
            rope_theta: 10000.0,
            rms_norm_eps: 1e-5,
            attention_bias: false,
            mlp_bias: false,
            bos_token_id: 1,
            eos_token_id: 2,
            use_cache: true,
            pad_token_id: None,
            rope_scaling: None,
            infilling: true,
            programming_languages: vec![
                "python".to_string(),
                "java".to_string(),
                "c++".to_string(),
                "javascript".to_string(),
                "rust".to_string(),
            ],
        }
    }
}

impl Config for CodeLlamaConfig {
    fn validate(&self) -> trustformers_core::errors::Result<()> {
        if !self.hidden_size.is_multiple_of(self.num_attention_heads) {
            return Err(
                trustformers_core::errors::TrustformersError::invalid_config(
                    "hidden_size must be divisible by num_attention_heads".to_string(),
                ),
            );
        }
        if !self.num_attention_heads.is_multiple_of(self.num_key_value_heads) {
            return Err(
                trustformers_core::errors::TrustformersError::invalid_config(
                    "num_attention_heads must be divisible by num_key_value_heads".to_string(),
                ),
            );
        }
        if self.vocab_size == 0 {
            return Err(
                trustformers_core::errors::TrustformersError::invalid_config(
                    "vocab_size must be greater than 0".to_string(),
                ),
            );
        }
        if let Some(ref scaling) = self.rope_scaling {
            if scaling.factor <= 0.0 {
                return Err(
                    trustformers_core::errors::TrustformersError::invalid_config(
                        "rope_scaling.factor must be positive".to_string(),
                    ),
                );
            }
        }
        Ok(())
    }

    fn architecture(&self) -> &'static str {
        "CodeLlama"
    }
}

impl CodeLlamaConfig {
    /// Compute head dimension
    pub fn head_dim(&self) -> usize {
        self.hidden_size / self.num_attention_heads
    }

    /// Number of query groups per KV head
    pub fn num_query_groups(&self) -> usize {
        self.num_attention_heads / self.num_key_value_heads
    }

    /// Whether this variant uses GQA
    pub fn uses_gqa(&self) -> bool {
        self.num_key_value_heads < self.num_attention_heads
    }

    /// Effective maximum context after optional RoPE scaling
    pub fn effective_max_context(&self) -> usize {
        match &self.rope_scaling {
            Some(s) => (self.max_position_embeddings as f32 * s.factor) as usize,
            None => self.max_position_embeddings,
        }
    }

    // ── Named presets ─────────────────────────────────────────────────────────

    /// CodeLlama-7B — general-purpose code model (no infilling)
    pub fn codellama_7b() -> Self {
        Self {
            hidden_size: 4096,
            intermediate_size: 11008,
            num_hidden_layers: 32,
            num_attention_heads: 32,
            num_key_value_heads: 32,
            max_position_embeddings: 16384,
            infilling: false,
            ..Self::default()
        }
    }

    /// CodeLlama-13B
    pub fn codellama_13b() -> Self {
        Self {
            hidden_size: 5120,
            intermediate_size: 13824,
            num_hidden_layers: 40,
            num_attention_heads: 40,
            num_key_value_heads: 40,
            max_position_embeddings: 16384,
            infilling: false,
            ..Self::default()
        }
    }

    /// CodeLlama-34B — linear RoPE scaling for 100K token context
    ///
    /// The scaling factor of 4.0 extends the native 16384 training context
    /// to ~100K tokens via linear frequency division.
    pub fn codellama_34b() -> Self {
        Self {
            hidden_size: 8192,
            intermediate_size: 22016,
            num_hidden_layers: 48,
            num_attention_heads: 64,
            num_key_value_heads: 8, // GQA
            max_position_embeddings: 16384,
            rope_scaling: Some(RopeScalingConfig {
                scaling_type: RopeScalingType::Linear,
                factor: 4.0, // 16384 * 4 ≈ 65536; further extrapolation to 100K
            }),
            infilling: false,
            ..Self::default()
        }
    }

    /// CodeLlama-70B — 4K native context, repository-level tasks
    pub fn codellama_70b() -> Self {
        Self {
            hidden_size: 8192,
            intermediate_size: 28672,
            num_hidden_layers: 80,
            num_attention_heads: 64,
            num_key_value_heads: 8, // GQA
            max_position_embeddings: 4096,
            infilling: false,
            ..Self::default()
        }
    }

    /// CodeLlama-7B-Instruct — instruction-tuned for chat-style code assistance
    pub fn codellama_7b_instruct() -> Self {
        Self {
            infilling: true, // Instruct variant keeps FIM capability
            ..Self::codellama_7b()
        }
    }

    /// Create from HuggingFace model name
    pub fn from_pretrained_name(name: &str) -> Option<Self> {
        match name {
            "codellama/CodeLlama-7b-hf" | "codellama-7b" => Some(Self::codellama_7b()),
            "codellama/CodeLlama-13b-hf" | "codellama-13b" => Some(Self::codellama_13b()),
            "codellama/CodeLlama-34b-hf" | "codellama-34b" => Some(Self::codellama_34b()),
            "codellama/CodeLlama-70b-hf" | "codellama-70b" => Some(Self::codellama_70b()),
            "codellama/CodeLlama-7b-Instruct-hf" | "codellama-7b-instruct" => {
                Some(Self::codellama_7b_instruct())
            },
            _ => None,
        }
    }
}

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

    #[test]
    fn test_codellama_default_vocab_size() {
        let cfg = CodeLlamaConfig::default();
        assert_eq!(cfg.vocab_size, 32016);
    }

    #[test]
    fn test_codellama_default_hidden_size() {
        let cfg = CodeLlamaConfig::default();
        assert_eq!(cfg.hidden_size, 4096);
    }

    #[test]
    fn test_codellama_default_num_hidden_layers() {
        let cfg = CodeLlamaConfig::default();
        assert_eq!(cfg.num_hidden_layers, 32);
    }

    #[test]
    fn test_codellama_default_num_attention_heads() {
        let cfg = CodeLlamaConfig::default();
        assert_eq!(cfg.num_attention_heads, 32);
    }

    #[test]
    fn test_codellama_default_max_position_embeddings() {
        let cfg = CodeLlamaConfig::default();
        assert_eq!(cfg.max_position_embeddings, 16384);
    }

    #[test]
    fn test_codellama_default_infilling() {
        let cfg = CodeLlamaConfig::default();
        assert!(cfg.infilling);
    }

    #[test]
    fn test_codellama_default_programming_languages() {
        let cfg = CodeLlamaConfig::default();
        assert!(!cfg.programming_languages.is_empty());
        assert!(cfg.programming_languages.contains(&"python".to_string()));
    }

    #[test]
    fn test_codellama_default_rope_scaling_none() {
        let cfg = CodeLlamaConfig::default();
        assert!(cfg.rope_scaling.is_none());
    }

    #[test]
    fn test_codellama_validate_passes_default() {
        let cfg = CodeLlamaConfig::default();
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn test_codellama_validate_fails_zero_vocab_size() {
        let cfg = CodeLlamaConfig {
            vocab_size: 0,
            ..CodeLlamaConfig::default()
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn test_codellama_validate_fails_hidden_not_divisible_by_heads() {
        let cfg = CodeLlamaConfig {
            hidden_size: 4096,
            num_attention_heads: 7,
            num_key_value_heads: 7,
            ..CodeLlamaConfig::default()
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn test_codellama_validate_fails_heads_not_divisible_by_kv_heads() {
        let cfg = CodeLlamaConfig {
            num_attention_heads: 32,
            num_key_value_heads: 7,
            ..CodeLlamaConfig::default()
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn test_codellama_7b_preset() {
        let cfg = CodeLlamaConfig::codellama_7b();
        assert_eq!(cfg.hidden_size, 4096);
        assert_eq!(cfg.num_hidden_layers, 32);
        assert!(!cfg.infilling);
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn test_codellama_13b_preset() {
        let cfg = CodeLlamaConfig::codellama_13b();
        assert_eq!(cfg.hidden_size, 5120);
        assert_eq!(cfg.num_hidden_layers, 40);
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn test_codellama_34b_preset_with_rope_scaling() {
        let cfg = CodeLlamaConfig::codellama_34b();
        assert_eq!(cfg.hidden_size, 8192);
        assert_eq!(cfg.num_key_value_heads, 8);
        assert!(cfg.rope_scaling.is_some());
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn test_codellama_34b_rope_scaling_type() {
        let cfg = CodeLlamaConfig::codellama_34b();
        if let Some(ref scaling) = cfg.rope_scaling {
            assert_eq!(scaling.scaling_type, RopeScalingType::Linear);
            assert!((scaling.factor - 4.0).abs() < 1e-6);
        }
    }

    #[test]
    fn test_codellama_70b_preset() {
        let cfg = CodeLlamaConfig::codellama_70b();
        assert_eq!(cfg.hidden_size, 8192);
        assert_eq!(cfg.num_hidden_layers, 80);
        assert_eq!(cfg.num_key_value_heads, 8);
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn test_codellama_head_dim() {
        let cfg = CodeLlamaConfig::codellama_7b();
        assert_eq!(cfg.head_dim(), 4096 / 32);
    }

    #[test]
    fn test_codellama_uses_gqa_7b_false() {
        let cfg = CodeLlamaConfig::codellama_7b();
        assert!(!cfg.uses_gqa());
    }

    #[test]
    fn test_codellama_uses_gqa_34b_true() {
        let cfg = CodeLlamaConfig::codellama_34b();
        assert!(cfg.uses_gqa());
    }

    #[test]
    fn test_codellama_effective_max_context_no_scaling() {
        let cfg = CodeLlamaConfig::codellama_7b();
        assert_eq!(cfg.effective_max_context(), cfg.max_position_embeddings);
    }

    #[test]
    fn test_codellama_effective_max_context_with_scaling() {
        let cfg = CodeLlamaConfig::codellama_34b();
        let effective = cfg.effective_max_context();
        assert!(effective > cfg.max_position_embeddings);
    }

    #[test]
    fn test_codellama_from_pretrained_name_7b() {
        let cfg = CodeLlamaConfig::from_pretrained_name("codellama-7b");
        assert!(cfg.is_some());
    }

    #[test]
    fn test_codellama_from_pretrained_name_unknown() {
        let cfg = CodeLlamaConfig::from_pretrained_name("unknown");
        assert!(cfg.is_none());
    }

    #[test]
    fn test_codellama_architecture_name() {
        let cfg = CodeLlamaConfig::default();
        assert_eq!(cfg.architecture(), "CodeLlama");
    }

    #[test]
    fn test_codellama_7b_instruct_preserves_infilling() {
        let cfg = CodeLlamaConfig::codellama_7b_instruct();
        assert!(cfg.infilling);
    }

    #[test]
    fn test_codellama_lcg_values_in_range() {
        let mut s = 42u64;
        s = s.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
        let v = (s % 1000) as f32 / 1000.0;
        assert!((0.0..1.0).contains(&v));
    }
}