-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(callbacks): Defer step/time-triggered ModelCheckpoint saves until validation metrics are available #21106
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
Merged
Borda
merged 8 commits into
Lightning-AI:master
from
littlebullGit:fix/20919-checkpoint-step-val-metric
Aug 29, 2025
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d094b1c
fix(callbacks): defer step/time-triggered ModelCheckpoint saves until…
littlebullGit b88b546
test: disable logger in model checkpoint tests to avoid side effects
littlebullGit 59dda02
refactor: defer DeepSpeed import and logging configuration until needed
littlebullGit 6c1554a
test: add mock-based CPU tests for DeepSpeed strategy import paths
littlebullGit ef816b6
Revert "refactor: defer DeepSpeed import and logging configuration un…
littlebullGit 836de5a
Revert "test: add mock-based CPU tests for DeepSpeed strategy import …
littlebullGit ced28da
Merge branch 'master' into fix/20919-checkpoint-step-val-metric
Borda a2a5964
chlog
Borda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
229 changes: 229 additions & 0 deletions
229
tests/tests_fabric/strategies/test_deepspeed_imports_mock.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
# Copyright The Lightning AI team. | ||
# This test file provides CPU-only coverage for DeepSpeed lazy-import paths by mocking a minimal | ||
# `deepspeed` module. It does not require GPUs or the real DeepSpeed package. | ||
|
||
import sys | ||
from types import ModuleType | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
|
||
from lightning.fabric.strategies import DeepSpeedStrategy | ||
|
||
|
||
class _FakeLogger: | ||
def __init__(self): | ||
self.levels = [] | ||
|
||
def setLevel(self, lvl): | ||
self.levels.append(lvl) | ||
|
||
|
||
class _FakeZeroInit: | ||
def __init__(self, *args, **kwargs): | ||
# record for assertions | ||
self.args = args | ||
self.kwargs = kwargs | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc, tb): | ||
return False | ||
|
||
|
||
@pytest.fixture | ||
def fake_deepspeed(monkeypatch): | ||
"""Inject a minimal fake `deepspeed` package into sys.modules.""" | ||
ds = ModuleType("deepspeed") | ||
# Mark as a package with a spec and path so importlib won't complain | ||
import importlib.machinery | ||
|
||
ds.__spec__ = importlib.machinery.ModuleSpec("deepspeed", loader=Mock(), is_package=True) | ||
ds.__path__ = [] # type: ignore[attr-defined] | ||
|
||
# utils.logging.logger | ||
utils_mod = ModuleType("deepspeed.utils") | ||
logging_mod = ModuleType("deepspeed.utils.logging") | ||
utils_mod.__spec__ = importlib.machinery.ModuleSpec("deepspeed.utils", loader=Mock(), is_package=True) | ||
logging_mod.__spec__ = importlib.machinery.ModuleSpec("deepspeed.utils.logging", loader=Mock(), is_package=False) | ||
logger = _FakeLogger() | ||
logging_mod.logger = logger | ||
utils_mod.logging = logging_mod | ||
ds.utils = utils_mod | ||
|
||
# zero.Init | ||
zero_mod = ModuleType("deepspeed.zero") | ||
zero_mod.__spec__ = importlib.machinery.ModuleSpec("deepspeed.zero", loader=Mock(), is_package=False) | ||
zero_mod.Init = _FakeZeroInit | ||
ds.zero = zero_mod | ||
|
||
# checkpointing.configure | ||
checkpointing_mod = ModuleType("deepspeed.checkpointing") | ||
checkpointing_mod.__spec__ = importlib.machinery.ModuleSpec( | ||
"deepspeed.checkpointing", loader=Mock(), is_package=False | ||
) | ||
recorded = {"configure_calls": []} | ||
|
||
def _configure(**kwargs): | ||
recorded["configure_calls"].append(kwargs) | ||
|
||
checkpointing_mod.configure = _configure | ||
ds.checkpointing = checkpointing_mod | ||
|
||
# initialize | ||
recorded["initialize_calls"] = [] | ||
|
||
def _initialize(**kwargs): | ||
recorded["initialize_calls"].append(kwargs) | ||
# return values: (engine, optimizer, _, scheduler) | ||
return Mock(name="engine"), Mock(name="optimizer"), None, Mock(name="scheduler") | ||
|
||
ds.initialize = _initialize | ||
|
||
# init_distributed | ||
recorded["init_distributed_calls"] = [] | ||
|
||
def _init_distributed(*args, **kwargs): | ||
recorded["init_distributed_calls"].append((args, kwargs)) | ||
|
||
ds.init_distributed = _init_distributed | ||
|
||
# install into sys.modules | ||
monkeypatch.setitem(sys.modules, "deepspeed", ds) | ||
monkeypatch.setitem(sys.modules, "deepspeed.utils", utils_mod) | ||
monkeypatch.setitem(sys.modules, "deepspeed.utils.logging", logging_mod) | ||
monkeypatch.setitem(sys.modules, "deepspeed.zero", zero_mod) | ||
monkeypatch.setitem(sys.modules, "deepspeed.checkpointing", checkpointing_mod) | ||
|
||
# Pretend deepspeed is installed by forcing availability flag to True | ||
monkeypatch.setattr("lightning.fabric.strategies.deepspeed._DEEPSPEED_AVAILABLE", True, raising=False) | ||
|
||
return ds, logger, recorded | ||
|
||
|
||
def _make_strategy_with_defaults(): | ||
# Use defaults; we'll tweak attributes per test as needed | ||
return DeepSpeedStrategy() | ||
|
||
|
||
def _get_backend() -> str: | ||
# simple helper used to override strategy._get_process_group_backend | ||
return "gloo" | ||
|
||
|
||
def test_module_sharded_context_sets_logger_and_returns_zero_init(fake_deepspeed): | ||
ds_mod, logger, recorded = fake_deepspeed | ||
|
||
strategy = _make_strategy_with_defaults() | ||
# The context asserts that the config was initialized | ||
strategy._config_initialized = True # type: ignore[attr-defined] | ||
|
||
ctx = strategy.module_sharded_context() | ||
assert isinstance(ctx, _FakeZeroInit) | ||
# logger.setLevel should be called at least once | ||
assert len(logger.levels) >= 1 | ||
|
||
|
||
def test_initialize_engine_import_and_logger_and_call(fake_deepspeed): | ||
ds_mod, logger, recorded = fake_deepspeed | ||
|
||
strategy = _make_strategy_with_defaults() | ||
# root_device.index is read; use a CUDA device number even on CPU-only hosts (no allocation happens) | ||
import torch | ||
|
||
strategy.parallel_devices = [torch.device("cuda", 0)] # type: ignore[attr-defined] | ||
|
||
class _Param: | ||
requires_grad = True | ||
|
||
model = Mock() | ||
model.parameters.return_value = [_Param()] | ||
|
||
engine, optimizer, scheduler = strategy._initialize_engine(model) | ||
|
||
# assertions | ||
assert len(logger.levels) >= 1 | ||
assert recorded["initialize_calls"], "deepspeed.initialize was not called" | ||
call = recorded["initialize_calls"][0] | ||
assert call["config"] == strategy.config | ||
assert call["model"] is model | ||
assert call["dist_init_required"] is False | ||
# returned mocks are propagated | ||
from unittest.mock import Mock as _M | ||
|
||
assert isinstance(engine, _M) | ||
assert engine._mock_name == "engine" | ||
assert isinstance(optimizer, _M) | ||
assert optimizer._mock_name == "optimizer" | ||
assert isinstance(scheduler, _M) | ||
assert scheduler._mock_name == "scheduler" | ||
|
||
|
||
def test_init_deepspeed_distributed_calls_import_and_init(fake_deepspeed, monkeypatch): | ||
ds_mod, logger, recorded = fake_deepspeed | ||
|
||
strategy = _make_strategy_with_defaults() | ||
|
||
# minimal cluster env | ||
class _CE: | ||
main_port = 12345 | ||
main_address = "127.0.0.1" | ||
|
||
def global_rank(self): | ||
return 0 | ||
|
||
def local_rank(self): | ||
return 0 | ||
|
||
def node_rank(self): | ||
return 0 | ||
|
||
def world_size(self): | ||
return 1 | ||
|
||
def teardown(self): | ||
pass | ||
|
||
strategy.cluster_environment = _CE() | ||
strategy._process_group_backend = "gloo" # avoid CUDA requirement | ||
strategy._timeout = 300 # type: ignore[attr-defined] | ||
|
||
strategy._get_process_group_backend = _get_backend # type: ignore[assignment] | ||
|
||
# ensure non-Windows path | ||
monkeypatch.setattr("platform.system", lambda: "Linux") | ||
|
||
strategy._init_deepspeed_distributed() | ||
|
||
assert len(logger.levels) >= 1 | ||
assert recorded["init_distributed_calls"], "deepspeed.init_distributed was not called" | ||
args, kwargs = recorded["init_distributed_calls"][0] | ||
assert args[0] == "gloo" | ||
assert kwargs["distributed_port"] == 12345 | ||
assert "timeout" in kwargs | ||
|
||
|
||
def test_set_deepspeed_activation_checkpointing_configured(fake_deepspeed): | ||
ds_mod, logger, recorded = fake_deepspeed | ||
|
||
strategy = _make_strategy_with_defaults() | ||
# ensure config contains activation_checkpointing keys | ||
assert isinstance(strategy.config, dict) | ||
strategy.config.setdefault("activation_checkpointing", {}) | ||
strategy.config["activation_checkpointing"].update({ | ||
"partition_activations": True, | ||
"contiguous_memory_optimization": False, | ||
"cpu_checkpointing": True, | ||
"profile": False, | ||
}) | ||
|
||
strategy._set_deepspeed_activation_checkpointing() | ||
|
||
assert len(logger.levels) >= 1 | ||
assert recorded["configure_calls"], "deepspeed.checkpointing.configure was not called" | ||
cfg = recorded["configure_calls"][0] | ||
assert cfg["partition_activations"] is True | ||
assert cfg["contiguous_checkpointing"] is False | ||
assert cfg["checkpoint_in_cpu"] is True | ||
assert cfg["profile"] is False |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.