-
Notifications
You must be signed in to change notification settings - Fork 11k
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
main : don't print special tokens with --grammar #6923
Conversation
The CLI interface was recently changed to print special control tokens like the </s> stop message one. This token shouldn't be printed if the grammar flag was passed, unless the grammar specifies it, because that breaks shell-scriptability.
Looks like this was #6807 ?
Looks like your PR accomplishes this part...
... but is this condition included in your PR? It looks like the PR checks whether any grammar is present at all, and doesn't check to see if the grammar specifies special control tokens (?). That said, I'm not entirely sure how a grammar would specify a special control token (I probably need to learn the tokenizer better...). If there's a mechanism or escape sequence to tag part of a grammar as a control token (rather than regular text), then I'm not aware of it. But now that you mention it, that sounds like the sort of thing we may want to add support for later (?).
Do you have an example of the sort of script that fails without this PR? Apologies if any of my questions seem ignorant or dense -- mainly trying to get up to speed with what you're talking about. I trust that what you wrote is important, and I would love it if you could help me understand it better. Thank you very much! |
See:
I like to ask LLMs yes/no questions in shell scripts. I use the grammar flag to force it to only print yes or no. If it instead prints |
That makes sense. I don't use shell scripts with grammars, but I wonder if this functionality would be better added behind a command line option to specifically render special tokens or hide them? If I'm trying to debug a grammar- constrained generation, it would tend to want to display the special tokens rather than hide them. How do you feel about separating this flag out into its own option? |
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.
Logic checks out. Intent is sensible
What's your thought about @HanClinto idea of separating it out into it's own flag? Regardless, should be safe to make a separate PR and merge this in anyway as the default behavior of omitting special token with grammar make sense.
FYI: Android CI is bit broken in main branch, but I see a PR coming in soon to fix. But this doesn't touch android anyway.
I agree it's better to have this as separate flag - should consolidate with the existing |
Possible approach that @jart could potentially use in jart#3 to address @HanClinto 's idea of separating out the flag. Investigated @ggerganov idea of consolidating with existing conversation flag but there is significant enough difference in semantic that i could not merge it. Feel free to adjust as needed or ignore if it puts too much complexity to this PR. |
One thing you could do is print the special tokens out of band to file descriptor 3. Then if a shell script doesn't want them, it could either pass a flag to disable them, or simply say |
I haven't had a chance to look at the grammar yet, but I'm wondering what the logic is behind including the special tokens in the output? |
To test this you can try these cmake -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build
echo "== Expect Control Token To Shared Console 3>&1 =="
./build/bin/main --hf-repo TheBloke/phi-2-GGUF --hf-file phi-2.Q6_K.gguf --grammar 'root ::= "yes" | "no"' --temp 0 -c 0 --no-display-prompt --log-disable -p "<|user|>
Say yes
<|assistant|>" 2>/dev/null 3>&1
echo
echo "== Expect No Control Token To Console because 3>/dev/null =="
./build/bin/main --hf-repo TheBloke/phi-2-GGUF --hf-file phi-2.Q6_K.gguf --grammar 'root ::= "yes" | "no"' --temp 0 -c 0 --no-display-prompt --log-disable -p "<|user|>
Say yes
<|assistant|>" 2>/dev/null 3>/dev/null
echo
echo "== Expect No Control Token To Console because 3>&- =="
./build/bin/main --hf-repo TheBloke/phi-2-GGUF --hf-file phi-2.Q6_K.gguf --grammar 'root ::= "yes" | "no"' --temp 0 -c 0 --no-display-prompt --log-disable -p "<|user|>
Say yes
<|assistant|>" 2>/dev/null 3>&-
echo
echo == Expect No Control Token To Console as we are still in grammar mode ==
./build/bin/main --hf-repo TheBloke/phi-2-GGUF --hf-file phi-2.Q6_K.gguf --grammar 'root ::= "yes" | "no"' --temp 0 -c 0 --no-display-prompt --log-disable -p "<|user|>
Say yes
<|assistant|>" 2>/dev/null
echo
echo == Expect Control Token To Console as we are in normal completion mode ==
./build/bin/main --hf-repo TheBloke/phi-2-GGUF --hf-file phi-2.Q6_K.gguf --temp 0 -c 0 --no-display-prompt --log-disable -p "<|user|>
Hi
<|assistant|>" 2>/dev/null 3>&1
echo |
Might be handy for debugging at least. Also it has tokens showing the split between the user, assistant and end of text. Might be handy for integration if not using the apis or libraries for some reason. It does get me thinking if it's possible to also separate the input special tokens so that the special tokens are fully out of band. Might help make it a little bit more secure? Anyway, is everyone happy enough with the new changes? |
@ngxson thanks. Confident now that full consensus has been reached now. Merging |
The CLI interface was recently changed to print special control tokens like the stop message one. This token shouldn't be printed if the grammar flag was passed, unless the grammar specifies it, because that breaks shell-scriptability.