Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some bug patches + torch no_grad forward pass #157

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions cvxpylayers/torch/cvxpylayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,18 @@ def forward(ctx, *params):
ctx.shapes.append(A.shape)
info['canon_time'] = time.time() - start

# compute solution and derivative function
# compute solution (always)
# and derivative function (if needed for reverse mode)
start = time.time()
try:
xs, _, _, _, ctx.DT_batch = diffcp.solve_and_derivative_batch(
As, bs, cs, cone_dicts, **solver_args)
if any(p.requires_grad for p in params):
xs, _, _, _, ctx.DT_batch = (
diffcp.solve_and_derivative_batch(
As, bs, cs, cone_dicts, **solver_args)
)
else:
xs, _, _ = diffcp.solve_only_batch(
As, bs, cs, cone_dicts, **solver_args)
except diffcp.SolverError as e:
print(
"Please consider re-formulating your problem so that "
Expand Down
51 changes: 43 additions & 8 deletions cvxpylayers/torch/test_cvxpylayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,9 @@ def test_least_squares(self):

def lstsq(
A,
b): return torch.solve(
(A_th.t() @ b_th).unsqueeze(1),
A_th.t() @ A_th +
torch.eye(n).double())[0]
b): return torch.linalg.solve(
A.t() @ A + torch.eye(n, dtype=torch.float64),
(A.t() @ b).unsqueeze(1))
x_lstsq = lstsq(A_th, b_th)

grad_A_cvxpy, grad_b_cvxpy = grad(x.sum(), [A_th, b_th])
Expand Down Expand Up @@ -325,10 +324,9 @@ def test_broadcasting(self):

def lstsq(
A,
b): return torch.solve(
(A.t() @ b).unsqueeze(1),
A.t() @ A +
torch.eye(n).double())[0]
b): return torch.linalg.solve(
A.t() @ A + torch.eye(n).double(),
(A.t() @ b).unsqueeze(1))
x_lstsq = lstsq(A_th, b_th_0)

grad_A_cvxpy, grad_b_cvxpy = grad(x.sum(), [A_th, b_th])
Expand Down Expand Up @@ -416,6 +414,43 @@ def test_basic_gp(self):
"eps": 1e-12, "acceleration_lookback": 0})[0].sum(),
(a_tch, b_tch, c_tch), atol=1e-3, rtol=1e-3)

def test_no_grad_context(self):
n, m = 2, 3
x = cp.Variable(n)
A = cp.Parameter((m, n))
b = cp.Parameter(m)
constraints = [x >= 0]
objective = cp.Minimize(0.5 * cp.pnorm(A @ x - b, p=1))
problem = cp.Problem(objective, constraints)
assert problem.is_dpp()

cvxpylayer = CvxpyLayer(problem, parameters=[A, b], variables=[x])
A_tch = torch.randn(m, n, requires_grad=True)
b_tch = torch.randn(m, requires_grad=True)

with torch.no_grad():
solution, = cvxpylayer(A_tch, b_tch)

self.assertFalse(solution.requires_grad)

def test_requires_grad_false(self):
n, m = 2, 3
x = cp.Variable(n)
A = cp.Parameter((m, n))
b = cp.Parameter(m)
constraints = [x >= 0]
objective = cp.Minimize(0.5 * cp.pnorm(A @ x - b, p=1))
problem = cp.Problem(objective, constraints)
assert problem.is_dpp()

cvxpylayer = CvxpyLayer(problem, parameters=[A, b], variables=[x])
A_tch = torch.randn(m, n, requires_grad=False)
b_tch = torch.randn(m, requires_grad=False)

solution, = cvxpylayer(A_tch, b_tch)

self.assertFalse(solution.requires_grad)


if __name__ == '__main__':
unittest.main()
Loading