torsh-sparse
Sparse tensor operations for ToRSh, leveraging scirs2-sparse for efficient sparse matrix computations.
Status: Stable for CPU sparse formats, operations, neural-network layers, and linear algebra;
GPU/cuSPARSE acceleration is a documented placeholder, not yet functional (see "GPU Acceleration"
below). Tests: 259/259 passing (cargo nextest run --all-features).
Overview
This crate provides comprehensive sparse tensor support:
- Sparse Formats: COO, CSR, CSC, BSR, DIA, DSR, ELL, RLE, and Symmetric formats
- Operations: Sparse matrix multiplication, addition, elementwise ops, transpose, softmax
- Conversions: Dense to sparse, format conversions
- GPU Scaffolding:
CudaSparseTensor/CudaSparseOpstypes exist but are currently placeholders (see "GPU Acceleration" below) — there is no working cuSPARSE backend yet - Integration: Seamless integration with scirs2-sparse
All of the free functions below are called directly on the crate root / torsh_sparse::ops /
torsh_sparse::linalg paths shown — there is no sparse:: namespace in this crate.
Usage
Creating Sparse Tensors
use ;
use Shape;
use *;
// From COO format (row indices, column indices, values, shape)
let row_indices = vec!;
let col_indices = vec!;
let values = vec!;
let sparse_coo = new?;
// Or from (row, col, value) triplets
let coo2 = from_triplets?;
// From a dense tensor (any target format, values below `threshold` are dropped)
let dense = tensor!;
let sparse = sparse_from_dense?;
// From CSR format (row pointers, column indices, values, shape)
let row_ptr = vec!; // row pointers, len = rows + 1
let col_idx = vec!;
let csr_values = vec!;
let sparse_csr = new?;
Sparse Operations
use ops;
// Sparse-sparse matrix multiplication
let c = sparse_matmul?;
// Sparse-dense multiplication (SpMM): sparse @ dense -> dense
let dense_vector = ?;
let result = spmm?;
// Element-wise operations
let sum = element_add?;
let hadamard_product = sphadamard?;
// Transpose
let transposed = transpose?;
// ...or, equivalently, the inherent method:
let transposed = sparse_matrix.transpose?;
Format Conversions
// Convert between formats (methods on the SparseTensor trait)
let csr = sparse_coo.to_csr?;
let csc = sparse_coo.to_csc?;
// Convert to dense
let dense = sparse_tensor.to_dense?;
Advanced Sparse Operations
// Sparse linear algebra
use ;
// Incomplete Cholesky decomposition (for symmetric positive-definite matrices)
let cholesky = new?;
// Iterative solvers: Ax = b
// conjugate_gradient(matrix, b, x0, tol, max_iter) -> (x, iterations, residual)
let = conjugate_gradient?;
let = gmres?;
let = bicgstab?;
Sparse Neural Network Layers
use ;
// Sparse Linear layer: new(in_features, out_features, sparsity, use_bias)
let sparse_linear = new?; // 90% sparse
// Sparse Embedding
let sparse_embedding = new?;
// Graph Convolution (for GNNs): new(in_features, out_features, use_bias, add_self_loops, normalize)
let gcn = new?;
GPU Acceleration
torsh_sparse::gpu defines the shape of a future cuSPARSE-backed API
(CudaSparseTensor, CudaSparseOps, CudaSparseTensorFactory), but as of this release it is a
placeholder: CudaSparseTensor::is_cuda_available() always returns false, and operations like
spmm, spgemm, dense↔sparse GPU conversion, and batched ops all return
"not yet implemented" errors regardless of the cuda feature flag. Real cuSPARSE bindings are
not currently wired up (the cudarc dependency behind the optional cuda feature is not yet used
by this module). Treat this as a documented placeholder, not a working GPU backend, until this
note is updated.
Sparse Gradients
// Sparse optimizer for sparse gradients (new(lr, beta1, beta2, eps, weight_decay, amsgrad))
use SparseAdam;
let sparse_adam = new;
// Gradient accumulation for sparse tensors, keyed by tensor id
use SparseGradientAccumulator;
let mut grad_accumulator = new;
grad_accumulator.accumulate?;
Utilities
use ;
// Analyze sparsity
let stats = analyze_sparse_tensor?;
println!;
println!;
println!;
println!;
// Benchmark and compare all sparse formats for a given tensor
let comparison = compare_format_performance?;
Integration with SciRS2
This crate fully leverages scirs2-sparse for:
- Optimized sparse BLAS operations
- Efficient sparse matrix formats
- Hardware-accelerated sparse computations
- Advanced sparse linear algebra
Performance Tips
- Choose the right format for your access pattern
- Use CSR for row-wise operations, CSC for column-wise
- Consider hybrid formats for mixed access patterns
- Use batched operations when possible
- Profile different sparse formats for your use case (see
compare_format_performanceabove)
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.