-
Notifications
You must be signed in to change notification settings - Fork 19.7k
Introduces QuantizationConfig for fine-grained quantization control #21896
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
base: master
Are you sure you want to change the base?
Introduces QuantizationConfig for fine-grained quantization control #21896
Conversation
Summary of ChangesHello @JyotinderSingh, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the quantization capabilities within Keras by introducing a flexible Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request introduces a QuantizationConfig to provide a more flexible and customizable quantization API. This is a significant improvement, allowing users to specify their own quantizers for weights and activations, and enabling features like weight-only quantization. The changes are well-implemented across various layers including Dense, EinsumDense, Embedding, and ReversibleEmbedding, as well as the model-level quantize method. The new QuantizationConfig class is well-designed with serialization support, and the accompanying tests are comprehensive. I have a couple of suggestions for minor code improvements to reduce redundancy and enhance clarity.
keras/src/layers/core/dense.py
Outdated
| # Handle activation quantization | ||
| if config and config.activation_quantizer: | ||
| self.inputs_quantizer = config.activation_quantizer | ||
| elif config and config.activation_quantizer is None: | ||
| # Weight-only quantization | ||
| pass | ||
| else: | ||
| # Default behavior | ||
| self.inputs_quantizer = quantizers.AbsMaxQuantizer(axis=-1) | ||
|
|
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.
This block for handling activation quantization is redundant. The logic is already correctly and sufficiently handled inside _int8_build (and _int4_build) via QuantizationConfig.activation_quantizer_or_default. Removing this block will make the code cleaner and avoid duplication without changing the behavior.
| if self.tie_weights: | ||
| embeddings = ops.transpose(self._embeddings) | ||
| kernel = ops.transpose(self._embeddings) | ||
| scale = ops.transpose(self.embeddings_scale) | ||
| pack_axis = 0 | ||
| orig_dim = self.output_dim | ||
| else: | ||
| embeddings = self.reverse_embeddings | ||
| kernel = self.reverse_embeddings | ||
| scale = self.reverse_embeddings_scale | ||
| unpacked_embeddings = quantizers.unpack_int4( | ||
| embeddings, self.output_dim, axis=0 | ||
| pack_axis = 0 | ||
| orig_dim = self.output_dim |
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.
The assignments for pack_axis and orig_dim are duplicated in both branches of the if self.tie_weights: condition. You can move these assignments out of the if/else block to reduce code duplication and improve readability.
| if self.tie_weights: | |
| embeddings = ops.transpose(self._embeddings) | |
| kernel = ops.transpose(self._embeddings) | |
| scale = ops.transpose(self.embeddings_scale) | |
| pack_axis = 0 | |
| orig_dim = self.output_dim | |
| else: | |
| embeddings = self.reverse_embeddings | |
| kernel = self.reverse_embeddings | |
| scale = self.reverse_embeddings_scale | |
| unpacked_embeddings = quantizers.unpack_int4( | |
| embeddings, self.output_dim, axis=0 | |
| pack_axis = 0 | |
| orig_dim = self.output_dim | |
| pack_axis = 0 | |
| orig_dim = self.output_dim | |
| if self.tie_weights: | |
| kernel = ops.transpose(self._embeddings) | |
| scale = ops.transpose(self.embeddings_scale) | |
| else: | |
| kernel = self.reverse_embeddings | |
| scale = self.reverse_embeddings_scale |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #21896 +/- ##
===========================================
- Coverage 82.36% 61.45% -20.92%
===========================================
Files 578 580 +2
Lines 59816 60047 +231
Branches 9387 9428 +41
===========================================
- Hits 49270 36903 -12367
- Misses 8147 20811 +12664
+ Partials 2399 2333 -66
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
fad1ed2 to
2ae1e37
Compare
2ae1e37 to
a3668d5
Compare
No description provided.