-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
speculative : add grammar support #2991
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
69f2faf
speculative : add grammar support
ggerganov e0a8658
grammars : add json_arr.gbnf
ggerganov 2d89da4
grammar : add comments to new grammar file
ggerganov 0134578
grammar : remove one nested level
ggerganov ebe41d4
common : warm-up with 2 tokens - seems to work better
ggerganov 6c150d7
speculative : print draft token pieces
ggerganov e7dc5b0
speculative : reuse grammar parser + better logs and comments
ggerganov 2db2471
speculative : avoid grammar_mem
ggerganov c79d130
make : fix speculative build
ggerganov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# This is the same as json.gbnf but we restrict whitespaces at the end of the root array | ||
# Useful for generating JSON arrays | ||
|
||
root ::= arr | ||
value ::= object | array | string | number | ("true" | "false" | "null") ws | ||
|
||
arr ::= | ||
"[\n" ws ( | ||
value | ||
(",\n" ws value)* | ||
)? "]" | ||
|
||
object ::= | ||
"{" ws ( | ||
string ":" ws value | ||
("," ws string ":" ws value)* | ||
)? "}" ws | ||
|
||
array ::= | ||
"[" ws ( | ||
value | ||
("," ws value)* | ||
)? "]" ws | ||
|
||
string ::= | ||
"\"" ( | ||
[^"\\] | | ||
"\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes | ||
)* "\"" ws | ||
|
||
number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws | ||
|
||
# Optional space: by convention, applied in this grammar after literal chars when allowed | ||
ws ::= ([ \t\n] ws)? |
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be a reason to allow grammar states to share a common rules array, as previously discussed for beam search - I think that would avoid the need for relocating pointers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. I guess we can make the rules
std::shared_ptr
so they persist as long as at least one grammar references them.The
rules
never mutate after we create a grammar withllama_grammar_init()
, correct?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the rules are read only. Having them alongside the actual parse state (stacks + partial utf8) is really just a convenience. And we could maybe accomplish sharing just by splitting out the rules and separately managing them from the actual state(s). But the added complexity of that might not be worth it compared to the
shared_ptr
approach.