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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
//! 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
}
/// Keeps the first `positions` positions and drops the rest, so a caller
/// can take back tokens it fed, such as rejected speculative ones.
pub fn truncate(&mut self, positions: usize) {
self.length = self.length.min(positions);
self.keys.truncate(self.length * self.width);
self.values.truncate(self.length * self.width);
}
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,
}
/// Everything the backward pass needs from a cross-attention forward pass.
///
/// The same contents as [`AttentionCache`], except that the two token sets are
/// kept separately, because the backward pass has to hand a gradient back to
/// each of them. Both are needed even when the key side is a frozen encoder:
/// the projections still want their weight gradients.
#[derive(Clone, Debug)]
pub struct CrossAttentionCache {
queries_in: Matrix,
kv_in: Matrix,
queries: Matrix,
keys: Matrix,
values: Matrix,
/// `[head][queries, kv_len]`, one softmax row per query. Nothing is
/// masked, so every row sums to one over the whole key set.
probabilities: Vec<Matrix>,
merged: Matrix,
q_len: usize,
kv_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,
/// Whether a query may only read keys at or before its own position.
///
/// False is the encoder shape: every position reads the whole sequence.
/// Defaults to true on load, so a snapshot written before this existed
/// restores as the decoder it was.
#[serde(default = "causal_by_default")]
causal: bool,
/// Whether rotary positions are applied to Q and K.
///
/// False is the set shape: the rows are an unordered collection rather
/// than a sequence, which is what a stack of learned latent queries is.
/// Rotating them would impose an order the model then has to learn around.
/// Defaults to true on load, for the same reason `causal` does.
#[serde(default = "causal_by_default")]
rope_enabled: bool,
}
pub(crate) fn causal_by_default() -> bool {
true
}
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.is_multiple_of(num_kv_heads) {
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,
causal: true,
rope_enabled: true,
})
}
/// Builds a cross-attention layer: queries come from one token set,
/// keys and values from another, which may be a different width.
///
/// `d_kv` is the width of the token set being read — 768 for a ViT-B
/// tower feeding a narrower denoiser, say. The layer is non-causal and
/// applies no rotary positions: the two sides have unrelated position
/// systems, and whatever the key side's positions mean is already baked
/// into the tokens by the encoder that produced them.
///
/// `rope` is still required, because [`MultiHeadAttention`] carries one
/// field for both shapes; clone the one the surrounding self-attention
/// already built. Nothing reads it while `rope_enabled` is false.
///
/// ```
/// # use rusting_brain::{MultiHeadAttention, Rope};
/// # use rand::SeedableRng;
/// # let mut rng = rand::rngs::StdRng::seed_from_u64(0);
/// let rope = Rope::new(8, 16, 10000.0)?;
/// let layer = MultiHeadAttention::cross(64, 96, 4, 4, 8, rope, &mut rng)?;
/// assert_eq!(layer.kv_features(), 96);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn cross(
d_model: usize,
d_kv: usize,
num_heads: usize,
num_kv_heads: usize,
head_dim: usize,
rope: Rope,
rng: &mut StdRng,
) -> Result<Self, NetworkError> {
let mut layer = Self::new(d_model, num_heads, num_kv_heads, head_dim, rope, rng)?;
layer.key = Linear::new(d_kv, num_kv_heads * head_dim, rng);
layer.value = Linear::new(d_kv, num_kv_heads * head_dim, rng);
layer.causal = false;
layer.rope_enabled = false;
Ok(layer)
}
/// Drops the causal mask, so every position reads the whole sequence.
///
/// This is what separates an encoder from a decoder. Nothing else about
/// the layer changes: the backward pass already treats a masked weight as
/// a zero probability rather than as a special case, so it needs no
/// knowledge of which shape it is differentiating.
pub fn set_causal(&mut self, causal: bool) {
self.causal = causal;
}
pub fn is_causal(&self) -> bool {
self.causal
}
/// Turns rotary positions off, which is what a set of tokens with no
/// order wants. See the `rope_enabled` field.
pub fn set_rope_enabled(&mut self, enabled: bool) {
self.rope_enabled = enabled;
}
pub fn is_rope_enabled(&self) -> bool {
self.rope_enabled
}
pub fn d_model(&self) -> usize {
self.query.in_features()
}
/// Width of the token set the keys and values are read from. Equal to
/// [`d_model`](Self::d_model) for self-attention.
pub fn kv_features(&self) -> usize {
self.key.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> {
if !self.causal {
return Err(NetworkError::InvalidConfig(
"a bidirectional layer cannot decode from a cache: every position reads the \
whole sequence, so an appended token changes the tokens before it"
.into(),
));
}
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.
if self.rope_enabled {
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)
}
/// Full forward pass reading one token set from another.
///
/// `queries_in` is `[sequences * q_len, d_model]` and `kv_in` is
/// `[sequences * kv_len, kv_features]`, and the two have to agree on how
/// many sequences they hold. Query `i` of a sequence reads every key of
/// *that* sequence and nothing from its neighbours.
///
/// Neither the causal mask nor the rotary rotation applies here whatever
/// [`set_causal`](Self::set_causal) and
/// [`set_rope_enabled`](Self::set_rope_enabled) were told: a mask over two
/// unrelated position systems has no meaning, and a query set that reads
/// an encoder's output has no shared coordinate to rotate against.
pub fn forward_train_cross(
&self,
queries_in: &Matrix,
kv_in: &Matrix,
q_len: usize,
kv_len: usize,
) -> Result<(Matrix, CrossAttentionCache), NetworkError> {
self.check_cross(queries_in, kv_in, q_len, kv_len)?;
let queries = self.query.forward(queries_in);
let keys = self.key.forward(kv_in);
let values = self.value.forward(kv_in);
let mut probabilities = Vec::with_capacity(self.num_heads);
let mut merged = Matrix::new(queries_in.rows, self.num_heads * self.head_dim);
for head in 0..self.num_heads {
let scores = self.cross_head_scores(&queries, &keys, head, q_len, kv_len);
self.accumulate_cross_head_output(&scores, &values, head, q_len, kv_len, &mut merged);
probabilities.push(scores);
}
let output = self.output.forward(&merged);
Ok((
output,
CrossAttentionCache {
queries_in: queries_in.clone(),
kv_in: kv_in.clone(),
queries,
keys,
values,
probabilities,
merged,
q_len,
kv_len,
},
))
}
/// Accumulates weight gradients and returns `(dL/dqueries_in, dL/dkv_in)`.
///
/// The second gradient is what a caller drops when the key side is a
/// frozen encoder, and what it propagates when the key side trains. The
/// projections accumulate their own weight gradients either way.
pub fn backward_cross(
&mut self,
cache: &CrossAttentionCache,
grad_output: &Matrix,
) -> Result<(Matrix, Matrix), NetworkError> {
let head_dim = self.head_dim;
let scale = self.scale();
let group_size = self.group_size();
let q_len = cache.q_len;
let kv_len = cache.kv_len;
let grad_merged = self.output.backward(&cache.merged, grad_output);
let mut grad_queries = Matrix::new(cache.queries.rows, self.num_heads * head_dim);
let mut grad_keys = Matrix::new(cache.keys.rows, self.num_kv_heads * head_dim);
let mut grad_values = Matrix::new(cache.values.rows, self.num_kv_heads * head_dim);
// Laid out exactly like the self-attention backward above: a head is a
// strided window of the packed projections, `sgemm` reads it where it
// lies, and `grad_scores` is the only scratch. The one structural
// difference is that the score block is `[q_len, kv_len]` rather than
// square, so the two sides index with different lengths.
let (query_cols, kv_cols) = (grad_queries.cols, grad_keys.cols);
// One task per sequence, every head inside it: each sequence owns its
// rows of the three gradients, so the tasks never write the same slot.
grad_queries
.data
.par_chunks_mut(q_len * query_cols)
.zip(grad_keys.data.par_chunks_mut(kv_len * kv_cols))
.zip(grad_values.data.par_chunks_mut(kv_len * kv_cols))
.enumerate()
.for_each(|(sequence, ((grad_queries, grad_keys), grad_values))| {
let mut grad_scores = vec![0.0f32; q_len * kv_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;
let probability_offset = sequence * q_len * kv_len;
let query_offset = sequence * q_len * cache.queries.cols + query_base;
let merged_offset = sequence * q_len * grad_merged.cols + query_base;
let key_offset = sequence * kv_len * cache.keys.cols + kv_base;
let value_offset = sequence * kv_len * cache.values.cols + kv_base;
unsafe {
// dL/dprobability = dL/dmerged_head * V_head^T.
matrixmultiply::sgemm(
q_len,
head_dim,
kv_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(),
kv_len as isize,
1,
);
// dL/dV_head += P^T * dL/dmerged_head, accumulating
// because `group_size` query heads share these columns.
matrixmultiply::sgemm(
kv_len,
q_len,
head_dim,
1.0,
probabilities.data.as_ptr().add(probability_offset),
1,
kv_len as isize,
grad_merged.data.as_ptr().add(merged_offset),
grad_merged.cols as isize,
1,
1.0,
grad_values.as_mut_ptr().add(kv_base),
kv_cols as isize,
1,
);
}
// Softmax backward, in place. Nothing is masked here, so
// every entry of the block takes part.
for position in 0..q_len {
let weights =
&probabilities.data[probability_offset + position * kv_len..][..kv_len];
let row = &mut grad_scores[position * kv_len..][..kv_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(
q_len,
kv_len,
head_dim,
1.0,
grad_scores.as_ptr(),
kv_len as isize,
1,
cache.keys.data.as_ptr().add(key_offset),
cache.keys.cols as isize,
1,
1.0,
grad_queries.as_mut_ptr().add(query_base),
query_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(
kv_len,
q_len,
head_dim,
1.0,
grad_scores.as_ptr(),
1,
kv_len as isize,
cache.queries.data.as_ptr().add(query_offset),
cache.queries.cols as isize,
1,
1.0,
grad_keys.as_mut_ptr().add(kv_base),
kv_cols as isize,
1,
);
}
}
});
// No inverse rotation: `forward_train_cross` applied none.
let grad_queries_in = self.query.backward(&cache.queries_in, &grad_queries);
let mut grad_kv_in = self.key.backward(&cache.kv_in, &grad_keys);
let from_values = self.value.backward(&cache.kv_in, &grad_values);
for (slot, value) in grad_kv_in.data.iter_mut().zip(&from_values.data) {
*slot += value;
}
Ok((grad_queries_in, grad_kv_in))
}
/// Validates a cross-attention call and returns how many sequences it packs.
fn check_cross(
&self,
queries_in: &Matrix,
kv_in: &Matrix,
q_len: usize,
kv_len: usize,
) -> Result<usize, NetworkError> {
if queries_in.cols != self.d_model() {
return Err(NetworkError::InvalidInput {
expected: self.d_model(),
actual: queries_in.cols,
});
}
if kv_in.cols != self.kv_features() {
return Err(NetworkError::InvalidInput {
expected: self.kv_features(),
actual: kv_in.cols,
});
}
if q_len == 0 || kv_len == 0 {
return Err(NetworkError::InvalidConfig(
"cross-attention needs at least one query and one key per sequence".into(),
));
}
if !queries_in.rows.is_multiple_of(q_len) || !kv_in.rows.is_multiple_of(kv_len) {
return Err(NetworkError::InvalidConfig(format!(
"{} query rows and {} key rows do not split into sequences of {q_len} and {kv_len}",
queries_in.rows, kv_in.rows
)));
}
let sequences = queries_in.rows / q_len;
if sequences != kv_in.rows / kv_len {
return Err(NetworkError::InvalidConfig(format!(
"{sequences} query sequences against {} key sequences",
kv_in.rows / kv_len
)));
}
Ok(sequences)
}
/// Softmaxed cross-attention weights for one head, `[queries, kv_len]`.
///
/// Nothing is masked, so every row is a full softmax over the key set of
/// its own sequence.
fn cross_head_scores(
&self,
queries: &Matrix,
keys: &Matrix,
head: usize,
q_len: usize,
kv_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();
// One task per sequence. A caller that attends across a handful of
// positions per pixel packs thousands of short sequences, and a
// serial loop of tiny `sgemm` calls leaves every other core idle.
let mut scores = Matrix::new(queries.rows, kv_len);
scores
.data
.par_chunks_mut(q_len * kv_len)
.enumerate()
.for_each(|(sequence, block)| {
let query_offset = sequence * q_len * queries.cols + query_base;
let key_offset = sequence * kv_len * keys.cols + kv_base;
unsafe {
matrixmultiply::sgemm(
q_len,
head_dim,
kv_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,
block.as_mut_ptr(),
kv_len as isize,
1,
);
}
});
scores.data.par_chunks_mut(kv_len).for_each(softmax);
scores
}
/// `merged[:, head] += probabilities * V_head`, per sequence.
fn accumulate_cross_head_output(
&self,
probabilities: &Matrix,
values: &Matrix,
head: usize,
q_len: usize,
kv_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 cols = merged.cols;
// A sequence's rows of `merged` are one contiguous block, so each task
// owns the block it writes.
merged
.data
.par_chunks_mut(q_len * cols)
.enumerate()
.for_each(|(sequence, block)| unsafe {
matrixmultiply::sgemm(
q_len,
kv_len,
head_dim,
1.0,
probabilities.data.as_ptr().add(sequence * q_len * kv_len),
kv_len as isize,
1,
values
.data
.as_ptr()
.add(sequence * kv_len * values.cols + kv_base),
values.cols as isize,
1,
1.0,
block.as_mut_ptr().add(query_base),
cols as isize,
1,
);
});
}
/// Every projection in this layer, for uploading to a device or quantizing.
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);
if self.rope_enabled {
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);
if self.rope_enabled {
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 visible = if self.causal {
row % seq_len + 1
} else {
seq_len
};
softmax(&mut weights[..visible]);
weights[visible..].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_bidirectional_layer_lets_a_later_token_change_an_earlier_output() {
let mut layer = attention(2, 1);
layer.set_causal(false);
let original = inputs(4, 8);
let (before, _) = layer.forward_train(&original, Layout::default()).unwrap();
let mut edited = original.clone();
for value in edited.row_mut(3) {
*value = 9.0;
}
let (after, _) = layer.forward_train(&edited, Layout::default()).unwrap();
// The exact opposite of the causal case: every earlier position reads
// the token that changed, so every output moves.
for token in 0..4 {
assert_ne!(before.row(token), after.row(token), "token {token}");
}
}
#[test]
fn a_bidirectional_layer_refuses_to_decode_from_a_cache() {
let mut layer = attention(2, 1);
layer.set_causal(false);
let mut cache = KvCache::new(1, 4);
assert!(layer.forward_cached(&inputs(1, 8), &mut cache).is_err());
}
#[test]
fn bidirectional_backward_matches_finite_differences() {
let mut layer = attention(2, 1);
layer.set_causal(false);
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 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()
);
}
// --- cross-attention ---------------------------------------------------
fn cross(num_heads: usize, num_kv_heads: usize) -> MultiHeadAttention {
let mut rng = StdRng::seed_from_u64(7);
let head_dim = 4;
let rope = Rope::new(head_dim, 32, 10000.0).unwrap();
// d_model 8, key/value side 6: the widths must be allowed to differ.
MultiHeadAttention::cross(8, 6, num_heads, num_kv_heads, head_dim, rope, &mut rng).unwrap()
}
/// A scalar to differentiate: the sum of the output weighted by a fixed,
/// non-uniform pattern, so no gradient can cancel itself out of the total.
fn weights(rows: usize, cols: usize) -> Matrix {
Matrix::from_vec(
rows,
cols,
(0..rows * cols)
.map(|i| ((i * 13) % 17) as f32 / 8.0 - 1.0)
.collect(),
)
}
fn objective(output: &Matrix, weight: &Matrix) -> f32 {
output
.data
.iter()
.zip(&weight.data)
.map(|(value, weight)| value * weight)
.sum()
}
#[test]
fn cross_output_keeps_the_query_shape() {
let layer = cross(2, 2);
let (output, cache) = layer
.forward_train_cross(&inputs(6, 8), &inputs(4, 6), 3, 2)
.unwrap();
assert_eq!(output.rows, 6);
assert_eq!(output.cols, 8);
assert_eq!(cache.probabilities[0].rows, 6);
assert_eq!(cache.probabilities[0].cols, 2);
}
#[test]
fn cross_attends_to_every_key_of_its_own_sequence_only() {
let layer = cross(2, 1);
let (_, cache) = layer
.forward_train_cross(&inputs(4, 8), &inputs(6, 6), 2, 3)
.unwrap();
// Two sequences of two queries, each reading three keys, and every row
// is a full softmax: no mask, so nothing is zero and each row sums to
// one over its own three keys.
for head in &cache.probabilities {
for row in 0..head.rows {
let total: f32 = head.row(row).iter().sum();
assert!((total - 1.0).abs() < 1e-5, "row {row} sums to {total}");
assert!(head.row(row).iter().all(|&weight| weight > 0.0));
}
}
}
#[test]
fn cross_rejects_mismatched_sequence_counts() {
let layer = cross(2, 2);
// Two query sequences of two against three key sequences of two.
assert!(
layer
.forward_train_cross(&inputs(4, 8), &inputs(6, 6), 2, 2)
.is_err()
);
}
#[test]
fn cross_rejects_the_wrong_key_width() {
let layer = cross(2, 2);
assert!(
layer
.forward_train_cross(&inputs(4, 8), &inputs(4, 8), 2, 2)
.is_err()
);
}
#[test]
fn cross_query_gradient_matches_finite_differences() {
let mut layer = cross(4, 2);
let (q_len, kv_len) = (3, 4);
let mut queries_in = inputs(6, 8);
let kv_in = inputs(8, 6);
let weight = weights(6, 8);
let (_, cache) = layer
.forward_train_cross(&queries_in, &kv_in, q_len, kv_len)
.unwrap();
let (grad_queries_in, _) = layer.backward_cross(&cache, &weight).unwrap();
let epsilon = 1e-3;
for index in [0, 5, 17, 31, 47] {
let original = queries_in.data[index];
queries_in.data[index] = original + epsilon;
let (high, _) = layer
.forward_train_cross(&queries_in, &kv_in, q_len, kv_len)
.unwrap();
queries_in.data[index] = original - epsilon;
let (low, _) = layer
.forward_train_cross(&queries_in, &kv_in, q_len, kv_len)
.unwrap();
queries_in.data[index] = original;
let numeric = (objective(&high, &weight) - objective(&low, &weight)) / (2.0 * epsilon);
assert!(
(grad_queries_in.data[index] - numeric).abs() < 1e-2,
"index {index}: {} vs {numeric}",
grad_queries_in.data[index]
);
}
}
#[test]
fn cross_key_side_gradient_matches_finite_differences() {
let mut layer = cross(4, 2);
let (q_len, kv_len) = (3, 4);
let queries_in = inputs(6, 8);
let mut kv_in = inputs(8, 6);
let weight = weights(6, 8);
let (_, cache) = layer
.forward_train_cross(&queries_in, &kv_in, q_len, kv_len)
.unwrap();
let (_, grad_kv_in) = layer.backward_cross(&cache, &weight).unwrap();
let epsilon = 1e-3;
for index in [0, 7, 19, 30, 41] {
let original = kv_in.data[index];
kv_in.data[index] = original + epsilon;
let (high, _) = layer
.forward_train_cross(&queries_in, &kv_in, q_len, kv_len)
.unwrap();
kv_in.data[index] = original - epsilon;
let (low, _) = layer
.forward_train_cross(&queries_in, &kv_in, q_len, kv_len)
.unwrap();
kv_in.data[index] = original;
let numeric = (objective(&high, &weight) - objective(&low, &weight)) / (2.0 * epsilon);
assert!(
(grad_kv_in.data[index] - numeric).abs() < 1e-2,
"index {index}: {} vs {numeric}",
grad_kv_in.data[index]
);
}
}
#[test]
fn cross_value_weight_gradient_matches_finite_differences() {
let mut layer = cross(4, 2);
let (q_len, kv_len) = (3, 4);
let queries_in = inputs(6, 8);
let kv_in = inputs(8, 6);
let weight = weights(6, 8);
let (_, cache) = layer
.forward_train_cross(&queries_in, &kv_in, q_len, kv_len)
.unwrap();
layer.backward_cross(&cache, &weight).unwrap();
let epsilon = 1e-3;
for index in [0, 3, 11, 23] {
let expected = layer.value.weight.grad.data[index];
let original = layer.value.weight.value.data[index];
layer.value.weight.value.data[index] = original + epsilon;
let (high, _) = layer
.forward_train_cross(&queries_in, &kv_in, q_len, kv_len)
.unwrap();
layer.value.weight.value.data[index] = original - epsilon;
let (low, _) = layer
.forward_train_cross(&queries_in, &kv_in, q_len, kv_len)
.unwrap();
layer.value.weight.value.data[index] = original;
let numeric = (objective(&high, &weight) - objective(&low, &weight)) / (2.0 * epsilon);
assert!(
(expected - numeric).abs() < 1e-2,
"index {index}: {expected} vs {numeric}"
);
}
}
#[test]
fn grouped_cross_heads_share_one_key_head() {
// Four query heads over two key/value heads: the shared columns are
// accumulated into, and a gradient that overwrote instead of adding
// would come out of this check half the size it should be.
let mut layer = cross(4, 2);
let (q_len, kv_len) = (2, 3);
let queries_in = inputs(2, 8);
let mut kv_in = inputs(3, 6);
let weight = weights(2, 8);
let (_, cache) = layer
.forward_train_cross(&queries_in, &kv_in, q_len, kv_len)
.unwrap();
let (_, grad_kv_in) = layer.backward_cross(&cache, &weight).unwrap();
let epsilon = 1e-3;
for index in [1, 9, 14] {
let original = kv_in.data[index];
kv_in.data[index] = original + epsilon;
let (high, _) = layer
.forward_train_cross(&queries_in, &kv_in, q_len, kv_len)
.unwrap();
kv_in.data[index] = original - epsilon;
let (low, _) = layer
.forward_train_cross(&queries_in, &kv_in, q_len, kv_len)
.unwrap();
kv_in.data[index] = original;
let numeric = (objective(&high, &weight) - objective(&low, &weight)) / (2.0 * epsilon);
assert!(
(grad_kv_in.data[index] - numeric).abs() < 1e-2,
"index {index}: {} vs {numeric}",
grad_kv_in.data[index]
);
}
}
#[test]
fn rope_can_be_turned_off_for_self_attention() {
// A set of rows has no order: with rotary positions off, permuting the
// rows permutes the output and changes nothing else.
let mut layer = attention(2, 2);
layer.set_causal(false);
layer.set_rope_enabled(false);
let rows = inputs(3, 8);
let (straight, _) = layer.forward_train(&rows, Layout::default()).unwrap();
let mut swapped = Matrix::new(3, 8);
for (target, source) in [0usize, 2, 1].iter().enumerate() {
swapped.row_mut(target).copy_from_slice(rows.row(*source));
}
let (permuted, _) = layer.forward_train(&swapped, Layout::default()).unwrap();
for (target, source) in [0usize, 2, 1].iter().enumerate() {
for column in 0..8 {
let expected = straight.row(*source)[column];
let actual = permuted.row(target)[column];
assert!(
(expected - actual).abs() < 1e-5,
"row {target}, column {column}: {actual} vs {expected}"
);
}
}
}
}