Skip to content

Commit 25e5689

Browse files
authored
fix(aggregation): Register tensor attributes as buffers (#839)
1 parent 46e7ded commit 25e5689

17 files changed

Lines changed: 119 additions & 9 deletions

‎CHANGELOG.md‎

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

99
## [Unreleased]
1010

11+
### Fixed
12+
13+
- Fixed `.to()` not moving the tensors given to `Constant`, `ConstantWeighting`, `UPGrad`,
14+
`UPGradWeighting`, `DualProj`, `DualProjWeighting`, `AlignedMTL`, `AlignedMTLWeighting`, `ConFIG`,
15+
`SDMGradWeighting`, `CRMOGMWeighting` and `GradDrop` (`weights`, `pref_vector`,
16+
`initial_weights` and `leak`). These tensors are now registered as non-persistent buffers, so
17+
they follow the dtype and device of the aggregator or weighting without being added to its
18+
`state_dict`.
19+
1120
## [0.18.1] - 2026-11-02
1221

1322
### Added

‎src/torchjd/aggregation/_aligned_mtl.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __init__(
3636
scale_mode: SUPPORTED_SCALE_MODE = "min",
3737
) -> None:
3838
super().__init__()
39+
self.register_buffer("_pref_vector", None, persistent=False)
3940
self.pref_vector = pref_vector
4041
self.scale_mode: SUPPORTED_SCALE_MODE = scale_mode
4142

‎src/torchjd/aggregation/_config.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ConFIG(Aggregator, _NonDifferentiable):
3131

3232
def __init__(self, pref_vector: Tensor | None = None) -> None:
3333
super().__init__()
34+
self.register_buffer("_pref_vector", None, persistent=False)
3435
self.pref_vector = pref_vector
3536

3637
def forward(self, matrix: Matrix, /) -> Tensor:

‎src/torchjd/aggregation/_constant.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class ConstantWeighting(_MatrixWeighting):
1414
:param weights: The weights to return at each call.
1515
"""
1616

17+
weights: Tensor
18+
1719
def __init__(self, weights: Tensor) -> None:
1820
if weights.dim() != 1:
1921
raise ValueError(
@@ -22,7 +24,7 @@ def __init__(self, weights: Tensor) -> None:
2224
)
2325

2426
super().__init__()
25-
self.weights = weights
27+
self.register_buffer("weights", weights, persistent=False)
2628

2729
def forward(self, matrix: Tensor, /) -> Tensor:
2830
self._check_matrix_shape(matrix)
@@ -48,11 +50,10 @@ class Constant(WeightedAggregator):
4850

4951
def __init__(self, weights: Tensor) -> None:
5052
super().__init__(weighting=ConstantWeighting(weights=weights))
51-
self._weights = weights
5253

5354
def __repr__(self) -> str:
54-
return f"{self.__class__.__name__}(weights={repr(self._weights)})"
55+
return f"{self.__class__.__name__}(weights={repr(self.weighting.weights)})"
5556

5657
def __str__(self) -> str:
57-
weights_str = vector_to_str(self._weights)
58+
weights_str = vector_to_str(self.weighting.weights)
5859
return f"{self.__class__.__name__}([{weights_str}])"

‎src/torchjd/aggregation/_cr_mogm.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,15 @@ class CRMOGMWeighting(Weighting[_T], Stateful):
9898
cr_mogm.alpha = 1 - current_lr / initial_lr
9999
"""
100100

101+
_initial_weights: Tensor | None
102+
101103
def __init__(
102104
self, weighting: Weighting[_T], alpha: float = 0.9, initial_weights: Tensor | None = None
103105
) -> None:
104106
super().__init__()
105107
self.weighting = weighting
106108
self.alpha = alpha
107-
self._initial_weights = initial_weights
109+
self.register_buffer("_initial_weights", initial_weights, persistent=False)
108110
self.register_buffer("_lambda", None)
109111

110112
@property

‎src/torchjd/aggregation/_dualproj.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def __init__(
2828
projector: DualConeProjector | None = None,
2929
) -> None:
3030
super().__init__()
31+
self.register_buffer("_pref_vector", None, persistent=False)
3132
self.pref_vector = pref_vector
3233
self.projector = projector
3334

‎src/torchjd/aggregation/_graddrop.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class GradDrop(Aggregator, _NonDifferentiable):
3131
def __init__(self, f: Callable = _identity, leak: Tensor | None = None) -> None:
3232
super().__init__()
3333
self.f = f
34+
self.register_buffer("_leak", None, persistent=False)
3435
self.leak = leak
3536

3637
def forward(self, matrix: Matrix, /) -> Tensor:

‎src/torchjd/aggregation/_sdmgrad.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def __init__(
107107
self.momentum = momentum
108108
self.n_iter = n_iter
109109
self.lambda_ = lambda_
110+
self.register_buffer("_pref_vector", None, persistent=False)
110111
self.pref_vector = pref_vector
111112
self.register_buffer("_w", None)
112113
self._state_key: tuple[int, torch.dtype, torch.device] | None = None

‎src/torchjd/aggregation/_upgrad.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def __init__(
2929
projector: DualConeProjector | None = None,
3030
) -> None:
3131
super().__init__()
32+
self.register_buffer("_pref_vector", None, persistent=False)
3233
self.pref_vector = pref_vector
3334
self.projector = projector
3435

‎tests/unit/aggregation/test_aligned_mtl.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,17 @@ def test_float64_small_eigenvalue_is_kept() -> None:
6666
J = tensor_([[1.0, 0.0], [0.0, 1e-4]], dtype=torch.float64)
6767
result = AlignedMTL()(J)
6868
assert_close(result, tensor_([5e-5, 5e-5], dtype=torch.float64))
69+
70+
71+
def test_to_moves_pref_vector() -> None:
72+
A = AlignedMTL(pref_vector=tensor_([1.0, 2.0], dtype=torch.float32)).to(torch.float64)
73+
weighting = A.gramian_weighting.weighting
74+
assert isinstance(weighting, ConstantWeighting)
75+
76+
assert A.pref_vector is not None
77+
assert A.pref_vector.dtype == torch.float64
78+
assert weighting.weights.dtype == torch.float64
79+
buffers = list(A.buffers())
80+
assert any(buffer is A.pref_vector for buffer in buffers)
81+
assert any(buffer is weighting.weights for buffer in buffers)
82+
assert len(A.state_dict()) == 0

0 commit comments

Comments
 (0)