-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature]: Only apply Guided/Structured grammar after reasoning steps in Reasoning models #12619
Open
1 task done
Labels
Comments
Related to #11908 |
I have a PoC tested with class XGrammarLogitsProcessor:
"""Wrapper class to support pickle protocol"""
config: GrammarConfig
ctx: xgr.CompiledGrammar | None = None
token_bitmask: torch.Tensor = None # type: ignore[assignment]
matchers: list[xgr.GrammarMatcher] = field(default_factory=list)
batch_size: int = field(default=1)
prefilled: bool = field(default=False)
def __call__(self, input_ids: list[int],
scores: torch.Tensor) -> torch.Tensor:
+ if not reasoning_end(input_ids):
+ return scores
if self.ctx is None:
self._ensure_ctx() def reasoning_end(input_ids: list[int]) -> bool:
"""Check if the input_ids contain the end of reasoning token."""
# Hard coded endthink token id </think>
endthink_token_id = 151649
if endthink_token_id in input_ids:
return True I can generalize it to support all R1 models and structured engines if you think this approach is effective. full code: |
Having a draft PR, #12955 Please let me know if the approach works for you. |
2 tasks
1 task
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🚀 The feature, motivation and pitch
Only apply Guided/Structured grammar only in the answer for reasoning model. i.e. for DeepSeek R1 only enforce grammar inside
<answer></answer>
or after</think>
This would make Reasoning models more useful in agent workflow expecting structured output.
Alternatives
No response
Additional context
No response
Before submitting a new issue...
The text was updated successfully, but these errors were encountered: