fix(tests): dtype-aware reference cast in test_from_save_pretrained_dtype_inference#13882
Open
Anai-Guo wants to merge 1 commit into
Open
Conversation
The reference model in test_from_save_pretrained_dtype_inference was cast with a blanket model.to(dtype), which diverges from from_pretrained(torch_dtype=dtype): the latter keeps _keep_in_fp32_modules in fp32 and leaves non-persistent buffers (e.g. RoPE inv_freq, not stored in the checkpoint) at their __init__ dtype. The blanket cast produced spurious output mismatches for models with either. Mirror the loader dtype-aware casting on the reference instead.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
Fixes #13869.
test_from_save_pretrained_dtype_inferencebuilds its reference output with a blanketmodel.to(dtype), then compares it againstfrom_pretrained(tmp_path, torch_dtype=dtype). These two casts are not equivalent, so the comparison can diverge spuriously:_keep_in_fp32_modules:from_pretrainedkeeps these modules in fp32 (seeload_model_dict_into_metainmodel_loading_utils.py), butmodel.to(dtype)casts them todtype.inv_freqcreated with an explicit dtype): these are not saved to the checkpoint, sofrom_pretrainednever casts them and they keep the dtype assigned in__init__.model.to(dtype)casts them unconditionally.Either case makes the in-memory reference differ from the loaded model and the output assertion fails even though save/load is correct.
This PR replaces the blanket cast with one that mirrors the loader's dtype-aware semantics: parameters and persistent buffers are cast to
dtype(except_keep_in_fp32_modules, which stay fp32), while non-persistent buffers are left at their__init__dtype.Before submitting
Who can review?
@dg845 @DN6 (diagnosis from #13869 / #13862 review)
🤖 Generated with Claude Code