Skip to content

Commit ab4b296

Browse files
authored
fix(aggregation): Use Gramian dtype in AlignedMTL rank tolerance (#792)
1 parent b98cc51 commit ab4b296

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ changelog does not include internal changes that do not affect the user.
1818
matrix are almost equal. Rounding errors could make the squared distance between such rows
1919
slightly negative, giving a `nan` distance that was then ignored when computing the scores.
2020
Squared distances are now clamped to be non-negative before taking the square root.
21+
- Fixed `AlignedMTL` and `AlignedMTLWeighting` ignoring tasks with a much smaller gradient than
22+
the others when the input is in `float64`. The tolerance used to find the rank of the Gramian was
23+
always based on the machine epsilon of the default dtype (usually `float32`) instead of the dtype
24+
of the Gramian, so valid small eigenvalues were discarded.
2125
- Fixed `Engine.compute_gramian` silently ignoring the contribution of a module when one of its
2226
forward passes had no associated backward pass (e.g. because its output was detached). It now
2327
raises a `ValueError` instead.

‎src/torchjd/aggregation/_aligned_mtl.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def _compute_balance_transformation(
6161
scale_mode: SUPPORTED_SCALE_MODE = "min",
6262
) -> Tensor:
6363
lambda_, V = torch.linalg.eigh(M, UPLO="U") # More modern equivalent to torch.symeig
64-
tol = torch.max(lambda_) * len(M) * torch.finfo().eps
64+
tol = torch.max(lambda_) * len(M) * torch.finfo(M.dtype).eps
6565
rank = sum(lambda_ > tol)
6666

6767
if rank == 0:

‎tests/unit/aggregation/test_aligned_mtl.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import torch
22
from pytest import mark, raises
33
from torch import Tensor
4-
from utils.tensors import ones_
4+
from torch.testing import assert_close
5+
from utils.tensors import ones_, tensor_
56

67
from torchjd.aggregation import AlignedMTL, ConstantWeighting
78

@@ -59,3 +60,9 @@ def test_scale_mode_setter_updates_value() -> None:
5960
A.scale_mode = "rmse"
6061
assert A.scale_mode == "rmse"
6162
assert A.gramian_weighting.scale_mode == "rmse"
63+
64+
65+
def test_float64_small_eigenvalue_is_kept() -> None:
66+
J = tensor_([[1.0, 0.0], [0.0, 1e-4]], dtype=torch.float64)
67+
result = AlignedMTL()(J)
68+
assert_close(result, tensor_([5e-5, 5e-5], dtype=torch.float64))

0 commit comments

Comments
 (0)