[Perf][Model Runner V2] Compact sampling masks on GPU instead of unpacking the full-vocab bitmask on CPU (#54901)

Signed-off-by: aoshen02 <aoshen@inferact.ai>
Signed-off-by: vx120 <893600387@qq.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: vx120 <893600387@qq.com>
This commit is contained in:
aoshen02
2026-09-03 19:03:36 -07:00
committed by GitHub
co-authored by Claude Fable 5.1 vx120
parent d6bce42983
commit 560ef78bfe
10 changed files with 188 additions and 108 deletions
+12 -27
View File
@@ -54,42 +54,27 @@ class LogprobsLists(NamedTuple):
class SamplingMaskLists(NamedTuple):
"""CSR sampling masks; a step slice holds one position (``offsets=None``)."""
# [num_kept_tokens]
token_ids: np.ndarray
# [num_generated_tokens + 1]
offsets: np.ndarray
# [num_reqs + 1]
# [num_positions + 1], or None for a single position
offsets: np.ndarray | None = None
# Unused with one position per request; kept for the wire layout.
cu_num_generated_tokens: list[int] | None = None
def slice_request(self, req_idx: int, num_positions: int) -> "SamplingMaskLists":
if self.cu_num_generated_tokens is None:
start_idx = req_idx
else:
start_idx = self.cu_num_generated_tokens[req_idx]
end_idx = start_idx + num_positions
flat_start = self.offsets[start_idx]
flat_end = self.offsets[end_idx]
assert num_positions == 1 and self.offsets is not None
return SamplingMaskLists(
self.token_ids[flat_start:flat_end],
self.offsets[start_idx : end_idx + 1] - flat_start,
None,
self.token_ids[self.offsets[req_idx] : self.offsets[req_idx + 1]]
)
def to_nested_list(self) -> list[list[int]]:
"""Convert CSR representation to ``list[list[int]]``."""
return [
self.token_ids[int(self.offsets[i]) : int(self.offsets[i + 1])].tolist()
for i in range(len(self.offsets) - 1)
]
@staticmethod
def merge(chunks: Sequence["SamplingMaskLists"]) -> "SamplingMaskLists":
token_ids = np.concatenate([chunk.token_ids for chunk in chunks])
counts = np.concatenate([np.diff(chunk.offsets) for chunk in chunks])
offsets = np.empty(len(counts) + 1, dtype=np.int64)
offsets[0] = 0
np.cumsum(counts, dtype=np.int64, out=offsets[1:])
return SamplingMaskLists(token_ids, offsets)
token_ids = self.token_ids.tolist()
if self.offsets is None:
return [token_ids]
offsets = self.offsets.tolist()
return [token_ids[offsets[i] : offsets[i + 1]] for i in range(len(offsets) - 1)]
class LogprobsTensors(NamedTuple):