-
Notifications
You must be signed in to change notification settings - Fork 265
[Quantization] Attention/ KV Cache Refactor #1651
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: main
Are you sure you want to change the base?
Conversation
|
👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review. Note: This is required to complete the testing suite, please only add the label once the PR is code complete and local testing has been performed. |
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.
Summary of Changes
Hello @kylesayrs, 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 integrates the foundational components for applying 'online rotations' (specifically R1 and R2 from the SpinQuant paper) into the llmcompressor framework. It primarily introduces a new SpinQuantModifier that leverages novel model transformation utilities, such as embedding normalization and norm-linear fusion, to prepare models for more effective quantization. Additionally, it refines the handling of tied word embeddings, ensuring compatibility and robustness across various model configurations.
Highlights
- New Feature: SpinQuantModifier: Introduced a new
SpinQuantModifierto apply 'offline' rotations (R1 and R2) from the SpinQuant paper. These rotations transform model weights and activations to improve quantization accuracy without introducing runtime overhead. - Model Transformation Utilities: Added new utilities for normalizing embedding layers and fusing norm layers into subsequent linear layers. These are crucial preprocessing steps for applying SpinQuant rotations, ensuring transform invariance.
- Improved Tied Word Embedding Handling: Refactored and enhanced the utility for untying word embeddings. The updated implementation is more robust, correctly handling cases where embeddings are tied, especially with offloaded parameters, and centralizes the untying logic.
- Example Usage and Integration: Provided new example scripts (
compress_model.py,spinquant_example.py) demonstrating how to use theSpinQuantModifierfor model compression. The modifier is also integrated into the data-free pipeline for seamless application.
Using Gemini Code Assist
The 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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and 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 to provide feedback.
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
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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 support for SpinQuant online rotations, a technique for improving quantization performance. It adds a new SpinQuantModifier, along with utilities for model transformation like layer fusion and embedding normalization. The changes also include updates to the data-free pipeline, improvements to handling tied word embeddings, and new example scripts and tests.
My review identified a critical bug in a Pydantic validator within the new SpinQuantModifier that prevents it from being used. I've also pointed out a few medium-severity issues, including a required argument missing in a script, brittle directory name construction, a documentation typo, and a maintainability concern with a hardcoded pipeline selection. Addressing these points will improve the correctness and robustness of the new features.
a9b2f51 to
49e1d90
Compare
3a5a04a to
b85337f
Compare
9dda155 to
0b6624f
Compare
|
Last nightly worked, but e2e failed due to model storage issues |
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.
looks a lot cleaner
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.
Nice!
The base branch was changed.
f1b8e5a to
57bee27
Compare
Signed-off-by: Kyle Sayers <[email protected]>
Signed-off-by: Kyle Sayers <[email protected]>
d980e6d to
b98e61a
Compare
Signed-off-by: Kyle Sayers <[email protected]>
Signed-off-by: Kyle Sayers <[email protected]>
| if not hasattr(module, "quantization_scheme"): | ||
| continue | ||
| if not hasattr(module, "quantization_scheme"): | ||
| hooks |
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.
missing a return here?
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.
Ah! Not sure how that sneaked through
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.
Thanks for your comment, this is addressed now.
|
|
||
| # Select number of samples. 512 samples is a good place to start. | ||
| # Increasing the number of samples can improve accuracy. | ||
| NUM_CALIBRATION_SAMPLES = 512 |
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.
do we know how much data is actually needed for get decent results?
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.
512 is enough for the basic GSM8K evals
| update_offload_parameter(module, f"{base_name}_scale", scale) | ||
| update_offload_parameter(module, f"{base_name}_zero_point", zero_point) | ||
| if hasattr(module, f"{base_name}_zero_point"): | ||
| update_offload_parameter(module, f"{base_name}_zero_point", zero_point) |
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.
we usually dont run asym quant - why wasn't this a problem before?
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 is added because KV cache quantization is weird: it's the only scheme which does not have a compressor. For that reason, it's the only scheme where we cannot force zero points (vllm throws an error if zero points are present)
What should happen
- KV cache quant is initialized with forced zero points
- Calibration happens and zero points are updated (and stay zero if symmetric)
- Model does not have compressed weights and is saved in frozen state
- vLLM loads and throws away zero points if symmetric (only symmetric is implemented atm)
(alternatively, in step (3) we write an "attention" compressor which throws away zero points)
What this refactor does to avoid this
- KV cache quant is initialized without forced zero points
- Calibration happens and zero points are updated, only if they're present (they're not unless asymmetric)
- Model does not have compressed weights and is saved in frozen state (without zero points)
- vLLM loads scales only (since only symmetric kv cache quantization is supported atm)
|
|
||
| quantized_kv_cache = QuantizedKVParameterCache(scheme.output_activations) | ||
| setattr(module, "kv_cache", quantized_kv_cache) | ||
| def calibrate_value_hook(module: Module, value_states: torch.Tensor): |
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 clean
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 comes from aligning with the patterns and abstractions we've already created, not creating new ones which don't integrate and therefore don't provide as many features.
Signed-off-by: Kyle Sayers <[email protected]>
Purpose
{ "quantization_config": { "config_groups": { "group_0": { "format": null, "input_activations": { "dynamic": false, "num_bits": 8, "observer": "minmax", "strategy": "tensor", "symmetric": true, "type": "float" }, "output_activations": null, "targets": [ "LlamaAttention" ], "weights": null } }, "format": "dense", "ignore": [], "kv_cache_scheme": { "dynamic": false, "group_size": null, "num_bits": 8, "observer": "minmax", "strategy": "tensor", "symmetric": true, "type": "float" }, "quant_method": "compressed-tensors", "quantization_status": "frozen", }, }Prerequisites
Changes
Replace hooks
calibrate_kv_cache_input_hook,calibrate_kv_cache_output_hook,initialize_quantized_kv_cachecalibrate_query_hookcalibrate_key_hook,calibrate_value_hookMiscellaneous
_flatten_attention_flatten_attentionTests
Testing
Evaluation
eval.py
compress.py