Repository navigation
Add C grammar - #2357
Add C grammar#2357
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
|
@ejones let me know what you think. Also, I'm curious if you have been able to measure the quality of the grammar-based sampling as compared to without, if not, I would be happy to discuss ideas. |
ejones
left a comment
There was a problem hiding this comment.
I think this is fine for a start, thanks @siraben! The quality of the generated code will obviously be affected by restricting to a small subset of C (which the model is not aware of).
left-recursion
Yeah, that's a problem. We should do some checking or normalization for that
How does this interact with finetuned models?
I haven't run any myself, but I recall reports of using it with WizardLM from the original PR. I would expect it's similar, although any formatting required by the finetuned model would need to be incorporated into the grammar.
large sequences of whitespace
Yeah, I experienced this myself with JSON grammars. My solution was to limit the amount of whitespace (e.g to one character)
can certain productions be penalized?
Currently the penalties are all-or-nothing, but this is a good idea and I envisioned adding something like biases as a grammar feature.
measure the quality of the grammar-based sampling as compared to without,
Nothing rigorous, no. Happy to discuss!
| @@ -0,0 +1,42 @@ | |||
| root ::= (declaration)* | |||
|
|
|||
| declaration ::= dataType identifier "(" parameter? ")" "{" statement* "}" | |||
There was a problem hiding this comment.
Presumably this should allow multiple parameters e.g. parameter ws ("," ws parameter)*
| declaration ::= dataType identifier "(" parameter? ")" "{" statement* "}" | ||
|
|
||
| dataType ::= "int" ws | "float" ws | "char" ws | ||
| identifier ::= [a-zA-Z_] [a-zA-Z_0-9]* |
There was a problem hiding this comment.
This matches keywords, which, with the function call syntax, allows the model to produce things like while (n1/n2); (llama-2-13b did this quite a bit in my testing, although it's possible some of that was it trying to use syntaxes missing from this grammar). I don't have a great solution for this, other than over-constraining identifiers (e.g. forcing them to start with "n_"), or a complicated rule to precisely match character sequences other than "if", "for" etc. I came up with this hack for testing, which is of course overly restrictive:
identifier ::= [a-eghj-qs-vxyz] [a-zA-Z_0-9]*
We may need a grammar feature for this.
| parameter ::= dataType identifier | ||
|
|
||
| statement ::= | ||
| ( dataType identifier ws "=" ws expression ";" ) | |
There was a problem hiding this comment.
suggestion: I adopted the convention of always specifying whitespace after literal strings or character ranges (when allowed/required). This makes it easier to keep track of. In this example, ws could be moved to the end of identifier (and assumed wherever identifier is used), while the semicolon should allow optional trailing whitespace.
| ( "while" "(" condition ")" "{" statement* "}" ) | | ||
| ( "for" "(" forInit ";" ws condition ";" ws forUpdate ")" "{" statement* "}" ) | | ||
| ( "if" "(" condition ")" "{" statement* "}" ("else" "{" statement* "}")? ) | |
There was a problem hiding this comment.
Not sure if intentional but the keywords, parens, and braces here should presumably permit whitespace, e.g.,
"while" [ \t\n]* "("
or maybe limiting it to
"while" " "? "("
There was a problem hiding this comment.
I think this was added because the LLM has a tendency to sample blank tokens, it may depend on which model though.
|
Question: how I can write my own grammar files ? Exist any documentation about that? |
|
@ggerganov thoughts on this? Let me know if you want any other changes. |
Based on the work in #1773.
Some thoughts:
Generating a function to square a number
Fibonacci