Unverified Commit 14ecab5b authored by Yuan Tang's avatar Yuan Tang Committed by GitHub
Browse files

[Bugfix] Guided decoding falls back to outlines when fails to import xgrammar (#12976)


Signed-off-by: default avatarYuan Tang <terrytangyuan@gmail.com>
parent deb6c1c6
...@@ -40,6 +40,8 @@ def maybe_backend_fallback( ...@@ -40,6 +40,8 @@ def maybe_backend_fallback(
guided_params.backend = "outlines" guided_params.backend = "outlines"
if guided_params.backend == "xgrammar": if guided_params.backend == "xgrammar":
from vllm.model_executor.guided_decoding.xgrammar_decoding import (
xgr_installed)
# xgrammar only has x86 wheels for linux, fallback to outlines # xgrammar only has x86 wheels for linux, fallback to outlines
from vllm.platforms import current_platform from vllm.platforms import current_platform
if current_platform.get_cpu_architecture() is not CpuArchEnum.X86: if current_platform.get_cpu_architecture() is not CpuArchEnum.X86:
...@@ -77,6 +79,13 @@ def maybe_backend_fallback( ...@@ -77,6 +79,13 @@ def maybe_backend_fallback(
"Falling back to use outlines instead.") "Falling back to use outlines instead.")
guided_params.backend = "outlines" guided_params.backend = "outlines"
# If the xgrammar module cannot be imported successfully,
# we should still allow users to use guided decoding with a fallback.
elif not xgr_installed:
logger.warning("xgrammar module cannot be imported successfully. "
"Falling back to use outlines instead.")
guided_params.backend = "outlines"
if (guided_params.backend == "outlines" if (guided_params.backend == "outlines"
and guided_params.json_object is not None): and guided_params.json_object is not None):
# outlines doesn't support json_object, fallback to xgrammar # outlines doesn't support json_object, fallback to xgrammar
......
...@@ -14,7 +14,9 @@ from transformers import PreTrainedTokenizerFast ...@@ -14,7 +14,9 @@ from transformers import PreTrainedTokenizerFast
try: try:
import xgrammar as xgr import xgrammar as xgr
from xgrammar.base import _core as xgr_core from xgrammar.base import _core as xgr_core
xgr_installed = True
except ImportError: except ImportError:
xgr_installed = False
pass pass
from vllm.model_executor.guided_decoding.utils import (convert_lark_to_gbnf, from vllm.model_executor.guided_decoding.utils import (convert_lark_to_gbnf,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment