Code generation for solving and differentiating through convex optimization problems

Maximilian Schaller, Stephen BoydOptimization 2025

TL;DR

This paper introduces CVXPYgen, a code generator that creates specialized C code for solving convex optimization problems and efficiently computing the derivatives of the solution with respect to problem parameters. This significantly speeds up computations compared to general-purpose tools, enabling faster tuning and optimization in various applications.

What Problem Does This Paper Address?

Many real-world applications, such as machine learning, control systems, and finance, involve solving convex optimization problems repeatedly. Often, these problems are parameterized, meaning their structure or data depends on certain parameters. To optimize these systems, it's crucial to understand how the solution of the optimization problem changes when these parameters are adjusted. This is known as differentiating through the optimization problem.

Existing methods for this often rely on general-purpose automatic differentiation tools that treat the optimization solver as a black box. While functional, these methods can be slow because they don't leverage the specific mathematical structure of convex optimization problems. This paper addresses the need for a more efficient and specialized approach by developing a code generator that produces highly optimized C code for both solving and differentiating these problems.

Methods

The core contribution is the extension of CVXPYgen, a code generator for convex optimization problems. The key innovation is the addition of a custom C implementation for differentiating the solution with respect to problem parameters. This is achieved by:

  • Leveraging Convexity: Instead of treating the optimization solver as a black box, CVXPYgen exploits the optimality conditions of convex problems (specifically, Quadratic Programs - QPs) to derive analytical expressions for the gradients.
  • Efficient Linear System Solvers: The method involves solving a linear system derived from the KKT conditions of the QP. To speed this up, they employ low-rank updates to factorizations of the relevant matrices, avoiding costly full re-factorizations when only a few constraints change their active status.
  • Code Generation: CVXPYgen generates specialized C code for the specific convex optimization problem family. This compiled code is significantly faster than interpreted solutions.
  • Python Wrapper: A Python interface is provided for ease of use and integration with existing Python workflows.

Datasets

The paper does not introduce new datasets. Instead, it uses synthetic data generated for the specific examples to demonstrate the effectiveness of the proposed method. The data generation processes for each example are described in detail within the respective sections.

Key Insights and Ideas

  • Exploiting Structure for Speed: The most significant insight is that by exploiting the mathematical structure of convex optimization problems (specifically QPs), one can derive much faster gradient computations than general-purpose autodifferentiation.
  • Code Generation as a Performance Booster: Generating specialized C code for a specific problem family offers substantial performance gains, especially for repeated solves and gradient computations.
  • Low-Rank Updates for Dynamic Constraints: The ability to efficiently update matrix factorizations using low-rank updates is crucial for handling problems where constraint activity changes between iterations, a common scenario in optimization-based systems.
  • Bridging the Gap: CVXPYgen effectively bridges the gap between high-level modeling languages like CVXPY and the need for high-performance, deployable code in embedded or computationally intensive applications.

Key Results

The paper demonstrates significant speedups across three realistic examples:

  • Elastic Net Regression: Gradient computations were sped up by approximately 5x compared to CVXPYlayers.
  • Approximate Dynamic Programming Controller: Gradient computations saw a speedup of about 40x, with the overall tuning loop being about 6x faster.
  • Portfolio Optimization: Gradient computations were sped up by a factor of about 10x, and the overall tuning loop by about 3x.

These speedups are attributed to the efficient C code generation and the specialized gradient computation.

Limitations and Future Work

  • Focus on QPs: The current implementation of CVXPYgen for differentiation focuses on problems that can be reduced to Quadratic Programs (QPs), including Linear Programs (LPs). Extending this to more general conic programs would be a valuable future direction.
  • DPP Restrictions: While Disciplined Parametrized Programming (DPP) is used, it imposes some restrictions on how parameters enter the problem expressions (primarily affine mappings). Relaxing these restrictions could broaden applicability.
  • Python Overhead: In some cases, the overall speedup of the tuning loop is limited by Python overhead, suggesting further optimization of the Python interface or integration could be beneficial.

The authors suggest that their work enables new applications and further research into differentiable optimization layers and systems.

Summary updated

BibTeX
@inproceedings{schaller2025code,
  author = {Maximilian Schaller and Stephen Boyd},
  title = {{Code generation for solving and differentiating through convex optimization problems}},
  booktitle = {Optimization},
  year = {2025},
  doi = {10.1007/s11081-025-10057-2},
}
Figure