Repository navigation
Fix replicated parameter dtypes during tensor-parallel loading #15147
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
sayakpaul
merged 3 commits into
huggingface:main
from
yzhautouskay:yzhautouskay/tp-loading-preserve-dtype
Nov 4, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,14 +18,17 @@ | |
| import tempfile | ||
| import unittest.mock as mock | ||
| import uuid | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
| import requests_mock | ||
| import torch | ||
| from huggingface_hub import ModelCard, delete_repo, snapshot_download, try_to_load_from_cache | ||
| from huggingface_hub.utils import HfHubHTTPError, is_jinja_available | ||
| from safetensors.torch import save_file | ||
|
|
||
| from diffusers.models import FluxTransformer2DModel, SD3Transformer2DModel, UNet2DConditionModel | ||
| from diffusers.models.model_loading_utils import _load_shard_file_tp | ||
|
|
||
| from ..others.test_utils import TOKEN, USER, is_staging_test | ||
| from ..testing_utils import ( | ||
|
|
@@ -36,6 +39,34 @@ | |
|
|
||
|
|
||
| class TestModelUtils: | ||
| @pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16]) | ||
| @pytest.mark.parametrize("keep_in_fp32_modules", [None, ["keep"]]) | ||
| def test_tensor_parallel_loading_preserves_dtype(self, tmp_path, dtype, keep_in_fp32_modules): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could the regression include a replicated integer buffer alongside the floating parameters? The loader intentionally casts only floating tensors, and this explicit dtype forwarding should preserve integer checkpoint buffers even when the requested model dtype is FP16/BF16. |
||
| with torch.device("meta"): | ||
| model = torch.nn.ModuleDict({name: torch.nn.Linear(2, 2, dtype=dtype) for name in ("keep", "other")}) | ||
| state_dict = { | ||
| name: torch.arange(param.numel()).reshape(param.shape) / 10 for name, param in model.state_dict().items() | ||
| } | ||
| checkpoint = tmp_path / "model.safetensors" | ||
| save_file(state_dict, checkpoint) | ||
|
|
||
| tp_config = SimpleNamespace(_device=torch.device("cpu"), _mesh=SimpleNamespace(device_type="cpu")) | ||
| _load_shard_file_tp( | ||
| checkpoint, | ||
| model, | ||
| model.state_dict(), | ||
| tp_shard_specs={}, | ||
| tp_config=tp_config, | ||
| dtype=dtype, | ||
| keep_in_fp32_modules=keep_in_fp32_modules, | ||
| unexpected_keys=[], | ||
| ) | ||
|
|
||
| for name, param in model.named_parameters(): | ||
| expected_dtype = torch.float32 if keep_in_fp32_modules and name.startswith("keep.") else dtype | ||
| assert param.dtype == expected_dtype | ||
| torch.testing.assert_close(param, state_dict[name].to(expected_dtype), rtol=0, atol=0) | ||
|
|
||
| def test_missing_key_loading_warning_message(self): | ||
| logger = logging.getLogger("diffusers.models.modeling_utils") | ||
| with CaptureLogger(logger) as cap_logger: | ||
|
|
||
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.
Can this go to
tests/models/testing_utils/parallelism.pyin a separate class please?