|
1 | 1 | # Partly adapted from https://github.com/AvivNavon/nash-mtl — MIT License, Copyright (c) 2022 Aviv Navon. |
2 | 2 | # See NOTICES for the full license text. |
3 | 3 |
|
4 | | -from torchjd.aggregation._mixins import Stateful |
| 4 | +from torchjd.aggregation._mixins import Stateful, _NonDifferentiable |
5 | 5 |
|
6 | 6 | from ._utils.check_dependencies import check_dependencies_are_installed |
7 | 7 | from ._weighting_bases import _MatrixWeighting |
|
15 | 15 | from torch import Tensor |
16 | 16 |
|
17 | 17 | from ._aggregator_bases import WeightedAggregator |
18 | | -from ._utils.non_differentiable import raise_non_differentiable_error |
19 | 18 |
|
20 | 19 |
|
21 | | -class _NashMTLWeighting(_MatrixWeighting, Stateful): |
| 20 | +# Non-differentiable: the cvxpy solver operates on numpy arrays, breaking the autograd graph. |
| 21 | +class _NashMTLWeighting(_NonDifferentiable, Stateful, _MatrixWeighting): |
22 | 22 | """ |
23 | 23 | :class:`~torchjd.aggregation._mixins.Stateful` |
24 | 24 | :class:`~torchjd.aggregation.Weighting` [:class:`~torchjd.linalg.Matrix`] that |
@@ -199,7 +199,7 @@ def reset(self) -> None: |
199 | 199 | self.prvs_alpha = np.ones(self.n_tasks, dtype=np.float32) |
200 | 200 |
|
201 | 201 |
|
202 | | -class NashMTL(WeightedAggregator, Stateful): |
| 202 | +class NashMTL(_NonDifferentiable, Stateful, WeightedAggregator): |
203 | 203 | """ |
204 | 204 | :class:`~torchjd.aggregation._mixins.Stateful` |
205 | 205 | :class:`~torchjd.aggregation.WeightedAggregator` as proposed in Algorithm 1 of |
@@ -253,9 +253,6 @@ def __init__( |
253 | 253 | ), |
254 | 254 | ) |
255 | 255 |
|
256 | | - # This prevents considering the computed weights as constant w.r.t. the matrix. |
257 | | - self.register_full_backward_pre_hook(raise_non_differentiable_error) |
258 | | - |
259 | 256 | @property |
260 | 257 | def n_tasks(self) -> int: |
261 | 258 | return self.weighting.n_tasks |
|
0 commit comments