-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
fix AutoModel.from_pretrained(..., torch_dtype=...)
#13209
Merged
patrickvonplaten
merged 5 commits into
huggingface:master
from
stas00:pretrained_torch_dtype
Aug 24, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2805d3b
fix AutoModel.from_pretrained(..., torch_dtype=...)
stas00 3f569bf
fix to_diff_dict
stas00 59698fe
add better test
stas00 ada7e50
Merge remote-tracking branch 'origin/master' into pretrained_torch_dtype
stas00 9e94cac
torch is not always available when a model has self.torch_dtype
stas00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
OK, so torch is not always available when
config.torch_dtype is not None
- so nowconfig.torch_dtype
isn't always oftorch.dtype
.I'm thinking perhaps this whole approach needs to be redone and only use the "float32", "float16", etc. strings everywhere, including the
torch_dtype
arg infrom_pretrained
andfrom_config
args. And only convert totorch.dtype
at the point it's used when the model is loaded.That way
torch_dtype
doesn't need to have a special handling at config level.I hope this is recent/experimental enough that it's ok that we break the API.
Actually, if we do that, why even bother with
torch_
intorch_dtype
and not just rename it todtype
- perhaps non-pt frameworks could tap into it as well? After all fp16-saved data by torch isn't any different from flux or tf, no?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 can't think of a scenario where one would want one dtype for one framework and another dtype for another - so changing it to
dtype
sounds good to me.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.
So the main concern is the back-compat in the API arg
torch_dtype
- if it's OK if we break it, then I propose both the config and the arg infrom_pretrained
andfrom_config
to be just adtype
as a string: "auto", "float32", "float16", etc.And then in the case of torch we convert it to the right
torch.dtype
on the fly. perhaps flux/tf could use this too down the road.Sylvain is not here for another week. Do you both support this breaking API change, @LysandreJik and @patrickvonplaten?
So instead of:
It will be:
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.
On the other hand we already have the
dtype
attribute in modeling_utils, which returnstorch.dtype
https://github.com/huggingface/transformers/blob/master/src/transformers/modeling_utils.py#L205
So my proposal might be confusing.
Should I call it
dtype_str
perhaps?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.
If no version with
torch_dtype
has been released yet, I'm fine with changing it todtype
. However, note that in Flax we already have adtype
variable that is used to define the dtype the matmul operations are run in instead of the dtype of the actual weights. In Flax we would like to take this design: #13098 as outlined by @patil-surajThere 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.
@stas00 regarding the PR: #13098 - the idea of the PR is exactly to disentangle parameter dtype from matmul/computation dtype. In Flax, it's common practice that the
dtype
parameter defines the matmul/computation dtype, see: https://flax.readthedocs.io/en/latest/_autosummary/flax.linen.Dense.html#flax.linen.Dense.dtype instead of the parameter dtype and not the parameter dtype.So for Flax, I don't really think it would make sense to use a
config.dtype
to define weights dtype as it would be quite confusing with Flax's computation dtype parameter.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.
Works for me. Although this API is somewhat bad at the moment due to inconsistent type of values in the config file and the function - the former a string, the latter a
torch.dtype
. Perhaps I can change these to support both string "float32" and torch.dtype in the same param of the function.Agreed!
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.
So it's somewhat similar to
dtype
arg in the new pytorchautocast
feature it seems then, correct? (before it was a hardcoded fp16, but now it has adtype
arg to support bf16 too.)p.s. it's currently called
fast_dtype
but will renamed shortly todtype
in pt-nightly.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.
Sounds good to me to keep
torch_dtype
and have a separate discussion for the framework-agnosticdtype
parameter, to whichtorch_dtype
could be an alias to prevent breaking changes to the existing API.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.
Sounds like a plan, @LysandreJik
Issue created: #13246