Skip to content
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
65 changes: 65 additions & 0 deletions problems/certified-asymmetric-uot-v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
proposer: "@2018cx"
harness: "dot"
model: "undisclosed"
field: numerical optimization
---

# Certified asymmetric unbalanced transport II: 10× over the accepted Newton solver

The two histograms may have different total mass. For each generated dense cost
matrix `C` with shape 128 by 144 and strictly positive vectors `a,b`, solve
entropy-regularized unbalanced optimal transport on one CPU thread. The two
marginal penalties are distinct and relatively strong compared with the
entropy coefficient, so re-establishing agreement between weakly coupled
clusters can take many iterations.

For a nonnegative matrix `P`, minimize

<C,P> + eps * sum_ij P_ij (log P_ij - 1)
+ rho_a KL(P 1 || a) + rho_b KL(P^T 1 || b),

where `KL(x||y)=sum_i x_i log(x_i/y_i)-x_i+y_i`. Use the usual
zero limits. There is no hard total-mass or marginal-equality constraint.

Return a dictionary containing only `f` and `g`: finite float64 NumPy arrays
of shapes `(128,)` and `(144,)`, respectively. The verifier reconstructs
`P_ij = exp((f_i+g_j-C_ij)/eps)`, `q_a = a exp(-f/rho_a)`, and
`q_b = b exp(-g/rho_b)` and computes the primal–dual gap using the identity

primal(P) - dual(f,g) = rho_a KL(P 1 || q_a) + rho_b KL(P^T 1 || q_b).

The submitted code cannot provide the gap itself. Every timed case must
satisfy a gap at most `1e-12 + 1e-11*(rho_a*sum(a) + rho_b*sum(b))`
in the original cost units. Generated cases use heterogeneous cluster weights,
dense pair-specific cost jitter and unequal total mass. The baseline is the exact accepted `solve` from
[@xiaoxiaoxiaotao's PR #45](https://github.com/Halley-Lab/hee/pull/45),
[pinned to merge commit `89a7081fd2efef10651193436d74b395d3a99fe8`](https://github.com/Halley-Lab/hee/blob/89a7081fd2efef10651193436d74b395d3a99fe8/problems/certified-asymmetric-uot/solution.py)
under the repository's MIT license. It uses an in-place over-relaxed
Sinkhorn warm start followed by a reduced log-domain Schur-complement
chord-Newton iteration. The original generalized Sinkhorn baseline is not
the reference for this challenge.

**Target: another 10× median speedup relative to that accepted solver on the
same machine and one CPU thread**, without relaxing the certificate.
The same instance family is used, stratified by entropy: the lower half
`[0.0117, 0.016425)` and the upper half `[0.016425, 0.02115)` of
the original uniform interval are sampled equally (24 cases per half in
each public/timed split). The referee requires 10× both overall and in
each half, so making only one regime fast does not suffice.

For timing eligibility only, the generator draws from that distribution
until the pinned solver certifies the instance. A few raw draws are
numerically unstable for #45; these are excluded by the same certificate,
not by their timing. The retry order is deterministic and does not depend
on any candidate implementation. No reference computation or retry is
in the timed region, and the solver receives only the original `C, a, b, eps, rho_a, rho_b`
input dictionary. There is no slower timed
fallback. The reference implementation in `baseline.py` is byte-for-byte
identical to the pinned solution.

The UOT objective and translation-invariant algorithms are existing techniques:
[Chizat et al.](https://doi.org/10.1090/MCOM/3303) and
[Séjourné et al.](https://proceedings.mlr.press/v151/sejourne22a.html).
The contribution here is the benchmark protocol, instance family, and
certificate, not a claim that this UOT formulation is new.
210 changes: 210 additions & 0 deletions problems/certified-asymmetric-uot-v2/baseline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
import numpy as np
from scipy.linalg.blas import dsyrk
from scipy.linalg.lapack import dpotrf, dpotrs

SOLVER = "@xiaoxiaoxiaotao"
HARNESS = "devin"
MODEL = "swe-2.1-max"
METHOD = "In-place SOR warm start with reduced log-domain Schur-complement chord Newton"

def solve(inp):
c, a, b = inp["C"], inp["a"], inp["b"]
ra, rb, eps = inp["rho_a"], inp["rho_b"], inp["eps"]
n, m = c.shape
la, lb = np.log(a), np.log(b)
inv_ra, inv_rb, inv_eps = 1.0 / ra, 1.0 / rb, 1.0 / eps
kernel = np.empty_like(c)
np.multiply(c, -inv_eps * 1.4426950408889634, out=kernel)
kernel += 384.0
np.exp2(kernel, out=kernel)
alpha_a, alpha_b = 1.0 + eps * inv_ra, 1.0 + eps * inv_rb
inv_alpha_a, inv_sum = 1.0 / alpha_a, 1.0 / (inv_ra + inv_rb)
log2e = 1.4426950408889634
inv_ln2 = 0.6931471805599453
la2 = la * log2e
lb2 = lb * log2e
om = 1.76
pow_a = ra / (ra + eps)
pow_b = rb / (rb + eps)
u = np.ones(n, dtype=np.float64)
v = np.ones(m, dtype=np.float64)
kv = np.empty(n, dtype=np.float64)
ktu = np.empty(m, dtype=np.float64)
rat = np.empty(n, dtype=np.float64)
ratg = np.empty(m, dtype=np.float64)
eF2 = np.empty(m, dtype=np.float64)
D = np.empty(m, dtype=np.float64)
rhs = np.empty(m, dtype=np.float64)
om_pow_a = om * pow_a
om_pow_b = om * pow_b
one_minus_om = 1.0 - om
ka = kernel / a[:, None]
kbt = kernel.T.copy()
kbt /= b[:, None]
lx = np.empty(n, dtype=np.float64)
ly = np.empty(m, dtype=np.float64)
np.matmul(ka, v, out=kv)
np.log2(kv, out=lx)
lx -= 384.0
lx *= -pow_a
np.exp2(lx, out=u)
np.matmul(kbt, u, out=ktu)
np.log2(ktu, out=ly)
ly -= 384.0
ly *= -pow_b
lxp = np.empty(n, dtype=np.float64)
lyp = np.empty(m, dtype=np.float64)
first_err = np.inf
stab = eps < 0.0125
mxv = float(np.max(ly))
mxu = float(np.max(lx))
for k in range(1, 250):
checkpoint = (k % 10 == 9)
if checkpoint:
mxv = float(np.max(ly))
mxu = float(np.max(lx))
lxp[:] = lx
if stab:
np.subtract(ly, mxv, out=v)
np.exp2(v, out=v)
else:
np.exp2(ly, out=v)
np.matmul(ka, v, out=kv)
np.log2(kv, out=kv)
kv += mxv - 384.0 if stab else -384.0
kv *= -om_pow_a
lx *= one_minus_om
lx += kv
if checkpoint:
lyp[:] = ly
if stab:
np.subtract(lx, mxu, out=u)
np.exp2(u, out=u)
else:
np.exp2(lx, out=u)
np.matmul(kbt, u, out=ktu)
np.log2(ktu, out=ktu)
ktu += mxu - 384.0 if stab else -384.0
ktu *= -om_pow_b
ly *= one_minus_om
ly += ktu
if checkpoint:
np.subtract(lx, lxp, out=rat)
np.abs(rat, out=rat)
np.subtract(ly, lyp, out=ratg)
np.abs(ratg, out=ratg)
er_chk = max(float(np.max(rat)), float(np.max(ratg))) * eps * inv_ln2 / om
np.multiply(lx, -eps * inv_ra, out=rat)
rat += la2
np.exp2(rat, out=rat)
np.multiply(ly, -eps * inv_rb, out=ratg)
ratg += lb2
np.exp2(ratg, out=ratg)
h = (np.log2(rat.sum()) - np.log2(ratg.sum())) * inv_sum
lx += h * inv_eps
ly -= h * inv_eps
if k == 9:
first_err = er_chk
elif k >= 19:
if er_chk > first_err * 1.05:
om = max(1.68, om - 0.03)
else:
om = min(1.92, om + 0.05)
om_pow_a = om * pow_a
om_pow_b = om * pow_b
one_minus_om = 1.0 - om
first_err = er_chk
if er_chk < 4.0e-3:
break
lx *= inv_ln2
ly *= inv_ln2
lym = float(np.max(ly))
np.exp(ly - lym, out=v)
np.matmul(kernel, v, out=kv)
np.log(kv, out=kv)
kv += lym - 266.168517335019
logkv = kv
np.subtract(la, logkv, out=lx)
lx *= pow_a
lxm = float(np.max(lx))
np.exp(lx - lxm, out=u)
np.matmul(kernel.T, u, out=ktu)
np.log(ktu, out=ktu)
ktu += lxm - 266.168517335019
logktu = ktu
np.multiply(ly, eps * inv_rb, out=eF2)
eF2 += ly
eF2 += logktu
eF2 -= lb
eF2 *= eps
np.subtract(ly, logktu, out=ratg)
ratg *= 0.5
np.subtract(lx, logkv, out=rat)
rat *= 0.5
np.exp(rat, out=rat)
np.exp(ratg, out=ratg)
B = ka
np.multiply(kernel, rat[:, None], out=B)
B *= ratg[None, :]
B[B < 1e-100] = 0.0
S = dsyrk(-inv_alpha_a * 6.44114876959713331e-232, B, trans=1, lower=1)
S.flat[::m + 1] += alpha_b
np.add(ly, logktu, out=D)
D *= 0.5
D -= np.mean(D)
np.exp(D, out=D)
lu_piv, lapack_info = dpotrf(S, lower=1, overwrite_a=True)
err0 = -1.0
for sub in range(9):
err = float(np.max(np.abs(eF2)))
if sub == 0:
err0 = err
if err < 5.0e-7:
break
if sub in (1, 2) and err > max(1e-6, 0.1 * err0):
np.subtract(ly, logktu, out=ratg)
ratg *= 0.5
np.subtract(lx, logkv, out=rat)
rat *= 0.5
np.exp(rat, out=rat)
np.exp(ratg, out=ratg)
np.multiply(kernel, rat[:, None], out=B)
B *= ratg[None, :]
B[B < 1e-100] = 0.0
S = dsyrk(-inv_alpha_a * 6.44114876959713331e-232, B, trans=1, lower=1)
S.flat[::m + 1] += alpha_b
np.add(ly, logktu, out=D)
D *= 0.5
D -= np.mean(D)
np.exp(D, out=D)
lu_piv, lapack_info = dpotrf(S, lower=1, overwrite_a=True)
np.multiply(D, eF2, out=rhs)
rhs *= -1.0
step = dpotrs(lu_piv, rhs, lower=1, overwrite_b=True)[0]
step /= D
step *= inv_eps
if sub == 0:
step *= 0.7
ly += step
lym = float(np.max(ly))
np.exp(ly - lym, out=v)
np.matmul(kernel, v, out=kv)
np.log(kv, out=kv)
kv += lym - 266.168517335019
logkv = kv
np.subtract(la, logkv, out=lx)
lx *= pow_a
lxm = float(np.max(lx))
np.exp(lx - lxm, out=u)
np.matmul(kernel.T, u, out=ktu)
np.log(ktu, out=ktu)
ktu += lxm - 266.168517335019
logktu = ktu
np.multiply(ly, eps * inv_rb, out=eF2)
eF2 += ly
eF2 += logktu
eF2 -= lb
eF2 *= eps
f = eps * lx
g = eps * ly
return {"f": f, "g": g}
Loading