Skip to content

🚨🚨🚨 [Quantization] Store the original dtype in the config as a private attribute 🚨🚨🚨#26761

Merged
younesbelkada merged 8 commits into
huggingface:mainfrom
younesbelkada:add-orig-dtype
Oct 16, 2023
Merged

🚨🚨🚨 [Quantization] Store the original dtype in the config as a private attribute 🚨🚨🚨#26761
younesbelkada merged 8 commits into
huggingface:mainfrom
younesbelkada:add-orig-dtype

Conversation

@younesbelkada

@younesbelkada younesbelkada commented Oct 12, 2023

Copy link
Copy Markdown
Contributor

What does this PR do?

First step of an alternative design of #26560

For quantized models, instead of introducing a complex logic of retrieving the original weights dtype, I propose to simply add a private attribute _quantization_original_dtype in the config object.

to method does not need to be touched here as to cannot be called on quantized models (but for GPTQ models you can call to to perform device placement only - not for dtype casting)

that way we could adapt #26560 to simply check if the config has the attribute _quantization_original_dtype which is the case only for quantized models, else retrieve the dtype by retrieving the dtype of the linear layer weights in a classic manner.

cc @LysandreJik

Comment thread src/transformers/modeling_utils.py Outdated
@HuggingFaceDocBuilderDev

HuggingFaceDocBuilderDev commented Oct 12, 2023

Copy link
Copy Markdown

The documentation is not available anymore as the PR was closed or merged.

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make sure we prevent people form casting an already quantised model WDYT? Should not be a recommended / desirable use case

Comment thread src/transformers/modeling_utils.py Outdated
Comment thread src/transformers/modeling_utils.py Outdated
# one the weights have been quantized
# Note that once you have loaded a quantized model, you can't change its dtype so this will
# remain a single source of truth
config._quantization_original_dtype = torch_dtype

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
config._quantization_original_dtype = torch_dtype
config._pre_quantization_dtype = torch_dtype

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, LGTM let's make sure we don't break the workflow for other as it's breaking (not being able to cast to a dtype after init) and add a 🚨 !

Comment thread src/transformers/modeling_utils.py Outdated
Comment thread src/transformers/modeling_utils.py Outdated
Comment on lines +857 to +858
# pop the `_pre_quantization_dtype` as torch.dtypes are not serializable.
_ = serializable_config_dict.pop("_pre_quantization_dtype", None)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we pop it because it should not be saved no?

Comment thread src/transformers/modeling_utils.py Outdated
Comment on lines +3189 to +3190
# Note that once you have loaded a quantized model, you can't change its dtype so this will
# remain a single source of truth

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be needed in the quantizer config?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is ok since users can always load back quantized models with new torch_dtype making that _pre_quantization_dtype obsolete

@younesbelkada younesbelkada changed the title [Quantization] Store the original dtype in the config as a private attribute 🚨🚨🚨 [Quantization] Store the original dtype in the config as a private attribute 🚨🚨🚨 Oct 13, 2023
@younesbelkada
younesbelkada merged commit fd6a0ad into huggingface:main Oct 16, 2023
@younesbelkada
younesbelkada deleted the add-orig-dtype branch October 16, 2023 17:56
@SunMarc SunMarc mentioned this pull request Oct 27, 2023
EduardoPach pushed a commit to EduardoPach/transformers that referenced this pull request Nov 19, 2023
…ate attribute 🚨🚨🚨 (huggingface#26761)

* First step

* fix

* add adjustements for gptq

* change to `_pre_quantization_dtype`

* Update src/transformers/modeling_utils.py

Co-authored-by: Arthur <[email protected]>

* fix serialization

* Apply suggestions from code review

Co-authored-by: Arthur <[email protected]>

* fixup

---------

Co-authored-by: Arthur <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants