Skip to content

Commit e9a5016

Browse files
feat(scalarization): Add UW (#721)
* Add UW * Move Stateful from torchjd.aggregation to torchjd so that it can be shared by the aggregation and the scalarization packages --------- Signed-off-by: ppraneth <pranethparuchuri@gmail.com> Co-authored-by: Valérian Rey <valerian.rey@gmail.com>
1 parent 7f693d9 commit e9a5016

18 files changed

Lines changed: 246 additions & 33 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ changelog does not include internal changes that do not affect the user.
1010

1111
### Added
1212

13+
- Added `UW` (Uncertainty Weighting) from [Multi-Task Learning Using Uncertainty to Weigh Losses
14+
for Scene Geometry and
15+
Semantics](https://openaccess.thecvf.com/content_cvpr_2018/papers/Kendall_Multi-Task_Learning_Using_CVPR_2018_paper.pdf),
16+
a `Scalarizer` that combines the values using learned per-task uncertainties. It is the first
17+
stateful, trainable scalarizer: its log-variances are an `nn.Parameter` that must be passed to
18+
the optimizer.
1319
- Added `STCH` from [Smooth Tchebycheff Scalarization for Multi-Objective
1420
Optimization](https://openreview.net/pdf?id=m4dO5L6eCp), a `Scalarizer` that combines the input
1521
tensor of values into a smooth approximation of their (weighted, shifted) maximum.
@@ -19,6 +25,12 @@ changelog does not include internal changes that do not affect the user.
1925
Learning](https://openaccess.thecvf.com/content_CVPRW_2019/papers/WAD/Chennupati_MultiNet_Multi-Stream_Feature_Aggregation_and_Geometric_Loss_Strategy_for_Multi-Task_CVPRW_2019_paper.pdf),
2026
a `Scalarizer` that returns the geometric mean of the input tensor of values.
2127

28+
### Changed
29+
30+
- **BREAKING**: Moved the `Stateful` mixin from `torchjd.aggregation` to the top-level `torchjd`
31+
namespace, so it can be shared between the aggregation and scalarization packages. Import it as
32+
`torchjd.Stateful` instead of `torchjd.aggregation.Stateful`.
33+
2234
## [0.12.0] - 2026-05-28
2335

2436
### Added

‎docs/source/docs/aggregation/index.rst‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ Abstract base classes
1919
.. autoclass:: torchjd.aggregation.Weighting
2020
:members: __call__
2121

22-
.. autoclass:: torchjd.aggregation.Stateful
23-
:members: reset
24-
25-
2622
.. toctree::
2723
:hidden:
2824
:maxdepth: 1

‎docs/source/docs/scalarization/index.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Abstract base class
1010
.. autoclass:: torchjd.scalarization.Scalarizer
1111
:members: __call__
1212

13-
1413
.. toctree::
1514
:hidden:
1615
:maxdepth: 1
@@ -21,3 +20,4 @@ Abstract base class
2120
random.rst
2221
stch.rst
2322
sum.rst
23+
uw.rst
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:hide-toc:
2+
3+
UW
4+
==
5+
6+
.. autoclass:: torchjd.scalarization.UW
7+
:members: __call__, reset

‎docs/source/docs/stateful.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:orphan:
2+
3+
.. autoclass:: torchjd.Stateful
4+
:members: reset

‎docs/source/examples/grouping.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ the parameters:
1919

2020
In TorchJD, grouping is achieved by calling :func:`~torchjd.autojac.jac_to_grad` once per group
2121
after :func:`~torchjd.autojac.backward` or :func:`~torchjd.autojac.mtl_backward`, with a dedicated
22-
aggregator instance per group. For :class:`~torchjd.aggregation.Stateful` aggregators, each instance
23-
should independently maintain its own state (e.g. the EMA :math:`\hat{\phi}` state in
22+
aggregator instance per group. For :class:`~torchjd.Stateful` aggregators, each instance should
23+
independently maintain its own state (e.g. the EMA :math:`\hat{\phi}` state in
2424
:class:`~torchjd.aggregation.GradVac`, matching the per-block targets from the original paper).
2525

2626
.. note::

‎src/torchjd/__init__.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
from collections.abc import Callable
22
from warnings import warn as _warn
33

4+
from ._mixins import Stateful
45
from .autojac import backward as _backward, mtl_backward as _mtl_backward
56

67
_deprecated_items: dict[str, tuple[str, Callable]] = {
78
"backward": ("autojac", _backward),
89
"mtl_backward": ("autojac", _mtl_backward),
910
}
1011

12+
__all__ = ["Stateful"]
13+
1114

1215
def __getattr__(name: str) -> Callable:
1316
"""

‎src/torchjd/_mixins.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
from abc import ABC, abstractmethod
12
from importlib.util import find_spec
23
from typing import Any
34

45

6+
class Stateful(ABC):
7+
"""Mixin adding a reset method."""
8+
9+
@abstractmethod
10+
def reset(self) -> None:
11+
"""Resets the internal state."""
12+
13+
514
class _WithOptionalDeps:
615
"""
716
Mixin that raises :class:`ImportError` at instantiation time if required optional dependencies

‎src/torchjd/aggregation/__init__.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
from ._krum import Krum, KrumWeighting
5353
from ._mean import Mean, MeanWeighting
5454
from ._mgda import MGDA, MGDAWeighting
55-
from ._mixins import Stateful
5655
from ._modo import MoDoWeighting
5756
from ._nash_mtl import NashMTL
5857
from ._pcgrad import PCGrad, PCGradWeighting
@@ -94,7 +93,6 @@
9493
"PCGradWeighting",
9594
"Random",
9695
"RandomWeighting",
97-
"Stateful",
9896
"Sum",
9997
"SumWeighting",
10098
"TrimmedMean",

‎src/torchjd/aggregation/_cr_mogm.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from torch import Tensor
66

7-
from torchjd.aggregation._mixins import Stateful
7+
from torchjd._mixins import Stateful
88

99
from ._weighting_bases import Weighting
1010

@@ -13,7 +13,7 @@
1313

1414
class CRMOGMWeighting(Weighting[_T], Stateful):
1515
r"""
16-
:class:`~torchjd.aggregation._mixins.Stateful`
16+
:class:`~torchjd.Stateful`
1717
:class:`~torchjd.aggregation._weighting_bases.Weighting` that wraps another
1818
:class:`~torchjd.aggregation._weighting_bases.Weighting` and stabilises the weights it
1919
produces with an exponential moving average (EMA) across calls. This is the weight-smoothing
@@ -61,7 +61,7 @@ class CRMOGMWeighting(Weighting[_T], Stateful):
6161
6262
This weighting is stateful: it keeps :math:`\lambda_{k-1}` across calls. Use :meth:`reset`
6363
to restart the smoothing from the initial state. Note that calling :meth:`reset` will also
64-
reset the wrapped weighting if it is :class:`~torchjd.aggregation.Stateful`.
64+
reset the wrapped weighting if it is :class:`~torchjd.Stateful`.
6565
6666
:param weighting: The wrapped weighting whose output is smoothed.
6767
:param alpha: EMA coefficient on the previous weights. ``alpha=0`` disables smoothing
@@ -120,7 +120,7 @@ def alpha(self, value: float) -> None:
120120
def reset(self) -> None:
121121
r"""
122122
Clears the EMA state so the next forward restarts from the initial state. Also resets the
123-
wrapped weighting if it is :class:`~torchjd.aggregation._mixins.Stateful`.
123+
wrapped weighting if it is :class:`~torchjd.Stateful`.
124124
"""
125125

126126
if isinstance(self.weighting, Stateful):

0 commit comments

Comments
 (0)