cvxpylayers icon indicating copy to clipboard operation
cvxpylayers copied to clipboard

Support for CallbackParam

Open bastienlc opened this issue 10 months ago • 1 comments

cvxpy provides a feature called CallbackParam that allows operations on parameters that would not be permissible under the DPP ruleset. Consider the example below: we normally cannot multiply the matrices A1 and A2, but are able to do so with CallbackParam.

import cvxpy as cp


def create_problem_without_callback(k: int, n: int, m: int):
    A1 = cp.Parameter((n, k))
    A2 = cp.Parameter((k, m))
    B = cp.Parameter(n)
    X = cp.Variable(m)

    problem = cp.Problem(cp.Minimize(cp.norm((A1 @ A2) @ X + B, 2) + cp.norm(X, 1)))
    assert problem.is_dcp(dpp=True)


def create_problem_with_callback(k: int, n: int, m: int):
    A1 = cp.Parameter((n, k))
    A2 = cp.Parameter((k, m))
    A = cp.CallbackParam(shape=(n, m), callback=lambda: A1 @ A2)
    B = cp.Parameter(n)
    X = cp.Variable(m)

    problem = cp.Problem(cp.Minimize(cp.norm(A @ X + B, 2) + cp.norm(X, 1)))
    assert problem.is_dcp(dpp=True)


if __name__ == "__main__":
    create_problem_without_callback(2, 3, 4)  # Assertion Error
    create_problem_with_callback(2, 3, 4)  # OK

This feature might be useful if you don't want to compute A1 @ A2 ahead of time for instance.

From my experiments it looks like cvxpylayers does not support CallbackParam; either it does not recognize the set of parameters of the problem correctly, or it does not compute the callback.

I think supporting this feature would be pretty straightforward.

bastienlc avatar Mar 13 '25 08:03 bastienlc

Definitely possible to support this, but it'll take a little design work. That'll be on the agenda for after the refactor!

PTNobel avatar Jun 19 '25 18:06 PTNobel