1515 EngineResponse ,
1616 GenToken ,
1717 GuidanceEngineMetrics ,
18+ SamplingParams ,
1819)
1920
2021from ...registry import get_exchange
21- from ..._utils import log_cleanup , log_init , softmax
22+ from ..._utils import log_cleanup , log_init , softmax , apply_top_k_and_top_p_filter
2223from ...visual import (
2324 ExecutionCompletedMessage ,
2425 ExecutionStartedMessage ,
@@ -132,6 +133,8 @@ def __call__(
132133 grammar : str ,
133134 ensure_bos_token : bool = True ,
134135 echo : bool = True ,
136+ sampling_params : Optional [SamplingParams ] = None ,
137+ ** kwargs : Optional [dict ],
135138 ) -> Iterator [EngineResponse ]:
136139 """Main entry point for the inference-parser loop. Yields EngineCallResponse objects as
137140 the parser advances through the grammar.
@@ -148,6 +151,8 @@ def __call__(
148151 Grammar (RawFunction or GrammarFunction) used to extend the prompt.
149152 ensure_bos_token: bool
150153 Ensures that the prompt ends with the BOS token.
154+ sampling_params: Optional[SamplingParams]
155+ Additional sampling parameters to apply to the logits.
151156 """
152157 # TODO: Pass these to get_logits
153158 # images = state.images
@@ -331,6 +336,7 @@ def __call__(
331336 temperature = ll_response .temperature ,
332337 k = self ._top_k ,
333338 force_return_unmasked_probs = echo ,
339+ sampling_params = sampling_params ,
334340 )
335341 last_temperature = ll_response .temperature
336342
@@ -348,6 +354,7 @@ def get_next_token_with_top_k(
348354 temperature : float ,
349355 k : int = 5 ,
350356 force_return_unmasked_probs : bool = False ,
357+ sampling_params : Optional [SamplingParams ] = None ,
351358 ) -> EngineOutput :
352359 """Get the next token and associated top-k tokens from the engine.
353360
@@ -369,6 +376,8 @@ def get_next_token_with_top_k(
369376 The number of top-k tokens to return.
370377 force_return_unmasked_probs: bool
371378 If True, the top-k unmasked probabilities will be returned.
379+ sampling_params : Optional[SamplingParams]
380+ Additional sampling parameters to apply to the logits.
372381
373382 Returns
374383 -------
@@ -399,9 +408,9 @@ def get_top_k(_probs: NDArray, _k: int = 5) -> list[GenToken]:
399408
400409 # compute top-k without masking
401410 probs = (
402- softmax (np .array (logits ))
411+ softmax (apply_top_k_and_top_p_filter ( np .array (logits ), sampling_params ))
403412 if temperature < _TEMPERATURE_EPSILON
404- else softmax (np .array (logits ) / temperature )
413+ else softmax (apply_top_k_and_top_p_filter ( np .array (logits ) / temperature , sampling_params ) )
405414 )
406415
407416 top_k : list [GenToken ] = []
@@ -413,11 +422,14 @@ def get_top_k(_probs: NDArray, _k: int = 5) -> list[GenToken]:
413422 if mask is not None :
414423 mask = np .frombuffer (mask , dtype = np .uint8 )
415424 masked_logits = np .where (mask != 0 , logits , - np .inf )
425+
416426 if temperature < _TEMPERATURE_EPSILON :
417427 masked_logits = np .where (masked_logits == np .max (masked_logits ), 0 , - np .inf )
418428 else :
419429 masked_logits /= temperature
420430
431+ masked_logits = apply_top_k_and_top_p_filter (masked_logits , sampling_params )
432+
421433 masked_probs = softmax (masked_logits )
422434 masked_top_k = get_top_k (masked_probs , k )
423435
0 commit comments