rlx-cpu 0.2.13

CPU backend for RLX — SIMD kernels, BLAS dispatch, thread pool, arena executor
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
// RLX — versatile ML compiler + runtime.
// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! CPU kernels for the core Riemannian / SPD-manifold ops (`Op::BiMap`,
//! `Op::ReEig`, `Op::LogEig`, `Op::SpdBatchNorm`, `Op::SpdKarcherMean`
//! and their backwards).
//!
//! These reuse the `CpuKernel` scaffold — the same typed-view contract
//! as user custom ops — so the core ops ride the existing
//! `Thunk::CustomOp` execution path (both the closure-compiled and the
//! match-based executors) without bespoke `Thunk` variants. The compute
//! bodies live in [`crate::spd`]; per-op parameters (`eps`/`iters`/`tol`)
//! are baked into the kernel struct at compile time (F64, CPU-first).

use crate::op_registry::{CpuKernel, CpuTensorMut, CpuTensorRef};
use crate::spd;
use rlx_ir::SpdMatFn;

/// Read the `(rows, cols)` of an `[n,n]`-style square matrix input.
fn dim(t: &CpuTensorRef<'_>, i: usize) -> usize {
    t.shape().dim(i).unwrap_static()
}

/// BiMap forward: `Y = W · X · Wᵀ`. Inputs `[W [m,n], X [n,n]]`.
pub struct BiMapKernel;
impl CpuKernel for BiMapKernel {
    fn name(&self) -> &str {
        "core.bimap"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let w = inputs[0].expect_f64("bimap W")?;
        let x = inputs[1].expect_f64("bimap X")?;
        let (m, n) = (dim(&inputs[0], 0), dim(&inputs[0], 1));
        let out = output.expect_f64_mut("bimap Y")?;
        out.copy_from_slice(&spd::bimap(w, x, m, n));
        Ok(())
    }
}

/// ReEig forward: `Y = U max(ε, Σ) Uᵀ`. Input `[X [n,n]]`; output is
/// the packed `[Y (n²), λ (n), U (n²)]` so the backward reuses the
/// eigendecomposition. The builder narrows out `Y`.
pub struct ReEigKernel {
    pub eps: f64,
}
impl CpuKernel for ReEigKernel {
    fn name(&self) -> &str {
        "core.reeig"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let x = inputs[0].expect_f64("reeig X")?;
        let n = dim(&inputs[0], 0);
        let eps = self.eps;
        let out = output.expect_f64_mut("reeig packed")?;
        out.copy_from_slice(&spd::spectral_forward_packed(x, n, |l| l.max(eps)));
        Ok(())
    }
}

/// LogEig forward: `Y = logm(X)`. Packed `[Y, λ, U]` output (see [`ReEigKernel`]).
pub struct LogEigKernel {
    pub eps: f64,
}
impl CpuKernel for LogEigKernel {
    fn name(&self) -> &str {
        "core.logeig"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let x = inputs[0].expect_f64("logeig X")?;
        let n = dim(&inputs[0], 0);
        let eps = self.eps;
        let out = output.expect_f64_mut("logeig packed")?;
        out.copy_from_slice(&spd::spectral_forward_packed(x, n, |l| l.max(eps).ln()));
        Ok(())
    }
}

/// SPD batch-norm transport forward. Inputs `[X [B,n,n], mean [n,n], G [n,n]]`.
pub struct SpdBatchNormKernel {
    pub eps: f64,
}
impl CpuKernel for SpdBatchNormKernel {
    fn name(&self) -> &str {
        "core.spd_batch_norm"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let x = inputs[0].expect_f64("spd_bn X")?;
        let mean = inputs[1].expect_f64("spd_bn mean")?;
        let g = inputs[2].expect_f64("spd_bn G")?;
        let (batch, n) = (dim(&inputs[0], 0), dim(&inputs[0], 1));
        let out = output.expect_f64_mut("spd_bn Y")?;
        out.copy_from_slice(&spd::spd_bn_transport(x, mean, g, batch, n, self.eps));
        Ok(())
    }
}

/// Batch Fréchet/Karcher mean. Input `[X [B,n,n]]` → `[n,n]`.
pub struct SpdKarcherMeanKernel {
    pub iters: usize,
    pub tol: f64,
}
impl CpuKernel for SpdKarcherMeanKernel {
    fn name(&self) -> &str {
        "core.spd_karcher_mean"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let x = inputs[0].expect_f64("karcher X")?;
        let (batch, n) = (dim(&inputs[0], 0), dim(&inputs[0], 1));
        let covs: Vec<Vec<f64>> = (0..batch)
            .map(|bi| x[bi * n * n..(bi + 1) * n * n].to_vec())
            .collect();
        let out = output.expect_f64_mut("karcher mean")?;
        out.copy_from_slice(&spd::karcher_mean(&covs, n, self.iters, self.tol));
        Ok(())
    }
}

/// Weighted batch Fréchet/Karcher mean. Inputs `[X [B,n,n], w [B]]` → `[n,n]`.
pub struct SpdKarcherMeanWeightedKernel {
    pub iters: usize,
    pub tol: f64,
}
impl CpuKernel for SpdKarcherMeanWeightedKernel {
    fn name(&self) -> &str {
        "core.spd_karcher_mean_weighted"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let x = inputs[0].expect_f64("karcher_w X")?;
        let w = inputs[1].expect_f64("karcher_w weights")?;
        let (batch, n) = (dim(&inputs[0], 0), dim(&inputs[0], 1));
        let covs: Vec<Vec<f64>> = (0..batch)
            .map(|bi| x[bi * n * n..(bi + 1) * n * n].to_vec())
            .collect();
        let out = output.expect_f64_mut("karcher_w mean")?;
        out.copy_from_slice(&spd::karcher_mean_weighted(
            &covs, w, n, self.iters, self.tol,
        ));
        Ok(())
    }
}

/// AIRM log map at an arbitrary base. Inputs `[base [n,n], X [n,n]]` → `[n,n]`.
pub struct SpdLogMapKernel;
impl CpuKernel for SpdLogMapKernel {
    fn name(&self) -> &str {
        "core.spd_log_map"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let base = inputs[0].expect_f64("log_map base")?;
        let x = inputs[1].expect_f64("log_map X")?;
        let n = dim(&inputs[0], 0);
        let out = output.expect_f64_mut("log_map V")?;
        out.copy_from_slice(&spd::log_map(base, x, n));
        Ok(())
    }
}

/// AIRM exp map at an arbitrary base. Inputs `[base [n,n], V [n,n]]` → `[n,n]`.
pub struct SpdExpMapKernel;
impl CpuKernel for SpdExpMapKernel {
    fn name(&self) -> &str {
        "core.spd_exp_map"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let base = inputs[0].expect_f64("exp_map base")?;
        let v = inputs[1].expect_f64("exp_map V")?;
        let n = dim(&inputs[0], 0);
        let out = output.expect_f64_mut("exp_map Y")?;
        out.copy_from_slice(&spd::exp_map(base, v, n));
        Ok(())
    }
}

/// AIRM parallel transport. Inputs `[from [n,n], to [n,n], V [n,n]]` → `[n,n]`.
pub struct SpdParallelTransportKernel;
impl CpuKernel for SpdParallelTransportKernel {
    fn name(&self) -> &str {
        "core.spd_parallel_transport"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let from = inputs[0].expect_f64("transport from")?;
        let to = inputs[1].expect_f64("transport to")?;
        let v = inputs[2].expect_f64("transport V")?;
        let n = dim(&inputs[0], 0);
        let out = output.expect_f64_mut("transport out")?;
        out.copy_from_slice(&spd::parallel_transport(from, to, v, n));
        Ok(())
    }
}

/// Batched SPD spectral matrix function. Input `[X [B,n,n]]` → `[B,n,n]`.
pub struct SpdMatrixFnBatchKernel {
    pub kind: SpdMatFn,
}
impl CpuKernel for SpdMatrixFnBatchKernel {
    fn name(&self) -> &str {
        "core.spd_matrix_fn_batch"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let x = inputs[0].expect_f64("spd_matfn X")?;
        let (batch, n) = (dim(&inputs[0], 0), dim(&inputs[0], 1));
        let covs: Vec<Vec<f64>> = (0..batch)
            .map(|bi| x[bi * n * n..(bi + 1) * n * n].to_vec())
            .collect();
        let res = match self.kind {
            SpdMatFn::Logm => spd::logm_batch(&covs, n),
            SpdMatFn::Expm => spd::expm_batch(&covs, n),
            SpdMatFn::Sqrtm => spd::sqrtm_batch(&covs, n),
            SpdMatFn::Invsqrtm => spd::invsqrtm_batch(&covs, n),
        };
        let out = output.expect_f64_mut("spd_matfn Y")?;
        out.copy_from_slice(&res.concat());
        Ok(())
    }
}

/// SpdLogMap backward. Inputs `[base [n,n], x [n,n], dY [n,n]]` →
/// packed `[d_base ∥ d_x]` `[2,n,n]`.
pub struct SpdLogMapBackwardKernel;
impl CpuKernel for SpdLogMapBackwardKernel {
    fn name(&self) -> &str {
        "core.spd_log_map_backward"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let base = inputs[0].expect_f64("log_map_bwd base")?;
        let x = inputs[1].expect_f64("log_map_bwd x")?;
        let dy = inputs[2].expect_f64("log_map_bwd dY")?;
        let n = dim(&inputs[0], 0);
        let (d_base, d_x) = spd::log_map_backward(base, x, dy, n);
        let out = output.expect_f64_mut("log_map_bwd packed")?;
        out[..n * n].copy_from_slice(&d_base);
        out[n * n..].copy_from_slice(&d_x);
        Ok(())
    }
}

/// SpdExpMap backward. Inputs `[base [n,n], v [n,n], dY [n,n]]` →
/// packed `[d_base ∥ d_v]` `[2,n,n]`.
pub struct SpdExpMapBackwardKernel;
impl CpuKernel for SpdExpMapBackwardKernel {
    fn name(&self) -> &str {
        "core.spd_exp_map_backward"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let base = inputs[0].expect_f64("exp_map_bwd base")?;
        let v = inputs[1].expect_f64("exp_map_bwd v")?;
        let dy = inputs[2].expect_f64("exp_map_bwd dY")?;
        let n = dim(&inputs[0], 0);
        let (d_base, d_v) = spd::exp_map_backward(base, v, dy, n);
        let out = output.expect_f64_mut("exp_map_bwd packed")?;
        out[..n * n].copy_from_slice(&d_base);
        out[n * n..].copy_from_slice(&d_v);
        Ok(())
    }
}

/// SpdParallelTransport backward. Inputs `[from, to, v, dY]` (each `[n,n]`) →
/// packed `[d_from ∥ d_to ∥ d_v]` `[3,n,n]`.
pub struct SpdParallelTransportBackwardKernel;
impl CpuKernel for SpdParallelTransportBackwardKernel {
    fn name(&self) -> &str {
        "core.spd_parallel_transport_backward"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let from = inputs[0].expect_f64("transport_bwd from")?;
        let to = inputs[1].expect_f64("transport_bwd to")?;
        let v = inputs[2].expect_f64("transport_bwd v")?;
        let dy = inputs[3].expect_f64("transport_bwd dY")?;
        let n = dim(&inputs[0], 0);
        let (d_from, d_to, d_v) = spd::parallel_transport_backward(from, to, v, dy, n);
        let out = output.expect_f64_mut("transport_bwd packed")?;
        out[..n * n].copy_from_slice(&d_from);
        out[n * n..2 * n * n].copy_from_slice(&d_to);
        out[2 * n * n..].copy_from_slice(&d_v);
        Ok(())
    }
}

/// SpdMatrixFnBatch backward. Inputs `[X [B,n,n], dY [B,n,n]]` → `dX [B,n,n]`.
pub struct SpdMatrixFnBatchBackwardKernel {
    pub kind: SpdMatFn,
}
impl CpuKernel for SpdMatrixFnBatchBackwardKernel {
    fn name(&self) -> &str {
        "core.spd_matrix_fn_batch_backward"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let x = inputs[0].expect_f64("spd_matfn_bwd X")?;
        let dy = inputs[1].expect_f64("spd_matfn_bwd dY")?;
        let n = dim(&inputs[0], 1);
        // Same (f, f') as the batch forward (see spd::{logm,expm,sqrtm,invsqrtm}_batch).
        let dx = match self.kind {
            SpdMatFn::Logm => spd::matrix_fn_batch_backward(
                x,
                dy,
                n,
                |l| l.max(1e-12).ln(),
                |l| 1.0 / l.max(1e-12),
            ),
            SpdMatFn::Expm => spd::matrix_fn_batch_backward(x, dy, n, |l| l.exp(), |l| l.exp()),
            SpdMatFn::Sqrtm => spd::matrix_fn_batch_backward(
                x,
                dy,
                n,
                |l| l.max(0.0).sqrt(),
                |l| 0.5 / l.max(1e-12).sqrt(),
            ),
            SpdMatFn::Invsqrtm => spd::matrix_fn_batch_backward(
                x,
                dy,
                n,
                |l| 1.0 / l.max(1e-12).sqrt(),
                |l| -0.5 * l.max(1e-12).powf(-1.5),
            ),
        };
        let out = output.expect_f64_mut("spd_matfn_bwd dX")?;
        out.copy_from_slice(&dx);
        Ok(())
    }
}

/// `n` from a packed `[λ ∥ U]` length `L = n²+n` (⇒ `n = (√(1+4L)−1)/2`).
fn n_from_packed(len: usize) -> usize {
    (((1 + 4 * len) as f64).sqrt().round() as usize - 1) / 2
}

/// Symmetric eigendecomposition. Input `[A n,n]` → packed `[λ ∥ U]` `[n²+n]`.
pub struct EighKernel;
impl CpuKernel for EighKernel {
    fn name(&self) -> &str {
        "core.eigh"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let a = inputs[0].expect_f64("eigh A")?;
        let n = dim(&inputs[0], 0);
        let out = output.expect_f64_mut("eigh packed")?;
        out.copy_from_slice(&spd::eigh_packed(a, n));
        Ok(())
    }
}

/// Eigh backward. Inputs `[fwd [n²+n], bar [n²+n]]` → `Ā [n,n]`.
pub struct EighBackwardKernel;
impl CpuKernel for EighBackwardKernel {
    fn name(&self) -> &str {
        "core.eigh_backward"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let fwd = inputs[0].expect_f64("eigh_bwd fwd")?;
        let bar = inputs[1].expect_f64("eigh_bwd bar")?;
        let n = n_from_packed(fwd.len());
        let out = output.expect_f64_mut("eigh_bwd dA")?;
        out.copy_from_slice(&spd::eigh_backward_packed(fwd, bar, n));
        Ok(())
    }
}

/// Batched eigh. Input `[A B,n,n]` → packed `[B, n²+n]`.
pub struct EighBatchKernel;
impl CpuKernel for EighBatchKernel {
    fn name(&self) -> &str {
        "core.eigh_batch"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let a = inputs[0].expect_f64("eigh_batch A")?;
        let (b, n) = (dim(&inputs[0], 0), dim(&inputs[0], 1));
        let covs: Vec<Vec<f64>> = (0..b)
            .map(|bi| a[bi * n * n..(bi + 1) * n * n].to_vec())
            .collect();
        let out = output.expect_f64_mut("eigh_batch packed")?;
        out.copy_from_slice(&spd::eigh_batch_packed(&covs, n).concat());
        Ok(())
    }
}

/// Batched eigh backward. Inputs `[fwd [B,n²+n], bar [B,n²+n]]` → `Ā [B,n,n]`.
pub struct EighBatchBackwardKernel;
impl CpuKernel for EighBatchBackwardKernel {
    fn name(&self) -> &str {
        "core.eigh_batch_backward"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        use rayon::prelude::*;
        let fwd = inputs[0].expect_f64("eigh_batch_bwd fwd")?;
        let bar = inputs[1].expect_f64("eigh_batch_bwd bar")?;
        let (b, packed) = (dim(&inputs[0], 0), dim(&inputs[0], 1));
        let n = n_from_packed(packed);
        let da: Vec<f64> = (0..b)
            .into_par_iter()
            .flat_map(|bi| {
                let f = &fwd[bi * packed..(bi + 1) * packed];
                let bb = &bar[bi * packed..(bi + 1) * packed];
                spd::eigh_backward_packed(f, bb, n)
            })
            .collect();
        let out = output.expect_f64_mut("eigh_batch_bwd dA")?;
        out.copy_from_slice(&da);
        Ok(())
    }
}

/// ReEig backward. Inputs `[λ [n], U [n²], dY [n²]]` → `dX [n,n]`,
/// reusing the forward eigendecomposition (no recompute).
pub struct ReEigBackwardKernel {
    pub eps: f64,
}
impl CpuKernel for ReEigBackwardKernel {
    fn name(&self) -> &str {
        "core.reeig_backward"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let lam = inputs[0].expect_f64("reeig_bwd λ")?;
        let u = inputs[1].expect_f64("reeig_bwd U")?;
        let dy = inputs[2].expect_f64("reeig_bwd dY")?;
        let n = lam.len();
        let out = output.expect_f64_mut("reeig_bwd dX")?;
        out.copy_from_slice(&spd::reeig_backward_precomputed(lam, u, dy, n, self.eps));
        Ok(())
    }
}

/// LogEig backward. Inputs `[λ [n], U [n²], dY [n²]]` → `dX [n,n]`.
pub struct LogEigBackwardKernel {
    pub eps: f64,
}
impl CpuKernel for LogEigBackwardKernel {
    fn name(&self) -> &str {
        "core.logeig_backward"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let lam = inputs[0].expect_f64("logeig_bwd λ")?;
        let u = inputs[1].expect_f64("logeig_bwd U")?;
        let dy = inputs[2].expect_f64("logeig_bwd dY")?;
        let n = lam.len();
        let out = output.expect_f64_mut("logeig_bwd dX")?;
        out.copy_from_slice(&spd::logeig_backward_precomputed(lam, u, dy, n, self.eps));
        Ok(())
    }
}

/// SPD batch-norm backward w.r.t. X. Inputs `[mean [n,n], G [n,n], dY [B,n,n]]`.
pub struct SpdBnBackwardXKernel {
    pub eps: f64,
}
impl CpuKernel for SpdBnBackwardXKernel {
    fn name(&self) -> &str {
        "core.spd_batch_norm_backward_x"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let mean = inputs[0].expect_f64("spd_bn_bwd_x mean")?;
        let g = inputs[1].expect_f64("spd_bn_bwd_x G")?;
        let dy = inputs[2].expect_f64("spd_bn_bwd_x dY")?;
        let (batch, n) = (dim(&inputs[2], 0), dim(&inputs[0], 0));
        let out = output.expect_f64_mut("spd_bn_bwd_x dX")?;
        out.copy_from_slice(&spd::spd_bn_backward_x(mean, g, dy, batch, n, self.eps));
        Ok(())
    }
}

/// SPD batch-norm backward w.r.t. G. Inputs `[X [B,n,n], mean [n,n], G [n,n], dY [B,n,n]]`.
pub struct SpdBnBackwardGKernel {
    pub eps: f64,
}
impl CpuKernel for SpdBnBackwardGKernel {
    fn name(&self) -> &str {
        "core.spd_batch_norm_backward_g"
    }
    fn execute(
        &self,
        inputs: &[CpuTensorRef<'_>],
        output: CpuTensorMut<'_>,
        _attrs: &[u8],
    ) -> Result<(), String> {
        let x = inputs[0].expect_f64("spd_bn_bwd_g X")?;
        let mean = inputs[1].expect_f64("spd_bn_bwd_g mean")?;
        let g = inputs[2].expect_f64("spd_bn_bwd_g G")?;
        let dy = inputs[3].expect_f64("spd_bn_bwd_g dY")?;
        let (batch, n) = (dim(&inputs[0], 0), dim(&inputs[0], 1));
        let out = output.expect_f64_mut("spd_bn_bwd_g dG")?;
        out.copy_from_slice(&spd::spd_bn_backward_g(x, mean, g, dy, batch, n, self.eps));
        Ok(())
    }
}