NVIDIA / NVIDIA/Model-Optimizer

Bug for register_attention_for_kv_quant

Open
#1,064 1 comment 0 reactions 1 assignee View on GitHub

@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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.