numr 0.16.0

Numerical computing and tensor library for Rust, in the role of NumPy: n-dimensional arrays, linear algebra, FFT, statistics and autograd on CPU, CUDA and WebGPU
Documentation
//! LU, Cholesky, and QR decompositions for WebGPU backend.

use super::super::client::get_buffer;
use super::super::shaders::linalg as kernels;
use super::super::{WgpuClient, WgpuRuntime};
use crate::algorithm::linalg::{
    CholeskyDecomposition, LuDecomposition, QrDecomposition, validate_linalg_dtype,
    validate_matrix_2d, validate_square_matrix,
};
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::runtime::{AllocGuard, Runtime, RuntimeClient};
use crate::tensor::{Layout, Storage, Tensor};

pub fn lu_decompose(
    client: &WgpuClient,
    a: &Tensor<WgpuRuntime>,
) -> Result<LuDecomposition<WgpuRuntime>> {
    validate_linalg_dtype(a.dtype())?;
    let (m, n) = validate_matrix_2d(a.shape())?;
    let k = m.min(n);
    let dtype = a.dtype();
    let device = client.device();

    // WGSL only supports F32
    if dtype != DType::F32 {
        return Err(Error::UnsupportedDType {
            dtype,
            op: "WGPU lu_decompose (only F32 supported)",
        });
    }

    // Allocate buffers
    let lu_size = m * n * dtype.size_in_bytes();
    let lu_guard = AllocGuard::new(client.allocator(), lu_size)?;
    let lu_ptr = lu_guard.ptr();
    let lu_buffer =
        get_buffer(lu_ptr).ok_or_else(|| Error::Internal("Failed to get LU buffer".to_string()))?;

    let pivots_size = k * std::mem::size_of::<i32>();
    let pivots_guard = AllocGuard::new(client.allocator(), pivots_size)?;
    let pivots_ptr = pivots_guard.ptr();
    let pivots_buffer = get_buffer(pivots_ptr)
        .ok_or_else(|| Error::Internal("Failed to get pivots buffer".to_string()))?;

    let num_swaps_size = std::mem::size_of::<i32>();
    let num_swaps_guard = AllocGuard::new(client.allocator(), num_swaps_size)?;
    let num_swaps_ptr = num_swaps_guard.ptr();
    let num_swaps_buffer = get_buffer(num_swaps_ptr)
        .ok_or_else(|| Error::Internal("Failed to get num_swaps buffer".to_string()))?;

    let singular_flag_size = std::mem::size_of::<i32>();
    let singular_flag_guard = AllocGuard::new(client.allocator(), singular_flag_size)?;
    let singular_flag_ptr = singular_flag_guard.ptr();
    let singular_flag_buffer = get_buffer(singular_flag_ptr)
        .ok_or_else(|| Error::Internal("Failed to get singular_flag buffer".to_string()))?;

    // Copy input to LU buffer
    WgpuRuntime::copy_within_device(a.ptr(), lu_ptr, lu_size, device)?;

    // Create params buffer
    let params: [u32; 2] = [m as u32, n as u32];
    let params_buffer = client.create_uniform_buffer("lu_params", 8);
    client.write_buffer(&params_buffer, &params);

    // Zero-initialize flags
    let zero_i32: [i32; 1] = [0];
    client.write_buffer(&num_swaps_buffer, &zero_i32);
    client.write_buffer(&singular_flag_buffer, &zero_i32);

    // Launch kernel
    kernels::launch_lu_decompose(
        client.pipeline_cache(),
        &client.queue,
        &lu_buffer,
        &pivots_buffer,
        &num_swaps_buffer,
        &singular_flag_buffer,
        &params_buffer,
        dtype,
    )?;

    client.synchronize();

    // Read back flags
    let staging = client.create_staging_buffer("lu_flags_staging", 8);
    let mut encoder = client
        .wgpu_device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("lu_flags_copy"),
        });
    encoder.copy_buffer_to_buffer(&num_swaps_buffer, 0, &staging, 0, 4);
    encoder.copy_buffer_to_buffer(&singular_flag_buffer, 0, &staging, 4, 4);
    client.submit_and_wait(encoder);

    let mut flags = [0i32; 2];
    client.read_buffer(&staging, &mut flags)?;

    let num_swaps = flags[0] as usize;
    let singular = flags[1] != 0;

    // Guards will automatically deallocate flag buffers on drop
    drop(num_swaps_guard);
    drop(singular_flag_guard);

    if singular {
        // Guards will deallocate lu and pivots on drop
        drop(lu_guard);
        drop(pivots_guard);
        return Err(Error::Internal(format!(
            "LU decomposition failed: {}x{} matrix is singular (zero pivot encountered)",
            m, n
        )));
    }

    // Keep pivots as I32 tensor directly (WGSL has no i64 support)
    // Create tensors from GPU memory
    let lu = unsafe { WgpuClient::tensor_from_raw(lu_guard.release(), &[m, n], dtype, device) };
    let pivots =
        unsafe { WgpuClient::tensor_from_raw(pivots_guard.release(), &[k], DType::I32, device) };

    Ok(LuDecomposition {
        lu,
        pivots,
        num_swaps,
    })
}

pub fn cholesky_decompose(
    client: &WgpuClient,
    a: &Tensor<WgpuRuntime>,
) -> Result<CholeskyDecomposition<WgpuRuntime>> {
    validate_linalg_dtype(a.dtype())?;
    let n = validate_square_matrix(a.shape())?;
    let dtype = a.dtype();
    let device = client.device();

    if dtype != DType::F32 {
        return Err(Error::UnsupportedDType {
            dtype,
            op: "WGPU cholesky_decompose (only F32 supported)",
        });
    }

    // Allocate output on GPU
    let l_size = n * n * dtype.size_in_bytes();
    let l_guard = AllocGuard::new(client.allocator(), l_size)?;
    let l_ptr = l_guard.ptr();
    let l_buffer =
        get_buffer(l_ptr).ok_or_else(|| Error::Internal("Failed to get L buffer".to_string()))?;

    let not_pd_flag_size = std::mem::size_of::<i32>();
    let not_pd_flag_guard = AllocGuard::new(client.allocator(), not_pd_flag_size)?;
    let not_pd_flag_ptr = not_pd_flag_guard.ptr();
    let not_pd_flag_buffer = get_buffer(not_pd_flag_ptr)
        .ok_or_else(|| Error::Internal("Failed to get not_pd_flag buffer".to_string()))?;

    // Copy input to L buffer
    WgpuRuntime::copy_within_device(a.ptr(), l_ptr, l_size, device)?;

    // Create params buffer
    let params: [u32; 1] = [n as u32];
    let params_buffer = client.create_uniform_buffer("chol_params", 4);
    client.write_buffer(&params_buffer, &params);

    // Zero-initialize flag
    let zero_i32: [i32; 1] = [0];
    client.write_buffer(&not_pd_flag_buffer, &zero_i32);

    // Launch kernel
    kernels::launch_cholesky_decompose(
        client.pipeline_cache(),
        &client.queue,
        &l_buffer,
        &not_pd_flag_buffer,
        &params_buffer,
        dtype,
    )?;

    client.synchronize();

    // Read back flag
    let staging = client.create_staging_buffer("chol_flag_staging", 4);
    let mut encoder = client
        .wgpu_device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("chol_flag_copy"),
        });
    encoder.copy_buffer_to_buffer(&not_pd_flag_buffer, 0, &staging, 0, 4);
    client.submit_and_wait(encoder);

    let mut not_pd = [0i32; 1];
    client.read_buffer(&staging, &mut not_pd)?;

    // Guard will automatically deallocate flag buffer on drop
    drop(not_pd_flag_guard);

    if not_pd[0] != 0 {
        // Guard will deallocate l on drop
        drop(l_guard);
        return Err(Error::Internal(format!(
            "Cholesky decomposition failed: {}x{} matrix is not positive definite",
            n, n
        )));
    }

    let l = unsafe { WgpuClient::tensor_from_raw(l_guard.release(), &[n, n], dtype, device) };

    Ok(CholeskyDecomposition { l })
}

pub fn qr_decompose_internal(
    client: &WgpuClient,
    a: &Tensor<WgpuRuntime>,
    thin: bool,
) -> Result<QrDecomposition<WgpuRuntime>> {
    validate_linalg_dtype(a.dtype())?;
    let (m, n) = validate_matrix_2d(a.shape())?;
    let k = m.min(n);
    let dtype = a.dtype();
    let device = client.device();

    if dtype != DType::F32 {
        return Err(Error::UnsupportedDType {
            dtype,
            op: "WGPU qr_decompose (only F32 supported)",
        });
    }

    // Q dimensions: [m, m] for full, [m, k] for thin
    let q_cols = if thin { k } else { m };
    let q_size = m * q_cols * dtype.size_in_bytes();
    let q_guard = AllocGuard::new(client.allocator(), q_size)?;
    let q_ptr = q_guard.ptr();
    let q_buffer =
        get_buffer(q_ptr).ok_or_else(|| Error::Internal("Failed to get Q buffer".to_string()))?;

    // R is [m, n] but only upper triangular part is meaningful
    let r_size = m * n * dtype.size_in_bytes();
    let r_guard = AllocGuard::new(client.allocator(), r_size)?;
    let r_ptr = r_guard.ptr();
    let r_buffer =
        get_buffer(r_ptr).ok_or_else(|| Error::Internal("Failed to get R buffer".to_string()))?;

    // One Householder vector of `m` elements per reflection.
    let workspace_size = m * k.max(1) * dtype.size_in_bytes();
    let workspace_guard = AllocGuard::new(client.allocator(), workspace_size)?;
    let workspace_ptr = workspace_guard.ptr();
    let workspace_buffer = get_buffer(workspace_ptr)
        .ok_or_else(|| Error::Internal("Failed to get workspace buffer".to_string()))?;

    // Copy A to R (will be modified in place)
    WgpuRuntime::copy_within_device(a.ptr(), r_ptr, r_size, device)?;

    // Create params buffer
    let params: [u32; 3] = [m as u32, n as u32, if thin { 1 } else { 0 }];
    let params_buffer = client.create_uniform_buffer("qr_params", 12);
    client.write_buffer(&params_buffer, &params);

    kernels::launch_qr_decompose(
        client.pipeline_cache(),
        &client.queue,
        &q_buffer,
        &r_buffer,
        &workspace_buffer,
        &params_buffer,
        dtype,
    )?;

    // Guard will automatically deallocate workspace on drop
    drop(workspace_guard);

    client.synchronize();

    let q = unsafe { WgpuClient::tensor_from_raw(q_guard.release(), &[m, q_cols], dtype, device) };

    // R holds the first `r_rows` rows of its `m` x `n` allocation. Thin QR with
    // `m > n` reads fewer rows than it allocates, and the storage states the
    // allocation.
    let r_rows = if thin && m > n { k } else { m };
    let r_storage =
        unsafe { Storage::<WgpuRuntime>::from_ptr_owned(r_guard.release(), m * n, dtype, device) };
    let r = Tensor::from_parts(r_storage, Layout::contiguous(&[r_rows, n]));

    Ok(QrDecomposition { q, r })
}

#[cfg(test)]
mod tests {
    use super::super::test_support::*;
    use super::*;
    use crate::algorithm::LinearAlgebraAlgorithms;
    use crate::algorithm::linalg::decompositions::qr_check;
    use crate::ops::MatmulOps;
    use crate::runtime::wgpu::is_wgpu_available;

    #[test]
    fn qr_reconstructs_every_shape() {
        if !is_wgpu_available() {
            println!("No GPU available, skipping test");
            return;
        }
        let client = create_client();
        for &(m, n, thin) in qr_check::CASES {
            let values = qr_check::matrix(m, n);
            let a = Tensor::<WgpuRuntime>::from_slice(&values, &[m, n], client.device()).unwrap();
            let qr = if thin {
                client.qr_decompose_thin(&a).unwrap()
            } else {
                client.qr_decompose(&a).unwrap()
            };
            qr_check::assert_holds(
                &values,
                (m, n, thin),
                (qr.q.shape(), &qr.q.to_vec::<f32>()),
                (qr.r.shape(), &qr.r.to_vec::<f32>()),
            );
        }
    }

    #[test]
    fn test_lu_decomposition() {
        if !is_wgpu_available() {
            println!("No GPU available, skipping test");
            return;
        }

        let client = create_client();
        let device = client.device();

        // 2x2 matrix: [[4, 3], [6, 3]]
        let a =
            Tensor::<WgpuRuntime>::from_slice(&[4.0f32, 3.0, 6.0, 3.0], &[2, 2], device).unwrap();

        let lu = client.lu_decompose(&a).unwrap();

        assert_eq!(lu.lu.shape(), &[2, 2]);
        assert_eq!(lu.pivots.shape(), &[2]);
    }

    #[test]
    fn test_cholesky() {
        if !is_wgpu_available() {
            println!("No GPU available, skipping test");
            return;
        }

        let client = create_client();
        let device = client.device();

        // Symmetric positive definite: [[4, 2], [2, 5]]
        let a =
            Tensor::<WgpuRuntime>::from_slice(&[4.0f32, 2.0, 2.0, 5.0], &[2, 2], device).unwrap();

        let chol = client.cholesky_decompose(&a).unwrap();

        assert_eq!(chol.l.shape(), &[2, 2]);

        // L should be lower triangular
        let l_data: Vec<f32> = chol.l.to_vec();
        assert!((l_data[1]).abs() < 1e-5); // Upper triangle should be 0
    }

    #[test]
    fn test_qr_decomposition() {
        if !is_wgpu_available() {
            println!("No GPU available, skipping test");
            return;
        }

        let client = create_client();
        let device = client.device();

        // Test QR: A = Q @ R
        let a =
            Tensor::<WgpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[2, 2], device).unwrap();

        let qr = client.qr_decompose(&a).unwrap();

        // Verify Q @ R == A
        let reconstructed = client.matmul(&qr.q, &qr.r).unwrap();
        let reconstructed = reconstructed.contiguous().unwrap();
        let a_data: Vec<f32> = a.to_vec();
        let reconstructed_data: Vec<f32> = reconstructed.to_vec();

        for i in 0..4 {
            assert!(
                (a_data[i] - reconstructed_data[i]).abs() < 1e-4,
                "Mismatch at {}: {} vs {}",
                i,
                a_data[i],
                reconstructed_data[i]
            );
        }
    }
}