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

Update dtype_byte_size to handle torch.float8_e4m3fn/float8_e5m2 types #30488

Merged
merged 4 commits into from
Apr 26, 2024

Conversation

mgoin
Copy link
Contributor

@mgoin mgoin commented Apr 25, 2024

What does this PR do?

Currently using the new torch.float8_e4m3fn dtype will cause an error because dtype_byte_size() doesn't know where to find the number of bits in the dtype string.

Code to reproduce:

from transformers import AutoModelForCausalLM
import torch

model = AutoModelForCausalLM.from_pretrained("echarlaix/tiny-random-mistral")
model.lm_head.weight = torch.nn.Parameter(model.lm_head.weight.to(torch.float8_e4m3fn))
model.save_pretrained("test")

Error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/michael/venvs/test/lib/python3.10/site-packages/transformers/modeling_utils.py", line 2557, in save_pretrained
    shards, index = shard_checkpoint(state_dict, max_shard_size=max_shard_size, weights_name=weights_name)
  File "/home/michael/venvs/test/lib/python3.10/site-packages/transformers/modeling_utils.py", line 381, in shard_checkpoint
    weight_size = weight.numel() * dtype_byte_size(weight.dtype)
  File "/home/michael/venvs/test/lib/python3.10/site-packages/transformers/modeling_utils.py", line 328, in dtype_byte_size
    raise ValueError(f"`dtype` is not a valid dtype: {dtype}.")
ValueError: `dtype` is not a valid dtype: torch.float8_e4m3fn.

We can fix this by changing the regex from [^\d](\d+)$ to [^\d](\d+)_? so we match on every set of numbers following an alphabetical character. This can match multiple groups, but we can always look at the first group to get the first number which is significant. See the picture below for a demonstration of the matches on all numbered pytorch dtypes:
Screenshot 2024-04-25 at 12 27 07 PM

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.

Tagging @sgugger since they touched this function last.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@mgoin mgoin changed the title Update modeling_utils/dtype_byte_size to handle float8 types Update dtype_byte_size to handle torch.float8_e4m3fn/float8_e5m2 types Apr 25, 2024
@robertgshaw2-redhat
Copy link

Thanks! This is great for fp8

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this!

Could you add a test?

@mgoin
Copy link
Contributor Author

mgoin commented Apr 25, 2024

Sure @amyeroberts, I added a test specifically for dtype_byte_size over most of the torch dtypes. Let me know if you have something else in mind, thanks!

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Beautiful - thanks for updating and adding tests!

@amyeroberts
Copy link
Collaborator

For the quality checks, running make fixup should resolve

mgoin added 2 commits April 25, 2024 19:21
@amyeroberts amyeroberts merged commit 20081c7 into huggingface:main Apr 26, 2024
20 checks passed
@mgoin mgoin deleted the patch-1 branch April 26, 2024 15:38
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.

None yet

3 participants