$\bf{\Phi}_\textrm{Flow}$: Differentiable Simulations for PyTorch, TensorFlow and Jax

Philipp Holl, Nils ThuereyICML 2024

TL;DR

$\Phi_{\text{Flow}}$ is an open-source Python library that bridges the gap between deep learning frameworks and physical simulations. By providing high-level, differentiable abstractions for fluid dynamics and partial differential equations (PDEs), it allows researchers to write simulation code that works seamlessly across PyTorch, TensorFlow, and JAX without sacrificing performance or readability.

What Problem Does This Paper Address?

In the burgeoning field of "Scientific Machine Learning," researchers often need to combine neural networks with physical simulations. For example, you might want to train a network to control a robot in a fluid environment or solve an "inverse problem" where you guess the hidden properties of a material based on how heat moves through it.

The challenge is that standard machine learning (ML) libraries like PyTorch or JAX are designed for tensors and neural layers, not for physics. They lack built-in support for boundary conditions, differential operators (like Curl or Gradient), and complex linear solvers. Conversely, traditional physics simulators are rarely "differentiable," meaning you cannot easily use backpropagation to calculate how a change in the simulation's input affects the final output. This forces researchers to write low-level, specialized code that is difficult to debug, hard to reuse, and locked into a single ML backend.

Methods

The authors introduce $\Phi_{\text{Flow}}$ (PhiFlow), a toolkit designed to make writing a differentiable simulator as intuitive as building a neural network. Key technical innovations include:

  • Backend Agnosticism: Through an abstraction layer called $\Phi_{\text{ML}}$, the same simulation code can run on PyTorch, TensorFlow, JAX, or NumPy. Switching backends is often as simple as changing a single import line.
  • Dimensionality-Agnostic Code: Users can tag dimensions as "spatial." This allows a single function to handle 1D, 2D, or 3D simulations automatically, as the library infers the necessary operations based on the input shape.
Code snippet demonstrating a differentiable fluid simulation step in PyTorch using PhiFlow.
  • Differentiable Sparse Solvers: Many physics problems require solving linear systems ($Ax = b$). $\Phi_{\text{Flow}}$ includes solvers where the gradient can flow not just through the result, but through the matrix $A$ itself—a feature often missing in standard ML libraries.
  • Automatic Matrix Generation: Using "function tracing," the library can take a standard Python function and automatically build the corresponding sparse matrix for efficient computation.
  • Named Dimensions: Instead of tracking whether the "batch" size is the first or last dimension in a 4D tensor, $\Phi_{\text{Flow}}$ uses named dimensions (e.g., batch, spatial, instance), which drastically reduces "shape mismatch" errors and improves code readability.

Applications and Benchmarks

The paper focuses on the framework's utility across various physical domains rather than a single benchmark dataset. It demonstrates performance on several key tasks:

  • Inverse Heat Conduction: Reconstructing material composition from thermal snapshots. $\Phi_{\text{Flow}}$ elegantly handles the optimization loop required to fit the simulated final temperature to the observed data.
Inverse heat conduction problem solved using PhiFlow with JAX, showing the code and resulting temperature/conductivity fields.
  • Particle Image Velocimetry (PIV): Reconstructing fluid velocity fields from moving marker particles.
  • Fluid Simulation Learning: Training a U-Net to mimic an incompressible fluid solver.
  • Replicated Benchmarks: The authors re-implemented three famous experiments from other frameworks: Billiards (from DiffTaichi), Sphere Packing (from JAX-MD), and Wave Propagation (from NVIDIA Warp).

Key Insights and Ideas

  • The "Information-to-Code" Ratio: One of the most striking insights is how much boilerplate $\Phi_{\text{Flow}}$ eliminates. For the wave propagation example, $\Phi_{\text{Flow}}$ required only 14 lines of code compared to 165 lines in NVIDIA’s Warp framework.
  • LLM-Verified Readability: In a novel evaluation method, the authors used ChatGPT to explain their code versus code from other frameworks. The AI was significantly more accurate and concise when explaining $\Phi_{\text{Flow}}$ code, suggesting that the library’s abstractions align well with how humans (and models) conceptualize physics.
  • Differentiable Physics as a Layer: By making the simulator a differentiable "layer," the library enables "Solver-in-the-loop" training. This allows neural networks to learn to account for the long-term errors and "data shift" that typically plague hybrid ML-physics models.

Key Results

  • Performance Parity: Benchmarks across PyTorch, JAX, and TensorFlow showed that $\Phi_{\text{Flow}}$ introduces negligible overhead. JAX generally performed best for fluid tasks, while PyTorch excelled at highly parallelizable tasks.
  • Optimization Success: In the PIV experiment, the framework successfully reconstructed velocity fields, showing that Mean Squared Error (MSE) improved by an order of magnitude for every 8x increase in marker particles.
  • Training Stability: The U-Net trained to simulate fluids exhibited linear convergence, proving the gradients provided by the differentiable simulator were accurate and stable enough for deep learning.

Limitations and Future Work

While $\Phi_{\text{Flow}}$ is powerful, the authors note some areas for growth:

  • Mesh Support: While the library supports unstructured meshes, many of its high-level "out-of-the-box" features are currently optimized for regular grids.
  • Complexity of Physics: The library provides the building blocks for simulations. While it includes a fluid solver, users still need a solid understanding of numerical methods to build custom, stable simulators for more exotic physics.
  • Future Directions: The authors aim to continue expanding the library of built-in differential operators and further optimize the performance of the JAX backend for large-scale distributed simulations.

Summary updated

BibTeX
@inproceedings{holl2024bf,
  author = {Philipp Holl and Nils Thuerey},
  title = {{$\textbackslash\{\}bf\{\textbackslash\{\}Phi\}\_\textbackslash\{\}textrm\{Flow\}$: Differentiable Simulations for PyTorch, TensorFlow and Jax}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=4oD0tRrUOX},
}
Figure