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
//! GPU-accelerated SVD for HoloTensor LRDF encoding.
//!
//! Uses cuBLAS for matrix operations with hardware-optimized paths:
//! - TF32 mode for RTX 30/40 series (8x faster than FP32)
//! - GPU-resident deflation with `ger` (eliminates CPU round-trips)
//! - GPU normalization with `nrm2` + `scal`
/// CUDA-accelerated SVD using cuBLAS power iteration.
#[cfg(feature = "cuda")]
pub mod cuda {
use cudarc::cublas::sys::lib as cublas_lib;
use cudarc::cublas::{CudaBlas, Gemv, GemvConfig};
use cudarc::driver::{CudaDevice, CudaSlice, DevicePtr, DevicePtrMut};
use std::sync::Arc;
/// GPU-accelerated SVD using power iteration with cuBLAS.
///
/// Optimizations:
/// - All deflation done on GPU (no CPU round-trips)
/// - Vector normalization on GPU
/// - TF32 enabled for Ada/Ampere GPUs
pub struct GpuSvd {
device: Arc<CudaDevice>,
blas: CudaBlas,
}
impl GpuSvd {
/// Create new GPU SVD context.
pub fn new(device: Arc<CudaDevice>) -> Result<Self, cudarc::cublas::result::CublasError> {
let blas = CudaBlas::new(device.clone())?;
// Enable TF32 for Ampere+ GPUs (compute 8.0+)
// TF32 uses 19-bit mantissa, 8x faster than FP32, ~0.1% accuracy loss
// Safe for SVD where we don't need full FP32 precision
unsafe {
let handle = *blas.handle();
// Try to set TF32 mode - ignore errors on older GPUs
let _ = cublas_lib().cublasSetMathMode(
handle,
cudarc::cublas::sys::cublasMath_t::CUBLAS_TF32_TENSOR_OP_MATH,
);
}
Ok(Self { device, blas })
}
/// Get device reference.
pub fn device(&self) -> &Arc<CudaDevice> {
&self.device
}
/// Compute truncated SVD using GPU-accelerated power iteration.
///
/// **Fully GPU-resident**: Deflation and normalization done on GPU.
///
/// Returns (U, S, V) where:
/// - U: rows x rank (row-major)
/// - S: rank singular values
/// - V: cols x rank (row-major)
pub fn svd_power_iteration(
&self,
matrix: &[f32],
rows: usize,
cols: usize,
rank: usize,
iterations: usize,
) -> Result<(Vec<f32>, Vec<f32>, Vec<f32>), Box<dyn std::error::Error>> {
let d_matrix: CudaSlice<f32> = self.device.htod_sync_copy(matrix)?;
self.svd_power_iteration_gpu(d_matrix, rows, cols, rank, iterations)
}
/// Compute SVD from GPU-resident matrix (avoids host-to-device copy).
///
/// Use this when data is already on GPU (e.g., after FP8→F32 conversion).
pub fn svd_power_iteration_gpu(
&self,
mut d_matrix: CudaSlice<f32>,
rows: usize,
cols: usize,
rank: usize,
iterations: usize,
) -> Result<(Vec<f32>, Vec<f32>, Vec<f32>), Box<dyn std::error::Error>> {
let mut d_u: CudaSlice<f32> = self.device.alloc_zeros(rows)?;
let mut d_v: CudaSlice<f32> = self.device.alloc_zeros(cols)?;
let mut u_out = vec![0.0f32; rows * rank];
let mut s_out = vec![0.0f32; rank];
let mut v_out = vec![0.0f32; cols * rank];
for r in 0..rank {
// Initialize v randomly
let h_v: Vec<f32> = (0..cols)
.map(|i| {
let x = (((i + r) as u64 * 1103515245 + 12345) % 0x80000000) as f32;
(x / 0x80000000u32 as f32) - 0.5
})
.collect();
self.device.htod_sync_copy_into(&h_v, &mut d_v)?;
self.gpu_normalize(&mut d_v, cols)?;
// Power iteration
for _ in 0..iterations {
self.gemv_row_major(&d_matrix, &d_v, &mut d_u, rows, cols, false)?;
self.gpu_normalize(&mut d_u, rows)?;
self.gemv_row_major(&d_matrix, &d_u, &mut d_v, rows, cols, true)?;
self.gpu_normalize(&mut d_v, cols)?;
}
// Final u = A*v, sigma = ||u||, u = u/sigma
self.gemv_row_major(&d_matrix, &d_v, &mut d_u, rows, cols, false)?;
let sigma = self.gpu_norm(&d_u, rows)?;
s_out[r] = sigma;
if sigma > 1e-10 {
self.gpu_scale(&mut d_u, rows, 1.0 / sigma)?;
}
// Store u and v
let mut h_u = vec![0.0f32; rows];
let mut h_v = vec![0.0f32; cols];
self.device.dtoh_sync_copy_into(&d_u, &mut h_u)?;
self.device.dtoh_sync_copy_into(&d_v, &mut h_v)?;
for i in 0..rows {
u_out[i * rank + r] = h_u[i];
}
for j in 0..cols {
v_out[j * rank + r] = h_v[j];
}
// GPU Deflation: A = A - sigma * u * v^T
if r < rank - 1 {
self.gpu_ger(&mut d_matrix, &d_u, &d_v, rows, cols, -sigma)?;
}
}
Ok((u_out, s_out, v_out))
}
/// GPU matrix-vector multiply for row-major matrix.
fn gemv_row_major(
&self,
d_matrix: &CudaSlice<f32>,
d_x: &CudaSlice<f32>,
d_y: &mut CudaSlice<f32>,
rows: usize,
cols: usize,
transpose: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let (trans, m, n) = if transpose {
(
cudarc::cublas::sys::cublasOperation_t::CUBLAS_OP_N,
cols,
rows,
)
} else {
(
cudarc::cublas::sys::cublasOperation_t::CUBLAS_OP_T,
cols,
rows,
)
};
unsafe {
self.blas.gemv(
GemvConfig {
trans,
m: m as i32,
n: n as i32,
alpha: 1.0f32,
lda: cols as i32,
incx: 1,
beta: 0.0f32,
incy: 1,
},
d_matrix,
d_x,
d_y,
)?;
}
Ok(())
}
/// GPU rank-1 update: A = alpha * x * y^T + A
fn gpu_ger(
&self,
d_a: &mut CudaSlice<f32>,
d_u: &CudaSlice<f32>,
d_v: &CudaSlice<f32>,
rows: usize,
cols: usize,
alpha: f32,
) -> Result<(), Box<dyn std::error::Error>> {
unsafe {
let handle = *self.blas.handle();
// For row-major A(rows,cols) stored as col-major A^T(cols,rows):
// To do A[i,j] += alpha * u[i] * v[j] in row-major
// = A^T[j,i] += alpha * u[i] * v[j] in col-major view
// Use ger(m=cols, n=rows, x=v, y=u) on the col-major view
let status = cublas_lib().cublasSger_v2(
handle,
cols as i32, // m
rows as i32, // n
&alpha as *const f32,
*d_v.device_ptr() as *const f32, // x = v
1,
*d_u.device_ptr() as *const f32, // y = u
1,
*d_a.device_ptr_mut() as *mut f32,
cols as i32, // lda
);
if status != cudarc::cublas::sys::cublasStatus_t::CUBLAS_STATUS_SUCCESS {
return Err(format!("cublasSger failed: {:?}", status).into());
}
}
Ok(())
}
/// GPU vector normalization: x = x / ||x||
fn gpu_normalize(
&self,
d_x: &mut CudaSlice<f32>,
n: usize,
) -> Result<(), Box<dyn std::error::Error>> {
let norm = self.gpu_norm(d_x, n)?;
if norm > 1e-10 {
self.gpu_scale(d_x, n, 1.0 / norm)?;
}
Ok(())
}
/// GPU vector norm: ||x||
fn gpu_norm(
&self,
d_x: &CudaSlice<f32>,
n: usize,
) -> Result<f32, Box<dyn std::error::Error>> {
unsafe {
let handle = *self.blas.handle();
let mut result: f32 = 0.0;
let status = cublas_lib().cublasSnrm2_v2(
handle,
n as i32,
*d_x.device_ptr() as *const f32,
1,
&mut result as *mut f32,
);
if status != cudarc::cublas::sys::cublasStatus_t::CUBLAS_STATUS_SUCCESS {
return Err(format!("cublasSnrm2 failed: {:?}", status).into());
}
Ok(result)
}
}
/// GPU vector scale: x = alpha * x
fn gpu_scale(
&self,
d_x: &mut CudaSlice<f32>,
n: usize,
alpha: f32,
) -> Result<(), Box<dyn std::error::Error>> {
unsafe {
let handle = *self.blas.handle();
let status = cublas_lib().cublasSscal_v2(
handle,
n as i32,
&alpha as *const f32,
*d_x.device_ptr_mut() as *mut f32,
1,
);
if status != cudarc::cublas::sys::cublasStatus_t::CUBLAS_STATUS_SUCCESS {
return Err(format!("cublasSscal failed: {:?}", status).into());
}
}
Ok(())
}
}
}
/// Stub module when CUDA is not enabled.
#[cfg(not(feature = "cuda"))]
pub mod cuda {
/// GPU SVD stub (requires CUDA feature).
pub struct GpuSvd;
impl GpuSvd {
/// Create new GPU SVD context (returns error without CUDA).
pub fn new(_device: std::sync::Arc<()>) -> Result<Self, std::io::Error> {
Err(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"CUDA not enabled",
))
}
/// Compute truncated SVD (returns error without CUDA).
pub fn svd_power_iteration(
&self,
_matrix: &[f32],
_rows: usize,
_cols: usize,
_rank: usize,
_iterations: usize,
) -> Result<(Vec<f32>, Vec<f32>, Vec<f32>), Box<dyn std::error::Error>> {
Err("CUDA not enabled".into())
}
}
}
#[cfg(test)]
mod tests {
use super::cuda::GpuSvd;
#[test]
fn test_gpu_svd_stub_without_cuda() {
// Without CUDA feature, GpuSvd::new should return an error
#[cfg(not(feature = "cuda"))]
{
let result = GpuSvd::new(std::sync::Arc::new(()));
assert!(result.is_err());
}
}
}