Fast evaluation of spherical harmonics with <tt>sphericart</tt>

Filippo Bigi, Guillaume Fraux, Nicholas J. Browning, Michele Ceriotti

TL;DR

This paper introduces sphericart, a new C++ library for efficiently computing real-valued spherical harmonics and their derivatives directly in Cartesian coordinates. This approach offers numerical stability and computational speedups, making it particularly valuable for modern geometric machine learning applications in fields like materials science and chemistry.

What Problem Does This Paper Address?

Spherical harmonics are fundamental tools for describing functions with rotational symmetry on a sphere, finding applications across physics, chemistry, signal processing, and computer graphics. In recent years, they've become crucial for building rotationally equivariant machine learning models, especially for tasks like atomic-scale modeling of molecules and materials. However, traditional methods for evaluating spherical harmonics and their derivatives often suffer from numerical instabilities, particularly near the poles, and can be computationally expensive. This paper addresses the need for a robust, efficient, and user-friendly algorithm and implementation for calculating these essential mathematical functions and their gradients in Cartesian coordinates.

Methods

The core innovation lies in reformulating the evaluation of spherical harmonics and their derivatives in terms of "scaled" or "solid" harmonics. These are homogeneous polynomials of Cartesian coordinates, which avoid the normalization issues and numerical poles associated with traditional spherical coordinate-based methods. The authors derive elegant and stable recurrence relations for these scaled harmonics and their derivatives. A key advantage is that the derivatives can be computed with minimal additional cost, reusing intermediate results from the harmonic calculations. The implementation, sphericart, leverages these derivations to offer:

  • Cartesian-based evaluation: Directly uses (x, y, z) coordinates, avoiding costly spherical coordinate conversions.
  • Numerical stability: Eliminates poles and instabilities present in some traditional methods.
  • Efficient derivative computation: Integrates derivative calculation seamlessly with harmonic evaluation.
  • Optimized implementation: Utilizes hard-coded expressions for low angular momentum orders (l) and general recursive formulas for higher orders, balancing performance and generality.
  • Multi-platform support: Provides a C++ library with C bindings, a Python API, and a PyTorch implementation with GPU acceleration (CUDA).

Datasets

Not discussed in the paper. The paper focuses on the algorithmic and implementation aspects of spherical harmonic evaluation, not on training machine learning models on specific datasets.

Key Insights and Ideas

  • The power of solid harmonics: Shifting from normalized spherical harmonics to solid harmonics (scaled by $r^l$) in Cartesian coordinates is the key to achieving both numerical stability and computational efficiency.
  • Derivatives as a natural byproduct: The derived recurrence relations for scaled harmonics allow for the computation of their Cartesian derivatives with very little overhead, reusing intermediate calculations. This is crucial for applications like force calculations in molecular dynamics.
  • Bridging theory and practice: The paper not only presents the mathematical derivations but also provides a highly optimized, multi-platform library (sphericart) that makes these advancements readily accessible to researchers.
  • Hard-coding for speed: For low orders of spherical harmonics, using pre-computed, hard-coded expressions offers significant speedups compared to general recursive algorithms, a common optimization strategy in scientific computing.

Key Results

The paper demonstrates substantial performance improvements with sphericart compared to existing methods. Key quantitative results include:

  • Speedups: Benchmarking shows sphericart to be 10-40 times faster than the spherical harmonics implementation in e3nn (a popular library for equivariant neural networks).
  • CPU Performance: Table I and II show significant speedups for both computing spherical harmonics and their derivatives, with parallelization over threads further enhancing performance. For example, computing Ym and its derivatives up to $l_{max}=16$ takes 22.2 ns/point with hard-coded expressions on a CPU.
  • GPU Performance: Table III highlights the efficiency of the CUDA implementation, achieving times as low as 0.3 ns/point for $l_{max}=1$ and 10k points. For larger $l_{max}$ (e.g., $l_{max}=16$), the GPU significantly outperforms the CPU, achieving 5.2 ns/point for double precision.
  • Scalability: Figure 1 illustrates good parallel scaling on CPUs with increasing numbers of threads.

Limitations and Future Work

  • Normalized Spherical Harmonics: While the primary focus is on scaled harmonics, the paper mentions providing an implementation for normalized spherical harmonics, noting a small performance overhead (5-10%) for this conversion.
  • Hard-coded Limit: The hard-coded implementation is limited to $l_{max}=6$ for CPU and $l_{max}=3$ for GPU in the CUDA kernel. For higher $l_{max}$, the library switches to a general recursive implementation.
  • Future Work: The authors plan to extend sphericart to support more programming languages and frameworks, further optimize it for new hardware platforms, and improve parallelism. They also suggest exploring alternative recursive formulas to potentially simplify expressions for derivatives.

Summary updated

Abstract

Spherical harmonics provide a smooth, orthogonal, and symmetry-adapted basis to expand functions on a sphere, and they are used routinely in physical and theoretical chemistry as well as in different fields of science and technology, from geology and atmospheric sciences to signal processing and computer graphics. More recently, they have become a key component of rotationally equivariant models in geometric machine learning, including applications to atomic-scale modeling of molecules and materials. We present an elegant and efficient algorithm for the evaluation of the real-valued spherical harmonics. Our construction features many of the desirable properties of existing schemes and allows us to compute Cartesian derivatives in a numerically stable and computationally efficient manner. To facilitate usage, we implement this algorithm in sphericart, a fast C++ library that also provides C bindings, a Python API, and a PyTorch implementation that includes a GPU kernel.
BibTeX
@article{bigi2023fast,
  author = {Filippo Bigi and Guillaume Fraux and Nicholas J. Browning and Michele Ceriotti},
  title = {{Fast evaluation of spherical harmonics with <tt>sphericart</tt>}},
  journal = {The Journal of Chemical Physics},
  year = {2023},
  doi = {10.1063/5.0156307},
}

Loading related content...

Figure