NVIDIA / NVIDIA/Model-Optimizer
Bug for register_attention_for_kv_quant
Open
@kaix-nv is already working on this.
Since Mar 19, 2026.
bug
torch.quantization
triaged
- Dominant language
- Python
- Stars
- 3.8k
- Forks
- 604
- Avg merge
- 2d 8h
- Merged PRs (30d)
- 142
Description
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
attn = q @ k
attn = F.softmax(attn)
attn = (attn @ v)
return attn
@classmethod
def get_input(cls, device: str = "cpu"):
q = torch.randn(1, 4, 8, device=device)
k = torch.randn(1, 4, 8, device=device)
v = torch.randn(1, 4, 8, device=device)
return (q, k, v),
After I applied register_attention_for_kv_quant to CommonAttention,CommonAttention is follows:
class _QuantCommonAttention(nn.Module):
def forward(self, qkv: tuple, extra_arg=None):
q, k, v = qkv
k = k.transpose(-2, -1)
attn = q @ self.v_bmm_quantizer(k)
attn = F.softmax(attn)
attn = self.q_bmm_quantizer(attn) @ torch.transpose(self.k_bmm_quantizer(torch.transpose(v, -1, -2)), -1, -2)
return attn
@classmethod
def get_input(cls, device: str='cpu'):
q = torch.randn(1, 4, 8, device=device)
k = torch.randn(1, 4, 8, device=device)
v = torch.randn(1, 4, 8, device=device)
return (q, k, v)
Is this correct?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.