numr 0.5.2

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
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
//! Common utilities for WebGPU sparse linear algebra.

use std::time::Duration;

use wgpu::{
    BindGroupLayout, BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingType,
    BufferBindingType, ShaderStages,
};

use super::super::{WgpuClient, WgpuRuntime};
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::ops::{CumulativeOps, ShapeOps};
use crate::runtime::RuntimeClient;
use crate::sparse::CsrData;
use crate::tensor::Tensor;

/// Workgroup size for sparse linear algebra kernels.
pub const WORKGROUP_SIZE: u32 = 256;

/// Validate dtype for WebGPU sparse linear algebra (F32 only).
pub fn validate_wgpu_dtype(dtype: DType, op: &'static str) -> Result<()> {
    if dtype != DType::F32 {
        return Err(Error::UnsupportedDType { dtype, op });
    }
    Ok(())
}

/// Create bind group layout for ILU/IC kernels.
pub fn create_ilu_ic_layout(device: &wgpu::Device) -> BindGroupLayout {
    device.create_bind_group_layout(&BindGroupLayoutDescriptor {
        label: Some("ilu_ic_layout"),
        entries: &[
            // level_rows (read-only input)
            BindGroupLayoutEntry {
                binding: 0,
                visibility: ShaderStages::COMPUTE,
                ty: BindingType::Buffer {
                    ty: BufferBindingType::Storage { read_only: true },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            // row_ptrs (read-only input)
            BindGroupLayoutEntry {
                binding: 1,
                visibility: ShaderStages::COMPUTE,
                ty: BindingType::Buffer {
                    ty: BufferBindingType::Storage { read_only: true },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            // col_indices (read-only input)
            BindGroupLayoutEntry {
                binding: 2,
                visibility: ShaderStages::COMPUTE,
                ty: BindingType::Buffer {
                    ty: BufferBindingType::Storage { read_only: true },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            // values (read_write for in-place factorization)
            BindGroupLayoutEntry {
                binding: 3,
                visibility: ShaderStages::COMPUTE,
                ty: BindingType::Buffer {
                    ty: BufferBindingType::Storage { read_only: false },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            // diag_indices (read-only input, written by find_diag_indices separately)
            BindGroupLayoutEntry {
                binding: 4,
                visibility: ShaderStages::COMPUTE,
                ty: BindingType::Buffer {
                    ty: BufferBindingType::Storage { read_only: true },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            // params (uniform)
            BindGroupLayoutEntry {
                binding: 5,
                visibility: ShaderStages::COMPUTE,
                ty: BindingType::Buffer {
                    ty: BufferBindingType::Uniform,
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
        ],
    })
}

/// Create bind group layout for triangular solve kernels.
pub fn create_trsv_layout(device: &wgpu::Device) -> BindGroupLayout {
    device.create_bind_group_layout(&BindGroupLayoutDescriptor {
        label: Some("trsv_layout"),
        entries: &[
            // level_rows (read-only input)
            BindGroupLayoutEntry {
                binding: 0,
                visibility: ShaderStages::COMPUTE,
                ty: BindingType::Buffer {
                    ty: BufferBindingType::Storage { read_only: true },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            // row_ptrs (read-only input)
            BindGroupLayoutEntry {
                binding: 1,
                visibility: ShaderStages::COMPUTE,
                ty: BindingType::Buffer {
                    ty: BufferBindingType::Storage { read_only: true },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            // col_indices (read-only input)
            BindGroupLayoutEntry {
                binding: 2,
                visibility: ShaderStages::COMPUTE,
                ty: BindingType::Buffer {
                    ty: BufferBindingType::Storage { read_only: true },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            // values (read-only input)
            BindGroupLayoutEntry {
                binding: 3,
                visibility: ShaderStages::COMPUTE,
                ty: BindingType::Buffer {
                    ty: BufferBindingType::Storage { read_only: true },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            // b (read-only input)
            BindGroupLayoutEntry {
                binding: 4,
                visibility: ShaderStages::COMPUTE,
                ty: BindingType::Buffer {
                    ty: BufferBindingType::Storage { read_only: true },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            // x (read_write output)
            BindGroupLayoutEntry {
                binding: 5,
                visibility: ShaderStages::COMPUTE,
                ty: BindingType::Buffer {
                    ty: BufferBindingType::Storage { read_only: false },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            // params (uniform)
            BindGroupLayoutEntry {
                binding: 6,
                visibility: ShaderStages::COMPUTE,
                ty: BindingType::Buffer {
                    ty: BufferBindingType::Uniform,
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
        ],
    })
}

impl WgpuClient {
    /// Wait for GPU operations to complete.
    pub(super) fn poll_wait(&self) {
        let _ = self.wgpu_device.poll(wgpu::PollType::Wait {
            submission_index: None,
            timeout: Some(Duration::from_secs(60)),
        });
    }
}

/// Split factored LU matrix into L and U components - GPU-native implementation.
///
/// Keeps values entirely on GPU. Only row_ptrs and col_indices are on CPU
/// (they come from level scheduling which currently requires CPU).
pub fn split_lu_wgpu(
    client: &WgpuClient,
    n: usize,
    row_ptrs: &[i64],
    col_indices: &[i64],
    values_gpu: &Tensor<WgpuRuntime>,
) -> Result<crate::algorithm::sparse_linalg::IluDecomposition<WgpuRuntime>> {
    use super::super::ops::helpers::get_tensor_buffer;

    let dtype = values_gpu.dtype();
    let device = client.device();

    if dtype != DType::F32 {
        return Err(Error::UnsupportedDType {
            dtype,
            op: "split_lu_wgpu (GPU version only supports F32)",
        });
    }

    // Upload row_ptrs and col_indices to GPU (convert I64 → I32 for WebGPU)
    let row_ptrs_i32: Vec<i32> = row_ptrs.iter().map(|&x| x as i32).collect();
    let col_indices_i32: Vec<i32> = col_indices.iter().map(|&x| x as i32).collect();

    let row_ptrs_gpu =
        Tensor::<WgpuRuntime>::from_slice(&row_ptrs_i32, &[n + 1], &client.device_id);
    let col_indices_gpu = Tensor::<WgpuRuntime>::from_slice(
        &col_indices_i32,
        &[col_indices.len()],
        &client.device_id,
    );

    // Step 1: Count L and U non-zeros per row on GPU
    let l_counts_gpu = Tensor::<WgpuRuntime>::zeros(&[n], DType::I32, device);
    let u_counts_gpu = Tensor::<WgpuRuntime>::zeros(&[n], DType::I32, device);

    let row_ptrs_buf = get_tensor_buffer(&row_ptrs_gpu)?;
    let col_indices_buf = get_tensor_buffer(&col_indices_gpu)?;
    let l_counts_buf = get_tensor_buffer(&l_counts_gpu)?;
    let u_counts_buf = get_tensor_buffer(&u_counts_gpu)?;

    let count_params: [u32; 4] = [n as u32, 0, 0, 0];
    let count_params_buf = client.create_uniform_buffer("split_lu_count_params", 16);
    client.write_buffer(&count_params_buf, &count_params);

    // Launch count kernel using proper launcher
    super::super::shaders::launch_split_lu_count(
        client.pipeline_cache(),
        &client.queue,
        &*row_ptrs_buf,
        &*col_indices_buf,
        &*l_counts_buf,
        &*u_counts_buf,
        &count_params_buf,
        n,
    )?;

    // Step 2: Compute prefix sum to get row_ptrs
    // Prepend zero on GPU using concat
    let zero_i32 = Tensor::<WgpuRuntime>::zeros(&[1], DType::I32, device);
    let l_counts_with_zero = client.cat(&[&zero_i32, &l_counts_gpu], 0)?;
    let u_counts_with_zero = client.cat(&[&zero_i32, &u_counts_gpu], 0)?;

    let l_row_ptrs_i32 = client.cumsum(&l_counts_with_zero, 0)?;
    let u_row_ptrs_i32 = client.cumsum(&u_counts_with_zero, 0)?;

    // Get total sizes from last element of row_ptrs
    // This is the ONE necessary scalar read for allocation
    let l_nnz = {
        let last = l_row_ptrs_i32.narrow(0, n, 1)?.contiguous();
        last.to_vec::<i32>()[0] as usize
    };
    let u_nnz = {
        let last = u_row_ptrs_i32.narrow(0, n, 1)?.contiguous();
        last.to_vec::<i32>()[0] as usize
    };

    // Step 3: Allocate output buffers
    let l_col_indices_gpu = Tensor::<WgpuRuntime>::zeros(&[l_nnz], DType::I32, device);
    let l_values_gpu = Tensor::<WgpuRuntime>::zeros(&[l_nnz], dtype, device);
    let u_col_indices_gpu = Tensor::<WgpuRuntime>::zeros(&[u_nnz], DType::I32, device);
    let u_values_gpu = Tensor::<WgpuRuntime>::zeros(&[u_nnz], dtype, device);

    // Step 4: Scatter values into L and U on GPU
    let values_buf = get_tensor_buffer(values_gpu)?;
    let l_row_ptrs_buf = get_tensor_buffer(&l_row_ptrs_i32)?;
    let l_col_indices_buf = get_tensor_buffer(&l_col_indices_gpu)?;
    let l_values_buf = get_tensor_buffer(&l_values_gpu)?;
    let u_row_ptrs_buf = get_tensor_buffer(&u_row_ptrs_i32)?;
    let u_col_indices_buf = get_tensor_buffer(&u_col_indices_gpu)?;
    let u_values_buf = get_tensor_buffer(&u_values_gpu)?;

    let scatter_params: [u32; 4] = [n as u32, 0, 0, 0];
    let scatter_params_buf = client.create_uniform_buffer("split_lu_scatter_params", 16);
    client.write_buffer(&scatter_params_buf, &scatter_params);

    // Launch scatter kernels using proper launchers (split to stay within 8-buffer limit)
    super::super::shaders::launch_split_lu_scatter_l(
        client.pipeline_cache(),
        &client.queue,
        &*row_ptrs_buf,
        &*col_indices_buf,
        &*values_buf,
        &*l_row_ptrs_buf,
        &*l_col_indices_buf,
        &*l_values_buf,
        &scatter_params_buf,
        n,
        dtype,
    )?;

    super::super::shaders::launch_split_lu_scatter_u(
        client.pipeline_cache(),
        &client.queue,
        &*row_ptrs_buf,
        &*col_indices_buf,
        &*values_buf,
        &*u_row_ptrs_buf,
        &*u_col_indices_buf,
        &*u_values_buf,
        &scatter_params_buf,
        n,
        dtype,
    )?;

    client.poll_wait();

    // Convert I32 to I64 on CPU (WGSL limitation - no I64 support)
    // This transfers metadata only (row_ptrs, col_indices), not VALUES
    let l_row_ptrs_i32_vec: Vec<i32> = l_row_ptrs_i32.to_vec();
    let u_row_ptrs_i32_vec: Vec<i32> = u_row_ptrs_i32.to_vec();
    let l_col_indices_i32_vec: Vec<i32> = l_col_indices_gpu.to_vec();
    let u_col_indices_i32_vec: Vec<i32> = u_col_indices_gpu.to_vec();

    let l_row_ptrs_i64: Vec<i64> = l_row_ptrs_i32_vec.iter().map(|&x| x as i64).collect();
    let u_row_ptrs_i64: Vec<i64> = u_row_ptrs_i32_vec.iter().map(|&x| x as i64).collect();
    let l_col_indices_i64: Vec<i64> = l_col_indices_i32_vec.iter().map(|&x| x as i64).collect();
    let u_col_indices_i64: Vec<i64> = u_col_indices_i32_vec.iter().map(|&x| x as i64).collect();

    let l_row_ptrs_t = Tensor::<WgpuRuntime>::from_slice(&l_row_ptrs_i64, &[n + 1], device);
    let l_col_indices_t = Tensor::<WgpuRuntime>::from_slice(&l_col_indices_i64, &[l_nnz], device);
    let u_row_ptrs_t = Tensor::<WgpuRuntime>::from_slice(&u_row_ptrs_i64, &[n + 1], device);
    let u_col_indices_t = Tensor::<WgpuRuntime>::from_slice(&u_col_indices_i64, &[u_nnz], device);

    let l = CsrData::new(l_row_ptrs_t, l_col_indices_t, l_values_gpu, [n, n])?;
    let u = CsrData::new(u_row_ptrs_t, u_col_indices_t, u_values_gpu, [n, n])?;

    Ok(crate::algorithm::sparse_linalg::IluDecomposition { l, u })
}

/// Extract lower triangular matrix after IC factorization - GPU-native implementation.
///
/// Keeps values entirely on GPU. Only row_ptrs and col_indices are on CPU
/// (they come from level scheduling which currently requires CPU).
pub fn extract_lower_wgpu(
    client: &WgpuClient,
    n: usize,
    row_ptrs: &[i64],
    col_indices: &[i64],
    values_gpu: &Tensor<WgpuRuntime>,
) -> Result<crate::algorithm::sparse_linalg::IcDecomposition<WgpuRuntime>> {
    use super::super::ops::helpers::get_tensor_buffer;

    let dtype = values_gpu.dtype();
    let device = client.device();

    if dtype != DType::F32 {
        return Err(Error::UnsupportedDType {
            dtype,
            op: "extract_lower_wgpu (GPU version only supports F32)",
        });
    }

    // Upload row_ptrs and col_indices to GPU (convert I64 → I32 for WebGPU)
    let row_ptrs_i32: Vec<i32> = row_ptrs.iter().map(|&x| x as i32).collect();
    let col_indices_i32: Vec<i32> = col_indices.iter().map(|&x| x as i32).collect();

    let row_ptrs_gpu =
        Tensor::<WgpuRuntime>::from_slice(&row_ptrs_i32, &[n + 1], &client.device_id);
    let col_indices_gpu = Tensor::<WgpuRuntime>::from_slice(
        &col_indices_i32,
        &[col_indices.len()],
        &client.device_id,
    );

    // Step 1: Count lower triangle non-zeros per row on GPU
    let l_counts_gpu = Tensor::<WgpuRuntime>::zeros(&[n], DType::I32, device);

    let row_ptrs_buf = get_tensor_buffer(&row_ptrs_gpu)?;
    let col_indices_buf = get_tensor_buffer(&col_indices_gpu)?;
    let l_counts_buf = get_tensor_buffer(&l_counts_gpu)?;

    let count_params: [u32; 4] = [n as u32, 0, 0, 0];
    let count_params_buf = client.create_uniform_buffer("extract_lower_count_params", 16);
    client.write_buffer(&count_params_buf, &count_params);

    // Launch count kernel using proper launcher
    super::super::shaders::launch_extract_lower_count(
        client.pipeline_cache(),
        &client.queue,
        &*row_ptrs_buf,
        &*col_indices_buf,
        &*l_counts_buf,
        &count_params_buf,
        n,
    )?;

    // Step 2: Compute prefix sum to get row_ptrs
    // Prepend zero on GPU using concat
    let zero_i32 = Tensor::<WgpuRuntime>::zeros(&[1], DType::I32, device);
    let l_counts_with_zero = client.cat(&[&zero_i32, &l_counts_gpu], 0)?;

    let l_row_ptrs_i32 = client.cumsum(&l_counts_with_zero, 0)?;

    // Get total size from last element of row_ptrs
    // This is the ONE necessary scalar read for allocation
    let l_nnz = {
        let last = l_row_ptrs_i32.narrow(0, n, 1)?.contiguous();
        last.to_vec::<i32>()[0] as usize
    };

    // Step 3: Allocate output buffers
    let l_col_indices_gpu = Tensor::<WgpuRuntime>::zeros(&[l_nnz], DType::I32, device);
    let l_values_gpu = Tensor::<WgpuRuntime>::zeros(&[l_nnz], dtype, device);

    // Step 4: Scatter values into L on GPU
    let values_buf = get_tensor_buffer(values_gpu)?;
    let l_row_ptrs_buf = get_tensor_buffer(&l_row_ptrs_i32)?;
    let l_col_indices_buf = get_tensor_buffer(&l_col_indices_gpu)?;
    let l_values_buf = get_tensor_buffer(&l_values_gpu)?;

    let scatter_params: [u32; 4] = [n as u32, 0, 0, 0];
    let scatter_params_buf = client.create_uniform_buffer("extract_lower_scatter_params", 16);
    client.write_buffer(&scatter_params_buf, &scatter_params);

    // Launch scatter kernel using proper launcher
    super::super::shaders::launch_extract_lower_scatter(
        client.pipeline_cache(),
        &client.queue,
        &*row_ptrs_buf,
        &*col_indices_buf,
        &*values_buf,
        &*l_row_ptrs_buf,
        &*l_col_indices_buf,
        &*l_values_buf,
        &scatter_params_buf,
        n,
        dtype,
    )?;

    client.poll_wait();

    // Convert I32 to I64 on CPU (WGSL limitation - no I64 support)
    // This transfers metadata only (row_ptrs, col_indices), not VALUES
    let l_row_ptrs_i32_vec: Vec<i32> = l_row_ptrs_i32.to_vec();
    let l_col_indices_i32_vec: Vec<i32> = l_col_indices_gpu.to_vec();

    let l_row_ptrs_i64: Vec<i64> = l_row_ptrs_i32_vec.iter().map(|&x| x as i64).collect();
    let l_col_indices_i64: Vec<i64> = l_col_indices_i32_vec.iter().map(|&x| x as i64).collect();

    let l_row_ptrs_t = Tensor::<WgpuRuntime>::from_slice(&l_row_ptrs_i64, &[n + 1], device);
    let l_col_indices_t = Tensor::<WgpuRuntime>::from_slice(&l_col_indices_i64, &[l_nnz], device);

    let l = CsrData::new(l_row_ptrs_t, l_col_indices_t, l_values_gpu, [n, n])?;

    Ok(crate::algorithm::sparse_linalg::IcDecomposition { l })
}

// ============================================================================
// GPU-native i64→i32 casting (avoids manual CPU conversion)
// ============================================================================

/// Cast i64 CSR indices to i32 on GPU to eliminate manual CPU conversion.
///
/// WebGPU doesn't support i64 compute types, but we can cast on GPU by reading
/// raw bytes (i64 stored as pair of u32, we extract low 32 bits).
///
/// **Why this matters**: Without this, code transfers i64 tensors to CPU, converts
/// to i32, then uploads back. This GPU-native cast stays entirely on the device.
pub fn cast_i64_to_i32_gpu(
    client: &WgpuClient,
    tensor_i64: &Tensor<WgpuRuntime>,
) -> Result<Tensor<WgpuRuntime>> {
    use super::super::ops::helpers::get_tensor_buffer;

    let n = tensor_i64.numel();
    let device = client.device();

    // Allocate output i32 tensor
    let tensor_i32 = Tensor::<WgpuRuntime>::zeros(&[n], DType::I32, device);

    // Launch GPU-native cast kernel
    let input_buf = get_tensor_buffer(tensor_i64)?;
    let output_buf = get_tensor_buffer(&tensor_i32)?;

    super::super::shaders::sparse_level_compute::launch_cast_i64_to_i32(
        client.pipeline_cache(),
        &client.queue,
        &*input_buf,
        &*output_buf,
        n,
    )?;

    client.poll_wait();

    Ok(tensor_i32)
}