From 65eef2612efba5759aaebbabfae37763cf56a277 Mon Sep 17 00:00:00 2001 From: alichen Date: Thu, 13 Aug 2026 02:52:10 -0700 Subject: [PATCH] [SM120] flash_mla: allocate the page-split buffer outside inference mode The lazy persistent buffer in `_split_kv_pages_to_64` is first allocated during autotune, which runs under inference mode, and is written again during CUDA graph capture, which does not. Mutating an inference tensor there raises. Allocate it under `torch.inference_mode(False)` so the buffer outlives the mode it was created in. Reported downstream while running DSV4 on SM120. Co-Authored-By: Claude Opus 5 (1M context) --- .../kernels/ops/attention/flash_mla_sm120.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/python/sglang/kernels/ops/attention/flash_mla_sm120.py b/python/sglang/kernels/ops/attention/flash_mla_sm120.py index ec43c55089ddc..6f39fa05b2528 100644 --- a/python/sglang/kernels/ops/attention/flash_mla_sm120.py +++ b/python/sglang/kernels/ops/attention/flash_mla_sm120.py @@ -400,12 +400,16 @@ def _split_kv_pages_to_64( key = f"flash_mla_sm120_split:{dev}" buf = buffers.get(key) if buf is None or buf.shape[0] < num_dst_pages: - buf = torch.empty( - num_dst_pages, - _BYTES_PER_DST_PAGE_PADDED, - dtype=torch.uint8, - device=dev, - ) + # The first allocation can happen under inference mode (autotune), but + # the buffer is written again during CUDA graph capture outside + # inference mode, where an inference tensor cannot be mutated. + with torch.inference_mode(False): + buf = torch.empty( + num_dst_pages, + _BYTES_PER_DST_PAGE_PADDED, + dtype=torch.uint8, + device=dev, + ) buffers[key] = buf out = buf[:num_dst_pages]