feat: reduce constrained-decoding overhead in TP (#13947)

Signed-off-by: Raayan Dhar raayan.dhar@gmail.com <raayan.dhar@gmail.com>
Signed-off-by: raayandhar <raayan.dhar@gmail.com>
This commit is contained in:
Raayan Dhar
2026-01-09 08:38:32 -08:00
committed by GitHub
parent 5dcff94791
commit 76b3c698d6
7 changed files with 229 additions and 71 deletions

View File

@@ -201,7 +201,11 @@ class SamplingBatchInfo:
return
# Find a grammar from the list
first_grammar = next(grammar for grammar in self.grammars if grammar)
first_grammar = next((grammar for grammar in self.grammars if grammar), None)
if first_grammar is None:
self.vocab_mask = None
self.apply_mask_func = None
return
# TODO(lianmin): Maybe we can reuse the existing mask?
self.vocab_mask = first_grammar.allocate_vocab_mask(
@@ -232,7 +236,7 @@ class SamplingBatchInfo:
else:
self.acc_linear_penalties = None
def apply_logits_bias(self, logits: torch.Tensor):
def apply_logits_bias(self, logits: torch.Tensor, apply_vocab_mask: bool = True):
if self.acc_linear_penalties is not None:
# Used in the overlap mode
logits.add_(self.acc_linear_penalties)
@@ -241,7 +245,7 @@ class SamplingBatchInfo:
# Used in the non-overlap mode
self.penalizer_orchestrator.apply(logits)
if self.vocab_mask is not None:
if apply_vocab_mask and self.vocab_mask is not None:
self.apply_mask_func(logits=logits, vocab_mask=self.vocab_mask)
if self.logit_bias is not None:

View File

@@ -102,6 +102,19 @@ class SamplingParams:
if self.top_k == -1:
self.top_k = TOP_K_ALL # whole vocabulary
@property
def has_grammar_constraint(self) -> bool:
"""
Helper property to check if any grammar constraint
exists for these SamplingParams.
"""
return (
self.json_schema is not None
or self.regex is not None
or self.ebnf is not None
or self.structural_tag is not None
)
def verify(self, vocab_size):
if self.temperature < 0.0:
raise ValueError(