[6ff4a8]: / tests / backends / ott / test_backend.py

Download this file

409 lines (344 with data), 16.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
from typing import Optional, Tuple, Type, Union
import pytest
import jax
import jax.numpy as jnp
import numpy as np
from ott.geometry import costs
from ott.geometry.geometry import Geometry
from ott.geometry.low_rank import LRCGeometry
from ott.geometry.pointcloud import PointCloud
from ott.problems.linear.linear_problem import LinearProblem
from ott.problems.quadratic import quadratic_problem
from ott.problems.quadratic.quadratic_problem import QuadraticProblem
from ott.solvers.linear import solve as sinkhorn
from ott.solvers.linear.sinkhorn import Sinkhorn
from ott.solvers.linear.sinkhorn_lr import LRSinkhorn
from ott.solvers.quadratic.gromov_wasserstein import GromovWasserstein
from ott.solvers.quadratic.gromov_wasserstein_lr import LRGromovWasserstein
from moscot._types import ArrayLike, Device_t
from moscot.backends.ott import GWSolver, SinkhornSolver
from moscot.backends.ott._utils import InitializerResolver, alpha_to_fused_penalty
from moscot.base.output import BaseDiscreteSolverOutput
from moscot.base.solver import O, OTSolver
from moscot.utils.tagged_array import Tag, TaggedArray
from tests._utils import ATOL, RTOL, Geom_t
from tests.plotting.conftest import PlotTester, PlotTesterMeta
class TestSinkhorn:
@pytest.mark.fast
@pytest.mark.parametrize("jit", [False, True])
@pytest.mark.parametrize("eps", [None, 1e-2, 1e-1])
def test_matches_ott(self, x: Geom_t, eps: Optional[float], jit: bool):
fn = jax.jit(sinkhorn) if jit else sinkhorn
gt = fn(PointCloud(x, epsilon=eps))
solver = SinkhornSolver(jit=jit)
assert solver.xy is None
assert isinstance(solver.solver, Sinkhorn)
pred = solver(a=jnp.ones(len(x)) / len(x), b=jnp.ones(len(x)) / len(x), xy=(x, x), epsilon=eps)
assert solver.rank == -1
assert not solver.is_low_rank
assert isinstance(solver.xy, Geometry)
assert pred.rank == -1
np.testing.assert_allclose(gt.matrix, pred.transport_matrix, rtol=RTOL, atol=ATOL)
@pytest.mark.parametrize("rank", [5, 10])
@pytest.mark.parametrize("initializer", ["random", "rank2", "k-means"])
def test_solver_rank(self, y: Geom_t, rank: Optional[int], initializer: str):
eps = 1e-2
default_gamma_lr_sinhorn = 500
initializer = InitializerResolver.lr_from_str(initializer, rank=rank)
lr_sinkhorn = LRSinkhorn(rank=rank, initializer=initializer, gamma=default_gamma_lr_sinhorn)
problem = LinearProblem(PointCloud(y, epsilon=eps))
gt = lr_sinkhorn(problem)
solver = SinkhornSolver(rank=rank, initializer=initializer)
assert solver.rank == rank
assert solver.is_low_rank
assert solver.xy is None
assert isinstance(solver.solver, LRSinkhorn)
pred = solver(a=jnp.ones(len(y)) / len(y), b=jnp.ones(len(y)) / len(y), xy=(y, y), epsilon=eps)
assert isinstance(solver.xy, PointCloud)
assert pred.rank == rank
np.testing.assert_allclose(solver._problem.geom.cost_matrix, problem.geom.cost_matrix, rtol=RTOL, atol=ATOL)
np.testing.assert_allclose(gt.matrix, pred.transport_matrix, rtol=RTOL, atol=ATOL)
@pytest.mark.parametrize(("rank", "cost_fn"), [(2, costs.Euclidean()), (3, costs.SqPNorm(p=1.5))])
def test_geometry_rank(self, x: Geom_t, rank: int, cost_fn: costs.CostFn):
eps = 0.05
geom = PointCloud(x, epsilon=eps, cost_fn=cost_fn).to_LRCGeometry(rank=rank)
problem = LinearProblem(geom)
gt = Sinkhorn()(problem)
solver = SinkhornSolver()
assert not solver.is_low_rank
pred = solver(
a=jnp.ones(len(x)) / len(x),
b=jnp.ones(len(x)) / len(x),
xy=TaggedArray(x, cost=cost_fn),
epsilon=eps,
cost_matrix_rank=rank,
)
assert isinstance(solver.xy, LRCGeometry)
np.testing.assert_allclose(solver._problem.geom.cost_matrix, problem.geom.cost_matrix, rtol=RTOL, atol=ATOL)
np.testing.assert_allclose(gt.matrix, pred.transport_matrix, rtol=RTOL, atol=ATOL)
class TestGW:
@pytest.mark.parametrize("jit", [False, True])
@pytest.mark.parametrize("eps", [5e-2, 1e-2, 1e-1])
def test_matches_ott(self, x: Geom_t, y: Geom_t, eps: Optional[float], jit: bool):
thresh = 1e-2
pc_x, pc_y = PointCloud(x, epsilon=eps), PointCloud(y, epsilon=eps)
prob = quadratic_problem.QuadraticProblem(pc_x, pc_y)
sol = GromovWasserstein(epsilon=eps, threshold=thresh, linear_solver=Sinkhorn())
solver = jax.jit(sol, static_argnames=["threshold", "epsilon"]) if jit else sol
gt = solver(prob)
solver = GWSolver(jit=jit, epsilon=eps, threshold=thresh)
assert isinstance(solver.solver, GromovWasserstein)
assert solver.x is None
assert solver.y is None
pred = solver(
a=jnp.ones(len(x)) / len(x),
b=jnp.ones(len(y)) / len(y),
x=x,
y=y,
tags={"x": "point_cloud", "y": "point_cloud"},
alpha=1.0,
)
assert solver.is_fused is False
assert solver.rank == -1
assert not solver.is_low_rank
assert isinstance(solver.x, PointCloud)
assert isinstance(solver.y, PointCloud)
assert pred.rank == -1
np.testing.assert_allclose(gt.matrix, pred.transport_matrix, rtol=RTOL, atol=ATOL)
@pytest.mark.parametrize("eps", [5e-1, 1])
def test_epsilon(self, x_cost: jnp.ndarray, y_cost: jnp.ndarray, eps: Optional[float]) -> None:
thresh = 1e-3
problem = QuadraticProblem(
geom_xx=Geometry(cost_matrix=x_cost, epsilon=eps), geom_yy=Geometry(cost_matrix=y_cost, epsilon=eps)
)
gt = GromovWasserstein(epsilon=eps, threshold=thresh, linear_solver=Sinkhorn())(problem)
solver = GWSolver(epsilon=eps, threshold=thresh)
pred = solver(
a=jnp.ones(len(x_cost)) / len(x_cost),
b=jnp.ones(len(y_cost)) / len(y_cost),
x=x_cost,
y=y_cost,
tags={"x": Tag.COST_MATRIX, "y": Tag.COST_MATRIX},
alpha=1.0,
)
assert solver.is_fused is False
assert pred.rank == -1
assert solver.rank == -1
assert isinstance(solver.x, Geometry)
assert isinstance(solver.y, Geometry)
np.testing.assert_allclose(gt.matrix, pred.transport_matrix, rtol=RTOL, atol=ATOL)
@pytest.mark.parametrize("rank", [-1, 7])
def test_solver_rank(self, x: Geom_t, y: Geom_t, rank: int) -> None:
thresh, eps = 1e-2, 1e-2
if rank > -1:
initializer = InitializerResolver.lr_from_str("random", rank=rank)
gt = LRGromovWasserstein(epsilon=eps, rank=rank, threshold=thresh, initializer=initializer)(
QuadraticProblem(PointCloud(x, epsilon=eps), PointCloud(y, epsilon=eps))
)
else:
gt = GromovWasserstein(epsilon=eps, threshold=thresh, linear_solver=Sinkhorn(threshold=thresh))(
QuadraticProblem(PointCloud(x, epsilon=eps), PointCloud(y, epsilon=eps))
)
solver = GWSolver(rank=rank, epsilon=eps, threshold=thresh)
pred = solver(
a=jnp.ones(len(x)) / len(x),
b=jnp.ones(len(y)) / len(y),
x=x,
y=y,
tags={"x": "point_cloud", "y": "point_cloud"},
alpha=1.0,
)
assert solver.is_fused is False
assert solver.rank == rank
assert pred.rank == rank
np.testing.assert_allclose(gt.matrix, pred.transport_matrix, rtol=RTOL, atol=ATOL)
class TestFGW:
@pytest.mark.parametrize("alpha", [0.25, 0.75])
@pytest.mark.parametrize("eps", [1e-2, 1e-1, 5e-1])
def test_matches_ott(self, x: Geom_t, y: Geom_t, xy: Geom_t, eps: Optional[float], alpha: float) -> None:
thresh = 1e-2
xx, yy = xy
ott_solver = GromovWasserstein(epsilon=eps, threshold=thresh, linear_solver=Sinkhorn())
problem = quadratic_problem.QuadraticProblem(
geom_xx=PointCloud(x, epsilon=eps),
geom_yy=PointCloud(y, epsilon=eps),
geom_xy=PointCloud(xx, yy, epsilon=eps),
fused_penalty=alpha_to_fused_penalty(alpha),
)
gt = ott_solver(problem)
solver = GWSolver(epsilon=eps, threshold=thresh)
assert isinstance(solver.solver, GromovWasserstein)
assert solver.xy is None
pred = solver(
a=jnp.ones(len(x)) / len(x),
b=jnp.ones(len(y)) / len(y),
x=x,
y=y,
xy=xy,
alpha=alpha,
tags={"x": "point_cloud", "y": "point_cloud", "xy": "point_cloud"},
)
assert solver.is_fused is True
assert solver.rank == -1
assert pred.rank == -1
assert isinstance(solver.xy, PointCloud)
np.testing.assert_allclose(gt.matrix, pred.transport_matrix, rtol=RTOL, atol=ATOL)
@pytest.mark.fast
@pytest.mark.parametrize("alpha", [0.1, 0.9])
def test_alpha(self, x: Geom_t, y: Geom_t, xy: Geom_t, alpha: float) -> None:
thresh, eps = 5e-2, 1e-1
xx, yy = xy
ott_solver = GromovWasserstein(epsilon=eps, threshold=thresh, linear_solver=Sinkhorn())
problem = quadratic_problem.QuadraticProblem(
geom_xx=PointCloud(x, epsilon=eps),
geom_yy=PointCloud(y, epsilon=eps),
geom_xy=PointCloud(xx, yy, epsilon=eps),
fused_penalty=alpha_to_fused_penalty(alpha),
)
gt = ott_solver(problem)
solver = GWSolver(epsilon=eps, threshold=thresh)
pred = solver(
a=jnp.ones(len(x)) / len(x),
b=jnp.ones(len(y)) / len(y),
x=x,
y=y,
xy=xy,
alpha=alpha,
tags={"x": "point_cloud", "y": "point_cloud", "xy": "point_cloud"},
)
assert solver.is_fused is True
assert not solver.is_low_rank
assert pred.rank == -1
assert not pred.is_low_rank
np.testing.assert_allclose(gt.matrix, pred.transport_matrix, rtol=RTOL, atol=ATOL)
@pytest.mark.parametrize("eps", [1e-3, 5e-2])
def test_epsilon(
self, x_cost: jnp.ndarray, y_cost: jnp.ndarray, xy_cost: jnp.ndarray, eps: Optional[float]
) -> None:
thresh, alpha = 5e-1, 0.66
problem = QuadraticProblem(
geom_xx=Geometry(cost_matrix=x_cost, epsilon=eps),
geom_yy=Geometry(cost_matrix=y_cost, epsilon=eps),
geom_xy=Geometry(cost_matrix=xy_cost, epsilon=eps),
fused_penalty=alpha_to_fused_penalty(alpha),
)
gt = GromovWasserstein(epsilon=eps, threshold=thresh, linear_solver=Sinkhorn())(problem)
solver = GWSolver(epsilon=eps, threshold=thresh)
pred = solver(
a=jnp.ones(len(x_cost)) / len(x_cost),
b=jnp.ones(len(y_cost)) / len(y_cost),
x=x_cost,
y=y_cost,
xy=xy_cost,
alpha=alpha,
tags={"x": Tag.COST_MATRIX, "y": Tag.COST_MATRIX, "xy": Tag.COST_MATRIX},
)
assert solver.is_fused is True
assert pred.rank == -1
np.testing.assert_allclose(gt.matrix, pred.transport_matrix, rtol=RTOL, atol=ATOL)
class TestScaleCost:
@pytest.mark.parametrize("scale_cost", [1.0, 0.5, "mean", "max_cost", "max_norm", "max_bound"])
def test_scale(self, x: Geom_t, scale_cost: Union[float, str]) -> None:
eps = 1e-2
gt = sinkhorn(PointCloud(x, epsilon=eps, scale_cost=scale_cost))
solver = SinkhornSolver()
pred = solver(
a=jnp.ones(len(x)) / len(x), b=jnp.ones(len(x)) / len(x), xy=(x, x), epsilon=eps, scale_cost=scale_cost
)
np.testing.assert_allclose(gt.matrix, pred.transport_matrix, rtol=RTOL, atol=ATOL)
class TestSolverOutput:
def test_properties(self, x: ArrayLike, y: ArrayLike) -> None:
solver = SinkhornSolver()
out = solver(a=jnp.ones(len(x)) / len(x), b=jnp.ones(len(y)) / len(y), xy=(x, y), epsilon=1e-1)
a, b = out.a, out.b
assert isinstance(a, jnp.ndarray)
assert a.shape == (out.shape[0],)
assert isinstance(b, jnp.ndarray)
assert b.shape == (out.shape[1],)
assert isinstance(out.converged, bool)
assert isinstance(out.cost, float)
assert out.cost >= 0
assert out.shape == (x.shape[0], y.shape[0])
@pytest.mark.parametrize("batched", [False, True])
@pytest.mark.parametrize("rank", [-1, 5])
def test_push(
self,
x: Geom_t,
y: Geom_t,
ab: Tuple[ArrayLike, ArrayLike],
rank: int,
batched: bool,
) -> None:
a, _ = ab
a, ndim = (a, a.shape[1]) if batched else (a[:, 0], None)
solver = SinkhornSolver(rank=rank)
out = solver(a=jnp.ones(len(x)) / len(x), b=jnp.ones(len(y)) / len(y), xy=(x, y))
p = out.push(a, scale_by_marginals=False)
assert isinstance(out, BaseDiscreteSolverOutput)
assert isinstance(p, jnp.ndarray)
if batched:
assert p.shape == (out.shape[1], ndim)
else:
assert p.shape == (out.shape[1],)
@pytest.mark.parametrize("batched", [False, True])
@pytest.mark.parametrize("solver_t", [GWSolver])
def test_pull(
self,
x: ArrayLike,
y: ArrayLike,
xy: ArrayLike,
ab: Tuple[ArrayLike, ArrayLike],
solver_t: Type[OTSolver[O]],
batched: bool,
) -> None:
_, b = ab
b, ndim = (b, b.shape[1]) if batched else (b[:, 0], None)
xx, yy = xy
solver = solver_t()
out = solver(a=jnp.ones(len(x)) / len(x), b=jnp.ones(len(y)) / len(y), x=x, y=y, xy=(xx, yy), alpha=0.5)
p = out.pull(b, scale_by_marginals=False)
assert isinstance(out, BaseDiscreteSolverOutput)
assert isinstance(p, jnp.ndarray)
if batched:
assert p.shape == (out.shape[0], ndim)
else:
assert p.shape == (out.shape[0],)
@pytest.mark.parametrize("batched", [False, True])
@pytest.mark.parametrize("forward", [False, True])
def test_scale_by_marginals(self, x: Geom_t, ab: Tuple[ArrayLike, ArrayLike], forward: bool, batched: bool) -> None:
solver = SinkhornSolver()
a, _ = ab
z = a if batched else a[:, 0]
out = solver(a=jnp.ones(len(x)) / len(x), b=jnp.ones(len(x)) / len(x), xy=(x, x))
p = (out.push if forward else out.pull)(z, scale_by_marginals=True)
if batched:
np.testing.assert_allclose(p.sum(axis=0), z.sum(axis=0))
else:
np.testing.assert_allclose(p.sum(), z.sum())
@pytest.mark.parametrize("device", [None, "cpu", "cpu:0", "cpu:1", "explicit"])
def test_to_device(self, x: Geom_t, device: Optional[Device_t]) -> None:
# simple integration test
solver = SinkhornSolver()
if device == "explicit":
device = jax.devices()[0]
_ = solver(a=jnp.ones(len(x)) / len(x), b=jnp.ones(len(x)) / len(x), xy=(x, x), device=device)
elif device == "cpu:1":
with pytest.raises(IndexError, match=r"Unable to fetch the device with `id=1`."):
_ = solver(a=jnp.ones(len(x)) / len(x), b=jnp.ones(len(x)) / len(x), xy=(x, x), device=device)
else:
_ = solver(a=jnp.ones(len(x)) / len(x), b=jnp.ones(len(x)) / len(x), xy=(x, x), device=device)
class TestOutputPlotting(PlotTester, metaclass=PlotTesterMeta):
def test_plot_costs(self, x: Geom_t, y: Geom_t):
out = GWSolver()(a=jnp.ones(len(x)) / len(x), b=jnp.ones(len(y)) / len(y), x=x, y=y, alpha=1.0)
out.plot_costs()
def test_plot_costs_last(self, x: Geom_t, y: Geom_t):
out = GWSolver(rank=2)(a=jnp.ones(len(x)) / len(x), b=jnp.ones(len(y)) / len(y), x=x, y=y, alpha=1.0)
out.plot_costs(last=3)
def test_plot_errors_sink(self, x: Geom_t, y: Geom_t):
out = SinkhornSolver()(a=jnp.ones(len(x)) / len(x), b=jnp.ones(len(y)) / len(y), xy=(x, y))
out.plot_errors()
def test_plot_errors_gw(self, x: Geom_t, y: Geom_t):
out = GWSolver(store_inner_errors=True)(
a=jnp.ones(len(x)) / len(x), b=jnp.ones(len(y)) / len(y), x=x, y=y, alpha=1.0
)
out.plot_errors()