-
Notifications
You must be signed in to change notification settings - Fork 382
[megatron] Stream ChunkedDistributedLogprob.backward into a preallocated buffer (lower peak memory) #1806
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
Open
dyurk-lila
wants to merge
4
commits into
NovaSky-AI:main
Choose a base branch
from
dyurk-lila:perf/streaming-chunked-logprob-backward
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[megatron] Stream ChunkedDistributedLogprob.backward into a preallocated buffer (lower peak memory) #1806
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
07df484
[megatron] Stream ChunkedDistributedLogprob.backward into a prealloca…
dyurk-lila ec128cb
Update tests/backends/skyrl_train/distributed/test_chunked_logprob_ba…
dyurk-lila b5f8215
Trim chunked logprob docs
dyurk-lila 9ca754c
Trim chunked logprob tests
dyurk-lila 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
116 changes: 116 additions & 0 deletions
116
tests/backends/skyrl_train/distributed/test_chunked_logprob_backward_streaming.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,116 @@ | ||
| """Targeted CPU regression test for streamed ``ChunkedDistributedLogprob.backward``. | ||
|
|
||
| Run with: | ||
| uv run --isolated --extra skyrl-train --extra dev -- pytest -s \ | ||
| tests/backends/skyrl_train/distributed/test_chunked_logprob_backward_streaming.py | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
| from types import ModuleType | ||
|
|
||
| import pytest | ||
| import torch | ||
| import torch.distributed as dist | ||
|
|
||
| from skyrl.backends.skyrl_train.distributed.utils import get_free_port | ||
|
|
||
| # Stub megatron so CPU CI can import model_utils without megatron-core. | ||
| # The fixture restores prior modules, leaving GPU lanes with real megatron intact. | ||
|
|
||
| _MEGATRON_MODULES = [ | ||
| "megatron", | ||
| "megatron.core", | ||
| "megatron.core.parallel_state", | ||
| ] | ||
|
|
||
| _mock_modules: dict[str, ModuleType] = {} | ||
| for _name in _MEGATRON_MODULES: | ||
| _mock_modules[_name] = ModuleType(_name) | ||
|
|
||
| _mock_modules["megatron.core"].parallel_state = _mock_modules["megatron.core.parallel_state"] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module", autouse=True) | ||
| def _stub_megatron_modules(): | ||
| """Install the mock ``megatron`` modules for this module only.""" | ||
| saved = {_name: sys.modules.get(_name) for _name in _MEGATRON_MODULES} | ||
| for _name in _MEGATRON_MODULES: | ||
| sys.modules[_name] = _mock_modules[_name] | ||
| try: | ||
| yield | ||
| finally: | ||
| for _name in _MEGATRON_MODULES: | ||
| if saved[_name] is None: | ||
| sys.modules.pop(_name, None) | ||
| else: | ||
| sys.modules[_name] = saved[_name] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def tp_group(): | ||
| """Single-rank gloo TP group; only destroy it if this fixture created it.""" | ||
| initialized_here = False | ||
| if not dist.is_initialized(): | ||
| os.environ["MASTER_ADDR"] = "localhost" | ||
| os.environ["MASTER_PORT"] = str(get_free_port()) | ||
| os.environ["RANK"] = "0" | ||
| os.environ["WORLD_SIZE"] = "1" | ||
| dist.init_process_group(backend="gloo", rank=0, world_size=1) | ||
| initialized_here = True | ||
| yield dist.group.WORLD | ||
| if initialized_here and dist.is_initialized(): | ||
| dist.destroy_process_group() | ||
|
|
||
|
|
||
| def _backward_grad(func_cls, logits, target, vocab_start, vocab_end, tp_group, *, chunk_size=None): | ||
| """Return the input grad using a non-uniform upstream gradient.""" | ||
| leaf = logits.detach().clone().requires_grad_(True) | ||
| if chunk_size is None: | ||
| out = func_cls.apply(leaf, target, vocab_start, vocab_end, tp_group, False) | ||
| else: | ||
| out = func_cls.apply(leaf, target, vocab_start, vocab_end, chunk_size, tp_group, False) | ||
| grad_seed = torch.linspace(0.5, 1.5, steps=out.numel(), device=out.device, dtype=out.dtype).reshape(out.shape) | ||
| out.backward(grad_seed) | ||
| return leaf.grad.detach() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "case", | ||
| [ | ||
| pytest.param((3, 17, 64, 5, "mixed_oov"), id="ragged_mixed_oov"), | ||
| pytest.param((2, 8, 32, 64, "all_oov"), id="single_chunk_all_oov"), | ||
| ], | ||
| ) | ||
| def test_streamed_backward_matches_non_chunked_for_targeted_cases(tp_group, case): | ||
| """Chunked backward stays bit-identical while writing into the streamed buffer.""" | ||
| from skyrl.backends.skyrl_train.distributed.megatron.model_utils import ( | ||
| ChunkedDistributedLogprob, | ||
| DistributedLogprob, | ||
| ) | ||
|
|
||
| batch_size, seq_len, vocab_size, chunk_size, target_mode = case | ||
| device = torch.device("cpu") | ||
| torch.manual_seed(1) | ||
|
|
||
| logits = torch.randn(batch_size, seq_len, vocab_size, dtype=torch.float32, device=device) * 2.0 | ||
| if target_mode == "all_oov": | ||
| target = torch.full((batch_size, seq_len), vocab_size + 5, device=device, dtype=torch.long) | ||
| else: | ||
| target = torch.randint(0, vocab_size, (batch_size, seq_len), device=device, dtype=torch.long) | ||
| target[:, ::3] = vocab_size + 5 | ||
|
|
||
| grad_ref = _backward_grad(DistributedLogprob, logits, target, 0, vocab_size, tp_group) | ||
| grad_chunk = _backward_grad( | ||
| ChunkedDistributedLogprob, | ||
| logits, | ||
| target, | ||
| 0, | ||
| vocab_size, | ||
| tp_group, | ||
| chunk_size=chunk_size, | ||
| ) | ||
|
|
||
| assert grad_chunk.shape == grad_ref.shape == logits.shape | ||
| assert grad_chunk.dtype == torch.float32 | ||
| assert torch.equal(grad_chunk, grad_ref), "streamed chunked grad must be bit-identical to non-chunked grad" | ||
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
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.