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
use tenferro_tensor::{Tensor, TensorBackend, TensorView};
/// Backend surface required by the linalg extension runtime.
///
/// # Examples
///
/// ```rust
/// use tenferro_linalg::backend::LinalgBackend;
/// use tenferro_cpu::CpuBackend;
///
/// fn accepts_linalg_backend<B: LinalgBackend>(_backend: &mut B) {}
///
/// let mut backend = CpuBackend::new();
/// accepts_linalg_backend(&mut backend);
/// ```
pub trait LinalgBackend: TensorBackend {
/// Compute a Cholesky 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.
fn triangular_solve(
&mut self,
a: &Tensor,
b: &Tensor,
left_side: bool,
lower: bool,
transpose_a: bool,
unit_diagonal: bool,
) -> tenferro_tensor::Result<Tensor>;
/// Compute public LU outputs `(P, L, U, parity)`.
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::backend_failure(
"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.
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`.
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)`.
fn svd(&mut self, input: &Tensor) -> tenferro_tensor::Result<Vec<Tensor>>;
#[doc(hidden)]
fn svd_values(&mut self, _input: &Tensor) -> tenferro_tensor::Result<Tensor> {
Err(tenferro_tensor::Error::backend_failure(
"svd_values",
format!(
"backend {} does not implement internal singular-values-only decomposition",
std::any::type_name::<Self>()
),
))
}
/// Compute a singular value decomposition from a borrowed tensor view.
///
/// Backends may canonicalize the view inside the same placement family, but
/// must not silently transfer between CPU and GPU memory.
///
/// # Examples
///
/// ```rust
/// use tenferro_linalg::LinalgBackend;
/// use tenferro_cpu::CpuBackend;
/// use tenferro_tensor::{TensorView, TypedTensor};
///
/// let input = TypedTensor::<f64>::from_vec_col_major(
/// vec![2, 2],
/// vec![1.0, 0.0, 0.0, 2.0],
/// )?;
/// let outputs = CpuBackend::new().svd_read(TensorView::F64(input.as_view()))?;
/// assert_eq!(outputs[1].shape(), &[2]);
/// # Ok::<(), tenferro_tensor::Error>(())
/// ```
fn svd_read(&mut self, _input: TensorView<'_>) -> tenferro_tensor::Result<Vec<Tensor>> {
Err(tenferro_tensor::Error::backend_failure(
"svd",
"backend does not accept borrowed tensor views 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`.
fn qr(&mut self, input: &Tensor) -> tenferro_tensor::Result<Vec<Tensor>>;
/// 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]`.
fn eigh(&mut self, input: &Tensor) -> tenferro_tensor::Result<Vec<Tensor>>;
#[doc(hidden)]
fn eigh_values(&mut self, _input: &Tensor) -> tenferro_tensor::Result<Tensor> {
Err(tenferro_tensor::Error::backend_failure(
"eigh_values",
format!(
"backend {} does not implement internal Hermitian eigenvalues-only decomposition",
std::any::type_name::<Self>()
),
))
}
/// Compute public general eigendecomposition outputs `(values, vectors)`.
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::backend_failure(
"eig_values",
format!(
"backend {} does not implement internal general eigenvalues-only decomposition",
std::any::type_name::<Self>()
),
))
}
/// Solve a dense linear system.
fn solve(&mut self, a: &Tensor, b: &Tensor) -> tenferro_tensor::Result<Tensor>;
#[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::backend_failure(
"lu_solve_prepared",
format!(
"backend {} does not implement internal prepared LU solve",
std::any::type_name::<Self>()
),
))
}
}