| """Naive PyTorch paged-attention decode reference (correctness oracle, not SOTA). |
| |
| Single-query decode: each batch element has a query of shape (num_heads, head_dim) |
| and attends over a KV cache of `seq_len[b]` tokens stored as fixed-size pages in |
| a global pool. Pages for batch element b are listed in `block_table[b]`. |
| |
| The reference performs the slow path: |
| 1. Gather pages -> contiguous (seq_len, num_kv_heads, head_dim) per batch element. |
| 2. Repeat KV heads for grouped-query (broadcast num_kv_heads -> num_heads). |
| 3. Manual softmax(QK^T / sqrt(d)) @ V in fp32, cast back to bf16. |
| |
| This avoids torch.nn.functional.scaled_dot_product_attention (which is on the |
| forbidden list) so the agent cannot dispatch through SDPA either. |
| """ |
| import math |
|
|
| import torch |
| import torch.nn as nn |
|
|
| OP_TYPE = "attention" |
| SUPPORTED_PRECISIONS = ["bf16"] |
| HARDWARE_REQUIRED = ["RTX_PRO_6000", "H100", "B200"] |
|
|
|
|
| |
| BATCH = 8 |
| NUM_HEADS = 32 |
| NUM_KV_HEADS = 8 |
| HEAD_DIM = 128 |
| SEQ_LEN = 1024 |
| PAGE_SIZE = 16 |
|
|
|
|
| class Model(nn.Module): |
| """Single-query paged attention decode. |
| |
| Forward inputs (all on device): |
| query: (batch, num_heads, head_dim) bf16 |
| kv_cache: (num_blocks, page_size, num_kv_heads, head_dim * 2) |
| Layout: last dim packs [K | V] so a single gather pulls both. |
| Stored as bf16. |
| block_table: (batch, max_blocks) int32 |
| seq_lens: (batch,) int32 |
| |
| Output: |
| attn_out: (batch, num_heads, head_dim) bf16 |
| """ |
|
|
| def __init__( |
| self, |
| batch: int, |
| num_heads: int, |
| num_kv_heads: int, |
| head_dim: int, |
| seq_len: int, |
| page_size: int, |
| ): |
| super().__init__() |
| assert num_heads % num_kv_heads == 0, "num_heads must be a multiple of num_kv_heads (GQA)" |
| self.batch = batch |
| self.num_heads = num_heads |
| self.num_kv_heads = num_kv_heads |
| self.head_dim = head_dim |
| self.seq_len = seq_len |
| self.page_size = page_size |
| self.group_size = num_heads // num_kv_heads |
| self.scale = 1.0 / math.sqrt(head_dim) |
|
|
| |
| |
| |
| self.register_buffer("_dummy", torch.zeros(1, dtype=torch.bfloat16), persistent=False) |
|
|
| def forward( |
| self, |
| query: torch.Tensor, |
| kv_cache: torch.Tensor, |
| block_table: torch.Tensor, |
| seq_lens: torch.Tensor, |
| ) -> torch.Tensor: |
| B, H, D = query.shape |
| Hkv = self.num_kv_heads |
| G = self.group_size |
| P = self.page_size |
|
|
| out = torch.empty(B, H, D, dtype=query.dtype, device=query.device) |
|
|
| for b in range(B): |
| L = int(seq_lens[b].item()) |
| num_pages = (L + P - 1) // P |
| pages = block_table[b, :num_pages].long() |
| |
| kv = kv_cache.index_select(0, pages) |
| kv = kv.reshape(num_pages * P, Hkv, 2 * D) |
| kv = kv[:L] |
| k = kv[..., :D] |
| v = kv[..., D:] |
|
|
| |
| k = k.repeat_interleave(G, dim=1) |
| v = v.repeat_interleave(G, dim=1) |
|
|
| q = query[b] |
| |
| qf = q.float() |
| kf = k.float() |
| vf = v.float() |
| |
| scores = torch.einsum("hd,lhd->hl", qf, kf) * self.scale |
| probs = torch.softmax(scores, dim=-1) |
| |
| o = torch.einsum("hl,lhd->hd", probs, vf) |
| out[b] = o.to(query.dtype) |
|
|
| return out |
|
|
|
|
| def get_inputs(): |
| """Build random paged inputs for the current module-level shape knobs.""" |
| B = BATCH |
| H = NUM_HEADS |
| Hkv = NUM_KV_HEADS |
| D = HEAD_DIM |
| L = SEQ_LEN |
| P = PAGE_SIZE |
|
|
| pages_per_seq = (L + P - 1) // P |
| |
| |
| total_pages = max(B * pages_per_seq + 8, 64) |
|
|
| query = torch.randn(B, H, D, dtype=torch.bfloat16) * 0.1 |
| kv_cache = torch.randn(total_pages, P, Hkv, 2 * D, dtype=torch.bfloat16) * 0.1 |
|
|
| perm = torch.randperm(total_pages)[: B * pages_per_seq].reshape(B, pages_per_seq).int() |
| |
| block_table = perm.contiguous() |
| seq_lens = torch.full((B,), L, dtype=torch.int32) |
|
|
| return [query, kv_cache, block_table, seq_lens] |
|
|
|
|
| def get_init_inputs(): |
| return [BATCH, NUM_HEADS, NUM_KV_HEADS, HEAD_DIM, SEQ_LEN, PAGE_SIZE] |
|
|