rusting_brain 2.0.0

A Rust deep-learning library for training and running transformer language models, dense and mixture-of-experts, on CPU or CUDA.
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
//! Grouped-query self-attention with rotary positions and a causal mask.

use crate::activations::softmax;
use crate::batch::Layout;
use crate::matrix::Matrix;
use crate::network::NetworkError;
use crate::param::{Linear, Param};
use crate::rope::Rope;
use rand::rngs::StdRng;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};

/// Growable key/value history for one attention layer.
///
/// Keys and values are stored post-RoPE and post-projection, laid out
/// `[position, kv_heads * head_dim]`, so a decode step appends one row and the
/// attention math reads the whole buffer unchanged.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct KvCache {
    keys: Vec<f32>,
    values: Vec<f32>,
    width: usize,
    length: usize,
}

impl KvCache {
    pub fn new(kv_heads: usize, head_dim: usize) -> Self {
        Self {
            keys: Vec::new(),
            values: Vec::new(),
            width: kv_heads * head_dim,
            length: 0,
        }
    }

    /// Reserves room for `positions` tokens, so a generation loop does not
    /// reallocate on every step.
    pub fn with_capacity(kv_heads: usize, head_dim: usize, positions: usize) -> Self {
        let width = kv_heads * head_dim;
        Self {
            keys: Vec::with_capacity(width * positions),
            values: Vec::with_capacity(width * positions),
            width,
            length: 0,
        }
    }

    /// Number of cached positions, which is also the absolute position of the
    /// next token to be appended.
    pub fn len(&self) -> usize {
        self.length
    }

    pub fn is_empty(&self) -> bool {
        self.length == 0
    }

    pub fn clear(&mut self) {
        self.keys.clear();
        self.values.clear();
        self.length = 0;
    }

    /// Appends one or more positions worth of keys and values.
    pub fn append(&mut self, keys: &Matrix, values: &Matrix) -> Result<(), NetworkError> {
        if keys.cols != self.width || values.cols != self.width {
            return Err(NetworkError::InvalidConfig(format!(
                "kv cache expected {} columns, got {} keys and {} values",
                self.width, keys.cols, values.cols
            )));
        }

        self.keys.extend_from_slice(&keys.data);
        self.values.extend_from_slice(&values.data);
        self.length += keys.rows;
        Ok(())
    }

    pub fn keys(&self) -> &[f32] {
        &self.keys
    }

    pub fn values(&self) -> &[f32] {
        &self.values
    }
}

/// Everything the backward pass needs from a training forward pass.
///
/// Attention backward needs the post-RoPE projections and the attention
/// probabilities, none of which can be recovered cheaply from the output. The
/// cached-decode path builds no cache at all, because generation never runs
/// backward.
#[derive(Clone, Debug)]
pub struct AttentionCache {
    input: Matrix,
    queries: Matrix,
    keys: Matrix,
    values: Matrix,
    /// `[head][row, key]`, one softmax row per query. A key column is an
    /// offset *within the query's own sequence*, so the matrix is
    /// `[batch * seq_len, seq_len]` however many sequences are packed in.
    probabilities: Vec<Matrix>,
    merged: Matrix,
    seq_len: usize,
}

/// Multi-head attention with `num_kv_heads <= num_heads`.
///
/// Plain multi-head attention is the `num_kv_heads == num_heads` case, so there
/// is one implementation rather than two.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct MultiHeadAttention {
    pub query: Linear,
    pub key: Linear,
    pub value: Linear,
    pub output: Linear,
    pub rope: Rope,
    num_heads: usize,
    num_kv_heads: usize,
    head_dim: usize,
}

impl MultiHeadAttention {
    pub fn new(
        d_model: usize,
        num_heads: usize,
        num_kv_heads: usize,
        head_dim: usize,
        rope: Rope,
        rng: &mut StdRng,
    ) -> Result<Self, NetworkError> {
        if num_heads == 0 || num_kv_heads == 0 {
            return Err(NetworkError::InvalidConfig(
                "attention needs at least one query head and one key/value head".into(),
            ));
        }
        if num_kv_heads > num_heads || num_heads % num_kv_heads != 0 {
            return Err(NetworkError::InvalidConfig(format!(
                "num_heads ({num_heads}) must be a multiple of num_kv_heads ({num_kv_heads})"
            )));
        }
        if rope.head_dim() != head_dim {
            return Err(NetworkError::InvalidConfig(format!(
                "rope was built for head_dim {} but attention uses {head_dim}",
                rope.head_dim()
            )));
        }

        Ok(Self {
            query: Linear::new(d_model, num_heads * head_dim, rng),
            key: Linear::new(d_model, num_kv_heads * head_dim, rng),
            value: Linear::new(d_model, num_kv_heads * head_dim, rng),
            output: Linear::new(num_heads * head_dim, d_model, rng),
            rope,
            num_heads,
            num_kv_heads,
            head_dim,
        })
    }

    pub fn d_model(&self) -> usize {
        self.query.in_features()
    }

    pub fn num_heads(&self) -> usize {
        self.num_heads
    }

    pub fn num_kv_heads(&self) -> usize {
        self.num_kv_heads
    }

    pub fn head_dim(&self) -> usize {
        self.head_dim
    }

    /// How many query heads share one key/value head.
    fn group_size(&self) -> usize {
        self.num_heads / self.num_kv_heads
    }

    fn scale(&self) -> f32 {
        (self.head_dim as f32).sqrt().recip()
    }

    /// Full-sequence forward pass, keeping what the backward pass needs.
    ///
    /// `layout` says how the rows split into sequences; the default is one
    /// sequence covering every row. Attention is the only sub-layer where the
    /// split matters, because a token must not attend across a sequence
    /// boundary: a query at position `t` of its sequence sees keys `0..=t` of
    /// *that* sequence and nothing else.
    pub fn forward_train(
        &self,
        input: &Matrix,
        layout: Layout<'_>,
    ) -> Result<(Matrix, AttentionCache), NetworkError> {
        layout.check(input.rows)?;
        let seq_len = layout.seq_len(input.rows);
        let (queries, keys, values) = self.project_batched(input, seq_len)?;

        let mut probabilities = Vec::with_capacity(self.num_heads);
        let mut merged = Matrix::new(input.rows, self.num_heads * self.head_dim);

        for head in 0..self.num_heads {
            let scores = self.head_scores_batched(&queries, &keys, head, seq_len);
            self.accumulate_head_output_batched(&scores, &values, head, seq_len, &mut merged);
            probabilities.push(scores);
        }

        let output = self.output.forward(&merged);

        Ok((
            output,
            AttentionCache {
                input: input.clone(),
                queries,
                keys,
                values,
                probabilities,
                merged,
                seq_len,
            },
        ))
    }

    /// Incremental forward pass against a cache.
    ///
    /// `input` holds the new tokens only; the cache supplies the history. The
    /// first call with an empty cache and a full prompt is the prefill, every
    /// later call is normally a single row.
    pub fn forward_cached(
        &self,
        input: &Matrix,
        cache: &mut KvCache,
    ) -> Result<Matrix, NetworkError> {
        let position_offset = cache.len();
        let (queries, keys, values) = self.project(input, position_offset)?;
        cache.append(&keys, &values)?;

        let history = cache.len();
        let width = keys.cols;

        #[cfg(feature = "cuda")]
        if let Some(context) = self.gpu_context() {
            let cached_keys = Matrix::from_vec(history, width, cache.keys().to_vec());
            let cached_values = Matrix::from_vec(history, width, cache.values().to_vec());
            let (_, merged) = crate::gpu_transformer::attention_heads(
                &context,
                &queries,
                &cached_keys,
                &cached_values,
                self.num_heads,
                self.num_kv_heads,
                self.head_dim,
                position_offset,
                false,
            );
            return Ok(self.output.forward(&merged));
        }

        // The cache is read in place. Copying it into a `Matrix` per layer per
        // decoded token is O(history) memory traffic on top of O(history) math,
        // which dominates once the history is a few hundred tokens long.
        let mut merged = Matrix::new(input.rows, self.num_heads * self.head_dim);
        for head in 0..self.num_heads {
            let scores = self.head_scores(
                &queries,
                cache.keys(),
                width,
                head,
                position_offset,
                history,
            );
            self.accumulate_head_output(&scores, cache.values(), width, head, &mut merged);
        }

        Ok(self.output.forward(&merged))
    }

    /// Accumulates weight gradients and returns `dL/dinput`.
    ///
    /// Training always sees the whole sequence at once, so this is the
    /// counterpart of [`MultiHeadAttention::forward_train`] only.
    pub fn backward(
        &mut self,
        cache: &AttentionCache,
        grad_output: &Matrix,
    ) -> Result<Matrix, NetworkError> {
        let rows = cache.input.rows;
        let seq_len = cache.seq_len;
        let head_dim = self.head_dim;
        let scale = self.scale();
        let group_size = self.group_size();

        let grad_merged = self.output.backward(&cache.merged, grad_output);

        let mut grad_queries = Matrix::new(rows, self.num_heads * head_dim);
        let mut grad_keys = Matrix::new(rows, self.num_kv_heads * head_dim);
        let mut grad_values = Matrix::new(rows, self.num_kv_heads * head_dim);

        // One head of one sequence is a strided window of the packed
        // `[rows, heads * head_dim]` projections, and `sgemm` takes a row and a
        // column stride per operand, so each of the five products below reads
        // its head where it lies instead of gathering it into a temporary.
        // `dL/dscore` is the only scratch, and it is reused across heads.
        let sequences = rows / seq_len;
        let mut grad_scores = vec![0.0f32; seq_len * seq_len];

        for head in 0..self.num_heads {
            let probabilities = &cache.probabilities[head];
            let query_base = head * head_dim;
            let kv_base = (head / group_size) * head_dim;

            for sequence in 0..sequences {
                let probability_offset = sequence * seq_len * seq_len;
                let query_offset = sequence * seq_len * cache.queries.cols + query_base;
                let merged_offset = sequence * seq_len * grad_merged.cols + query_base;
                let key_offset = sequence * seq_len * cache.keys.cols + kv_base;
                let value_offset = sequence * seq_len * cache.values.cols + kv_base;

                unsafe {
                    // dL/dprobability = dL/dmerged_head * V_head^T.
                    matrixmultiply::sgemm(
                        seq_len,
                        head_dim,
                        seq_len,
                        1.0,
                        grad_merged.data.as_ptr().add(merged_offset),
                        grad_merged.cols as isize,
                        1,
                        cache.values.data.as_ptr().add(value_offset),
                        1,
                        cache.values.cols as isize,
                        0.0,
                        grad_scores.as_mut_ptr(),
                        seq_len as isize,
                        1,
                    );

                    // dL/dV_head += P^T * dL/dmerged_head. Accumulating rather
                    // than assigning is what makes grouped-query attention
                    // correct: `group_size` query heads share these columns.
                    matrixmultiply::sgemm(
                        seq_len,
                        seq_len,
                        head_dim,
                        1.0,
                        probabilities.data.as_ptr().add(probability_offset),
                        1,
                        seq_len as isize,
                        grad_merged.data.as_ptr().add(merged_offset),
                        grad_merged.cols as isize,
                        1,
                        1.0,
                        grad_values.data.as_mut_ptr().add(value_offset),
                        grad_values.cols as isize,
                        1,
                    );
                }

                // Softmax backward, in place over the whole square. A masked
                // entry has probability zero, so it stays zero here and the
                // causal mask needs no separate handling.
                for position in 0..seq_len {
                    let weights =
                        &probabilities.data[probability_offset + position * seq_len..][..seq_len];
                    let row = &mut grad_scores[position * seq_len..][..seq_len];
                    let dot: f32 = weights.iter().zip(row.iter()).map(|(p, g)| p * g).sum();
                    for (slot, &weight) in row.iter_mut().zip(weights) {
                        *slot = weight * (*slot - dot) * scale;
                    }
                }

                unsafe {
                    // dL/dQ_head += dL/dscore * K_head.
                    matrixmultiply::sgemm(
                        seq_len,
                        seq_len,
                        head_dim,
                        1.0,
                        grad_scores.as_ptr(),
                        seq_len as isize,
                        1,
                        cache.keys.data.as_ptr().add(key_offset),
                        cache.keys.cols as isize,
                        1,
                        1.0,
                        grad_queries.data.as_mut_ptr().add(query_offset),
                        grad_queries.cols as isize,
                        1,
                    );

                    // dL/dK_head += dL/dscore^T * Q_head, accumulating for the
                    // same grouped-query reason as the value gradient.
                    matrixmultiply::sgemm(
                        seq_len,
                        seq_len,
                        head_dim,
                        1.0,
                        grad_scores.as_ptr(),
                        1,
                        seq_len as isize,
                        cache.queries.data.as_ptr().add(query_offset),
                        cache.queries.cols as isize,
                        1,
                        1.0,
                        grad_keys.data.as_mut_ptr().add(key_offset),
                        grad_keys.cols as isize,
                        1,
                    );
                }
            }
        }

        // RoPE is orthogonal, so its backward pass is the same rotation
        // applied with the opposite sign.
        self.rope
            .apply_inverse_batched(&mut grad_queries, self.num_heads, seq_len)?;
        self.rope
            .apply_inverse_batched(&mut grad_keys, self.num_kv_heads, seq_len)?;

        let mut grad_input = self.query.backward(&cache.input, &grad_queries);
        let from_keys = self.key.backward(&cache.input, &grad_keys);
        let from_values = self.value.backward(&cache.input, &grad_values);
        for ((slot, k), v) in grad_input
            .data
            .iter_mut()
            .zip(&from_keys.data)
            .zip(&from_values.data)
        {
            *slot += k + v;
        }

        Ok(grad_input)
    }

    /// Every projection in this layer, for uploading to a device.
    #[cfg(feature = "cuda")]
    pub(crate) fn linears_mut(&mut self) -> Vec<&mut Linear> {
        vec![
            &mut self.query,
            &mut self.key,
            &mut self.value,
            &mut self.output,
        ]
    }

    /// The device the projections live on, if any. Attention borrows it for the
    /// two score matmuls; everything between them stays on the host.
    #[cfg(feature = "cuda")]
    fn gpu_context(&self) -> Option<std::sync::Arc<crate::gpu_transformer::GpuContext>> {
        self.query
            .weight
            .device
            .as_ref()
            .map(|device| device.context().clone())
    }

    pub fn params_mut(&mut self) -> Vec<&mut Param> {
        let mut params = self.query.params_mut();
        params.extend(self.key.params_mut());
        params.extend(self.value.params_mut());
        params.extend(self.output.params_mut());
        params
    }

    /// Q, K and V for the new tokens, with RoPE already applied to Q and K.
    fn project(
        &self,
        input: &Matrix,
        position_offset: usize,
    ) -> Result<(Matrix, Matrix, Matrix), NetworkError> {
        if input.cols != self.d_model() {
            return Err(NetworkError::InvalidInput {
                expected: self.d_model(),
                actual: input.cols,
            });
        }

        let mut queries = self.query.forward(input);
        let mut keys = self.key.forward(input);
        let values = self.value.forward(input);

        self.rope
            .apply(&mut queries, self.num_heads, position_offset)?;
        self.rope
            .apply(&mut keys, self.num_kv_heads, position_offset)?;

        Ok((queries, keys, values))
    }

    /// Q, K and V for a packed batch, with RoPE already applied to Q and K.
    fn project_batched(
        &self,
        input: &Matrix,
        seq_len: usize,
    ) -> Result<(Matrix, Matrix, Matrix), NetworkError> {
        if input.cols != self.d_model() {
            return Err(NetworkError::InvalidInput {
                expected: self.d_model(),
                actual: input.cols,
            });
        }

        let mut queries = self.query.forward(input);
        let mut keys = self.key.forward(input);
        let values = self.value.forward(input);

        self.rope
            .apply_batched(&mut queries, self.num_heads, seq_len)?;
        self.rope
            .apply_batched(&mut keys, self.num_kv_heads, seq_len)?;

        Ok((queries, keys, values))
    }

    /// Softmaxed attention weights for one head of a packed batch,
    /// `[batch * seq_len, seq_len]`.
    ///
    /// Column `k` of row `r` is the weight the query at position `r % seq_len`
    /// puts on key `k` *of its own sequence*. Masked entries are exactly zero
    /// rather than `-inf` exponentiated, so nothing downstream has to carry the
    /// mask around and no numerical accident can soften it.
    fn head_scores_batched(
        &self,
        queries: &Matrix,
        keys: &Matrix,
        head: usize,
        seq_len: usize,
    ) -> Matrix {
        let head_dim = self.head_dim;
        let query_base = head * head_dim;
        let kv_base = (head / self.group_size()) * head_dim;
        let scale = self.scale();
        let sequences = queries.rows / seq_len;

        let mut scores = Matrix::new(queries.rows, seq_len);
        for sequence in 0..sequences {
            let query_offset = sequence * seq_len * queries.cols + query_base;
            let key_offset = sequence * seq_len * keys.cols + kv_base;
            let score_offset = sequence * seq_len * seq_len;

            // Q_head times K_head transposed, both read in place out of the
            // packed projections through `sgemm`'s row and column strides. The
            // masked upper triangle is computed and then discarded, which costs
            // half the multiplies but buys a single blocked, threaded kernel in
            // place of `seq_len` growing dot-product loops.
            unsafe {
                matrixmultiply::sgemm(
                    seq_len,
                    head_dim,
                    seq_len,
                    scale,
                    queries.data.as_ptr().add(query_offset),
                    queries.cols as isize,
                    1,
                    keys.data.as_ptr().add(key_offset),
                    1,
                    keys.cols as isize,
                    0.0,
                    scores.data.as_mut_ptr().add(score_offset),
                    seq_len as isize,
                    1,
                );
            }
        }

        // Normalizing afterwards rather than inside the loop above puts every
        // row of every sequence in one parallel pass. `softmax` is an `exp` per
        // element, and the masked tail is cleared to an exact zero so that
        // nothing downstream has to know where the causal boundary is.
        scores
            .data
            .par_chunks_mut(seq_len)
            .enumerate()
            .for_each(|(row, weights)| {
                let position = row % seq_len;
                softmax(&mut weights[..position + 1]);
                weights[position + 1..].fill(0.0);
            });

        scores
    }

    /// `merged[:, head] += probabilities * V_head`, per sequence.
    fn accumulate_head_output_batched(
        &self,
        probabilities: &Matrix,
        values: &Matrix,
        head: usize,
        seq_len: usize,
        merged: &mut Matrix,
    ) {
        let head_dim = self.head_dim;
        let query_base = head * head_dim;
        let kv_base = (head / self.group_size()) * head_dim;
        let sequences = probabilities.rows / seq_len;

        for sequence in 0..sequences {
            // Masked weights are exactly zero, so the whole square takes part
            // in the multiply and the causal mask costs nothing here. `beta` is
            // one because the head writes its own column range of `merged`.
            unsafe {
                matrixmultiply::sgemm(
                    seq_len,
                    seq_len,
                    head_dim,
                    1.0,
                    probabilities
                        .data
                        .as_ptr()
                        .add(sequence * seq_len * seq_len),
                    seq_len as isize,
                    1,
                    values
                        .data
                        .as_ptr()
                        .add(sequence * seq_len * values.cols + kv_base),
                    values.cols as isize,
                    1,
                    1.0,
                    merged
                        .data
                        .as_mut_ptr()
                        .add(sequence * seq_len * merged.cols + query_base),
                    merged.cols as isize,
                    1,
                );
            }
        }
    }

    /// Softmaxed attention weights for one head, `[queries, history]`.
    ///
    /// Row `i` describes the query at absolute position
    /// `position_offset + i`, which may attend to every key up to and including
    /// its own position. Masked entries are left at zero rather than set to
    /// `-inf` and exponentiated, so the causal structure is in the loop bounds
    /// and cannot be softened by a numerical accident.
    fn head_scores(
        &self,
        queries: &Matrix,
        keys: &[f32],
        key_cols: usize,
        head: usize,
        position_offset: usize,
        history: usize,
    ) -> Matrix {
        let head_dim = self.head_dim;
        let query_base = head * head_dim;
        let kv_base = (head / self.group_size()) * head_dim;
        let scale = self.scale();

        let mut scores = Matrix::new(queries.rows, history);
        for query in 0..queries.rows {
            let query_row = &queries.row(query)[query_base..query_base + head_dim];
            let visible = position_offset + query + 1;
            debug_assert!(visible <= history);

            let row = scores.row_mut(query);
            for (key, slot) in row.iter_mut().enumerate().take(visible) {
                let base = key * key_cols + kv_base;
                let key_row = &keys[base..base + head_dim];
                *slot = crate::matrix::dot(query_row, key_row) * scale;
            }
            softmax(&mut row[..visible]);
        }

        scores
    }

    /// `merged[:, head] += probabilities * V_head`.
    fn accumulate_head_output(
        &self,
        probabilities: &Matrix,
        values: &[f32],
        value_cols: usize,
        head: usize,
        merged: &mut Matrix,
    ) {
        let head_dim = self.head_dim;
        let query_base = head * head_dim;
        let kv_base = (head / self.group_size()) * head_dim;

        for query in 0..probabilities.rows {
            let weights = probabilities.row(query);
            let target = &mut merged.row_mut(query)[query_base..query_base + head_dim];

            for (key, &weight) in weights.iter().enumerate() {
                if weight == 0.0 {
                    continue;
                }
                let base = key * value_cols + kv_base;
                let value_row = &values[base..base + head_dim];
                for (slot, &value) in target.iter_mut().zip(value_row) {
                    *slot += weight * value;
                }
            }
        }
    }
}

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

    fn attention(num_heads: usize, num_kv_heads: usize) -> MultiHeadAttention {
        let mut rng = StdRng::seed_from_u64(11);
        let head_dim = 4;
        let rope = Rope::new(head_dim, 32, 10000.0).unwrap();
        MultiHeadAttention::new(8, num_heads, num_kv_heads, head_dim, rope, &mut rng).unwrap()
    }

    fn inputs(tokens: usize, d_model: usize) -> Matrix {
        // Deterministic, spread over both signs, and not symmetric in a way
        // that could hide an indexing mistake.
        Matrix::from_vec(
            tokens,
            d_model,
            (0..tokens * d_model)
                .map(|i| ((i * 37) % 23) as f32 / 11.0 - 1.0)
                .collect(),
        )
    }

    #[test]
    fn output_keeps_the_model_dimension() {
        let layer = attention(2, 2);
        let (output, _) = layer
            .forward_train(&inputs(5, 8), Layout::default())
            .unwrap();

        assert_eq!(output.rows, 5);
        assert_eq!(output.cols, 8);
    }

    #[test]
    fn grouped_query_heads_must_divide_evenly() {
        let mut rng = StdRng::seed_from_u64(1);
        let rope = Rope::new(4, 32, 10000.0).unwrap();

        assert!(MultiHeadAttention::new(8, 6, 4, 4, rope.clone(), &mut rng).is_err());
        assert!(MultiHeadAttention::new(8, 4, 8, 4, rope, &mut rng).is_err());
    }

    #[test]
    fn attention_weights_sum_to_one_over_the_visible_prefix() {
        let layer = attention(2, 1);
        let (_, cache) = layer
            .forward_train(&inputs(4, 8), Layout::default())
            .unwrap();

        for head in &cache.probabilities {
            for query in 0..head.rows {
                let total: f32 = head.row(query).iter().sum();
                assert!((total - 1.0).abs() < 1e-5);
            }
        }
    }

    #[test]
    fn the_causal_mask_zeroes_every_future_key() {
        let layer = attention(2, 2);
        let (_, cache) = layer
            .forward_train(&inputs(5, 8), Layout::default())
            .unwrap();

        for head in &cache.probabilities {
            for query in 0..head.rows {
                for key in (query + 1)..head.cols {
                    assert_eq!(head.row(query)[key], 0.0, "query {query} saw key {key}");
                }
            }
        }
    }

    #[test]
    fn a_later_token_cannot_change_an_earlier_output() {
        let layer = attention(2, 1);
        let original = inputs(4, 8);
        let (before, _) = layer.forward_train(&original, Layout::default()).unwrap();

        // Rewrite the last token entirely. Everything before it must be
        // bit-for-bit unchanged, which is the property the mask exists for.
        let mut edited = original.clone();
        for value in edited.row_mut(3) {
            *value = 9.0;
        }
        let (after, _) = layer.forward_train(&edited, Layout::default()).unwrap();

        assert_eq!(&before.data[..3 * 8], &after.data[..3 * 8]);
        assert_ne!(before.row(3), after.row(3));
    }

    #[test]
    fn a_cached_decode_reproduces_the_full_sequence_forward() {
        let layer = attention(4, 2);
        let sequence = inputs(6, 8);
        let (expected, _) = layer.forward_train(&sequence, Layout::default()).unwrap();

        let mut cache = KvCache::new(2, 4);
        for token in 0..sequence.rows {
            let step = Matrix::from_vec(1, 8, sequence.row(token).to_vec());
            let output = layer.forward_cached(&step, &mut cache).unwrap();

            assert_eq!(cache.len(), token + 1);
            for (actual, expected) in output.data.iter().zip(expected.row(token)) {
                assert!(
                    (actual - expected).abs() < 1e-5,
                    "token {token}: {actual} vs {expected}"
                );
            }
        }
    }

    #[test]
    fn a_prefill_then_decode_reproduces_the_full_sequence_forward() {
        let layer = attention(2, 2);
        let sequence = inputs(5, 8);
        let (expected, _) = layer.forward_train(&sequence, Layout::default()).unwrap();

        let mut cache = KvCache::new(2, 4);
        let prompt = Matrix::from_vec(3, 8, sequence.data[..3 * 8].to_vec());
        layer.forward_cached(&prompt, &mut cache).unwrap();

        for token in 3..sequence.rows {
            let step = Matrix::from_vec(1, 8, sequence.row(token).to_vec());
            let output = layer.forward_cached(&step, &mut cache).unwrap();
            for (actual, expected) in output.data.iter().zip(expected.row(token)) {
                assert!((actual - expected).abs() < 1e-5);
            }
        }
    }

    #[test]
    fn backward_matches_finite_differences() {
        let mut layer = attention(2, 1);
        let input = inputs(4, 8);

        let (output, cache) = layer.forward_train(&input, Layout::default()).unwrap();
        let grad_output = Matrix::from_vec(output.rows, output.cols, vec![1.0; output.data.len()]);
        let grad_input = layer.backward(&cache, &grad_output).unwrap();

        let epsilon = 1e-3;
        for index in 0..input.data.len() {
            let mut bumped = input.clone();
            bumped.data[index] += epsilon;
            let high: f32 = layer
                .forward_train(&bumped, Layout::default())
                .unwrap()
                .0
                .data
                .iter()
                .sum();
            bumped.data[index] -= 2.0 * epsilon;
            let low: f32 = layer
                .forward_train(&bumped, Layout::default())
                .unwrap()
                .0
                .data
                .iter()
                .sum();
            let numeric = (high - low) / (2.0 * epsilon);
            assert!(
                (grad_input.data[index] - numeric).abs() < 2e-2,
                "index {index}: {} vs {numeric}",
                grad_input.data[index]
            );
        }
    }

    #[test]
    fn query_weight_gradient_matches_finite_differences() {
        let mut layer = attention(2, 2);
        let input = inputs(3, 8);

        let (output, cache) = layer.forward_train(&input, Layout::default()).unwrap();
        let grad_output = Matrix::from_vec(output.rows, output.cols, vec![1.0; output.data.len()]);
        layer.backward(&cache, &grad_output).unwrap();
        let analytic = layer.query.weight.grad.data.clone();

        let epsilon = 1e-3;
        for (index, &expected) in analytic.iter().enumerate() {
            let mut probe = layer.clone();
            probe.query.weight.value.data[index] += epsilon;
            let high: f32 = probe
                .forward_train(&input, Layout::default())
                .unwrap()
                .0
                .data
                .iter()
                .sum();
            probe.query.weight.value.data[index] -= 2.0 * epsilon;
            let low: f32 = probe
                .forward_train(&input, Layout::default())
                .unwrap()
                .0
                .data
                .iter()
                .sum();
            let numeric = (high - low) / (2.0 * epsilon);
            assert!(
                (expected - numeric).abs() < 2e-2,
                "index {index}: {} vs {numeric}",
                expected
            );
        }
    }

    #[test]
    fn a_cache_rejects_the_wrong_width() {
        let mut cache = KvCache::new(2, 4);
        assert!(
            cache
                .append(&Matrix::new(1, 4), &Matrix::new(1, 4))
                .is_err()
        );
    }
}