Skip to content

[Bugfix] Fix Per-Token Dynamic Activation Quantization #393

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

max410011
Copy link

@max410011 max410011 commented Jul 14, 2025

Summary

This PR fixes the activation quantization issue described in Issue #394, where the input scale shape was incorrect when using the Dynamic TOKEN strategy.

Fix

  • Corrected the reduction dimensions to ensure only the hidden dimension is reduced.
  • This ensures the input scale shape is (batch_size, seq_len, 1) instead of (1, seq_len, hidden_dim).

@brian-dellabetta
Copy link
Contributor

brian-dellabetta commented Jul 15, 2025

Hi @max410011 , appreciate the thorough detail in the issue! I tried your PR, and both original main and your branch seem to work, the resultant models can be loaded up and run in vllm, which surprises me. This is some old code, and per-token/per-channel always slips me up. I will ask around to see if your reasoning in the issue description is correct.

Copy link
Contributor

@brian-dellabetta brian-dellabetta left a comment

Choose a reason for hiding this comment

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

i validated that this gives the shape described in #394 , and after internal conversations this is correct. This is only an issue when running outside of vllm

Copy link
Contributor

@kylesayrs kylesayrs left a comment

Choose a reason for hiding this comment

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

Could you add a test to demonstrate and verify that these changes are correct? Awesome catch and resolution, thanks!

@@ -167,7 +167,7 @@ def compute_dynamic_scales_and_zp(

keep_dims = True
if args.strategy == QuantizationStrategy.TOKEN:
dim = {1, 2}
dim = {0, 1}
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be generalized to reflect all dims except the last? There are cases where activations are passed with 4 or 5 dimensions, not just 3

Copy link
Author

Choose a reason for hiding this comment

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

As far as I know, the input/output to a linear layer in typical LLMs usually has the shape (bs, seq_len, hidden_dim).

Other activation shapes, such as (bs, num_heads, seq_len, head_dim), generally appear in the attention computation (e.g., Q @ K^T, attention_weights @ V). If we intend for this function to support quantization in those parts as well, then yes, it makes sense to generalize it accordingly.

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.

3 participants