DSA Baseline
#2
by zanderjiang - opened
solutions/baseline/dsa/dsa_sparse_attention_h16_ckv512_kpe64_topk2048_ps64/flashinfer_wrapper_5af199.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "flashinfer_wrapper_5af199",
|
| 3 |
+
"definition": "dsa_sparse_attention_h16_ckv512_kpe64_topk2048_ps64",
|
| 4 |
+
"author": "flashinfer",
|
| 5 |
+
"spec": {
|
| 6 |
+
"language": "python",
|
| 7 |
+
"target_hardware": [
|
| 8 |
+
"NVIDIA B200"
|
| 9 |
+
],
|
| 10 |
+
"entry_point": "main.py::run",
|
| 11 |
+
"dependencies": [
|
| 12 |
+
"flashinfer"
|
| 13 |
+
],
|
| 14 |
+
"destination_passing_style": false
|
| 15 |
+
},
|
| 16 |
+
"description": "Solution using FlashInfer trtllm_batch_decode_with_kv_cache_mla for DSA sparse attention (h16, ckv512, kpe64, topk2048, page_size=64).",
|
| 17 |
+
"sources": [
|
| 18 |
+
{
|
| 19 |
+
"path": "main.py",
|
| 20 |
+
"content": "import torch\nimport flashinfer.decode\n\n_WORKSPACE_SIZE_BYTES = 128 * 1024 * 1024\n_workspace_cache = {}\n\nQK_NOPE_HEAD_DIM = 128\nKV_LORA_RANK = 512\nQK_ROPE_HEAD_DIM = 64\nTOPK = 2048\n\n\ndef _get_workspace(device):\n key = str(device)\n buf = _workspace_cache.get(key)\n if buf is None:\n buf = torch.zeros(_WORKSPACE_SIZE_BYTES, dtype=torch.uint8, device=device)\n _workspace_cache[key] = buf\n return buf\n\n\ndef run(q_nope, q_pe, ckv_cache, kpe_cache, sparse_indices, sm_scale):\n num_tokens = q_nope.shape[0]\n num_pages, page_size, _ = ckv_cache.shape\n device = q_nope.device\n\n if isinstance(sm_scale, torch.Tensor):\n bmm1_scale = float(sm_scale.item())\n else:\n bmm1_scale = float(sm_scale)\n\n query = torch.cat([q_nope, q_pe], dim=-1).unsqueeze(1) # [T, 1, H, ckv+kpe]\n kv_cache = torch.cat([ckv_cache, kpe_cache], dim=-1) # [num_pages, page_size, ckv+kpe]\n block_tables = sparse_indices.unsqueeze(1) # [T, 1, topk]\n\n # seq_lens = number of valid (non -1) entries per token\n # The kernel only reads the first seq_lens entries from block_tables;\n # valid entries are already contiguous at the front.\n seq_lens = (sparse_indices != -1).sum(dim=1).to(torch.int32)\n max_seq_len = int(seq_lens.max().item())\n workspace = _get_workspace(device)\n\n output = flashinfer.decode.trtllm_batch_decode_with_kv_cache_mla(\n query=query,\n kv_cache=kv_cache,\n workspace_buffer=workspace,\n qk_nope_head_dim=QK_NOPE_HEAD_DIM,\n kv_lora_rank=KV_LORA_RANK,\n qk_rope_head_dim=QK_ROPE_HEAD_DIM,\n block_tables=block_tables,\n seq_lens=seq_lens,\n max_seq_len=max_seq_len,\n sparse_mla_top_k=TOPK,\n bmm1_scale=bmm1_scale,\n )\n output = output.squeeze(1) # [T, H, ckv]\n\n return (output,)\n"
|
| 21 |
+
}
|
| 22 |
+
]
|
| 23 |
+
}
|
solutions/baseline/dsa/dsa_topk_indexer_fp8_h64_d128_topk2048_ps64/flashinfer_deepgemm_wrapper_2ba145.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "flashinfer_deepgemm_wrapper_2ba145",
|
| 3 |
+
"definition": "dsa_topk_indexer_fp8_h64_d128_topk2048_ps64",
|
| 4 |
+
"author": "flashinfer, deep_gemm",
|
| 5 |
+
"spec": {
|
| 6 |
+
"language": "python",
|
| 7 |
+
"target_hardware": [
|
| 8 |
+
"NVIDIA B200"
|
| 9 |
+
],
|
| 10 |
+
"entry_point": "main.py::run",
|
| 11 |
+
"dependencies": [
|
| 12 |
+
"flashinfer-python",
|
| 13 |
+
"deep_gemm"
|
| 14 |
+
],
|
| 15 |
+
"destination_passing_style": false
|
| 16 |
+
},
|
| 17 |
+
"description": "Solution using deep_gemm FP8 paged MQA logits and FlashInfer top-k page table transform for DSA top-k indexer (fp8, h64, d128, topk2048, page_size=64).",
|
| 18 |
+
"sources": [
|
| 19 |
+
{
|
| 20 |
+
"path": "main.py",
|
| 21 |
+
"content": "import torch\nimport deep_gemm\nimport flashinfer\n\n\n@torch.no_grad()\ndef run(q_index_fp8, k_index_cache_fp8, weights, seq_lens, block_table):\n \"\"\"\n DeepSeek sparse attention top-K indexer using deep_gemm FP8 kernel + FlashInfer.\n \n Pipeline: deep_gemm.fp8_paged_mqa_logits -> flashinfer.top_k_page_table_transform\n \"\"\"\n batch_size, num_index_heads, index_head_dim = q_index_fp8.shape\n num_pages, page_size, _, _ = k_index_cache_fp8.shape\n topk = 2048\n\n # Check constants\n assert num_index_heads == 64\n assert index_head_dim == 128\n assert page_size == 64\n\n device = q_index_fp8.device\n max_num_pages = block_table.shape[1]\n max_context_len = max_num_pages * page_size\n\n # deep_gemm expects q shape: [batch, next_n, heads, head_dim]\n q_index_fp8_4d = q_index_fp8.unsqueeze(1) # [batch, 1, heads, head_dim]\n k_index_cache_uint8 = k_index_cache_fp8.view(torch.uint8)\n\n # Get schedule metadata for deep_gemm\n num_sms = torch.cuda.get_device_properties(device).multi_processor_count\n schedule_meta = deep_gemm.get_paged_mqa_logits_metadata(seq_lens, page_size, num_sms)\n\n # Compute FP8 attention scores using deep_gemm\n logits = deep_gemm.fp8_paged_mqa_logits(\n q_index_fp8_4d,\n k_index_cache_uint8,\n weights,\n seq_lens,\n block_table,\n schedule_meta,\n max_context_len,\n clean_logits=False,\n )\n\n # Build token-level page table for FlashInfer\n offsets = torch.arange(page_size, device=device, dtype=torch.int32)\n physical = block_table.unsqueeze(-1) * page_size + offsets # [batch, max_num_pages, page_size]\n physical_flat = physical.reshape(batch_size, -1) # [batch, max_num_pages * page_size]\n token_indices = torch.arange(max_num_pages * page_size, device=device)\n mask = token_indices.unsqueeze(0) < seq_lens.unsqueeze(1)\n token_page_table = torch.where(mask, physical_flat, torch.zeros_like(physical_flat))\n\n # Run FlashInfer top-k selection\n topk_indices = flashinfer.top_k_page_table_transform(\n input=logits.to(torch.float16), \n src_page_table=token_page_table, \n lengths=seq_lens, \n k=topk\n )\n\n return (topk_indices,)\n"
|
| 22 |
+
}
|
| 23 |
+
]
|
| 24 |
+
}
|