Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 814
[PyTorch] Fixed bug with loading calibrated weights#771
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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 |
|---|---|---|
| @@ -65,6 +65,9 @@ def __init__(self, precision, use_bias): | ||
| self.inp_type = tex.DType.kFloat8E4M3 | ||
| self.weights_type = tex.DType.kFloat8E4M3 | ||
| self.outp_type = precision | ||
| def get_fp8_weights_scratchpad(self, is_first_microbatch): | ||
| raise RuntimeError("Method get_fp8_weights_scratchpad is dummy and should not be invoked.") | ||
| def forward(self, inp, weight): | ||
| inp_fp8 = cast_to_fp8( | ||
| @@ -145,14 +148,11 @@ def test_fp8_model_checkpoint( | ||
| params_dtype=dtype, | ||
| device=device, | ||
| ) | ||
| # Keep track of model output | ||
| x = torch.randn(dims, dtype=dtype, device=device) | ||
| with te.fp8_autocast(): | ||
| y_ref = model(x.detach().clone()).detach().clone() | ||
| # Keep track of weights and FP8 scaling factors | ||
| weight_ref = model.weight.float().detach().clone() | ||
| fp8_meta_ref = { "scaling_fwd": {}, "scaling_bwd": {} } | ||
| with te.fp8_autocast(), torch.no_grad(): | ||
| fp8_meta_fwd = model.fp8_meta["scaling_fwd"] | ||
| @@ -168,6 +168,18 @@ def test_fp8_model_checkpoint( | ||
| fp8_meta_bwd.scale.copy_(fp8_meta_bwd_ref["scale"]) | ||
| fp8_meta_bwd.scale_inv.copy_(fp8_meta_bwd_ref["scale_inv"]) | ||
| del fp8_meta_fwd, fp8_meta_bwd | ||
| # [ This is part of logic that tests save_fp8_model=False and load_fp8_model=True ] | ||
| # This line copies the fp8 scale_inv from the model metadata to the weight fp8 tensor. | ||
| # The sole purpose of the following lines is to set the scale_inv of the weight tensor, which is the simplest method. | ||
| # It is essential for these values to be equal, so setting scale_inv only in the model metadata is insufficient. | ||
| model.weight.data.copy_(model.weight.float().cuda()) | ||
| # After copying, the tensor computes the meta scale_inv based on the amax history; we then reset these values. | ||
| model.fp8_meta["scaling_fwd"].scale = fp8_meta_fwd_ref["scale"] | ||
| model.fp8_meta["scaling_fwd"].scale_inv = fp8_meta_fwd_ref["scale_inv"] | ||
| # Keep track of weights and FP8 scaling factors | ||
| weight_ref = model.weight.float().detach().clone() | ||
| # Save checkpoint | ||
| byte_stream = io.BytesIO() | ||
| @@ -214,6 +226,18 @@ def test_fp8_model_checkpoint( | ||
| with pytest.raises(AssertionError): | ||
| torch.testing.assert_close(y, y_ref, **tols) | ||
| # [ This is part of logic that tests save_fp8_model=False and load_fp8_model=True ] | ||
| # When save_fp8_model=True, we load a model with weights in high precision, | ||
| # which does not include _scale_inv, | ||
| # but has the fp8 scaling factor in the meta data. This scenario can occur | ||
| # when using te.fp8_autocast(enabled=False, calibrating=True). | ||
| # | ||
| # In such cases, the default behavior of load_state_dict is incorrect - it loads tensors first, | ||
| # followed by the fp8 metadata. This results in an incorrect _scale_inv for the tensor. This behavior | ||
| # is corrected by overriding the _load_state_dict method from PyTorch in TransformerEngineBaseModule, | ||
| # to load the fp8 metadata before loading tensors. | ||
| # | ||
| # Load checkpoint | ||
| model.load_state_dict(torch.load(io.BytesIO(model_bytes))) | ||
| del model_bytes | ||
| @@ -232,3 +256,10 @@ def test_fp8_model_checkpoint( | ||
| with te.fp8_autocast(): | ||
| y = model(x.detach().clone()) | ||
| torch.testing.assert_close(y, y_ref, **tols) | ||
| if load_fp8_model: | ||
sudhakarsingh27 marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| # [ This is part of logic that tests save_fp8_model=False and load_fp8_model=True ] | ||
| # We need to ensure that the tensor's scale_inv parameter matches its meta data. | ||
| # This is crucial to avoid confusion about which value is correct. | ||
| meta_index = model.weight._fp8_meta_index | ||
| torch.testing.assert_close(model.weight._scale_inv.item(), fp8_meta_fwd_ref["scale_inv"][meta_index].item()) | ||
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
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.