Sparse Tensors
Thetorch.sparse module provides support for sparse tensors in multiple formats including COO, CSR, CSC, BSR, and BSC layouts.
Sparse Tensor Formats
PyTorch supports several sparse tensor formats:- COO (Coordinate format): Stores indices and values separately
- CSR (Compressed Sparse Row): Row-compressed format
- CSC (Compressed Sparse Column): Column-compressed format
- BSR (Block Sparse Row): Block-based row-compressed format
- BSC (Block Sparse Column): Block-based column-compressed format
Construction
Creating COO Tensors
2D tensor of shape
(ndim, nnz) containing indices of non-zero elements.1D tensor of shape
(nnz,) containing values.Size of the sparse tensor. If None, inferred from indices.
Creating CSR Tensors
Compressed row indices.
Column indices.
Values of non-zero elements.
Conversion
to_sparse
Desired sparse layout. Options:
torch.sparse_coo, torch.sparse_csr, etc.Block size for block sparse formats (BSR/BSC).
to_dense
Whether to mask gradients by the sparsity pattern.
Arithmetic Operations
addmm
mat + alpha * (mat1 @ mat2) * beta.
Dense matrix to be added.
Sparse matrix (COO or CSR) to be multiplied.
Dense matrix to be multiplied.
Multiplier for
mat.Multiplier for the matrix product.
mm
Sparse matrix (first operand).
Dense or sparse matrix (second operand).
Reduction operation for duplicate indices. Options:
'sum', 'mean', 'amax', 'amin'.sampled_addmm
input.
Sparse CSR tensor defining the sampling pattern.
Dense matrix of shape
(m, k).Dense matrix of shape
(k, n).Reduction Operations
sum
Sparse tensor to sum.
Dimension(s) to reduce. If None, reduce all dimensions.
Desired data type of output.
softmax
Sparse tensor input.
Dimension along which to apply softmax.
log_softmax
Linear Algebra
spsolve (Sparse Solver)
Sparse CSR coefficient matrix of shape
(n, n).Dense right-hand side of shape
(n,).Whether to solve
input @ out = other (True) or out @ input = other (False).Utility Functions
spdiags
diagonals along specified diagonals.
Matrix storing diagonals row-wise.
The diagonals to be set (0 = main diagonal, negative = below, positive = above).
Shape of the output tensor.
Desired layout. Options:
torch.sparse_coo, torch.sparse_csr, torch.sparse_csc.Example Usage
Best Practices
Choosing Sparse Format
Choosing Sparse Format
- COO: Best for construction and random access patterns
- CSR: Optimized for row-wise operations and matrix-vector products
- CSC: Optimized for column-wise operations
- BSR/BSC: Use for block-structured sparsity (e.g., in neural networks)
Performance Considerations
Performance Considerations
- Convert to CSR/CSC for repeated arithmetic operations
- Coalesce COO tensors before operations with
.coalesce() - Use
to_sparse()only when sparsity is significant (>90% zeros) - Batch sparse operations when possible
- Consider dense operations for small tensors or low sparsity
Gradient Support
Gradient Support
- COO @ Dense: Backward supported for both inputs
- CSR @ Dense: Backward supported for both inputs
- Sparse @ Sparse: Forward works, backward not supported
- Use
masked_grad=Trueinto_dense()to preserve sparsity pattern in gradients