Lightning-AI / Lightning-AI/pytorch-lightning

restore_training_state before on_fit_start?

Open
#20,338 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature optimization trainer: fit
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

Description & Motivation

I need to move some opimizer states to the device of the corresponding grad of the embeddings
I extended the optimizer to do it after super().load_state_dict but _optimizer_to_device(optimizer, self.root_device) moves them back from cpu to accelerator.
And there is no way to do it in on_fit_start, which was proposed by https://github.com/Lightning-AI/pytorch-lightning/issues/8035, for parameters but this doesn't work with optimizers variables because optimizer state loading happens after on_fit_start while parameters loading happens before on_fit_start.

see also https://github.com/Lightning-AI/pytorch-lightning/issues/3698

Pitch

Move

        # hook
        if self.state.fn == TrainerFn.FITTING:
            call._call_callback_hooks(self, "on_fit_start")
            call._call_lightning_module_hook(self, "on_fit_start")

After

        # restore optimizers, etc.
        log.debug(f"{self.__class__.__name__}: restoring training state")
        self._checkpoint_connector.restore_training_state()
Alternatives

Can't think of an alernative solution. If someone knows, let me know.

Additional context

No response

cc @lantiga @borda

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the training lifecycle that calls on_fit_start and at _checkpoint_connector.restore_training_state(), then compare their current ordering with the proposed change. Check how _optimizer_to_device(optimizer, self.root_device) participates in restoration. Done means optimizer state restoration occurs before the fit-start hooks without disrupting the existing training-state flow.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.