Abstract
There are many ways to represent a molecule as input to a machine learning model and each is associated with loss and retention of certain kinds of information. In the interest of preserving three-dimensional spatial information, including bond angles and torsions, we have developed libmolgrid, a general-purpose library for representing three-dimensional molecules using multidimensional arrays. This library also provides functionality for composing batches of data suited to machine learning workflows, including data augmentation, class balancing, and example stratification according to a regression variable or data subgroup, and it further supports temporal and spatial recurrences over that data to facilitate work with recurrent neural networks, dynamical data, and size extensive modeling. It was designed for seamless integration with popular deep learning frameworks, including Caffe, PyTorch, and Keras, providing good performance by leveraging graphical processing units (GPUs) for computationally-intensive tasks and efficient memory usage through the use of memory views over preallocated buffers. libmolgrid is a free and open source project that is actively supported, serving the growing need in the molecular modeling community for tools that streamline the process of data ingestion, representation construction, and principled machine learning model development.
Introduction
In drug discovery and computational chemistry, the choice of molecular representation is a critical factor in model performance. While 1D text strings (SMILES) or 2D graphs are computationally efficient, they often discard vital 3D spatial information, such as bond angles, torsions, and the precise geometric fit of a ligand within a protein pocket. Representing molecules as 3D voxel grids—essentially 3D pixels—preserves this spatial context, but introduces significant computational overhead.
Generating these grids on the fly during training is an "embarrassingly parallel" task, yet it has historically been a bottleneck when performed on CPUs. Furthermore, chemical datasets present unique challenges: they are frequently imbalanced and require extensive 3D data augmentation (rotations and translations) to prevent models from learning biases based on initial orientations. libmolgrid was developed to address these challenges by providing a high-performance, GPU-accelerated pipeline for molecular gridding.
Library Architecture
libmolgrid is a C++ library with Python bindings designed to handle the heavy lifting of molecular data preparation. Its architecture is centered around three core modules:
- ExampleProvider: This module manages data ingestion. It handles shuffling, oversampling of underrepresented classes (class balancing), and "stratification." Stratification ensures that training batches are sampled evenly across different protein targets or regression ranges, preventing the model from "cheating" by memorizing specific protein scaffolds.
- GridMaker: The engine of the library,
GridMakerconverts atomic coordinates into multidimensional arrays. It leverages GPU acceleration to represent atom densities as Gaussian distributions. It also performs real-time 3D rotations and translations, providing essential data augmentation without slowing down the training loop. - Grids and ManagedGrids: These are the primary data structures.
ManagedGridshandle their own memory allocation and can migrate data between CPU and GPU.Gridsact as "views" over existing memory, enabling a zero-copy approach when passing data to deep learning frameworks.
Efficient Memory Management and Interoperability
A key innovation of libmolgrid is its framework interoperability. By utilizing memory views, the library can treat a PyTorch tensor or a NumPy array as its own internal grid. This eliminates the need for redundant and slow memory transfers between the CPU and GPU when moving data from the gridding library to the neural network.
As shown in the figure above, ManagedGrid objects (a) manage their own buffers and facilitate explicit transfers, while Grid objects (b) can be constructed as views over pre-existing memory from other libraries. This design ensures that the GPU never has to send data back to the CPU just to move it between the gridding library and the training framework.
Performance and Key Results
The library demonstrates a substantial performance advantage when integrated with Caffe or PyTorch. On a Titan V GPU, training a simple Convolutional Neural Network (CNN) with libmolgrid was significantly faster than using Keras with a TensorFlow 1.x backend. The authors identified a performance penalty in TensorFlow 1.x due to its requirement that grids be copied into a NumPy array on the CPU before being sent back to the GPU for training—a bottleneck that the zero-copy design of libmolgrid specifically avoids.
Beyond simple classification, the library supports differentiable gridding. By including a backward function that computes atomic gradients, researchers can visualize what a network has learned or optimize molecular coordinates to fit a model's predictions. As a proof of concept, the authors trained a CNN to perform "Cartesian Reduction"—predicting original 3D coordinates from voxelized density. The model achieved a root mean squared accuracy of 0.09Å, which is significantly finer than the 0.5Å grid resolution used.
Conclusion and Future Work
libmolgrid provides a robust solution for the computational bottlenecks of 3D molecular deep learning. While current performance is optimized for Caffe and PyTorch, future updates aim to leverage TensorFlow 2.0 to mitigate existing data migration overheads. Additionally, the development team plans to expand the library's capabilities to include non-Cartesian coordinate systems, such as spherical grids, to better accommodate molecular symmetries.
Summary updated