Repository navigation
[ops] test: cover chunk CE with activation offload and checkpointing #1004
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
Coach257
merged 9 commits into
ByteDance-Seed:main
from
YLlllllllllll:fix/chunk-loss-activation-offload
Oct 29, 2026
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
208292f
[ops, dist, config, docs, ci] fix: make chunk loss compatible with ac…
YLlllllllllll 795577b
[ops, docs] fix: reconcile chunk loss offload with current main
YLlllllllllll e10bc1b
[ops, ci] fix: consolidate chunk-loss parity gates on current main
YLlllllllllll d3846dd
[test] fix: exercise native NPU chunk-loss offload transfers
YLlllllllllll 29f26c1
[ops] fix: evaluate chunked loss without gradient precomputation
YLlllllllllll 102943a
[ops] test: port chunk CE offload and checkpoint coverage to VeomniOp
YLlllllllllll 2637106
[test] fix: make chunk logits eligible for offload audit
YLlllllllllll a5a3703
[ops] test: isolate deterministic chunk-loss offload parity
YLlllllllllll ccdf019
[integration] fix: preserve upstream tests and PR behavior
YLlllllllllll 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
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
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,120 @@ | ||
| """CPU parity gates for chunk loss under saved tensor hooks and checkpointing.""" | ||
|
|
||
| from contextlib import nullcontext | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
| import torch | ||
| from torch.utils.checkpoint import checkpoint | ||
|
|
||
| import veomni.ops.kernels.cross_entropy.chunk_loss as chunk_module | ||
| from veomni.distributed.offloading import build_activation_offloading_context | ||
| from veomni.distributed.sequence_parallel.loss import ReduceLoss | ||
|
|
||
|
|
||
| def _legacy_forward(ctx, hidden, weight, bias, loss_forward, kwargs_chunks, chunk_size): | ||
| loss = torch.tensor(0.0, device=hidden.device) | ||
| dh, dw = torch.empty_like(hidden), torch.zeros_like(weight) | ||
| for x, target, kwargs in zip(hidden.split(chunk_size, 1), dh.split(chunk_size, 1), kwargs_chunks): | ||
| (dx, delta), (value, _) = torch.func.grad_and_value(loss_forward, (0, 1), has_aux=True)( | ||
| x, weight, bias, **kwargs | ||
| ) | ||
| loss.add_(value) | ||
| target.copy_(dx) | ||
| dw.add_(delta) | ||
| ctx.save_for_backward(dh, dw) | ||
| return loss | ||
|
|
||
|
|
||
| class LegacyChunkLoss(torch.autograd.Function): | ||
| forward = staticmethod(_legacy_forward) | ||
| backward = staticmethod(chunk_module.ChunkLoss.backward) | ||
|
|
||
|
|
||
| def _run(monkeypatch, implementation, dtype, sp, hooks, gc, frozen_head=False, all_ignored=False): | ||
| monkeypatch.setattr(chunk_module, "ChunkLoss", implementation) | ||
| monkeypatch.setattr(chunk_module, "get_parallel_state", lambda: SimpleNamespace(sp_enabled=sp)) | ||
| # Exercise the real reducer including its zero-token guard, with a one-rank | ||
| # collective seam. Multi-rank communication is outside this CPU unit gate. | ||
| monkeypatch.setattr(torch.distributed, "all_reduce", lambda *a, **k: None) | ||
| monkeypatch.setattr(torch.distributed, "get_world_size", lambda *a, **k: 1) | ||
| monkeypatch.setattr( | ||
| chunk_module, "reduce_sequence_parallel_loss", lambda loss, count: ReduceLoss.apply(loss, count, object()) | ||
| ) | ||
| torch.manual_seed(127) | ||
| h = torch.randn(2, 11, 7, dtype=dtype, requires_grad=True) | ||
| w = torch.randn(19, 7, dtype=dtype, requires_grad=not frozen_head) | ||
| labels = torch.randint(0, 19, (2, 11)) | ||
| labels[:, 1:7] = -100 # whole ignored chunks and a short final chunk | ||
| if all_ignored: | ||
| labels.fill_(-100) | ||
| packed = [] | ||
| unpacked = [] | ||
|
|
||
| def pack(t): | ||
| packed.append(t.shape) | ||
| return t.detach().clone() | ||
|
|
||
| def unpack(t): | ||
| unpacked.append(t.shape) | ||
| return t | ||
|
|
||
| def forward(x, head): | ||
| # Upstream graph ensures local autograd does not accidentally consume | ||
| # the decoder graph or its accumulated gradients. | ||
| return chunk_module.chunk_loss_function(x.sin(), head, labels, chunk_size=3, vocab_size=19)[0] | ||
|
|
||
| fwd_context = torch.autograd.graph.saved_tensors_hooks(pack, unpack) if hooks else nullcontext() | ||
| bwd_context = nullcontext() | ||
| if hooks == "production": | ||
| fwd_context, bwd_context = build_activation_offloading_context( | ||
| enable_activation=True, enable_gradient_checkpointing=gc | ||
| ) | ||
| with fwd_context: | ||
| loss = checkpoint(forward, h, w, use_reentrant=False) if gc else forward(h, w) | ||
| with bwd_context: | ||
| (loss * 0.37).backward() # non-unit upstream gradient (e.g. accumulation) | ||
| if hooks and hooks != "production": | ||
| assert packed and unpacked | ||
| return loss.detach(), h.grad, w.grad | ||
|
|
||
|
|
||
| def test_original_transform_rejects_saved_tensor_hooks(): | ||
| x = torch.ones(2) | ||
| with torch.autograd.graph.saved_tensors_hooks(lambda t: t, lambda t: t): | ||
| with pytest.raises(RuntimeError, match="don't yet support saved tensor hooks"): | ||
| torch.func.grad_and_value(lambda t: t.square().sum())(x) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16]) | ||
| @pytest.mark.parametrize("sp", [False, True]) | ||
| @pytest.mark.parametrize( | ||
| "hooks,gc", [(False, False), (True, False), (False, True), (True, True), ("production", True)] | ||
| ) | ||
| def test_loss_and_gradients_match_legacy(monkeypatch, dtype, sp, hooks, gc): | ||
| fixed = chunk_module.ChunkLoss | ||
| expected = _run(monkeypatch, LegacyChunkLoss, dtype, sp, False, False) | ||
| actual = _run(monkeypatch, fixed, dtype, sp, hooks, gc) | ||
| for got, ref in zip(actual, expected): | ||
| torch.testing.assert_close(got, ref, rtol=0, atol=0) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16]) | ||
| def test_frozen_head_and_upstream_graph(monkeypatch, dtype): | ||
| fixed = chunk_module.ChunkLoss | ||
| expected = _run(monkeypatch, LegacyChunkLoss, dtype, False, False, False, frozen_head=True) | ||
| actual = _run(monkeypatch, fixed, dtype, False, True, True, frozen_head=True) | ||
| assert actual[2] is None | ||
| for got, ref in zip(actual[:2], expected[:2]): | ||
| torch.testing.assert_close(got, ref, rtol=0, atol=0) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16]) | ||
| def test_zero_valid_sp_slice(monkeypatch, dtype): | ||
| fixed = chunk_module.ChunkLoss | ||
| expected = _run(monkeypatch, LegacyChunkLoss, dtype, True, False, False, all_ignored=True) | ||
| actual = _run(monkeypatch, fixed, dtype, True, True, True, all_ignored=True) | ||
| for got, ref in zip(actual, expected): | ||
| torch.testing.assert_close(got, ref, rtol=0, atol=0) | ||
| assert torch.isfinite(got).all() | ||
| assert torch.count_nonzero(got) == 0 | ||
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.