Embedded Code Generation With CVXPY

Maximilian Schaller, Goran Banjac, Steven Diamond, Akshay Agrawal, Bartolomeo Stellato, Stephen P. Boyd

Abstract

We introduce CVXPYgen, a tool for generating custom C code, suitable for embedded applications, that solves a parametrized class of convex optimization problems. CVXPYgen is based on CVXPY, a Python-embedded domain-specific language that supports a natural syntax (that follows the mathematical description) for specifying convex optimization problems. Along with the C implementation of a custom solver, CVXPYgen creates a Python wrapper for prototyping and desktop (non-embedded) applications. We give two examples, position control of a quadcopter and back-testing a portfolio optimization model. CVXPYgen outperforms a state-of-the-art code generation tool in terms of problem size it can handle, binary code size, and solve times. CVXPYgen and the generated solvers are open-source.

Introduction

Convex optimization is a workhorse in fields like robotics, finance, and signal processing. However, there is a major friction point between development and deployment. Researchers prefer using high-level "parser-solvers" like CVXPY in Python because they allow for natural mathematical syntax. But these tools are often too slow and resource-intensive for embedded systems—such as drones or medical devices—that require real-time performance, static memory allocation, and minimal binary sizes.

Existing solutions like CVXGEN helped bridge this gap by generating custom C code, but they have significant limitations: they often only support Quadratic Programs (QPs), aggressively "unroll" loops (leading to massive binary files), and struggle with high-dimensional parameters. CVXPYgen aims to provide the flexibility of CVXPY with the raw speed and efficiency required for embedded hardware.

Methodology: From Python to Embedded C

CVXPYgen leverages a concept called Disciplined Parametrized Programming (DPP). By enforcing specific rules on how parameters enter an optimization problem, the authors ensure that the mapping from user-defined parameters to the solver's internal format is always a simple linear (affine) transformation.

Comparison between standard parser-solver (a) and the CVXPYgen code generation workflow (b).

Key technical features include: * Code Generation: It generates "division-free" C code for the canonicalization (setup) and retrieval (output) steps. * Solver Integration: It currently interfaces with high-performance solvers like OSQP (for QPs), SCS, and ECOS (for Second-Order Cone Programs). * Sparse Matrix Support: Unlike older tools that unroll every operation, CVXPYgen uses sparse matrix-vector multiplications. This keeps the binary size small even for large problems. * Partial Updates: If only a few parameters change between solves, CVXPYgen only re-calculates the necessary parts of the problem, significantly boosting speed.

Experimental Evaluation

The authors evaluated CVXPYgen using two primary benchmarks:

  1. Quadcopter MPC: A Model Predictive Control (MPC) problem for a custom-built 14x14 cm drone. They tested various "horizon lengths" ($H$) ranging from 6 to 60 steps.
  2. Portfolio Optimization: A financial back-testing scenario using historical S&P 500 data from 2017–2019, testing portfolios containing between 10 and 100 different assets ($N$).

Results and Discussion

CVXPYgen demonstrates superior scalability and efficiency compared to previous tools. While CVXGEN failed at a horizon length of 18 in the drone test due to memory constraints, CVXPYgen easily handled a horizon of 60.

Solve time and binary size comparison between CVXGEN and CVXPYgen for the quadcopter MPC problem.

Key Findings: * Binary Size: CVXPYgen produces significantly smaller executables than CVXGEN, making it much more suitable for memory-constrained microcontrollers. * Speed vs. Python: In the portfolio optimization test, the generated C code was 6x faster than standard CVXPY for small problems and 2.5x faster for larger ones. * Real-World Deployment: The authors successfully ran the generated code on a quadcopter’s Intel Atom processor at 30 Hz to track a circular trajectory in real-time. * The "Break-Even" Point: It takes roughly 5,000 solves to justify the time spent generating and compiling the C code compared to standard Python execution. For applications like high-frequency trading or 30Hz drone control, this threshold is crossed in minutes.

Conclusion

CVXPYgen bridges the gap between high-level modeling and high-performance embedded execution. While currently focused on LPs, QPs, and SOCPs, it provides a robust path for deploying complex optimization models on hardware without dynamic memory allocation. Future work will involve expanding the library of supported solvers and further optimizing the C code for specific hardware architectures.

Summary updated

BibTeX
@article{schaller2022embedded,
  author = {Maximilian Schaller and Goran Banjac and Steven Diamond and Akshay Agrawal and Bartolomeo Stellato and Stephen P. Boyd},
  title = {{Embedded Code Generation With CVXPY}},
  journal = {IEEE Control. Syst. Lett.},
  year = {2022},
  doi = {10.1109/lcsys.2022.3173209},
}
Figure