Skip to content

Commit 6d71c5b

Browse files
committed
more tests
1 parent f496652 commit 6d71c5b

1 file changed

Lines changed: 31 additions & 96 deletions

File tree

cvxpy/tests/nlp_tests/stress_tests_diff_engine/test_stacked_permuted_dense.py

Lines changed: 31 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,13 @@ def test_deep_composition(self):
144144
Y = cp.Variable((n, n), bounds=[-1, 1])
145145
obj = cp.Minimize(cp.sum(cp.multiply(
146146
cp.sin(A @ cp.cos(B @ cp.logistic(C @ (X @ Y).T))),
147-
cp.cos(A @ cp.cos(B @ cp.logistic(C @ Y))),
147+
cp.cos(A @ cp.cos(B @ cp.logistic((C @ Y) @ (X @ Y)))),
148148
)))
149149
prob = cp.Problem(obj)
150150
prob.solve(nlp=True)
151151
checker = DerivativeChecker(prob)
152152
checker.run_and_assert()
153153

154-
"""
155154
def test_multiply_spd_plain_var(self):
156155
np.random.seed(0)
157156
n, m = 5, 6
@@ -164,79 +163,27 @@ def test_multiply_spd_plain_var(self):
164163
checker = DerivativeChecker(prob)
165164
checker.run_and_assert()
166165

167-
def test_multiply_plain_var_pd(self):
166+
def test_multiply_plain_var_spd(self):
168167
np.random.seed(0)
169168
n, m = 5, 6
170169
A = np.random.rand(m, n)
171-
x = cp.Variable(n, bounds=[-1, 1])
172-
y = cp.Variable(m, bounds=[-1, 1])
173-
obj = cp.Minimize(cp.sum(cp.multiply(cp.sin(y), cp.cos(A @ x))))
174-
prob = cp.Problem(obj)
175-
prob.solve(nlp=True)
176-
checker = DerivativeChecker(prob)
177-
checker.run_and_assert()
178-
179-
def test_pd_index_propagation(self):
180-
# Indexing into a permuted dense propagates permuted dense via index_alloc /
181-
# index_fill_values. Use a non-sorted index with duplicates to stress the
182-
# permutation path.
183-
np.random.seed(0)
184-
n, m = 5, 8
185-
A = np.random.rand(m, n)
186-
B = np.random.rand(m, n)
187-
x = cp.Variable(n, bounds=[-1, 1])
188-
y = cp.Variable(n, bounds=[-1, 1])
189-
idx_A = [0, 2, 4, 1, 3, 0, 7]
190-
idx_B = [0, 4, 2, 3, 1, 0, 7]
191-
obj = cp.Minimize(
192-
cp.sum(cp.multiply(cp.sin((A @ x)[idx_A]), cp.cos((B @ y)[idx_B])))
193-
)
194-
prob = cp.Problem(obj)
195-
prob.solve(nlp=True)
196-
checker = DerivativeChecker(prob)
197-
checker.run_and_assert()
198-
199-
def test_pd_transpose_propagation(self):
200-
# Transpose of a PD result. Column-shape variables make .T non-trivial:
201-
# (A @ x) is (m, 1), (A @ x).T is (1, m).
202-
np.random.seed(0)
203-
n, m = 5, 6
204-
A = np.random.rand(m, n)
205-
B = np.random.rand(m, n)
206-
x = cp.Variable((n, 1), bounds=[-1, 1])
207-
y = cp.Variable((n, 1), bounds=[-1, 1])
208-
obj = cp.Minimize(cp.sum(cp.multiply(cp.sin((A @ x).T), cp.cos((B @ y).T))))
209-
prob = cp.Problem(obj)
210-
prob.solve(nlp=True)
211-
checker = DerivativeChecker(prob)
212-
checker.run_and_assert()
213-
214-
def test_pd_broadcast_propagation(self):
215-
# Reshape PD results to column / row vectors and let multiply broadcast.
216-
np.random.seed(0)
217-
n, m = 5, 6
218-
A = np.random.rand(m, n)
219-
B = np.random.rand(m, n)
220-
x = cp.Variable(n, bounds=[-1, 1])
221-
y = cp.Variable(n, bounds=[-1, 1])
222-
obj = cp.Minimize(cp.sum(cp.multiply(
223-
cp.reshape(cp.sin(A @ x), (m, 1), order='F'),
224-
cp.reshape(cp.cos(B @ y), (1, m), order='F'),
225-
)))
170+
X = cp.Variable((n, n), bounds=[-1, 1])
171+
y = cp.Variable((m, 1), bounds=[-1, 1])
172+
obj = cp.Minimize(cp.sum(cp.multiply(cp.sin(y), cp.cos(A @ X))))
226173
prob = cp.Problem(obj)
227174
prob.solve(nlp=True)
228175
checker = DerivativeChecker(prob)
229176
checker.run_and_assert()
230177

231-
def test_multiply_pd_pd_right(self):
178+
def test_multiply_spd_spd_right_two(self):
232179
# Right matmul with dense A and dense B
233180
np.random.seed(0)
234181
n, m = 5, 6
235182
A = np.random.rand(n, m)
236183
B = np.random.rand(n, m)
237-
x = cp.Variable(n, bounds=[-1, 1])
238-
y = cp.Variable(n, bounds=[-1, 1])
239-
obj = cp.Minimize(cp.sum(cp.multiply(cp.sin(x @ A), cp.cos(y @ B))))
184+
X = cp.Variable((n, n), bounds=[-1, 1])
185+
Y = cp.Variable((n, n), bounds=[-1, 1])
186+
obj = cp.Minimize(cp.sum(cp.multiply(cp.sin(X @ A), cp.cos(Y @ B))))
240187
prob = cp.Problem(obj)
241188
prob.solve(nlp=True)
242189
checker = DerivativeChecker(prob)
@@ -248,60 +195,48 @@ def test_multiply_pd_sparse_right(self):
248195
n, m = 5, 6
249196
A = np.random.rand(n, m)
250197
B = sp.random(n, m, density=0.5, format='csr')
251-
x = cp.Variable(n, bounds=[-1, 1])
252-
y = cp.Variable(n, bounds=[-1, 1])
253-
obj = cp.Minimize(cp.sum(cp.multiply(cp.sin(x @ A), cp.cos(y @ B))))
198+
X = cp.Variable((n, n), bounds=[-1, 1])
199+
Y = cp.Variable((n, n), bounds=[-1, 1])
200+
obj = cp.Minimize(cp.sum(cp.multiply(cp.sin(X @ A), cp.cos(Y @ B))))
254201
prob = cp.Problem(obj)
255202
prob.solve(nlp=True)
256203
checker = DerivativeChecker(prob)
257204
checker.run_and_assert()
258205

259-
def test_pd_index_propagation_right(self):
260-
# Right matmul with index
206+
def test_spd_index_propagation(self):
207+
# Indexing into a permuted dense propagates permuted dense via index_alloc /
208+
# index_fill_values. Use a non-sorted index with duplicates to stress the
209+
# permutation path.
261210
np.random.seed(0)
262211
n, m = 5, 8
263-
A = np.random.rand(n, m)
264-
B = np.random.rand(n, m)
265-
x = cp.Variable(n, bounds=[-1, 1])
266-
y = cp.Variable(n, bounds=[-1, 1])
212+
A = np.random.rand(m, n)
213+
B = np.random.rand(m, n)
214+
X = cp.Variable((n, n), bounds=[-1, 1])
215+
Y = cp.Variable((n, n), bounds=[-1, 1])
267216
idx_A = [0, 2, 4, 1, 3, 0, 7]
268217
idx_B = [0, 4, 2, 3, 1, 0, 7]
269218
obj = cp.Minimize(
270-
cp.sum(cp.multiply(cp.sin((x @ A)[idx_A]), cp.cos((y @ B)[idx_B])))
219+
cp.sum(cp.multiply(cp.sin((A @ X)[idx_A]), cp.cos((B @ Y)[idx_B])))
271220
)
272221
prob = cp.Problem(obj)
273222
prob.solve(nlp=True)
274223
checker = DerivativeChecker(prob)
275224
checker.run_and_assert()
276225

277-
def test_pd_transpose_propagation_right(self):
278-
# Right matmul with transpose
279-
np.random.seed(0)
280-
n, m = 5, 6
281-
A = np.random.rand(n, m)
282-
B = np.random.rand(n, m)
283-
x = cp.Variable((1, n), bounds=[-1, 1])
284-
y = cp.Variable((1, n), bounds=[-1, 1])
285-
obj = cp.Minimize(cp.sum(cp.multiply(cp.sin((x @ A).T), cp.cos((y @ B).T))))
286-
prob = cp.Problem(obj)
287-
prob.solve(nlp=True)
288-
checker = DerivativeChecker(prob)
289-
checker.run_and_assert()
290-
291-
def test_pd_broadcast_propagation_right(self):
292-
# Reshape right-rooted PD results and force (m, 1) * (1, m) broadcast.
226+
def test_pd_index_propagation_right(self):
227+
# Right matmul with index
293228
np.random.seed(0)
294-
n, m = 5, 6
229+
n, m = 5, 8
295230
A = np.random.rand(n, m)
296231
B = np.random.rand(n, m)
297-
x = cp.Variable(n, bounds=[-1, 1])
298-
y = cp.Variable(n, bounds=[-1, 1])
299-
obj = cp.Minimize(cp.sum(cp.multiply(
300-
cp.reshape(cp.sin(x @ A), (m, 1), order='F'),
301-
cp.reshape(cp.cos(y @ B), (1, m), order='F'),
302-
)))
232+
X = cp.Variable((n, n), bounds=[-1, 1])
233+
Y = cp.Variable((n, n), bounds=[-1, 1])
234+
idx_A = [0, 2, 4, 1, 3, 0, 7]
235+
idx_B = [0, 4, 2, 3, 1, 0, 7]
236+
obj = cp.Minimize(
237+
cp.sum(cp.multiply(cp.sin((X @ A)[0, idx_A]), cp.cos((Y @ B)[1, idx_B])))
238+
)
303239
prob = cp.Problem(obj)
304240
prob.solve(nlp=True)
305241
checker = DerivativeChecker(prob)
306242
checker.run_and_assert()
307-
"""

0 commit comments

Comments
 (0)