oxidelm 0.2.0

Bare-metal pure-Rust Transformer engine with reverse-mode autograd, BPE tokenization, and wgpu acceleration
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
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
#[cfg(feature = "gpu")]
use crate::gpu::{GpuContext, GpuError};
use crate::kv_quant::{pack_kv, KvQuantScheme, PackedKvTensor};
use crate::layers::{Initializer, Linear};
use crate::rope::RopeScaling;
use crate::{IndexTensor, Tensor, TensorError};

pub fn try_causal_softmax(scores: &Tensor) -> Result<Tensor, TensorError> {
    let shape = scores.shape();
    if shape.len() < 2 {
        return Err(TensorError::new(
            "causal attention scores must be at least 2D",
        ));
    }
    let sequence_length = shape[shape.len() - 1];
    if shape[shape.len() - 2] != sequence_length {
        return Err(TensorError::new("causal scores must be square"));
    }
    scores.try_masked_softmax_last_dim(0)
}

pub fn causal_softmax(scores: &Tensor) -> Tensor {
    try_causal_softmax(scores).unwrap_or_else(|error| panic!("{error}"))
}

#[derive(Clone)]
pub struct AttentionKvCache {
    pub key: Tensor,
    pub value: Tensor,
    /// When set, `key`/`value` are dequantized views of this packed storage.
    packed: Option<(PackedKvTensor, PackedKvTensor)>,
    scheme: Option<KvQuantScheme>,
}

impl AttentionKvCache {
    pub fn from_full(key: Tensor, value: Tensor) -> Self {
        Self {
            key,
            value,
            packed: None,
            scheme: None,
        }
    }

    pub fn from_quantized(
        key: Tensor,
        value: Tensor,
        packed_key: PackedKvTensor,
        packed_value: PackedKvTensor,
        scheme: KvQuantScheme,
    ) -> Self {
        Self {
            key,
            value,
            packed: Some((packed_key, packed_value)),
            scheme: Some(scheme),
        }
    }

    pub fn is_quantized(&self) -> bool {
        self.packed.is_some()
    }

    pub fn scheme(&self) -> Option<KvQuantScheme> {
        self.scheme
    }

    pub fn storage_bytes(&self) -> usize {
        if let Some((key, value)) = &self.packed {
            key.storage_bytes() + value.storage_bytes()
        } else {
            (self.key.numel() + self.value.numel()) * std::mem::size_of::<f32>()
        }
    }

    pub fn sequence_length(&self) -> usize {
        self.key.shape()[2]
    }

    pub fn batch_size(&self) -> usize {
        self.key.shape()[0]
    }

    /// Concatenate caches along the batch axis. All entries must share `[H, S, D]`.
    pub fn concat_batch(caches: &[&Self]) -> Result<Self, TensorError> {
        if caches.is_empty() {
            return Err(TensorError::new("cannot concat an empty KV cache batch"));
        }
        let key = concat_batch_tensors(
            &caches
                .iter()
                .map(|cache| &cache.key)
                .collect::<Vec<_>>(),
        )?;
        let value = concat_batch_tensors(
            &caches
                .iter()
                .map(|cache| &cache.value)
                .collect::<Vec<_>>(),
        )?;
        // Re-quantize if all sources share a scheme.
        let scheme = caches[0].scheme;
        if let Some(scheme) = scheme {
            if caches.iter().all(|cache| cache.scheme == Some(scheme)) {
                return quantize_cache_tensors(key, value, scheme)
                    .map_err(|error| TensorError::new(error));
            }
        }
        Ok(Self::from_full(key, value))
    }

    /// Split a batched cache into `batch_size` independent `B = 1` caches.
    pub fn split_batch(&self) -> Result<Vec<Self>, TensorError> {
        let keys = split_batch_tensor(&self.key)?;
        let values = split_batch_tensor(&self.value)?;
        if keys.len() != values.len() {
            return Err(TensorError::new("KV cache batch sizes differ during split"));
        }
        let scheme = self.scheme;
        keys.into_iter()
            .zip(values)
            .map(|(key, value)| {
                if let Some(scheme) = scheme {
                    quantize_cache_tensors(key, value, scheme)
                        .map_err(|error| TensorError::new(error))
                } else {
                    Ok(Self::from_full(key, value))
                }
            })
            .collect()
    }

    /// Keep only the first `sequence_length` cached positions (CPU-materialized).
    pub fn truncate(&mut self, sequence_length: usize) -> Result<(), TensorError> {
        let current = self.sequence_length();
        if sequence_length > current {
            return Err(TensorError::new(
                "cannot truncate KV cache past its sequence length",
            ));
        }
        if sequence_length == current {
            return Ok(());
        }
        if sequence_length == 0 {
            return Err(TensorError::new(
                "truncate length must be positive; clear the layer cache instead",
            ));
        }
        self.key = truncate_sequence_tensor(&self.key, sequence_length)?;
        self.value = truncate_sequence_tensor(&self.value, sequence_length)?;
        if let Some(scheme) = self.scheme {
            *self = quantize_cache_tensors(self.key.clone(), self.value.clone(), scheme)
                .map_err(|error| TensorError::new(error))?;
        } else {
            self.packed = None;
        }
        Ok(())
    }
}

fn quantize_cache_tensors(
    key: Tensor,
    value: Tensor,
    scheme: KvQuantScheme,
) -> Result<AttentionKvCache, String> {
    let key_shape = key.shape();
    let value_shape = value.shape();
    if key_shape != value_shape || key_shape.len() != 4 {
        return Err("KV tensors must share shape [B, H, S, D]".into());
    }
    let (batch, heads, seq, head_dim) = (key_shape[0], key_shape[1], key_shape[2], key_shape[3]);
    let packed_key = pack_kv(&key.shared_data(), batch, heads, seq, head_dim, scheme)?;
    let packed_value = pack_kv(&value.shared_data(), batch, heads, seq, head_dim, scheme)?;
    let key_dequant = Tensor::new(packed_key.dequantize(), key_shape.clone(), false);
    let value_dequant = Tensor::new(packed_value.dequantize(), value_shape, false);
    Ok(AttentionKvCache::from_quantized(
        key_dequant,
        value_dequant,
        packed_key,
        packed_value,
        scheme,
    ))
}

fn cpu_owned_tensor(tensor: &Tensor) -> Result<Tensor, TensorError> {
    let cpu = tensor
        .to_cpu()
        .map_err(|error| TensorError::new(error.to_string()))?;
    Ok(Tensor::new(cpu.data(), cpu.shape(), false))
}

fn truncate_sequence_tensor(tensor: &Tensor, sequence_length: usize) -> Result<Tensor, TensorError> {
    let owned = cpu_owned_tensor(tensor)?;
    let shape = owned.shape();
    if shape.len() != 4 {
        return Err(TensorError::new(
            "KV cache tensors must have shape [B, H, S, D]",
        ));
    }
    if sequence_length > shape[2] {
        return Err(TensorError::new(
            "cannot truncate KV cache past its sequence length",
        ));
    }
    if sequence_length == shape[2] {
        return Ok(owned);
    }
    let narrowed = owned.try_narrow(2, 0, sequence_length)?;
    Ok(Tensor::new(narrowed.data(), narrowed.shape(), false))
}

fn concat_batch_tensors(tensors: &[&Tensor]) -> Result<Tensor, TensorError> {
    if tensors.is_empty() {
        return Err(TensorError::new("cannot concat an empty tensor batch"));
    }
    let mut owned = Vec::with_capacity(tensors.len());
    for tensor in tensors {
        owned.push(cpu_owned_tensor(tensor)?);
    }
    let reference = owned[0].shape();
    if reference.len() != 4 {
        return Err(TensorError::new(
            "KV cache tensors must have shape [B, H, S, D]",
        ));
    }
    let mut total_batch = 0usize;
    let mut data = Vec::new();
    for tensor in &owned {
        let shape = tensor.shape();
        if shape[1..] != reference[1..] {
            return Err(TensorError::new(
                "KV cache concat requires matching head, sequence, and width dims",
            ));
        }
        total_batch += shape[0];
        data.extend_from_slice(&tensor.shared_data());
    }
    Ok(Tensor::new(
        data,
        vec![total_batch, reference[1], reference[2], reference[3]],
        false,
    ))
}

fn split_batch_tensor(tensor: &Tensor) -> Result<Vec<Tensor>, TensorError> {
    let owned = cpu_owned_tensor(tensor)?;
    let shape = owned.shape();
    if shape.len() != 4 {
        return Err(TensorError::new(
            "KV cache tensors must have shape [B, H, S, D]",
        ));
    }
    let batch = shape[0];
    let stride = shape[1] * shape[2] * shape[3];
    let data = owned.shared_data();
    let mut parts = Vec::with_capacity(batch);
    for batch_index in 0..batch {
        let start = batch_index * stride;
        parts.push(Tensor::new(
            data[start..start + stride].to_vec(),
            vec![1, shape[1], shape[2], shape[3]],
            false,
        ));
    }
    Ok(parts)
}

#[derive(Clone)]
pub struct MultiHeadAttention {
    pub q_proj: Linear,
    pub k_proj: Linear,
    pub v_proj: Linear,
    pub o_proj: Linear,
    pub hidden_size: usize,
    pub num_heads: usize,
    pub num_key_value_heads: usize,
    pub head_dim: usize,
    pub rope_theta: f32,
    pub rope_scaling: Option<RopeScaling>,
    /// Optional KV-cache quantization applied on append.
    kv_quant: Option<KvQuantScheme>,
}

impl MultiHeadAttention {
    pub fn new(hidden_size: usize, num_heads: usize, rope_theta: f32, seed: u64) -> Self {
        Self::new_grouped(hidden_size, num_heads, num_heads, rope_theta, seed)
    }

    pub fn new_grouped(
        hidden_size: usize,
        num_heads: usize,
        num_key_value_heads: usize,
        rope_theta: f32,
        seed: u64,
    ) -> Self {
        let mut initializer = Initializer::new(seed);
        Self::from_initializer(
            hidden_size,
            num_heads,
            num_key_value_heads,
            rope_theta,
            None,
            &mut initializer,
            1.0,
            false,
        )
    }

    pub(crate) fn from_initializer(
        hidden_size: usize,
        num_heads: usize,
        num_key_value_heads: usize,
        rope_theta: f32,
        rope_scaling: Option<RopeScaling>,
        initializer: &mut Initializer,
        residual_scale: f32,
        use_bias: bool,
    ) -> Self {
        assert!(num_heads > 0, "attention needs at least one head");
        assert!(
            num_key_value_heads > 0 && num_heads % num_key_value_heads == 0,
            "query heads must be divisible by key/value heads"
        );
        assert_eq!(hidden_size % num_heads, 0, "heads must divide hidden size");
        let head_dim = hidden_size / num_heads;
        let key_value_size = num_key_value_heads * head_dim;
        assert_eq!(head_dim % 2, 0, "RoPE head width must be even");
        assert!(rope_theta > 0.0, "RoPE theta must be positive");
        Self {
            q_proj: Linear::from_initializer(hidden_size, hidden_size, initializer, use_bias),
            k_proj: Linear::from_initializer(hidden_size, key_value_size, initializer, use_bias),
            v_proj: Linear::from_initializer(hidden_size, key_value_size, initializer, use_bias),
            o_proj: Linear::from_initializer_scaled(
                hidden_size,
                hidden_size,
                initializer,
                residual_scale,
                use_bias,
            ),
            hidden_size,
            num_heads,
            num_key_value_heads,
            head_dim,
            rope_theta,
            rope_scaling,
            kv_quant: None,
        }
    }

    pub fn enable_kv_quant(&mut self, scheme: KvQuantScheme) {
        self.kv_quant = Some(scheme);
    }

    pub fn clear_kv_quant(&mut self) {
        self.kv_quant = None;
    }

    pub fn kv_quant(&self) -> Option<KvQuantScheme> {
        self.kv_quant
    }

    pub fn forward(&self, input: &Tensor) -> Tensor {
        self.forward_with_optional_attention_mask(input, None)
    }

    pub fn forward_with_attention_mask(
        &self,
        input: &Tensor,
        attention_mask: &IndexTensor,
    ) -> Tensor {
        self.forward_with_optional_attention_mask(input, Some(attention_mask))
    }

    pub(crate) fn forward_with_optional_attention_mask(
        &self,
        input: &Tensor,
        attention_mask: Option<&IndexTensor>,
    ) -> Tensor {
        self.forward_training(input, attention_mask, 0.0, 0)
    }

    pub(crate) fn forward_training(
        &self,
        input: &Tensor,
        attention_mask: Option<&IndexTensor>,
        dropout_probability: f32,
        dropout_seed: u32,
    ) -> Tensor {
        self.forward_training_chunked(input, attention_mask, dropout_probability, dropout_seed, 0)
    }

    /// Like [`Self::forward_training`], optionally computing attention in query-position
    /// chunks so peak activation scales with `chunk_size` instead of full `S`.
    pub(crate) fn forward_training_chunked(
        &self,
        input: &Tensor,
        attention_mask: Option<&IndexTensor>,
        dropout_probability: f32,
        dropout_seed: u32,
        chunk_size: usize,
    ) -> Tensor {
        let shape = input.shape();
        assert_eq!(shape.len(), 3, "attention input must have shape [B, S, D]");
        let (batch_size, sequence_length, hidden_size) = (shape[0], shape[1], shape[2]);
        assert_eq!(
            hidden_size, self.hidden_size,
            "attention input width mismatch"
        );

        let query = self
            .split_heads(
                &self.q_proj.forward(input),
                batch_size,
                sequence_length,
                self.num_heads,
            )
            .rope_with_offset_and_scaling(2, self.rope_theta, 0, self.rope_scaling.as_ref());
        let key = self
            .split_heads(
                &self.k_proj.forward(input),
                batch_size,
                sequence_length,
                self.num_key_value_heads,
            )
            .rope_with_offset_and_scaling(2, self.rope_theta, 0, self.rope_scaling.as_ref());
        let value = self.split_heads(
            &self.v_proj.forward(input),
            batch_size,
            sequence_length,
            self.num_key_value_heads,
        );
        let scale = 1.0 / (self.head_dim as f32).sqrt();
        let context = if chunk_size == 0 || chunk_size >= sequence_length {
            query.tiled_causal_attention_with_optional_mask(
                &key,
                &value,
                scale,
                0,
                attention_mask,
                dropout_probability,
                dropout_seed,
            )
        } else {
            // Process query tiles under a peak-activation cap; keys/values stay full
            // (causal) so each chunk can attend to all prior positions.
            let mut chunks = Vec::new();
            let mut start = 0;
            while start < sequence_length {
                let length = chunk_size.min(sequence_length - start);
                let query_chunk = query.narrow(2, start, length);
                let chunk = query_chunk.tiled_causal_attention_with_optional_mask(
                    &key,
                    &value,
                    scale,
                    start,
                    attention_mask,
                    dropout_probability,
                    dropout_seed,
                );
                chunks.push(chunk);
                start += length;
            }
            Tensor::cat(&chunks, 2)
        };
        let merged = context.permute(vec![0, 2, 1, 3]).reshape(vec![
            batch_size,
            sequence_length,
            hidden_size,
        ]);
        self.o_proj.forward(&merged)
    }

    #[cfg(feature = "gpu")]
    pub fn forward_gpu(&self, input: &Tensor, gpu: &GpuContext) -> Result<Tensor, GpuError> {
        self.forward_gpu_with_optional_attention_mask(input, None, gpu)
    }

    #[cfg(feature = "gpu")]
    pub fn forward_gpu_with_attention_mask(
        &self,
        input: &Tensor,
        attention_mask: &IndexTensor,
        gpu: &GpuContext,
    ) -> Result<Tensor, GpuError> {
        self.forward_gpu_with_optional_attention_mask(input, Some(attention_mask), gpu)
    }

    #[cfg(feature = "gpu")]
    pub(crate) fn forward_gpu_with_optional_attention_mask(
        &self,
        input: &Tensor,
        attention_mask: Option<&IndexTensor>,
        gpu: &GpuContext,
    ) -> Result<Tensor, GpuError> {
        self.forward_training_gpu(input, attention_mask, 0.0, 0, gpu)
    }

    #[cfg(feature = "gpu")]
    pub(crate) fn forward_training_gpu(
        &self,
        input: &Tensor,
        attention_mask: Option<&IndexTensor>,
        dropout_probability: f32,
        dropout_seed: u32,
        gpu: &GpuContext,
    ) -> Result<Tensor, GpuError> {
        let shape = input.shape();
        assert_eq!(shape.len(), 3, "attention input must have shape [B, S, D]");
        let (batch_size, sequence_length, hidden_size) = (shape[0], shape[1], shape[2]);
        assert_eq!(
            hidden_size, self.hidden_size,
            "attention input width mismatch"
        );

        let query = self
            .split_heads_gpu(
                &self.q_proj.forward_gpu(input, gpu)?,
                batch_size,
                sequence_length,
                self.num_heads,
                gpu,
            )?
            .gpu_rope_with_offset_and_scaling(
                2,
                self.rope_theta,
                0,
                self.rope_scaling.as_ref(),
                gpu,
            )?;
        let key = self
            .split_heads_gpu(
                &self.k_proj.forward_gpu(input, gpu)?,
                batch_size,
                sequence_length,
                self.num_key_value_heads,
                gpu,
            )?
            .gpu_rope_with_offset_and_scaling(
                2,
                self.rope_theta,
                0,
                self.rope_scaling.as_ref(),
                gpu,
            )?;
        let value = self.split_heads_gpu(
            &self.v_proj.forward_gpu(input, gpu)?,
            batch_size,
            sequence_length,
            self.num_key_value_heads,
            gpu,
        )?;

        let context = gpu.tiled_causal_attention_tensor_with_optional_mask(
            &query,
            &key,
            &value,
            1.0 / (self.head_dim as f32).sqrt(),
            0,
            attention_mask,
            dropout_probability,
            dropout_seed,
        )?;
        let merged = context.gpu_permute(vec![0, 2, 1, 3], gpu)?.reshape(vec![
            batch_size,
            sequence_length,
            hidden_size,
        ]);
        self.o_proj.forward_gpu(&merged, gpu)
    }

    pub fn forward_cached(
        &self,
        input: &Tensor,
        cache: &mut Option<AttentionKvCache>,
        position_offset: usize,
        max_sequence_length: usize,
    ) -> Tensor {
        let shape = input.shape();
        assert_eq!(shape.len(), 3, "attention input must have shape [B, S, D]");
        let (batch_size, sequence_length, hidden_size) = (shape[0], shape[1], shape[2]);
        assert_eq!(
            hidden_size, self.hidden_size,
            "attention input width mismatch"
        );

        let query = self
            .split_heads(
                &self.q_proj.forward(input),
                batch_size,
                sequence_length,
                self.num_heads,
            )
            .rope_with_offset_and_scaling(
                2,
                self.rope_theta,
                position_offset,
                self.rope_scaling.as_ref(),
            );
        let key = self
            .split_heads(
                &self.k_proj.forward(input),
                batch_size,
                sequence_length,
                self.num_key_value_heads,
            )
            .rope_with_offset_and_scaling(
                2,
                self.rope_theta,
                position_offset,
                self.rope_scaling.as_ref(),
            );
        let value = self.split_heads(
            &self.v_proj.forward(input),
            batch_size,
            sequence_length,
            self.num_key_value_heads,
        );
        let past_length = update_cache(
            cache,
            &key,
            &value,
            max_sequence_length,
            sequence_length,
            self.kv_quant,
        );
        let merged = self.cached_context(&query, cache.as_ref().unwrap(), past_length);
        self.o_proj.forward(&merged)
    }

    #[cfg(feature = "gpu")]
    pub fn forward_cached_gpu(
        &self,
        input: &Tensor,
        cache: &mut Option<AttentionKvCache>,
        position_offset: usize,
        max_sequence_length: usize,
        gpu: &GpuContext,
    ) -> Result<Tensor, GpuError> {
        let shape = input.shape();
        assert_eq!(shape.len(), 3, "attention input must have shape [B, S, D]");
        let (batch_size, sequence_length, hidden_size) = (shape[0], shape[1], shape[2]);
        assert_eq!(
            hidden_size, self.hidden_size,
            "attention input width mismatch"
        );

        let query = self
            .split_heads_gpu(
                &self.q_proj.forward_gpu(input, gpu)?,
                batch_size,
                sequence_length,
                self.num_heads,
                gpu,
            )?
            .gpu_rope_with_offset_and_scaling(
                2,
                self.rope_theta,
                position_offset,
                self.rope_scaling.as_ref(),
                gpu,
            )?;
        let key = self
            .split_heads_gpu(
                &self.k_proj.forward_gpu(input, gpu)?,
                batch_size,
                sequence_length,
                self.num_key_value_heads,
                gpu,
            )?
            .gpu_rope_with_offset_and_scaling(
                2,
                self.rope_theta,
                position_offset,
                self.rope_scaling.as_ref(),
                gpu,
            )?;
        let value = self.split_heads_gpu(
            &self.v_proj.forward_gpu(input, gpu)?,
            batch_size,
            sequence_length,
            self.num_key_value_heads,
            gpu,
        )?;
        let past_length = update_cache_gpu(
            cache,
            &key,
            &value,
            max_sequence_length,
            sequence_length,
            self.kv_quant,
            gpu,
        )?;
        let merged = self.cached_context_gpu(&query, cache.as_ref().unwrap(), past_length, gpu)?;
        self.o_proj.forward_gpu(&merged, gpu)
    }

    #[cfg(feature = "gpu")]
    fn cached_context_gpu(
        &self,
        query: &Tensor,
        cache: &AttentionKvCache,
        past_length: usize,
        gpu: &GpuContext,
    ) -> Result<Tensor, GpuError> {
        let query_shape = query.shape();
        let batch_size = query_shape[0];
        let query_length = query_shape[2];
        let context = query.gpu_tiled_causal_attention(
            &cache.key,
            &cache.value,
            1.0 / (self.head_dim as f32).sqrt(),
            past_length,
            gpu,
        )?;
        Ok(context.gpu_permute(vec![0, 2, 1, 3], gpu)?.reshape(vec![
            batch_size,
            query_length,
            self.hidden_size,
        ]))
    }

    fn cached_context(
        &self,
        query: &Tensor,
        cache: &AttentionKvCache,
        past_length: usize,
    ) -> Tensor {
        let query_shape = query.shape();
        let batch_size = query_shape[0];
        let query_length = query_shape[2];
        let context = query.tiled_causal_attention(
            &cache.key,
            &cache.value,
            1.0 / (self.head_dim as f32).sqrt(),
            past_length,
        );
        context
            .permute(vec![0, 2, 1, 3])
            .reshape(vec![batch_size, query_length, self.hidden_size])
    }

    fn split_heads(
        &self,
        input: &Tensor,
        batch_size: usize,
        sequence_length: usize,
        heads: usize,
    ) -> Tensor {
        input
            .reshape(vec![batch_size, sequence_length, heads, self.head_dim])
            .permute(vec![0, 2, 1, 3])
    }

    #[cfg(feature = "gpu")]
    fn split_heads_gpu(
        &self,
        input: &Tensor,
        batch_size: usize,
        sequence_length: usize,
        heads: usize,
        gpu: &GpuContext,
    ) -> Result<Tensor, GpuError> {
        input
            .reshape(vec![batch_size, sequence_length, heads, self.head_dim])
            .gpu_permute(vec![0, 2, 1, 3], gpu)
    }

    pub fn parameters(&self) -> Vec<Tensor> {
        let mut parameters = Vec::new();
        parameters.extend(self.q_proj.parameters());
        parameters.extend(self.k_proj.parameters());
        parameters.extend(self.v_proj.parameters());
        parameters.extend(self.o_proj.parameters());
        parameters
    }
}

fn update_cache(
    cache: &mut Option<AttentionKvCache>,
    key: &Tensor,
    value: &Tensor,
    max_sequence_length: usize,
    new_sequence_length: usize,
    kv_quant: Option<KvQuantScheme>,
) -> usize {
    assert!(new_sequence_length <= max_sequence_length);
    let updated_key = append_sequence(
        cache.as_ref().map(|cached| &cached.key),
        key,
        max_sequence_length,
    );
    let updated_value = append_sequence(
        cache.as_ref().map(|cached| &cached.value),
        value,
        max_sequence_length,
    );
    let total_length = updated_key.shape()[2];
    *cache = Some(match kv_quant {
        Some(scheme) => quantize_cache_tensors(updated_key, updated_value, scheme)
            .unwrap_or_else(|error| panic!("KV quant failed: {error}")),
        None => AttentionKvCache::from_full(updated_key, updated_value),
    });
    total_length - new_sequence_length
}

#[cfg(feature = "gpu")]
fn update_cache_gpu(
    cache: &mut Option<AttentionKvCache>,
    key: &Tensor,
    value: &Tensor,
    max_sequence_length: usize,
    new_sequence_length: usize,
    kv_quant: Option<KvQuantScheme>,
    gpu: &GpuContext,
) -> Result<usize, GpuError> {
    assert!(new_sequence_length <= max_sequence_length);
    let updated_key = gpu.append_sequence_tensor(
        cache.as_ref().map(|cached| &cached.key),
        key,
        max_sequence_length,
    )?;
    let updated_value = gpu.append_sequence_tensor(
        cache.as_ref().map(|cached| &cached.value),
        value,
        max_sequence_length,
    )?;
    let total_length = updated_key.shape()[2];
    *cache = Some(match kv_quant {
        Some(scheme) => {
            // Quantize on CPU (dequant views), then re-upload for attention kernels.
            let key_cpu = updated_key
                .to_cpu()
                .map_err(|error| GpuError::Map(error.to_string()))?;
            let value_cpu = updated_value
                .to_cpu()
                .map_err(|error| GpuError::Map(error.to_string()))?;
            let quantized = quantize_cache_tensors(
                Tensor::new(key_cpu.data(), key_cpu.shape(), false),
                Tensor::new(value_cpu.data(), value_cpu.shape(), false),
                scheme,
            )
            .map_err(GpuError::InvalidShape)?;
            AttentionKvCache::from_quantized(
                quantized.key.to_gpu(gpu)?,
                quantized.value.to_gpu(gpu)?,
                quantized.packed.as_ref().unwrap().0.clone(),
                quantized.packed.as_ref().unwrap().1.clone(),
                scheme,
            )
        }
        None => AttentionKvCache::from_full(updated_key, updated_value),
    });
    Ok(total_length - new_sequence_length)
}

fn append_sequence(existing: Option<&Tensor>, addition: &Tensor, maximum: usize) -> Tensor {
    let addition_shape = addition.shape();
    assert_eq!(addition_shape.len(), 4);
    let outer = addition_shape[0] * addition_shape[1];
    let added_length = addition_shape[2];
    let width = addition_shape[3];
    let retained_length = existing
        .map(|tensor| tensor.shape()[2].min(maximum - added_length))
        .unwrap_or(0);
    let mut data = Vec::with_capacity(outer * (retained_length + added_length) * width);
    let addition_data = addition.shared_data();

    if let Some(existing) = existing {
        let existing_shape = existing.shape();
        assert_eq!(existing_shape[0], addition_shape[0]);
        assert_eq!(existing_shape[1], addition_shape[1]);
        assert_eq!(existing_shape[3], width);
        let existing_length = existing_shape[2];
        let existing_data = existing.shared_data();
        for outer_index in 0..outer {
            let retained_start =
                (outer_index * existing_length + existing_length - retained_length) * width;
            data.extend_from_slice(
                &existing_data[retained_start..retained_start + retained_length * width],
            );
            let addition_start = outer_index * added_length * width;
            data.extend_from_slice(
                &addition_data[addition_start..addition_start + added_length * width],
            );
        }
    } else {
        data = (*addition_data).clone();
    }

    Tensor::new(
        data,
        vec![
            addition_shape[0],
            addition_shape[1],
            retained_length + added_length,
            width,
        ],
        false,
    )
}

#[cfg(test)]
mod tests {
    use super::causal_softmax;
    use crate::{IndexTensor, Tensor};

    #[test]
    fn causal_mask_blocks_future_positions() {
        let scores = Tensor::zeros(vec![1, 1, 4, 4], false);
        let probabilities = causal_softmax(&scores).data();
        for query in 0..4 {
            for key in query + 1..4 {
                assert_eq!(probabilities[query * 4 + key], 0.0);
            }
            let allowed = &probabilities[query * 4..=query * 4 + query];
            let sum: f32 = allowed.iter().sum();
            assert!((sum - 1.0).abs() < 1e-5);
        }
    }

    #[test]
    fn causal_softmax_matches_additive_mask_reference() {
        let scores = Tensor::new(
            vec![0.2, -0.3, 0.7, 1.1, -0.8, 0.4, 0.6, -0.2, 0.9],
            vec![1, 3, 3],
            false,
        );
        let mut mask = vec![0.0; 9];
        for query in 0..3 {
            for key in query + 1..3 {
                mask[query * 3 + key] = f32::NEG_INFINITY;
            }
        }
        let reference = scores
            .add(&Tensor::new(mask, vec![3, 3], false))
            .softmax_last_dim()
            .data();
        let actual = causal_softmax(&scores).data();
        for (left, right) in actual.iter().zip(reference.iter()) {
            assert!((left - right).abs() < 1e-6);
        }
    }

    #[test]
    fn tiled_attention_ignores_padding_keys_and_queries() {
        let query = Tensor::new(
            vec![1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0],
            vec![2, 1, 2, 2],
            false,
        );
        let key = Tensor::new(
            vec![1.0, 0.0, 100.0, 0.0, 100.0, 0.0, 1.0, 0.0],
            vec![2, 1, 2, 2],
            false,
        );
        let value = Tensor::new(
            vec![3.0, 4.0, 9.0, 9.0, 9.0, 9.0, 5.0, 6.0],
            vec![2, 1, 2, 2],
            false,
        );
        let mask = IndexTensor::new(vec![1, 0, 0, 1], vec![2, 2]);

        let output = query.tiled_causal_attention_with_mask(&key, &value, 1.0, 0, &mask);

        assert_eq!(output.data(), vec![3.0, 4.0, 0.0, 0.0, 0.0, 0.0, 5.0, 6.0]);
    }
}