|
| 1 | +From cef4c4308729099f4d2fc11bfd5f5132cc6c0225 Mon Sep 17 00:00:00 2001 |
| 2 | +From: AllSpark <allspark@microsoft.com> |
| 3 | +Date: Wed, 28 Jan 2026 18:21:32 +0000 |
| 4 | +Subject: [PATCH] override SWALR.state_dict and load_state_dict; add |
| 5 | + _set_anneal_func and use in __init__ |
| 6 | + |
| 7 | +Signed-off-by: Azure Linux Security Servicing Account <azurelinux-security@microsoft.com> |
| 8 | +Upstream-reference: AI Backport of https://github.com/pytorch/pytorch/commit/167ad09be5af5c52666759412a3804068c6955d1.patch |
| 9 | +--- |
| 10 | + torch/optim/swa_utils.py | 39 +++++++++++++++++++++++++++++++++++---- |
| 11 | + 1 file changed, 35 insertions(+), 4 deletions(-) |
| 12 | + |
| 13 | +diff --git a/torch/optim/swa_utils.py b/torch/optim/swa_utils.py |
| 14 | +index 90b3f159..e792f727 100644 |
| 15 | +--- a/torch/optim/swa_utils.py |
| 16 | ++++ b/torch/optim/swa_utils.py |
| 17 | +@@ -7,6 +7,8 @@ import torch |
| 18 | + from torch.nn import Module |
| 19 | + from torch.optim.lr_scheduler import LRScheduler |
| 20 | + from torch.utils._foreach_utils import _get_foreach_kernels_supported_devices |
| 21 | ++from typing_extensions import override |
| 22 | ++ |
| 23 | + |
| 24 | + __all__ = [ |
| 25 | + 'AveragedModel', |
| 26 | +@@ -326,10 +328,7 @@ class SWALR(LRScheduler): |
| 27 | + if anneal_strategy not in ['cos', 'linear']: |
| 28 | + raise ValueError("anneal_strategy must by one of 'cos' or 'linear', " |
| 29 | + f"instead got {anneal_strategy}") |
| 30 | +- elif anneal_strategy == 'cos': |
| 31 | +- self.anneal_func = self._cosine_anneal |
| 32 | +- elif anneal_strategy == 'linear': |
| 33 | +- self.anneal_func = self._linear_anneal |
| 34 | ++ self._set_anneal_func(anneal_strategy) |
| 35 | + if not isinstance(anneal_epochs, int) or anneal_epochs < 0: |
| 36 | + raise ValueError(f"anneal_epochs must be equal or greater than 0, got {anneal_epochs}") |
| 37 | + self.anneal_epochs = anneal_epochs |
| 38 | +@@ -375,3 +374,35 @@ class SWALR(LRScheduler): |
| 39 | + alpha = self.anneal_func(t) |
| 40 | + return [group['swa_lr'] * alpha + lr * (1 - alpha) |
| 41 | + for group, lr in zip(self.optimizer.param_groups, prev_lrs)] |
| 42 | ++ |
| 43 | ++ |
| 44 | ++ def _set_anneal_func(self, anneal_strategy): |
| 45 | ++ self._anneal_strategy = anneal_strategy |
| 46 | ++ if anneal_strategy == 'cos': |
| 47 | ++ self.anneal_func = self._cosine_anneal |
| 48 | ++ else: |
| 49 | ++ self.anneal_func = self._linear_anneal |
| 50 | ++ |
| 51 | ++ @override |
| 52 | ++ def state_dict(self): |
| 53 | ++ """Return the state of the scheduler as a :class:`dict`. |
| 54 | ++ |
| 55 | ++ It contains an entry for every variable in self.__dict__ which |
| 56 | ++ is not the optimizer or anneal_func. |
| 57 | ++ """ |
| 58 | ++ return { |
| 59 | ++ key: value |
| 60 | ++ for key, value in self.__dict__.items() |
| 61 | ++ if key not in ("optimizer", "anneal_func") |
| 62 | ++ } |
| 63 | ++ |
| 64 | ++ @override |
| 65 | ++ def load_state_dict(self, state_dict): |
| 66 | ++ """Load the scheduler's state. |
| 67 | ++ |
| 68 | ++ Args: |
| 69 | ++ state_dict (dict): scheduler state. Should be an object returned |
| 70 | ++ from a call to :meth:`state_dict`. |
| 71 | ++ """ |
| 72 | ++ self.__dict__.update(state_dict) |
| 73 | ++ self._set_anneal_func(self._anneal_strategy) |
| 74 | +-- |
| 75 | +2.45.4 |
| 76 | + |
0 commit comments