Grammar API v2#628
Open
mhayes853 wants to merge 31 commits into
Open
Conversation
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
Signed-off-by: Matthew Hayes <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduces a Grammar API to enable structured generation in coming PRs.
For now, this isn't directly integrated with completion, but rather exposes all the necessary APIs to make that happen in the near future. The underlying state machine used is XGrammar, and for the most part the implementation directly wraps it.
There are 4 essential types:
Grammaran actual representation of a grammar, with conveniences for plain JSON, JSON schema, regex, empty, universal, structural tags, etc.GrammarMatcherthe state machine type responsible for computing bitmasks.acceptandnext_bitmaskdo most of the driving. However, there are also methods to rollback, and to check the termination state.forkis an efficient way to create a copy of matcher (at it's current state) without having to go throughGrammarEngine.GrammarEngineresponsible for compilingGrammarMatcherinstances from aGrammarinstance.GrammarCompilerandCompilerGrammarfrom XGrammar. I felt it was a conceptually simpler API to keep these concepts bound together.forkwhich can be used for efficient copies.compile_matchermethod which takes aGrammarinstance and returns aGrammarMatcher.GrammarVocabularytokenizer info required by the other grammar types.TokenizerInfofrom XGrammar, there is also now a new tokenizer method to produce it.Among other things, I also extracted a helper for constructing the tokenizer from the model path, and also updated the parsing logic for the HF tokenizer file within the tokenizer. This is mainly because the grammar matcher needs the tokenizer info, but the only way to load the tokenizer previously was to go through the entire model initialization (when only the tokenizer is needed). XGrammar also provides a transitive dependency on picojson for proper JSON parsing which should be useful going forward.