-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Bug: llama-gbnf-validator parses grammar but gets a seg fault when validating an input string against the grammar #10321
Comments
Meant to say: the line numbers in the stack trace may be off a bit from the version on GitHub. They correspond to my edited version with the "print" statements, which is attached. |
Thank you for the report and the test case! |
Note that this bug appears related to #7720, but I don't understand the bit about it not working properly in some cases. Best I can tell, the parser doesn't care about upper-case / lower-case, but something might have changed since I last looked at it. |
HanClinto wrote: "Does the bug occur with the release version, how is one to know if the bug is in your modifications."
Answer: How is one to know? Because my only modification is adding print statements... The bug occurs with the original released version as well.
|
Please focus on the primary bug, 1. There is an infinite loop when the grammar (which passes the verification) is used to parse a simple program. There could possibly be something wrong with my grammar, but I don't see it, and the verifier doesn't see it either. And there is also something wrong with the logic in parsing code. It makes some assumptions that are not valid, thus doing an infinite regress. Since the validator apparently does not do a thorough job of checking the grammar, what do you suggest that I need to do to check it? |
@HanClinto Please respond. Thanks. |
I don't remember writing that note, but the extent of your modifications are clear to me, thanks.
My comment was to cross-reference your third point to a previously logged bug in our tracker, so that anyone looking at the other issue could be aware of discussion that is happening here, and vice-versa. Please don't take it to mean that I was ignoring your primary issue.
I have applied your changes and successfully run them, but I have not yet dug into your GBNF file to debug what is going on. I haven't forgotten about this issue, but as I'm currently in the middle of changing jobs, things have been a bit busier than normal the past several weeks. Glancing through your file, and it really is quite impressively large! It's clear you've put a lot of effort into this, and I'm impressed. I can understand why you are having a difficult time validating it manually. Are you using this grammar for anything other than LLM generation, or are you intending to use this as a formal grammar in another context as well? This is not a huge issue, but one thing to note:
Support for Something that I'm curious about is how well a different parser would do with this sort of grammar. It might be possible to adapt this grammar to the llguidance engine, which has some promising advantages over our default gbnf engine. That feature is not yet merged into the trunk, though you can check out #10224 if you wanted to experiment with it. I'm not positive, but I'm optimistic that it might work better for a grammar of this size. My next step is to run your grammar through the parser and double check that it's linking everything correctly, and try to figure out where the infinite loop is happening that you're referencing. We've certainly had trouble with some very large stacks and out-of-memory exceptions with our current parsing structure. I've laid out some ideas for how we might be able to optimize this, but it's a very difficult problem that I haven't fully solved yet. Your test case will provide an excellent stress test, thank you! |
To answer your questions:
I'm glad my test case will help in your development! I have written grammar checkers and compilers before, and I see that your C++ implementation is trying to be very fast (rather than very understandable). Anyway, I couldn't quite understand it, especially given the lousy C++ IDE I'm using (CLion). I would appreciate some estimates of a timeframe when you might be able to debug the infinite loop, or fix the validator, or merge the llguidance engine into the trunk. |
What happened?
I ran
./llama-gbnf-validator mygrammar.txt mytestprogram.txt
and after checking the grammar itself, it started to parse the test file and it went into an infinite loop calling static voidllama_grammar_advance_stack()
and eventually blew up intiny_malloc_from_free_list()
mygrammar.txt
mytestprogram.txt
llama-grammar.cpp.txt
I modified llama-grammar.cpp to add some console debug statements, so the line numbers in the stack trace may be off a bit from the version I used. See the attached file llama-grammar.cpp.txt for my minor changes.
I found numerous bugs and problems with the validator, including these:
nts?
ornts*
ornts+
on the rhs of a rule in the grammar, wherents
is a defined non-terminal symbol defined elsewhere, I get the error "Undefined rule" with no indication of what the rule is, or how or why it is was created as undefined. To fix it, I have to parenthesize the nts, e.g.,(nts)?
, on the right hand side of the rule being defined. Nowhere is it documented thatnts?
is not valid gbnf, and if it is, the validator should complain, rather than just producing an invalid grammar representation.ws
(whitespace) non-terminal symbols in seemingly random places throughout the grammar rules because there is apparently no notion of a lexical analyzer. This requires a very tedious trial-and-error process, because if I put unnecessaryws
tokens in a rule, it prevents the rule from firing, and if I leave a necessary one out, it also prevents the rule from firing! If you cannot make it work like other bnf grammars, please at least document when one must addws
and when must not.Name and Version
% ./llama-cli --version
version: 4075 (fb4a0ec)
built with Apple clang version 16.0.0 (clang-1600.0.26.4) for arm64-apple-darwin23.6.0
What operating system are you seeing the problem on?
Mac
Relevant log output
The text was updated successfully, but these errors were encountered: