Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/torchjd/aggregation/_pref_vector_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
from .constant import _ConstantWeighting


def _check_pref_vector(pref_vector: Tensor | None) -> None:
"""Checks the correctness of the parameter pref_vector."""

if pref_vector is not None:
if pref_vector.ndim != 1:
raise ValueError(
"Parameter `pref_vector` must be a vector (1D Tensor). Found `pref_vector.ndim = "
f"{pref_vector.ndim}`."
)


def _pref_vector_to_weighting(pref_vector: Tensor | None, default: _Weighting) -> _Weighting:
"""
Returns the weighting associated to a given preference vector, with a fallback to a default
Expand All @@ -25,6 +14,11 @@ def _pref_vector_to_weighting(pref_vector: Tensor | None, default: _Weighting) -
if pref_vector is None:
return default
else:
if pref_vector.ndim != 1:
raise ValueError(
"Parameter `pref_vector` must be a vector (1D Tensor). Found `pref_vector.ndim = "
f"{pref_vector.ndim}`."
)
return _ConstantWeighting(pref_vector)


Expand Down
7 changes: 1 addition & 6 deletions src/torchjd/aggregation/aligned_mtl.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@
from torch import Tensor
from torch.linalg import LinAlgError

from ._pref_vector_utils import (
_check_pref_vector,
_pref_vector_to_str_suffix,
_pref_vector_to_weighting,
)
from ._pref_vector_utils import _pref_vector_to_str_suffix, _pref_vector_to_weighting
from .bases import _WeightedAggregator, _Weighting
from .mean import _MeanWeighting

Expand Down Expand Up @@ -66,7 +62,6 @@ class AlignedMTL(_WeightedAggregator):
"""

def __init__(self, pref_vector: Tensor | None = None):
_check_pref_vector(pref_vector)
weighting = _pref_vector_to_weighting(pref_vector, default=_MeanWeighting())
self._pref_vector = pref_vector

Expand Down
11 changes: 3 additions & 8 deletions src/torchjd/aggregation/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@
import torch
from torch import Tensor

from torchjd.aggregation._pref_vector_utils import (
_check_pref_vector,
_pref_vector_to_str_suffix,
_pref_vector_to_weighting,
)
from torchjd.aggregation.bases import Aggregator
from torchjd.aggregation.sum import _SumWeighting
from ._pref_vector_utils import _pref_vector_to_str_suffix, _pref_vector_to_weighting
from .bases import Aggregator
from .sum import _SumWeighting


class ConFIG(Aggregator):
Expand Down Expand Up @@ -66,7 +62,6 @@ class ConFIG(Aggregator):

def __init__(self, pref_vector: Tensor | None = None):
super().__init__()
_check_pref_vector(pref_vector)
self.weighting = _pref_vector_to_weighting(pref_vector, default=_SumWeighting())
self._pref_vector = pref_vector

Expand Down
7 changes: 1 addition & 6 deletions src/torchjd/aggregation/dualproj.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

from ._dual_cone_utils import _project_weights
from ._gramian_utils import _compute_regularized_normalized_gramian
from ._pref_vector_utils import (
_check_pref_vector,
_pref_vector_to_str_suffix,
_pref_vector_to_weighting,
)
from ._pref_vector_utils import _pref_vector_to_str_suffix, _pref_vector_to_weighting
from .bases import _WeightedAggregator, _Weighting
from .mean import _MeanWeighting

Expand Down Expand Up @@ -51,7 +47,6 @@ def __init__(
reg_eps: float = 0.0001,
solver: Literal["quadprog"] = "quadprog",
):
_check_pref_vector(pref_vector)
weighting = _pref_vector_to_weighting(pref_vector, default=_MeanWeighting())
self._pref_vector = pref_vector

Expand Down
7 changes: 1 addition & 6 deletions src/torchjd/aggregation/upgrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@

from ._dual_cone_utils import _project_weights
from ._gramian_utils import _compute_regularized_normalized_gramian
from ._pref_vector_utils import (
_check_pref_vector,
_pref_vector_to_str_suffix,
_pref_vector_to_weighting,
)
from ._pref_vector_utils import _pref_vector_to_str_suffix, _pref_vector_to_weighting
from .bases import _WeightedAggregator, _Weighting
from .mean import _MeanWeighting

Expand Down Expand Up @@ -51,7 +47,6 @@ def __init__(
reg_eps: float = 0.0001,
solver: Literal["quadprog"] = "quadprog",
):
_check_pref_vector(pref_vector)
weighting = _pref_vector_to_weighting(pref_vector, default=_MeanWeighting())
self._pref_vector = pref_vector

Expand Down
26 changes: 26 additions & 0 deletions tests/unit/aggregation/test_pref_vector_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from contextlib import nullcontext as does_not_raise

import torch
from pytest import mark, raises
from torch import Tensor
from unit._utils import ExceptionContext

from torchjd.aggregation._pref_vector_utils import _pref_vector_to_weighting
from torchjd.aggregation.mean import _MeanWeighting


@mark.parametrize(
["pref_vector", "expectation"],
[
(None, does_not_raise()),
(torch.ones([]), raises(ValueError)),
(torch.ones([0]), does_not_raise()),
(torch.ones([1]), does_not_raise()),
(torch.ones([5]), does_not_raise()),
(torch.ones([1, 1]), raises(ValueError)),
(torch.ones([1, 1, 1]), raises(ValueError)),
],
)
def test_pref_vector_to_weighting_check(pref_vector: Tensor | None, expectation: ExceptionContext):
with expectation:
_ = _pref_vector_to_weighting(pref_vector, default=_MeanWeighting())