-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add guided decoding to TGIS gRPC API (#31)
Within the existing `decoding` request parameter section: ```protobuf enum ResponseFormat { // Plain text, no constraints TEXT = 0; // Valid json JSON = 1; } message StringChoices { repeated string choices = 1; } // Mutually-exclusive guided decoding options oneof guided { // Output will be in the specified format ResponseFormat format = 3; // Output will follow the provided JSON schema string json_schema = 4; // Output will follow the provided regex pattern string regex = 5; // Output will be exactly one of the specified choices StringChoices choice = 6; // Output will follow the provided context free grammar string grammar = 7; } ``` Signed-off-by: Nick Hill <[email protected]>
- Loading branch information
Showing
3 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import asyncio | ||
import concurrent.futures | ||
from copy import copy | ||
from re import escape as regex_escape | ||
from typing import Tuple, Union | ||
|
||
import vllm.model_executor.guided_decoding.outlines_decoding as outlines_decoding # noqa: E501 | ||
from vllm.entrypoints.grpc.pb.generation_pb2 import DecodingParameters | ||
from vllm.model_executor.guided_decoding.outlines_decoding import ( | ||
GuidedDecodingMode, _get_cached_logits_processor) | ||
from vllm.model_executor.guided_decoding.outlines_logits_processors import ( | ||
JSONLogitsProcessor, RegexLogitsProcessor) | ||
|
||
|
||
async def get_outlines_guided_decoding_logits_processor( | ||
decoding_params: DecodingParameters, | ||
tokenizer) -> Union[JSONLogitsProcessor, RegexLogitsProcessor, None]: | ||
""" | ||
Check for guided decoding parameters | ||
and get the necessary logits processor for the given guide. | ||
We cache logit processors by (guide, tokenizer), and on cache hit | ||
we make a shallow copy to reuse the same underlying FSM. | ||
""" | ||
guide, mode = _get_guide_and_mode(decoding_params) | ||
if not guide: | ||
return None | ||
|
||
if outlines_decoding.global_thread_pool is None: | ||
outlines_decoding.global_thread_pool = ( | ||
concurrent.futures.ThreadPoolExecutor(max_workers=2)) | ||
loop = asyncio.get_running_loop() | ||
|
||
result = await loop.run_in_executor( | ||
outlines_decoding.global_thread_pool, | ||
_get_cached_logits_processor, | ||
guide, | ||
tokenizer, | ||
mode, | ||
None, # guided_whitespace_pattern - TBD | ||
) | ||
|
||
logits_processor = copy(result) | ||
# reset logits processor's internal state | ||
logits_processor.init_state() | ||
return logits_processor | ||
|
||
|
||
def _get_guide_and_mode( | ||
decoding_params: DecodingParameters, | ||
) -> Union[Tuple[str, GuidedDecodingMode], Tuple[None, None]]: | ||
guided = decoding_params.WhichOneof("guided") | ||
if guided is not None: | ||
if guided == "json_schema": | ||
return decoding_params.json_schema, GuidedDecodingMode.JSON | ||
if guided == "regex": | ||
return decoding_params.regex, GuidedDecodingMode.REGEX | ||
if guided == "choice": | ||
choice_list = decoding_params.choice.choices | ||
if len(choice_list) < 2: | ||
raise ValueError("Must provide at least two choices") | ||
# choice just uses regex | ||
choices = [regex_escape(str(choice)) for choice in choice_list] | ||
choices_regex = "(" + "|".join(choices) + ")" | ||
return choices_regex, GuidedDecodingMode.CHOICE | ||
if guided == "grammar": | ||
return decoding_params.grammar, GuidedDecodingMode.GRAMMAR | ||
if decoding_params.format == DecodingParameters.JSON: | ||
return outlines_decoding.JSON_GRAMMAR, GuidedDecodingMode.GRAMMAR | ||
return None, None |