TL;DR
This paper introduces torch-sla, a new PyTorch library that brings differentiable sparse linear algebra to the platform. It offers a unified API for various sparse solvers (direct, iterative, nonlinear, eigenvalue) across multiple backends and devices, enabling end-to-end training for scientific machine learning tasks that were previously difficult or impossible in PyTorch.
What Problem Does This Paper Address?
Scientific machine learning and simulations often rely on solving large systems of linear equations with sparse matrices. Differentiating through these solves is crucial for tasks like inverse problem solving and learning physical parameters. However, PyTorch's native sparse support is limited: torch.sparse offers only low-level operations and a non-differentiable CPU solver, while torch.linalg is for dense matrices. This forces researchers to either drop gradients, use less efficient workarounds, or switch to other frameworks like JAX. torch-sla aims to fill this gap by providing a comprehensive, differentiable sparse linear algebra library within the PyTorch ecosystem.
Methods
torch-sla is built on three core components:
- Unified Backend Abstraction: A single API (
.solve(),.matvec(), etc.) is provided for four types of sparse tensors (single matrix, batched with shared/distinct patterns, distributed). This API automatically dispatches to one of five backends (SciPy, Eigen, cuDSS, CuPy, and a PyTorch-native iterative solver) based on device, problem size, and matrix properties. This abstraction is extensible to new backends. - Adjoint Differentiation Framework: The library implements an implicit-function-theorem (IFT) adjoint, which is a well-established technique for obtaining gradients of implicit functions. This approach ensures that the computational graph for backpropagation remains O(1) in the number of solver iterations, drastically reducing memory and time complexity compared to naive backpropagation through iterative solvers.
- Distributed Solvers with Halo Exchange: For problems exceeding single-device memory,
torch-slasupports domain decomposition with halo exchange. Crucially, the halo exchange mechanism is designed to be autograd-compatible, meaning the transposed halo exchange in the backward pass correctly propagates gradients across distributed processes.
Datasets
Not discussed in the paper. The paper focuses on synthetic benchmarks (e.g., 2D Poisson equation) and an inverse coefficient learning task.
Key Insights and Ideas
- The O(1) Graph for Adjoint Solves is a Game-Changer: The most significant insight is the application of the IFT adjoint to sparse linear solvers. This technique bypasses the need to store intermediate solver states, reducing memory requirements from O(k*n) to O(n + nnz) and making it feasible to differentiate through thousands of iterations of iterative solvers.
- Unified API for Heterogeneous Backends: Providing a single, consistent interface for diverse solvers (CPU/GPU, direct/iterative) and automatically dispatching based on problem characteristics is a major usability improvement. This allows users to leverage the best solver for their specific problem without manual intervention.
- Autograd-Compatible Distributed Solves: Extending the adjoint differentiation to distributed settings with halo exchange is a complex but critical achievement. It ensures that the benefits of differentiable sparse linear algebra are not lost when scaling to multi-GPU or multi-node systems.
- Bridging the PyTorch Sparse Ecosystem Gap: The paper highlights a clear deficiency in PyTorch's native sparse linear algebra capabilities and presents
torch-slaas a direct solution, making advanced scientific machine learning tasks more accessible within the PyTorch framework.
Key Results
- Memory Reduction: For a 1M-DOF problem with 1000 CG iterations, the adjoint backward pass reduced memory usage by 195x compared to naive backpropagation.
- Scalability:
torch-slascales to 169M DOF on a single H200 GPU and 400M DOF across three H200 GPUs. - Performance: On a 1M-DOF 2D Poisson problem, the
torch-slaCG backend achieved a speedup of 38x over cuDSS. - Gradient Accuracy: Gradients for linear, nonlinear, and eigenvalue problems were verified against naive autograd and finite differences, showing excellent agreement (relative errors below 10⁻⁵).
- End-to-End Demonstration: An inverse coefficient learning task successfully recovered a variable conductivity field with 0.23% relative L2 error in under a minute, demonstrating the practical usability of
torch-slain realistic training loops.
Limitations and Future Work
- Preconditioner Limitations: The PyTorch-native iterative solver currently only supports Jacobi preconditioning, which is insufficient for very large problems.
- Backend Extension: The authors plan to integrate mature distributed sparse libraries like PETSc, Trilinos, and hypre as backends.
- Learned Preconditioners: A significant future direction is to support learned preconditioners, enabling
torch-slato serve as a platform for training custom solvers. - 3D PDEs and Indefinite Systems: Broader validation on more complex problem types like 3D PDEs and indefinite systems is needed.
- Global Determinant: The distributed
detcomputation is not scalable and requires further work.
Summary updated