Fast evaluation of spherical harmonics with sphericart

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

TL;DR

This paper introduces "sphericart," a new, highly efficient C++ library for calculating real-valued spherical harmonics and their derivatives. By reformulating the problem in terms of solid harmonics and using novel recursive relations, sphericart achieves significant speedups over existing methods, making it a valuable tool for fields relying on rotational symmetry, particularly in geometric machine learning for materials science.

What Problem Does This Paper Address?

Spherical harmonics (SH) are fundamental mathematical tools for describing functions with rotational symmetry on a sphere. They are widely used in fields ranging from physics and chemistry to computer graphics and signal processing. A growing application area is geometric machine learning, where SH are used as descriptors for atomic structures in molecules and materials, enabling the development of "rotationally equivariant" models.

However, computing SH and, crucially, their derivatives, can be computationally expensive and prone to numerical instabilities, especially when dealing with large numbers of points or high angular momentum (l) values. Existing methods often involve converting Cartesian coordinates to spherical coordinates, which can introduce poles and inaccuracies, particularly near the z-axis. Furthermore, efficiently computing derivatives alongside the SH themselves adds another layer of complexity. This paper addresses the need for a numerically stable, computationally efficient, and easy-to-use method for evaluating real-valued SH and their Cartesian derivatives.

Methods

The core innovation of this paper lies in the reformulation of the spherical harmonic evaluation. Instead of directly computing the standard SH, the authors introduce "solid harmonics," which are homogeneous polynomials of Cartesian coordinates. This approach avoids the need for explicit normalization and circumvents the numerical issues associated with spherical coordinates.

Key methodological contributions include:

  • Solid Harmonics Formulation: The paper derives compact and stable recursive relations for these solid harmonics (denoted as $\hat{Y}_l^m$) and their derivatives directly in Cartesian coordinates. This is achieved by leveraging known recurrence relations for associated Legendre polynomials and carefully manipulating them.
  • Efficient Derivative Computation: A significant advantage is that the derivatives of the solid harmonics can be computed with minimal additional cost, reusing intermediate quantities from the SH evaluation. This is a major improvement over methods that require separate, potentially unstable, derivative calculations.
  • Real-Valued SH: The implementation focuses on real-valued SH, which are often preferred in practical applications. The standard (complex) SH can be recovered from the real-valued ones with a simple transformation.
  • Hard-coded Expressions: For low values of angular momentum (l), the authors use a computer algebra system to derive and implement hard-coded expressions. This exploits the fact that for small l, there are specific, highly optimized formulas that outperform general recursive methods.
  • Hybrid Approach: The library employs a hybrid strategy: it uses the efficient hard-coded expressions for low l and switches to the general recursive algorithm for higher l, balancing performance and generality.
  • Parallelization and GPU Acceleration: The implementation is designed for parallel execution on CPUs using OpenMP and also includes a CUDA kernel for GPU acceleration, further boosting performance for large-scale computations.

Datasets

Not discussed in the paper. The paper focuses on the algorithmic development and benchmarking of the sphericart library itself, rather than its application to specific datasets.

Key Insights and Ideas

  • Solid Harmonics are Key: The shift from standard spherical harmonics to solid harmonics (polynomials of Cartesian coordinates) is the central insight. This elegantly sidesteps numerical issues related to spherical coordinates and simplifies derivative calculations.
  • Derivatives are Cheap: The paper demonstrates that computing Cartesian derivatives of SH can be done with very little overhead by reusing intermediate results from the SH calculation itself. This is a crucial finding for applications that require both function values and gradients.
  • Hard-coding for Speed: For low angular momentum values, pre-computed, hard-coded expressions offer substantial speedups. This highlights the trade-off between generality and optimization in numerical algorithms.
  • Practicality Matters: The development of a library with C, Python, and PyTorch APIs, including GPU support, underscores the importance of making advanced mathematical tools accessible and usable for researchers in various domains.

Key Results

The paper presents extensive benchmarking results demonstrating the efficiency of the sphericart library.

  • Significant Speedups: Compared to a general-purpose recursive algorithm, the hard-coded implementation in sphericart shows speedups of up to 4-5 times for evaluating SH and their derivatives for low $l_{max}$.
  • CPU Performance: On a CPU, sphericart achieves impressive parallel scaling. For $l_{max}=16$ and 16 OpenMP threads, the time per point is around 18.3 ns for SH and 164 ns for SH with derivatives (double precision).
  • GPU Performance: The CUDA implementation offers further acceleration. For $l_{max}=16$ and 100,000 points, the GPU achieves 5.2 ns/point for SH and 16.0 ns/point for SH with derivatives (double precision).
  • Comparison to e3nn: The authors report that sphericart is 10 to 40 times faster than the spherical harmonics implementation in the e3nn library, a popular choice for equivariant neural networks.
  • Single vs. Double Precision: For large $l_{max}$ and many data points, single-precision floating-point arithmetic can halve computation time on GPUs, though it can be slower for smaller computations.

Limitations and Future Work

  • Normalization Overhead: While the core implementation uses solid harmonics, the paper notes that converting to normalized spherical harmonics incurs a small overhead (5-10%) for both function values and derivatives. This is acceptable as the scaling can often be absorbed into radial terms.
  • Future Optimization: The authors acknowledge that the performance curves show some "irregular behavior," suggesting room for further optimization.
  • Broader Language Support: Future work will focus on extending the software library to other programming languages and frameworks.
  • New Hardware Platforms: Optimization for new hardware platforms and improved parallelism are also planned.

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 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 which 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 sphericart}},
  journal = {The Journal of Chemical Physics},
  year = {2023},
  doi = {10.1063/5.0156307},
}

Loading related content...

Figure