Skip to content

feat(aggregation): Add getters and setters to all remaining aggregators#660

Merged
ValerianRey merged 15 commits intomainfrom
feat/aggregator-properties
Apr 21, 2026
Merged

feat(aggregation): Add getters and setters to all remaining aggregators#660
ValerianRey merged 15 commits intomainfrom
feat/aggregator-properties

Conversation

@mattbuot
Copy link
Copy Markdown
Contributor

@mattbuot mattbuot commented Apr 20, 2026

Summary

Replicates the getter/setter pattern established in #658 (UPGrad) across every other aggregator that has constructor parameters. For each, the Weighting class stores each parameter as _name with a public @property + @setter pair; setters validate their inputs; outer aggregator classes forward to the inner weighting when applicable; __init__ calls the setters so validation always runs; __repr__ / __str__ read through the properties.

Commits are split per aggregator for easier review:

  • DualProj / DualProjWeightingpref_vector, norm_eps, reg_eps (direct copy of UPGrad pattern)
  • AlignedMTL / AlignedMTLWeightingpref_vector, scale_mode
  • ConFIGpref_vector (single class, no Weighting)
  • CAGrad / CAGradWeightingc, norm_eps
  • Krum / KrumWeightingn_byzantine, n_selected
  • MGDA / MGDAWeightingepsilon, max_iters
  • NashMTL / _NashMTLWeightingn_tasks, max_norm, update_weights_every, optim_niter
  • TrimmedMeantrim_number
  • GradDropleak (f kept as a plain attribute, matching UPGrad's solver)
  • CHANGELOG entry

Validation choices

  • pref_vector setters rebuild the internal weighting via pref_vector_to_weighting (mirrors UPGrad).
  • Non-negative: norm_eps, reg_eps, c, n_byzantine, trim_number, max_norm. max_norm == 0 disables the clipping, matching the existing if self.max_norm > 0 check in NashMTL.forward.
  • Strictly positive: epsilon, max_iters, n_selected, n_tasks, update_weights_every, optim_niter.
  • leak setter preserves the existing 1-D-tensor validation.
  • scale_mode has no validation (relies on the SUPPORTED_SCALE_MODE type alias, as requested).

NashMTL note

NashMTL is stateful. Setters do not auto-reset internal state — users must call .reset() after changing parameters (especially n_tasks, which determines the shape of the cached state). This is documented in both docstrings.

Test plan

  • uv run pytest tests/unit/aggregation → 1804 passed
  • uv run ty check src/torchjd/aggregation → all checks passed
  • All 9 modified modules at 100% line coverage locally
  • CI passes

mattbuot added 10 commits April 20, 2026 21:17
Expose pref_vector, norm_eps, and reg_eps as properties on DualProj and
DualProjWeighting, mirroring the UPGrad pattern. The norm_eps and
reg_eps setters validate that the new value is non-negative.
Expose pref_vector and scale_mode as properties on AlignedMTL and
AlignedMTLWeighting, mirroring the UPGrad pattern.
Expose pref_vector as a property on ConFIG. The setter rebuilds the
internal weighting via pref_vector_to_weighting to keep state in sync.
Expose c and norm_eps as properties on CAGrad and CAGradWeighting. The
setters validate that the new value is non-negative.
Expose n_byzantine and n_selected as properties on Krum and
KrumWeighting. The setters enforce n_byzantine >= 0 and n_selected >= 1.
Expose epsilon and max_iters as properties on MGDA and MGDAWeighting.
The setters enforce that both parameters are strictly positive.
Expose n_tasks, max_norm, update_weights_every, and optim_niter as
properties on NashMTL and _NashMTLWeighting. Validation:
n_tasks/update_weights_every/optim_niter > 0, max_norm >= 0 (0 disables
norm clipping, matching existing forward logic).

Setters do not automatically reset the internal state; users must call
reset() if the state needs rebuilding (especially after changing
n_tasks). Documented in both class docstrings.
Expose trim_number as a property on TrimmedMean. The setter preserves
the existing non-negative validation.
Expose leak as a property on GradDrop. The setter preserves the
existing 1D-tensor validation. f is kept as a plain public attribute
(no validation needed), matching the pattern used for UPGrad's solver.
Covers AlignedMTL, CAGrad, ConFIG, DualProj, GradDrop, Krum, MGDA,
NashMTL, and TrimmedMean.
@ValerianRey ValerianRey added cc: feat Conventional commit type for new features. package: aggregation labels Apr 20, 2026
Copy link
Copy Markdown
Contributor

@ValerianRey ValerianRey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good, thank you very much! Just a few nitpicks in the tests that I'll fix myself before merging.

@PierreQuinton I think we could release v0.11.0 with this.

Comment thread tests/unit/aggregation/test_aligned_mtl.py
Comment thread tests/unit/aggregation/test_cagrad.py
Comment thread tests/unit/aggregation/test_cagrad.py
Comment thread tests/unit/aggregation/test_dualproj.py
Comment thread tests/unit/aggregation/test_dualproj.py
Comment thread tests/unit/aggregation/test_krum.py
Comment thread tests/unit/aggregation/test_krum.py
Comment thread tests/unit/aggregation/test_mgda.py
Comment thread tests/unit/aggregation/test_mgda.py
Comment thread tests/unit/aggregation/test_nash_mtl.py
ValerianRey and others added 2 commits April 21, 2026 13:48
Co-authored-by: Valérian Rey <31951177+ValerianRey@users.noreply.github.com>
@ValerianRey ValerianRey marked this pull request as ready for review April 21, 2026 11:52
@ValerianRey ValerianRey requested review from a team and PierreQuinton as code owners April 21, 2026 11:52
@ValerianRey ValerianRey enabled auto-merge (squash) April 21, 2026 12:00
@ValerianRey ValerianRey merged commit b6c40b3 into main Apr 21, 2026
16 checks passed
@ValerianRey ValerianRey deleted the feat/aggregator-properties branch April 21, 2026 12:01
@mattbuot
Copy link
Copy Markdown
Contributor Author

@ValerianRey I meant to review myself before setting ready to review but you were faster than me, thanks!

@ValerianRey
Copy link
Copy Markdown
Contributor

@ValerianRey I meant to review myself before setting ready to review but you were faster than me, thanks!

No worries! That was a really good PR ty!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cc: feat Conventional commit type for new features. package: aggregation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants