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

TF_Deberta supporting mixed precision #32618

Merged

Conversation

pinesnow72
Copy link
Contributor

What does this PR do?

This PR fixes some codes of TF DeBERTa (version 1 and 2) that do not support Mixed Precision.

With TF and Keras, mixed precision is configured with the following settings:

policy = keras.mixed_precision.Policy('mixed_float16')
keras.mixed_precision.set_global_policy(policy)

However, the existing TFDebertaModel and TFDebertaV2Model throws TypeError with this mixed precision setting.
With mixed precision, TF and Keras requires to use Layer.dtype for model or layer's weights and Layer.compute_dtype for internal tensor computation. But the current TFDebertaModel and TFDebertaV2Model codes do not seem to reflect this requirement and definitely assume the dtype would be tf.float32.

I searched and corrected some error-prone code snippets in modeling_tf_deberta.py and modeling_tf_deberta_v2.py.
This correction worked correctly with mixed precision in my local machine.

This PR fixes #31989

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • [V] Did you read the contributor guideline,
    Pull Request section?
  • [V] Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@Rocketknight1, @ArthurZucker

Corrected some codes which do not support mixed precision
Corrected some codes which do not support mixed precision
@Rocketknight1
Copy link
Member

Hi @pinesnow72, this looks good! Feel free to ping me whenever it's ready for review. The issues with code quality can be fixed by doing pip install transformers[quality] followed by make style in the repo directory.

@pinesnow72
Copy link
Contributor Author

Hi @pinesnow72, this looks good! Feel free to ping me whenever it's ready for review. The issues with code quality can be fixed by doing pip install transformers[quality] followed by make style in the repo directory.

Hi @Rocketknight1, As your comment, the style was corrected and uploaded. Thanks.

@Rocketknight1
Copy link
Member

LGTM now! cc @amyeroberts for core maintainer review, and thanks for the PR!

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 opening a PR to handle this!

Just a small question

@@ -701,9 +701,9 @@ def linear(w, b, x):
ws = tf.split(
tf.transpose(self.in_proj.weight[0]), num_or_size_splits=self.num_attention_heads * 3, axis=0
)
qkvw = tf.TensorArray(dtype=tf.float32, size=3)
qkvw = tf.TensorArray(dtype=self.dtype, size=3)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the difference between self.dtype and self.compute_dtype ?

Copy link
Member

Choose a reason for hiding this comment

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

self.dtype is the dtype the layer's weights are stored in, self.compute_dtype is the dtype used for computation. By default these are the same, but in mixed-precision it's common for self.dtype to be float32 while self.compute_dtype is (b)float16

@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.

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 making mixed precision possible for this model!

@amyeroberts amyeroberts merged commit 9d2ab88 into huggingface:main Aug 13, 2024
21 checks passed
@pinesnow72
Copy link
Contributor Author

Thanks for accepting this PR

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.

TFDebertaModel and TFDebertaV2Model throws TypeError when keras.fit with Mixed Precision
4 participants