Skip to content

fix(aggregation): Register tensor attributes as buffers - #839

Open
SajalDevX wants to merge 2 commits into
SimplexLab:mainfrom
SajalDevX:fix/register-tensors-as-buffers
Open

SajalDevX wants to merge 2 commits into
SimplexLab:mainfrom
SajalDevX:fix/register-tensors-as-buffers

Conversation

@SajalDevX

Copy link
Copy Markdown
Contributor

Follow-up to #826, as suggested there by @ValerianRey.

Tensors given to aggregators and weightings were stored as plain attributes, so .to() did not move them. For example Constant(torch.tensor([1., 2.])).to(torch.float64) kept its weights in float32.

This registers every such tensor as a buffer instead:

  • ConstantWeighting.weights (and Constant now reads its weights from its weighting instead of keeping a second reference in _weights)
  • _pref_vector of UPGradWeighting, DualProjWeighting, AlignedMTLWeighting, ConFIG and SDMGradWeighting (the aggregators wrapping them get it for free)
  • _initial_weights of CRMOGMWeighting

Attribute access is unchanged (weights, pref_vector, the setters, repr/str). The stateful state tensors (_w, _lambda, _phi_t, ...) were already buffers, so I left them alone.

Open question: persistent=False. I went with non persistent buffers. These tensors are constructor arguments, like lr or alpha which are not in the state_dict either, and making them persistent would add new keys to the state_dict of any module holding an aggregator, so loading a checkpoint saved with 0.18 using strict=True would fail with missing keys. The downside is that a checkpoint does not restore them, which is a bit different from the stateful buffers (_w, _lambda, ...) that are persistent. Happy to switch to persistent=True if you prefer that.

Not included: the scalarizers in torchjd.scalarization (Constant, STCH, PBI, COSMOS) also keep weights / reference as plain attributes. I can do the same there in this PR or a separate one if you want.

Testing

Added a test_to_moves_* test for each class above. Each builds the object with float32 tensors, calls .to(torch.float64), and checks that the tensors are now float64, that they are among the module's buffers, and that they are not in the state_dict. The 7 new tests fail on main and pass with this change.

  • uv run pytest tests/unit -W error: 3703 passed, 66 skipped, 33 xfailed (CPU)
  • new tests also pass with PYTEST_TORCH_DTYPE=float64
  • ruff check, ruff format, pre-commit on changed files: clean
  • ty check: no new diagnostics (only the existing unresolved imports for the optional plot deps I did not install)
  • not run: CUDA tests (no GPU here)

@SajalDevX
SajalDevX requested review from a team and KhusPatel4450 as code owners November 2, 2026 03:41
@ValerianRey ValerianRey added package: aggregation cc: fix Conventional commit type for bug fixes of the actual library (changes to src). labels Nov 2, 2026

@ValerianRey ValerianRey left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi, thanks for the PR!

Please correct me if I'm wrong, but I think GradDrop's leak param is missing. Apart from that, LGTM.

Also, could you please open a PR with a similar change for the scalarizers?

This branch has not been deployed

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

Labels

cc: fix Conventional commit type for bug fixes of the actual library (changes to src). package: aggregation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants