Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed cyclic lr state dict #1468

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/super_gradients/training/sg_trainer/sg_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
read_ckpt_state_dict,
load_checkpoint_to_model,
load_pretrained_weights,
get_scheduler_state,
)
from super_gradients.training.datasets.datasets_utils import DatasetStatisticsTensorboardLogger
from super_gradients.training.utils.callbacks import (
Expand Down Expand Up @@ -616,7 +617,7 @@ def _save_checkpoint(
state["processing_params"] = processing_params

if self._torch_lr_scheduler is not None:
state["torch_scheduler_state_dict"] = self._torch_lr_scheduler.state_dict()
state["torch_scheduler_state_dict"] = get_scheduler_state(self._torch_lr_scheduler)

# SAVES CURRENT MODEL AS ckpt_latest
self.sg_logger.add_checkpoint(tag="ckpt_latest.pth", state_dict=state, global_step=epoch)
Expand Down
16 changes: 15 additions & 1 deletion src/super_gradients/training/utils/checkpoint_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import collections
import os
import tempfile
from typing import Union, Mapping
from typing import Union, Mapping, Dict

import pkg_resources
import torch
from torch import nn, Tensor
from torch.optim.lr_scheduler import CyclicLR

from super_gradients.common.abstractions.abstract_logger import get_logger
from super_gradients.common.data_interface.adnn_model_repository_data_interface import ADNNModelRepositoryDataInterfaces
Expand Down Expand Up @@ -1597,3 +1598,16 @@ def load_pretrained_weights_local(model: torch.nn.Module, architecture: str, pre

pretrained_state_dict = torch.load(pretrained_weights, map_location=map_location)
_load_weights(architecture, model, pretrained_state_dict)


def get_scheduler_state(scheduler) -> Dict:
"""
Wrapper for getting a torch lr scheduler state dict, resolving some issues with CyclicLR
(see https://github.com/pytorch/pytorch/pull/91400)
:param scheduler: torch.optim.lr_scheduler._LRScheduler, the scheduler
:return: the scheduler's state_dict
"""
state = scheduler.state_dict()
if isinstance(scheduler, CyclicLR) and int(torch.version.__version__.split(".")[0]) < 2:
shaydeci marked this conversation as resolved.
Show resolved Hide resolved
del state["_scale_fn_ref"]
return state