DiffTaichi: Differentiable Programming for Physical Simulation

Yuanming Hu, Luke Anderson, Tzu-Mao Li, Qi Sun, Nathan Carr, Jonathan Ragan-Kelley, Frédo Durand

Introduction

In robotics and computer graphics, optimizing a physical system—such as finding the optimal muscle actuations for a soft robot to walk—is a notoriously challenging problem. Traditionally, researchers rely on Reinforcement Learning (RL), which treats the underlying physics as a "black box." While flexible, RL is highly sample-inefficient, often requiring millions of trials to converge.

If we instead use a differentiable simulator, we can leverage gradient-based optimization (similar to backpropagation in neural networks) to find solutions orders of magnitude faster. However, existing tools force a harsh "performance-productivity" trade-off:

  1. Deep Learning Frameworks (TensorFlow/PyTorch): Easy to use but painfully slow for physics. They lack "megakernels" (fused operations), leading to excessive memory overhead and inefficient GPU kernel launches.
  2. Hand-written CUDA: Incredibly fast but a nightmare to write and maintain. Manually deriving and coding the "adjoint" (gradient) version of a complex fluid simulator takes months of engineering and is highly prone to errors.

DiffTaichi bridges this gap. It is a new differentiable programming language designed to build high-performance physical simulators that play nicely with gradient-based optimization, achieving the raw speed of hand-engineered CUDA code with significantly less effort.

Methods: Two-Scale Automatic Differentiation

DiffTaichi is an imperative language embedded in Python but compiled to high-performance CPU/GPU code. Its architecture relies on a novel two-scale automatic differentiation (AD) system:

DiffTaichi compiler architecture and tape system
  • Local AD (Inside Kernels): DiffTaichi uses Source Code Transformation (SCT). It takes imperative code (loops, if statements, etc.), flattens the logic, and automatically generates a corresponding "gradient kernel." This allows for the creation of "megakernels" where complex physics steps are fused into a single, high-arithmetic-intensity operation.
  • Global AD (Across Time Steps): A lightweight "tape" records the order of kernel launches during the forward simulation. To calculate gradients, the system simply replays these kernels in reverse order.
  • Decoupled Data Layout: Inherited from the original Taichi language, DiffTaichi allows programmers to change how data is stored in memory (e.g., switching from a dense array to a sparse structure) without rewriting the core simulation logic.

Evaluated Simulators

Rather than using a static dataset, the authors evaluated DiffTaichi by building 10 diverse differentiable simulators, which now serve as a robust benchmark for the field.

Overview of the policy network architecture and the 10 physical simulators built with DiffTaichi

These simulators cover a wide range of physical phenomena:

  • Fluid Dynamics: smoke (incompressible flow), liquid (3D water), and wave (shallow water).
  • Deformable & Rigid Bodies: diffmpm (soft bodies), mass_spring, and rigid_body.
  • Others: billiards, electric (field optimization), and differentiable renderers (water_renderer, volume_renderer).

Key Insights

  • The "Megakernel" Advantage: The primary reason DiffTaichi outperforms frameworks like TensorFlow is "arithmetic intensity." By fusing many operations into one kernel, it keeps data in fast GPU registers rather than constantly moving it to and from main memory.
  • Gradients are not Magic: A simulator can be "correct" in the forward direction but produce useless gradients. A major contribution of this work is the Time-of-Impact (TOI) fix. In rigid body collisions, naive time-stepping creates "saw-tooth" gradients that point the wrong way. By explicitly calculating the exact moment of impact, DiffTaichi produces smooth, physically meaningful gradients.
  • Imperative is Intuitive: Most physicists and engineers think in terms of loops and state updates. DiffTaichi allows them to write code that looks like standard C++ or Fortran while seamlessly getting the benefits of automatic differentiation.

Results and Performance

DiffTaichi demonstrates massive improvements in both speed and developer productivity:

  • Speed: In the diffmpm (elastic object) benchmark, DiffTaichi was 188x faster than TensorFlow and matched the performance of hand-written CUDA.
  • Productivity: The DiffTaichi version of the same simulator was 4.2x shorter (in lines of code) than the CUDA version.
  • Optimization Efficiency: Using DiffTaichi, neural network controllers for robots were typically optimized within tens of iterations. In contrast, model-free RL algorithms often converge orders of magnitude slower.
  • Versatility: The system successfully handled complex, diverse tasks ranging from fooling a VGG-16 image classifier using water refraction to reconstructing 3D objects from 2D "X-ray" photos.

Limitations and Future Work

While powerful, differentiable simulation with DiffTaichi has a few limitations:

  • Memory Consumption: To perform backpropagation, the system must store the history of the simulation. For long, high-resolution simulations, this can exhaust GPU memory. The authors suggest checkpointing (storing only some states and recomputing others) as a future solution.
  • Discontinuities: While the TOI fix helps, rigid body physics is fundamentally full of discontinuities (e.g., a ball either hits a corner or it doesn't). These "cliffs" in the optimization landscape can still trap gradient descent in local minima.
  • Numerical Stability: Gradients of certain physical laws (like the $1/r^2$ force in electrostatics) can become unstable when objects get too close together, requiring careful "safeguarding" of the code.

Summary updated

BibTeX
@inproceedings{hu2020difftaichi,
  author = {Yuanming Hu and Luke Anderson and Tzu-Mao Li and Qi Sun and Nathan Carr and Jonathan Ragan-Kelley and Frédo Durand},
  title = {{DiffTaichi: Differentiable Programming for Physical Simulation}},
  booktitle = {International Conference on Learning Representations},
  year = {2020},
  url = {https://openreview.net/forum?id=iZW9YKCNwT_},
}
Figure