Skip to content

Commit 6c02f3b

Browse files
authored
fix(aggregation): Fix __call__ docs (#693)
* Fix inheritance order in non-differentiable aggregators and weightings * Fix _NonDifferentiable docstring
1 parent bb52b6f commit 6c02f3b

11 files changed

Lines changed: 20 additions & 21 deletions

File tree

‎src/torchjd/aggregation/_cagrad.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
# Non-differentiable: the cvxpy solver operates on numpy arrays, breaking the autograd graph.
21-
class CAGradWeighting(_WithOptionalDeps, _NonDifferentiable, _GramianWeighting):
21+
class CAGradWeighting(_WithOptionalDeps, _GramianWeighting, _NonDifferentiable):
2222
_REQUIRED_DEPS = ["numpy", "cvxpy", "clarabel"]
2323
_INSTALL_HINT = 'Install them with: pip install "torchjd[cagrad]"'
2424
"""
@@ -94,7 +94,7 @@ def norm_eps(self, value: float) -> None:
9494
self._norm_eps = value
9595

9696

97-
class CAGrad(_NonDifferentiable, GramianWeightedAggregator):
97+
class CAGrad(GramianWeightedAggregator, _NonDifferentiable):
9898
"""
9999
:class:`~torchjd.aggregation.GramianWeightedAggregator` as defined in Algorithm 1 of
100100
`Conflict-Averse Gradient Descent for Multi-task Learning

‎src/torchjd/aggregation/_config.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
# Non-differentiable: the pseudoinverse and the normalization are not differentiable in this context.
17-
class ConFIG(_NonDifferentiable, Aggregator):
17+
class ConFIG(Aggregator, _NonDifferentiable):
1818
"""
1919
:class:`~torchjd.aggregation.Aggregator` as defined in Equation 2 of `ConFIG:
2020
Towards Conflict-free Training of Physics Informed Neural Networks

‎src/torchjd/aggregation/_dualproj.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
# Non-differentiable: the QP solver operates on numpy arrays, breaking the autograd graph.
14-
class DualProjWeighting(_NonDifferentiable, _GramianWeighting):
14+
class DualProjWeighting(_GramianWeighting, _NonDifferentiable):
1515
r"""
1616
:class:`~torchjd.aggregation.Weighting` [:class:`~torchjd.linalg.PSDMatrix`]
1717
giving the weights of :class:`~torchjd.aggregation.DualProj`.
@@ -53,7 +53,7 @@ def projector(self, value: DualConeProjector | None) -> None:
5353
self._projector = projector_or_default(value)
5454

5555

56-
class DualProj(_NonDifferentiable, GramianWeightedAggregator):
56+
class DualProj(GramianWeightedAggregator, _NonDifferentiable):
5757
r"""
5858
:class:`~torchjd.aggregation.GramianWeightedAggregator` that averages the rows of the input
5959
matrix, and projects the result onto the dual cone of the rows of the matrix. This corresponds

‎src/torchjd/aggregation/_fairgrad.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
# Non-differentiable: the scipy solver operates on numpy arrays, breaking the autograd graph.
24-
class FairGradWeighting(_WithOptionalDeps, _NonDifferentiable, _GramianWeighting):
24+
class FairGradWeighting(_WithOptionalDeps, _GramianWeighting, _NonDifferentiable):
2525
r"""
2626
:class:`~torchjd.aggregation.Weighting` [:class:`~torchjd.linalg.PSDMatrix`] giving the
2727
weights of :class:`~torchjd.aggregation.FairGrad`, as defined in Equation 4 of `Fair Resource
@@ -78,7 +78,7 @@ def alpha(self, value: float) -> None:
7878
self._alpha = value
7979

8080

81-
class FairGrad(_NonDifferentiable, GramianWeightedAggregator):
81+
class FairGrad(GramianWeightedAggregator, _NonDifferentiable):
8282
r"""
8383
:class:`~torchjd.aggregation.GramianWeightedAggregator` using the step decision of Algorithm 1
8484
of `Fair Resource Allocation in Multi-Task Learning

‎src/torchjd/aggregation/_graddrop.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def _identity(P: Tensor) -> Tensor:
1414

1515

1616
# Non-differentiable: the sign-based random masking is not differentiable.
17-
class GradDrop(_NonDifferentiable, Aggregator):
17+
class GradDrop(Aggregator, _NonDifferentiable):
1818
"""
1919
:class:`~torchjd.aggregation.Aggregator` that applies the gradient combination
2020
steps from GradDrop, as defined in lines 10 to 15 of Algorithm 1 of `Just Pick a Sign:

‎src/torchjd/aggregation/_gradvac.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
# Non-differentiable: weights are modified in-place during the gradient correction loop.
16-
class GradVacWeighting(_NonDifferentiable, Stateful, _GramianWeighting):
16+
class GradVacWeighting(_GramianWeighting, Stateful, _NonDifferentiable):
1717
r"""
1818
:class:`~torchjd.aggregation._mixins.Stateful`
1919
:class:`~torchjd.aggregation.Weighting` [:class:`~torchjd.linalg.PSDMatrix`]
@@ -128,7 +128,7 @@ def _ensure_state(self, m: int, dtype: torch.dtype) -> None:
128128
self._state_key = key
129129

130130

131-
class GradVac(_NonDifferentiable, Stateful, GramianWeightedAggregator):
131+
class GradVac(GramianWeightedAggregator, Stateful, _NonDifferentiable):
132132
r"""
133133
:class:`~torchjd.aggregation._mixins.Stateful`
134134
:class:`~torchjd.aggregation.GramianWeightedAggregator` implementing the aggregation step of

‎src/torchjd/aggregation/_imtl_g.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
# Non-differentiable: differentiating through pinv(gramian) would give incorrect gradients.
12-
class IMTLGWeighting(_NonDifferentiable, _GramianWeighting):
12+
class IMTLGWeighting(_GramianWeighting, _NonDifferentiable):
1313
"""
1414
:class:`~torchjd.aggregation.Weighting` [:class:`~torchjd.linalg.PSDMatrix`]
1515
giving the weights of :class:`~torchjd.aggregation.IMTLG`.
@@ -25,7 +25,7 @@ def forward(self, gramian: PSDMatrix, /) -> Tensor:
2525
return weights
2626

2727

28-
class IMTLG(_NonDifferentiable, GramianWeightedAggregator):
28+
class IMTLG(GramianWeightedAggregator, _NonDifferentiable):
2929
"""
3030
:class:`~torchjd.aggregation.GramianWeightedAggregator` generalizing the method described in
3131
`Towards Impartial Multi-task Learning <https://discovery.ucl.ac.uk/id/eprint/10120667/>`_.

‎src/torchjd/aggregation/_mixins.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ class _NonDifferentiable(nn.Module):
1919
the call in :func:`torch.no_grad`.
2020
2121
.. warning::
22-
This mixin must appear **before** any :class:`torch.nn.Module` base class in the inheritance
23-
list. Placing it after will silently have no effect, because :meth:`__call__` would be
24-
resolved to :class:`torch.nn.Module` before reaching this mixin.
22+
Placing this mixin *before* the primary base will cause it to shadow the primary class's
23+
:meth:`__call__` signature in generated documentation.
2524
"""
2625

2726
def __call__(self, *args: Any, **kwargs: Any) -> Any:

‎src/torchjd/aggregation/_nash_mtl.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
# Non-differentiable: the cvxpy solver operates on numpy arrays, breaking the autograd graph.
24-
class _NashMTLWeighting(_WithOptionalDeps, _NonDifferentiable, Stateful, _MatrixWeighting):
24+
class _NashMTLWeighting(_WithOptionalDeps, _MatrixWeighting, Stateful, _NonDifferentiable):
2525
_REQUIRED_DEPS = ["numpy", "cvxpy", "ecos"]
2626
_INSTALL_HINT = 'Install them with: pip install "torchjd[nash_mtl]"'
2727
"""
@@ -204,7 +204,7 @@ def reset(self) -> None:
204204
self.prvs_alpha = np.ones(self.n_tasks, dtype=np.float32)
205205

206206

207-
class NashMTL(_NonDifferentiable, Stateful, WeightedAggregator):
207+
class NashMTL(WeightedAggregator, Stateful, _NonDifferentiable):
208208
"""
209209
:class:`~torchjd.aggregation._mixins.Stateful`
210210
:class:`~torchjd.aggregation.WeightedAggregator` as proposed in Algorithm 1 of

‎src/torchjd/aggregation/_pcgrad.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
# Non-differentiable: weights are modified in-place during the gradient projection loop.
14-
class PCGradWeighting(_NonDifferentiable, _GramianWeighting):
14+
class PCGradWeighting(_GramianWeighting, _NonDifferentiable):
1515
"""
1616
:class:`~torchjd.aggregation.Weighting` [:class:`~torchjd.linalg.PSDMatrix`]
1717
giving the weights of :class:`~torchjd.aggregation.PCGrad`.
@@ -47,7 +47,7 @@ def forward(self, gramian: PSDMatrix, /) -> Tensor:
4747
return weights.to(device)
4848

4949

50-
class PCGrad(_NonDifferentiable, GramianWeightedAggregator):
50+
class PCGrad(GramianWeightedAggregator, _NonDifferentiable):
5151
"""
5252
:class:`~torchjd.aggregation.GramianWeightedAggregator` as defined in Algorithm 1 of
5353
`Gradient Surgery for Multi-Task Learning <https://arxiv.org/pdf/2001.06782.pdf>`_.

0 commit comments

Comments
 (0)