Skip to content

[MRG] Restore dual feasibility at zero-mass bins in d-MMOT - #933

Open
xihaian251 wants to merge 1 commit into
PythonOT:masterfrom
xihaian251:fix/dmmot-zero-mass-dual-feasibility
Open

xihaian251 wants to merge 1 commit into
PythonOT:masterfrom
xihaian251:fix/dmmot-zero-mass-dual-feasibility

Conversation

@xihaian251

Copy link
Copy Markdown
Contributor

Summary

ot.lp.dmmot_monge_1dgrid_loss can return dual potentials that are not feasible when an input histogram contains exact zero-mass bins. The dual objective still equals the primal value because the offending bins carry zero mass, but the returned array is not a dual certificate and therefore is not a valid subgradient for autograd.

Minimal exact example (d=2, n=2):

import numpy as np
import ot

A = np.array([[1.0, 1.0], [0.0, 0.0]])
obj, log = ot.lp.dmmot_monge_1dgrid_loss(A, log=True)
u, v = log["dual"]
C = np.array([[0.0, 1.0], [1.0, 0.0]])
print(C - np.asarray(u)[:, None] - np.asarray(v)[None, :])
# [[ 0,  1],
#  [ 0, -1]]

The true primal and the reported dual objective are both 0, but the returned dual violates feasibility at (1,1). The root cause is in the original greedy dual construction: the first main-loop update raises one marginal's potential when a bin is exhausted, while a zero-mass bin in the other marginal never receives the compensating potential. Terminal fill-in does not repair it. PR #923 does not create this defect; it faithfully exposes the invalid dual through Torch backward.

Fix

After the greedy dual construction, exact zero-mass bins are lowered to

B = (d - 1) * (n - 1).

Greedy increments for the max-min Monge cost lie in {−1,0,+1}, so original potentials are bounded in absolute value by n−1. Any tuple containing a zero bin therefore has dual sum at most −B+(d−1)(n−1)=0≤c. Tuples whose indices all have positive mass are feasible by an ε-perturbation argument: strictly positive marginals admit the original Monge greedy primal-dual theorem; positive-bin potentials converge as ε→0; feasibility inequalities are closed. Changing zero-mass potentials leaves the dual objective unchanged, so the repaired dual remains optimal.

This returns a valid dual subgradient representative at zero-mass boundaries. It does not claim that the value is differentiable there, nor that the returned subgradient is unique.

Evidence, separated by method

  1. Theory. The positive-support feasibility lemma is proved by the exact mathematical ε→0 limit and closedness of linear inequalities. The zero-bin bound is proved from the greedy increment invariant and the Monge cost structure. This argument is not based on finite sampling.
  2. Exact/rational and symbolic verification. The canonical counterexample uses only 0/1 data. Wolfram symbolic certificates cover weak duality, the subgradient inequality, zero-bin objective invariance, the tighter feasibility bound, and gauge invariance on the probability-simplex tangent space.
  3. Floating-point numerical tests. A total of 35,410 deterministic and random cases were evaluated, including 31,989 cases with exact zero masses; no feasibility or objective failure was found. A separate set of 720 ε-perturbation comparisons checked that positive-bin potentials converge unchanged. These floating-point tests corroborate the theorem; rounding can affect tie-breaking in the greedy loop and is not part of the exact limit proof.
  4. Independent LP cross-validation. An independent SciPy linprog Oracle was run on a 958-case subset of the above suite, with no objective or feasibility failures. The remaining cases were checked by direct constraint evaluation and primal/dual objective comparison, not by linprog.

Combined with PR #923, Torch shape, dtype and cotangent tests pass, and the zero-boundary one-sided subgradient inequality holds. Local real merges with #923 in both orders produce identical production code and pass 17/17 combined d-MMOT tests.

Compatibility and cost

Strictly positive and near-zero positive inputs are unchanged. The repair adds one O(nd) zero scan and at most O(nd) assignments. The full solver is O(nd²) time when d is variable because each greedy iteration recomputes max/min over the d coordinates; with d fixed, it is O(n). Space is O(nd).

Scope and limitations

This PR fixes the original dual construction. PR #923 fixes a separate Torch set_gradients registration bug; the two patches are independent and complementary. CUDA and TensorFlow were not tested; JAX grad remains unsupported by the current NumPy-conversion path. No unique-gradient claim is made at nonsmooth zero-mass boundaries.

@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.09%. Comparing base (367355d) to head (af5fc56).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #933   +/-   ##
=======================================
  Coverage   97.09%   97.09%           
=======================================
  Files         130      130           
  Lines       27229    27258   +29     
=======================================
+ Hits        26437    26466   +29     
  Misses        792      792           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant