fix: Add anemoi weight averaging classes in order to work with Imputers and Scalers#1113
Draft
jakob-schloer wants to merge 1 commit into
Draft
fix: Add anemoi weight averaging classes in order to work with Imputers and Scalers#1113jakob-schloer wants to merge 1 commit into
jakob-schloer wants to merge 1 commit into
Conversation
Contributor
|
@jakob-schloer I am marking this as draft, as from what I understand, it might be better to check if there are ways we can adapt the dynamic scalers and imputers so that we don't have to hack the EMA callback so much. And @ssmmnn11 is looking at preparing a branch with some suggestions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
For weight averaging, a copy of the model is created at the beginning of training and its parameters are updated/averaged over the course of training. The pytorch lightning classes for weight averaging pair parameters and buffers positionally via
zip, which fails in Anemoi when used together with:Imputers (
ConstantImputerand friends) register scratch buffers (nan_locations,loss_mask_training) that get reassigned with new shapes on the first forward pass. The averaged model's deep-copy still holds the originalshape=(0,)tensor, so the per-batchupdate_parametersof the PLAverageModelcall crashes with a shape mismatch.Updating loss scalers (
NaNMaskScaler, etc.) callScaleTensor.update_scalerevery batch — this pops and re-registers the scaler buffer, shuffling the buffer order in the live model relative to the averaged snapshot. With matching shapes the positionalzipsilently mis-pairs tensors; with mismatched shapes it crashes.Changes
anemoi.training.diagnostics.callbacks.weight_averaging.AveragedModel— name-based matching, shape filtering, and a float/non-float split (non-float buffers are sync'd from source rather than averaged).WeightAveraging— overridessetup,_swap_models,_copy_average_to_currentso all transfer paths between live and averaged model use name-based matching.EMAWeightAveragingandSWAWeightAveraging— thin subclasses mirroringpytorch_lightning.callbacks.EMAWeightAveraging(and theanalogous SWA configuration).
_get_weight_averaging_callbackstill instantiates whatever the user targets, but logs a warning if a stockpytorch_lightning.callbacks.*class is configured, since those will crash when used in combination with imputers or updating scalers.Usage
Notes
Since we still allow stock
pytorch_lightning.callbacks.*classes, this is not a breaking changeAs a contributor to the Anemoi framework, please ensure that your changes include unit tests, updates to any affected dependencies and documentation, and have been tested in a parallel setting (i.e., with multiple GPUs). As a reviewer, you are also responsible for verifying these aspects and requesting changes if they are not adequately addressed. For guidelines about those please refer to https://anemoi.readthedocs.io/en/latest/
By opening this pull request, I affirm that all authors agree to the Contributor License Agreement.
📚 Documentation preview 📚: https://anemoi-training--1113.org.readthedocs.build/en/1113/
📚 Documentation preview 📚: https://anemoi-graphs--1113.org.readthedocs.build/en/1113/
📚 Documentation preview 📚: https://anemoi-models--1113.org.readthedocs.build/en/1113/