Skip to content

Commit

Permalink
Merge pull request #418 from martindevans/grammar_test
Browse files Browse the repository at this point in the history
Grammar Test (#394 repro)
  • Loading branch information
martindevans authored Jan 8, 2024
2 parents b620e74 + eebe4cb commit b2334b8
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions LLama.Unittest/GrammarTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public GrammarTest()
_params = new ModelParams(Constants.ModelPath)
{
ContextSize = 2048,
Seed = 92,
};
_model = LLamaWeights.LoadFromFile(_params);
}
Expand Down Expand Up @@ -88,5 +89,64 @@ public async Task SampleWithTrivialGrammar()

Assert.Equal("cat", result[0]);
}

//this test is flakey - it reproduces an error which appears to be a bug in llama.cpp
//[Fact]
//public async Task SampleTwiceWithGrammar()
// {
// var executor = new StatelessExecutor(_model, _params);

// var grammar = Grammar.Parse("""
//root ::= (object | array) endline?
//endline ::= "<|im_end|>" ws
//value ::= object | array | string | number | ("true" | "false" | "null") ws

//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]) # 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)?
//""",
// "root");

// using (var grammarInstance = grammar.CreateInstance())
// {
// var inferenceParams = new InferenceParams
// {
// MaxTokens = 20,
// AntiPrompts = new[] { ".", "Input:", "\n", "<|im_end|>" },
// Grammar = grammarInstance,
// };
// var result = await executor.InferAsync("Write a JSON array with the first 6 positive numbers", inferenceParams).ToListAsync();
// }

// using (var grammarInstance2 = grammar.CreateInstance())
// {
// var inferenceParams2 = new InferenceParams
// {
// MaxTokens = 20,
// AntiPrompts = new[] { ".", "Input:", "\n" },
// Grammar = grammarInstance2,
// };
// var result2 = await executor.InferAsync("Write a JSON array with the first 6 positive numbers", inferenceParams2).ToListAsync();
// }
// }
}
}

0 comments on commit b2334b8

Please sign in to comment.