Skip to main content

SparseTensor

Struct SparseTensor 

Source
pub struct SparseTensor {
    pub values: NodeId,
    pub col_idx: NodeId,
    pub row_ptr: NodeId,
    pub n_rows: usize,
    pub n_cols: usize,
}
Expand description

CSR-format sparse matrix at the IR level. Bundles the three CSR NodeIds with structural shape info known at graph-build time.

Modeled on jax.experimental.sparse.BCOO — a wrapper around (data, indices) arrays plus a shape tuple, with methods that expand to the right subgraph for each operation.

Fields§

§values: NodeId

Non-zero values in row-major CSR order. F64.

§col_idx: NodeId

Column index per non-zero. I32.

§row_ptr: NodeId

Start index in values / col_idx per row, length n_rows + 1. I32.

§n_rows: usize

Logical row count of A.

§n_cols: usize

Logical column count of A (n_rows == n_cols for square / SPD).

Implementations§

Source§

impl SparseTensor

Source

pub fn from_csr( values: NodeId, col_idx: NodeId, row_ptr: NodeId, n_rows: usize, n_cols: usize, ) -> Self

Build from existing CSR NodeIds. Caller is responsible for the layout invariants (sortedness within rows, row_ptr.len() == n_rows + 1, etc.).

Source

pub fn mat_vec(&self, g: &mut Graph, x: NodeId) -> NodeId

y = A · x for a length-n_cols dense vector.

Source

pub fn solve(&self, g: &mut Graph, b: NodeId) -> NodeId

x = A⁻¹ · b via direct LU.

Source

pub fn cg_solve( &self, g: &mut Graph, b: NodeId, max_iter: u32, tol: f64, ) -> NodeId

x = A⁻¹ · b via Conjugate Gradient. SPD only. tol is the absolute residual threshold; max_iter caps iteration count.

Source

pub fn solve_general( &self, g: &mut Graph, b: NodeId, adjoint: &SparseTensor, ) -> NodeId

x = A⁻¹ · b via direct LU for non-symmetric A. The caller supplies an explicit transpose adjoint (CSR of Aᵀ) — for square non-symmetric matrices, Aᵀ has the same nnz pattern as A after a CSR↔CSC swap. The forward solve uses only self; the VJP routes the adjoint solve through adjoint. Use this in place of solve when the assumption Aᵀ = A would be wrong.

Source

pub fn pcg_solve( &self, g: &mut Graph, b: NodeId, max_iter: u32, tol: f64, ) -> NodeId

x = A⁻¹ · b via Jacobi-preconditioned CG. SPD only. Convergence dramatically faster than plain CG on ill- conditioned matrices where diag(A) captures most of the magnitude variation — typical for circuit MNA matrices with mixed-magnitude device parameters. The preconditioner is extracted from the CSR by the kernel; no separate input.

Source

pub fn transpose_values( &self, g: &mut Graph, col_idx_t: NodeId, row_ptr_t: NodeId, ) -> NodeId

Permute this tensor’s values into the values vector of Aᵀ. The transposed pattern (col_idx_t, row_ptr_t) is supplied as NodeIds — typically computed once via crate::csr_transpose_pattern and embedded as Op::Constant since the pattern is fixed across Newton iterations.

Source

pub fn cholesky_solve(&self, g: &mut Graph, b: NodeId) -> NodeId

x = A⁻¹ · b via direct sparse Cholesky for SPD A. Densifies into a dense buffer and calls LAPACK dpotrf + triangular solves. Mirror of solve (LU-based) but ½× factor cost and numerically more stable; only valid when A is SPD.

Source

pub fn lsqr_solve( &self, g: &mut Graph, b: NodeId, max_iter: u32, tol: f64, ) -> NodeId

x = argmin ||A·x - b||₂ via LSQR (Paige-Saunders 1982). Works for any A (square / over-determined / under-determined); returns the minimum-norm solution when A is rank-deficient or under-determined. VJP not implemented in v1.

Source

pub fn bicgstab_solve( &self, g: &mut Graph, b: NodeId, max_iter: u32, tol: f64, ) -> NodeId

x = A⁻¹ · b via BiCGSTAB for non-symmetric A. Same shape contract as CG/PCG (no explicit adjoint pattern needed — the kernel itself can solve Aᵀ·x = b via a flag, used by VJPs).

Source

pub fn ilu_pcg_solve( &self, g: &mut Graph, b: NodeId, max_iter: u32, tol: f64, ) -> NodeId

x = A⁻¹ · b via ILU(0)-preconditioned CG. SPD A required (same contract as CG/PCG). ILU is factored on each call — for static-pattern Newton loops the cost amortizes against the faster convergence vs. Jacobi-PCG.

Source

pub fn gmres_solve( &self, g: &mut Graph, b: NodeId, max_iter: u32, tol: f64, adjoint: &SparseTensor, ) -> NodeId

x = A⁻¹ · b via GMRES for non-symmetric A. Same transpose-triplet contract as solve_general. max_iter caps Krylov dimension; tol is the residual norm threshold.

Trait Implementations§

Source§

impl Clone for SparseTensor

Source§

fn clone(&self) -> SparseTensor

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for SparseTensor

Source§

impl Debug for SparseTensor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.