Scorch: A Library for Sparse Deep Learning

Bobby Yan, Alexander J. Root, Trevor Gale, David Broman, Fredrik KjolstadarXiv 2024 · DOI

Introduction

As deep learning models scale toward trillions of parameters, traditional dense computation—where every element in a matrix is stored and processed—becomes prohibitively expensive in terms of memory and compute. Sparsity, the practice of storing and computing only non-zero elements, offers a potential solution. However, mainstream frameworks like PyTorch and JAX provide limited support for sparse operations, often restricted to basic formats like CSR or COO.

Researchers experimenting with novel sparse architectures, such as custom Mixture-of-Experts or Graph Neural Networks (GNNs), frequently face a "sparsity tax": the significant engineering effort required to hand-write and optimize complex C++ or CUDA kernels. Scorch addresses this gap by providing a high-performance library that seamlessly integrates sparse tensor computation into the PyTorch ecosystem.

Architecture and Implementation

Scorch acts as a compiler-driven layer on top of PyTorch. Its primary innovation is a specialized compiler stack that transforms high-level Python expressions into optimized CPU code.

Scorch API integration and runtime architecture.

As shown in the architecture diagram, Scorch intercepts PyTorch calls via a simple import scorch as torch statement. If the tensors involved are sparse, they are routed to the Scorch compiler; otherwise, they are passed back to the standard PyTorch runtime. This "everything is a tensor" philosophy allows users to utilize familiar tools like torch.einsum without learning a new programming model.

Technical Innovations

Scorch utilizes several key optimizations to achieve high performance without manual tuning:

  • Automatic Loop Ordering: In sparse mathematics, the iteration order of loops can drastically change computational complexity. Scorch employs a fast heuristic auto-scheduler to select the most efficient order (e.g., Gustavson vs. Inner Product) in milliseconds.
  • Sparse-Aware Tiling: While tiling improves cache locality in dense math, applying it blindly to sparse data can degrade performance due to expensive index searches. Scorch identifies specific "dense" loops within sparse operations that are safe to tile.
  • Format Inference: The library automatically determines the optimal storage format (CSR, CSC, or Dense) for output tensors based on input characteristics, relieving researchers of manual bookkeeping.
  • Operation Fusion: For operations like Sampled Dense-Dense Matrix Multiplication (SDDMM), Scorch fuses steps into a single kernel. This avoids materializing massive intermediate dense matrices, preventing the "asymptotic trap" that often slows down standard frameworks.
Examples of different loop orderings for sparse matrix multiplication handled by the Scorch auto-scheduler.

Experimental Evaluation

Scorch was evaluated across several domains using standard benchmarks, including the SuiteSparse Matrix Collection for fundamental kernels and various datasets for end-to-end tasks (Cora, MNIST, BigBird).

Key results include:

  • Graph Neural Networks (GCNs): Scorch achieved an average speedup of 2.08× over PyTorch Sparse, showing superior scaling on large datasets like OGBN-arXiv.
  • Sparse Autoencoders: The library delivered speedups between 1.42× and 5.78×, with the most significant gains observed in high-dimensional datasets like CelebA.
  • Sparse Transformers: Modest but steady gains of 1.05× to 1.24× were recorded for BigBird inference.
  • SDDMM Performance: On large-scale matrices, Scorch outperformed PyTorch by orders of magnitude by avoiding the materialization of dense intermediate results.

Limitations and Future Work

While Scorch provides significant performance gains, current limitations include a focus on CPU inference and a lack of Autograd support, meaning it cannot yet be used for training sparse models from scratch. Future development aims to lower the compiler to Triton or CUDA for GPU support and implement horizontal fusion across multiple lines of Python code to further enhance efficiency.

Summary updated

BibTeX
@misc{yan2024scorch,
  author = {Bobby Yan and Alexander J. Root and Trevor Gale and David Broman and Fredrik Kjolstad},
  title = {{Scorch: A Library for Sparse Deep Learning}},
  year = {2024},
  eprint = {2405.16883},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2405.16883},
}
Figure