Skip to content

HuggingFace LogitsProcessor to accept also list of compiled grammars #276

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

Open
lukaszkolodziejczyk opened this issue Mar 26, 2025 · 0 comments

Comments

@lukaszkolodziejczyk
Copy link
Contributor

Thanks for this amazing library!

I'd like to be able to provide different compiled grammars within single batch. I couldn't find a way to do this and I believe changes proposed in this PR are one way to enable that.

I'd appreciate your comment and if I should pursue work on this PR. Thanks!

Ubospica pushed a commit that referenced this issue Apr 4, 2025
#275)

My use case is that I need to follow a different grammar for each row in
a batch. I couldn't find a way to do this natively with current API, so
started hacking and came up with the changes in the PR. I'm not sure
about the broader context, so would appreciate a comment if this
approach indeed makes sense.
```
class CountryPoland(ClassSchema):
    country: Literal["Poland"]

class CountryGermany(ClassSchema):
    country: Literal["Germany"]

tokenizer_info = xgr.TokenizerInfo.from_huggingface(tokenizer, vocab_size=config.vocab_size)
grammar_compiler = xgr.GrammarCompiler(tokenizer_info)
compiled_grammars = [
    grammar_compiler.compile_json_schema(CountryPoland),
    grammar_compiler.compile_json_schema(CountryGermany),
]
logit_processor = LogitsProcessor(compiled_grammars)
logit_processor = LogitsProcessorList([logit_processor])

model.generate(**model_inputs, max_new_tokens=512, logits_processor=logit_processor)
```
[Linked issue](#276).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant