Differentiable Convex Optimization Layers

Akshay Agrawal, Brandon Amos, Shane T. Barratt, Stephen P. Boyd, Steven Diamond, J. Zico Kolter

TL;DR

This paper introduces a systematic method to treat complex convex optimization problems as standard, differentiable layers within deep learning frameworks like PyTorch and TensorFlow. By introducing a new mathematical grammar called Disciplined Parametrized Programming (DPP) and a standardized "Affine-Solver-Affine" (ASA) form, the authors enable researchers to backpropagate through optimization problems automatically and efficiently. This eliminates the need to manually derive gradients or canonicalize problems by hand.

The Challenge: Embedding Hard Constraints in Neural Networks

Deep learning models excel at pattern recognition but often struggle with tasks requiring hard constraints or specific mathematical structures, such as resource allocation or control logic. To bridge this gap, researchers have attempted to embed optimization problems directly into neural networks as "layers."

Historically, however, this has been a "black art." Existing tools were rigid, requiring users to manually transform their high-level problems into a specific "canonical form" (like a standard Quadratic Program) before they could be solved. This process is tedious, error-prone, and demands deep expertise in convex analysis. Furthermore, backpropagating through these solvers was often slow or numerically unstable. This paper aims to make differentiable optimization as accessible and easy to use as a standard ReLU or Convolutional layer.

Methods: Bridging High-Level Descriptions and Low-Level Solvers

The authors bridge the gap between high-level problem descriptions and low-level differentiable solvers through three key innovations:

  1. Disciplined Parametrized Programming (DPP): A new "grammar" for optimization. It is a subset of Disciplined Convex Programming (DCP) that restricts how parameters (like neural network weights) can interact with variables. This ensures that the mapping from parameters to the final problem data is always affine (linear plus a constant).
  2. Affine-Solver-Affine (ASA) Form: The authors prove that any problem following the DPP grammar can be decomposed into three distinct stages:
  3. An affine map from parameters to cone problem data.
  4. A cone solver (the "black box" that finds the solution).
  5. An affine map that retrieves the original variables from the solver's output.
  6. Implicit Differentiation: Rather than "unrolling" the solver's internal iterations—which is memory-intensive and slow—the authors use the Implicit Function Theorem on the solver's optimality conditions. This allows them to compute exact analytical gradients efficiently.

These methods were implemented in CVXPY 1.1, providing a seamless interface for PyTorch and TensorFlow 2.0.

Key Insights

  • Canonicalization is the Bottleneck: A major realization in this work was that the time spent converting a high-level problem into a solver-ready format was often the slowest part of the pipeline. By using the ASA form and sparse matrix multiplications, the authors achieved massive speedups in this "lowering" process.
  • Generality vs. Specificity: Previous differentiable layers (like OptNet) were specialized for Quadratic Programs (QPs). This work handles a much broader class of problems (e.g., Second-Order Cone Programs) while remaining competitive in speed with specialized solvers.
  • Inductive Bias: Differentiable optimization layers provide a powerful "inductive bias." In control tasks, for example, the model doesn't just "guess" an action; it solves an optimization problem that is guaranteed to respect physical constraints.

Key Results and Performance

The paper evaluates the methodology on algorithmic performance benchmarks and specific use-cases, including synthetic data poisoning and stochastic control.

  • Canonicalization Speedup: CVXPY 1.1 is roughly 12 times faster at canonicalizing problems than the previous version (1.0.23).
Canonicalization time comparison between CVXPY versions
  • Competitive Performance: For dense Quadratic Programs, the general-purpose layer is as fast as the specialized qpth solver. For sparse problems, it is roughly 5 times faster because it can exploit the problem's underlying structure.
  • Improved Control: In a stochastic control simulation, using the differentiable layer to learn policy parameters decreased the average cost by approximately 40% compared to a standard LQR baseline.
Average cost reduction over iterations in the stochastic control task
  • Ease of Use: The authors demonstrated that a complex optimization problem could be turned into a PyTorch layer in just a few lines of code, significantly lowering the barrier to entry for researchers.

Limitations and Future Work

While powerful, the framework is strictly limited to convex optimization. Non-convex problems, which frequently appear in certain physical simulations, cannot be handled directly. Additionally, even with efficient canonicalization, solving a full optimization problem in the forward pass is significantly slower than a standard neural network matrix multiplication. This latency may limit its use in real-time, high-frequency applications. Future work may involve interfacing with even faster, specialized solvers (like OSQP) to further reduce this forward-pass latency.

Summary updated

BibTeX
@inproceedings{agrawal2019differentiable,
  author = {Akshay Agrawal and Brandon Amos and Shane T. Barratt and Stephen P. Boyd and Steven Diamond and J. Zico Kolter},
  title = {{Differentiable Convex Optimization Layers}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2019},
  url = {https://openreview.net/forum?id=af7HTgkIeE},
}
Figure