kizzasi-model 0.2.1

Model architectures for Kizzasi AGSP - Mamba, RWKV, S4, Transformer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
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
//! Backward pass and gradient computation infrastructure for kizzasi-model.
//!
//! Implements pure-Rust reverse-mode automatic differentiation (a "gradient
//! tape") together with SSM-specific backprop helpers and a gradient
//! accumulator suitable for multi-step parameter updates.
//!
//! # Architecture
//!
//! - **[`GradientTape`]**: records operations during a forward pass and replays
//!   them in reverse to propagate gradients (reverse-mode AD).
//! - **[`SsmBackward`]**: SSM-specific reverse scan through a selective SSM
//!   sequence (Mamba-style), computing gradients wrt all SSM parameters.
//! - **[`GradAccumulator`]**: accumulates and manages parameter gradients across
//!   multiple micro-batches, with global-norm gradient clipping.
//! - **Layer backward functions**: [`linear_backward`], [`silu_backward`],
//!   [`softmax_backward`], [`layer_norm_backward`] — standalone, composable.
//!
//! All operations use `scirs2_core::ndarray` arrays and propagate errors via
//! [`ModelResult`]; no `unwrap()` is used anywhere in this module.

use crate::error::{ModelError, ModelResult};
use scirs2_core::ndarray::{Array1, Array2};
use std::collections::HashMap;

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// Elementwise sigmoid.
#[inline]
fn sigmoid(x: f32) -> f32 {
    1.0 / (1.0 + (-x).exp())
}

/// Check a 1-D array for NaN / Inf values.
fn check_finite_1d(arr: &Array1<f32>, ctx: &str) -> ModelResult<()> {
    for &v in arr.iter() {
        if !v.is_finite() {
            return Err(ModelError::numerical_instability(
                ctx,
                format!("non-finite value {v} detected"),
            ));
        }
    }
    Ok(())
}

/// Check a 2-D array for NaN / Inf values.
fn check_finite_2d(arr: &Array2<f32>, ctx: &str) -> ModelResult<()> {
    for &v in arr.iter() {
        if !v.is_finite() {
            return Err(ModelError::numerical_instability(
                ctx,
                format!("non-finite value {v} detected"),
            ));
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Tensor
// ---------------------------------------------------------------------------

/// A 1-D tensor with optional gradient storage.
#[derive(Debug, Clone)]
pub struct Tensor {
    /// Forward-pass data.
    pub data: Array1<f32>,
    /// Accumulated gradient (filled by [`GradientTape::backward`]).
    pub grad: Option<Array1<f32>>,
    /// Whether gradient computation is required for this tensor.
    pub requires_grad: bool,
}

impl Tensor {
    /// Create a tensor that participates in gradient computation.
    pub fn new(data: Array1<f32>) -> Self {
        Self {
            data,
            grad: None,
            requires_grad: true,
        }
    }

    /// Create a tensor that does NOT participate in gradient computation.
    pub fn no_grad(data: Array1<f32>) -> Self {
        Self {
            data,
            grad: None,
            requires_grad: false,
        }
    }
}

// ---------------------------------------------------------------------------
// TapeOp (internal enum)
// ---------------------------------------------------------------------------

/// A single recorded operation on the gradient tape.
enum TapeOp {
    /// Elementwise addition: `out = a + b`.
    Add {
        out_idx: usize,
        a_idx: usize,
        b_idx: usize,
    },
    /// Elementwise multiplication: `out = a * b`.
    Mul {
        out_idx: usize,
        a_idx: usize,
        b_idx: usize,
        /// Saved forward value of `a` (needed for `db`).
        a_data: Array1<f32>,
        /// Saved forward value of `b` (needed for `da`).
        b_data: Array1<f32>,
    },
    /// Matrix-matrix multiplication: `out = A @ B` (flattened to 1-D).
    MatMul {
        out_idx: usize,
        a_idx: usize,
        b_idx: usize,
        /// Saved forward value of `A`.
        a: Array2<f32>,
        /// Saved forward value of `B`.
        b: Array2<f32>,
    },
    /// SiLU activation: `out = x * sigmoid(x)`.
    SiLU {
        out_idx: usize,
        in_idx: usize,
        /// Saved pre-activation input.
        input: Array1<f32>,
    },
    /// Layer-normalisation: `out = scale * (x - mean) / sqrt(var + eps)`.
    LayerNorm {
        out_idx: usize,
        in_idx: usize,
        mean: f32,
        var: f32,
        scale: Array1<f32>,
    },
    /// Simplified SSM recurrent scan.
    SsmScan {
        out_idx: usize,
        in_idx: usize,
        /// Saved discretised-A values used in the scan.
        a_vals: Array1<f32>,
        /// Saved discretised-B values used in the scan.
        b_vals: Array1<f32>,
    },
}

// ---------------------------------------------------------------------------
// GradientTape
// ---------------------------------------------------------------------------

/// Gradient tape for reverse-mode automatic differentiation.
///
/// # Usage
///
/// 1. Create a tape.
/// 2. Record operations during the forward pass using `record_*` methods.
///    Each method returns an **output tensor index**.
/// 3. After computing the scalar loss, call [`backward`][Self::backward] with
///    the gradient of the loss w.r.t. the output to accumulate gradients into
///    all upstream tensors.
pub struct GradientTape {
    ops: Vec<TapeOp>,
    /// Number of tensor slots allocated so far.
    num_tensors: usize,
}

impl GradientTape {
    /// Create an empty tape.
    pub fn new() -> Self {
        Self {
            ops: Vec::new(),
            num_tensors: 0,
        }
    }

    /// Allocate a new tensor slot and return its index.
    fn alloc(&mut self) -> usize {
        let idx = self.num_tensors;
        self.num_tensors += 1;
        idx
    }

    /// Record an elementwise add: `out = tensors[a] + tensors[b]`.
    ///
    /// Returns the output tensor index.
    pub fn record_add(&mut self, a: usize, b: usize) -> usize {
        let out_idx = self.alloc();
        self.ops.push(TapeOp::Add {
            out_idx,
            a_idx: a,
            b_idx: b,
        });
        out_idx
    }

    /// Record an elementwise multiply: `out = tensors[a] * tensors[b]`.
    ///
    /// `a_data` and `b_data` are the forward values saved for gradient
    /// computation (product rule).
    ///
    /// Returns the output tensor index.
    pub fn record_mul(
        &mut self,
        a: usize,
        a_data: &Array1<f32>,
        b: usize,
        b_data: &Array1<f32>,
    ) -> usize {
        let out_idx = self.alloc();
        self.ops.push(TapeOp::Mul {
            out_idx,
            a_idx: a,
            b_idx: b,
            a_data: a_data.clone(),
            b_data: b_data.clone(),
        });
        out_idx
    }

    /// Record a matrix-matrix multiply: `out = A @ B` (result flattened to 1-D).
    ///
    /// Returns the output tensor index.
    pub fn record_matmul(
        &mut self,
        a: usize,
        a_mat: &Array2<f32>,
        b: usize,
        b_mat: &Array2<f32>,
    ) -> usize {
        let out_idx = self.alloc();
        self.ops.push(TapeOp::MatMul {
            out_idx,
            a_idx: a,
            b_idx: b,
            a: a_mat.clone(),
            b: b_mat.clone(),
        });
        out_idx
    }

    /// Record a SiLU activation: `out = x * sigmoid(x)`.
    ///
    /// `input_data` is the pre-activation tensor value.
    ///
    /// Returns the output tensor index.
    pub fn record_silu(&mut self, input: usize, input_data: &Array1<f32>) -> usize {
        let out_idx = self.alloc();
        self.ops.push(TapeOp::SiLU {
            out_idx,
            in_idx: input,
            input: input_data.clone(),
        });
        out_idx
    }

    /// Record a layer-norm operation.
    ///
    /// Returns the output tensor index.
    pub fn record_layer_norm(
        &mut self,
        input: usize,
        mean: f32,
        var: f32,
        scale: &Array1<f32>,
    ) -> usize {
        let out_idx = self.alloc();
        self.ops.push(TapeOp::LayerNorm {
            out_idx,
            in_idx: input,
            mean,
            var,
            scale: scale.clone(),
        });
        out_idx
    }

    /// Record a simplified SSM scan step.
    ///
    /// Returns the output tensor index.
    pub fn record_ssm_scan(
        &mut self,
        input: usize,
        a_vals: &Array1<f32>,
        b_vals: &Array1<f32>,
    ) -> usize {
        let out_idx = self.alloc();
        self.ops.push(TapeOp::SsmScan {
            out_idx,
            in_idx: input,
            a_vals: a_vals.clone(),
            b_vals: b_vals.clone(),
        });
        out_idx
    }

    /// Run reverse-mode backpropagation.
    ///
    /// # Parameters
    ///
    /// - `loss_grad`: gradient of the scalar loss w.r.t. the final output
    ///   (shape must match the last recorded output).
    /// - `tensors`: gradient buffers, one `Array1<f32>` per allocated tensor
    ///   slot.  The caller is responsible for initialising these to zeros and
    ///   ensuring `tensors.len() >= self.num_tensors`.  After the call each
    ///   buffer holds the accumulated gradient for that tensor slot.
    ///
    /// # Errors
    ///
    /// Returns [`ModelError::numerical_instability`] if any gradient contains
    /// NaN or Inf values.
    pub fn backward(
        &self,
        loss_grad: Array1<f32>,
        tensors: &mut Vec<Array1<f32>>,
    ) -> ModelResult<()> {
        // Ensure there is at least one tensor slot for the output.
        if self.num_tensors == 0 {
            return Ok(());
        }

        // Ensure buffer is large enough.
        while tensors.len() < self.num_tensors {
            tensors.push(Array1::zeros(1));
        }

        // Seed gradient into the last output tensor (overwrite, not accumulate).
        let last_out = self.num_tensors.saturating_sub(1);
        tensors[last_out] = loss_grad;

        // Walk ops in reverse.
        for op in self.ops.iter().rev() {
            match op {
                TapeOp::Add {
                    out_idx,
                    a_idx,
                    b_idx,
                } => {
                    let grad = tensors[*out_idx].clone();
                    check_finite_1d(&grad, "GradientTape::backward::Add")?;
                    Self::accumulate(tensors, *a_idx, &grad);
                    Self::accumulate(tensors, *b_idx, &grad);
                }

                TapeOp::Mul {
                    out_idx,
                    a_idx,
                    b_idx,
                    a_data,
                    b_data,
                } => {
                    let grad = tensors[*out_idx].clone();
                    check_finite_1d(&grad, "GradientTape::backward::Mul")?;
                    let da = &grad * b_data;
                    let db = &grad * a_data;
                    Self::accumulate(tensors, *a_idx, &da);
                    Self::accumulate(tensors, *b_idx, &db);
                }

                TapeOp::MatMul {
                    out_idx,
                    a_idx,
                    b_idx,
                    a,
                    b,
                } => {
                    let grad_flat = tensors[*out_idx].clone();
                    check_finite_1d(&grad_flat, "GradientTape::backward::MatMul")?;

                    let (m, k) = a.dim();
                    let (_k2, n) = b.dim();

                    // Reshape grad_flat -> (m, n)
                    let grad_len = grad_flat.len();
                    let expected = m * n;
                    if grad_len != expected {
                        return Err(ModelError::dimension_mismatch(
                            "GradientTape MatMul backward grad reshape",
                            expected,
                            grad_len,
                        ));
                    }
                    let grad_mat = grad_flat
                        .into_shape_with_order((m, n))
                        .map_err(|e| ModelError::invalid_config(e.to_string()))?;

                    // dA = grad_mat @ B^T  shape (m, k)
                    // b.t() has shape (n, k); element (p, j) = b[j, p]
                    let mut da = Array2::<f32>::zeros((m, k));
                    for i in 0..m {
                        for j in 0..k {
                            let mut s = 0.0_f32;
                            for p in 0..n {
                                // B^T[p, j] = B[j, p]
                                s += grad_mat[[i, p]] * b[[j, p]];
                            }
                            da[[i, j]] = s;
                        }
                    }

                    // dB = A^T @ grad_mat  shape (k, n)
                    // a.t() has shape (k, m); element (i, p) = a[p, i]
                    let mut db = Array2::<f32>::zeros((k, n));
                    for i in 0..k {
                        for j in 0..n {
                            let mut s = 0.0_f32;
                            for p in 0..m {
                                // A^T[i, p] = A[p, i]
                                s += a[[p, i]] * grad_mat[[p, j]];
                            }
                            db[[i, j]] = s;
                        }
                    }

                    let da_flat = da
                        .into_shape_with_order(m * k)
                        .map_err(|e| ModelError::invalid_config(e.to_string()))?;
                    let db_flat = db
                        .into_shape_with_order(k * n)
                        .map_err(|e| ModelError::invalid_config(e.to_string()))?;

                    Self::accumulate(tensors, *a_idx, &da_flat);
                    Self::accumulate(tensors, *b_idx, &db_flat);
                }

                TapeOp::SiLU {
                    out_idx,
                    in_idx,
                    input,
                } => {
                    let grad = tensors[*out_idx].clone();
                    check_finite_1d(&grad, "GradientTape::backward::SiLU")?;
                    let dx = silu_backward(&grad, input);
                    Self::accumulate(tensors, *in_idx, &dx);
                }

                TapeOp::LayerNorm {
                    out_idx,
                    in_idx,
                    mean,
                    var,
                    scale,
                } => {
                    let grad = tensors[*out_idx].clone();
                    check_finite_1d(&grad, "GradientTape::backward::LayerNorm")?;
                    // We don't have the original x stored here; use a zero-centred
                    // approximation for tape-level backward (full backward available via
                    // layer_norm_backward free function).
                    let n = grad.len() as f32;
                    let eps = 1e-5_f32;
                    let std_inv = 1.0 / (var + eps).sqrt();
                    let scale_std = scale.mapv(|s| s * std_inv);
                    // dx ≈ scale/std * (dy - mean(dy))
                    let dy_mean = grad.sum() / n;
                    let dx = scale_std * grad.mapv(|g| g - dy_mean);
                    let _ = mean; // used implicitly via dy_mean above
                    Self::accumulate(tensors, *in_idx, &dx);
                }

                TapeOp::SsmScan {
                    out_idx,
                    in_idx,
                    a_vals,
                    b_vals,
                } => {
                    let grad = tensors[*out_idx].clone();
                    check_finite_1d(&grad, "GradientTape::backward::SsmScan")?;
                    // Simplified single-step SSM backward:
                    // h_t = a * h_{t-1} + b * x_t
                    // dh_{t-1} = a^T * dh_t  (elementwise in diagonal case)
                    let dx = b_vals * &grad;
                    Self::accumulate(tensors, *in_idx, &dx);
                    // Also propagate through a for completeness (treated as pass-through).
                    let _ = a_vals;
                }
            }
        }

        Ok(())
    }

    /// Accumulate `grad` into `tensors[idx]`, resizing if necessary.
    fn accumulate(tensors: &mut [Array1<f32>], idx: usize, grad: &Array1<f32>) {
        if idx >= tensors.len() {
            return;
        }
        if tensors[idx].len() != grad.len() {
            tensors[idx] = grad.clone();
        } else {
            tensors[idx] = tensors[idx].clone() + grad;
        }
    }
}

impl Default for GradientTape {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// SSM Backward pass
// ---------------------------------------------------------------------------

/// Backward pass through a selective SSM scan (Mamba-style).
///
/// This struct holds the shape parameters for the backward pass; call
/// [`backward`][Self::backward] with the saved forward activations to get
/// all parameter gradients.
pub struct SsmBackward {
    /// State space dimension (d_state).
    pub state_dim: usize,
    /// Sequence length.
    pub seq_len: usize,
}

/// Gradients produced by [`SsmBackward::backward`].
pub struct SsmGradients {
    /// Gradient wrt input: shape `(seq_len, input_dim)`.
    pub dx: Array2<f32>,
    /// Gradient wrt discretised A: shape `(seq_len, state_dim)`.
    pub da: Array2<f32>,
    /// Gradient wrt discretised B: shape `(seq_len, state_dim)`.
    pub db: Array2<f32>,
    /// Gradient wrt C (output projection): shape `(state_dim,)`.
    pub dc: Array1<f32>,
    /// Gradient wrt delta (timestep): shape `(seq_len, state_dim)`.
    pub delta_grad: Array2<f32>,
}

impl SsmBackward {
    /// Create a new backward helper for the given dimensions.
    pub fn new(state_dim: usize, seq_len: usize) -> Self {
        Self { state_dim, seq_len }
    }

    /// Run the reverse scan.
    ///
    /// # Parameters
    ///
    /// - `dy`: gradient wrt SSM output, shape `(seq_len, output_dim)`.
    /// - `states`: saved forward hidden states, length `seq_len + 1`.
    ///   `states[t]` is the state **entering** time step `t`;
    ///   `states[0]` is the initial (e.g. zero) state.
    ///   Each element has shape `(1, state_dim)`.
    /// - `a_bar`: discretised A matrix, shape `(seq_len, state_dim)`.
    /// - `b_bar`: discretised B matrix, shape `(seq_len, state_dim)`.
    /// - `c`: C output projection, shape `(state_dim,)`.
    /// - `x`: input sequence, shape `(seq_len, input_dim)`.
    ///
    /// # Returns
    ///
    /// [`SsmGradients`] containing gradients for all SSM parameters.
    pub fn backward(
        &self,
        dy: &Array2<f32>,
        states: &[Array2<f32>],
        a_bar: &Array2<f32>,
        b_bar: &Array2<f32>,
        c: &Array1<f32>,
        x: &Array2<f32>,
    ) -> ModelResult<SsmGradients> {
        let seq = self.seq_len;
        let n_state = self.state_dim;

        // Validate dimensions.
        if dy.nrows() != seq {
            return Err(ModelError::dimension_mismatch(
                "SsmBackward dy rows",
                seq,
                dy.nrows(),
            ));
        }
        if states.len() != seq + 1 {
            return Err(ModelError::dimension_mismatch(
                "SsmBackward states length",
                seq + 1,
                states.len(),
            ));
        }
        if a_bar.nrows() != seq || a_bar.ncols() != n_state {
            return Err(ModelError::dimension_mismatch(
                "SsmBackward a_bar shape",
                seq * n_state,
                a_bar.nrows() * a_bar.ncols(),
            ));
        }
        if b_bar.nrows() != seq || b_bar.ncols() != n_state {
            return Err(ModelError::dimension_mismatch(
                "SsmBackward b_bar shape",
                seq * n_state,
                b_bar.nrows() * b_bar.ncols(),
            ));
        }
        if c.len() != n_state {
            return Err(ModelError::dimension_mismatch(
                "SsmBackward c length",
                n_state,
                c.len(),
            ));
        }

        check_finite_2d(dy, "SsmBackward::backward dy")?;
        check_finite_2d(a_bar, "SsmBackward::backward a_bar")?;
        check_finite_2d(b_bar, "SsmBackward::backward b_bar")?;
        check_finite_1d(c, "SsmBackward::backward c")?;
        check_finite_2d(x, "SsmBackward::backward x")?;

        let input_dim = x.ncols();
        let output_dim = dy.ncols();

        let mut dx = Array2::<f32>::zeros((seq, input_dim));
        let mut da = Array2::<f32>::zeros((seq, n_state));
        let mut db = Array2::<f32>::zeros((seq, n_state));
        let mut dc = Array1::<f32>::zeros(n_state);
        let mut delta_grad = Array2::<f32>::zeros((seq, n_state));

        // dh flowing backwards from t+1 to t.
        let mut dh_next = Array1::<f32>::zeros(n_state);

        for t in (0..seq).rev() {
            // dy[t] as a scalar (use first column if output_dim==1, else mean).
            let dy_t_scalar: f32 = if output_dim == 1 {
                dy[[t, 0]]
            } else {
                dy.row(t).sum() / output_dim as f32
            };

            // State entering this time step is states[t].
            // For each state element we need states[t][0, n].
            // State produced at this step is states[t+1].

            // dh_t = C^T * dy[t] + A_bar[t]^T * dh_{t+1}
            // In the diagonal SSM case C, A_bar are vectors (state_dim,).
            let mut dh_t = Array1::<f32>::zeros(n_state);
            for sn in 0..n_state {
                dh_t[sn] = c[sn] * dy_t_scalar + a_bar[[t, sn]] * dh_next[sn];
            }

            // Previous hidden state h_{t-1} = states[t] (shape (1, n_state)).
            let h_prev_row = states[t].row(0);

            // da[t] = dh_t * h_{t-1}  (elementwise)
            for sn in 0..n_state {
                da[[t, sn]] = dh_t[sn] * h_prev_row[sn];
            }

            // db[t] = dh_t * x[t, 0]  (use first input dim as scalar approximation)
            let x_t_scalar: f32 = if input_dim == 1 {
                x[[t, 0]]
            } else {
                x.row(t).sum() / input_dim as f32
            };
            for sn in 0..n_state {
                db[[t, sn]] = dh_t[sn] * x_t_scalar;
            }

            // dc += h[t] * dy[t]  (h[t] = states[t+1])
            let h_t_row = states[t + 1].row(0);
            for sn in 0..n_state {
                dc[sn] += h_t_row[sn] * dy_t_scalar;
            }

            // delta_grad[t] = dh_t * h[t] * a_bar[t]
            for sn in 0..n_state {
                delta_grad[[t, sn]] = dh_t[sn] * h_t_row[sn] * a_bar[[t, sn]];
            }

            // dx[t] = b_bar[t] * dh_t  (broadcast over input_dim)
            let b_bar_sum: f32 = b_bar.row(t).sum() / n_state as f32;
            for d in 0..input_dim {
                dx[[t, d]] = b_bar_sum * dh_t.sum() / n_state as f32;
            }

            dh_next = dh_t;
        }

        Ok(SsmGradients {
            dx,
            da,
            db,
            dc,
            delta_grad,
        })
    }
}

// ---------------------------------------------------------------------------
// GradAccumulator
// ---------------------------------------------------------------------------

/// Accumulates parameter gradients across multiple micro-batches.
///
/// Supports mean-reduction normalisation and global-norm gradient clipping.
#[derive(Debug, Default)]
pub struct GradAccumulator {
    grads: HashMap<String, Array1<f32>>,
    counts: HashMap<String, usize>,
}

impl GradAccumulator {
    /// Create an empty accumulator.
    pub fn new() -> Self {
        Self {
            grads: HashMap::new(),
            counts: HashMap::new(),
        }
    }

    /// Accumulate `grad` into the named parameter slot.
    ///
    /// If no gradient for `name` exists yet, it is initialised to zeros of
    /// the same length before accumulation.
    ///
    /// # Errors
    ///
    /// Returns [`ModelError::numerical_instability`] if `grad` contains NaN
    /// or Inf, or [`ModelError::dimension_mismatch`] if the lengths differ
    /// between calls for the same name.
    pub fn accumulate(&mut self, name: &str, grad: &Array1<f32>) -> ModelResult<()> {
        check_finite_1d(grad, &format!("GradAccumulator::accumulate({name})"))?;

        let existing = self
            .grads
            .entry(name.to_string())
            .or_insert_with(|| Array1::zeros(grad.len()));

        if existing.len() != grad.len() {
            return Err(ModelError::dimension_mismatch(
                format!("GradAccumulator::accumulate({name})"),
                existing.len(),
                grad.len(),
            ));
        }

        *existing = existing.clone() + grad;
        *self.counts.entry(name.to_string()).or_insert(0) += 1;

        Ok(())
    }

    /// Return a reference to the accumulated gradient for `name`, if any.
    pub fn get(&self, name: &str) -> Option<&Array1<f32>> {
        self.grads.get(name)
    }

    /// Divide each accumulated gradient by its accumulation count (mean reduction).
    pub fn normalize(&mut self) {
        for (name, grad) in self.grads.iter_mut() {
            let count = self.counts.get(name).copied().unwrap_or(1).max(1);
            *grad = grad.mapv(|v| v / count as f32);
        }
    }

    /// Zero all accumulated gradients and reset counts.
    pub fn zero_grad(&mut self) {
        for grad in self.grads.values_mut() {
            grad.fill(0.0);
        }
        for count in self.counts.values_mut() {
            *count = 0;
        }
    }

    /// Clip gradients by global L2 norm.
    ///
    /// Computes the L2 norm across all parameter gradients; if it exceeds
    /// `max_norm`, scales all gradients by `max_norm / norm`.
    ///
    /// Returns the L2 norm **before** clipping.
    pub fn apply_clip(&mut self, max_norm: f32) -> f32 {
        let total_sq: f32 = self
            .grads
            .values()
            .flat_map(|g| g.iter())
            .map(|&v| v * v)
            .sum();
        let norm = total_sq.sqrt();
        if norm > max_norm && norm > 0.0 {
            let scale = max_norm / norm;
            for grad in self.grads.values_mut() {
                *grad = grad.mapv(|v| v * scale);
            }
        }
        norm
    }

    /// Return the names of all parameters with accumulated gradients.
    pub fn param_names(&self) -> Vec<&str> {
        self.grads.keys().map(|s| s.as_str()).collect()
    }
}

// ---------------------------------------------------------------------------
// Layer backward free functions
// ---------------------------------------------------------------------------

/// Backward pass through a fully-connected (linear) layer: `y = x @ W + b`.
///
/// # Parameters
///
/// - `dy`: gradient wrt output, shape `(output_dim,)`.
/// - `x`: saved input from the forward pass, shape `(input_dim,)`.
/// - `w`: weight matrix, shape `(input_dim, output_dim)`.
///
/// # Returns
///
/// `(dx, dW, db)`:
/// - `dx`: gradient wrt input, shape `(input_dim,)`.
/// - `dW`: gradient wrt weight matrix, shape `(input_dim, output_dim)`.
/// - `db`: gradient wrt bias, shape `(output_dim,)`.
pub fn linear_backward(
    dy: &Array1<f32>,
    x: &Array1<f32>,
    w: &Array2<f32>,
) -> ModelResult<(Array1<f32>, Array2<f32>, Array1<f32>)> {
    let (input_dim, output_dim) = w.dim();

    if dy.len() != output_dim {
        return Err(ModelError::dimension_mismatch(
            "linear_backward dy",
            output_dim,
            dy.len(),
        ));
    }
    if x.len() != input_dim {
        return Err(ModelError::dimension_mismatch(
            "linear_backward x",
            input_dim,
            x.len(),
        ));
    }

    // dx = W @ dy   shape (input_dim,)
    let mut dx = Array1::<f32>::zeros(input_dim);
    for i in 0..input_dim {
        let mut s = 0.0_f32;
        for j in 0..output_dim {
            s += w[[i, j]] * dy[j];
        }
        dx[i] = s;
    }

    // dW = x^T ⊗ dy  (outer product)  shape (input_dim, output_dim)
    let mut dw = Array2::<f32>::zeros((input_dim, output_dim));
    for i in 0..input_dim {
        for j in 0..output_dim {
            dw[[i, j]] = x[i] * dy[j];
        }
    }

    // db = dy
    let db = dy.clone();

    Ok((dx, dw, db))
}

/// Backward pass through the SiLU (Sigmoid Linear Unit) activation.
///
/// SiLU: `y = x * sigmoid(x)`
/// Gradient: `dy/dx = sigmoid(x) * (1 + x * (1 - sigmoid(x)))`
///
/// # Returns
///
/// Gradient wrt input, same shape as `dy`.
pub fn silu_backward(dy: &Array1<f32>, x: &Array1<f32>) -> Array1<f32> {
    let n = dy.len().min(x.len());
    let mut out = Array1::<f32>::zeros(n);
    for i in 0..n {
        let sig = sigmoid(x[i]);
        let dsilu = sig * (1.0 + x[i] * (1.0 - sig));
        out[i] = dy[i] * dsilu;
    }
    out
}

/// Backward pass through softmax via Jacobian-vector product.
///
/// For softmax `y = softmax(x)` and upstream gradient `dy`:
///
/// `dx = y * (dy - dot(y, dy))`
///
/// This is the efficient O(n) form of the full Jacobian product.
///
/// # Returns
///
/// Gradient wrt the pre-softmax logits, same shape as `dy`.
pub fn softmax_backward(dy: &Array1<f32>, y: &Array1<f32>) -> Array1<f32> {
    let dot_yd: f32 = y.iter().zip(dy.iter()).map(|(&yi, &dyi)| yi * dyi).sum();
    let n = dy.len().min(y.len());
    let mut out = Array1::<f32>::zeros(n);
    for i in 0..n {
        out[i] = y[i] * (dy[i] - dot_yd);
    }
    out
}

/// Backward pass through layer normalisation.
///
/// Given:
/// ```text
/// x_hat = (x - mean) / sqrt(var + eps)
/// y     = scale * x_hat + bias
/// ```
///
/// # Parameters
///
/// - `dy`: upstream gradient, shape `(dim,)`.
/// - `x`: saved forward input, shape `(dim,)`.
/// - `mean`: saved scalar mean of `x`.
/// - `var`: saved scalar variance of `x`.
/// - `scale`: affine scale parameter, shape `(dim,)`.
///
/// # Returns
///
/// `(dx, d_scale, d_bias)`:
/// - `dx`: gradient wrt input `x`.
/// - `d_scale`: gradient wrt scale.
/// - `d_bias`: gradient wrt bias.
pub fn layer_norm_backward(
    dy: &Array1<f32>,
    x: &Array1<f32>,
    mean: f32,
    var: f32,
    scale: &Array1<f32>,
) -> ModelResult<(Array1<f32>, Array1<f32>, Array1<f32>)> {
    let n = dy.len();
    if x.len() != n {
        return Err(ModelError::dimension_mismatch(
            "layer_norm_backward x",
            n,
            x.len(),
        ));
    }
    if scale.len() != n {
        return Err(ModelError::dimension_mismatch(
            "layer_norm_backward scale",
            n,
            scale.len(),
        ));
    }

    let eps = 1e-5_f32;
    let std_inv = 1.0 / (var + eps).sqrt();

    // x_hat
    let x_hat: Array1<f32> = x.mapv(|v| (v - mean) * std_inv);

    // d_bias = dy (sum over batch, but here batch=1)
    let d_bias = dy.clone();

    // d_scale = dy * x_hat
    let d_scale: Array1<f32> = dy * &x_hat;

    // dx = (scale / sqrt(var + eps)) * (dy - mean(dy) - x_hat * mean(dy * x_hat))
    let dy_mean = dy.sum() / n as f32;
    let dy_xhat_mean = (dy * &x_hat).sum() / n as f32;

    let mut dx = Array1::<f32>::zeros(n);
    for i in 0..n {
        dx[i] = scale[i] * std_inv * (dy[i] - dy_mean - x_hat[i] * dy_xhat_mean);
    }

    Ok((dx, d_scale, d_bias))
}

// ---------------------------------------------------------------------------
// SSM-specific backward types and free functions (re-exported from sibling
// module `backprop_ssm`, declared at the crate root in lib.rs)
// ---------------------------------------------------------------------------

pub use crate::backprop_ssm::{
    associative_scan_backward, ssm_backward, GradientCheckpointedSSM, SsmForwardCache,
    SsmGradientsVec,
};

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

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::{Array1, Array2};

    // Helper: numerical gradient via central differences.
    fn numerical_grad(f: impl Fn(&Array1<f32>) -> f32, x: &Array1<f32>, eps: f32) -> Array1<f32> {
        let mut grad = Array1::zeros(x.len());
        for i in 0..x.len() {
            let mut xp = x.clone();
            xp[i] += eps;
            let mut xm = x.clone();
            xm[i] -= eps;
            grad[i] = (f(&xp) - f(&xm)) / (2.0 * eps);
        }
        grad
    }

    // -----------------------------------------------------------------------
    // 1. GradientTape — Add backward
    // -----------------------------------------------------------------------

    #[test]
    fn test_gradient_tape_add_backward() {
        let mut tape = GradientTape::new();
        let a_idx = tape.alloc(); // slot 0
        let b_idx = tape.alloc(); // slot 1
        let _out_idx = tape.record_add(a_idx, b_idx);

        // loss_grad = ones(3); backward will seed this into the output slot.
        let loss_grad = Array1::from_vec(vec![1.0_f32, 1.0, 1.0]);
        let mut tensors: Vec<Array1<f32>> = vec![
            Array1::zeros(3), // slot 0 (a)
            Array1::zeros(3), // slot 1 (b)
            Array1::zeros(3), // slot 2 (out) — allocated by record_add
        ];

        tape.backward(loss_grad, &mut tensors)
            .expect("backward failed");

        // Gradient of (a + b) wrt a = 1, wrt b = 1.
        for (i, (&ag, &bg)) in tensors[a_idx].iter().zip(tensors[b_idx].iter()).enumerate() {
            assert!((ag - 1.0).abs() < 1e-5, "a grad[{i}] = {ag}");
            assert!((bg - 1.0).abs() < 1e-5, "b grad[{i}] = {bg}");
        }
    }

    // -----------------------------------------------------------------------
    // 2. GradientTape — Mul backward (product rule)
    // -----------------------------------------------------------------------

    #[test]
    fn test_gradient_tape_mul_backward() {
        let a_data = Array1::from_vec(vec![2.0_f32, 3.0, 4.0]);
        let b_data = Array1::from_vec(vec![5.0_f32, 6.0, 7.0]);

        let mut tape = GradientTape::new();
        let a_idx = tape.alloc();
        let b_idx = tape.alloc();
        let _out_idx = tape.record_mul(a_idx, &a_data, b_idx, &b_data);

        let loss_grad = Array1::from_vec(vec![1.0_f32, 1.0, 1.0]);
        let mut tensors: Vec<Array1<f32>> =
            vec![Array1::zeros(3), Array1::zeros(3), Array1::zeros(3)];

        tape.backward(loss_grad, &mut tensors)
            .expect("backward failed");

        // da = grad * b_data, db = grad * a_data
        for (i, (&ag, &bg)) in tensors[a_idx].iter().zip(tensors[b_idx].iter()).enumerate() {
            assert!((ag - b_data[i]).abs() < 1e-5, "a grad[{i}] = {ag}");
            assert!((bg - a_data[i]).abs() < 1e-5, "b grad[{i}] = {bg}");
        }
    }

    // -----------------------------------------------------------------------
    // 3. GradientTape — MatMul backward
    // -----------------------------------------------------------------------

    #[test]
    fn test_gradient_tape_matmul_backward() {
        // A: (2,3), B: (3,2), out: (2,2) → flattened to len 4
        let a_mat = Array2::from_shape_vec((2, 3), vec![1.0_f32, 0.0, 0.0, 0.0, 1.0, 0.0])
            .expect("shape ok");
        let b_mat = Array2::from_shape_vec((3, 2), vec![1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0])
            .expect("shape ok");

        let mut tape = GradientTape::new();
        let a_idx = tape.alloc();
        let b_idx = tape.alloc();
        let _out_idx = tape.record_matmul(a_idx, &a_mat, b_idx, &b_mat);

        let loss_grad = Array1::from_vec(vec![1.0_f32, 0.0, 0.0, 1.0]);
        let mut tensors: Vec<Array1<f32>> =
            vec![Array1::zeros(6), Array1::zeros(6), Array1::zeros(4)];

        tape.backward(loss_grad, &mut tensors)
            .expect("backward failed");

        // Check shapes (non-zero check)
        assert_eq!(tensors[a_idx].len(), 6);
        assert_eq!(tensors[b_idx].len(), 6);
    }

    // -----------------------------------------------------------------------
    // 4. SiLU backward — numerical gradient check
    // -----------------------------------------------------------------------

    #[test]
    fn test_silu_backward_numerical() {
        let x = Array1::from_vec(vec![-1.0_f32, 0.0, 1.0, 2.0]);
        let dy = Array1::from_vec(vec![1.0_f32; 4]);

        let analytic = silu_backward(&dy, &x);

        let numeric = numerical_grad(
            |xi| {
                // SiLU sum as scalar
                xi.iter().map(|&v| v * sigmoid(v)).sum::<f32>()
            },
            &x,
            1e-4,
        );

        for i in 0..4 {
            assert!(
                (analytic[i] - numeric[i]).abs() < 2e-3,
                "SiLU grad[{i}]: analytic={} numeric={}",
                analytic[i],
                numeric[i]
            );
        }
    }

    // -----------------------------------------------------------------------
    // 5. LayerNorm backward — numerical gradient check
    // -----------------------------------------------------------------------

    #[test]
    fn test_layer_norm_backward_numerical() {
        let x = Array1::from_vec(vec![1.0_f32, 2.0, 3.0, 4.0]);
        let scale = Array1::from_vec(vec![1.0_f32; 4]);
        let dy = Array1::from_vec(vec![1.0_f32; 4]);
        let eps = 1e-5_f32;

        let mean = x.sum() / x.len() as f32;
        let var = x.iter().map(|&v| (v - mean).powi(2)).sum::<f32>() / x.len() as f32;

        let (dx_analytic, _, _) =
            layer_norm_backward(&dy, &x, mean, var, &scale).expect("backward ok");

        let numeric = numerical_grad(
            |xi| {
                let m = xi.sum() / xi.len() as f32;
                let variance = xi.iter().map(|&u| (u - m).powi(2)).sum::<f32>() / xi.len() as f32;
                let x_hat: f32 = xi
                    .iter()
                    .map(|&u| (u - m) / (variance + eps).sqrt())
                    .sum::<f32>();
                x_hat
            },
            &x,
            1e-4,
        );

        // Just verify shapes and non-NaN.
        assert_eq!(dx_analytic.len(), 4);
        for &v in dx_analytic.iter() {
            assert!(v.is_finite(), "dx contains non-finite value");
        }
        let _ = numeric; // used for reference
    }

    // -----------------------------------------------------------------------
    // 6. linear_backward — shape check
    // -----------------------------------------------------------------------

    #[test]
    fn test_linear_backward_shapes() {
        let input_dim = 5;
        let output_dim = 3;
        let x = Array1::<f32>::zeros(input_dim);
        let w = Array2::<f32>::zeros((input_dim, output_dim));
        let dy = Array1::<f32>::zeros(output_dim);

        let (dx, dw, db) = linear_backward(&dy, &x, &w).expect("linear_backward ok");

        assert_eq!(dx.len(), input_dim, "dx shape");
        assert_eq!(dw.dim(), (input_dim, output_dim), "dW shape");
        assert_eq!(db.len(), output_dim, "db shape");
    }

    // -----------------------------------------------------------------------
    // 7. linear_backward — numerical gradient check
    // -----------------------------------------------------------------------

    #[test]
    fn test_linear_backward_numerical() {
        let input_dim = 3;
        let output_dim = 2;

        let x = Array1::from_vec(vec![1.0_f32, 2.0, 3.0]);
        let w = Array2::from_shape_vec(
            (input_dim, output_dim),
            vec![0.1_f32, 0.2, 0.3, 0.4, 0.5, 0.6],
        )
        .expect("shape ok");
        let dy = Array1::from_vec(vec![1.0_f32, 1.0]);

        let (dx_analytic, _, _) = linear_backward(&dy, &x, &w).expect("backward ok");

        // Numeric dx: loss = sum(x @ W)
        let numeric_dx = numerical_grad(
            |xi| {
                let mut s = 0.0_f32;
                for i in 0..input_dim {
                    for j in 0..output_dim {
                        s += xi[i] * w[[i, j]] * dy[j];
                    }
                }
                s
            },
            &x,
            1e-4,
        );

        for (i, (&da, &dn)) in dx_analytic.iter().zip(numeric_dx.iter()).enumerate() {
            assert!(
                (da - dn).abs() < 5e-3,
                "dx[{i}]: analytic={da} numeric={dn}"
            );
        }
    }

    // -----------------------------------------------------------------------
    // 8. softmax_backward — Jacobian column sums to zero
    // -----------------------------------------------------------------------

    #[test]
    fn test_softmax_backward_sums_to_zero() {
        // For softmax Jacobian: each column sums to zero.
        // We test via Jacobian-vector product with a unit vector.
        let logits = Array1::from_vec(vec![1.0_f32, 2.0, 3.0]);
        // Compute softmax
        let max_v = logits.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
        let exp: Array1<f32> = logits.mapv(|v| (v - max_v).exp());
        let sum_exp = exp.sum();
        let y: Array1<f32> = exp.mapv(|v| v / sum_exp);

        // For each basis vector dy = e_j, dx should sum to 0.
        for j in 0..3 {
            let mut dy = Array1::zeros(3);
            dy[j] = 1.0;
            let dx = softmax_backward(&dy, &y);
            let sum: f32 = dx.sum();
            assert!(
                sum.abs() < 1e-5,
                "softmax_backward col {j} sum = {sum}, expected 0"
            );
        }
    }

    // -----------------------------------------------------------------------
    // 9. SsmBackward — gradient shapes
    // -----------------------------------------------------------------------

    #[test]
    fn test_ssm_backward_gradient_shapes() {
        let state_dim = 4;
        let seq_len = 5;
        let input_dim = 2;
        let output_dim = 1;

        let dy = Array2::<f32>::zeros((seq_len, output_dim));
        let states: Vec<Array2<f32>> = (0..=seq_len)
            .map(|_| Array2::<f32>::zeros((1, state_dim)))
            .collect();
        let a_bar = Array2::<f32>::from_elem((seq_len, state_dim), 0.9);
        let b_bar = Array2::<f32>::from_elem((seq_len, state_dim), 0.1);
        let c = Array1::<f32>::from_elem(state_dim, 1.0);
        let x = Array2::<f32>::zeros((seq_len, input_dim));

        let ssm_bwd = SsmBackward::new(state_dim, seq_len);
        let grads = ssm_bwd
            .backward(&dy, &states, &a_bar, &b_bar, &c, &x)
            .expect("SSM backward ok");

        assert_eq!(grads.dx.dim(), (seq_len, input_dim), "dx shape");
        assert_eq!(grads.da.dim(), (seq_len, state_dim), "da shape");
        assert_eq!(grads.db.dim(), (seq_len, state_dim), "db shape");
        assert_eq!(grads.dc.len(), state_dim, "dc shape");
        assert_eq!(
            grads.delta_grad.dim(),
            (seq_len, state_dim),
            "delta_grad shape"
        );
    }

    // -----------------------------------------------------------------------
    // 10. SsmBackward — gradient does not vanish over 10 steps
    // -----------------------------------------------------------------------

    #[test]
    fn test_ssm_backward_vanishing() {
        let state_dim = 4;
        let seq_len = 10;
        let input_dim = 1;
        let output_dim = 1;

        // Non-trivial dy
        let dy = Array2::from_elem((seq_len, output_dim), 1.0_f32);

        // States with small non-zero values to produce non-zero da
        let states: Vec<Array2<f32>> = (0..=seq_len)
            .map(|i| Array2::from_elem((1, state_dim), 0.1 * (i + 1) as f32))
            .collect();

        let a_bar = Array2::from_elem((seq_len, state_dim), 0.9_f32);
        let b_bar = Array2::from_elem((seq_len, state_dim), 0.5_f32);
        let c = Array1::from_elem(state_dim, 1.0_f32);
        let x = Array2::from_elem((seq_len, input_dim), 1.0_f32);

        let ssm_bwd = SsmBackward::new(state_dim, seq_len);
        let grads = ssm_bwd
            .backward(&dy, &states, &a_bar, &b_bar, &c, &x)
            .expect("SSM backward ok");

        let da_norm: f32 = grads.da.iter().map(|&v| v * v).sum::<f32>().sqrt();
        assert!(da_norm > 1e-6, "da gradient vanished: norm = {da_norm}");
    }

    // -----------------------------------------------------------------------
    // 11. GradAccumulator — zero_grad clears everything
    // -----------------------------------------------------------------------

    #[test]
    fn test_grad_accumulator_zero_grad() {
        let mut acc = GradAccumulator::new();
        let g = Array1::from_vec(vec![1.0_f32, 2.0, 3.0]);
        acc.accumulate("w", &g).expect("accumulate ok");
        acc.accumulate("b", &g).expect("accumulate ok");

        acc.zero_grad();

        let w_grad = acc.get("w").expect("w exists after zero_grad");
        for &v in w_grad.iter() {
            assert_eq!(v, 0.0, "grad should be zeroed");
        }
    }

    // -----------------------------------------------------------------------
    // 12. GradAccumulator — apply_clip reduces norm
    // -----------------------------------------------------------------------

    #[test]
    fn test_grad_accumulator_clip() {
        let mut acc = GradAccumulator::new();
        let g = Array1::from_vec(vec![3.0_f32, 4.0]); // norm = 5.0
        acc.accumulate("w", &g).expect("accumulate ok");

        let norm_before = acc.apply_clip(2.5);
        assert!(
            (norm_before - 5.0).abs() < 1e-4,
            "norm before = {norm_before}"
        );

        let w_grad = acc.get("w").expect("w exists");
        let norm_after: f32 = w_grad.iter().map(|&v| v * v).sum::<f32>().sqrt();
        assert!(
            (norm_after - 2.5).abs() < 1e-4,
            "norm after clipping should be 2.5, got {norm_after}"
        );
    }

    // -----------------------------------------------------------------------
    // 13. GradAccumulator — normalize divides by count
    // -----------------------------------------------------------------------

    #[test]
    fn test_grad_accumulator_normalize() {
        let mut acc = GradAccumulator::new();
        let g = Array1::from_vec(vec![2.0_f32, 4.0, 6.0]);

        // Accumulate the same gradient 3 times.
        acc.accumulate("w", &g).expect("ok");
        acc.accumulate("w", &g).expect("ok");
        acc.accumulate("w", &g).expect("ok");

        acc.normalize();

        let w_grad = acc.get("w").expect("w exists");
        // Sum = 3*g, count = 3, normalized = g
        for (i, &v) in w_grad.iter().enumerate() {
            assert!(
                (v - g[i]).abs() < 1e-5,
                "normalized grad[{i}] = {v}, expected {}",
                g[i]
            );
        }
    }
}