Skip to content

[MRG] Fix dmmot_monge_1dgrid_loss raising on backward - #923

Open
xihaian251 wants to merge 3 commits into
PythonOT:masterfrom
xihaian251:fix/dmmot-gradient-structure
Open

xihaian251 wants to merge 3 commits into
PythonOT:masterfrom
xihaian251:fix/dmmot-gradient-structure

Conversation

@xihaian251

@xihaian251 xihaian251 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Component: ot.lp.dmmot. No public API change, no new dependency, no backend is modified.

Motivation and context / Related issue

ot.lp.dmmot_monge_1dgrid_loss is meant to be differentiable — the call site is even
commented "define forward/backward relations for pytorch" — but calling backward on it
raises on every backend that supports autodiff.

Minimal reproduction:

import numpy as np, ot, torch

A = np.vstack([ot.datasets.make_1D_gauss(4, m=20, s=5),
               ot.datasets.make_1D_gauss(4, m=60, s=8)]).T
At = torch.from_numpy(A).requires_grad_()
ot.lp.dmmot_monge_1dgrid_loss(At).backward()
  • PyTorch: TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'Tensor'
  • TensorFlow: ValueError: custom_gradient function expected to return 1 gradients, but returned 2 instead.

The forward value is correct in both cases; only the backward pass fails.

Root cause. In ot/lp/dmmot.py:

obj = nx.set_gradients(obj, (A_copy), (dual))

(A_copy) and (dual) are not tuples — parentheses around a single name are only
grouping. So inputs is one tensor of shape (n, d), while grads is a Python list of
d NumPy arrays
, each of length n. set_gradients expects one gradient per input, in
the backend's own array type. Instrumented at the call site:

inputs: tuple? no   type=ndarray   shape=(4, 2)
grads:  tuple? no   type=list      len=2   element types=['ndarray', 'ndarray']   shapes=[(4,), (4,)]

Neither existing test in test/test_dmmot.py calls backward: one compares a forward value
after nx.to_numpy, the other checks optimizer output. As a result this line has never
been executed in a backward pass by the test suite.

No existing issue covers this; I did not open one to avoid duplicating traffic, but I am
happy to create one if maintainers prefer.

Implementation

# dual[j] is the potential of marginal j: the gradient wrt column j of A
grad_A = np.stack(dual, axis=1)
obj = nx.set_gradients(obj, (A_copy,), (nx.from_numpy(grad_A, type_as=A_copy),))
  • dual[j] is the dual potential of marginal j, and the dual objective is
    sum_j <A[:, j], dual[j]>, so the gradient with respect to A is the potentials stacked
    as columns: np.stack(dual, axis=1) has shape (n, d), matching A exactly.
  • nx.from_numpy(..., type_as=A_copy) converts to the backend's array type and matches the
    input's dtype and device, which the previous NumPy list did not.
  • (A_copy,) and (grad,) are genuine single-element tuples, so the input/gradient
    arity is unambiguous for every backend.
  • The potentials are passed through as computed. They are defined only up to a
    per-marginal constant, and on mass-preserving perturbations any such constant
    contributes nothing to the directional derivative. I deliberately did not centre
    them: independent per-column centring is not guaranteed to preserve the multi-marginal
    dual constraints, so this PR keeps the change purely structural. The returned gradient
    is exactly log["dual"] stacked as columns.
  • Forward value, log["dual"] and log["dual objective"] are unchanged.

How has this been tested (if it applies)

New non-regression tests. 8 cases in test/test_dmmot.py:
test_dmmot_monge_1dgrid_loss_gradients over coeff ∈ {1.0, 7.0, -2.0, 0.0} ×
(n, d) ∈ {(4, 2), (6, 3)}. Each asserts the forward value is unchanged, the gradient
dtype matches the input, and the gradient equals coeff times the stacked duals. The
non-square (6, 3) case matters because it makes the input count differ from the old
list length, so an arity-only fix would not pass it. coeff = 0.0 and -2.0 cover
external scaling of the loss.

Negative control. The unpatched function was extracted verbatim from a clean checkout
of master and bound onto the patched tree, then the same test file was run:

8 failed, 6 passed

All 8 new cases fail on master — including coeff = 1.0, since this is a hard crash
rather than a scaling error — and all 6 pre-existing cases still pass.

Additional local checks (PyTorch). 4 input shapes (4,2), (6,3), (5,5), (8,2) ×
{float32, float64} × coeff ∈ {1, 7, -2, 0} = 32 combinations, all with
max|grad - coeff * expected| = 0.000e+00, gradient dtype and device preserved in every
case. The forward objective and log["dual"] are bit-identical to the unpatched baseline.

Test suites (local, two separate environments, not combined):

environment scope result
torch 2.10.0+cpu, jax 0.11.2, numpy 2.5.3, no TF test/test_dmmot.py 14 passed
torch 2.10.0+cpu, jax 0.11.2, numpy 2.5.3, no TF full test/ suite 3583 passed, 468 skipped, 6 xfailed, 0 failed
TensorFlow 2.21.0 CPU, numpy 2.5.4, no torch/jax test/test_dmmot.py 4 passed, 8 skipped (new cases are torch-guarded)

The unpatched baseline in the same environment gives 3575 passed / 468 skipped / 6 xfailed,
so the whole delta is the 8 new cases; skipped and xfailed are unchanged.

Lint: ruff 0.5.2 (the version pinned in .pre-commit-config.yaml), all four hooks —
check ot/, check --preview --select=NPY201 ot/, check --ignore=D103,D400,E402 test/,
format --check ot/ — all pass. git diff --check clean.

On CI: linux-torch is the job that actually exercises the new tests — all 8 are
listed individually as PASSED in its log (job total 2805 passed, 66 skipped, 6 xfailed).
In linux-tf and the other non-torch jobs they are SKIPPED, as intended by the
skipif(not torch) guard.

Backend capabilities not covered by this PR, stated explicitly:

  • TensorFlow. The gradient structure is fixed: the returned gradient now equals the
    stacked duals exactly. However TensorflowBackend.set_gradients separately returns
    grads unchanged and discards the upstream cotangent, so an external coefficient is
    still not propagated: measured grad(c * loss) == grad(loss) for c ∈ {1, 7, -2, 0}.
    That is an independent pre-existing defect in the backend, not addressed here. For
    that reason no TensorFlow regression test with c != 1 is added — it could not pass
    until the backend is fixed.
  • JAX. dmmot_monge_1dgrid_loss calls nx.to_numpy(A), so it is not traceable:
    jax.grad raises TracerArrayConversionError both before and after this change. This
    PR does not make the solver JAX-differentiable; that would require a separate
    rewrite of the solver internals.
  • Not tested locally: GPU, CuPy, torch.compile, and Python versions other than 3.13.

Local results are not a substitute for GitHub Actions; the checks on this branch are the
authoritative result.

PR checklist

  • I have read the CONTRIBUTING document.
  • The documentation is up-to-date with the changes I made (check build artifacts).
  • All tests passed, and additional code has been covered with new tests.
  • I have added the PR and Issue fix to the RELEASES.md file.

Notes on the checklist: no public signature or docstring changes, so no documentation
update is required, and no examples/ script is needed since CONTRIBUTING asks for one
only when adding functionality. The tests box reflects both the local runs above and the
passing CI jobs on this branch. The RELEASES.md entry is added under #### Closed issues
in the style of the neighbouring entries, and references this PR.

@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.07%. Comparing base (3367480) to head (da4dde0).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #923   +/-   ##
=======================================
  Coverage   97.07%   97.07%           
=======================================
  Files         130      130           
  Lines       27095    27113   +18     
=======================================
+ Hits        26302    26320   +18     
  Misses        793      793           
🚀 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