huggingface / huggingface/transformers
[model loading] framework-agnostic dtype parameter
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 166k
- Forks
- 34.6k
- Avg merge
- 3d 9h
- Merged PRs (30d)
- 281
Description
This is a split off from one of the discussions at https://github.com/huggingface/transformers/pull/13209:
- It all started with trying to load torch models under either the desired dtype or the the dtype of the pretrained model - and thus avoid 2x memory usage needs e.g. if the model needs to be just fp16. So we added
torch_dtypetofrom_pretrainedandfrom_config. - Then we started storing
torch_dtypein the config file for future possibly automatic loading model in the optimal "regime". - This resulted in a discrepancy where the same symbol sometimes means
torch.dtypeat other times a string like "float32" as we can't storetorch.dtypein json. - then in https://github.com/huggingface/transformers/pull/13209#discussion_r693292542 we started discussing how
dtypeis really the same across pt/tf/flux and perhaps we should just usedtypein the config and variables and have it consistently to be a string ("float32") and convert it to the right dtype object of the desired framework at the point of use, e.g.getattr(torch, "float32")
A possible solution is to deprecate torch_dtype and replace it with dtype string both in config and in the function argument.
Possible conflicts with the naming:
-
we already have the
dtypeattribute in modeling_utils, which returnstorch.dtypebased on the first param's dtype.https://github.com/huggingface/transformers/blob/master/src/transformers/modeling_utils.py#L205
The context is different, but still this is something to consider to avoid ambiguity.
I may have missed some other areas. So please share if something else needs to be added.
Additional notes:
#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.
@LysandreJik, @sgugger, @patrickvonplaten
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing modeling_utils.py, the dtype attribute, and the from_pretrained and from_config entry points, then read the linked discussion in pull request 13209. Trace how torch_dtype is represented in configuration and converted for each framework. Done means the naming and compatibility approach is agreed and the affected loading paths are consistently covered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, tensorflow
- Domain
- machine-learning
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100