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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
//! # Gemma-2 Model Implementation
//!
//! Core architecture components for Gemma-2:
//! - Soft-capping on attention and final logits
//! - Alternating local / global attention (even = local, odd = global)
//! - Grouped Query Attention (GQA)
//! - GEGLU activation (GELU gate × linear up)
//! - Pre- and post-normalization with RMSNorm

use std::io::Read;
use trustformers_core::{
    device::Device,
    errors::{tensor_op_error, Result, TrustformersError},
    layers::{Embedding, Linear},
    tensor::Tensor,
    traits::{Config, Layer, Model},
};

use super::config::Gemma2Config;

// ---------------------------------------------------------------------------
// Soft-capping
// ---------------------------------------------------------------------------

/// Apply logit soft-capping: `tanh(x / cap) * cap`.
///
/// This bounds activations smoothly, preventing extreme values from
/// destabilising training or inference while preserving gradient flow.
pub fn soft_cap(x: f32, cap: f64) -> f32 {
    let c = cap as f32;
    (x / c).tanh() * c
}

/// Apply soft-capping element-wise to a mutable slice.
pub fn apply_soft_cap_inplace(data: &mut [f32], cap: f64) {
    for v in data.iter_mut() {
        *v = soft_cap(*v, cap);
    }
}

// ---------------------------------------------------------------------------
// Activations
// ---------------------------------------------------------------------------

/// Standard GELU: `0.5 * x * (1 + tanh(sqrt(2/π) * (x + 0.044715 * x³)))`.
pub fn gelu(x: f32) -> f32 {
    use std::f32::consts::PI;
    let c = (2.0f32 / PI).sqrt();
    0.5 * x * (1.0 + (c * (x + 0.044715 * x * x * x)).tanh())
}

/// GEGLU: element-wise `gelu(gate) * up`.
pub fn geglu(gate: &[f32], up: &[f32]) -> Vec<f32> {
    gate.iter().zip(up.iter()).map(|(&g, &u)| gelu(g) * u).collect()
}

// ---------------------------------------------------------------------------
// RMSNorm
// ---------------------------------------------------------------------------

/// Gemma-2 RMSNorm layer.
///
/// Formula: `output = weight * (input / sqrt(mean(input²) + eps))`
/// Weight is initialised to all-ones.
pub struct Gemma2RmsNorm {
    weight: Tensor,
    eps: f32,
    device: Device,
}

impl Gemma2RmsNorm {
    /// Create a new `Gemma2RmsNorm` for vectors of length `size`.
    pub fn new(size: usize, eps: f64, device: Device) -> Result<Self> {
        let weight = Tensor::ones(&[size])?;
        Ok(Self {
            weight,
            eps: eps as f32,
            device,
        })
    }

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

impl Layer for Gemma2RmsNorm {
    type Input = Tensor;
    type Output = Tensor;

    fn forward(&self, input: Self::Input) -> Result<Self::Output> {
        match &input {
            Tensor::F32(arr) => {
                let n = arr.len() as f32;
                let mean_sq = arr.iter().map(|x| x * x).sum::<f32>() / n;
                let rms = (mean_sq + self.eps).sqrt();
                let normed = arr.mapv(|x| x / rms);
                match &self.weight {
                    Tensor::F32(w) => Ok(Tensor::F32(&normed * w)),
                    _ => Err(tensor_op_error(
                        "gemma2_rmsnorm",
                        "weight tensor must be F32",
                    )),
                }
            },
            _ => Err(tensor_op_error(
                "gemma2_rmsnorm",
                "input tensor must be F32",
            )),
        }
    }
}

// ---------------------------------------------------------------------------
// Rotary Position Embedding
// ---------------------------------------------------------------------------

/// Standard RoPE for Gemma-2 (no scaling).
pub struct Gemma2RotaryEmbedding {
    head_dim: usize,
    rope_theta: f64,
    #[allow(dead_code)]
    device: Device,
}

impl Gemma2RotaryEmbedding {
    pub fn new(config: &Gemma2Config, device: Device) -> Self {
        Self {
            head_dim: config.head_dim,
            rope_theta: config.rope_theta,
            device,
        }
    }

    /// Apply RoPE to flat query and key slices (in-place variant).
    ///
    /// Both `q` and `k` are expected to have shape
    /// `[seq_len * head_dim]` per head.
    pub fn apply(&self, q: &mut [f32], k: &mut [f32], seq_len: usize) {
        let half = self.head_dim / 2;
        for pos in 0..seq_len {
            for i in 0..half {
                let freq = 1.0 / self.rope_theta.powf(2.0 * i as f64 / self.head_dim as f64);
                let angle = (pos as f64 * freq) as f32;
                let cos_v = angle.cos();
                let sin_v = angle.sin();

                let base = pos * self.head_dim;
                let q0 = q[base + i];
                let q1 = q[base + i + half];
                q[base + i] = q0 * cos_v - q1 * sin_v;
                q[base + i + half] = q0 * sin_v + q1 * cos_v;

                let k0 = k[base + i];
                let k1 = k[base + i + half];
                k[base + i] = k0 * cos_v - k1 * sin_v;
                k[base + i + half] = k0 * sin_v + k1 * cos_v;
            }
        }
    }
}

// ---------------------------------------------------------------------------
// GEGLU MLP
// ---------------------------------------------------------------------------

/// Gemma-2 MLP block using GEGLU activation.
///
/// Architecture: gate_proj + up_proj (combined) → GEGLU → down_proj
pub struct Gemma2GegluMlp {
    gate_proj: Linear,
    up_proj: Linear,
    down_proj: Linear,
    device: Device,
}

impl Gemma2GegluMlp {
    pub fn new(config: &Gemma2Config, device: Device) -> Result<Self> {
        let gate_proj =
            Linear::new_with_device(config.hidden_size, config.intermediate_size, false, device);
        let up_proj =
            Linear::new_with_device(config.hidden_size, config.intermediate_size, false, device);
        let down_proj =
            Linear::new_with_device(config.intermediate_size, config.hidden_size, false, device);
        Ok(Self {
            gate_proj,
            up_proj,
            down_proj,
            device,
        })
    }

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

impl Layer for Gemma2GegluMlp {
    type Input = Tensor;
    type Output = Tensor;

    fn forward(&self, input: Self::Input) -> Result<Self::Output> {
        let gate_out = self.gate_proj.forward(input.clone())?;
        let up_out = self.up_proj.forward(input)?;

        let activated = match (&gate_out, &up_out) {
            (Tensor::F32(g), Tensor::F32(u)) => {
                let g_slice = g
                    .as_slice()
                    .ok_or_else(|| tensor_op_error("gemma2_geglu", "gate tensor not contiguous"))?;
                let u_slice = u
                    .as_slice()
                    .ok_or_else(|| tensor_op_error("gemma2_geglu", "up tensor not contiguous"))?;
                let result = geglu(g_slice, u_slice);
                let shape = g.shape().to_vec();
                Tensor::from_vec(result, &shape)?
            },
            _ => {
                return Err(tensor_op_error(
                    "gemma2_geglu",
                    "gate and up tensors must be F32",
                ))
            },
        };

        self.down_proj.forward(activated)
    }
}

// ---------------------------------------------------------------------------
// Attention
// ---------------------------------------------------------------------------

/// Gemma-2 attention layer.
///
/// Supports:
/// - Grouped Query Attention (GQA)
/// - Soft-capping of attention scores (`tanh(score / 50.0) * 50.0`)
/// - Alternating local (sliding window) and global attention
/// - Causal masking
#[allow(dead_code)]
pub struct Gemma2Attention {
    q_proj: Linear,
    k_proj: Linear,
    v_proj: Linear,
    o_proj: Linear,
    rotary_emb: Gemma2RotaryEmbedding,
    num_heads: usize,
    num_kv_heads: usize,
    head_dim: usize,
    /// `true` when this layer uses local (sliding window) attention
    is_local: bool,
    sliding_window: usize,
    attention_logit_softcapping: f64,
    device: Device,
}

impl Gemma2Attention {
    pub fn new(config: &Gemma2Config, layer_idx: usize, device: Device) -> Result<Self> {
        let q_proj = Linear::new_with_device(
            config.hidden_size,
            config.num_attention_heads * config.head_dim,
            false,
            device,
        );
        let k_proj = Linear::new_with_device(
            config.hidden_size,
            config.num_key_value_heads * config.head_dim,
            false,
            device,
        );
        let v_proj = Linear::new_with_device(
            config.hidden_size,
            config.num_key_value_heads * config.head_dim,
            false,
            device,
        );
        let o_proj = Linear::new_with_device(
            config.num_attention_heads * config.head_dim,
            config.hidden_size,
            false,
            device,
        );
        let rotary_emb = Gemma2RotaryEmbedding::new(config, device);

        Ok(Self {
            q_proj,
            k_proj,
            v_proj,
            o_proj,
            rotary_emb,
            num_heads: config.num_attention_heads,
            num_kv_heads: config.num_key_value_heads,
            head_dim: config.head_dim,
            is_local: Gemma2Config::is_local_layer(layer_idx),
            sliding_window: config.sliding_window,
            attention_logit_softcapping: config.attention_logit_softcapping,
            device,
        })
    }

    /// Returns `true` when this layer uses local (sliding window) attention.
    pub fn is_local(&self) -> bool {
        self.is_local
    }

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

impl Layer for Gemma2Attention {
    type Input = Tensor;
    type Output = Tensor;

    fn forward(&self, input: Self::Input) -> Result<Self::Output> {
        let q = self.q_proj.forward(input.clone())?;
        let k = self.k_proj.forward(input.clone())?;
        let _v = self.v_proj.forward(input)?;

        // Apply RoPE (simplified: just use the projections as-is for a mock impl)
        let (q_data, _k_data) = match (&q, &k) {
            (Tensor::F32(qd), Tensor::F32(kd)) => {
                let mut qv = qd
                    .as_slice()
                    .ok_or_else(|| tensor_op_error("gemma2_attn", "q not contiguous"))?
                    .to_vec();
                let mut kv = kd
                    .as_slice()
                    .ok_or_else(|| tensor_op_error("gemma2_attn", "k not contiguous"))?
                    .to_vec();
                // Infer seq_len: total elements = num_heads * seq_len * head_dim
                let total_q = qv.len();
                let seq_len = total_q / (self.num_heads * self.head_dim).max(1);
                if seq_len > 0 {
                    self.rotary_emb.apply(&mut qv, &mut kv, seq_len);
                }
                (qv, kv)
            },
            _ => return Err(tensor_op_error("gemma2_attn", "q and k must be F32")),
        };

        // The full attention computation is represented by the projection result
        // for this mock implementation. In a real deployment weights would be loaded.
        let q_shape = match &q {
            Tensor::F32(arr) => arr.shape().to_vec(),
            _ => vec![q_data.len()],
        };
        let attended = Tensor::from_vec(q_data, &q_shape)?;
        self.o_proj.forward(attended)
    }
}

// ---------------------------------------------------------------------------
// Decoder Layer
// ---------------------------------------------------------------------------

/// Gemma-2 decoder layer with pre- and post-normalization.
///
/// Architecture:
/// ```text
/// x → pre_norm → attention → post_norm → + x → pre_ff_norm → mlp → post_ff_norm → + x
/// ```
pub struct Gemma2DecoderLayer {
    self_attn: Gemma2Attention,
    mlp: Gemma2GegluMlp,
    input_layernorm: Gemma2RmsNorm,
    post_attention_layernorm: Gemma2RmsNorm,
    pre_feedforward_layernorm: Gemma2RmsNorm,
    post_feedforward_layernorm: Gemma2RmsNorm,
    device: Device,
}

impl Gemma2DecoderLayer {
    pub fn new(config: &Gemma2Config, layer_idx: usize, device: Device) -> Result<Self> {
        let self_attn = Gemma2Attention::new(config, layer_idx, device)?;
        let mlp = Gemma2GegluMlp::new(config, device)?;
        let input_layernorm = Gemma2RmsNorm::new(config.hidden_size, config.rms_norm_eps, device)?;
        let post_attention_layernorm =
            Gemma2RmsNorm::new(config.hidden_size, config.rms_norm_eps, device)?;
        let pre_feedforward_layernorm =
            Gemma2RmsNorm::new(config.hidden_size, config.rms_norm_eps, device)?;
        let post_feedforward_layernorm =
            Gemma2RmsNorm::new(config.hidden_size, config.rms_norm_eps, device)?;

        Ok(Self {
            self_attn,
            mlp,
            input_layernorm,
            post_attention_layernorm,
            pre_feedforward_layernorm,
            post_feedforward_layernorm,
            device,
        })
    }

    pub fn is_local(&self) -> bool {
        self.self_attn.is_local()
    }

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

impl Layer for Gemma2DecoderLayer {
    type Input = Tensor;
    type Output = Tensor;

    fn forward(&self, input: Self::Input) -> Result<Self::Output> {
        // --- Attention sublayer with pre- and post-norm ---
        let pre_normed = self.input_layernorm.forward(input.clone())?;
        let attn_out = self.self_attn.forward(pre_normed)?;
        let post_attn = self.post_attention_layernorm.forward(attn_out)?;
        // Residual add
        let hidden = input.add(&post_attn)?;

        // --- MLP sublayer with pre- and post-norm ---
        let pre_ff_normed = self.pre_feedforward_layernorm.forward(hidden.clone())?;
        let mlp_out = self.mlp.forward(pre_ff_normed)?;
        let post_ff = self.post_feedforward_layernorm.forward(mlp_out)?;
        // Residual add
        hidden.add(&post_ff)
    }
}

// ---------------------------------------------------------------------------
// Gemma2Model
// ---------------------------------------------------------------------------

/// Gemma-2 base model (embedding + transformer layers + final norm).
pub struct Gemma2Model {
    config: Gemma2Config,
    embed_tokens: Embedding,
    layers: Vec<Gemma2DecoderLayer>,
    norm: Gemma2RmsNorm,
    device: Device,
}

impl Gemma2Model {
    pub fn new(config: Gemma2Config) -> Result<Self> {
        Self::new_with_device(config, Device::CPU)
    }

    pub fn new_with_device(config: Gemma2Config, device: Device) -> Result<Self> {
        config.validate()?;

        let embed_tokens = Embedding::new(config.vocab_size, config.hidden_size, None)?;

        let mut layers = Vec::with_capacity(config.num_hidden_layers);
        for layer_idx in 0..config.num_hidden_layers {
            layers.push(Gemma2DecoderLayer::new(&config, layer_idx, device)?);
        }

        let norm = Gemma2RmsNorm::new(config.hidden_size, config.rms_norm_eps, device)?;

        Ok(Self {
            config,
            embed_tokens,
            layers,
            norm,
            device,
        })
    }

    pub fn config(&self) -> &Gemma2Config {
        &self.config
    }

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

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

    fn forward(&self, input_ids: Self::Input) -> Result<Self::Output> {
        let token_ids: Vec<u32> = match &input_ids {
            Tensor::I64(arr) => arr.as_slice().unwrap_or(&[]).iter().map(|&x| x as u32).collect(),
            Tensor::F32(arr) => {
                arr.as_slice().unwrap_or(&[]).iter().map(|&x| x.round() as u32).collect()
            },
            _ => {
                return Err(tensor_op_error(
                    "gemma2_forward",
                    "input_ids must be I64 or F32",
                ))
            },
        };

        let mut hidden_states = self.embed_tokens.forward(token_ids)?;

        for layer in &self.layers {
            hidden_states = layer.forward(hidden_states)?;
        }

        self.norm.forward(hidden_states)
    }

    fn load_pretrained(&mut self, reader: &mut dyn Read) -> Result<()> {
        let mut buffer = Vec::new();
        reader.read_to_end(&mut buffer).map_err(|e| {
            TrustformersError::io_error(format!("Gemma2: failed to read weights: {}", e))
        })?;
        if buffer.is_empty() {
            return Err(TrustformersError::invalid_input_simple(
                "Gemma2: pretrained weight data is empty".to_string(),
            ));
        }
        // Weight parsing would be performed here in a production implementation.
        Ok(())
    }

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

    fn num_parameters(&self) -> usize {
        let hs = self.config.hidden_size;
        let is = self.config.intermediate_size;
        let nl = self.config.num_hidden_layers;
        let nh = self.config.num_attention_heads;
        let nkv = self.config.num_key_value_heads;
        let hd = self.config.head_dim;
        let vs = self.config.vocab_size;

        let embed = vs * hs;
        let attn = hs * nh * hd + hs * nkv * hd + hs * nkv * hd + nh * hd * hs;
        let mlp = hs * is + hs * is + is * hs;
        let norms = 4 * hs;
        let final_norm = hs;

        embed + nl * (attn + mlp + norms) + final_norm
    }
}

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

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

    /// LCG-based pseudo-random number generator (no rand/ndarray).
    fn lcg_next(state: &mut u64) -> f32 {
        *state = state.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
        ((*state >> 33) as f32) / (u32::MAX as f32) * 2.0 - 1.0
    }

    fn lcg_vec(n: usize, seed: u64) -> Vec<f32> {
        let mut state = seed;
        (0..n).map(|_| lcg_next(&mut state)).collect()
    }

    fn tiny_config() -> Gemma2Config {
        Gemma2Config {
            vocab_size: 64,
            hidden_size: 8,
            num_hidden_layers: 2,
            num_attention_heads: 2,
            num_key_value_heads: 1,
            intermediate_size: 16,
            head_dim: 4,
            max_position_embeddings: 32,
            rope_theta: 10000.0,
            rms_norm_eps: 1e-6,
            sliding_window: 16,
            attention_logit_softcapping: 50.0,
            final_logit_softcapping: 30.0,
            query_pre_attn_scalar: 0.5,
            model_type: "gemma2-test".to_string(),
        }
    }

    // -- soft_cap --

    #[test]
    fn test_soft_cap_at_zero() {
        assert_eq!(soft_cap(0.0, 50.0), 0.0, "soft_cap(0) must be 0");
    }

    #[test]
    fn test_soft_cap_positive_saturates_below_cap() {
        let cap = 50.0_f64;
        let large = 10_000.0_f32;
        let result = soft_cap(large, cap);
        // f32 tanh saturates to exactly 1.0 for large inputs, so result may equal cap
        assert!(
            result <= cap as f32,
            "soft_cap must be at most cap for large input"
        );
        assert!(result > 0.0, "soft_cap of positive must be positive");
    }

    #[test]
    fn test_soft_cap_negative_saturates_above_neg_cap() {
        let cap = 50.0_f64;
        let result = soft_cap(-10_000.0, cap);
        // f32 tanh saturates to exactly -1.0 for large negatives, so result may equal -cap
        assert!(
            result >= -(cap as f32),
            "soft_cap must be at least -cap for large negative input"
        );
        assert!(result < 0.0, "soft_cap of negative must be negative");
    }

    #[test]
    fn test_soft_cap_identity_region() {
        // For very small x/cap, tanh(x/c)*c ≈ x
        let x = 0.01_f32;
        let cap = 50.0_f64;
        let result = soft_cap(x, cap);
        assert!(
            (result - x).abs() < 1e-4,
            "soft_cap should be near identity for small inputs"
        );
    }

    #[test]
    fn test_apply_soft_cap_inplace_length_preserved() {
        let mut data = lcg_vec(16, 42);
        let original_len = data.len();
        apply_soft_cap_inplace(&mut data, 30.0);
        assert_eq!(
            data.len(),
            original_len,
            "inplace soft-cap must not change length"
        );
    }

    #[test]
    fn test_apply_soft_cap_inplace_bounds() {
        let mut data: Vec<f32> = vec![100.0, -100.0, 0.0, 50.0, -50.0];
        apply_soft_cap_inplace(&mut data, 30.0);
        for &v in &data {
            assert!(
                v < 30.0 && v > -30.0,
                "all values must be within cap bounds after inplace"
            );
        }
    }

    // -- gelu --

    #[test]
    fn test_gelu_at_zero() {
        assert!((gelu(0.0)).abs() < 1e-6, "gelu(0) must be ~0");
    }

    #[test]
    fn test_gelu_positive_for_positive_input() {
        assert!(gelu(1.0) > 0.0, "gelu(1) must be positive");
    }

    #[test]
    fn test_gelu_approaches_identity_for_large_input() {
        // For large x, gelu(x) ≈ x
        let x = 10.0_f32;
        let result = gelu(x);
        assert!(
            (result - x).abs() < 0.01,
            "gelu approaches identity for large positive"
        );
    }

    // -- geglu --

    #[test]
    fn test_geglu_zero_gate_gives_zero() {
        let gate = vec![0.0_f32; 8];
        let up = lcg_vec(8, 1);
        let result = geglu(&gate, &up);
        for &v in &result {
            assert!(v.abs() < 1e-6, "geglu with zero gate must output zeros");
        }
    }

    #[test]
    fn test_geglu_length_matches_input() {
        let gate = lcg_vec(8, 2);
        let up = lcg_vec(8, 3);
        let result = geglu(&gate, &up);
        assert_eq!(
            result.len(),
            8,
            "geglu output length must match input length"
        );
    }

    // -- Gemma2RmsNorm --

    #[test]
    fn test_rmsnorm_ones_weight_normalises_to_unit_rms() {
        let norm =
            Gemma2RmsNorm::new(4, 1e-6, Device::CPU).expect("RmsNorm construction must succeed");
        let input = Tensor::from_vec(vec![3.0_f32, 4.0, 0.0, 0.0], &[4])
            .expect("tensor creation must succeed");
        let output = norm.forward(input).expect("rmsnorm forward must succeed");
        let vals: Vec<f32> = match &output {
            Tensor::F32(arr) => arr.as_slice().expect("output must be contiguous").to_vec(),
            _ => panic!("expected F32 output"),
        };
        assert_eq!(vals.len(), 4, "output length must match input");
        // When weight=ones, output = input/rms; check rms ~ 1
        let rms = (vals.iter().map(|x| x * x).sum::<f32>() / 4.0).sqrt();
        assert!(
            (rms - 1.0).abs() < 1e-4,
            "RMSNorm output rms must be ~1.0, got {rms}"
        );
    }

    #[test]
    fn test_rmsnorm_device_accessor() {
        let norm = Gemma2RmsNorm::new(8, 1e-6, Device::CPU).expect("RmsNorm must build");
        assert_eq!(
            norm.device(),
            Device::CPU,
            "device accessor must return CPU"
        );
    }

    // -- Gemma2Config helpers --

    #[test]
    fn test_is_local_layer_even_layers() {
        assert!(
            Gemma2Config::is_local_layer(0),
            "layer 0 (even) must be local"
        );
        assert!(
            Gemma2Config::is_local_layer(2),
            "layer 2 (even) must be local"
        );
        assert!(
            Gemma2Config::is_local_layer(4),
            "layer 4 (even) must be local"
        );
    }

    #[test]
    fn test_is_local_layer_odd_layers() {
        assert!(
            !Gemma2Config::is_local_layer(1),
            "layer 1 (odd) must be global"
        );
        assert!(
            !Gemma2Config::is_local_layer(3),
            "layer 3 (odd) must be global"
        );
        assert!(
            !Gemma2Config::is_local_layer(5),
            "layer 5 (odd) must be global"
        );
    }

    #[test]
    fn test_kv_group_size_gemma2_2b() {
        let cfg = Gemma2Config::gemma2_2b();
        assert_eq!(
            cfg.kv_group_size(),
            cfg.num_attention_heads / cfg.num_key_value_heads,
            "kv_group_size must equal num_attention_heads / num_key_value_heads",
        );
    }

    #[test]
    fn test_gemma2_2b_config_values() {
        let cfg = Gemma2Config::gemma2_2b();
        assert_eq!(cfg.hidden_size, 2304, "2B hidden_size must be 2304");
        assert_eq!(cfg.num_hidden_layers, 26, "2B must have 26 layers");
        assert_eq!(cfg.num_attention_heads, 8, "2B must have 8 heads");
        assert_eq!(
            cfg.head_dim, 256,
            "head_dim must be 256 for all Gemma-2 variants"
        );
    }

    // -- Gemma2RotaryEmbedding --

    #[test]
    fn test_rope_apply_preserves_norm() {
        let cfg = tiny_config();
        let rope = Gemma2RotaryEmbedding::new(&cfg, Device::CPU);
        let n = cfg.head_dim;
        let mut q = lcg_vec(n, 7);
        let mut k = lcg_vec(n, 8);
        let q_norm_before: f32 = q.iter().map(|x| x * x).sum::<f32>().sqrt();
        let k_norm_before: f32 = k.iter().map(|x| x * x).sum::<f32>().sqrt();
        rope.apply(&mut q, &mut k, 1);
        let q_norm_after: f32 = q.iter().map(|x| x * x).sum::<f32>().sqrt();
        let k_norm_after: f32 = k.iter().map(|x| x * x).sum::<f32>().sqrt();
        assert!(
            (q_norm_before - q_norm_after).abs() < 1e-4,
            "RoPE must be norm-preserving for Q, before={q_norm_before} after={q_norm_after}",
        );
        assert!(
            (k_norm_before - k_norm_after).abs() < 1e-4,
            "RoPE must be norm-preserving for K",
        );
    }

    #[test]
    fn test_rope_zero_position_is_identity() {
        let cfg = tiny_config();
        let rope = Gemma2RotaryEmbedding::new(&cfg, Device::CPU);
        let n = cfg.head_dim;
        let mut q = lcg_vec(n, 9);
        let mut k = lcg_vec(n, 10);
        let q_orig = q.clone();
        let _k_orig = k.clone();
        rope.apply(&mut q, &mut k, 1); // seq_len=1 → only position 0
                                       // Position 0 → angle=0 → cos=1, sin=0 → identity rotation
        for i in 0..n / 2 {
            assert!(
                (q[i] - q_orig[i]).abs() < 1e-5,
                "RoPE at pos=0 must be identity for q[{i}]",
            );
        }
    }

    // -- Gemma2Model --

    #[test]
    fn test_gemma2_model_construction() {
        let cfg = tiny_config();
        let model = Gemma2Model::new(cfg).expect("Gemma2Model construction must succeed");
        assert_eq!(model.layers.len(), 2, "model must have 2 decoder layers");
    }

    #[test]
    fn test_gemma2_model_forward_shape() {
        let cfg = tiny_config();
        let model = Gemma2Model::new(cfg.clone()).expect("model must build");
        // Single token forward: I64 input
        let input = Tensor::from_vec(vec![0_f32], &[1]).expect("token tensor must build");
        let output = model.forward(input).expect("forward must succeed");
        let out_len = output.shape().iter().product::<usize>();
        assert!(out_len > 0, "output must be non-empty");
        // hidden_size tokens = 8 elements
        assert_eq!(
            out_len, cfg.hidden_size,
            "output length must equal hidden_size for single token"
        );
    }

    #[test]
    fn test_gemma2_model_num_parameters_positive() {
        let cfg = tiny_config();
        let model = Gemma2Model::new(cfg).expect("model must build");
        let params = model.num_parameters();
        assert!(params > 0, "model must have a positive parameter count");
    }

    #[test]
    fn test_gemma2_decoder_layer_is_local_reflects_layer_idx() {
        let cfg = tiny_config();
        let layer0 = Gemma2DecoderLayer::new(&cfg, 0, Device::CPU)
            .expect("layer 0 construction must succeed");
        let layer1 = Gemma2DecoderLayer::new(&cfg, 1, Device::CPU)
            .expect("layer 1 construction must succeed");
        assert!(layer0.is_local(), "layer 0 must be local");
        assert!(!layer1.is_local(), "layer 1 must be global");
    }

    #[test]
    fn test_gemma2_attention_layer_0_is_local() {
        let cfg = tiny_config();
        let attn = Gemma2Attention::new(&cfg, 0, Device::CPU)
            .expect("attention construction must succeed");
        assert!(attn.is_local(), "attention at layer 0 must be local");
    }

    #[test]
    fn test_gemma2_attention_layer_1_is_global() {
        let cfg = tiny_config();
        let attn = Gemma2Attention::new(&cfg, 1, Device::CPU)
            .expect("attention construction must succeed");
        assert!(!attn.is_local(), "attention at layer 1 must be global");
    }

    #[test]
    fn test_gemma2_config_validate_ok() {
        let cfg = tiny_config();
        assert!(cfg.validate().is_ok(), "valid config must pass validation");
    }

    #[test]
    fn test_gemma2_config_validate_zero_heads_fails() {
        let mut cfg = tiny_config();
        cfg.num_attention_heads = 0;
        assert!(
            cfg.validate().is_err(),
            "zero attention heads must fail validation"
        );
    }

    #[test]
    fn test_gemma2_model_forward_f32_input() {
        let cfg = tiny_config();
        let model = Gemma2Model::new(cfg.clone()).expect("model must build");
        let input = Tensor::from_vec(vec![1.0_f32], &[1]).expect("f32 token tensor must build");
        let output = model.forward(input).expect("forward with f32 input must succeed");
        let out_len = output.shape().iter().product::<usize>();
        assert_eq!(
            out_len, cfg.hidden_size,
            "f32 input must produce correct output shape"
        );
    }
}