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.
Introduction
Spherical harmonics ($Y_l^m$) are fundamental mathematical components used to describe functions on a sphere across diverse scientific fields. Recently, they have become a cornerstone of geometric deep learning, enabling rotationally equivariant models to represent the 3D orientation of atoms in molecules and materials.
Traditional evaluation methods typically convert Cartesian coordinates ($x, y, z$) into spherical coordinates ($\theta, \phi$). This approach suffers from numerical instability near the $z$-axis and significant computational overhead due to expensive trigonometric function calls. In machine learning applications requiring gradients for atomic force calculations, these standard recurrence relations become a performance bottleneck.
Methodology
The researchers behind sphericart address these issues by shifting the calculation to solid harmonics ($\tilde{Y}_l^m = r^l Y_l^m$). Solid harmonics are homogeneous polynomials of $x, y, z$, allowing for new recurrence relations that are entirely polynomial-based. This eliminates trigonometric calls and avoids numerical singularities at the poles.
Key features of the sphericart implementation include:
- Efficient Derivatives: Cartesian derivatives are expressed simply, reusing values from the forward pass to minimize redundant calculations.
- Hybrid Evaluation: For low-order harmonics ($l \leq 6$), the library utilizes hard-coded expressions generated by a computer algebra system to minimize multiplications. Higher orders switch to an optimized general recursion.
- Cross-Platform Support: The library is implemented in C++ with custom CUDA kernels for GPU acceleration, integrated with PyTorch.
Performance Evaluation
Benchmarks demonstrate that sphericart is 10 to 40 times faster than existing libraries like e3nn. As shown in Table 1, on a single CPU thread, evaluating harmonics up to $l=6$ takes approximately 22 ns per point using the hard-coded version, nearly twice as fast as the general recursive approach.
GPU performance is equally impressive. On an NVIDIA A100, the library processes 100,000 points in double precision at rates between 5 and 20 ns per point, depending on the maximum degree $l$. Switching to single-precision arithmetic further halves computation time for large-scale problems.
Scalability and Limitations
While sphericart offers significant speedups, the authors observed some non-linear behavior in parallel scaling on CPUs.
As illustrated in Figure 1, while performance generally improves with more threads, the scaling curves exhibit irregularities. This suggests potential for further micro-architectural optimizations related to memory handling and cache utilization. Future development will focus on extending framework support, further optimizing GPU kernels, and implementing higher-order derivatives.
Conclusion
By leveraging polynomial-based solid harmonics and optimized implementations, sphericart provides a high-performance solution for evaluating spherical harmonics. Its integration into geometric deep learning workflows offers a substantial reduction in computational cost without sacrificing numerical stability.
Summary updated