You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I tested the register_attention_for_kv_quant function of modelopt, I found the following issues:
a simple attention pytorch code is follows:
class CommonAttention(nn.Module):
def forward(self, qkv: tuple, extra_arg=None):
# NOTE: Add unused argument y with default value to test that replaced attention retain original defaults
q, k, v = qkv
The register_attention_for_kv_quant function incorrectly patches binary matmul operations (@) in attention layers. The function applies quantizers to incorrect operands and uses wrong transpose operations, resulting in semantically incorrect transformations of the attention computation.
Root Cause / Approach
The patch_binop function has two critical issues: (1) It applies the v_bmm_quantizer to the wrong operand in the first matmul (should quantize v, not k), and (2) It applies incorrect transpose logic that doesn't preserve the original computation semantics. The original code attn @ v becomes (quantized_attn) @ (transposed-quantized-transposed v), which changes the shape and meaning of the operation.
Suggested Fix
The patch_binop function needs to be corrected to: (1) For the first @ operation (q @ k.T), apply quantizers to q and k appropriately; (2) For the second @ operation (attn @ v), apply q_bmm_quantizer to attn and v_bmm_quantizer to v without incorrect transpose wrapping. The transpose=True parameter should only affect the key tensor in the attention pattern (q @ k.T), not the value tensor in the output computation. Review the intended quantization points: typically q and k are quantized in the attention computation, and v is quantized separately in the weighted sum.
Activity
ChenhanYu commentedon Mar 19, 2026
Triage Analysis
Summary
The
register_attention_for_kv_quantfunction incorrectly patches binary matmul operations (@) in attention layers. The function applies quantizers to incorrect operands and uses wrong transpose operations, resulting in semantically incorrect transformations of the attention computation.Root Cause / Approach
The
patch_binopfunction has two critical issues: (1) It applies the v_bmm_quantizer to the wrong operand in the first matmul (should quantize v, not k), and (2) It applies incorrect transpose logic that doesn't preserve the original computation semantics. The original codeattn @ vbecomes(quantized_attn) @ (transposed-quantized-transposed v), which changes the shape and meaning of the operation.Suggested Fix
The
patch_binopfunction needs to be corrected to: (1) For the first @ operation (q @ k.T), apply quantizers to q and k appropriately; (2) For the second @ operation (attn @ v), apply q_bmm_quantizer to attn and v_bmm_quantizer to v without incorrect transpose wrapping. The transpose=True parameter should only affect the key tensor in the attention pattern (q @ k.T), not the value tensor in the output computation. Review the intended quantization points: typically q and k are quantized in the attention computation, and v is quantized separately in the weighted sum.Relevant Files
modelopt/torch/quantization/plugins/attention.pytests/unit/torch/quantization/plugins/test_attention_quant.pyAuto-triaged by pensieve
/magic-triageFix q/k/v quantizer wiring in register_attention_for_kv_quant (NVIDIA…