-
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?
Conversation
Thanks for the work! I am still a bit confused about the utility of the APIs. Possible to explain it in simpler terms? Cc: @SunMarc as well. |
Related PR on Transformers: huggingface/transformers#41138 Currently i have to acces the state dict to load the newly created params in quantization. And this can break if the state dict is sharded. And Diffusers will still throw unexpected keys warning regardles of if they were actually used or not as seen in the screenshot: ![]() This PR makes it so we can update the unexpected keys and expected keys so the params that will be added in quantization won't be skipped by Diffusers and Diffusers won't throw unnecessary unexpected keys warning. |
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.
Looks okay to me! Thanks!
@SunMarc could you review as well?
|
||
for param_name, param in state_dict.items(): | ||
if param_name not in empty_state_dict: | ||
if param_name in unexpected_keys: |
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.
Should this not be?
if param_name in unexpected_keys: | |
if param_name not in empty_state_dict or param_name in unexpected_keys: |
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.
Parameters that will be added in quantization isn't in the empty_state_dict yet. They will be added to the model in create_quantized_param within this loop.
Transformers uses param_name not in expected_keys
for this check. I used the unexpected keys here instead because diffusers doesn't pass the expected keys to this loop.
# hf_quantizer can add parameters that doesn't exist yet | ||
# they will be in the loaded_state_dict when 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.
I would also provide more details when this can arise.
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.
Pushed a new commit that fixes the failing pipeline tests when unexpected_keys is None. Also added more details to this comment lines.
Also, Transformers has From Transformers: |
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.
Thanks for adding this ! Eager to see the integration with SDNQ !
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 |
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 indeed this is kind of what we did in _infer_parameter_dtype
in transformers
# 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 comment
The 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
|
||
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: |
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
Acutally this is not that useful and we will probably remove it in transformers, |
What does this PR do?
Adds
update_expected_keys
andupdate_unexpected_keys
APIs to DiffusersQuantizer.Makes
load_model_dict_into_meta
compatible with updated unexpected / expected keys added in DiffusersQuantizer.Fixes #12470
Before submitting
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
Core library: