Skip to content

Comments

Add parameter support to NLP diff engine#151

Open
Transurgeon wants to merge 6 commits intomasterfrom
nlp-param-support
Open

Add parameter support to NLP diff engine#151
Transurgeon wants to merge 6 commits intomasterfrom
nlp-param-support

Conversation

@Transurgeon
Copy link
Member

Enable CVXPY Parameters as live nodes in the C expression tree, so parameter values can be updated via memcpy without rebuilding the tree. This is critical for parametric NLP problems (MPC, portfolio optimization).

Changes:

  • inverse_data.py: add get_param_offsets() static method
  • converters.py: add build_parameter_dict(), _normalize_shape() helper, Parameter branches in matmul/multiply converters, param_dict threading through convert_expr/convert_expressions (now returns 4-tuple)
  • c_problem.py: use convert_expressions(), register params with C problem, add update_params() method for fast parameter value refresh

Description

Please include a short summary of the change.
Issue link (if applicable):

Type of change

  • New feature (backwards compatible)
  • New feature (breaking API changes)
  • Bug fix
  • Other (Documentation, CI, ...)

Contribution checklist

  • Add our license to new files.
  • Check that your code adheres to our coding style.
  • Write unittests.
  • Run the unittests and check that they’re passing.
  • Run the benchmarks to make sure your change doesn’t introduce a regression.

Transurgeon and others added 2 commits February 17, 2026 16:26
Enable CVXPY Parameters as live nodes in the C expression tree, so
parameter values can be updated via memcpy without rebuilding the tree.
This is critical for parametric NLP problems (MPC, portfolio optimization).

Changes:
- inverse_data.py: add get_param_offsets() static method
- converters.py: add build_parameter_dict(), _normalize_shape() helper,
  Parameter branches in matmul/multiply converters, param_dict threading
  through convert_expr/convert_expressions (now returns 4-tuple)
- c_problem.py: use convert_expressions(), register params with C problem,
  add update_params() method for fast parameter value refresh

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The refactor in 33fe669 lost the inline code that copied initial
values from dimension-reduced variables (diag, sparse, symmetric) to
their reduced counterparts. This caused NLP solvers to find value=None
on the reduced variable, breaking initialization. Mirror the existing
parameter value-propagation pattern by calling lower_value(var) after
creating the reduced variable.

Also fix lower_value to handle sparse diagonal matrices by using
.diagonal() instead of np.diag(), which does not accept sparse inputs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions
Copy link

github-actions bot commented Feb 17, 2026

Benchmarks that have improved:

   before           after         ratio
 [0c4f10e2]       [6bac4ca2]
  •     548±0ms          493±0ms     0.90  semidefinite_programming.SemidefiniteProgramming.time_compile_problem
    
  •     1.22±0s          1.02±0s     0.84  gini_portfolio.Cajas.time_compile_problem
    

Benchmarks that have stayed the same:

   before           after         ratio
 [0c4f10e2]       [6bac4ca2]
     39.1±0ms         39.9±0ms     1.02  matrix_stuffing.SmallMatrixStuffing.time_compile_problem
      12.1±0s          12.3±0s     1.02  finance.CVaRBenchmark.time_compile_problem
     14.5±0ms         14.7±0ms     1.02  simple_LP_benchmarks.SimpleFullyParametrizedLPBenchmark.time_compile_problem
      1.56±0s          1.58±0s     1.01  tv_inpainting.TvInpainting.time_compile_problem
      986±0ms          999±0ms     1.01  finance.FactorCovarianceModel.time_compile_problem
      886±0ms          893±0ms     1.01  simple_LP_benchmarks.SimpleScalarParametrizedLPBenchmark.time_compile_problem
      671±0ms          675±0ms     1.01  matrix_stuffing.ConeMatrixStuffingBench.time_compile_problem
      20.5±0s          20.6±0s     1.01  sdp_segfault_1132_benchmark.SDPSegfault1132Benchmark.time_compile_problem
      737±0ms          740±0ms     1.00  simple_QP_benchmarks.LeastSquares.time_compile_problem
     15.1±0ms         15.2±0ms     1.00  simple_QP_benchmarks.ParametrizedQPBenchmark.time_compile_problem
      1.78±0s          1.78±0s     1.00  simple_QP_benchmarks.UnconstrainedQP.time_compile_problem
      226±0ms          226±0ms     1.00  gini_portfolio.Murray.time_compile_problem
      134±0ms          133±0ms     1.00  high_dim_convex_plasticity.ConvexPlasticity.time_compile_problem
      278±0ms          277±0ms     1.00  slow_pruning_1668_benchmark.SlowPruningBenchmark.time_compile_problem
      282±0ms          281±0ms     1.00  matrix_stuffing.ParamSmallMatrixStuffing.time_compile_problem
      3.92±0s          3.90±0s     1.00  huber_regression.HuberRegression.time_compile_problem
      1.41±0s          1.40±0s     1.00  matrix_stuffing.ParamConeMatrixStuffing.time_compile_problem
      4.46±0s          4.44±0s     1.00  svm_l1_regularization.SVMWithL1Regularization.time_compile_problem
      10.1±0s          10.1±0s     0.99  simple_LP_benchmarks.SimpleLPBenchmark.time_compile_problem
      239±0ms          238±0ms     0.99  simple_QP_benchmarks.SimpleQPBenchmark.time_compile_problem
      5.06±0s          5.02±0s     0.99  optimal_advertising.OptimalAdvertising.time_compile_problem
      317±0ms          313±0ms     0.98  gini_portfolio.Yitzhaki.time_compile_problem
      2.98±0s          2.79±0s     0.94  quantum_hilbert_matrix.QuantumHilbertMatrix.time_compile_problem

Transurgeon and others added 4 commits February 17, 2026 20:45
Detect the reshape(sparse_coeff @ reduced_param, shape, 'F') @ x pattern
produced by CvxAttr2Constr for sparse parameters and fuse it into a single
make_left_matmul/make_right_matmul with native CSR sparsity, instead of
two separate matmuls. Values in update_params are converted to CSR order
via COO→CSR for fused sparse parameters.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Test scalar, vector, dense matrix, and sparse matrix parameter
multiplication with derivative checking and parameter value updates.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Parameters are always registered via build_parameter_dict, so the
fallthrough path that created them as make_constant was dead code.
Replace with an explicit error if a parameter is missing from param_dict.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
# Recursive case: atoms
atom_name = type(expr).__name__

# Try to fuse sparse-parameter reconstruction matmuls before normal dispatch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to move this logic into convert_matmul?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants