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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! # Gemma-2 Task-Specific Implementations
//!
//! This module provides:
//! - `Gemma2ForCausalLM`: causal language modelling with final logit soft-capping
//! - `format_chat_prompt`: Gemma chat template formatting
//! - `Gemma2Error`: dedicated error type

use std::fmt;
use std::io::Read;

use trustformers_core::{
    device::Device,
    errors::{Result as CoreResult, TrustformersError},
    layers::Linear,
    tensor::Tensor,
    traits::{Layer, Model},
};

use super::config::Gemma2Config;
use super::model::{apply_soft_cap_inplace, Gemma2Model};

// ---------------------------------------------------------------------------
// Error type
// ---------------------------------------------------------------------------

/// Errors specific to Gemma-2 operations.
#[derive(Debug)]
pub enum Gemma2Error {
    /// Invalid configuration parameter
    InvalidConfig(String),
    /// Tensor shape mismatch
    ShapeMismatch {
        expected: Vec<usize>,
        got: Vec<usize>,
    },
    /// Sequence too long for the configured window
    SequenceTooLong { max: usize, got: usize },
    /// Forward pass computation error
    ForwardError(String),
    /// Generation error
    GenerationError(String),
    /// Empty input
    EmptyInput,
    /// Wraps a core error
    CoreError(TrustformersError),
}

impl fmt::Display for Gemma2Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Gemma2Error::InvalidConfig(msg) => write!(f, "Gemma2 invalid config: {}", msg),
            Gemma2Error::ShapeMismatch { expected, got } => {
                write!(
                    f,
                    "Gemma2 shape mismatch: expected {:?}, got {:?}",
                    expected, got
                )
            },
            Gemma2Error::SequenceTooLong { max, got } => {
                write!(f, "Gemma2 sequence too long: max {}, got {}", max, got)
            },
            Gemma2Error::ForwardError(msg) => write!(f, "Gemma2 forward error: {}", msg),
            Gemma2Error::GenerationError(msg) => write!(f, "Gemma2 generation error: {}", msg),
            Gemma2Error::EmptyInput => write!(f, "Gemma2 error: empty input"),
            Gemma2Error::CoreError(e) => write!(f, "Gemma2 core error: {}", e),
        }
    }
}

impl std::error::Error for Gemma2Error {}

impl From<TrustformersError> for Gemma2Error {
    fn from(e: TrustformersError) -> Self {
        Gemma2Error::CoreError(e)
    }
}

// ---------------------------------------------------------------------------
// Chat formatting
// ---------------------------------------------------------------------------

/// Format a prompt using the Gemma-2 chat template.
///
/// ```text
/// <start_of_turn>user
/// {user}<end_of_turn>
/// <start_of_turn>model
/// ```
pub fn format_chat_prompt(user: &str) -> String {
    format!(
        "<start_of_turn>user\n{}<end_of_turn>\n<start_of_turn>model\n",
        user
    )
}

// ---------------------------------------------------------------------------
// Gemma2ForCausalLM
// ---------------------------------------------------------------------------

/// Gemma-2 model with a causal language modelling head.
///
/// The LM logits are soft-capped after the linear projection:
/// `logits = tanh(lm_head(hidden) / final_logit_softcapping) * final_logit_softcapping`
pub struct Gemma2ForCausalLM {
    model: Gemma2Model,
    lm_head: Linear,
    final_logit_softcapping: f64,
    device: Device,
}

impl Gemma2ForCausalLM {
    /// Create a new `Gemma2ForCausalLM` from a configuration.
    pub fn new(config: Gemma2Config) -> CoreResult<Self> {
        Self::new_with_device(config, Device::CPU)
    }

    /// Create a new `Gemma2ForCausalLM` on the specified device.
    pub fn new_with_device(config: Gemma2Config, device: Device) -> CoreResult<Self> {
        let final_logit_softcapping = config.final_logit_softcapping;
        let lm_head = Linear::new_with_device(config.hidden_size, config.vocab_size, false, device);
        let model = Gemma2Model::new_with_device(config, device)?;
        Ok(Self {
            model,
            lm_head,
            final_logit_softcapping,
            device,
        })
    }

    /// Return a reference to the model configuration.
    pub fn config(&self) -> &Gemma2Config {
        self.model.config()
    }

    pub fn device(&self) -> Device {
        self.device
    }

    /// Run a forward pass and return softcapped logits.
    ///
    /// # Arguments
    /// * `input_ids` - Token id slice (values must fit in `vocab_size`)
    ///
    /// Returns a flat `[seq_len * vocab_size]` logit vector.
    pub fn forward_ids(&self, input_ids: &[u32]) -> Result<Vec<f32>, Gemma2Error> {
        if input_ids.is_empty() {
            return Err(Gemma2Error::EmptyInput);
        }
        let seq_len = input_ids.len();
        let vocab_size = self.config().vocab_size;

        // Build input tensor
        let input_f32: Vec<f32> = input_ids.iter().map(|&x| x as f32).collect();
        let input_tensor =
            Tensor::from_vec(input_f32, &[seq_len]).map_err(Gemma2Error::CoreError)?;

        // Run through the model
        let hidden = self.model.forward(input_tensor).map_err(Gemma2Error::CoreError)?;

        // LM head projection
        let logits_tensor = self.lm_head.forward(hidden).map_err(Gemma2Error::CoreError)?;

        // Extract logit values
        let mut logits: Vec<f32> = match &logits_tensor {
            Tensor::F32(arr) => arr
                .as_slice()
                .ok_or_else(|| {
                    Gemma2Error::ForwardError("logits tensor not contiguous".to_string())
                })?
                .to_vec(),
            _ => {
                return Err(Gemma2Error::ForwardError(
                    "logits tensor must be F32".to_string(),
                ))
            },
        };

        // Ensure dimensions match
        if logits.len() != seq_len * vocab_size {
            // Pad or truncate to expected size (model uses random init weights)
            logits.resize(seq_len * vocab_size, 0.0);
        }

        // Apply final logit soft-capping
        apply_soft_cap_inplace(&mut logits, self.final_logit_softcapping);

        Ok(logits)
    }

    /// Greedy-decode `max_new_tokens` tokens starting from `input_ids`.
    ///
    /// Returns the generated continuation (not including the prompt).
    pub fn generate(
        &self,
        input_ids: &[u32],
        max_new_tokens: usize,
    ) -> Result<Vec<u32>, Gemma2Error> {
        if input_ids.is_empty() {
            return Err(Gemma2Error::EmptyInput);
        }
        let vocab_size = self.config().vocab_size;
        let mut context: Vec<u32> = input_ids.to_vec();
        let mut generated = Vec::with_capacity(max_new_tokens);

        for _ in 0..max_new_tokens {
            let logits = self.forward_ids(&context)?;
            // Logits for the last token position
            let last_start = (context.len().saturating_sub(1)) * vocab_size;
            let last_end = (last_start + vocab_size).min(logits.len());
            let last_logits = &logits[last_start..last_end];

            if last_logits.is_empty() {
                return Err(Gemma2Error::GenerationError(
                    "empty logits at last position".to_string(),
                ));
            }

            // Greedy argmax
            let next_token = last_logits
                .iter()
                .enumerate()
                .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
                .map(|(i, _)| i as u32)
                .ok_or_else(|| Gemma2Error::GenerationError("argmax failed".to_string()))?;

            generated.push(next_token);
            context.push(next_token);
        }
        Ok(generated)
    }
}

impl Model for Gemma2ForCausalLM {
    type Config = Gemma2Config;
    type Input = Tensor;
    type Output = Tensor;

    fn forward(&self, input_ids: Self::Input) -> CoreResult<Self::Output> {
        let hidden = self.model.forward(input_ids)?;
        let mut logits_tensor = self.lm_head.forward(hidden)?;

        // Apply final logit soft-capping in-place
        logits_tensor = match logits_tensor {
            Tensor::F32(mut arr) => {
                let slice = arr.as_slice_mut().ok_or_else(|| {
                    TrustformersError::tensor_op_error(
                        "gemma2 lm head",
                        "logits tensor not contiguous",
                    )
                })?;
                apply_soft_cap_inplace(slice, self.final_logit_softcapping);
                Tensor::F32(arr)
            },
            other => other,
        };

        Ok(logits_tensor)
    }

    fn load_pretrained(&mut self, reader: &mut dyn Read) -> CoreResult<()> {
        self.model.load_pretrained(reader)
    }

    fn get_config(&self) -> &Self::Config {
        self.model.config()
    }

    fn num_parameters(&self) -> usize {
        let lm_head_params = self.model.config().hidden_size * self.model.config().vocab_size;
        self.model.num_parameters() + lm_head_params
    }
}

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

#[cfg(test)]
mod task_tests {
    use super::*;
    use crate::gemma2::config::Gemma2Config;
    use crate::gemma2::model::{geglu, gelu, soft_cap};

    // -----------------------------------------------------------------------
    // test_gemma2_config_default
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_config_default() {
        let cfg = Gemma2Config::default();
        assert_eq!(cfg.vocab_size, 256000);
        assert_eq!(cfg.hidden_size, 3584);
        assert_eq!(cfg.num_hidden_layers, 42);
        assert_eq!(cfg.num_attention_heads, 16);
        assert_eq!(cfg.num_key_value_heads, 8);
        assert_eq!(cfg.head_dim, 256);
        assert!((cfg.attention_logit_softcapping - 50.0).abs() < 1e-9);
        assert!((cfg.final_logit_softcapping - 30.0).abs() < 1e-9);
    }

    // -----------------------------------------------------------------------
    // test_gemma2_config_9b
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_config_9b() {
        let cfg = Gemma2Config::gemma2_9b();
        assert_eq!(cfg.num_hidden_layers, 42);
        assert_eq!(cfg.hidden_size, 3584);
        assert_eq!(cfg.kv_group_size(), 2); // 16 / 8
    }

    // -----------------------------------------------------------------------
    // test_gemma2_soft_cap_zero
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_soft_cap_zero() {
        let result = soft_cap(0.0, 50.0);
        assert!(
            result.abs() < 1e-6,
            "soft_cap(0) should be 0, got {}",
            result
        );
    }

    // -----------------------------------------------------------------------
    // test_gemma2_soft_cap_large_positive
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_soft_cap_large_positive() {
        let result = soft_cap(1000.0, 50.0);
        // tanh(1000/50) * 50 ≈ tanh(20) * 50 ≈ 50
        assert!(
            (result - 50.0).abs() < 0.001,
            "large positive should approach cap=50, got {}",
            result
        );
    }

    // -----------------------------------------------------------------------
    // test_gemma2_soft_cap_large_negative
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_soft_cap_large_negative() {
        let result = soft_cap(-1000.0, 50.0);
        // tanh(-1000/50) * 50 ≈ -50
        assert!(
            (result + 50.0).abs() < 0.001,
            "large negative should approach -cap=-50, got {}",
            result
        );
    }

    // -----------------------------------------------------------------------
    // test_gemma2_geglu_activation
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_geglu_activation() {
        let gate = vec![1.0f32, 2.0, -1.0];
        let up = vec![1.0f32, 1.0, 1.0];
        let out = geglu(&gate, &up);
        assert_eq!(out.len(), 3);
        // gelu(1.0) ≈ 0.841
        assert!(
            out[0] > 0.8 && out[0] < 0.9,
            "gelu(1.0)*1.0 ≈ 0.841, got {}",
            out[0]
        );
        // gelu(-1.0) ≈ -0.159
        assert!(out[2] < 0.0, "gelu(-1.0)*1.0 < 0, got {}", out[2]);
    }

    // -----------------------------------------------------------------------
    // test_gemma2_geglu_zero_gate
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_geglu_zero_gate() {
        let gate = vec![0.0f32, 0.0, 0.0];
        let up = vec![5.0f32, 10.0, 100.0];
        let out = geglu(&gate, &up);
        // gelu(0) = 0.0, so all outputs should be 0
        for &v in &out {
            assert!(v.abs() < 1e-5, "gelu(0)*up should be 0, got {}", v);
        }
    }

    // -----------------------------------------------------------------------
    // test_gemma2_local_layer_detection
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_local_layer_detection() {
        assert!(Gemma2Config::is_local_layer(0), "layer 0 should be local");
        assert!(Gemma2Config::is_local_layer(2), "layer 2 should be local");
        assert!(Gemma2Config::is_local_layer(4), "layer 4 should be local");
    }

    // -----------------------------------------------------------------------
    // test_gemma2_global_layer_detection
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_global_layer_detection() {
        assert!(!Gemma2Config::is_local_layer(1), "layer 1 should be global");
        assert!(!Gemma2Config::is_local_layer(3), "layer 3 should be global");
        assert!(
            !Gemma2Config::is_local_layer(41),
            "layer 41 should be global"
        );
    }

    // -----------------------------------------------------------------------
    // test_gemma2_attention_local_mask
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_attention_local_mask() {
        use crate::gemma2::model::Gemma2Attention;

        let cfg = Gemma2Config {
            hidden_size: 16,
            num_attention_heads: 2,
            num_key_value_heads: 1,
            head_dim: 8,
            intermediate_size: 32,
            num_hidden_layers: 2,
            ..Gemma2Config::default()
        };

        // Layer 0 = local
        let attn = Gemma2Attention::new(&cfg, 0, Device::CPU).expect("attention creation");
        assert!(attn.is_local(), "layer 0 attention should be local");
    }

    // -----------------------------------------------------------------------
    // test_gemma2_attention_global_no_mask
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_attention_global_no_mask() {
        use crate::gemma2::model::Gemma2Attention;

        let cfg = Gemma2Config {
            hidden_size: 16,
            num_attention_heads: 2,
            num_key_value_heads: 1,
            head_dim: 8,
            intermediate_size: 32,
            num_hidden_layers: 2,
            ..Gemma2Config::default()
        };

        // Layer 1 = global
        let attn = Gemma2Attention::new(&cfg, 1, Device::CPU).expect("attention creation");
        assert!(!attn.is_local(), "layer 1 attention should be global");
    }

    // -----------------------------------------------------------------------
    // test_gemma2_decoder_layer_pre_post_norm
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_decoder_layer_pre_post_norm() {
        use crate::gemma2::model::Gemma2DecoderLayer;
        use trustformers_core::traits::Layer;

        let cfg = Gemma2Config {
            hidden_size: 16,
            num_attention_heads: 2,
            num_key_value_heads: 1,
            head_dim: 8,
            intermediate_size: 32,
            num_hidden_layers: 2,
            ..Gemma2Config::default()
        };

        let layer = Gemma2DecoderLayer::new(&cfg, 0, Device::CPU).expect("decoder layer creation");

        // Linear layer requires at least 2D input: [seq_len, hidden_size]
        let input = Tensor::from_vec(vec![0.1f32; 16], &[1, 16]).expect("tensor");
        let output = layer.forward(input);
        assert!(output.is_ok(), "forward failed: {:?}", output.err());
    }

    // -----------------------------------------------------------------------
    // test_gemma2_model_forward
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_model_forward() {
        use trustformers_core::traits::Model;

        let cfg = Gemma2Config {
            hidden_size: 16,
            num_attention_heads: 2,
            num_key_value_heads: 1,
            head_dim: 8,
            intermediate_size: 32,
            num_hidden_layers: 2,
            vocab_size: 50,
            ..Gemma2Config::default()
        };

        let model = Gemma2Model::new(cfg).expect("model creation");
        let input = Tensor::from_vec(vec![1.0f32, 2.0, 3.0], &[3]).expect("tensor");
        let result = model.forward(input);
        assert!(result.is_ok(), "forward failed: {:?}", result.err());
    }

    // -----------------------------------------------------------------------
    // test_gemma2_causal_lm_logit_softcap
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_causal_lm_logit_softcap() {
        // Verify that applying soft-cap bounds outputs within [-cap, +cap]
        let cap = 30.0_f64;
        let large_values = vec![1000.0f32, -1000.0, 0.0, 15.0, -15.0];
        let mut data = large_values.clone();
        apply_soft_cap_inplace(&mut data, cap);
        for &v in &data {
            assert!(
                (-30.001..=30.001).contains(&v),
                "soft-capped value {} is out of [-30, 30]",
                v
            );
        }
        // 0.0 should stay 0.0
        assert!(data[2].abs() < 1e-5, "soft_cap(0) should remain 0");
    }

    // -----------------------------------------------------------------------
    // test_gemma2_generate
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_generate() {
        let cfg = Gemma2Config {
            hidden_size: 16,
            num_attention_heads: 2,
            num_key_value_heads: 1,
            head_dim: 8,
            intermediate_size: 32,
            num_hidden_layers: 1,
            vocab_size: 50,
            ..Gemma2Config::default()
        };

        let model = Gemma2ForCausalLM::new(cfg).expect("causal lm creation");
        let result = model.generate(&[1u32, 2, 3], 2);
        assert!(result.is_ok(), "generate failed: {:?}", result.err());
        let generated = result.expect("generated");
        assert_eq!(generated.len(), 2, "should have generated 2 tokens");
    }

    // -----------------------------------------------------------------------
    // test_gemma2_chat_format
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_chat_format() {
        let prompt = format_chat_prompt("Write a poem about Rust.");
        assert!(prompt.contains("<start_of_turn>user"));
        assert!(prompt.contains("Write a poem about Rust."));
        assert!(prompt.contains("<end_of_turn>"));
        assert!(prompt.contains("<start_of_turn>model"));
    }

    // -----------------------------------------------------------------------
    // test_gemma2_error_display
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_error_display() {
        let err = Gemma2Error::InvalidConfig("bad param".to_string());
        let s = format!("{}", err);
        assert!(s.contains("invalid config") || s.contains("InvalidConfig"));
        assert!(s.contains("bad param"));

        let err2 = Gemma2Error::SequenceTooLong {
            max: 4096,
            got: 8192,
        };
        let s2 = format!("{}", err2);
        assert!(s2.contains("4096"));
        assert!(s2.contains("8192"));

        let err3 = Gemma2Error::EmptyInput;
        let s3 = format!("{}", err3);
        assert!(s3.contains("empty"));
    }

    // -----------------------------------------------------------------------
    // test_gemma2_gqa_heads
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_gqa_heads() {
        let cfg = Gemma2Config::gemma2_9b();
        assert_eq!(cfg.num_attention_heads, 16);
        assert_eq!(cfg.num_key_value_heads, 8);
        assert_eq!(cfg.kv_group_size(), 2);

        let cfg2b = Gemma2Config::gemma2_2b();
        assert_eq!(cfg2b.num_attention_heads, 8);
        assert_eq!(cfg2b.num_key_value_heads, 4);
        assert_eq!(cfg2b.kv_group_size(), 2);
    }

    // -----------------------------------------------------------------------
    // test_gemma2_gelu_values
    // -----------------------------------------------------------------------
    #[test]
    fn test_gemma2_gelu_values() {
        // gelu(0) = 0
        assert!(gelu(0.0).abs() < 1e-5, "gelu(0) should be 0");
        // gelu is positive for positive inputs
        assert!(gelu(1.0) > 0.0, "gelu(1.0) should be positive");
        // gelu approaches x for large x
        let large = gelu(10.0);
        assert!((large - 10.0).abs() < 0.01, "gelu(10) ≈ 10, got {}", large);
    }
}