Skip to content

[KV Cache] support kv cache int8 per channel quantization #1663

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Eviannn
Copy link

@Eviannn Eviannn commented Jul 19, 2025

SUMMARY:
kv cache quant int8 per channel is supported using this pr.
Besieds, compressed-tensors needs to be updated as well: neuralmagic/compressed-tensors#398

TEST PLAN:
specify type=int8 and strategy=channel in recipe

Signed-off-by: evian <eviantai@u.nus.edu>
Copy link

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

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 @Eviannn, 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 introduces comprehensive support for int8 per-channel quantization of Key-Value (KV) cache tensors. This involves strategically reshaping the KV cache tensors before quantization to allow for per-channel scaling across the combined head and dimension axis, and then reshaping them back to their original form. Additionally, the quantization observer logic has been enhanced to correctly compute quantization parameters for these newly shaped tensors, ensuring accurate and efficient quantization for LLMs.

Highlights

  • KV Cache Per-Channel Quantization Support: Implemented support for int8 per-channel quantization of Key-Value (KV) cache tensors, enabling more granular quantization for improved model efficiency and reduced memory footprint.
  • Tensor Reshaping for Per-Channel Quantization: Introduced pre-quantization reshaping of KV cache tensors from [batch, num_heads, seq_len, head_dim] to [batch, seq_len, num_heads * head_dim] and subsequent reshaping back after de-quantization. This transformation facilitates per-channel scaling across the combined head and dimension axis.
  • Dynamic Quantization Parameter Calculation: Updated the quantization observer to dynamically determine the correct dimension for calculating per-channel quantization parameters. This enhancement specifically accommodates the new 3D shape of KV cache tensors, ensuring accurate scale and zero-point computation.
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

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

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request introduces per-channel quantization for the KV cache. A potential issue was identified where tensor reshaping logic is applied unconditionally, which could break existing quantization strategies. The review suggests making this logic conditional to ensure the changes only affect the new per-channel quantization path.

Comment on lines +97 to +103
# reshape for per channel scenario
num_heads = key_states.shape[1]
head_dim = key_states.shape[-1]
# from [batch_size, num_heads, seq_len - residual_length, head_dim]
# to [batch_size, seq_len - residual_length, num_heads * head_dim]
key_states = key_states.transpose(1, 2).flatten(2)
value_states = value_states.transpose(1, 2).flatten(2)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The reshape logic is specific to per-channel quantization but is currently applied unconditionally. This can cause incorrect behavior for other quantization strategies. Consider adding a conditional check to apply this logic only when self.quantization_args.strategy == QuantizationStrategy.CHANNEL.

Suggested change
# reshape for per channel scenario
num_heads = key_states.shape[1]
head_dim = key_states.shape[-1]
# from [batch_size, num_heads, seq_len - residual_length, head_dim]
# to [batch_size, seq_len - residual_length, num_heads * head_dim]
key_states = key_states.transpose(1, 2).flatten(2)
value_states = value_states.transpose(1, 2).flatten(2)
# reshape for per channel scenario
num_heads = key_states.shape[1]
head_dim = key_states.shape[-1]
if self.quantization_args.strategy == QuantizationStrategy.CHANNEL:
# from [batch_size, num_heads, seq_len - residual_length, head_dim]
# to [batch_size, seq_len - residual_length, num_heads * head_dim]
key_states = key_states.transpose(1, 2).flatten(2)
value_states = value_states.transpose(1, 2).flatten(2)

Copy link
Author

Choose a reason for hiding this comment

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

per tensor is same

Comment on lines +117 to +123
# reshape for per channel scenario
# from [batch_size, seq_len - residual_length, num_heads * head_dim]
# to [batch_size, num_heads, seq_len - residual_length, head_dim]
qdq_key_states = qdq_key_states.view(
qdq_key_states.shape[0], qdq_key_states.shape[1], num_heads, head_dim).transpose(1, 2)
qdq_value_states = qdq_value_states.view(
qdq_value_states.shape[0], qdq_value_states.shape[1], num_heads, head_dim).transpose(1, 2)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This reverse reshape operation is specific to the per-channel scenario and should be executed conditionally. Applying this to tensors from other quantization strategies will result in shape mismatches. Wrap this in a condition that checks for the per-channel strategy: if self.quantization_args.strategy == QuantizationStrategy.CHANNEL:.

Suggested change
# reshape for per channel scenario
# from [batch_size, seq_len - residual_length, num_heads * head_dim]
# to [batch_size, num_heads, seq_len - residual_length, head_dim]
qdq_key_states = qdq_key_states.view(
qdq_key_states.shape[0], qdq_key_states.shape[1], num_heads, head_dim).transpose(1, 2)
qdq_value_states = qdq_value_states.view(
qdq_value_states.shape[0], qdq_value_states.shape[1], num_heads, head_dim).transpose(1, 2)
if self.quantization_args.strategy == QuantizationStrategy.CHANNEL:
# reshape for per channel scenario
# from [batch_size, seq_len - residual_length, num_heads * head_dim]
# to [batch_size, num_heads, seq_len - residual_length, head_dim]
qdq_key_states = qdq_key_states.view(
qdq_key_states.shape[0], qdq_key_states.shape[1], num_heads, head_dim).transpose(1, 2)
qdq_value_states = qdq_value_states.view(
qdq_value_states.shape[0], qdq_value_states.shape[1], num_heads, head_dim).transpose(1, 2)

Copy link
Author

Choose a reason for hiding this comment

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

per tensor is same

@dsikka
Copy link
Collaborator

dsikka commented Jul 19, 2025

@Eviannn thank you for your PR!
I will review and test your changes.

@dsikka dsikka added the ready When a PR is ready for review label Jul 19, 2025
@Eviannn
Copy link
Author

Eviannn commented Jul 21, 2025

@Eviannn thank you for your PR! I will review and test your changes.

thx a lot! I wonder why test_cache.py uses 3 dims of key/value states while the annotate in QuantizedKVParameterCache shows that the shape is [batch_size, num_heads, seq_len - residual_length, head_dim]

@Eviannn
Copy link
Author

Eviannn commented Jul 21, 2025

@Eviannn thank you for your PR! I will review and test your changes.

CI failed when running test_kv_cache.py, but in my local envs the script can pass......

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready When a PR is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants