Skip to content

Commit 4a66f67

Browse files
authored
fix(scalarization): Register tensor attributes as buffers (#842)
1 parent 25e5689 commit 4a66f67

10 files changed

Lines changed: 70 additions & 7 deletions

File tree

‎CHANGELOG.md‎

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

1111
### Fixed
1212

13+
- Fixed `.to()` not moving the tensors given to the `Constant`, `STCH`, `PBI` and `COSMOS`
14+
scalarizers (`weights` and `reference`). These tensors are now registered as non-persistent
15+
buffers, so they follow the dtype and device of the scalarizer without being added to its
16+
`state_dict`. `FAMO`'s `min_losses` buffer is also no longer in its `state_dict`.
1317
- Fixed `.to()` not moving the tensors given to `Constant`, `ConstantWeighting`, `UPGrad`,
1418
`UPGradWeighting`, `DualProj`, `DualProjWeighting`, `AlignedMTL`, `AlignedMTLWeighting`, `ConFIG`,
1519
`SDMGradWeighting`, `CRMOGMWeighting` and `GradDrop` (`weights`, `pref_vector`,

‎src/torchjd/scalarization/_constant.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ class Constant(Scalarizer):
1414
passed at call time.
1515
"""
1616

17+
weights: Tensor
18+
1719
def __init__(self, weights: Tensor) -> None:
1820
super().__init__()
19-
self.weights = weights
21+
self.register_buffer("weights", weights, persistent=False)
2022

2123
def forward(self, values: Tensor, /) -> Tensor:
2224
if values.shape != self.weights.shape:

‎src/torchjd/scalarization/_cosmos.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class COSMOS(Scalarizer):
4242
objective.
4343
"""
4444

45+
weights: Tensor
46+
4547
def __init__(self, lambda_: float, weights: Tensor) -> None:
4648
if lambda_ < 0.0:
4749
raise ValueError(
@@ -50,7 +52,7 @@ def __init__(self, lambda_: float, weights: Tensor) -> None:
5052

5153
super().__init__()
5254
self.lambda_ = lambda_
53-
self.weights = weights
55+
self.register_buffer("weights", weights, persistent=False)
5456

5557
def forward(self, values: Tensor, /) -> Tensor:
5658
check_same_shape_as_values(self.weights, values, "weights")

‎src/torchjd/scalarization/_famo.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def __init__(
128128
f"Parameter `min_losses` should have shape {tuple(self._w.shape)} (matching the "
129129
f"shape of the logits). Found `min_losses.shape = {tuple(min_losses.shape)}`."
130130
)
131-
self.register_buffer("min_losses", min_losses)
131+
self.register_buffer("min_losses", min_losses, persistent=False)
132132

133133
self.lr = lr
134134
self.weight_decay = weight_decay

‎src/torchjd/scalarization/_pbi.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,17 @@ class PBI(Scalarizer):
5151
:math:`10^{-6}` at that point and is negligible elsewhere.
5252
"""
5353

54+
weights: Tensor
55+
reference: Tensor | None
56+
5457
def __init__(self, theta: float, weights: Tensor, reference: Tensor | None = None) -> None:
5558
if theta < 0.0:
5659
raise ValueError(f"Parameter `theta` should be non-negative. Found `theta = {theta}`.")
5760

5861
super().__init__()
5962
self.theta = theta
60-
self.weights = weights
61-
self.reference = reference
63+
self.register_buffer("weights", weights, persistent=False)
64+
self.register_buffer("reference", reference, persistent=False)
6265

6366
def forward(self, values: Tensor, /) -> Tensor:
6467
check_same_shape_as_values(self.weights, values, "weights")

‎src/torchjd/scalarization/_stch.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class STCH(Scalarizer):
3838
is applied. If provided, it must have the same shape as the values passed at call time.
3939
"""
4040

41+
weights: Tensor | None
42+
reference: Tensor | None
43+
4144
def __init__(
4245
self,
4346
mu: float,
@@ -49,8 +52,8 @@ def __init__(
4952

5053
super().__init__()
5154
self.mu = mu
52-
self.weights = weights
53-
self.reference = reference
55+
self.register_buffer("weights", weights, persistent=False)
56+
self.register_buffer("reference", reference, persistent=False)
5457

5558
def forward(self, values: Tensor, /) -> Tensor:
5659
check_same_shape_as_values(self.weights, values, "weights")

‎tests/unit/scalarization/test_constant.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,11 @@ def test_representations() -> None:
6161
def test_str_with_non_vector_weights() -> None:
6262
assert str(Constant(weights=ones_((3, 4)))) == "Constant(weights of shape (3, 4))"
6363
assert str(Constant(weights=ones_(()))) == "Constant(weights of shape ())"
64+
65+
66+
def test_to_moves_weights() -> None:
67+
s = Constant(weights=tensor_([1.0, 2.0], dtype=torch.float32)).to(torch.float64)
68+
69+
assert s.weights.dtype == torch.float64
70+
assert any(buffer is s.weights for buffer in s.buffers())
71+
assert len(s.state_dict()) == 0

‎tests/unit/scalarization/test_cosmos.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,11 @@ def test_representations() -> None:
8686
s = COSMOS(lambda_=0.5, weights=torch.tensor([0.5, 0.5]))
8787
assert repr(s) == "COSMOS(lambda_=0.5, weights=tensor([0.5000, 0.5000]))"
8888
assert str(s) == "COSMOS"
89+
90+
91+
def test_to_moves_weights() -> None:
92+
s = COSMOS(lambda_=0.5, weights=tensor_([0.5, 0.5], dtype=torch.float32)).to(torch.float64)
93+
94+
assert s.weights.dtype == torch.float64
95+
assert any(buffer is s.weights for buffer in s.buffers())
96+
assert len(s.state_dict()) == 0

‎tests/unit/scalarization/test_pbi.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,19 @@ def test_representations() -> None:
103103
s = PBI(theta=5.0, weights=torch.tensor([0.5, 0.5]))
104104
assert repr(s) == "PBI(theta=5.0, weights=tensor([0.5000, 0.5000]), reference=None)"
105105
assert str(s) == "PBI"
106+
107+
108+
def test_to_moves_weights_and_reference() -> None:
109+
s = PBI(
110+
theta=5.0,
111+
weights=tensor_([0.5, 0.5], dtype=torch.float32),
112+
reference=tensor_([0.0, 1.0], dtype=torch.float32),
113+
).to(torch.float64)
114+
115+
assert s.reference is not None
116+
assert s.weights.dtype == torch.float64
117+
assert s.reference.dtype == torch.float64
118+
buffers = list(s.buffers())
119+
assert any(buffer is s.weights for buffer in buffers)
120+
assert any(buffer is s.reference for buffer in buffers)
121+
assert len(s.state_dict()) == 0

‎tests/unit/scalarization/test_stch.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,20 @@ def test_representations() -> None:
8080
s = STCH(mu=0.5)
8181
assert repr(s) == "STCH(mu=0.5, weights=None, reference=None)"
8282
assert str(s) == "STCH"
83+
84+
85+
def test_to_moves_weights_and_reference() -> None:
86+
s = STCH(
87+
mu=1.0,
88+
weights=tensor_([0.5, 0.5], dtype=torch.float32),
89+
reference=tensor_([0.0, 1.0], dtype=torch.float32),
90+
).to(torch.float64)
91+
92+
assert s.weights is not None
93+
assert s.reference is not None
94+
assert s.weights.dtype == torch.float64
95+
assert s.reference.dtype == torch.float64
96+
buffers = list(s.buffers())
97+
assert any(buffer is s.weights for buffer in buffers)
98+
assert any(buffer is s.reference for buffer in buffers)
99+
assert len(s.state_dict()) == 0

0 commit comments

Comments
 (0)