-
Notifications
You must be signed in to change notification settings - Fork 581
fp32 master weights for the DFlash draft, and keep them across a resume #2322
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
Open
h-guo18
wants to merge
2
commits into
main
Choose a base branch
from
haoguo/dflash-fp32-master-weights
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+91
−0
Open
Changes from all commits
Commits
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
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.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: NVIDIA/Model-Optimizer
Length of output: 42788
🤖 get_repo_knowledge executed:
get_repo_knowledge NVIDIA/Model-Optimizer /tmp/coderabbit-repo-knowledge/nvidia-model-optimizer-9541ba76/architecture /tmp/coderabbit-repo-knowledge/nvidia-model-optimizer-9541ba76/conventions /tmp/coderabbit-repo-knowledge/nvidia-model-optimizer-9541ba76/learningsLength of output: 47564
🏁 Script executed:
Repository: NVIDIA/Model-Optimizer
Length of output: 33795
🏁 Script executed:
Repository: NVIDIA/Model-Optimizer
Length of output: 34584
🏁 Script executed:
Repository: NVIDIA/Model-Optimizer
Length of output: 36558
🌐 Web query:
Hugging Face Transformers from_pretrained dtype="auto" checkpoint weights parameter dtype casting documentation💡 Result:
In the Hugging Face Transformers library, the
from_pretrainedmethod allows you to control the data type (dtype) of model weights during instantiation [1]. By default, weights are often loaded in full precision (torch.float32), but settingdtype="auto"(ortorch_dtype="auto") instructs the library to automatically determine and apply the appropriate dtype [1][2][3]. Whendtype="auto"is used, the library follows this priority logic [1]: 1. Config File Check: Transformers first attempts to use thedtypeortorch_dtypevalue explicitly defined in the model'sconfig.jsonfile [4][1]. 2. Checkpoint Inference: If no dtype information is present in the configuration, the library inspects the checkpoint itself. It identifies the first floating-point weight found in the checkpoint and adopts that weight's dtype for the entire model [4][1]. This functionality is designed to be memory-efficient, as it allows weights to be initialized in their native storage format, potentially avoiding the overhead of loading them intofloat32and then casting [2]. Important Considerations: - Compatibility: It is recommended to usedtype="auto"to load models in their most optimal format, but this assumes the model's configuration is correctly set by the author [3]. If a model'sconfig.jsonlacks this information and the checkpoint was saved in a different precision than its original training dtype,"auto"will reflect the save-time dtype rather than the training-time dtype [1]. - Nested Models: In some architectures involving nested configurations (e.g., vision-language models), there have been reported limitations where"auto"may only apply to the root configuration rather than individual sub-components, though this is subject to library updates [5]. - Legacy Arguments: Whiletorch_dtypeis commonly used in tutorials,dtypeis also supported as an alias in many contexts [1][6]. If you need to force a specific precision regardless of the config or checkpoint, you can pass a concrete type liketorch.float16ortorch.bfloat16directly to thedtypeargument instead of using"auto"[1].Citations:
🌐 Web query:
site:github.com/huggingface/transformers v4.57.0 modeling_utils.py from_pretrained dtype auto load_state_dict parameter dtype cast💡 Result:
In Hugging Face Transformers version 4.57.0, the
dtype="auto"(ortorch_dtype="auto") functionality is implemented withinmodeling_utils.pyto allow the model to automatically infer the appropriate data type for weight loading [1][2][3]. Whendtype="auto"is provided to thefrom_pretrainedmethod, the following logic is executed: 1. Config Check: The system first checks if adtypeis explicitly defined in the model'sconfigobject [1]. If it is, that value is used [1]. 2. Sharded Metadata/State Dict: If not found in the config, the system inspects available sharded metadata or the state dictionary [1]. If a state dictionary is present, it attempts to derive the data type from the weights (specifically looking for floating-point types) [1][3]. 3. Fallback: If no information can be inferred from the configuration or weights, the system defaults totorch.float32[1][3]. Thedtypecast occurs as part of the model instantiation process, which is managed by_set_default_dtype[1]. This method creates a temporary context usingtorch.set_default_dtype()to ensure that when the model is initialized, weights are created or cast into the target data type before loading the state dictionary [1][3][4]. Key points regarding the behavior in v4.57.0: - Theautokeyword is intended to minimize memory usage by matching the loading dtype to the format in which the model weights were saved [5][3]. - In composite models (models with nested sub-configs),automay primarily use the root config's settings, which has been identified as a limitation in certain architectures where individual backbones might require different dtypes [6]. - Thedtypeparameter is intended to be a flexible entry point acceptingtorch.dtype, the string"auto", or a dictionary for models with composite configurations [1][3]. Becauseload_state_dictis typically called during or after the architecture is initialized, thedtypecontext manager handles the memory allocation strategy to ensure the model instantiation respects the desired precision [1][4].Citations:
🌐 Web query:
site:github.com/huggingface/transformers "dtype == \"auto\"" "from_pretrained" "param_dtype"💡 Result:
In the Hugging Face Transformers library, setting
dtype="auto"(or the deprecatedtorch_dtype="auto") within thefrom_pretrainedmethod instructs the library to automatically determine the floating-point precision for loading the model weights [1][2]. Behavior ofdtype="auto": 1. Config Lookup: The library first checks theconfig.jsonfile associated with the model for adtypeortorch_dtypeentry [1][2]. If found, it uses that value [1]. 2. Fallback Mechanism: If the configuration does not specify adtype, the library attempts to derive it from the model's checkpoint weights [1][2]. It typically examines the first floating-point weight found in the state dictionary and assumes all other floating-point weights share that same precision [1]. 3. Default Value: If nodtypeis provided (i.e.,None), Transformers defaults to using the PyTorch default dtype, which is typicallytorch.float32[1]. Important Context and Recent Changes: - Naming Convention: While the library historically used the parametertorch_dtype, it has been transitioning to the simplerdtypeto align with PyTorch conventions [3]. The library provides backward compatibility fortorch_dtype[3]. - Defaulting to "auto": In recent versions (starting late 2024), the library has moved toward defaulting to"auto"in many contexts, reducing the reliance ontorch.float32as a hardcoded default, which helps ensure that models are loaded in the precision intended by the original author [4][5]. - Limitations: Users should be aware that"auto"relies on metadata that may not always be present or accurate [6]. For critical applications, explicitly specifying atorch.dtype(e.g.,torch.float16,torch.bfloat16) is often recommended to ensure consistency across different hardware environments [2][6]. Additionally, issues may arise with complex or nested model architectures, where the top-level configuration might not reflect the specific dtype requirements of sub-components [7][8].Citations:
Preserve fp32 draft parameters across HF resume.
When
dtype="auto"resolves tobfloat16, Transformers can load the restoreddflash_moduleinbfloat16. The latermodel.dflash_module.float()call changes the dtype but cannot recover values lost during downcasting. Load the draft parameters in fp32 before checkpoint loading, while keeping base parameters in the target dtype.test_flag_survives_save_restoreonly callsmtsp.convert()and checks an attribute. It does not save or reload a checkpoint. Extend it to perform the HF round trip and assert the draft dtype and an fp32 value that is not exactly representable inbfloat16.📍 Affects 2 files
examples/speculative_decoding/main.py#L235-L237(this comment)tests/unit/torch/speculative/plugins/test_hf_dflash.py#L613-L624🤖 Prompt for AI Agents
Sources: Coding guidelines, Path instructions