Abstract
In this paper, we introduce McTorch, a manifold optimization library for deep learning that extends PyTorch. It aims to lower the barrier for users wishing to use manifold constraints in deep learning applications, i.e., when the parameters are constrained to lie on a manifold. Such constraints include the popular orthogonality and rank constraints, and have been recently used in a number of applications in deep learning. McTorch follows PyTorch's architecture and decouples manifold definitions and optimizers, i.e., once a new manifold is added it can be used with any existing optimizer and vice-versa. McTorch is available at https://github.com/mctorch.
Introduction
In modern deep learning, model parameters are typically treated as elements of a standard Euclidean space, where they can take any arbitrary value. However, many specialized applications benefit from restricting parameters to specific mathematical structures known as smooth Riemannian manifolds. For instance, enforcing orthogonality in weight matrices (the Stiefel manifold) can prevent vanishing or exploding gradients, while symmetric positive definite constraints are essential in metric learning and computer vision.
Despite the theoretical benefits, a significant tooling gap has existed. Traditional manifold optimization toolboxes, such as Manopt or Pymanopt, are designed for standard nonlinear optimization and lack the GPU acceleration and automatic differentiation capabilities required for deep learning. Conversely, mainstream frameworks like PyTorch do not natively support manifold constraints. McTorch bridges this divide, providing a seamless interface for applying Riemannian geometry within the PyTorch ecosystem.
System Architecture and Implementation
McTorch is built as a specialized extension of PyTorch, mirroring its core architecture to provide a familiar experience for researchers and developers. The library introduces several key technical components to handle the complexities of differential geometry under the hood:
The Manifold Module
The torch.nn.manifold module introduces a base Manifold class that encapsulates essential geometric operations. Two critical operations include:
- Retraction (
retr): A mapping that projects a tangent vector back onto the manifold, ensuring that parameter updates remain within the constrained space. - Gradient Conversion (
egrad2rgrad): A function that transforms standard Euclidean gradients—calculated via backpropagation—into Riemannian gradients suitable for the specific manifold geometry.
Modified Parameter and Optimizer Classes
To support these operations, McTorch updates PyTorch’s native Parameter class to accept manifold types and dimensions. This modified class includes an rgrad attribute to store the Riemannian gradient. Furthermore, standard optimizers (such as SGD and Adagrad) have been adapted to recognize these manifold parameters. Because the library decouples manifold definitions from the optimization logic, any newly implemented manifold is immediately compatible with all existing optimizers.
Seamless Layer Integration
McTorch simplifies the user experience by integrating manifold support directly into standard neural network layers. Layers such as Linear, Conv1d, Conv2d, and Conv3d are updated with a weight_manifold property. This allows a user to constrain a layer's weights simply by passing an argument during initialization, such as weight_manifold=nn.Stiefel().
Key Insights
The primary contribution of McTorch is the abstraction of complex mathematics. By automating the conversion of gradients and the projection of updates, it allows researchers to leverage Riemannian optimization without requiring expertise in differential geometry. The "plug-and-play" architecture ensures that manifold-constrained training feels identical to standard PyTorch training loops, maintaining high developer productivity while enabling more sophisticated model constraints.
Current Status and Future Work
At present, McTorch supports foundational manifolds like the Stiefel (orthonormal columns) and Positive Definite manifolds, alongside optimizers like SGD, Adagrad, and Conjugate Gradient. While the library currently focuses on these high-impact use cases, future development aims to expand the library’s library of manifolds and optimization algorithms. The authors are also focused on developing a comprehensive suite of benchmarks to further demonstrate the empirical advantages of manifold optimization across diverse deep learning tasks.
Summary updated