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

/// LLaMA-2 model configuration
///
/// LLaMA-2 is the successor to LLaMA-1, featuring:
/// - Grouped Query Attention (GQA) for efficient inference
/// - Extended context window of 4096 tokens
/// - RLHF-fine-tuned chat variants
///
/// Reference: "Llama 2: Open Foundation and Fine-Tuned Chat Models"
///            (Touvron et al., Meta AI, 2023)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LLaMA2Config {
    /// Vocabulary size (32000 for LLaMA-2)
    pub hidden_size: usize,
    /// Intermediate (FFN) hidden dimension
    pub intermediate_size: usize,
    /// Number of transformer decoder layers
    pub num_hidden_layers: usize,
    /// Number of query attention heads
    pub num_attention_heads: usize,
    /// Number of key/value attention heads — the GQA parameter
    ///
    /// When `num_key_value_heads < num_attention_heads`, the model uses Grouped
    /// Query Attention where each KV head is shared among
    /// `num_attention_heads / num_key_value_heads` query heads.
    pub num_key_value_heads: usize,
    /// Vocabulary size
    pub vocab_size: usize,
    /// Maximum sequence length (4096 for LLaMA-2, vs 2048 for LLaMA-1)
    pub max_position_embeddings: usize,
    /// Base frequency for Rotary Position Embeddings
    pub rope_theta: f32,
    /// Epsilon for RMSNorm stability
    pub rms_norm_eps: f64,
    /// Hidden activation function (always "silu" for LLaMA-2)
    pub hidden_act: String,
    /// Tensor parallelism degree (1 = no sharding, for metadata only)
    pub pretraining_tp: usize,
    /// Whether to use attention bias
    pub attention_bias: bool,
    /// Whether to use MLP bias
    pub mlp_bias: bool,
    /// Beginning-of-sequence token ID
    pub bos_token_id: u32,
    /// End-of-sequence token ID
    pub eos_token_id: u32,
    /// Whether to use KV cache during generation
    pub use_cache: bool,
    /// Optional pad token ID
    pub pad_token_id: Option<u32>,
}

impl Default for LLaMA2Config {
    fn default() -> Self {
        Self {
            hidden_size: 4096,
            intermediate_size: 11008,
            num_hidden_layers: 32,
            num_attention_heads: 32,
            num_key_value_heads: 32, // Full MHA by default
            vocab_size: 32000,
            max_position_embeddings: 4096,
            rope_theta: 10000.0,
            rms_norm_eps: 1e-5,
            hidden_act: "silu".to_string(),
            pretraining_tp: 1,
            attention_bias: false,
            mlp_bias: false,
            bos_token_id: 1,
            eos_token_id: 2,
            use_cache: true,
            pad_token_id: None,
        }
    }
}

impl Config for LLaMA2Config {
    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 self.num_hidden_layers == 0 {
            return Err(
                trustformers_core::errors::TrustformersError::invalid_config(
                    "num_hidden_layers must be greater than 0".to_string(),
                ),
            );
        }
        if self.pretraining_tp == 0 {
            return Err(
                trustformers_core::errors::TrustformersError::invalid_config(
                    "pretraining_tp must be at least 1".to_string(),
                ),
            );
        }
        Ok(())
    }

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

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

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

    /// Whether this config uses Grouped Query Attention
    pub fn uses_gqa(&self) -> bool {
        self.num_key_value_heads < self.num_attention_heads
    }

    /// LLaMA-2 7B — 32 Q heads, 32 KV heads (standard MHA, no GQA sharing)
    ///
    /// Paper: Table 1, 7B model
    pub fn llama2_7b() -> Self {
        Self {
            hidden_size: 4096,
            intermediate_size: 11008,
            num_hidden_layers: 32,
            num_attention_heads: 32,
            num_key_value_heads: 32, // No KV sharing
            vocab_size: 32000,
            max_position_embeddings: 4096,
            ..Self::default()
        }
    }

    /// LLaMA-2 13B — 40 Q heads, 40 KV heads (standard MHA)
    ///
    /// Paper: Table 1, 13B model
    pub fn llama2_13b() -> Self {
        Self {
            hidden_size: 5120,
            intermediate_size: 13824,
            num_hidden_layers: 40,
            num_attention_heads: 40,
            num_key_value_heads: 40, // No KV sharing
            vocab_size: 32000,
            max_position_embeddings: 4096,
            ..Self::default()
        }
    }

    /// LLaMA-2 70B — 64 Q heads, 8 KV heads (GQA with 8x sharing)
    ///
    /// Paper: Table 1, 70B model — the first LLaMA variant to use GQA in production
    pub fn llama2_70b() -> Self {
        Self {
            hidden_size: 8192,
            intermediate_size: 28672,
            num_hidden_layers: 80,
            num_attention_heads: 64,
            num_key_value_heads: 8, // GQA: each KV head serves 8 Q heads
            vocab_size: 32000,
            max_position_embeddings: 4096,
            ..Self::default()
        }
    }

    /// LLaMA-2-chat 7B — instruction-tuned via RLHF
    pub fn llama2_7b_chat() -> Self {
        Self::llama2_7b()
    }

    /// LLaMA-2-chat 13B — instruction-tuned via RLHF
    pub fn llama2_13b_chat() -> Self {
        Self::llama2_13b()
    }

    /// LLaMA-2-chat 70B — instruction-tuned via RLHF
    pub fn llama2_70b_chat() -> Self {
        Self::llama2_70b()
    }

    /// Create configuration by HuggingFace model name
    pub fn from_pretrained_name(name: &str) -> Option<Self> {
        match name {
            "meta-llama/Llama-2-7b-hf" | "llama2-7b" => Some(Self::llama2_7b()),
            "meta-llama/Llama-2-13b-hf" | "llama2-13b" => Some(Self::llama2_13b()),
            "meta-llama/Llama-2-70b-hf" | "llama2-70b" => Some(Self::llama2_70b()),
            "meta-llama/Llama-2-7b-chat-hf" | "llama2-7b-chat" => Some(Self::llama2_7b_chat()),
            "meta-llama/Llama-2-13b-chat-hf" | "llama2-13b-chat" => Some(Self::llama2_13b_chat()),
            "meta-llama/Llama-2-70b-chat-hf" | "llama2-70b-chat" => Some(Self::llama2_70b_chat()),
            _ => None,
        }
    }
}

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

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

    #[test]
    fn test_llama2_default_intermediate_size() {
        let cfg = LLaMA2Config::default();
        assert_eq!(cfg.intermediate_size, 11008);
    }

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

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

    #[test]
    fn test_llama2_default_num_key_value_heads() {
        let cfg = LLaMA2Config::default();
        assert_eq!(cfg.num_key_value_heads, 32);
    }

    #[test]
    fn test_llama2_default_vocab_size() {
        let cfg = LLaMA2Config::default();
        assert_eq!(cfg.vocab_size, 32000);
    }

    #[test]
    fn test_llama2_default_max_position_embeddings() {
        let cfg = LLaMA2Config::default();
        assert_eq!(cfg.max_position_embeddings, 4096);
    }

    #[test]
    fn test_llama2_default_hidden_act() {
        let cfg = LLaMA2Config::default();
        assert_eq!(cfg.hidden_act, "silu");
    }

    #[test]
    fn test_llama2_default_use_cache() {
        let cfg = LLaMA2Config::default();
        assert!(cfg.use_cache);
    }

    #[test]
    fn test_llama2_default_pad_token_id_none() {
        let cfg = LLaMA2Config::default();
        assert!(cfg.pad_token_id.is_none());
    }

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

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

    #[test]
    fn test_llama2_validate_fails_zero_num_hidden_layers() {
        let cfg = LLaMA2Config {
            num_hidden_layers: 0,
            ..LLaMA2Config::default()
        };
        assert!(cfg.validate().is_err());
    }

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

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

    #[test]
    fn test_llama2_validate_fails_zero_pretraining_tp() {
        let cfg = LLaMA2Config {
            pretraining_tp: 0,
            ..LLaMA2Config::default()
        };
        assert!(cfg.validate().is_err());
    }

    #[test]
    fn test_llama2_7b_preset() {
        let cfg = LLaMA2Config::llama2_7b();
        assert_eq!(cfg.hidden_size, 4096);
        assert_eq!(cfg.num_hidden_layers, 32);
        assert_eq!(cfg.num_attention_heads, 32);
        assert_eq!(cfg.num_key_value_heads, 32);
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn test_llama2_13b_preset() {
        let cfg = LLaMA2Config::llama2_13b();
        assert_eq!(cfg.hidden_size, 5120);
        assert_eq!(cfg.num_hidden_layers, 40);
        assert_eq!(cfg.num_attention_heads, 40);
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn test_llama2_70b_preset_gqa() {
        let cfg = LLaMA2Config::llama2_70b();
        assert_eq!(cfg.hidden_size, 8192);
        assert_eq!(cfg.num_attention_heads, 64);
        assert_eq!(cfg.num_key_value_heads, 8);
        assert!(cfg.uses_gqa());
        assert!(cfg.validate().is_ok());
    }

    #[test]
    fn test_llama2_head_dim() {
        let cfg = LLaMA2Config::llama2_7b();
        assert_eq!(cfg.head_dim(), 4096 / 32);
    }

    #[test]
    fn test_llama2_num_query_groups_no_gqa() {
        let cfg = LLaMA2Config::llama2_7b();
        assert_eq!(cfg.num_query_groups(), 1);
    }

    #[test]
    fn test_llama2_num_query_groups_with_gqa() {
        let cfg = LLaMA2Config::llama2_70b();
        assert_eq!(cfg.num_query_groups(), 64 / 8);
    }

    #[test]
    fn test_llama2_uses_gqa_false_for_7b() {
        let cfg = LLaMA2Config::llama2_7b();
        assert!(!cfg.uses_gqa());
    }

    #[test]
    fn test_llama2_from_pretrained_name_7b() {
        let cfg = LLaMA2Config::from_pretrained_name("llama2-7b");
        assert!(cfg.is_some());
        let c = cfg.unwrap_or_default();
        assert_eq!(c.hidden_size, 4096);
    }

    #[test]
    fn test_llama2_from_pretrained_name_unknown() {
        let cfg = LLaMA2Config::from_pretrained_name("unknown-model");
        assert!(cfg.is_none());
    }

    #[test]
    fn test_llama2_chat_variant_same_arch_as_base() {
        let base = LLaMA2Config::llama2_7b();
        let chat = LLaMA2Config::llama2_7b_chat();
        assert_eq!(base.hidden_size, chat.hidden_size);
        assert_eq!(base.num_hidden_layers, chat.num_hidden_layers);
    }

    #[test]
    fn test_llama2_bos_eos_token_ids() {
        let cfg = LLaMA2Config::default();
        assert_eq!(cfg.bos_token_id, 1);
        assert_eq!(cfg.eos_token_id, 2);
    }

    #[test]
    fn test_llama2_architecture_name() {
        let cfg = LLaMA2Config::default();
        assert_eq!(cfg.architecture(), "LLaMA-2");
    }

    #[test]
    fn test_llama2_lcg_derived_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));
    }

    #[test]
    fn test_llama2_pretraining_tp_default() {
        let cfg = LLaMA2Config::default();
        assert_eq!(cfg.pretraining_tp, 1);
    }

    #[test]
    fn test_llama2_attention_and_mlp_bias_default_false() {
        let cfg = LLaMA2Config::default();
        assert!(!cfg.attention_bias);
        assert!(!cfg.mlp_bias);
    }
}