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

Fix unexpected dtype policy changes when quantization fails #19690

Merged
merged 1 commit into from
May 8, 2024

Conversation

james77777778
Copy link
Contributor

This PR fixes unexpected dtype policy changes when quantization fails.

For example, the current codebase will output False in the following snippet:

import numpy as np

from keras import layers

layer = layers.Embedding(10, 16)
layer.build()
x = np.random.randint(0, 9, size=(1, 3))
original_dtype_policy = layer.dtype_policy

try:
    layer.quantize("float8")  # Will fail
except:
    pass
print(original_dtype_policy == layer.dtype_policy)  # False

With this PR, the output will be True.

@@ -266,6 +266,8 @@ def _get_all_valid_policies(self):

@keras_export("keras.dtype_policies.QuantizedFloat8DTypePolicy")
class QuantizedFloat8DTypePolicy(QuantizedDTypePolicy):
default_amax_history_length = 1024
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a static variable that can be utilized in _float8_build

quantized_dtype = f"{mode}_from_{self.dtype_policy.name}"
# We set the internal `self._dtype_policy` instead of using the
# setter to avoid double `quantize` call
self._dtype_policy = dtype_policies.get(quantized_dtype)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've moved the dtype policy setting to the bottom of quantize to prevent unexpected dtype policy changes when quantization fails.

Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the fix.

@fchollet fchollet merged commit e620cb4 into keras-team:master May 8, 2024
7 checks passed
@james77777778 james77777778 deleted the fix-quantize branch May 9, 2024 01:29
@gbaned gbaned added this to Assigned Reviewer in PR Queue via automation May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
PR Queue
Assigned Reviewer
Development

Successfully merging this pull request may close these issues.

None yet

3 participants