nuvec-nn 0.5.0

Neural-network modules, layers and optimizers for nuvec
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
//! Optimizers, learning-rate schedules and gradient clipping, with MLX's formulas
//! (`mlx.optimizers`).
//!
//! An optimizer updates the trainable leaves of a model from a gradient tree of the
//! model's type (as `nuvec::value_and_grad` returns it): `opt.update(&mut model, &grads)`.
//! Its state is a tree (`step`, `learning_rate`, and the slots by parameter path), so an
//! optimizer is part of a compiled training step's state and of a checkpoint
//! (`nuvec::save(path, &(model, opt))`):
//!
//! - state is keyed by parameter path: freezing, unfreezing or replacing parameters never
//!   moves state between them, and the state of a parameter that is not updated is kept;
//! - the step counter and the learning rate are 0-D arrays (as MLX's
//!   `optimizer.state["learning_rate"]`); a schedule writes the rate from the step at every
//!   update (evaluated before the step is counted, in float32), so a compiled step sees
//!   every new step and rate;
//! - hyperparameters are static fields of the tree: changing one retraces a compiled step.

use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::Arc;

use nuvec::tree::{Leaves, Visit, VisitMut};
use nuvec::{Array, Category, DType, Error, OpErrorKind, Result, Tree, ops};

use crate::Module;

/// Updates a model's trainable leaves from their gradients, as MLX's `Optimizer`.
///
/// The trait is object-safe (`Box<dyn Optimizer>` is an optimizer and a tree too). Its
/// state is reached only through [`Optimizer::visit_state`] and
/// [`Optimizer::visit_state_mut`] (each optimizer has its own slot type).
pub trait Optimizer {
    /// One step over named parameters (the path keys each parameter's state) and their
    /// gradients: the updated parameters, in order. Increments the step once.
    fn apply(&mut self, params: &[(&str, &Array)], grads: &[&Array]) -> Result<Vec<Array>>;

    /// Creates zero state for `params` that have none (paths with state are kept).
    fn init_params(&mut self, params: &[(&str, &Array)]);

    /// Visits the state (`step`, `learning_rate`, slots) and the hyperparameters.
    fn visit_state(&self, v: &mut Visit<'_>);

    /// Visits the state mutably.
    fn visit_state_mut(&mut self, v: &mut VisitMut<'_>);

    /// The learning rate: the 0-D float32 array in the state.
    fn learning_rate(&self) -> Array;

    /// Sets the learning rate array (in a compiled step it is an input: no retrace).
    fn set_learning_rate(&mut self, lr: f32);

    /// A boxed copy (object-safe `Clone`).
    fn clone_box(&self) -> Box<dyn Optimizer>;

    /// Creates the state of every trainable leaf of `model` now (instead of at the first
    /// update), so a compiled step's state structure is fixed from the start; also needed
    /// before loading a checkpoint into a fresh optimizer.
    fn init<M: Tree>(&mut self, model: &M)
    where
        Self: Sized,
    {
        let params = model.trainable_parameters();
        let refs: Vec<(&str, &Array)> = params.iter().map(|(p, a)| (p.as_str(), a)).collect();
        self.init_params(&refs);
    }

    /// Applies one step to the trainable leaves of `model` from `grads`, a gradient tree of
    /// the model's type with the model's paths (else an error). Frozen parameters and
    /// buffers are left alone; their state is kept.
    fn update<M: Tree>(&mut self, model: &mut M, grads: &M) -> Result<()>
    where
        Self: Sized,
    {
        let (params, gradients) = trainable_with_grads(model, grads)?;
        let p: Vec<(&str, &Array)> = params.iter().map(|(n, a)| (n.as_str(), a)).collect();
        let g: Vec<&Array> = gradients.iter().collect();
        let updated = self.apply(&p, &g)?;
        let mut updated: HashMap<&str, Array> = params
            .iter()
            .map(|(n, _)| n.as_str())
            .zip(updated)
            .collect();
        *model = model.map(|path, a| updated.remove(path).unwrap_or_else(|| a.clone()));
        Ok(())
    }
}

/// Named parameters and their gradients.
type Trainable = (Vec<(String, Array)>, Vec<Array>);

/// The trainable leaves of `model` with their paths, and their gradients from `grads`
/// (which must have the model's leaf paths).
fn trainable_with_grads<M: Tree>(model: &M, grads: &M) -> Result<Trainable> {
    let (paths, gpaths) = (model.paths(), grads.paths());
    if paths != gpaths {
        let first = paths.iter().zip(&gpaths).find(|(a, b)| a != b).map_or_else(
            || "a different number of leaves".to_owned(),
            |(a, b)| format!("{b:?} where the model has {a:?}"),
        );
        return Err(Error::InvalidArgument(format!(
            "update: the gradients do not have the model's paths: {first}"
        )));
    }
    let trainable: HashSet<String> = model
        .trainable_parameters()
        .into_iter()
        .map(|(p, _)| p)
        .collect();
    let mut params = Vec::new();
    let mut gs = Vec::new();
    for ((path, a), g) in model.flatten().into_iter().zip(grads.leaves()) {
        if trainable.contains(&path) {
            params.push((path, a));
            gs.push(g);
        }
    }
    Ok((params, gs))
}

impl Optimizer for Box<dyn Optimizer> {
    fn apply(&mut self, params: &[(&str, &Array)], grads: &[&Array]) -> Result<Vec<Array>> {
        (**self).apply(params, grads)
    }
    fn init_params(&mut self, params: &[(&str, &Array)]) {
        (**self).init_params(params);
    }
    fn visit_state(&self, v: &mut Visit<'_>) {
        (**self).visit_state(v);
    }
    fn visit_state_mut(&mut self, v: &mut VisitMut<'_>) {
        (**self).visit_state_mut(v);
    }
    fn learning_rate(&self) -> Array {
        (**self).learning_rate()
    }
    fn set_learning_rate(&mut self, lr: f32) {
        (**self).set_learning_rate(lr);
    }
    fn clone_box(&self) -> Box<dyn Optimizer> {
        (**self).clone_box()
    }
}

impl Clone for Box<dyn Optimizer> {
    fn clone(&self) -> Self {
        (**self).clone_box()
    }
}

/// A boxed optimizer is a tree through its state: the one hand-written tree impl of
/// nuvec-nn (a trait object cannot be derived).
impl Leaves for Box<dyn Optimizer> {
    fn visit(&self, v: &mut Visit<'_>) {
        (**self).visit_state(v);
    }
}

impl Tree for Box<dyn Optimizer> {
    fn visit_mut(&mut self, v: &mut VisitMut<'_>) {
        (**self).visit_state_mut(v);
    }
}

impl std::fmt::Debug for dyn Optimizer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("dyn Optimizer")
    }
}

/// A boxed optimizer (a tree through [`Optimizer::visit_state`]).
type Boxed = Box<dyn Optimizer>;

/// An optimizer's state: the step counter and the learning rate as 0-D arrays, and one
/// slot (momenta, moment estimates) per parameter path.
#[derive(Debug, Clone, Tree)]
pub struct OptimizerState<S: Tree> {
    /// Steps taken (a 0-D uint64 array).
    pub step: Array,
    /// The learning rate (a 0-D float32 array).
    pub learning_rate: Array,
    /// The slots by parameter path.
    pub slots: BTreeMap<String, S>,
}

impl<S: Tree> OptimizerState<S> {
    fn new(lr: f32) -> Self {
        Self {
            step: Array::from(0u64),
            learning_rate: Array::from(lr),
            slots: BTreeMap::new(),
        }
    }
}

/// A learning rate: a constant or a function of the step (a 0-D array, from 0) returning a
/// 0-D array, so a compiled step sees every new rate.
#[derive(Clone)]
pub enum LearningRate {
    /// The same rate at every step (it initialises the state's rate array).
    Constant(f32),
    /// The rate at each step, written into the state before each update.
    Schedule(Arc<dyn Fn(&Array) -> Array + Send + Sync>),
}

impl LearningRate {
    /// The rate at `step` (a 0-D array), in float32.
    pub fn at(&self, step: &Array) -> Array {
        match self {
            Self::Constant(lr) => Array::from(*lr),
            Self::Schedule(f) => f(step).astype(DType::F32),
        }
    }

    fn initial(&self) -> Array {
        self.at(&Array::from(0u64))
    }
}

impl std::fmt::Debug for LearningRate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Constant(lr) => write!(f, "Constant({lr})"),
            Self::Schedule(_) => f.write_str("Schedule"),
        }
    }
}

impl From<f64> for LearningRate {
    fn from(lr: f64) -> Self {
        Self::Constant(lr as f32)
    }
}

impl From<f32> for LearningRate {
    fn from(lr: f32) -> Self {
        Self::Constant(lr)
    }
}

/// Learning-rate schedules, as MLX's `mlx.optimizers` schedulers: float32 array functions of
/// the step.
pub mod schedules {
    use std::sync::Arc;

    use nuvec::{Array, DType};

    use super::LearningRate;

    fn schedule(f: impl Fn(&Array) -> Array + Send + Sync + 'static) -> LearningRate {
        LearningRate::Schedule(Arc::new(f))
    }

    /// The step in float32.
    fn steps(step: &Array) -> Array {
        step.astype(DType::F32)
    }

    /// `b^t` for a float32 array `t` (`exp(t ln b)`, in float32, `b` rounded to float32 as
    /// MLX does).
    fn power(b: f64, t: &Array) -> Array {
        t.multiply(f64::from(b as f32).ln()).exp()
    }

    /// `init · decay_rate^step`.
    pub fn exponential_decay(init: f64, decay_rate: f64) -> LearningRate {
        schedule(move |step| power(decay_rate, &steps(step)).multiply(init))
    }

    /// `init · decay_rate^⌊step / step_size⌋`.
    pub fn step_decay(init: f64, decay_rate: f64, step_size: u64) -> LearningRate {
        let size = step_size.max(1) as f64;
        schedule(move |step| {
            let k = steps(step)
                .divide(size)
                .astype(DType::U32)
                .astype(DType::F32);
            power(decay_rate, &k).multiply(init)
        })
    }

    /// From `init` to `end` along half a cosine over `decay_steps`, then `end`.
    pub fn cosine_decay(init: f64, decay_steps: u64, end: f64) -> LearningRate {
        let decay_steps = decay_steps.max(1) as f64;
        let rate = std::f64::consts::PI / decay_steps;
        schedule(move |step| {
            let s = steps(step).minimum(decay_steps);
            let decay = s.multiply(rate).cos().add(1.0).multiply(0.5);
            decay.multiply(init - end).add(end)
        })
    }

    /// From `init` to `end` linearly over `steps`, then `end`.
    pub fn linear_schedule(init: f64, end: f64, steps_total: u64) -> LearningRate {
        let n = steps_total.max(1) as f64;
        let slope = (end - init) / n;
        schedule(move |step| steps(step).minimum(n).multiply(slope).add(init))
    }

    /// `schedules[0]` until `boundaries[0]`, then `schedules[1]` (counting its steps
    /// from the boundary), and so on. `boundaries` must have one entry fewer than
    /// `schedules` (an error otherwise).
    pub fn join_schedules(
        schedules: Vec<LearningRate>,
        boundaries: Vec<u64>,
    ) -> nuvec::Result<LearningRate> {
        if schedules.len() != boundaries.len() + 1 {
            return Err(nuvec::Error::InvalidArgument(format!(
                "join_schedules: {} schedules need {} boundaries, got {}",
                schedules.len(),
                schedules.len().saturating_sub(1),
                boundaries.len()
            )));
        }
        Ok(schedule(move |step| {
            let t = steps(step);
            let mut out = schedules[0].at(&t);
            for (b, s) in boundaries.iter().zip(&schedules[1..]) {
                let b = *b as f64;
                let later = s.at(&t.subtract(b).maximum(0.0));
                out = t.less(b).select(&out, &later);
            }
            out
        }))
    }
}

/// The learning rate cast to the gradient's dtype.
fn rate(lr: &Array, like: &Array) -> Array {
    lr.astype(like.dtype())
}

/// One step of an optimizer with slot type `S`: checks the arguments, writes the scheduled
/// rate, increments the step, and updates each parameter with `f(p, g, slot, lr, step)`
/// (`step` counted already). The state changes only on success.
fn step<S: Tree>(
    state: &mut OptimizerState<S>,
    schedule: &LearningRate,
    params: &[(&str, &Array)],
    grads: &[&Array],
    new_slot: impl Fn(&Array) -> S,
    mut f: impl FnMut(&Array, &Array, &mut S, &Array, &Array) -> Array,
) -> Result<Vec<Array>> {
    if params.len() != grads.len() {
        return Err(Error::InvalidArgument(format!(
            "{} gradients for {} parameters",
            grads.len(),
            params.len()
        )));
    }
    let mut next = state.clone();
    if let LearningRate::Schedule(s) = schedule {
        next.learning_rate = s(&next.step).astype(DType::F32);
    }
    next.step = next.step.add(1u64);
    let mut seen = HashSet::new();
    let mut out = Vec::with_capacity(params.len());
    for (&(path, p), &g) in params.iter().zip(grads) {
        if !seen.insert(path) {
            return Err(Error::InvalidArgument(format!(
                "parameter {path:?} given twice"
            )));
        }
        let slot = next
            .slots
            .entry(path.to_owned())
            .or_insert_with(|| new_slot(p));
        let mut mismatch = false;
        slot.for_each(|_, s| mismatch |= s.shape() != p.shape());
        if mismatch {
            return Err(Error::InvalidArgument(format!(
                "parameter {path:?} changed its shape to {:?}",
                p.shape()
            )));
        }
        out.push(f(p, g, slot, &next.learning_rate, &next.step).check()?);
    }
    *state = next;
    Ok(out)
}

/// Implements the object-safe part of [`Optimizer`] for an optimizer with a `state` field.
macro_rules! state_access {
    ($slot:ty) => {
        fn init_params(&mut self, params: &[(&str, &Array)]) {
            for &(path, p) in params {
                self.state
                    .slots
                    .entry(path.to_owned())
                    .or_insert_with(|| <$slot>::zeros(p));
            }
        }
        fn visit_state(&self, v: &mut Visit<'_>) {
            v.static_field("optimizer", &std::any::type_name::<Self>());
            Leaves::visit(self, v);
        }
        fn visit_state_mut(&mut self, v: &mut VisitMut<'_>) {
            Tree::visit_mut(self, v);
        }
        fn learning_rate(&self) -> Array {
            self.state.learning_rate.clone()
        }
        fn set_learning_rate(&mut self, lr: f32) {
            self.state.learning_rate = Array::from(lr);
        }
        fn clone_box(&self) -> Box<dyn Optimizer> {
            Box::new(self.clone())
        }
    };
}

/// One state array like `p` (zeros).
fn zeros(p: &Array) -> Array {
    ops::zeros(p.shape(), p.dtype())
}

/// SGD's state: the momentum buffer.
#[derive(Debug, Clone, Tree)]
pub struct SgdSlot {
    /// The momentum.
    pub v: Array,
}

impl SgdSlot {
    fn zeros(p: &Array) -> Self {
        Self { v: zeros(p) }
    }
}

/// Stochastic gradient descent with optional momentum, dampening, Nesterov momentum and
/// weight decay, as MLX's `optimizers.SGD`.
#[derive(Debug, Clone, Tree)]
pub struct Sgd {
    #[tree(skip, static_unchecked)]
    schedule: LearningRate,
    #[tree(skip)]
    momentum: f64,
    #[tree(skip)]
    weight_decay: f64,
    #[tree(skip)]
    dampening: f64,
    #[tree(skip)]
    nesterov: bool,
    #[tree(rename = "")]
    state: OptimizerState<SgdSlot>,
}

/// A fresh state whose rate array is the schedule's at step 0.
fn fresh<S: Tree>(schedule: &LearningRate) -> OptimizerState<S> {
    let mut state = OptimizerState::new(0.0);
    state.learning_rate = schedule.initial();
    state
}

impl Sgd {
    /// Plain SGD.
    pub fn new(learning_rate: impl Into<LearningRate>) -> Self {
        let schedule = learning_rate.into();
        Self {
            state: fresh(&schedule),
            schedule,
            momentum: 0.0,
            weight_decay: 0.0,
            dampening: 0.0,
            nesterov: false,
        }
    }

    /// Sets the momentum.
    pub fn momentum(mut self, momentum: f64) -> Self {
        self.momentum = momentum;
        self
    }

    /// Sets the (L2) weight decay added to the gradient.
    pub fn weight_decay(mut self, weight_decay: f64) -> Self {
        self.weight_decay = weight_decay;
        self
    }

    /// Sets the dampening of the momentum.
    pub fn dampening(mut self, dampening: f64) -> Self {
        self.dampening = dampening;
        self
    }

    /// Enables Nesterov momentum.
    pub fn nesterov(mut self) -> Self {
        self.nesterov = true;
        self
    }
}

impl Optimizer for Sgd {
    fn apply(&mut self, params: &[(&str, &Array)], grads: &[&Array]) -> Result<Vec<Array>> {
        let (momentum, wd, damp, nesterov) = (
            self.momentum,
            self.weight_decay,
            self.dampening,
            self.nesterov,
        );
        let sgd = |p: &Array, g: &Array, s: &mut SgdSlot, lr: &Array, _: &Array| {
            let mut g = g.clone();
            if wd != 0.0 {
                g = g.add(p.multiply(wd));
            }
            let lr = rate(lr, &g);
            if momentum <= 0.0 {
                return p.subtract(lr.multiply(&g));
            }
            let mut next = s.v.multiply(momentum);
            next = if damp > 0.0 {
                next.add(g.multiply(1.0 - damp))
            } else {
                next.add(&g)
            };
            let update = if nesterov {
                g.add(next.multiply(momentum))
            } else {
                next.clone()
            };
            s.v = next;
            p.subtract(lr.multiply(&update))
        };
        step(
            &mut self.state,
            &self.schedule,
            params,
            grads,
            SgdSlot::zeros,
            sgd,
        )
    }

    state_access!(SgdSlot);
}

/// Adam's (and AdamW's) state: the first and second moment estimates.
#[derive(Debug, Clone, Tree)]
pub struct AdamSlot {
    /// The first moment.
    pub m: Array,
    /// The second moment.
    pub v: Array,
}

impl AdamSlot {
    fn zeros(p: &Array) -> Self {
        Self {
            m: zeros(p),
            v: zeros(p),
        }
    }
}

/// `1 - b^step` in float32, an array function of the step (MLX's bias correction, with `b`
/// rounded to float32 as MLX does). Computed as `-expm1(step · ln b)` with Kahan's
/// `expm1(x) = (u - 1) · x / log(u)`, `u = exp(x)`, which avoids the cancellation of
/// `1 - exp(x)` for `b` near 1 (0.999).
fn one_minus_power(b: f64, step: &Array) -> Array {
    let ln_b = f64::from(b as f32).ln();
    let x = step.astype(DType::F32).multiply(ln_b);
    let u = x.exp();
    let kahan = u.subtract(1.0).multiply(&x).divide(u.log());
    u.equal(1.0).select(&x, &kahan).negative()
}

/// Adam's hyperparameters.
#[derive(Debug, Clone, Copy)]
struct AdamParams {
    betas: (f64, f64),
    eps: f64,
    bias_correction: bool,
    /// AdamW's decoupled weight decay (0 for Adam).
    decay: f64,
}

/// One Adam step.
fn adam_step(
    p: &Array,
    g: &Array,
    s: &mut AdamSlot,
    lr: &Array,
    step: &Array,
    h: AdamParams,
) -> Array {
    let (b1, b2) = h.betas;
    let lr = rate(lr, g);
    let p = if h.decay != 0.0 {
        // MLX's AdamW: `parameter * (1 - lr * weight_decay)`.
        p.multiply(ops::subtract(1.0, lr.multiply(h.decay)))
    } else {
        p.clone()
    };
    s.m = s.m.multiply(b1).add(g.multiply(1.0 - b1));
    s.v = s.v.multiply(b2).add(g.multiply(g).multiply(1.0 - b2));
    if h.bias_correction {
        // In float32, as MLX (`1 - b ** step` on a float32 array).
        let c1 = lr.divide(one_minus_power(b1, step)).astype(g.dtype());
        let c2 =
            ops::divide(Array::from(1.0f32), one_minus_power(b2, step).sqrt()).astype(g.dtype());
        let denominator = s.v.sqrt().multiply(&c2).add(h.eps);
        p.subtract(c1.multiply(&s.m).divide(&denominator))
    } else {
        p.subtract(lr.multiply(&s.m).divide(s.v.sqrt().add(h.eps)))
    }
}

/// Adam, as MLX's `optimizers.Adam` (no weight decay; bias correction off unless
/// enabled, as in MLX). [`AdamW`] adds decoupled weight decay.
#[derive(Debug, Clone, Tree)]
pub struct Adam {
    #[tree(skip, static_unchecked)]
    schedule: LearningRate,
    #[tree(skip)]
    betas: (f64, f64),
    #[tree(skip)]
    eps: f64,
    #[tree(skip)]
    bias_correction: bool,
    #[tree(rename = "")]
    state: OptimizerState<AdamSlot>,
}

impl Adam {
    /// Adam with betas `(0.9, 0.999)` and `eps = 1e-8`.
    pub fn new(learning_rate: impl Into<LearningRate>) -> Self {
        let schedule = learning_rate.into();
        Self {
            state: fresh(&schedule),
            schedule,
            betas: (0.9, 0.999),
            eps: 1e-8,
            bias_correction: false,
        }
    }

    /// Sets the betas.
    pub fn betas(mut self, b1: f64, b2: f64) -> Self {
        self.betas = (b1, b2);
        self
    }

    /// Sets `eps`, added to the denominator.
    pub fn eps(mut self, eps: f64) -> Self {
        self.eps = eps;
        self
    }

    /// Enables bias correction.
    pub fn bias_correction(mut self) -> Self {
        self.bias_correction = true;
        self
    }
}

impl Optimizer for Adam {
    fn apply(&mut self, params: &[(&str, &Array)], grads: &[&Array]) -> Result<Vec<Array>> {
        let h = AdamParams {
            betas: self.betas,
            eps: self.eps,
            bias_correction: self.bias_correction,
            decay: 0.0,
        };
        let f = |p: &Array, g: &Array, s: &mut AdamSlot, lr: &Array, t: &Array| {
            adam_step(p, g, s, lr, t, h)
        };
        step(
            &mut self.state,
            &self.schedule,
            params,
            grads,
            AdamSlot::zeros,
            f,
        )
    }

    state_access!(AdamSlot);
}

/// AdamW, as MLX's `optimizers.AdamW`: Adam with decoupled weight decay (MLX's default
/// 0.01).
#[derive(Debug, Clone, Tree)]
pub struct AdamW {
    #[tree(skip, static_unchecked)]
    schedule: LearningRate,
    #[tree(skip)]
    betas: (f64, f64),
    #[tree(skip)]
    eps: f64,
    #[tree(skip)]
    bias_correction: bool,
    #[tree(skip)]
    weight_decay: f64,
    #[tree(rename = "")]
    state: OptimizerState<AdamSlot>,
}

impl AdamW {
    /// AdamW with MLX's defaults: betas `(0.9, 0.999)`, `eps = 1e-8`, weight decay 0.01.
    pub fn new(learning_rate: impl Into<LearningRate>) -> Self {
        let schedule = learning_rate.into();
        Self {
            state: fresh(&schedule),
            schedule,
            betas: (0.9, 0.999),
            eps: 1e-8,
            bias_correction: false,
            weight_decay: 0.01,
        }
    }

    /// Sets the betas.
    pub fn betas(mut self, b1: f64, b2: f64) -> Self {
        self.betas = (b1, b2);
        self
    }

    /// Sets `eps`, added to the denominator.
    pub fn eps(mut self, eps: f64) -> Self {
        self.eps = eps;
        self
    }

    /// Sets the decoupled weight decay.
    pub fn weight_decay(mut self, weight_decay: f64) -> Self {
        self.weight_decay = weight_decay;
        self
    }

    /// Enables bias correction.
    pub fn bias_correction(mut self) -> Self {
        self.bias_correction = true;
        self
    }
}

impl Optimizer for AdamW {
    fn apply(&mut self, params: &[(&str, &Array)], grads: &[&Array]) -> Result<Vec<Array>> {
        let h = AdamParams {
            betas: self.betas,
            eps: self.eps,
            bias_correction: self.bias_correction,
            decay: self.weight_decay,
        };
        let f = |p: &Array, g: &Array, s: &mut AdamSlot, lr: &Array, t: &Array| {
            adam_step(p, g, s, lr, t, h)
        };
        step(
            &mut self.state,
            &self.schedule,
            params,
            grads,
            AdamSlot::zeros,
            f,
        )
    }

    state_access!(AdamSlot);
}

/// Lion's (and Muon's) state: one momentum.
#[derive(Debug, Clone, Tree)]
pub struct MomentumSlot {
    /// The momentum.
    pub m: Array,
}

impl MomentumSlot {
    fn zeros(p: &Array) -> Self {
        Self { m: zeros(p) }
    }
}

/// Lion, as MLX's `optimizers.Lion`: the sign of an interpolated momentum, with optional
/// decoupled weight decay.
#[derive(Debug, Clone, Tree)]
pub struct Lion {
    #[tree(skip, static_unchecked)]
    schedule: LearningRate,
    #[tree(skip)]
    betas: (f64, f64),
    #[tree(skip)]
    weight_decay: f64,
    #[tree(rename = "")]
    state: OptimizerState<MomentumSlot>,
}

impl Lion {
    /// Lion with betas `(0.9, 0.99)` and no weight decay.
    pub fn new(learning_rate: impl Into<LearningRate>) -> Self {
        let schedule = learning_rate.into();
        Self {
            state: fresh(&schedule),
            schedule,
            betas: (0.9, 0.99),
            weight_decay: 0.0,
        }
    }

    /// Sets the betas (interpolation, momentum).
    pub fn betas(mut self, b1: f64, b2: f64) -> Self {
        self.betas = (b1, b2);
        self
    }

    /// Sets the decoupled weight decay.
    pub fn weight_decay(mut self, weight_decay: f64) -> Self {
        self.weight_decay = weight_decay;
        self
    }
}

impl Optimizer for Lion {
    fn apply(&mut self, params: &[(&str, &Array)], grads: &[&Array]) -> Result<Vec<Array>> {
        let ((b1, b2), wd) = (self.betas, self.weight_decay);
        let lion = |p: &Array, g: &Array, s: &mut MomentumSlot, lr: &Array, _: &Array| {
            let lr = rate(lr, g);
            let c = s.m.multiply(b1).add(g.multiply(1.0 - b1));
            s.m = s.m.multiply(b2).add(g.multiply(1.0 - b2));
            let p = if wd > 0.0 {
                ops::subtract(1.0, lr.multiply(wd)).multiply(p)
            } else {
                p.clone()
            };
            p.subtract(lr.multiply(c.sign()))
        };
        step(
            &mut self.state,
            &self.schedule,
            params,
            grads,
            MomentumSlot::zeros,
            lion,
        )
    }

    state_access!(MomentumSlot);
}

/// Muon, as MLX's `optimizers.Muon`: momentum (Nesterov by default) whose update is
/// orthogonalised by Newton–Schulz iterations for parameters of two or more dimensions.
#[derive(Debug, Clone, Tree)]
pub struct Muon {
    #[tree(skip, static_unchecked)]
    schedule: LearningRate,
    #[tree(skip)]
    momentum: f64,
    #[tree(skip)]
    weight_decay: f64,
    #[tree(skip)]
    nesterov: bool,
    #[tree(skip)]
    ns_steps: usize,
    #[tree(rename = "")]
    state: OptimizerState<MomentumSlot>,
}

impl Muon {
    /// Muon with MLX's defaults: momentum 0.95, weight decay 0.01, Nesterov, 5 steps.
    pub fn new(learning_rate: impl Into<LearningRate>) -> Self {
        let schedule = learning_rate.into();
        Self {
            state: fresh(&schedule),
            schedule,
            momentum: 0.95,
            weight_decay: 0.01,
            nesterov: true,
            ns_steps: 5,
        }
    }

    /// Sets the momentum.
    pub fn momentum(mut self, momentum: f64) -> Self {
        self.momentum = momentum;
        self
    }

    /// Sets the weight decay added to the gradient.
    pub fn weight_decay(mut self, weight_decay: f64) -> Self {
        self.weight_decay = weight_decay;
        self
    }

    /// Uses plain momentum instead of Nesterov's (MLX's default).
    pub fn without_nesterov(mut self) -> Self {
        self.nesterov = false;
        self
    }

    /// Sets the number of Newton–Schulz iterations.
    pub fn ns_steps(mut self, n: usize) -> Self {
        self.ns_steps = n;
        self
    }
}

impl Optimizer for Muon {
    fn apply(&mut self, params: &[(&str, &Array)], grads: &[&Array]) -> Result<Vec<Array>> {
        let (momentum, wd, nesterov, ns) = (
            self.momentum,
            self.weight_decay,
            self.nesterov,
            self.ns_steps,
        );
        let muon = |p: &Array, g: &Array, s: &mut MomentumSlot, lr: &Array, _: &Array| {
            let mut g = g.clone();
            if wd != 0.0 {
                g = g.add(p.multiply(wd));
            }
            s.m = s.m.multiply(momentum).add(g.multiply(1.0 - momentum));
            let mut update = if nesterov {
                g.multiply(1.0 - momentum).add(s.m.multiply(momentum))
            } else {
                s.m.clone()
            };
            let mut lr = rate(lr, &g);
            if update.ndim() >= 2 {
                let shape = update.shape().to_vec();
                let flat = update.reshape([shape[0], -1]);
                update = orthogonalize(&flat, ns).reshape(&shape[..]);
                let (rows, cols) = (shape[shape.len() - 2], shape[shape.len() - 1]);
                lr = lr.multiply((f64::from(rows) / f64::from(cols)).max(1.0).sqrt());
            }
            p.subtract(lr.multiply(&update))
        };
        step(
            &mut self.state,
            &self.schedule,
            params,
            grads,
            MomentumSlot::zeros,
            muon,
        )
    }

    state_access!(MomentumSlot);
}

/// The nearest semi-orthogonal matrix to `x` (2-D), by MLX's quintic Newton–Schulz
/// iteration.
fn orthogonalize(x: &Array, steps: usize) -> Array {
    let (a, b, c) = (3.4445, -4.775, 2.0315);
    let transpose = x.shape()[0] > x.shape()[1];
    let mut x = if transpose { x.t() } else { x.clone() };
    let norm = x.multiply(&x).sum_keepdims(..).sqrt();
    x = x.divide(norm.add(1e-7));
    for _ in 0..steps {
        let am = x.matmul(&x.t());
        let bm = am.multiply(b).addmm(&am, &am, c as f32, 1.0);
        x = x.multiply(a).addmm(&bm, &x, 1.0, 1.0);
    }
    if transpose { x.t() } else { x }
}

/// Parameter groups, as MLX's `MultiOptimizer`: each group's filter selects parameters by
/// path (the first matching group wins) and its optimizer updates them; the default
/// optimizer takes the rest. Every group steps once per update. Its state is a tree:
/// `groups.0.…`, …, `default.…`.
///
/// ```
/// use nuvec_nn::optim::{Adam, MultiOptimizer, Sgd};
///
/// let opt = MultiOptimizer::new(Sgd::new(1e-2).weight_decay(1e-4))
///     .group(|path| path.contains("lora_a"), Adam::new(1e-3));
/// # let _ = opt;
/// ```
#[derive(Debug, Clone, Tree)]
pub struct MultiOptimizer {
    groups: Vec<Boxed>,
    default: Boxed,
    #[tree(skip, static_unchecked)]
    filters: Vec<Filter>,
}

/// A group's path filter.
#[derive(Clone)]
struct Filter(Arc<dyn Fn(&str) -> bool + Send + Sync>);

impl std::fmt::Debug for Filter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("Filter")
    }
}

impl MultiOptimizer {
    /// Groups with `default` for every parameter no group selects.
    pub fn new(default: impl Optimizer + 'static) -> Self {
        Self {
            groups: Vec::new(),
            default: Box::new(default),
            filters: Vec::new(),
        }
    }

    /// Adds a group: `opt` updates the parameters whose path matches `filter` (and no
    /// earlier group's).
    pub fn group(
        mut self,
        filter: impl Fn(&str) -> bool + Send + Sync + 'static,
        opt: impl Optimizer + 'static,
    ) -> Self {
        self.groups.push(Box::new(opt));
        self.filters.push(Filter(Arc::new(filter)));
        self
    }

    /// The group of `path`: an index into `groups`, or `groups.len()` for the default.
    fn group_of(&self, path: &str) -> usize {
        self.filters
            .iter()
            .position(|f| (f.0)(path))
            .unwrap_or(self.groups.len())
    }
}

impl Optimizer for MultiOptimizer {
    fn apply(&mut self, params: &[(&str, &Array)], grads: &[&Array]) -> Result<Vec<Array>> {
        if params.len() != grads.len() {
            return Err(Error::InvalidArgument(format!(
                "{} gradients for {} parameters",
                grads.len(),
                params.len()
            )));
        }
        let n = self.groups.len() + 1;
        let mut members: Vec<Vec<usize>> = vec![Vec::new(); n];
        for (i, (path, _)) in params.iter().enumerate() {
            members[self.group_of(path)].push(i);
        }
        let mut next = self.clone();
        let mut out: Vec<Option<Array>> = vec![None; params.len()];
        for (k, idx) in members.iter().enumerate() {
            let p: Vec<(&str, &Array)> = idx.iter().map(|&i| params[i]).collect();
            let g: Vec<&Array> = idx.iter().map(|&i| grads[i]).collect();
            let opt = if k < next.groups.len() {
                &mut next.groups[k]
            } else {
                &mut next.default
            };
            let updated = opt.apply(&p, &g)?;
            if updated.len() != idx.len() {
                return Err(Error::InvalidArgument(format!(
                    "MultiOptimizer: a group's optimizer returned {} parameters for {}",
                    updated.len(),
                    idx.len()
                )));
            }
            for (&i, a) in idx.iter().zip(updated) {
                out[i] = Some(a);
            }
        }
        *self = next;
        Ok(out
            .into_iter()
            .map(|a| a.expect("every parameter has a group"))
            .collect())
    }

    fn init_params(&mut self, params: &[(&str, &Array)]) {
        let n = self.groups.len();
        for k in 0..=n {
            let p: Vec<(&str, &Array)> = params
                .iter()
                .copied()
                .filter(|(path, _)| self.group_of(path) == k)
                .collect();
            if k < n {
                self.groups[k].init_params(&p);
            } else {
                self.default.init_params(&p);
            }
        }
    }

    fn visit_state(&self, v: &mut Visit<'_>) {
        v.static_field("optimizer", &std::any::type_name::<Self>());
        Leaves::visit(self, v);
    }

    fn visit_state_mut(&mut self, v: &mut VisitMut<'_>) {
        Tree::visit_mut(self, v);
    }

    fn learning_rate(&self) -> Array {
        self.default.learning_rate()
    }

    fn set_learning_rate(&mut self, lr: f32) {
        for g in &mut self.groups {
            g.set_learning_rate(lr);
        }
        self.default.set_learning_rate(lr);
    }

    fn clone_box(&self) -> Box<dyn Optimizer> {
        Box::new(self.clone())
    }
}

/// `grads` scaled so that their joint L2 norm is at most `max_norm`, and that norm before
/// scaling, as MLX's `optimizers.clip_grad_norm` (float16 norms in float32). A negative
/// `max_norm` gives failed arrays.
#[track_caller]
pub fn clip_grad_norm<T: Tree>(grads: &T, max_norm: f32) -> (T, Array) {
    if max_norm < 0.0 {
        let failed = Array::failure(
            "clip_grad_norm",
            OpErrorKind::InvalidArgument(format!("max_norm should be >= 0, got {max_norm}")),
        );
        let clipped = grads.map(|_, g| failed.astype(g.dtype()).broadcast_to(g.shape()));
        return (clipped, failed);
    }
    let mut squared: Option<Array> = None;
    grads.for_each(|_, g| {
        let g = if g.dtype() == DType::F16 {
            g.astype(DType::F32)
        } else {
            g.clone()
        };
        let s = g.multiply(&g).sum(..);
        squared = Some(match squared.take() {
            Some(acc) => acc.add(&s),
            None => s,
        });
    });
    let total = squared.unwrap_or_else(|| Array::from(0.0f32)).sqrt();
    let normalizer = ops::divide(f64::from(max_norm), total.add(1e-6)).minimum(1.0);
    let clipped = grads.map(|_, g| {
        let scaled = g.multiply(&normalizer);
        if g.dtype().category() == Category::Float {
            scaled.astype(g.dtype())
        } else {
            scaled
        }
    });
    (clipped, total)
}