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
//! Trainable tensors for the transformer stack.
//!
//! [`Network`](crate::network::Network) keeps its gradients in side tables and
//! stores them already negated, because its update rule adds them. The
//! transformer layers are a deeper tree of modules, so they carry their
//! gradient and optimizer state next to the weight instead and store the plain
//! derivative `dL/dw`, which the update rule subtracts.
//!
//! # Putting weights on a device
//!
//! [`Param::to_cuda`] moves one parameter, and a model of two hundred
//! parameters wants one device context, not two hundred. The way to get that
//! here is [`CudaDevice`]: an opaque handle a caller builds once and passes to
//! [`Param::to_cuda_on`] or [`Linear::to_cuda_on`] for every weight in turn.
//!
//! A handle, rather than a builder taking `&mut [&mut Param]`, because a model
//! is a tree: its weights live in nested structs and a caller reaches them by
//! walking that tree, not by collecting every one of them into a slice first.
//! Borrowing them all at once to hand to a builder means either a
//! `params_mut()` on every module or a borrow that outlives the walk. A handle
//! is `&`-shared, so it rides along the walk and each module moves its own
//! weights. It also gives the budget somewhere to live: the handle keeps a
//! running total of what it has put on the device and refuses the parameter
//! that would cross the budget, which a per-parameter budget cannot do.
use crate::matrix::Matrix;
use crate::network::NetworkError;
use crate::optimizers::Optimizer;
use rand::{Rng, rngs::StdRng};
use serde::{Deserialize, Serialize};
/// A weight matrix plus its gradient and Adam moments.
///
/// Only the value is serialized: a snapshot restores the weights and restarts
/// the moment estimates from zero, which is what
/// [`Network::load_json`](crate::network::Network::load_json) already does for
/// dense networks.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(from = "Matrix", into = "Matrix")]
pub struct Param {
pub value: Matrix,
pub grad: Matrix,
moment1: Matrix,
moment2: Matrix,
/// Device mirror, present between
/// [`TransformerLm::to_cuda`](crate::transformer::TransformerLm::to_cuda)
/// and `to_cpu`. While it is present the host `value`, `grad` and moments
/// are stale: the device owns them.
#[cfg(feature = "cuda")]
pub(crate) device: Option<crate::gpu_transformer::DeviceParam>,
/// Set by [`Param::quantize`]. While it is present `value` keeps its shape
/// but holds no data, and the forward pass reads the bytes instead.
#[serde(skip)]
pub(crate) quantized: Option<crate::quantized::Quantized>,
/// Set by [`Param::freeze`]. A frozen parameter accumulates no gradient and
/// ignores the optimizer step, and its gradient and moment buffers are
/// released, which is where LoRA's memory saving comes from: three
/// weight-sized buffers per base parameter instead of four.
///
/// Not serialized. A snapshot records the weights; what is frozen follows
/// from [`TransformerConfig::lora`](crate::transformer::TransformerConfig),
/// which is restored with the rest of the configuration.
#[serde(skip)]
frozen: bool,
/// Set by
/// [`TransformerLm::quantization_aware`](crate::transformer::TransformerLm::quantization_aware).
/// The forward pass rounds the weight through the int8 grid while the
/// stored weight and its gradient stay full precision, so training sees the
/// rounding that [`Param::quantize`] will later make permanent.
///
/// Not serialized: it is a property of a run, not of the weights.
#[serde(skip)]
fake_quantize: bool,
}
/// A device, held open so many parameters can share one context.
///
/// Building a [`crate::gpu_transformer::GpuContext`] costs a CUDA stream, a
/// cuBLAS handle and a module lookup per kernel, so a model moves onto the
/// device through one of these rather than one context per weight. See the
/// module documentation for why this is a handle and not a builder.
///
/// Fails closed: without the `cuda` feature [`CudaDevice::new`] returns
/// [`NetworkError::CudaFeatureDisabled`] rather than leaving the weights on the
/// host and saying nothing.
#[derive(Debug)]
pub struct CudaDevice {
#[cfg(feature = "cuda")]
context: std::sync::Arc<crate::gpu_transformer::GpuContext>,
/// Zero means no budget, matching
/// [`TransformerLm::to_cuda`](crate::transformer::TransformerLm::to_cuda).
budget_mib: usize,
reserved_bytes: std::sync::atomic::AtomicUsize,
}
impl CudaDevice {
/// Opens `device` and holds it. `memory_budget_mib` of zero means no
/// budget; otherwise the handle refuses the parameter that would take its
/// running total past the budget.
///
/// The budget counts what the parameters themselves take. Activations,
/// workspaces and the driver's own overhead are not in it, so a budget set
/// to the whole card will still run out of memory.
pub fn new(device: usize, memory_budget_mib: usize) -> Result<Self, NetworkError> {
#[cfg(feature = "cuda")]
{
Ok(Self {
context: crate::gpu_transformer::GpuContext::new(device)?,
budget_mib: memory_budget_mib,
reserved_bytes: std::sync::atomic::AtomicUsize::new(0),
})
}
#[cfg(not(feature = "cuda"))]
{
let _ = (device, memory_budget_mib);
Err(NetworkError::CudaFeatureDisabled)
}
}
/// As [`CudaDevice::new`], but cuBLAS runs its GEMMs on the tensor cores in
/// TF32. See
/// [`TransformerBuilder::mixed_precision`](crate::transformer::TransformerBuilder::mixed_precision)
/// for what that costs in accuracy.
pub fn with_mixed_precision(
device: usize,
memory_budget_mib: usize,
) -> Result<Self, NetworkError> {
#[cfg(feature = "cuda")]
{
Ok(Self {
context: crate::gpu_transformer::GpuContext::with_precision(device, true)?,
budget_mib: memory_budget_mib,
reserved_bytes: std::sync::atomic::AtomicUsize::new(0),
})
}
#[cfg(not(feature = "cuda"))]
{
let _ = (device, memory_budget_mib);
Err(NetworkError::CudaFeatureDisabled)
}
}
/// The context the parameters moved through this handle share, for a
/// caller inside the crate that has its own kernels to launch on it.
#[cfg(feature = "cuda")]
pub(crate) fn context(&self) -> &std::sync::Arc<crate::gpu_transformer::GpuContext> {
&self.context
}
/// The budget this handle was opened with, in MiB. Zero means no budget.
pub fn budget_mib(&self) -> usize {
self.budget_mib
}
/// What the parameters moved through this handle so far take on the
/// device, in MiB.
pub fn reserved_mib(&self) -> usize {
self.reserved_bytes
.load(std::sync::atomic::Ordering::Relaxed)
.div_ceil(1024 * 1024)
}
/// Gives back the room a parameter took when it moves back to the host, so
/// a handle that moves a model on and off the device does not drift up to
/// its budget.
#[cfg(feature = "cuda")]
fn release(&self, bytes: usize) {
self.reserved_bytes
.fetch_sub(bytes, std::sync::atomic::Ordering::Relaxed);
}
/// Books `bytes` against the budget, or refuses before anything is
/// allocated.
#[cfg(feature = "cuda")]
fn reserve(&self, bytes: usize) -> Result<(), NetworkError> {
let total = bytes
+ self
.reserved_bytes
.fetch_add(bytes, std::sync::atomic::Ordering::Relaxed);
let estimated_mib = total.div_ceil(1024 * 1024);
if self.budget_mib > 0 && estimated_mib > self.budget_mib {
self.release(bytes);
return Err(NetworkError::CudaMemoryBudget {
estimated_mib,
budget_mib: self.budget_mib,
});
}
Ok(())
}
}
impl From<Matrix> for Param {
fn from(value: Matrix) -> Self {
Self::new(value)
}
}
impl From<Param> for Matrix {
fn from(param: Param) -> Self {
param.value
}
}
/// Compares weights only. Gradients and moments are transient training state,
/// and two models that predict identically are equal for every purpose a caller
/// has.
impl PartialEq for Param {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl Param {
pub fn new(value: Matrix) -> Self {
let (rows, cols) = (value.rows, value.cols);
Self {
value,
grad: Matrix::new(rows, cols),
moment1: Matrix::new(rows, cols),
moment2: Matrix::new(rows, cols),
#[cfg(feature = "cuda")]
device: None,
quantized: None,
frozen: false,
fake_quantize: false,
}
}
pub fn zeros(rows: usize, cols: usize) -> Self {
Self::new(Matrix::new(rows, cols))
}
/// Every entry set to `value`. RMSNorm scales start at one.
pub fn filled(rows: usize, cols: usize, value: f32) -> Self {
Self::new(Matrix::from_vec(rows, cols, vec![value; rows * cols]))
}
/// He-style uniform initialization, matching the dense network's scheme so
/// a transformer built with a given seed is as reproducible as a `Network`.
pub fn he_uniform(rows: usize, cols: usize, fan_in: usize, rng: &mut StdRng) -> Self {
let scale = (2.0 / fan_in as f32).sqrt();
let data = (0..rows * cols)
.map(|_| rng.gen_range(-scale..scale))
.collect();
Self::new(Matrix::from_vec(rows, cols, data))
}
pub fn len(&self) -> usize {
self.value.data.len()
}
pub fn is_empty(&self) -> bool {
self.value.data.is_empty()
}
/// Replaces the weights with one byte per value and drops the gradient and
/// both moments, which are dead weight in an inference process and four
/// fifths of the memory. Returns the bytes the parameter now occupies.
///
/// One-way: the shape survives but the `f32` values do not, so anything
/// that trains or serializes the model afterwards has to refuse.
pub(crate) fn quantize(&mut self) -> usize {
if self.quantized.is_some() {
return self
.quantized
.as_ref()
.map_or(0, crate::quantized::Quantized::bytes);
}
let quantized = crate::quantized::Quantized::from_matrix(&self.value);
self.store_quantized(quantized)
}
/// [`Param::quantize`] at 4 bits a weight, on `codebook`.
pub(crate) fn quantize_4bit(&mut self, codebook: crate::quantized::Codebook) -> usize {
if let Some(quantized) = &self.quantized {
return quantized.bytes();
}
let quantized = crate::quantized::Quantized::from_matrix_4bit(&self.value, codebook);
self.store_quantized(quantized)
}
fn store_quantized(&mut self, quantized: crate::quantized::Quantized) -> usize {
let bytes = quantized.bytes();
self.quantized = Some(quantized);
self.value.data = Vec::new();
self.grad = Matrix::new(0, 0);
self.moment1 = Matrix::new(0, 0);
self.moment2 = Matrix::new(0, 0);
bytes
}
/// Stops the parameter training and releases its gradient and Adam
/// moments, which is three quarters of what it costs to train.
///
/// The weights stay, and a forward pass is unaffected. Reversed by
/// [`Param::unfreeze`], which allocates the buffers again, empty.
pub fn freeze(&mut self) {
self.frozen = true;
self.grad = Matrix::new(0, 0);
self.moment1 = Matrix::new(0, 0);
self.moment2 = Matrix::new(0, 0);
}
/// Gives the parameter its gradient and moment buffers back, zeroed.
pub fn unfreeze(&mut self) {
if !self.frozen {
return;
}
self.frozen = false;
let (rows, cols) = (self.value.rows, self.value.cols);
self.grad = Matrix::new(rows, cols);
self.moment1 = Matrix::new(rows, cols);
self.moment2 = Matrix::new(rows, cols);
}
pub fn is_frozen(&self) -> bool {
self.frozen
}
/// Rounds this weight through the int8 grid in the forward pass, without
/// changing what is stored or what the optimizer updates.
pub fn set_fake_quantize(&mut self, enabled: bool) {
self.fake_quantize = enabled;
}
/// Whether the forward pass rounds this weight.
pub fn is_fake_quantized(&self) -> bool {
self.fake_quantize
}
pub fn zero_grad(&mut self) {
if self.frozen {
return;
}
#[cfg(feature = "cuda")]
if let Some(device) = &mut self.device {
device.zero_grad();
return;
}
self.grad.zeros();
}
/// What this parameter takes on a device: the weight, and unless it is
/// frozen its gradient and both Adam moments.
#[cfg(feature = "cuda")]
fn device_bytes(&self) -> usize {
if let Some(quantized) = &self.quantized {
return quantized.bytes();
}
let buffers = if self.frozen { 1 } else { 4 };
self.value.data.len() * 4 * buffers
}
/// Moves the parameter onto `device` and keeps it there until
/// [`Param::to_cpu`].
///
/// While it is resident the device owns the weights: the host `value`,
/// `grad` and moments are stale, and [`Linear::forward`] and
/// [`Linear::backward`] run their GEMMs on the device.
///
/// A model with more than one parameter should open a [`CudaDevice`] once
/// and use [`Param::to_cuda_on`] instead; this builds a context, and a
/// context per weight is the thing that makes a two-hundred-parameter model
/// slow to start.
///
/// Fails closed: built without the `cuda` feature this returns
/// [`NetworkError::CudaFeatureDisabled`] rather than staying on the host
/// and saying nothing.
pub fn to_cuda(
&mut self,
device: usize,
memory_budget_mib: usize,
) -> Result<(), crate::network::NetworkError> {
self.to_cuda_on(&CudaDevice::new(device, memory_budget_mib)?)
}
/// [`Param::to_cuda`] onto a device a caller already opened.
pub fn to_cuda_on(&mut self, device: &CudaDevice) -> Result<(), crate::network::NetworkError> {
// A frozen int8 weight - a LoRA base after `quantize_lora_base` -
// never trains, so the device can read it as int8 too.
if self.quantized.is_some() && !self.frozen {
return Err(crate::network::NetworkError::UnsupportedCuda(
"a quantized parameter on a device: the device path trains in FP32 and the \
int8 weights cannot be recovered"
.into(),
));
}
#[cfg(feature = "cuda")]
{
if self.device.is_some() {
return Ok(());
}
let bytes = self.device_bytes();
device.reserve(bytes)?;
if let Err(error) = self.move_to_cuda(&device.context) {
device.release(bytes);
return Err(error);
}
Ok(())
}
#[cfg(not(feature = "cuda"))]
{
let _ = device;
Err(crate::network::NetworkError::CudaFeatureDisabled)
}
}
/// Copies the weights and gradient back from the device and drops the
/// mirror. A parameter that was never on a device is left alone.
pub fn to_cpu(&mut self) -> Result<(), crate::network::NetworkError> {
#[cfg(feature = "cuda")]
{
self.move_to_cpu()
}
#[cfg(not(feature = "cuda"))]
{
Ok(())
}
}
/// Uploads the parameter and keeps it resident until [`Param::move_to_cpu`].
#[cfg(feature = "cuda")]
pub(crate) fn move_to_cuda(
&mut self,
context: &std::sync::Arc<crate::gpu_transformer::GpuContext>,
) -> Result<(), crate::network::NetworkError> {
if self.device.is_none() {
if let Some(quantized) = &self.quantized {
self.device = Some(crate::gpu_transformer::DeviceParam::new_quantized(
context.clone(),
quantized,
self.value.rows,
self.value.cols,
)?);
return Ok(());
}
let mut device = crate::gpu_transformer::DeviceParam::new(
context.clone(),
&self.value,
self.frozen,
)?;
// The optimizer carries on where the host left it.
device.upload_moments(&self.moment1, &self.moment2)?;
self.device = Some(device);
}
Ok(())
}
/// Copies the device value back and drops the mirror.
#[cfg(feature = "cuda")]
pub(crate) fn move_to_cpu(&mut self) -> Result<(), crate::network::NetworkError> {
if let Some(device) = self.device.take() {
device.download_value(&mut self.value)?;
device.download_grad(&mut self.grad)?;
device.download_moments(&mut self.moment1, &mut self.moment2)?;
}
Ok(())
}
/// Holds the device Adam moments at one byte each, or back in FP32. See
/// [`TransformerLm::set_8bit_optimizer_state`](crate::transformer::TransformerLm::set_8bit_optimizer_state).
#[cfg(feature = "cuda")]
pub(crate) fn set_8bit_moments(
&mut self,
enabled: bool,
) -> Result<(), crate::network::NetworkError> {
match &mut self.device {
Some(device) => device.set_8bit_moments(enabled),
None => Ok(()),
}
}
/// Refreshes the host value (and gradient) without giving up residency.
#[cfg(feature = "cuda")]
pub(crate) fn sync_from_device(&mut self) -> Result<(), crate::network::NetworkError> {
if let Some(device) = &self.device {
device.download_value(&mut self.value)?;
device.download_grad(&mut self.grad)?;
}
Ok(())
}
/// The Adam moments, refreshed from the device first when the parameter is
/// resident there.
///
/// A snapshot carries weights only, so a run that stops and resumes has to
/// carry the moments separately; see
/// [`TransformerLm::save_optimizer_state`](crate::transformer::TransformerLm::save_optimizer_state).
pub fn moments(&mut self) -> Result<(&Matrix, &Matrix), crate::network::NetworkError> {
#[cfg(feature = "cuda")]
if let Some(device) = &self.device {
device.download_moments(&mut self.moment1, &mut self.moment2)?;
}
Ok((&self.moment1, &self.moment2))
}
/// The Adam moments as they stand on the host, without consulting a
/// device.
///
/// [`Param::moments`] is the accessor to reach for. This one exists for a
/// caller that has already refreshed a whole model and now wants to borrow
/// every parameter at once, which the `&mut self` there rules out.
pub(crate) fn host_moments(&self) -> (&Matrix, &Matrix) {
(&self.moment1, &self.moment2)
}
/// Whether a device mirror is present, in which case the device owns the
/// weights and writing to `value` on the host would be discarded.
pub(crate) fn is_on_device(&self) -> bool {
#[cfg(feature = "cuda")]
return self.device.is_some();
#[cfg(not(feature = "cuda"))]
return false;
}
/// Restores moments saved by an earlier run, uploading them when the
/// parameter is already resident on a device.
pub fn set_moments(
&mut self,
first: Matrix,
second: Matrix,
) -> Result<(), crate::network::NetworkError> {
if self.frozen {
return Ok(());
}
self.moment1 = first;
self.moment2 = second;
#[cfg(feature = "cuda")]
if let Some(device) = &mut self.device {
device.upload_moments(&self.moment1, &self.moment2)?;
}
Ok(())
}
/// Replaces the weights, uploading them when the parameter is resident on
/// a device.
///
/// The shape has to match: a weight is wired into a forward pass by its
/// dimensions, and a caller that meant to reshape wants a new parameter,
/// not this. Used by [`Ema::swap_in`](crate::ema::Ema::swap_in) to stand an
/// averaged weight in the live one's place.
pub fn set_value(&mut self, value: Matrix) -> Result<(), crate::network::NetworkError> {
if self.quantized.is_some() {
return Err(crate::network::NetworkError::InvalidConfig(
"a quantized parameter holds bytes, not weights, and cannot be assigned".into(),
));
}
if value.rows != self.value.rows || value.cols != self.value.cols {
return Err(crate::network::NetworkError::InvalidConfig(format!(
"this parameter is {}x{} but the new value is {}x{}",
self.value.rows, self.value.cols, value.rows, value.cols
)));
}
self.value = value;
#[cfg(feature = "cuda")]
if let Some(device) = &mut self.device {
device.upload_value(&self.value)?;
}
Ok(())
}
/// The sum of the squared gradient entries, for a caller building a global
/// gradient norm across every parameter.
///
/// Accumulated in `f64`: a 100M-parameter model sums that many small
/// squares into one number, and an `f32` accumulator loses the tail of
/// them.
pub fn grad_sum_squares(&self) -> Result<f64, crate::network::NetworkError> {
#[cfg(feature = "cuda")]
if let Some(device) = &self.device {
let norm = f64::from(device.grad_norm()?);
return Ok(norm * norm);
}
Ok(self
.grad
.data
.iter()
.map(|&g| f64::from(g) * f64::from(g))
.sum())
}
/// Applies one optimizer update and clears the gradient.
///
/// `step` is the global Adam step count, already incremented: the first
/// update is step 1, not 0 (while [`Schedule::rate`] counts from 0). Step 0
/// panics, because Adam's bias correction `1 - beta^0` would divide by zero
/// and turn every parameter NaN. SGD does not read it. `scale` divides the
/// accumulated gradient, which lets a caller accumulate over a batch and
/// average once here.
///
/// Weight decay applies to parameters with more than one row only. A
/// one-row parameter is a bias or a norm gain, and shrinking those
/// towards zero only fights the model (the nanoGPT rule).
///
/// [`Schedule::rate`]: crate::optimizers::Schedule::rate
pub fn step(&mut self, optimizer: &Optimizer, step: usize, scale: f32) {
assert!(step > 0, "optimizer steps count from 1");
if self.frozen {
return;
}
let undecayed;
let optimizer = match self.value.rows == 1 {
true => {
undecayed = optimizer.without_weight_decay();
&undecayed
}
false => optimizer,
};
#[cfg(feature = "cuda")]
if let Some(device) = &mut self.device {
device.step(optimizer, step, scale);
return;
}
match *optimizer {
Optimizer::Sgd { learning_rate } => {
for (value, grad) in self.value.data.iter_mut().zip(&self.grad.data) {
*value -= learning_rate * grad * scale;
}
}
Optimizer::Adam {
learning_rate,
beta1,
beta2,
epsilon,
weight_decay,
} => {
let bias_correction1 = 1.0 - beta1.powi(step as i32);
let bias_correction2 = 1.0 - beta2.powi(step as i32);
for (((value, grad), m), v) in self
.value
.data
.iter_mut()
.zip(&self.grad.data)
.zip(&mut self.moment1.data)
.zip(&mut self.moment2.data)
{
let grad = grad * scale;
*m = beta1 * *m + (1.0 - beta1) * grad;
*v = beta2 * *v + (1.0 - beta2) * grad * grad;
let m_hat = *m / bias_correction1;
let v_hat = *v / bias_correction2;
*value -= learning_rate * weight_decay * *value;
*value -= learning_rate * m_hat / (v_hat.sqrt() + epsilon);
}
}
Optimizer::Lion {
learning_rate,
beta1,
beta2,
weight_decay,
} => {
for ((value, grad), m) in self
.value
.data
.iter_mut()
.zip(&self.grad.data)
.zip(&mut self.moment1.data)
{
let grad = grad * scale;
// The step is taken from the moment as it was, and the
// moment is then updated with the other beta. The two
// betas and that ordering are what separate Lion from
// plain signed momentum.
let update = beta1 * *m + (1.0 - beta1) * grad;
*m = beta2 * *m + (1.0 - beta2) * grad;
*value -=
learning_rate * (crate::optimizers::sign(update) + weight_decay * *value);
}
}
}
self.zero_grad();
}
}
/// A bias-free `y = x * W^T` projection.
///
/// Every projection in a Qwen-style decoder is bias-free, so the bias that
/// [`DenseLayer`](crate::network::DenseLayer) carries would only ever be zero
/// here. Rows of `x` are tokens; `weight` is `[out_features, in_features]`.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Linear {
pub weight: Param,
/// Present between
/// [`TransformerLm::add_lora`](crate::transformer::TransformerLm::add_lora)
/// and `merge_lora`.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub lora: Option<Lora>,
}
/// A low-rank adapter over a [`Linear`]: `W + (alpha / rank) * up * down`.
///
/// `down` is `[rank, in_features]` and `up` is `[out_features, rank]`, so the
/// pair costs `rank * (in + out)` weights where the projection it adapts costs
/// `in * out`. `up` starts at zero, so attaching an adapter does not change
/// what the model predicts; the first optimizer step is what moves it.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Lora {
pub down: Param,
pub up: Param,
/// `alpha / rank`. Folded into the `[tokens, rank]` intermediate, which is
/// the smallest matrix in the adapter, so neither pass scales a full-width
/// one.
pub scale: f32,
}
impl Lora {
/// `down` is initialized like any other projection and `up` is zero, which
/// is the usual asymmetry: two random factors would start the adapter at a
/// random perturbation of a trained model.
pub fn new(
in_features: usize,
out_features: usize,
rank: usize,
alpha: f32,
rng: &mut StdRng,
) -> Self {
Self {
down: Param::he_uniform(rank, in_features, in_features, rng),
up: Param::zeros(out_features, rank),
scale: alpha / rank as f32,
}
}
pub fn rank(&self) -> usize {
self.down.value.rows
}
/// `scale * input * down^T`, the `[tokens, rank]` bottleneck both passes
/// need. The backward pass recomputes it rather than caching it: it is one
/// small GEMM, and caching it would give every projection in the model a
/// per-call buffer to thread through.
fn hidden(&self, input: &Matrix) -> Matrix {
let mut hidden = Matrix::new(input.rows, self.rank());
input.dot_rhs_transposed(&self.down.value, &mut hidden);
for value in &mut hidden.data {
*value *= self.scale;
}
hidden
}
pub(crate) fn add_forward(&self, input: &Matrix, output: &mut Matrix) {
let hidden = self.hidden(input);
let mut delta = Matrix::new(input.rows, self.up.value.rows);
hidden.dot_rhs_transposed(&self.up.value, &mut delta);
for (slot, value) in output.data.iter_mut().zip(&delta.data) {
*slot += value;
}
}
/// Accumulates both adapter gradients and adds the adapter's share of
/// `dL/dinput` to what the base projection already wrote.
pub(crate) fn backward(
&mut self,
input: &Matrix,
grad_output: &Matrix,
grad_input: &mut Matrix,
) {
let hidden = self.hidden(input);
grad_output.dot_self_transposed_accumulate(&hidden, &mut self.up.grad);
let mut grad_hidden = Matrix::new(grad_output.rows, self.rank());
grad_output.dot(&self.up.value, &mut grad_hidden);
for value in &mut grad_hidden.data {
*value *= self.scale;
}
grad_hidden.dot_self_transposed_accumulate(input, &mut self.down.grad);
grad_hidden.dot_accumulate(&self.down.value, grad_input);
}
pub fn params_mut(&mut self) -> Vec<&mut Param> {
vec![&mut self.down, &mut self.up]
}
}
impl Linear {
pub fn new(in_features: usize, out_features: usize, rng: &mut StdRng) -> Self {
Self {
weight: Param::he_uniform(out_features, in_features, in_features, rng),
lora: None,
}
}
/// Attaches an adapter and freezes the base weight, so the projection keeps
/// predicting what it did and only the adapter trains.
pub fn attach_lora(&mut self, rank: usize, alpha: f32, rng: &mut StdRng) {
self.weight.freeze();
self.lora = Some(Lora::new(
self.in_features(),
self.out_features(),
rank,
alpha,
rng,
));
}
/// Folds the adapter into the base weight and unfreezes it, leaving an
/// ordinary projection that computes the same thing.
pub fn merge_lora(&mut self) {
let Some(lora) = self.lora.take() else {
return;
};
let mut scaled = lora.down.value.clone();
for value in &mut scaled.data {
*value *= lora.scale;
}
lora.up
.value
.dot_accumulate(&scaled, &mut self.weight.value);
self.weight.unfreeze();
}
pub fn in_features(&self) -> usize {
self.weight.value.cols
}
pub fn out_features(&self) -> usize {
self.weight.value.rows
}
/// `[tokens, in_features] -> [tokens, out_features]`.
pub fn forward(&self, input: &Matrix) -> Matrix {
debug_assert_eq!(input.cols, self.in_features());
let mut output = self.base_forward(input);
if let Some(lora) = &self.lora {
lora.add_forward(input, &mut output);
}
output
}
/// The projection without its adapter, whichever of the three weight
/// representations is live.
fn base_forward(&self, input: &Matrix) -> Matrix {
// A quantized device weight falls through to the host's copy of the same
// bytes.
#[cfg(feature = "cuda")]
if let Some(device) = self.weight.device.as_ref().filter(|d| !d.is_quantized()) {
return device.matmul_rhs_transposed(input);
}
if let Some(quantized) = &self.weight.quantized {
return quantized.matmul_rhs_transposed(input);
}
if self.weight.fake_quantize {
// The int8 matmul inference will run, rather than a rounded copy
// multiplied in full precision: training then sees exactly the
// arithmetic `quantize` leaves behind.
//
// NOTE: the weight is rounded again on every forward pass, one
// pass over it per batch next to a GEMM over the same weight.
// Caching the rounding would have to be invalidated by every
// optimizer step.
return crate::quantized::Quantized::from_matrix(&self.weight.value)
.matmul_rhs_transposed(input);
}
let mut output = Matrix::new(input.rows, self.out_features());
input.dot_rhs_transposed(&self.weight.value, &mut output);
output
}
/// Accumulates the weight gradient and returns `dL/dinput`.
pub fn backward(&mut self, input: &Matrix, grad_output: &Matrix) -> Matrix {
debug_assert_eq!(grad_output.cols, self.out_features());
let mut grad_input = self.base_backward(input, grad_output);
if let Some(lora) = &mut self.lora {
lora.backward(input, grad_output, &mut grad_input);
}
grad_input
}
/// The base projection's share of the backward pass.
///
/// NOTE: a device-resident adapter accumulates into its host gradient
/// here rather than its device mirror. Nothing reaches this: device
/// training runs in `gpu_model`, which never calls a `Linear` method.
fn base_backward(&mut self, input: &Matrix, grad_output: &Matrix) -> Matrix {
#[cfg(feature = "cuda")]
if let Some(device) = self.weight.device.as_mut().filter(|d| !d.is_quantized()) {
device.accumulate_grad(grad_output, input);
return device.matmul(grad_output);
}
if !self.weight.is_frozen() {
grad_output.dot_self_transposed_accumulate(input, &mut self.weight.grad);
}
let mut grad_input = Matrix::new(grad_output.rows, self.in_features());
// NOTE: an int8 base is widened on every backward call, one pass
// over the weight next to a GEMM over it. The host path is the parity
// reference; device training reads the int8 bytes directly.
match &self.weight.quantized {
Some(quantized) => grad_output.dot(&quantized.dequantize(), &mut grad_input),
None => grad_output.dot(&self.weight.value, &mut grad_input),
}
grad_input
}
/// Moves the whole projection onto `device`: the base weight and, when one
/// is attached, both LoRA factors. There is no bias to move; every
/// projection here is bias-free.
///
/// After this [`Linear::forward`] and [`Linear::backward`] run their GEMMs
/// on the device. As [`Param::to_cuda`], a model of more than one
/// projection should open a [`CudaDevice`] once and use
/// [`Linear::to_cuda_on`].
pub fn to_cuda(
&mut self,
device: usize,
memory_budget_mib: usize,
) -> Result<(), crate::network::NetworkError> {
self.to_cuda_on(&CudaDevice::new(device, memory_budget_mib)?)
}
/// [`Linear::to_cuda`] onto a device a caller already opened.
pub fn to_cuda_on(&mut self, device: &CudaDevice) -> Result<(), crate::network::NetworkError> {
for param in self.params_mut() {
param.to_cuda_on(device)?;
}
Ok(())
}
/// Brings the projection and its adapter back to the host.
pub fn to_cpu(&mut self) -> Result<(), crate::network::NetworkError> {
for param in self.params_mut() {
param.to_cpu()?;
}
Ok(())
}
pub fn params_mut(&mut self) -> Vec<&mut Param> {
let mut params = vec![&mut self.weight];
if let Some(lora) = &mut self.lora {
params.extend(lora.params_mut());
}
params
}
}
/// Inverted dropout: while training each element is zeroed with probability
/// `rate` and the rest are scaled by `1 / (1 - rate)`, so inference is the
/// identity.
#[derive(Clone, Copy, Debug)]
pub struct Dropout {
rate: f32,
}
/// Which elements [`Dropout::forward`] kept: each one's factor, zero or
/// `1 / (1 - rate)`. Empty when nothing was dropped.
#[derive(Clone, Debug, Default)]
pub struct DropoutMask(Vec<f32>);
impl Dropout {
pub fn new(rate: f32) -> Result<Self, NetworkError> {
if !(0.0..1.0).contains(&rate) {
return Err(NetworkError::InvalidConfig(format!(
"a dropout rate is in [0, 1), not {rate}"
)));
}
Ok(Self { rate })
}
pub fn rate(&self) -> f32 {
self.rate
}
pub fn forward(
&self,
input: &Matrix,
rng: &mut StdRng,
training: bool,
) -> (Matrix, DropoutMask) {
if !training || self.rate == 0.0 {
return (input.clone(), DropoutMask::default());
}
let keep = 1.0 / (1.0 - self.rate);
let mask: Vec<f32> = (0..input.data.len())
.map(|_| match rng.r#gen::<f32>() < self.rate {
true => 0.0,
false => keep,
})
.collect();
let data = input.data.iter().zip(&mask).map(|(x, m)| x * m).collect();
(
Matrix::from_vec(input.rows, input.cols, data),
DropoutMask(mask),
)
}
/// `dL/d(input)`: the upstream gradient through the same mask.
pub fn backward(&self, mask: &DropoutMask, grad: &Matrix) -> Matrix {
if mask.0.is_empty() {
return grad.clone();
}
let data = grad.data.iter().zip(&mask.0).map(|(g, m)| g * m).collect();
Matrix::from_vec(grad.rows, grad.cols, data)
}
}
#[cfg(test)]
mod tests {
use super::*;
use rand::SeedableRng;
/// Same contract as the other CUDA tests: no device means the test reports
/// success without running, a broken device is a failure.
#[cfg(feature = "cuda")]
fn cuda_or_skip(budget_mib: usize) -> Option<CudaDevice> {
match crate::cuda_training::cuda_doctor(0, budget_mib) {
Ok(_) => Some(CudaDevice::new(0, budget_mib).expect("the CUDA doctor passed")),
Err(NetworkError::Cuda(message))
if message.contains("NO_DEVICE") || message.contains("no CUDA-capable device") =>
{
crate::cuda_training::skip_without_cuda();
None
}
Err(error) => panic!("CUDA is present but the CUDA doctor failed: {error}"),
}
}
#[test]
fn dropout_keeps_the_mean_and_routes_the_gradient_through_its_mask() {
let mut rng = StdRng::seed_from_u64(0);
let dropout = Dropout::new(0.15).unwrap();
let ones = Matrix::from_vec(100, 1000, vec![1.0; 100_000]);
let (out, mask) = dropout.forward(&ones, &mut rng, true);
let mean = out.data.iter().sum::<f32>() / 1e5;
assert!((mean - 1.0).abs() < 1e-2, "mean {mean}");
let dropped = out.data.iter().filter(|v| **v == 0.0).count() as f32 / 1e5;
assert!((dropped - 0.15).abs() < 5e-3, "dropped {dropped}");
// The output is linear in the input with the mask as its slope.
assert_eq!(dropout.backward(&mask, &ones).data, out.data);
let (eval, mask) = dropout.forward(&ones, &mut rng, false);
assert_eq!(eval.data, ones.data);
assert_eq!(dropout.backward(&mask, &ones).data, ones.data);
assert!(Dropout::new(1.0).is_err() && Dropout::new(-0.1).is_err());
}
#[cfg(feature = "cuda")]
#[test]
fn a_projection_on_the_device_matches_the_host_forward_and_backward() {
let Some(device) = cuda_or_skip(1024) else {
return;
};
let mut rng = StdRng::seed_from_u64(7);
let host = Linear::new(24, 16, &mut rng);
let mut resident = host.clone();
let input = Matrix::from_vec(
5,
24,
(0..5 * 24).map(|i| (i % 13) as f32 / 7.0 - 0.8).collect(),
);
let grad_output = Matrix::from_vec(
5,
16,
(0..5 * 16).map(|i| (i % 11) as f32 / 5.0 - 1.1).collect(),
);
resident
.to_cuda_on(&device)
.expect("the move to the device");
assert!(resident.weight.is_on_device());
assert_eq!(device.reserved_mib(), 1, "24 x 16 floats, four buffers");
let expected = host.forward(&input);
let actual = resident.forward(&input);
assert_eq!((actual.rows, actual.cols), (expected.rows, expected.cols));
for (index, (a, b)) in actual.data.iter().zip(&expected.data).enumerate() {
assert!(
(a - b).abs() < 1e-4,
"forward[{index}]: {a} on the device vs {b} on the host"
);
}
let mut host = host;
let expected = host.backward(&input, &grad_output);
let actual = resident.backward(&input, &grad_output);
for (index, (a, b)) in actual.data.iter().zip(&expected.data).enumerate() {
assert!(
(a - b).abs() < 1e-4,
"dL/dinput[{index}]: {a} on the device vs {b} on the host"
);
}
resident.to_cpu().expect("the move back to the host");
assert!(!resident.weight.is_on_device());
for (index, (a, b)) in resident
.weight
.grad
.data
.iter()
.zip(&host.weight.grad.data)
.enumerate()
{
assert!(
(a - b).abs() < 1e-3,
"dL/dweight[{index}]: {a} on the device vs {b} on the host"
);
}
}
#[cfg(feature = "cuda")]
#[test]
fn a_budget_smaller_than_the_weights_refuses_the_move() {
let Some(device) = cuda_or_skip(1) else {
return;
};
// One MiB holds 262,144 floats; four buffers over 512 x 512 is four
// times that.
let mut rng = StdRng::seed_from_u64(3);
let mut linear = Linear::new(512, 512, &mut rng);
assert!(matches!(
linear.to_cuda_on(&device),
Err(NetworkError::CudaMemoryBudget { budget_mib: 1, .. })
));
assert!(
!linear.weight.is_on_device(),
"a refused move leaves the host copy live"
);
assert_eq!(device.reserved_mib(), 0, "a refused move books nothing");
}
#[cfg(not(feature = "cuda"))]
#[test]
fn moving_a_parameter_to_a_device_without_the_feature_is_refused() {
let mut param = Param::zeros(4, 4);
assert!(matches!(
param.to_cuda(0, 0),
Err(NetworkError::CudaFeatureDisabled)
));
assert!(matches!(
CudaDevice::new(0, 0),
Err(NetworkError::CudaFeatureDisabled)
));
let mut rng = StdRng::seed_from_u64(1);
let mut linear = Linear::new(4, 4, &mut rng);
assert!(matches!(
linear.to_cuda(0, 0),
Err(NetworkError::CudaFeatureDisabled)
));
// The host path is untouched by a refused move.
assert!(linear.to_cpu().is_ok());
}
#[test]
#[should_panic(expected = "optimizer steps count from 1")]
fn step_zero_panics_instead_of_poisoning_the_parameters() {
let mut param = Param::new(Matrix::from_vec(1, 1, vec![1.0]));
param.grad.data[0] = 0.5;
param.step(&Optimizer::adam(1e-2), 0, 1.0);
}
/// One step with a zero gradient: only the decay moves anything.
fn decay_one_step(
optimizer: &Optimizer,
#[cfg(feature = "cuda")] device: Option<&CudaDevice>,
) -> (Vec<f32>, Vec<f32>) {
let mut matrix = Param::new(Matrix::from_vec(2, 2, vec![1.0, -2.0, 3.0, -4.0]));
let mut gain = Param::new(Matrix::from_vec(1, 3, vec![1.0, 2.0, 3.0]));
#[cfg(feature = "cuda")]
if let Some(device) = device {
matrix.to_cuda_on(device).unwrap();
gain.to_cuda_on(device).unwrap();
}
matrix.step(optimizer, 1, 1.0);
gain.step(optimizer, 1, 1.0);
matrix.to_cpu().unwrap();
gain.to_cpu().unwrap();
(matrix.value.data, gain.value.data)
}
#[test]
fn weight_decay_shrinks_matrices_and_leaves_one_row_parameters_alone() {
for optimizer in [
Optimizer::adam_with_weight_decay(0.1, 0.5),
Optimizer::lion_with_weight_decay(0.1, 0.5),
] {
let (matrix, gain) = decay_one_step(
&optimizer,
#[cfg(feature = "cuda")]
None,
);
// Shrunk by lr * wd = 0.05.
for (after, before) in matrix.iter().zip([1.0f32, -2.0, 3.0, -4.0]) {
assert!(
(after - 0.95 * before).abs() < 1e-6,
"{optimizer:?}: {after}"
);
}
assert_eq!(gain, [1.0, 2.0, 3.0], "{optimizer:?}");
}
}
#[cfg(feature = "cuda")]
#[test]
fn weight_decay_on_the_device_matches_the_host_or_skips_without_device() {
let Some(device) = cuda_or_skip(256) else {
return;
};
for optimizer in [
Optimizer::adam_with_weight_decay(0.1, 0.5),
Optimizer::lion_with_weight_decay(0.1, 0.5),
] {
let host = decay_one_step(&optimizer, None);
let cuda = decay_one_step(&optimizer, Some(&device));
for (a, b) in host
.0
.iter()
.chain(&host.1)
.zip(cuda.0.iter().chain(&cuda.1))
{
assert!((a - b).abs() < 1e-6, "{optimizer:?}: host {a}, device {b}");
}
}
}
#[test]
fn a_lion_step_moves_by_the_learning_rate_and_updates_the_moment_after() {
let mut param = Param::new(Matrix::from_vec(1, 1, vec![1.0]));
let optimizer = Optimizer::lion(0.1);
// update = 0.9 * 0 + 0.1 * 0.5, which is positive, so the step is the
// whole learning rate however small the gradient was.
param.grad.data[0] = 0.5;
param.step(&optimizer, 1, 1.0);
assert!((param.value.data[0] - 0.9).abs() < 1e-6);
assert!(
(param.moment1.data[0] - 0.005).abs() < 1e-7,
"the moment took the second beta, not the first: {}",
param.moment1.data[0]
);
assert_eq!(param.moment2.data[0], 0.0, "Lion keeps one moment");
// 0.9 * 0.005 + 0.1 * -0.5 is negative, so the sign flips with the
// gradient rather than being dragged by the old moment.
param.grad.data[0] = -0.5;
param.step(&optimizer, 2, 1.0);
assert!((param.value.data[0] - 1.0).abs() < 1e-6);
assert!((param.moment1.data[0] - -0.00005).abs() < 1e-7);
}
#[test]
fn linear_backward_matches_finite_differences() {
let mut rng = StdRng::seed_from_u64(7);
let mut linear = Linear::new(3, 2, &mut rng);
let input = Matrix::from_vec(2, 3, vec![0.5, -1.0, 2.0, 0.25, 0.75, -0.5]);
// Scalar objective: the sum of the outputs, whose gradient is all ones.
let grad_output = Matrix::from_vec(2, 2, vec![1.0; 4]);
let grad_input = linear.backward(&input, &grad_output);
let epsilon = 1e-3;
for index in 0..input.data.len() {
let mut bumped = input.clone();
bumped.data[index] += epsilon;
let high: f32 = linear.forward(&bumped).data.iter().sum();
bumped.data[index] -= 2.0 * epsilon;
let low: f32 = linear.forward(&bumped).data.iter().sum();
let numeric = (high - low) / (2.0 * epsilon);
assert!((grad_input.data[index] - numeric).abs() < 1e-2);
}
}
/// The adapter's three gradients against central differences: the base
/// weight is frozen, so `down`, `up` and `dL/dinput` are the whole backward
/// pass here.
#[test]
fn lora_backward_matches_finite_differences() {
let mut rng = StdRng::seed_from_u64(11);
let mut linear = Linear::new(4, 3, &mut rng);
linear.attach_lora(2, 4.0, &mut rng);
// `up` starts at zero, which makes every gradient through it zero and
// the check vacuous, so move it somewhere generic first.
let up = &mut linear.lora.as_mut().unwrap().up.value;
for (index, value) in up.data.iter_mut().enumerate() {
*value = 0.1 * (index as f32) - 0.25;
}
let input = Matrix::from_vec(2, 4, vec![0.5, -1.0, 2.0, 0.25, 0.75, -0.5, 1.5, -0.125]);
let grad_output = Matrix::from_vec(2, 3, vec![1.0; 6]);
let grad_input = linear.backward(&input, &grad_output);
let epsilon = 1e-3;
let objective =
|linear: &Linear, input: &Matrix| -> f32 { linear.forward(input).data.iter().sum() };
for index in 0..input.data.len() {
let mut bumped = input.clone();
bumped.data[index] += epsilon;
let high = objective(&linear, &bumped);
bumped.data[index] -= 2.0 * epsilon;
let low = objective(&linear, &bumped);
let numeric = (high - low) / (2.0 * epsilon);
assert!(
(grad_input.data[index] - numeric).abs() < 1e-2,
"grad_input[{index}]: {} vs {numeric}",
grad_input.data[index]
);
}
for weight in [0, 1] {
let expected: Vec<f32> = {
let lora = linear.lora.as_ref().unwrap();
if weight == 0 {
&lora.down.grad
} else {
&lora.up.grad
}
}
.data
.clone();
for (index, &analytic) in expected.iter().enumerate() {
let mut probe = linear.clone();
{
let lora = probe.lora.as_mut().unwrap();
let slot = if weight == 0 {
&mut lora.down.value
} else {
&mut lora.up.value
};
slot.data[index] += epsilon;
}
let high = objective(&probe, &input);
{
let lora = probe.lora.as_mut().unwrap();
let slot = if weight == 0 {
&mut lora.down.value
} else {
&mut lora.up.value
};
slot.data[index] -= 2.0 * epsilon;
}
let low = objective(&probe, &input);
let numeric = (high - low) / (2.0 * epsilon);
assert!(
(analytic - numeric).abs() < 1e-2,
"weight {weight} gradient[{index}]: {analytic} vs {numeric}"
);
}
}
}
/// Attaching an adapter must not move the model, and merging one must not
/// move it back.
#[test]
fn attaching_and_merging_an_adapter_preserve_the_output() {
let mut rng = StdRng::seed_from_u64(3);
let mut linear = Linear::new(4, 3, &mut rng);
let input = Matrix::from_vec(2, 4, vec![0.5, -1.0, 2.0, 0.25, 0.75, -0.5, 1.5, -0.125]);
let before = linear.forward(&input);
linear.attach_lora(2, 4.0, &mut rng);
assert!(linear.weight.is_frozen());
assert_eq!(linear.forward(&input), before);
// Train the adapter by hand: one SGD step on a non-zero gradient.
let grad_output = Matrix::from_vec(2, 3, vec![0.3; 6]);
let up = &mut linear.lora.as_mut().unwrap().up.value;
up.data.iter_mut().for_each(|value| *value = 0.05);
linear.backward(&input, &grad_output);
for param in linear.params_mut() {
param.step(&Optimizer::sgd(0.1), 1, 1.0);
}
let adapted = linear.forward(&input);
assert_ne!(adapted, before);
linear.merge_lora();
assert!(!linear.weight.is_frozen());
assert!(linear.lora.is_none());
for (merged, expected) in linear.forward(&input).data.iter().zip(&adapted.data) {
assert!((merged - expected).abs() < 1e-5, "{merged} vs {expected}");
}
}
/// A frozen weight keeps its value, accumulates nothing and ignores the
/// optimizer.
#[test]
fn a_frozen_weight_does_not_move() {
let mut rng = StdRng::seed_from_u64(5);
let mut linear = Linear::new(3, 2, &mut rng);
let before = linear.weight.value.clone();
linear.weight.freeze();
assert_eq!(linear.weight.grad.data.len(), 0);
let input = Matrix::from_vec(1, 3, vec![1.0, 2.0, 3.0]);
linear.backward(&input, &Matrix::from_vec(1, 2, vec![1.0, 1.0]));
linear.weight.step(&Optimizer::sgd(1.0), 1, 1.0);
assert_eq!(linear.weight.value, before);
}
#[test]
fn param_round_trips_as_a_bare_matrix() {
let param = Param::new(Matrix::from_vec(1, 3, vec![1.0, 2.0, 3.0]));
let json = serde_json::to_string(¶m).unwrap();
let restored: Param = serde_json::from_str(&json).unwrap();
assert_eq!(restored.value, param.value);
assert!(restored.grad.data.iter().all(|&g| g == 0.0));
}
}