2024-12-03 02:17:00 -05:00
|
|
|
from __future__ import annotations
|
2024-04-16 08:54:57 +03:00
|
|
|
|
2024-12-03 02:17:00 -05:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
from vllm.logger import init_logger
|
2024-12-03 13:32:21 -05:00
|
|
|
from vllm.platforms import CpuArchEnum, current_platform
|
2024-12-03 02:17:00 -05:00
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from transformers import PreTrainedTokenizer
|
|
|
|
|
|
|
|
|
|
from vllm.config import ModelConfig
|
|
|
|
|
from vllm.logits_process import LogitsProcessor
|
|
|
|
|
from vllm.sampling_params import GuidedDecodingParams
|
|
|
|
|
|
|
|
|
|
logger = init_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def maybe_backend_fallback(
|
|
|
|
|
guided_params: GuidedDecodingParams) -> GuidedDecodingParams:
|
|
|
|
|
# lm-format-enforce doesn't support grammar, fallback to xgrammar
|
|
|
|
|
if (guided_params.backend == "lm-format-enforcer"
|
|
|
|
|
and guided_params.grammar is not None):
|
|
|
|
|
logger.warning(
|
|
|
|
|
"lm-format-enforcer does not support grammar guided decoding. "
|
|
|
|
|
"Falling back to use xgrammar instead.")
|
|
|
|
|
guided_params.backend = "xgrammar"
|
|
|
|
|
|
|
|
|
|
if guided_params.backend == "xgrammar":
|
2024-12-03 13:32:21 -05:00
|
|
|
# xgrammar only has x86 wheels for linux, fallback to outlines
|
|
|
|
|
if current_platform.get_cpu_architecture() is not CpuArchEnum.X86:
|
|
|
|
|
logger.warning("xgrammar is only supported on x86 CPUs. "
|
|
|
|
|
"Falling back to use outlines instead.")
|
|
|
|
|
guided_params.backend = "outlines"
|
|
|
|
|
|
2024-12-03 02:17:00 -05:00
|
|
|
# xgrammar doesn't support regex or choice, fallback to outlines
|
|
|
|
|
if guided_params.regex is not None or guided_params.choice is not None:
|
|
|
|
|
logger.warning(
|
|
|
|
|
"xgrammar only supports json or grammar guided decoding. "
|
|
|
|
|
"Falling back to use outlines instead.")
|
|
|
|
|
guided_params.backend = "outlines"
|
|
|
|
|
|
|
|
|
|
# xgrammar only supports EBNF grammars and uses the GBNF format
|
|
|
|
|
# https://github.com/ggerganov/llama.cpp/blob/master/grammars/README.md
|
|
|
|
|
elif (guided_params.grammar is not None
|
|
|
|
|
and "::=" not in guided_params.grammar):
|
|
|
|
|
logger.warning("xgrammar only supports EBNF grammars. "
|
|
|
|
|
"Falling back to use outlines instead.")
|
|
|
|
|
guided_params.backend = "outlines"
|
|
|
|
|
|
|
|
|
|
return guided_params
|
2024-04-16 08:54:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_guided_decoding_logits_processor(
|
2024-12-03 02:17:00 -05:00
|
|
|
guided_params: GuidedDecodingParams, tokenizer: PreTrainedTokenizer,
|
|
|
|
|
model_config: ModelConfig) -> LogitsProcessor | None:
|
|
|
|
|
guided_params = maybe_backend_fallback(guided_params)
|
2024-09-30 19:34:25 -06:00
|
|
|
# CFG grammar not supported by LMFE, so we use outlines instead
|
2024-12-03 02:17:00 -05:00
|
|
|
if guided_params.backend == 'outlines':
|
2024-08-24 00:51:38 -07:00
|
|
|
# NOTE: lazy import outlines to avoid https://github.com/vllm-project/vllm/issues/4193
|
|
|
|
|
from vllm.model_executor.guided_decoding.outlines_decoding import ( # noqa
|
|
|
|
|
get_outlines_guided_decoding_logits_processor)
|
2024-04-16 08:54:57 +03:00
|
|
|
return await get_outlines_guided_decoding_logits_processor(
|
2024-09-30 19:34:25 -06:00
|
|
|
guided_params, tokenizer)
|
|
|
|
|
if guided_params.backend == 'lm-format-enforcer':
|
2024-08-03 23:12:09 -04:00
|
|
|
from vllm.model_executor.guided_decoding.lm_format_enforcer_decoding import ( # noqa
|
2024-09-30 19:34:25 -06:00
|
|
|
get_local_lm_format_enforcer_guided_decoding_logits_processor)
|
|
|
|
|
return get_local_lm_format_enforcer_guided_decoding_logits_processor(
|
|
|
|
|
guided_params, tokenizer)
|
2024-12-03 02:17:00 -05:00
|
|
|
if guided_params.backend == 'xgrammar':
|
|
|
|
|
from vllm.model_executor.guided_decoding.xgrammar_decoding import ( # noqa
|
|
|
|
|
get_local_xgrammar_guided_decoding_logits_processor)
|
|
|
|
|
return get_local_xgrammar_guided_decoding_logits_processor(
|
|
|
|
|
guided_params, tokenizer, model_config)
|
2024-04-16 08:54:57 +03:00
|
|
|
|
|
|
|
|
raise ValueError(
|
2024-09-30 19:34:25 -06:00
|
|
|
f"Unknown guided decoding backend '{guided_params.backend}'. "
|
2024-12-03 02:17:00 -05:00
|
|
|
"Must be one of 'outlines, 'lm-format-enforcer', 'xgrammar'")
|
2024-06-04 01:25:29 +02:00
|
|
|
|
|
|
|
|
|
2024-08-03 23:12:09 -04:00
|
|
|
def get_local_guided_decoding_logits_processor(
|
2024-12-03 02:17:00 -05:00
|
|
|
guided_params: GuidedDecodingParams, tokenizer: PreTrainedTokenizer,
|
|
|
|
|
model_config: ModelConfig) -> LogitsProcessor | None:
|
|
|
|
|
guided_params = maybe_backend_fallback(guided_params)
|
2024-09-30 19:34:25 -06:00
|
|
|
# CFG grammar not supported by LMFE, so we use outlines instead
|
2024-12-03 02:17:00 -05:00
|
|
|
if guided_params.backend == 'outlines':
|
2024-08-24 00:51:38 -07:00
|
|
|
# NOTE: lazy import outlines to avoid https://github.com/vllm-project/vllm/issues/4193
|
|
|
|
|
from vllm.model_executor.guided_decoding.outlines_decoding import ( # noqa
|
|
|
|
|
get_local_outlines_guided_decoding_logits_processor)
|
2024-08-03 23:12:09 -04:00
|
|
|
return get_local_outlines_guided_decoding_logits_processor(
|
2024-09-30 19:34:25 -06:00
|
|
|
guided_params, tokenizer)
|
|
|
|
|
if guided_params.backend == 'lm-format-enforcer':
|
2024-08-03 23:12:09 -04:00
|
|
|
from vllm.model_executor.guided_decoding.lm_format_enforcer_decoding import ( # noqa
|
|
|
|
|
get_local_lm_format_enforcer_guided_decoding_logits_processor)
|
|
|
|
|
return get_local_lm_format_enforcer_guided_decoding_logits_processor(
|
2024-09-30 19:34:25 -06:00
|
|
|
guided_params, tokenizer)
|
2024-12-03 02:17:00 -05:00
|
|
|
if guided_params.backend == 'xgrammar':
|
|
|
|
|
from vllm.model_executor.guided_decoding.xgrammar_decoding import ( # noqa
|
|
|
|
|
get_local_xgrammar_guided_decoding_logits_processor)
|
|
|
|
|
return get_local_xgrammar_guided_decoding_logits_processor(
|
|
|
|
|
guided_params, tokenizer, model_config)
|
2024-08-03 23:12:09 -04:00
|
|
|
|
|
|
|
|
raise ValueError(
|
2024-09-30 19:34:25 -06:00
|
|
|
f"Unknown guided decoding backend '{guided_params.backend}'. "
|
2024-12-03 02:17:00 -05:00
|
|
|
"Must be one of 'outlines, 'lm-format-enforcer', 'xgrammar'")
|