tenferro-linalg 0.3.0

Linear algebra traced APIs, eager helpers, extension runtime, and optional AD rules for tenferro.
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
use tenferro_tensor::{BackendSession, Tensor, TensorRead, TensorWrite};

pub(crate) use crate::error::unsupported_dtype;
use crate::extension::{
    apply_eigh_gauge, apply_qr_gauge, apply_svd_gauge, validate_derivative_eps, EighOptions,
    QrOptions, SvdOptions,
};

/// Backend surface required by the linalg extension runtime.
///
/// # Examples
///
/// ```rust
/// use tenferro_cpu::{with_cpu_exec_session, CpuBackend, CpuExecSession};
/// use tenferro_linalg::backend::LinalgBackend;
/// use tenferro_tensor::BackendSessionHost;
///
/// fn assert_linalg_backend<B: LinalgBackend>() {}
///
/// assert_linalg_backend::<CpuExecSession<'static>>();
/// let mut host = CpuBackend::new();
/// host.with_backend_session(|session| {
///     with_cpu_exec_session(session, |_backend| ())
///         .expect("CpuBackend must expose a CpuExecSession");
/// });
/// ```
pub trait LinalgBackend: BackendSession {
    /// Compute a Cholesky factorization.
    ///
    /// # Errors
    ///
    /// Returns `Error::Validation` for non-matrix, non-square, or unsupported
    /// input dtypes; `Error::Extension` with `ErrorKind::NumericalFailure`
    /// when the matrix is not positive definite; or a typed backend source
    /// when the provider cannot execute the factorization.
    fn cholesky(&mut self, input: &Tensor) -> tenferro_tensor::Result<Tensor>;

    /// Solve a triangular linear system with explicit side, triangle,
    /// transpose, and unit-diagonal flags.
    ///
    /// # Errors
    ///
    /// Returns `Error::Validation` for incompatible matrix/rhs shapes, rank,
    /// or dtype; `Error::Extension` with `ErrorKind::NumericalFailure` for a
    /// singular or zero-diagonal system; or a typed backend source for a
    /// provider failure.
    fn triangular_solve(
        &mut self,
        a: &Tensor,
        b: &Tensor,
        left_side: bool,
        lower: bool,
        transpose_a: bool,
        unit_diagonal: bool,
    ) -> tenferro_tensor::Result<Tensor>;

    // INVARIANT: the six flags are the public triangular-solve contract and mirror the owned hook.
    #[allow(clippy::too_many_arguments)]
    /// Solve a triangular linear system from tensor read targets.
    ///
    /// Backends may canonicalize the inputs inside the same placement family,
    /// but must not silently transfer between CPU and GPU memory.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
    /// use tenferro_linalg::LinalgBackend;
    /// use tenferro_tensor::{BackendSessionHost, Tensor, TensorRead};
    ///
    /// let a = Tensor::from_vec_col_major(vec![2, 2], vec![2.0_f64, 0.0, 1.0, 3.0])?;
    /// let b = Tensor::from_vec_col_major(vec![2, 1], vec![4.0_f64, 9.0])?;
    /// let mut host = CpuBackend::new();
    /// let x = host.with_backend_session(|session| {
    ///     with_cpu_exec_session(session, |backend| {
    ///         backend.triangular_solve_read(
    ///             TensorRead::from_tensor(&a),
    ///             TensorRead::from_tensor(&b),
    ///             true,
    ///             false,
    ///             false,
    ///             false,
    ///         )
    ///     })
    ///     .expect("CpuBackend must expose a CpuExecSession")
    /// })?;
    /// let Tensor::F64(x) = x else { unreachable!("F64 inputs return F64 output") };
    /// assert_eq!(x.host_data()?, &[0.5, 3.0]);
    /// # Ok::<(), tenferro_tensor::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// The default implementation returns `Error::Unsupported` because the
    /// backend does not accept tensor read targets. Implementations may return
    /// `Error::Validation` for incompatible shapes or dtypes,
    /// `Error::RuntimeState` for invalid placement, `Error::Extension` for a
    /// singular system, or a typed backend-source error.
    fn triangular_solve_read(
        &mut self,
        _a: TensorRead<'_>,
        _b: TensorRead<'_>,
        _left_side: bool,
        _lower: bool,
        _transpose_a: bool,
        _unit_diagonal: bool,
    ) -> tenferro_tensor::Result<Tensor> {
        Err(tenferro_tensor::Error::unsupported(
            "triangular_solve",
            "backend does not accept tensor reads at this execution boundary",
        ))
    }

    /// Compute public LU outputs `(P, L, U, parity)`.
    ///
    /// # Errors
    ///
    /// Returns `Error::Validation` when the input is not a supported matrix or
    /// dtype, and `Error::Extension` or a typed backend source when LU
    /// execution or pivot storage fails.
    fn lu(&mut self, input: &Tensor) -> tenferro_tensor::Result<Vec<Tensor>>;

    #[doc(hidden)]
    fn lu_factor(&mut self, _input: &Tensor) -> tenferro_tensor::Result<Vec<Tensor>> {
        Err(tenferro_tensor::Error::unsupported(
            "lu_factor",
            format!(
                "backend {} does not implement internal packed LU factorization",
                std::any::type_name::<Self>()
            ),
        ))
    }

    /// Compute complete-pivot LU outputs `(P, L, U, Q, parity)`.
    ///
    /// The reconstruction convention is `A = P^T * L * U * Q`, equivalently
    /// `P * A * Q^T = L * U`. `parity` is a scalar real tensor containing
    /// `+1` or `-1`: `F32` for `F32`/`C32` inputs and `F64` for `F64`/`C64`
    /// inputs.
    ///
    /// # Errors
    ///
    /// Returns `Error::Validation` for an invalid rank, square-shape
    /// requirement, or dtype, and `Error::Extension` or a typed backend source
    /// when complete-pivot factorization cannot be executed.
    fn full_piv_lu(&mut self, input: &Tensor) -> tenferro_tensor::Result<Vec<Tensor>>;

    /// Solve a linear system through the complete-pivot LU path.
    ///
    /// With `transpose_a = false`, this solves `A * x = b`. With
    /// `transpose_a = true`, this solves `A^T * x = b`.
    ///
    /// # Errors
    ///
    /// Returns `Error::Validation` for incompatible coefficient/rhs shapes or
    /// dtypes, `Error::Extension` with `ErrorKind::NumericalFailure` for a
    /// singular system, or a typed backend source for provider failure.
    fn full_piv_lu_solve(
        &mut self,
        a: &Tensor,
        b: &Tensor,
        transpose_a: bool,
    ) -> tenferro_tensor::Result<Tensor>;

    /// Compute public SVD outputs `(U, S, Vt)`.
    ///
    /// # Errors
    ///
    /// Returns `Error::Validation` for an unsupported rank or dtype and a
    /// typed `Error::Extension` or backend source when the solver fails.
    fn svd(&mut self, input: &Tensor) -> tenferro_tensor::Result<Vec<Tensor>>;

    /// Compute public SVD outputs `(U, S, Vt)` with explicit options.
    ///
    /// `derivative_eps` is validated for API consistency, but concrete backend
    /// execution does not perform AD. `gauge` controls optional singular-vector
    /// post-processing.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
    /// use tenferro_linalg::{LinalgBackend, SvdGauge, SvdOptions};
    /// use tenferro_tensor::{BackendSessionHost, Tensor};
    ///
    /// let input = Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 0.0, 0.0, 2.0])?;
    /// let mut host = CpuBackend::new();
    /// let outputs = host.with_backend_session(|session| {
    ///     with_cpu_exec_session(session, |backend| {
    ///         backend.svd_with_options(
    ///             &input,
    ///             SvdOptions::default().gauge(SvdGauge::CanonicalPivot),
    ///         )
    ///     })
    ///     .expect("CpuBackend must expose a CpuExecSession")
    /// })?;
    /// assert_eq!(outputs[1].shape(), &[2]);
    /// # Ok::<(), tenferro_tensor::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`tenferro_tensor::Error::Validation`] containing
    /// [`tenferro_tensor::ValidationError::InvalidArgument`] when
    /// `derivative_eps` is non-finite or non-positive, or when canonical gauge
    /// output metadata is malformed. It can return
    /// [`tenferro_tensor::Error::Validation`] with
    /// [`tenferro_tensor::ValidationError::RankMismatch`],
    /// [`tenferro_tensor::ValidationError::ShapeMismatch`], or
    /// [`tenferro_tensor::ValidationError::DTypeMismatch`] for the input
    /// or generated outputs, [`tenferro_tensor::Error::Extension`] with the
    /// typed `tenferro_linalg::Error::UnsupportedDType` or
    /// `NonConvergence` source, [`tenferro_tensor::Error::BackendSource`] for
    /// provider calls, and [`tenferro_tensor::Error::RuntimeState`] for
    /// placement failures. A CPU provider that was not compiled is reported
    /// as [`tenferro_tensor::ValidationError::InvalidArgument`] on the
    /// provider configuration.
    fn svd_with_options(
        &mut self,
        input: &Tensor,
        options: SvdOptions,
    ) -> tenferro_tensor::Result<Vec<Tensor>> {
        validate_derivative_eps("svd_with_options", options.derivative_eps)?;
        let mut outputs = self.svd(input)?;
        apply_svd_gauge(options.gauge, &mut outputs)?;
        Ok(outputs)
    }

    /// Compute public full-matrices SVD outputs `(U, S, Vt)` with `U` shaped
    /// `m x m` and `Vt` shaped `n x n`, so the trailing `Vt` rows span the
    /// input's right nullspace.
    ///
    /// # Errors
    ///
    /// The default implementation returns `Error::Unsupported`: a backend that
    /// does not implement the full variant reports it explicitly rather than
    /// silently falling back to the thin decomposition. Implementing backends
    /// may additionally return `Error::Validation` for an unsupported rank or
    /// dtype and a typed backend source when the solver fails.
    fn svd_full(&mut self, _input: &Tensor) -> tenferro_tensor::Result<Vec<Tensor>> {
        Err(tenferro_tensor::Error::unsupported(
            "svd_full",
            format!(
                "backend {} does not implement full-matrices SVD",
                std::any::type_name::<Self>()
            ),
        ))
    }

    #[doc(hidden)]
    fn svd_values(&mut self, _input: &Tensor) -> tenferro_tensor::Result<Tensor> {
        Err(tenferro_tensor::Error::unsupported(
            "svd_values",
            format!(
                "backend {} does not implement internal singular-values-only decomposition",
                std::any::type_name::<Self>()
            ),
        ))
    }

    /// Compute a singular value decomposition from a tensor read target.
    ///
    /// Backends may canonicalize the input inside the same placement family, but
    /// must not silently transfer between CPU and GPU memory.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tenferro_linalg::LinalgBackend;
    /// use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
    /// use tenferro_tensor::{BackendSessionHost, TensorRead, TensorView, TypedTensor};
    ///
    /// let input = TypedTensor::<f64>::from_vec_col_major(
    ///     vec![2, 2],
    ///     vec![1.0, 0.0, 0.0, 2.0],
    /// )?;
    /// let mut host = CpuBackend::new();
    /// let outputs = host.with_backend_session(|session| {
    ///     with_cpu_exec_session(session, |backend| {
    ///         backend.svd_read(TensorRead::from_view(TensorView::F64(input.as_view())))
    ///     })
    ///     .expect("CpuBackend must expose a CpuExecSession")
    /// })?;
    /// assert_eq!(outputs[1].shape(), &[2]);
    /// # Ok::<(), tenferro_tensor::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// The default implementation returns `Error::Unsupported` because the
    /// backend does not accept tensor read targets; an implementation may instead
    /// return validation or typed backend-source errors after canonicalizing
    /// the view.
    fn svd_read(&mut self, _input: TensorRead<'_>) -> tenferro_tensor::Result<Vec<Tensor>> {
        Err(tenferro_tensor::Error::unsupported(
            "svd",
            "backend does not accept tensor reads at this execution boundary",
        ))
    }

    /// Compute public QR outputs `(Q, R)`.
    ///
    /// QR is thin: for an `m x n` input, `Q` has shape `m x min(m, n)` and
    /// `R` has shape `min(m, n) x n`.
    ///
    /// # Errors
    ///
    /// Returns `Error::Validation` for an unsupported rank, shape, or dtype,
    /// and a typed `Error::Extension` or backend source when QR execution
    /// fails.
    fn qr(&mut self, input: &Tensor) -> tenferro_tensor::Result<Vec<Tensor>>;

    /// Compute public QR outputs `(Q, R)` with explicit options.
    ///
    /// `gauge` controls optional sign or phase post-processing.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
    /// use tenferro_linalg::{LinalgBackend, QrGauge, QrOptions};
    /// use tenferro_tensor::{BackendSessionHost, Tensor};
    ///
    /// let input = Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 0.0, 0.0, 2.0])?;
    /// let mut host = CpuBackend::new();
    /// let outputs = host.with_backend_session(|session| {
    ///     with_cpu_exec_session(session, |backend| {
    ///         backend.qr_with_options(
    ///             &input,
    ///             QrOptions::default().gauge(QrGauge::PositiveDiagonal),
    ///         )
    ///     })
    ///     .expect("CpuBackend must expose a CpuExecSession")
    /// })?;
    /// assert_eq!(outputs[0].shape(), &[2, 2]);
    /// # Ok::<(), tenferro_tensor::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`tenferro_tensor::Error::Validation`] containing
    /// [`tenferro_tensor::ValidationError::RankMismatch`] or
    /// [`tenferro_tensor::ValidationError::ShapeMismatch`] for an invalid
    /// matrix input, or [`tenferro_tensor::ValidationError::InvalidArgument`]
    /// for malformed gauge output metadata, checked size arithmetic, or an
    /// unavailable compiled provider. A mismatched generated `Q`/`R` dtype is reported as
    /// [`tenferro_tensor::ValidationError::DTypeMismatch`]. Provider
    /// unsupported dtype or numerical rejection is
    /// [`tenferro_tensor::Error::Extension`] with a typed linalg source, while
    /// provider failures use [`tenferro_tensor::Error::BackendSource`] and a
    /// backend-resident input uses [`tenferro_tensor::Error::RuntimeState`].
    fn qr_with_options(
        &mut self,
        input: &Tensor,
        options: QrOptions,
    ) -> tenferro_tensor::Result<Vec<Tensor>> {
        let mut outputs = self.qr(input)?;
        apply_qr_gauge(options.gauge, &mut outputs)?;
        Ok(outputs)
    }

    /// Compute public QR outputs `(Q, R)` from a tensor read target.
    ///
    /// Backends may canonicalize the input inside the same placement family, but
    /// must not silently transfer between CPU and GPU memory.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tenferro_linalg::LinalgBackend;
    /// use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
    /// use tenferro_tensor::{BackendSessionHost, TensorRead, TensorView, TypedTensor};
    ///
    /// let input = TypedTensor::<f64>::from_vec_col_major(
    ///     vec![2, 2],
    ///     vec![1.0, 0.0, 0.0, 2.0],
    /// )?;
    /// let mut host = CpuBackend::new();
    /// let outputs = host.with_backend_session(|session| {
    ///     with_cpu_exec_session(session, |backend| {
    ///         backend.qr_read(TensorRead::from_view(TensorView::F64(input.as_view())))
    ///     })
    ///     .expect("CpuBackend must expose a CpuExecSession")
    /// })?;
    /// assert_eq!(outputs[0].shape(), &[2, 2]);
    /// assert_eq!(outputs[1].shape(), &[2, 2]);
    /// # Ok::<(), tenferro_tensor::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// The default implementation returns `Error::Unsupported` because the
    /// backend does not accept tensor read targets; implementations may return
    /// validation or typed backend-source errors.
    fn qr_read(&mut self, _input: TensorRead<'_>) -> tenferro_tensor::Result<Vec<Tensor>> {
        Err(tenferro_tensor::Error::unsupported(
            "qr",
            "backend does not accept tensor reads at this execution boundary",
        ))
    }

    /// Compute public Hermitian eigendecomposition outputs `(values, vectors)`.
    ///
    /// The returned vector order is `[values, vectors]`, where `values` has
    /// shape `[n]` and `vectors` has shape `[n, n]`.
    ///
    /// # Errors
    ///
    /// Returns `Error::Validation` for a non-square or unsupported-dtype input
    /// and a typed `Error::Extension` or backend source when eigendecomposition
    /// fails.
    fn eigh(&mut self, input: &Tensor) -> tenferro_tensor::Result<Vec<Tensor>>;

    /// Compute public Hermitian eigendecomposition outputs with explicit options.
    ///
    /// `derivative_eps` is validated for API consistency, but concrete backend
    /// execution does not perform AD. `gauge` controls optional eigenvector
    /// post-processing.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
    /// use tenferro_linalg::{EighGauge, EighOptions, LinalgBackend};
    /// use tenferro_tensor::{BackendSessionHost, Tensor};
    ///
    /// let input = Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 0.0, 0.0, 2.0])?;
    /// let mut host = CpuBackend::new();
    /// let outputs = host.with_backend_session(|session| {
    ///     with_cpu_exec_session(session, |backend| {
    ///         backend.eigh_with_options(
    ///             &input,
    ///             EighOptions::default()
    ///                 .gauge(EighGauge::CanonicalPivot)
    ///                 .derivative_eps(1.0e-10),
    ///         )
    ///     })
    ///     .expect("CpuBackend must expose a CpuExecSession")
    /// })?;
    /// assert_eq!(outputs[0].shape(), &[2]);
    /// # Ok::<(), tenferro_tensor::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`tenferro_tensor::Error::Validation`] containing
    /// [`tenferro_tensor::ValidationError::InvalidArgument`] when
    /// `derivative_eps` is non-finite or non-positive, when canonical gauge
    /// output metadata is malformed, or when checked output-size arithmetic
    /// overflows. It can return [`tenferro_tensor::Error::Validation`] with
    /// [`tenferro_tensor::ValidationError::RankMismatch`] or
    /// [`tenferro_tensor::ValidationError::ShapeMismatch`] for the
    /// matrix input, or [`tenferro_tensor::ValidationError::DTypeMismatch`]
    /// for generated outputs. It can also return
    /// [`tenferro_tensor::Error::Extension`] with typed
    /// `tenferro_linalg::Error::UnsupportedDType` or `NonConvergence`, and
    /// [`tenferro_tensor::Error::BackendSource`] or
    /// [`tenferro_tensor::Error::RuntimeState`] for provider and placement
    /// failures.
    fn eigh_with_options(
        &mut self,
        input: &Tensor,
        options: EighOptions,
    ) -> tenferro_tensor::Result<Vec<Tensor>> {
        validate_derivative_eps("eigh_with_options", options.derivative_eps)?;
        let mut outputs = self.eigh(input)?;
        apply_eigh_gauge(options.gauge, &mut outputs)?;
        Ok(outputs)
    }

    /// Compute public Hermitian eigendecomposition outputs from a tensor read target.
    ///
    /// Backends may canonicalize the input inside the same placement family, but
    /// must not silently transfer between CPU and GPU memory.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tenferro_linalg::LinalgBackend;
    /// use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
    /// use tenferro_tensor::{BackendSessionHost, TensorRead, TensorView, TypedTensor};
    ///
    /// let input = TypedTensor::<f64>::from_vec_col_major(
    ///     vec![2, 2],
    ///     vec![1.0, 0.0, 0.0, 2.0],
    /// )?;
    /// let mut host = CpuBackend::new();
    /// let outputs = host.with_backend_session(|session| {
    ///     with_cpu_exec_session(session, |backend| {
    ///         backend.eigh_read(TensorRead::from_view(TensorView::F64(input.as_view())))
    ///     })
    ///     .expect("CpuBackend must expose a CpuExecSession")
    /// })?;
    /// assert_eq!(outputs[0].shape(), &[2]);
    /// assert_eq!(outputs[1].shape(), &[2, 2]);
    /// # Ok::<(), tenferro_tensor::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// The default implementation returns `Error::Unsupported` because the
    /// backend does not accept tensor read targets; implementations may return
    /// validation or typed backend-source errors.
    fn eigh_read(&mut self, _input: TensorRead<'_>) -> tenferro_tensor::Result<Vec<Tensor>> {
        Err(tenferro_tensor::Error::unsupported(
            "eigh",
            "backend does not accept tensor reads at this execution boundary",
        ))
    }

    /// Compute Cholesky factorization from a tensor read target.
    ///
    /// Backends may canonicalize the input inside the same placement family, but
    /// must not silently transfer between CPU and GPU memory.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tenferro_linalg::LinalgBackend;
    /// use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
    /// use tenferro_tensor::{BackendSessionHost, TensorRead, TensorView, TypedTensor};
    ///
    /// let input = TypedTensor::<f64>::from_vec_col_major(
    ///     vec![2, 2],
    ///     vec![4.0, 2.0, 2.0, 3.0],
    /// )?;
    /// let mut host = CpuBackend::new();
    /// let output = host.with_backend_session(|session| {
    ///     with_cpu_exec_session(session, |backend| {
    ///         backend.cholesky_read(TensorRead::from_view(TensorView::F64(input.as_view())))
    ///     })
    ///     .expect("CpuBackend must expose a CpuExecSession")
    /// })?;
    /// assert_eq!(output.shape(), &[2, 2]);
    /// # Ok::<(), tenferro_tensor::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// The default implementation returns `Error::Unsupported` because the
    /// backend does not accept tensor read targets; implementations may return
    /// validation or typed backend-source errors.
    fn cholesky_read(&mut self, _input: TensorRead<'_>) -> tenferro_tensor::Result<Tensor> {
        Err(tenferro_tensor::Error::unsupported(
            "cholesky",
            "backend does not accept tensor reads at this execution boundary",
        ))
    }

    /// Compute public LU outputs from a tensor read target.
    ///
    /// Backends may canonicalize the input inside the same placement family, but
    /// must not silently transfer between CPU and GPU memory.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tenferro_linalg::LinalgBackend;
    /// use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
    /// use tenferro_tensor::{BackendSessionHost, TensorRead, TensorView, TypedTensor};
    ///
    /// let input = TypedTensor::<f64>::from_vec_col_major(
    ///     vec![2, 2],
    ///     vec![1.0, 3.0, 2.0, 4.0],
    /// )?;
    /// let mut host = CpuBackend::new();
    /// let outputs = host.with_backend_session(|session| {
    ///     with_cpu_exec_session(session, |backend| {
    ///         backend.lu_read(TensorRead::from_view(TensorView::F64(input.as_view())))
    ///     })
    ///     .expect("CpuBackend must expose a CpuExecSession")
    /// })?;
    /// assert_eq!(outputs.len(), 4);
    /// # Ok::<(), tenferro_tensor::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// The default implementation returns `Error::Unsupported` because the
    /// backend does not accept tensor read targets; implementations may return
    /// validation or typed backend-source errors.
    fn lu_read(&mut self, _input: TensorRead<'_>) -> tenferro_tensor::Result<Vec<Tensor>> {
        Err(tenferro_tensor::Error::unsupported(
            "lu",
            "backend does not accept tensor reads at this execution boundary",
        ))
    }

    /// Compute public full-pivoting LU outputs from a tensor read target.
    ///
    /// Backends may canonicalize the input inside the same placement family, but
    /// must not silently transfer between CPU and GPU memory.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tenferro_linalg::LinalgBackend;
    /// use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
    /// use tenferro_tensor::{BackendSessionHost, TensorRead, TensorView, TypedTensor};
    ///
    /// let input = TypedTensor::<f64>::from_vec_col_major(
    ///     vec![2, 2],
    ///     vec![1.0, 3.0, 2.0, 4.0],
    /// )?;
    /// let mut host = CpuBackend::new();
    /// let outputs = host.with_backend_session(|session| {
    ///     with_cpu_exec_session(session, |backend| {
    ///         backend.full_piv_lu_read(TensorRead::from_view(TensorView::F64(input.as_view())))
    ///     })
    ///     .expect("CpuBackend must expose a CpuExecSession")
    /// })?;
    /// assert_eq!(outputs.len(), 5);
    /// # Ok::<(), tenferro_tensor::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// The default implementation returns `Error::Unsupported` because the
    /// backend does not accept tensor read targets; implementations may return
    /// validation or typed backend-source errors.
    fn full_piv_lu_read(&mut self, _input: TensorRead<'_>) -> tenferro_tensor::Result<Vec<Tensor>> {
        Err(tenferro_tensor::Error::unsupported(
            "full_piv_lu",
            "backend does not accept tensor reads at this execution boundary",
        ))
    }

    /// Compute general eigendecomposition outputs from a tensor read target.
    ///
    /// Backends may canonicalize the input inside the same placement family, but
    /// must not silently transfer between CPU and GPU memory.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tenferro_linalg::LinalgBackend;
    /// use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
    /// use tenferro_tensor::{BackendSessionHost, TensorRead, TensorView, TypedTensor};
    ///
    /// let input = TypedTensor::<f64>::from_vec_col_major(
    ///     vec![2, 2],
    ///     vec![2.0, 0.0, 0.0, 3.0],
    /// )?;
    /// let mut host = CpuBackend::new();
    /// let outputs = host.with_backend_session(|session| {
    ///     with_cpu_exec_session(session, |backend| {
    ///         backend.eig_read(TensorRead::from_view(TensorView::F64(input.as_view())))
    ///     })
    ///     .expect("CpuBackend must expose a CpuExecSession")
    /// })?;
    /// assert_eq!(outputs.len(), 2);
    /// # Ok::<(), tenferro_tensor::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// The default implementation returns `Error::Unsupported` because the
    /// backend does not accept tensor read targets; implementations may return
    /// validation or typed backend-source errors.
    fn eig_read(&mut self, _input: TensorRead<'_>) -> tenferro_tensor::Result<Vec<Tensor>> {
        Err(tenferro_tensor::Error::unsupported(
            "eig",
            "backend does not accept tensor reads at this execution boundary",
        ))
    }

    #[doc(hidden)]
    fn eigh_values(&mut self, _input: &Tensor) -> tenferro_tensor::Result<Tensor> {
        Err(tenferro_tensor::Error::unsupported(
            "eigh_values",
            format!(
                "backend {} does not implement internal Hermitian eigenvalues-only decomposition",
                std::any::type_name::<Self>()
            ),
        ))
    }

    /// Compute public general eigendecomposition outputs `(values, vectors)`.
    ///
    /// # Errors
    ///
    /// Returns `Error::Validation` for a non-square, rank, or dtype mismatch,
    /// and a typed `Error::Extension` or backend source when the eigensolver
    /// fails.
    fn eig(&mut self, input: &Tensor) -> tenferro_tensor::Result<Vec<Tensor>>;

    #[doc(hidden)]
    fn eig_values(&mut self, _input: &Tensor) -> tenferro_tensor::Result<Tensor> {
        Err(tenferro_tensor::Error::unsupported(
            "eig_values",
            format!(
                "backend {} does not implement internal general eigenvalues-only decomposition",
                std::any::type_name::<Self>()
            ),
        ))
    }

    /// Solve a dense linear system.
    ///
    /// # Errors
    ///
    /// Returns `Error::Validation` for incompatible matrix/rhs shapes, rank,
    /// or dtype; `Error::Extension` with `ErrorKind::NumericalFailure` for a
    /// singular system; or a typed backend source for provider failure.
    fn solve(&mut self, a: &Tensor, b: &Tensor) -> tenferro_tensor::Result<Tensor>;

    /// Solve a linear system from tensor read targets.
    ///
    /// Backends may canonicalize the inputs inside the same placement family,
    /// but must not silently transfer between CPU and GPU memory.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
    /// use tenferro_linalg::LinalgBackend;
    /// use tenferro_tensor::{BackendSessionHost, Tensor, TensorRead};
    ///
    /// let a = Tensor::from_vec_col_major(vec![2, 2], vec![2.0_f64, 0.0, 0.0, 3.0])?;
    /// let b = Tensor::from_vec_col_major(vec![2, 1], vec![4.0_f64, 9.0])?;
    /// let mut host = CpuBackend::new();
    /// let x = host.with_backend_session(|session| {
    ///     with_cpu_exec_session(session, |backend| {
    ///         backend.solve_read(
    ///             TensorRead::from_tensor(&a),
    ///             TensorRead::from_tensor(&b),
    ///         )
    ///     })
    ///     .expect("CpuBackend must expose a CpuExecSession")
    /// })?;
    /// let Tensor::F64(x) = x else { unreachable!("F64 inputs return F64 output") };
    /// assert_eq!(x.host_data()?, &[2.0, 3.0]);
    /// # Ok::<(), tenferro_tensor::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// The default implementation returns `Error::Unsupported` because the
    /// backend does not accept tensor read targets. Implementations may return
    /// `Error::Validation` for incompatible shapes or dtypes,
    /// `Error::RuntimeState` for invalid placement, `Error::Extension` for a
    /// singular system, or a typed backend-source error.
    fn solve_read(
        &mut self,
        _a: TensorRead<'_>,
        _b: TensorRead<'_>,
    ) -> tenferro_tensor::Result<Tensor> {
        Err(tenferro_tensor::Error::unsupported(
            "solve",
            "backend does not accept tensor reads at this execution boundary",
        ))
    }

    /// Solve into a caller-owned destination.
    ///
    /// The default preserves the ordinary read path and copies its result into
    /// `out`. Backends with a native destination path may override this method,
    /// but must validate the destination before the first write and preserve
    /// the same shape, dtype, placement, aliasing, and error contracts.
    ///
    /// # Errors
    ///
    /// Returns `tenferro_tensor_core::ShapeMismatch` or
    /// `tenferro_tensor_core::ValidationError::DTypeMismatch` for incompatible
    /// destination metadata, `tenferro_tensor_core::ValidationError::InvalidArgument`
    /// for aliasing or placement violations, `Error::Unsupported` when the
    /// provider is unavailable, and `Error::Singular` for a singular system.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
    /// use tenferro_linalg::LinalgBackend;
    /// use tenferro_tensor::{BackendSessionHost, Tensor, TensorRead, TensorWrite};
    ///
    /// let a = Tensor::from_vec_col_major(vec![2, 2], vec![2.0_f64, 0.0, 0.0, 4.0])?;
    /// let b = Tensor::from_vec_col_major(vec![2, 1], vec![4.0_f64, 8.0])?;
    /// let mut out = Tensor::from_vec_col_major(vec![2, 1], vec![0.0_f64; 2])?;
    /// let mut host = CpuBackend::new();
    /// host.with_backend_session(|session| {
    ///     with_cpu_exec_session(session, |backend| {
    ///         backend.solve_read_into(
    ///             TensorRead::from_tensor(&a),
    ///             TensorRead::from_tensor(&b),
    ///             TensorWrite::from_tensor(&mut out),
    ///         )
    ///     })
    ///     .expect("CpuBackend must expose a CpuExecSession")
    /// })?;
    /// assert_eq!(out.as_slice::<f64>()?, &[2.0, 2.0]);
    /// # Ok::<(), tenferro_tensor::Error>(())
    /// ```
    fn solve_read_into(
        &mut self,
        a: TensorRead<'_>,
        b: TensorRead<'_>,
        out: TensorWrite<'_>,
    ) -> tenferro_tensor::Result<()> {
        solve_read_into_default(self, a, b, out)
    }

    #[doc(hidden)]
    fn lu_solve_prepared(
        &mut self,
        _a: &Tensor,
        _packed_lu: &Tensor,
        _pivots: &Tensor,
        _b: &Tensor,
        _transpose_a: bool,
        _conjugate_a: bool,
    ) -> tenferro_tensor::Result<Tensor> {
        Err(tenferro_tensor::Error::unsupported(
            "lu_solve_prepared",
            format!(
                "backend {} does not implement internal prepared LU solve",
                std::any::type_name::<Self>()
            ),
        ))
    }
}

pub(crate) fn solve_read_into_default<B: LinalgBackend + ?Sized>(
    backend: &mut B,
    a: TensorRead<'_>,
    b: TensorRead<'_>,
    out: TensorWrite<'_>,
) -> tenferro_tensor::Result<()> {
    validate_solve_read_into(&a, &b, &out)?;
    let result = backend.solve_read(a, b)?;
    backend.copy_read_into(TensorRead::from_tensor(&result), out)
}

pub(crate) fn validate_solve_read_into(
    _a: &TensorRead<'_>,
    b: &TensorRead<'_>,
    out: &TensorWrite<'_>,
) -> tenferro_tensor::Result<()> {
    if b.shape() != out.shape() {
        return Err(tenferro_tensor::Error::shape_mismatch(
            "solve_read_into",
            b.shape().to_vec(),
            out.shape().to_vec(),
        ));
    }
    if b.dtype() != out.dtype() {
        return Err(tenferro_tensor::Error::dtype_mismatch(
            "solve_read_into",
            b.dtype(),
            out.dtype(),
        ));
    }
    if b.placement() != out.as_read().placement() {
        return Err(tenferro_tensor::Error::invalid_argument(
            "solve_read_into",
            "out",
            format!(
                "destination placement {:?} does not match rhs placement {:?}",
                out.as_read().placement(),
                b.placement()
            ),
        ));
    }
    let inputs = [_a.clone(), b.clone()];
    tenferro_tensor::backend::validate_read_into_destination("solve_read_into", &inputs, out)
}