[Perf] Parallelize fill_bitmask to accelerate high-throughput guided decoding (#21862)

Signed-off-by: Benjamin Chislett <benjamin.chislett@centml.ai>
This commit is contained in:
Benjamin Chislett
2025-08-05 19:57:49 -07:00
committed by GitHub
parent 8e6c7e873f
commit 7e6544c797
3 changed files with 102 additions and 39 deletions
+7 -2
View File
@@ -1324,9 +1324,14 @@ class GPUModelRunner(LoRAModelRunnerMixin, KVConnectorModelRunnerMixin):
cumulative_index += 1 + num_spec_tokens
grammar_bitmask = sorted_bitmask
# If the grammar bitmask and the logits have the same shape
# we don't need to pass indices to the kernel,
# since the bitmask is already aligned with the logits.
skip_out_indices = grammar_bitmask.shape[0] == logits.shape[0]
# Serialization of np.ndarray is much more efficient than a tensor,
# so we receive it in that format.
grammar_bitmask = torch.from_numpy(grammar_bitmask)
grammar_bitmask = torch.from_numpy(grammar_bitmask).contiguous()
# Force use of the torch.compile implementation from xgrammar to work
# around issues with the Triton kernel in concurrent structured output
@@ -1334,7 +1339,7 @@ class GPUModelRunner(LoRAModelRunnerMixin, KVConnectorModelRunnerMixin):
xgr_torch_compile.apply_token_bitmask_inplace_torch_compile(
logits,
grammar_bitmask.to(self.device, non_blocking=True),
indices=out_indices,
indices=out_indices if not skip_out_indices else None,
)
def sync_and_slice_intermediate_tensors(