Skip to content
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

Enable different torch dtype in sub models #34873

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

zucchini-nlp
Copy link
Member

@zucchini-nlp zucchini-nlp commented Nov 22, 2024

What does this PR do?

Fixes #33997. Enables users to use different torch dtypes for each of sub config. For ex load the vision model in full precision and the text model in half precision

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Comment on lines 4051 to 4058
for sub_config_key in config.sub_configs.keys():
sub_config = getattr(config, sub_config_key)
sub_config.torch_dtype = torch_dtype
elif isinstance(torch_dtype, dict):
for key, curr_dtype in torch_dtype.items():
if hasattr(config, key):
value = getattr(config, key)
value.torch_dtype = curr_dtype
Copy link
Member Author

Choose a reason for hiding this comment

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

if users passes one torch dtype as before, we just use it in all sub-configs. Otherwise a user can either set directly dtypes in configs before loading the model, or indicate a dict torch_dtype when loading similarly to attn_implementation_dispatch

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Thanks, can you add a test showcasing an example usage of this (for example with Llava!)

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

This is interesting but I am not sure we have everything ready:

  • does it work with the keep-in-float32 attribute as well?
  • does it work well with model that have enforced param with dtypes?
    (Some vision models have this! ) So maybe a little bit of testing is missing
    Makes a lot of sense otherwise!

Comment on lines +4024 to +4026
if hasattr(config, key):
value = getattr(config, key)
value.torch_dtype = curr_dtype
Copy link
Collaborator

Choose a reason for hiding this comment

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

else we should probably raise an error no?

@zucchini-nlp
Copy link
Member Author

Yeah, this one needs time and I'll come back after the model releases to make sure it works in all cases. Currently it has weird behavior in nested configs where a general text config has an attribute vision_config

@zucchini-nlp
Copy link
Member Author

Conditions to make the dtype dispatch correctly: use _from_config to init the submodule inside a bigger model as ChameleonVQVAE._from_config. That way the default dtype is set before initilizing the weights on each submodule, otherwise they are not set

The current design support setting dtype via dict when loading or directly via changing the config. The dict should have the same keys as sub-configs and can have an empty string key which will be the main dtype. Main dtype is used for any module that is not part of sub-config, for ex mm_projector

Added more tests and verified it works when keep_in_fp32_modules is indicated. For the case when model's forcefully limit dtype of certain classes, I haven't found an actual model that does it so not sure how to test that

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.

from_pretrained's torch_dtype "auto" mode doesn't handle nested models
3 participants