import math import torch import torch.nn as nn import triton import triton.language as tl OP_TYPE = "attention" SUPPORTED_PRECISIONS = ["bf16"] HARDWARE_REQUIRED = ["RTX_PRO_6000", "H100", "B200"] @triton.jit def paged_attn_decode_kernel( Q_ptr, K_ptr, BT_ptr, L_ptr, Out_ptr, stride_q_b, stride_q_h, stride_q_d, stride_kv_block, stride_kv_page, stride_kv_h, stride_kv_d, stride_bt_b, stride_bt_p, stride_out_b, stride_out_h, stride_out_d, num_heads, num_kv_heads, page_size, group_size, scale, BLOCK_KV: tl.constexpr, BLOCK_D: tl.constexpr, ): # Map thread block to batch and query head b = tl.program_id(0) h = tl.program_id(1) # GQA: KV head is query head // group_size h_kv = h // group_size # Load sequence length L = tl.load(L_ptr + b) if L <= 0: return # Load query offs_d = tl.arange(0, BLOCK_D) q_ptrs = Q_ptr + b * stride_q_b + h * stride_q_h + offs_d * stride_q_d q = tl.load(q_ptrs).to(tl.float32) # Initialize online softmax statistics m_i = -1e9 l_i = 0.0 acc = tl.zeros((BLOCK_D,), dtype=tl.float32) # Loop over KV cache blocks of BLOCK_KV tokens for tok_start in range(0, L, BLOCK_KV): offs_t = tok_start + tl.arange(0, BLOCK_KV) # Page indices and offsets offs_p = offs_t // page_size offs_o = offs_t % page_size # Load page IDs from block table bt_ptrs = BT_ptr + b * stride_bt_b + offs_p * stride_bt_p page_ids = tl.load(bt_ptrs, mask=offs_t < L, other=0) page_ids_2d = tl.expand_dims(page_ids, 1) offs_o_2d = tl.expand_dims(offs_o, 1) # Load K and V k_ptrs = K_ptr + page_ids_2d * stride_kv_block + offs_o_2d * stride_kv_page + h_kv * stride_kv_h + tl.expand_dims(offs_d, 0) * stride_kv_d v_ptrs = K_ptr + page_ids_2d * stride_kv_block + offs_o_2d * stride_kv_page + h_kv * stride_kv_h + tl.expand_dims(BLOCK_D + offs_d, 0) * stride_kv_d mask_2d = tl.expand_dims(offs_t < L, 1) k = tl.load(k_ptrs, mask=mask_2d, other=0.0).to(tl.float32) v = tl.load(v_ptrs, mask=mask_2d, other=0.0).to(tl.float32) # Compute scores: QK^T scores = tl.sum(k * tl.expand_dims(q, 0), axis=1) * scale # Mask out of bounds scores = tl.where(offs_t < L, scores, -1e9) # Online softmax update m_j = tl.max(scores, axis=0) m_next = tl.maximum(m_i, m_j) alpha_i = tl.exp(m_i - m_next) alpha_j = tl.exp(scores - m_next) l_j = tl.sum(alpha_j, axis=0) l_next = l_i * alpha_i + l_j acc = acc * alpha_i + tl.sum(v * tl.expand_dims(alpha_j, 1), axis=0) m_i = m_next l_i = l_next # Normalize accumulator out = acc / l_i # Store output out_ptrs = Out_ptr + b * stride_out_b + h * stride_out_h + offs_d * stride_out_d tl.store(out_ptrs, out.to(tl.float16)) class Model(nn.Module): 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 out = torch.empty_like(query) # Dynamic selection of BLOCK_KV and num_warps based on shape block_kv = 64 num_warps = 4 if self.head_dim == 128: if self.seq_len == 1024: block_kv, num_warps = 128, 4 elif self.seq_len == 2048: block_kv, num_warps = 32, 2 elif self.seq_len == 4096: block_kv, num_warps = 128, 4 elif self.seq_len == 1535: block_kv, num_warps = 64, 2 elif self.head_dim == 64: if self.seq_len == 2000: block_kv, num_warps = 256, 4 grid = (B, H) paged_attn_decode_kernel[grid]( query, kv_cache, block_table, seq_lens, out, query.stride(0), query.stride(1), query.stride(2), kv_cache.stride(0), kv_cache.stride(1), kv_cache.stride(2), kv_cache.stride(3), block_table.stride(0), block_table.stride(1), out.stride(0), out.stride(1), out.stride(2), self.num_heads, self.num_kv_heads, self.page_size, self.group_size, self.scale, BLOCK_KV=block_kv, BLOCK_D=self.head_dim, num_warps=num_warps, ) return out def get_inputs(): from reference import get_inputs as ref_get_inputs return ref_get_inputs() def get_init_inputs(): from reference import get_init_inputs as ref_get_init_inputs return ref_get_init_inputs()