Every CVXPY user knows the moment: six lines of Python describing a portfolio optimizer or a regularized least-squares fit, prob.solve() returns a number, and nobody asks how. The question arrives later — when the compile step eats seconds on every solve, when a Parameter update triggers a recompile you did not budget for, or when a solver rejects a cone type you never mentioned. At that point you are debugging a compiler, not writing math, and the usual tutorials stop exactly where you need to start.

The first thing to internalize: CVXPY ships no numerical solvers of its own. It is a frontend that translates a Python expression tree into standard-form data — sparse A, b, c — for external backends such as Clarabel, SCS, ECOS, OSQP or HiGHS. The math lives in your expression tree; the engineering lives in the reductions system that lowers that tree to matrices, plus a small C++ core called cvxcore that builds the linear-operator graph quickly.

We walked the master branch file by file, mapped the architecture into two diagrams, and verified every claim below against the real code. If you ever wondered what happens between cp.Minimize(...) and the moment a backend receives matrices, the rest of this post follows exactly that path.

CVXPY architecture overview: expression trees, atoms, problem API, reduction system and solver interfaces

CVXPY at a glance: user-facing expression trees on the left, the reduction system in the middle, and solver interfaces on the right. Everything between the problem and the solver is a rewrite, not a numerical routine.

Reading the overview from left to right:

  • Expression, L144 — the base of every node in a model; it carries curvature, sign, and the is_dcp / is_dgp predicate family that decides whether your problem is legal at all.
  • Leaf, L45 — shared parent of Variable, Constant and Parameter, owning shape, value and attributes.
  • Atom, L38 — base class of roughly a hundred operators (norms, exp, quad_over_lin, log_det), each shipping with its own canonicalizer.
  • Parameter, L36 — symbolic constants whose values are filled in after compilation; the docstring calls them the only way to modify a problem after creation.
  • Problem.solve, L601 — the public entry point; it dispatches to _solve or a registered custom solve method.
  • resolve_and_build_chain, L435 — resolves your solver string and assembles a reduction chain for this exact problem and backend.
  • Dcp2Cone, L45 — the workhorse: rewrites each atom’s epigraph into SOC, exponential or PSD cone constraints.
  • ConeMatrixStuffing, L484 — packs the canonical form into the sparse A, b, c the backend consumes.
CVXPY detailed architecture: every reduction from Complex2Real to the conic and QP solver bases

The detail view: pre-canonicalization rewrites on top, the Dcp2Cone canonicalization family in the middle, and the QP and conic solver bases at the bottom.

The expression tree is a small class hierarchy

The whole user-facing surface is four leaf types plus operators. Expression defines the contract: curvature (is_convex, is_concave, is_log_log_convex), sign, shape, and the grammar checks is_dcp, is_dgp, is_dqcp, plus newer gates like is_smooth and is_dnlp for the experimental nonlinear layer. Anything you build — cp.sum_squares(x), A @ x - b, a norm1 — is a tree of these nodes.

Three concrete leaves share Leaf as their parent. Variable holds optimization state and attributes; note that attributes are not annotations but constraints-in-waiting — nonneg=True, symmetric=True or PSD=True get materialized later by a dedicated reduction. Constant wraps plain numeric data. Parameter is the interesting one: it behaves like a constant to every check, but its value slot is deliberately empty until you call param.value = ..., which is what makes re-solving cheap.

On top of these sits the atoms package. cvxpy/atoms/__init__.py exports around a hundred callables and groups them into curated lists that the compiler reads later: EXP_ATOMS (eleven entries from log_sum_exp to xexp) requires the exponential cone, PSD_ATOMS (ten, including lambda_max and normNuc) requires semidefinite cones, and SOC_ATOMS marks eight atoms such as geo_mean that have approximate SOC-friendly variants like GeoMeanApprox. The Atom base declares how sign and curvature propagate through arguments, which is what lets the DCP checker validate a whole tree without special cases per operator.

Canonicalization is a graph-rewrite pass

Canonicalization is where a DSL becomes a compiler. The abstract Canonicalization reduction implements a generic rewrite: apply walks the objective and each constraint, canonicalize_tree recurses post-order over expr.args, and canonicalize_expr replaces one atom with its canonical form plus auxiliary constraints. A comment in the code states the policy precisely: constant subtrees are collapsed, but parameter subtrees are preserved when canonicalize_params=True — that distinction is what keeps parameterized problems re-compilable.

Dcp2Cone is the concrete subclass that owns the mapping from math to cones. Its machinery lives in cvxpy/reductions/dcp2cone/canonicalizers/, a folder of more than twenty per-atom modules: exp_canon lowers exp into exponential-cone constraints, quad_form_canon handles x.T P x including the PSD-factorization path, quad_over_lin_canon, geo_mean_canon, perspective_canon and friends each emit their epigraph constraints. There is even a quad/ subfolder with quadratic-objective variants used when a QP backend accepts the P matrix directly.

The reduction keeps an InverseData with an id map from your original constraints to their canonical replacements, so dual values can be walked back to the constraints you actually wrote. That map is filled during apply (one entry per constraint) and consumed by invert — the same pattern every reduction in the codebase follows.

The reduction chain, assembled per solve

Problem._solve calls get_problem_data, which delegates to resolve_and_build_chain in cvxpy/reductions/solvers/solving_chain.py. The builder has a three-stage structure documented in its own docstring. Stage one derives solver context: whether the backend is mixed-integer capable, which constraint types it supports, whether it accepts variable bounds, and whether it only handles three-dimensional SOC blocks — each of those flags comes straight off the solver class.

Stage two prepends problem-dependent rewrites. Complex2Real splits complex expressions into real and imaginary parts when complex2real.accepts(problem); Dgp2Dcp log-transforms geometric programs when you passed gp=True; FlipObjective negates a Maximize into a Minimize; a finite-set atom route turns valued membership into mixed-integer constraints. DPP handling also happens here: a non-DPP problem gets EvalParams prepended, which substitutes parameter values and gives up compiled reuse for correctness, while the experimental DIFFENGINE backend keeps parameters symbolic via CallbackParamFold.

Stage three is the conic lowering. _cone_matrix_stuffing_reductions returns the ordered block Dcp2Cone, then CvxAttr2Constr (attributes to constraints), optional ExactCone2Cone / ApproxCone2Cone translations toward what the solver natively eats, EliminateZeroSized, an optional SOCDim3 split, and finally ConeMatrixStuffing. After that, ExtractDirectCones, a ConeFormat reorder (a no-op when rows are already in the backend’s layout), and the solver instance itself. The container enforces one invariant: SolvingChain.__init__ raises if the last reduction is not a Solver. Execution then bottoms out in solve_via_data, and Problem.unpack_results walks every invert in reverse to restore your variable and dual values.

From 34 solver names to one sparse A

Some build facts worth pinning down. The registered backend roster in cvxpy/settings.py lists 34 names — from CLARABEL and ECOS through CUOPT, KNITRO and ending at UNO. The conic_solvers/ folder holds 28 concrete interface modules besides the shared ConicSolver base, and qp_solvers/ holds 12 besides QpSolver; that gap between the roster and the folder count is because several backends share an interface or appear only in the QP family. The SCIPY interface (class SCIPY) imports scipy.optimize and wraps its HiGHS-backed LP path, so solver="SCIPY" means your problem rides scipy’s native binding — the same library CVXPY already uses for sparse matrix assembly throughout stuffing.

Each interface is mostly a translation contract, not an algorithm: declare supported cones, map solver options, write solve_via_data to call the vendor library and unpack its status codes. The data handed over is described by ConeDims (per-cone dimension bookkeeping) and carried by ConeProg, which extends the abstract ParamProb so a compiled program can be re-solved under new parameter values.

Two more numbers. DPP_PARAM_THRESHOLD = 1000 in settings: parametric problems with a total parameter size at or above that switch canonicalization to the COO backend, which the code describes as O(nnz) operations for large parameter counts, instead of the default C++ path. And that C++ path is real: setup/extensions.py builds cvxcore as a plain setuptools extension from cvxcore.cpp, LinOpOperations.cpp, Utils.cpp and a SWIG-style wrapper, compiling the LinOp graph into problem data faster than pure Python would. The project keeps itself honest with 133 test files under cvxpy/tests running on pytest, and its pre-commit configuration lints with ruff as the first hook.

Parameters, DPP, and solving twice without recompiling

The parameter system is CVXPY’s answer to the loop-shaped-model pattern: fit once, re-solve many times. Parameter’s docstring is explicit that parameters are the only way to modify a problem after creation, and the class even refuses Expression-valued bounds so that the parameter-to-data mapping stays linear. Under DPP (disciplined parameterized programming) rules, a compiled problem’s data can be refreshed with new parameter values; ParamProb is the abstract interface for that, and ConeProg implements it for the conic path.

The machinery is visible in the compiled-data cache: Problem.get_problem_data maintains a Cache whose key combines solver, gp, ignore_dpp, use_quad_obj and canonicalization backend, so an unchanged problem plus unchanged parameters skips compilation entirely. When you opt into differentiability with requires_grad=True, Problem.backward and Problem.derivative propagate gradients through the same chain maps that reductions already built for dual recovery — no separate autodiff graph is constructed.

One subtlety worth knowing: Parameter.is_constant checks whether a DPP scope is active and answers differently in each case. That tiny switch is what lets the same object behave as a compile-time constant in one mode and a symbolic input in the other, and it is exactly the kind of detail that explains otherwise mysterious DPP warnings.

Honest Limits

This tour covered a sparse checkout — expressions, atoms, problems, reductions — read line-verified; cvxpy/lin_ops, cvxpy/transforms, the NLP layer and most of the test suite were only surveyed through tree listings and targeted file reads, so their internals deserve their own pass. The reductions system is under active refactoring: solving_chain.py now carries ProblemForm and SolverInfo types, and older write-ups describing functions by names that no longer exist will mislead you — trust the docstrings in the tree you have. Every count here (34 solvers, 28 conic interfaces, 12 QP interfaces, 133 test files, the 1000-parameter threshold) describes master at the time of writing and moves with releases. We benchmarked nothing; performance statements are limited to what the code itself asserts, such as the COO backend’s O(nnz) claim. Dual values, warm starting, bisection for quasiconvex problems, and the cp.nlp nonlinear layer got one sentence each at most — each is a separate subsystem with its own reductions.

Watch PyShine on YouTube

Contents