A Language and Compiler View on Differentiable Programming

Fei Wang, Tiark RompfWorkshops @ ICLR 2018

Deep learning frameworks have historically forced developers to choose between usability and performance. Static computation graphs (like early TensorFlow) require defining a graph before execution, enabling aggressive compiler optimizations at the cost of a clunky API that breaks standard debugging tools. Conversely, dynamic graphs (like PyTorch) record operations on the fly, offering an intuitive, standard programming experience but sacrificing whole-program optimization.

This paper argues that this trade-off is unnecessary. By leveraging classic programming language (PL) concepts, the authors propose a system that provides a simple, "just-a-library" interface while compiling down to highly optimized, low-level code.

Core Methodology

The authors bridge the usability-performance gap using two powerful PL techniques:

1. Multi-Stage Programming (Staging)

Instead of manually constructing a graph object, the authors utilize staging via Scala's Lightweight Modular Staging (LMS). Developers write standard code, but by simply changing variable types (e.g., from Double to Rep[Double]), the compiler is instructed to "stage" the code. This generates an intermediate representation (IR) of the logic rather than executing it immediately. This IR can then be compiled into highly optimized C code or specialized hardware kernels.

2. Delimited Continuations

Traditional reverse-mode automatic differentiation (AD) relies on a "tape" or Wengert list to track operations for the backward pass. The authors replace this global data structure with delimited continuations (using shift and reset operators). By capturing the "rest of the program" as a function, gradient calculations are injected directly into the program's control flow.

Key Insights and Results

  • Graph Construction is Staging: The paper highlights that what static frameworks call "graph construction" is essentially a manual, often awkward form of multi-stage programming. With native language support for staging, the computation graph becomes invisible to the user.
  • Tape-Free AD: Using delimited continuations allows the backward pass to become a natural part of function execution. This elegantly handles complex control flow, such as while loops, without the need to unroll them into a static graph.
  • Optimized Code Generation: The system can take a high-level Scala function containing a while loop and automatically generate a flat, recursive C function for gradient computation. This generated code strips away the overhead typically associated with high-level deep learning libraries.
  • Dual-Purpose Code: Built on a compiler framework, the exact same source code can be run with normal types for standard debugging, or with "staged" types to generate high-performance production code. The authors also note that while their examples focus on scalars, the approach naturally scales to tensor linear algebra.

Limitations and Future Outlook

While the proposed architecture is elegant, it comes with practical challenges:

  • Language Requirements: The techniques rely heavily on sophisticated language features. Implementing this in Python—the dominant language of deep learning—would require significant under-the-hood engineering, such as custom decorators or bytecode manipulation.
  • Conceptual Complexity: While the resulting API is user-friendly, building and maintaining the core engine requires deep expertise in functional programming, as delimited continuations are notoriously difficult to master.
  • Scale: The paper presents a conceptual view and prototype. It does not evaluate performance on massive-scale distributed training, which remains a primary use case for industry-standard frameworks.

Ultimately, this research provides a compelling blueprint for the future of differentiable programming, suggesting that the next generation of deep learning tools should be built from the compiler up.

Summary updated

BibTeX
@inproceedings{wang2018language,
  author = {Fei Wang and Tiark Rompf},
  title = {{A Language and Compiler View on Differentiable Programming}},
  booktitle = {Workshops @ ICLR},
  year = {2018},
}
Figure