AD for an Array Language with Nested Parallelism

Robert Schenck, Ola Rønning, Troels Henriksen, Cosmin E. Oancea

Abstract

This paper introduces a novel compiler-based approach for reverse-mode automatic differentiation (AD) in Futhark, a functional array language designed for nested parallelism on GPUs. By replacing traditional memory-intensive "tapes" with a strategic redundant recomputation model and leveraging language-specific optimizations, the authors achieve highly efficient gradient computations. The system outperforms established frameworks like PyTorch on complex nested workloads and remains competitive with low-level tools like Enzyme.

Introduction

Reverse-mode automatic differentiation (AD) is the cornerstone of modern machine learning and optimization. However, existing AD frameworks often struggle with nested parallelism—where parallel operations like map are nested within loops or other parallel constructs. On GPUs, the standard approach of using a "tape" to store intermediate values for the backward pass leads to irregular memory access and high global memory consumption, which can bottleneck performance.

This work addresses these issues by integrating AD directly into the Futhark compiler. Futhark's purely functional nature and support for nested parallelism provide a unique environment for optimizing the AD process.

Methodology

The authors' approach centers on several key techniques designed to minimize memory overhead and maximize GPU utilization:

Redundant Execution vs. Taping

Instead of saving every intermediate state to global memory (a "tape"), the Futhark AD system uses redundant execution. For straight-line code, variables are kept in scope. For nested scopes, the backward sweep re-executes the forward sweep to recover necessary values into registers. This trades cheap computation for expensive memory bandwidth. Figure 1 illustrates these basic transformations.

Basic AD transformations for straight-line code: (a) original program, (b) forward-mode AD, and (c) reverse-mode AD.

Loop Transformations and Checkpointing

For iterative structures, the compiler employs a checkpointing strategy. Rather than taping every intermediate, it saves only the loop-carried state to a "scratch" array. This process is often combined with stripmining and loop distribution to create "perfectly nested" scopes where re-execution overhead is minimized or eliminated by the compiler's dead-code elimination passes. Figure 2 demonstrates the transformation of a loop into its AD-ready form.

Transformation of loops for reverse-mode AD, illustrating the checkpointing of loop-carried state and stripmining.

Parallel Combinators and Accumulators

The system includes specific rewrite rules for Second-Order Array Combinators (SOACs) such as map, reduce, and scan. To handle gradient updates to free variables within parallel maps without violating functional purity, the authors introduced accumulators. These act as write-only views that the compiler translates into efficient atomic updates or specialized reductions on the GPU.

Evaluation and Results

The implementation was evaluated using ADBench, RSBench, XSBench, and several custom case studies including K-means clustering and LSTM networks.

  • Competitive Performance: Futhark's AD outperformed Tapenade on 4 out of 5 ADBench workloads and achieved overheads comparable to Enzyme (3.6x vs 4.2x on RSBench).
  • Nested Parallelism Advantage: On complex nested workloads, Futhark significantly outperformed PyTorch. For Gaussian Mixture Model (GMM) Jacobians, Futhark was up to 7.3x faster. For LSTMs, it was roughly 3x faster.
  • Sparse Data Handling: Futhark handled sparse K-means clustering efficiently, whereas PyTorch's AD was over 400x slower or failed on certain sparse formats.

Discussion and Limitations

The key insight of this work is that for GPU-based AD, compute is often "freer" than memory. By killing the tape and relying on compiler-driven recomputation, Futhark achieves superior scaling for nested parallel programs.

However, some limitations remain. The current system does not support loop-variant parameters that change shape, and AD for while loops requires manual iteration bounds or dynamic inspectors. Future work will focus on improving control flow efficiency and expanding support for dynamic shapes.

Summary updated

BibTeX
@inproceedings{schenck2022ad,
  author = {Robert Schenck and Ola Rønning and Troels Henriksen and Cosmin E. Oancea},
  title = {{AD for an Array Language with Nested Parallelism}},
  booktitle = {SC},
  year = {2022},
  doi = {10.1109/sc41404.2022.00063},
}
Figure