-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Add update expected / unexpected keys api to DiffusersQuantizer #12471
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -233,7 +233,7 @@ def load_model_dict_into_meta( | |
empty_state_dict = model.state_dict() | ||
|
||
for param_name, param in state_dict.items(): | ||
if param_name not in empty_state_dict: | ||
if unexpected_keys is not None and param_name in unexpected_keys: | ||
continue | ||
|
||
set_module_kwargs = {} | ||
|
@@ -260,10 +260,16 @@ def load_model_dict_into_meta( | |
# For compatibility with PyTorch load_state_dict which converts state dict dtype to existing dtype in model, and which | ||
# uses `param.copy_(input_param)` that preserves the contiguity of the parameter in the model. | ||
# Reference: https://github.com/pytorch/pytorch/blob/db79ceb110f6646523019a59bbd7b838f43d4a86/torch/nn/modules/module.py#L2040C29-L2040C29 | ||
old_param = model | ||
splits = param_name.split(".") | ||
for split in splits: | ||
old_param = getattr(old_param, split) | ||
if param_name in empty_state_dict: | ||
old_param = model | ||
splits = param_name.split(".") | ||
for split in splits: | ||
old_param = getattr(old_param, split) | ||
else: | ||
# hf_quantizer can add parameters that doesn't exist yet in the model and the empty_state_dict | ||
# they will be created in create_quantized_param and hf_quantizer should handle the loading of these parameters | ||
# these parameters will be in the loaded_state_dict from the model file instead when loading a pre_quantized model | ||
old_param = None | ||
Comment on lines
+263
to
+272
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. yeah indeed this is kind of what we did in |
||
|
||
if not isinstance(old_param, (torch.nn.Parameter, torch.Tensor)): | ||
old_param = None | ||
|
@@ -279,7 +285,7 @@ def load_model_dict_into_meta( | |
|
||
# bnb params are flattened. | ||
# gguf quants have a different shape based on the type of quantization applied | ||
if empty_state_dict[param_name].shape != param.shape: | ||
if param_name in empty_state_dict and empty_state_dict[param_name].shape != param.shape: | ||
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. just add a small comment for that as we will probably refactor the loading at some point to match what we have in transformers |
||
if ( | ||
is_quantized | ||
and hf_quantizer.pre_quantized | ||
|
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.
yeah that's better, actually in transformers we rely on unexpected keys