TL;DR
ACORNS is a code generation tool that automatically produces highly optimized C99 code for first and second-order derivatives (gradients and Hessians). By unrolling computational graphs and leveraging modern compiler optimizations, it achieves speedups of up to 1336x for Hessian evaluations compared to deep learning libraries like PyTorch, making it a powerful tool for scientific computing and physical simulations.
What Problem Does This Paper Address?
Automatic Differentiation (AD) has revolutionized deep learning, but the needs of scientific computing—such as geometry processing and physical simulation—are different. While libraries like PyTorch and TensorFlow are optimized for large, dense tensors and first-order derivatives, scientific applications often require second-order derivatives (Hessians) for small to medium-sized tensors.
Existing AD approaches generally fall into two camps: Operator Overloading, which is easy to use but carries significant runtime overhead, and Source Transformation, which generates faster code but is often difficult to integrate into modern build systems. The authors introduce ACORNS to bridge this gap, providing a source-transformation tool that is easy to use, generates dependency-free C code, and excels at computing Hessians.
Methods
ACORNS takes an algorithm written in a subset of C99 as input and outputs self-contained, multi-threaded C99 functions for the original expression and its derivatives.
The pipeline works as follows:
- Parsing: It uses
pycparserto convert the input C code into an Abstract Syntax Tree (AST). - Unrolling: To maximize optimization opportunities, the tool unrolls loops and evaluates static conditionals, creating a flat sequence of assignment instructions.
- Symbolic Differentiation: It performs symbolic differentiation on the unrolled AST to generate the gradient and Hessian. To optimize the Hessian, it only computes the lower triangular part, leveraging the matrix's symmetry.
- Code Generation & Compilation: The resulting formulas are exported as C code. ACORNS relies on modern compilers (like
gccorclangwith-O3) to perform redundancy elimination, vectorization, and memory load/store optimizations. - File Splitting: For massive expressions (e.g., a cubic Lagrangian element), the generated code can reach gigabytes in size, crashing compilers. ACORNS introduces a "file-splitting" strategy, breaking the output into multiple C files that compile independently to keep build times manageable.
Datasets
The authors evaluated ACORNS using three synthetic benchmarks and two real-world applications:
- Synthetic: A high-order polynomial, a scalar trigonometric function, and a vector-valued polynomial with variable parameters ($s$).
- Mesh Parametrization: Minimizing the symmetric Dirichlet energy for surface flattening.
- Finite Element Simulation: Simulating Neo-Hookean elastic deformations using the PolyFEM library.
Key Insights and Ideas
- The Power of Unrolling: By fully unrolling the computational graph, ACORNS gives the C compiler a global view of the mathematical operations. This allows the compiler to perform much more aggressive optimizations than are possible with operator-overloading AD libraries.
- Hessians as First-Class Citizens: Unlike many AD tools where second derivatives are an afterthought or computationally prohibitive, ACORNS is designed specifically to make dense Hessian computation efficient for small-to-medium tensors.
- Practical Build Integration: The authors provide a seamless integration path for CMake, treating the AD process as a standard code-generation step in the build pipeline.
- Compilation vs. Runtime Trade-off: ACORNS trades longer compilation times for significantly faster execution. The "file-splitting" technique is a critical practical innovation that prevents this trade-off from becoming a bottleneck for large-scale problems.
Key Results
- Speed: For gradients, ACORNS is roughly 5x faster than Mitsuba/Enoki and 3x faster than PyTorch.
- Hessian Performance: The performance gap widens significantly for second-order derivatives. ACORNS is 12.8x faster than Mitsuba and a staggering 1336x faster than PyTorch (which is not optimized for Hessians).
- Real-World Impact: In a Neo-Hookean elastic simulation, replacing Mitsuba with ACORNS resulted in an overall simulation speedup of 2x to 4x, depending on the finite element degree.
- Parallelization: The generated code scales almost linearly up to 15 threads using OpenMP before hitting memory bandwidth limits.
Limitations and Future Work
- C99 Subset: ACORNS currently only supports a subset of C99. It cannot handle variable-length loops or conditionals that depend on input data (unbounded loops).
- Compilation Time: For extremely large expressions, compilation remains slow, even with file splitting. The authors suggest future work could involve generating LLVM bytecode directly to bypass the C compiler's parsing stage.
- Sparsity: The current implementation focuses on dense Hessians. The authors aim to extend support to sparse Hessians and target GPU accelerators in future versions.
Summary updated