Repository navigation
[dist] fix: preserve persistent buffers during meta random initialization #967
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 21 commits into
ByteDance-Seed:main
from
YLlllllllllll:fix/fsdp2-cpu-offload-compute-buffers
Oct 28, 2026
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ff45cc7
[dist] fix: keep FSDP2 buffers on compute device with CPU offload
YLlllllllllll a134860
fix: keep FSDP2 offload buffers on compute device
YLlllllllllll 8d1738c
fix: resolve conflicts with upstream main
YLlllllllllll d12960e
fix: resolve FSDP2 merge conflict with upstream main
YLlllllllllll 1059c6f
chore: align FSDP2 file with upstream main before branch update
YLlllllllllll e8bc2d6
chore: align CI workflows with upstream main before branch update
YLlllllllllll 1526c68
Merge branch 'main' into fix/fsdp2-cpu-offload-compute-buffers
YLlllllllllll 5fc05b9
fix: reapply FSDP2 CPU-offload fix after syncing upstream main
YLlllllllllll 8eff52e
[dist] merge: keep FSDP2 buffer aliases while absorbing main material…
YLlllllllllll 601ec99
[ci] fix: run DCP checkpointer tests once per workflow
YLlllllllllll 0c299c3
[fsdp, tests] fix: reconcile offload buffer gates with model runtime
YLlllllllllll 9bc6f37
[dist] fix: sync buffer offload with selective recomputation
YLlllllllllll 5654f92
[dist] fix: preserve compute buffers across current-main CPU offload …
YLlllllllllll bad1835
Merge upstream main and preserve PR #967 coverage
YLlllllllllll 7bc7ccc
Merge upstream main and preserve CPU-offload buffer contracts
YLlllllllllll 804d743
[ci] test: schedule native CPU-offload buffer regression on NPU
YLlllllllllll 425774b
Merge fused load tests while preserving FSDP buffer offload coverage
YLlllllllllll 23a86a0
[checkpoint] fix: preserve shared buffers during HF loading
YLlllllllllll 75ec473
[checkpoint] fix: retain constructor buffers during random initializa…
YLlllllllllll b53ccf5
[checkpoint] test: cover random buffer materialization on accelerators
YLlllllllllll 8a5d06f
[dist] fix: preserve persistent buffers during random initialization
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # Copyright 2026 Bytedance Ltd. and/or its affiliates | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Config-derived buffers must survive random initialization from meta.""" | ||
|
|
||
| import pytest | ||
| import torch | ||
| from torch import nn | ||
|
|
||
| from scripts.ci.accelerator.launch_plan import LaunchSpec | ||
| from veomni.distributed.torch_parallelize import _materialize_and_load_weights | ||
| from veomni.models.checkpoint.weights import init_empty_weights | ||
| from veomni.utils.device import get_device_id, get_device_type, get_torch_device | ||
|
|
||
|
|
||
| class BufferModel(nn.Module): | ||
| def __init__(self, overwrite=False): | ||
| super().__init__() | ||
| self.weight = nn.Parameter(torch.empty(2)) | ||
| self.register_buffer("persistent_value", torch.tensor([2.0, 5.0])) | ||
| self.register_buffer("runtime_value", torch.tensor([3.0, 7.0]), persistent=False) | ||
| self.register_buffer("optional_value", None) | ||
| self.child = nn.Module() | ||
| self.child.register_buffer("persistent_value", torch.tensor([11.0, 13.0])) | ||
| self.overwrite = overwrite | ||
| self.init_calls = 0 | ||
|
|
||
| def init_weights(self): | ||
| self.init_calls += 1 | ||
| with torch.no_grad(): | ||
| self.weight.fill_(19) | ||
| if self.overwrite: | ||
| self.child.persistent_value.fill_(23) | ||
|
|
||
|
|
||
| def _check_materialization(device, *, overwrite, skip_hf): | ||
| with init_empty_weights(): | ||
| model = BufferModel(overwrite=overwrite) | ||
| _materialize_and_load_weights( | ||
| model, | ||
| None, | ||
| str(device), | ||
| should_skip_hf_weight_load=skip_hf, | ||
| is_peft_model=False, | ||
| adapter_path=None, | ||
| broadcast_from_rank0=False, | ||
| ) | ||
| assert model.weight.device.type == device.type | ||
| assert model.persistent_value.device.type == device.type | ||
| assert model.runtime_value.device.type == device.type | ||
| assert model.child.persistent_value.device.type == device.type | ||
| assert model.optional_value is None | ||
| torch.testing.assert_close(model.runtime_value.cpu(), torch.tensor([3.0, 7.0]), rtol=0, atol=0) | ||
| if skip_hf: | ||
| # A later DCP load is authoritative for persistent buffers and params. | ||
| assert model.init_calls == 0 | ||
| else: | ||
| assert model.init_calls == 1 | ||
| torch.testing.assert_close(model.weight.cpu(), torch.full((2,), 19.0), rtol=0, atol=0) | ||
| torch.testing.assert_close(model.persistent_value.cpu(), torch.tensor([2.0, 5.0]), rtol=0, atol=0) | ||
| child = [23.0, 23.0] if overwrite else [11.0, 13.0] | ||
| torch.testing.assert_close(model.child.persistent_value.cpu(), torch.tensor(child), rtol=0, atol=0) | ||
|
|
||
|
|
||
| @pytest.mark.launch_plan(provider=LaunchSpec(device="cpu")) | ||
| @pytest.mark.parametrize("overwrite", [False, True]) | ||
| @pytest.mark.parametrize("skip_hf", [False, True]) | ||
| def test_random_init_and_resume_buffer_materialization_cpu(overwrite, skip_hf): | ||
| _check_materialization(torch.device("cpu"), overwrite=overwrite, skip_hf=skip_hf) | ||
|
|
||
|
|
||
| @pytest.mark.launch_plan(provider=LaunchSpec(device="accelerator")) | ||
| def test_random_init_and_resume_buffer_materialization_accelerator(): | ||
| device_type = get_device_type() | ||
| if device_type == "cpu": | ||
| pytest.skip("requires an accelerator runtime") | ||
| assert get_torch_device().is_available(), "selected accelerator runtime is unavailable" | ||
| device = torch.device(device_type, get_device_id()) | ||
| for overwrite in (False, True): | ||
| for skip_hf in (False, True): | ||
| _check_materialization(device, overwrite=overwrite, skip_hf=skip_hf) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please simplify
_to_empty_preserving_nonpersistent_buffersto only addpreserve_persistent_buffers: bool = Falseon top ofmain's existing implementation (i.e. iteratingmodule._buffers.keys()whenpreserve_persistent_buffers=Trueandmodule._non_persistent_buffers_setwhenFalse), without thebuffer_groupsalias-tracking machinery.