[Perf][GLM-5.2] Blackwell decode optimizations (#48597)
Signed-off-by: Peiyuan Zhou <peiyuanzhou1994@gmail.com> Signed-off-by: zhou <zhou@zhoudeMacBook-Pro.local> Signed-off-by: Stefan Koncarevic <stefan.koncarevic@amd.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Giancarlo Delfin <32987265+TheEpicDolphin@users.noreply.github.com> Co-authored-by: Thien Tran <gau.nernst@yahoo.com.sg> Co-authored-by: OpenAI Codex <codex@openai.com> Co-authored-by: zhou <zhou@zhoudeMacBook-Pro.local> Co-authored-by: stefankoncarevic <Stefan.Koncarevic@amd.com>
This commit is contained in:
co-authored by
Claude
Giancarlo Delfin
Thien Tran
OpenAI Codex
zhou
stefankoncarevic
parent
0d77325b10
commit
dd72658e7d
@@ -128,6 +128,8 @@ def triton_convert_req_index_to_global_index(
|
||||
prefill_workspace_request_ids: torch.Tensor | None = None,
|
||||
prefill_workspace_starts: torch.Tensor | None = None,
|
||||
return_valid_counts: bool = False,
|
||||
out: torch.Tensor | None = None,
|
||||
valid_counts_out: torch.Tensor | None = None,
|
||||
) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]:
|
||||
"""
|
||||
out[token_id, indice_id] =
|
||||
@@ -178,14 +180,20 @@ def triton_convert_req_index_to_global_index(
|
||||
req_id_c = req_id.contiguous()
|
||||
block_table_c = block_table.contiguous()
|
||||
token_indices_c = token_indices.contiguous()
|
||||
out = torch.empty_like(token_indices_c)
|
||||
# `out`/`valid_counts_out` allow writing into a stable pre-allocated buffer
|
||||
# (used by the shared-physical-index cache to stay cudagraph-safe).
|
||||
out = torch.empty_like(token_indices_c) if out is None else out
|
||||
|
||||
# Allocate valid count buffer if needed (must be zero-initialized for atomics)
|
||||
valid_counts: torch.Tensor | None = None
|
||||
if return_valid_counts:
|
||||
valid_counts = torch.zeros(
|
||||
num_tokens, dtype=torch.int32, device=token_indices.device
|
||||
)
|
||||
if valid_counts_out is None:
|
||||
valid_counts = torch.zeros(
|
||||
num_tokens, dtype=torch.int32, device=token_indices.device
|
||||
)
|
||||
else:
|
||||
valid_counts = valid_counts_out
|
||||
valid_counts.zero_()
|
||||
|
||||
# Strides in elements
|
||||
bt_stride0, bt_stride1 = block_table_c.stride()
|
||||
@@ -341,3 +349,47 @@ def triton_filter_and_convert_dcp_index(
|
||||
assert valid_counts is not None
|
||||
return out, valid_counts
|
||||
return out
|
||||
|
||||
|
||||
# --- Physical-index shadow (DSA index_topk_freq > 1) -------------------------
|
||||
# GLM-5.2 uses index_topk_freq=4: only ~1/4 of layers write a fresh top-k into
|
||||
# the single shared topk_indices_buffer; the rest reuse it. Since block_table
|
||||
# and req_id_per_token are constant within a decode step, the physical indices
|
||||
# (block_table lookup of the logical top-k) are identical across a freq-group.
|
||||
# Fresh layers convert once into a STABLE shadow of the logical buffer and
|
||||
# skip layers read it -- eliminating ~3/4 of the per-layer convert kernels.
|
||||
#
|
||||
# Invariant: shadow[j] == convert(logical[j]) row-wise. Whoever re-arranges
|
||||
# the logical buffer (e.g. the MTP draft compact between the multi-token
|
||||
# step-0 layout and the single-token steps-1+ layout) must apply the same
|
||||
# gather to the shadow.
|
||||
#
|
||||
# Shadows are registered ONLY at buffer creation time (model init, never
|
||||
# inside cudagraph capture) via register_phys_shadow; every other access is
|
||||
# the read-only phys_shadow lookup, so addresses are stable across graph
|
||||
# replays and no allocation can happen during capture.
|
||||
import weakref as _weakref # noqa: E402
|
||||
|
||||
_PHYS_SHADOWS: dict[int, tuple[torch.Tensor, torch.Tensor]] = {}
|
||||
|
||||
|
||||
def register_phys_shadow(topk_buf: torch.Tensor) -> None:
|
||||
"""Allocate the (physical-index, valid-count) shadow for a logical top-k
|
||||
buffer. Call once where the buffer is created. The finalizer drops the
|
||||
entry with the owning buffer, so a recycled id can never alias a stale
|
||||
shadow."""
|
||||
key = id(topk_buf)
|
||||
if key not in _PHYS_SHADOWS:
|
||||
_PHYS_SHADOWS[key] = (
|
||||
torch.empty_like(topk_buf),
|
||||
torch.empty(topk_buf.shape[0], dtype=torch.int32, device=topk_buf.device),
|
||||
)
|
||||
_weakref.finalize(topk_buf, _PHYS_SHADOWS.pop, key, None)
|
||||
|
||||
|
||||
def phys_shadow(
|
||||
topk_buf: torch.Tensor,
|
||||
) -> tuple[torch.Tensor, torch.Tensor] | None:
|
||||
"""The registered shadow for this buffer, or None if it was never
|
||||
registered. Never allocates; capture-safe."""
|
||||
return _PHYS_SHADOWS.get(id(topk_buf))
|
||||
|
||||
Reference in New Issue
Block a user