News for hackers. Stuff that matters.
Re:Introducing Structured Outputs in the API by chhabraamit on Tuesday August 06, 2024 @02:04PM (#41173464)
How does llama.cpp’s grammar adherence work?

Does it keep validating the predicted tokens and backtrack when it’s not valid?

Re:Introducing Structured Outputs in the API by gamegoblin on Tuesday August 06, 2024 @02:10PM (#41173519)
It's essentially an Earley Parser[0]. It maintains a set of all possible currently valid parses, and zeroes out the probability of any token that isn't valid in at least 1 of the current potential parse trees.

There are contrived grammars you can give it that will make it use exponential memory, but in practice most real-world grammars aren't like this.

[0] https://en.wikipedia.org/wiki/Earley_parser

Re:Introducing Structured Outputs in the API by jules on Wednesday August 07, 2024 @02:16AM (#41178690)
[delayed]
Re:Introducing Structured Outputs in the API by orlp on Wednesday August 07, 2024 @08:37AM (#41180747)
You don't even need that for JSON. JSON can be expressed using a LR(1) grammar, so you can do it in linear time and space.
Re:Introducing Structured Outputs in the API by gamegoblin on Wednesday August 07, 2024 @12:36PM (#41182998)
Yes, the llama.cpp work supports arbitrary CFGs, not just JSON