[Bugfix][Structured Output] Support outlines engine with reasoning outputs for DeepSeek R1 (#14114)

This commit is contained in:
Ce Gao
2025-03-06 03:49:20 +00:00
committed by GitHub
parent abcc61e0af
commit f5f7f00cd9
8 changed files with 147 additions and 12 deletions
@@ -112,6 +112,7 @@ async def get_guided_decoding_logits_processor(
reasoner = get_reasoner(tokenizer, reasoning_backend)
guided_params = maybe_backend_fallback(guided_params)
# CFG grammar not supported by LMFE, so we use outlines instead
if guided_params.backend_name == 'outlines':
# NOTE: lazy import outlines to avoid https://github.com/vllm-project/vllm/issues/4193
@@ -43,7 +43,7 @@ class BaseLogitsProcessor:
def __init__(self, guide: Guide, reasoner: Optional[Reasoner]):
self._guide: Guide = guide
self._reasoner = reasoner
self._reasoner: Optional[Reasoner] = reasoner
# CFGState is used for the FSM state for CFGGuide
self._fsm_state: DefaultDict[int, Union[int,
CFGState]] = defaultdict(int)
@@ -54,10 +54,14 @@ class BaseLogitsProcessor:
# Skip the structured logits processing if reasoning is not finished.
# reasoner is not None only when `--enable-reasoning` is set.
if self._reasoner is not None and \
not self._reasoner.is_reasoning_end(
input_ids):
return scores
if self._reasoner is not None:
if not self._reasoner.is_reasoning_end(input_ids):
return scores
else:
# Remove the reasoning tokens from the input_ids
# We need this because our implementation relies on the
# hash of the input_ids to store the FSM state.
input_ids = self._reasoner.extract_content(input_ids)
seq_id = hash(tuple(input_ids))
@@ -4,10 +4,13 @@ from __future__ import annotations
from transformers import PreTrainedTokenizer
from vllm.logger import init_logger
from vllm.model_executor.guided_decoding.reasoner.deepseek_reasoner import ( # noqa: E501
DeepSeekReasoner)
from vllm.model_executor.guided_decoding.reasoner.reasoner import Reasoner
logger = init_logger(__name__)
def get_reasoner(tokenizer: PreTrainedTokenizer,
reasoning_backend: str | None) -> Reasoner | None:
@@ -17,7 +20,12 @@ def get_reasoner(tokenizer: PreTrainedTokenizer,
elif reasoning_backend == "deepseek_r1":
return DeepSeekReasoner.from_tokenizer(tokenizer)
else:
raise ValueError(f"Unknown reasoning backend '{reasoning_backend}'")
# Raise a warning for unknown reasoning backend and return None
# We cannot raise an error here because some reasoning models
# may not have a corresponding Reasoner class.
logger.warning("Unknown reasoning backend %s for structured outputs ",
reasoning_backend)
return None
__all__ = ["Reasoner", "get_reasoner"]
@@ -26,3 +26,13 @@ class DeepSeekReasoner(Reasoner):
def is_reasoning_end(self, input_ids: list[int]) -> bool:
return self.end_token_id in input_ids
def extract_content(self, input_ids: list[int]) -> list[int]:
"""
Extract the content after the end tokens
"""
if self.end_token_id not in input_ids or \
input_ids.index(self.end_token_id) + 1 == len(input_ids):
return []
else:
return input_ids[input_ids.index(self.end_token_id) + 1:]
@@ -17,3 +17,7 @@ class Reasoner(ABC):
@abstractmethod
def is_reasoning_end(self, input_ids: list[int]) -> bool:
pass
@abstractmethod
def extract_content(self, input_ids: list[int]) -> list[int]:
pass
@@ -392,7 +392,7 @@ class XGrammarLogitsProcessor:
def clone(self) -> XGrammarLogitsProcessor:
"""Create a new instance with shared compiled grammar
but separate state"""
new_processor = XGrammarLogitsProcessor(self.config)
new_processor = XGrammarLogitsProcessor(self.config, self.reasoner)
# Share the compiled grammar context (immutable after compilation)
new_processor.ctx = self.ctx