Skip to content
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

feat: add grammar support #1

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion cpp/rn-llama.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <iostream>
#include "common.h"
#include "llama.h"
#include "grammar-parser.h"

namespace rnllama {

Expand Down Expand Up @@ -141,6 +142,9 @@ struct llama_rn_context
llama_context *ctx = nullptr;
gpt_params params;

grammar_parser::parse_state parsed_grammar;
llama_grammar *grammar = nullptr;

bool truncated = false;
bool stopped_eos = false;
bool stopped_word = false;
Expand All @@ -165,6 +169,7 @@ struct llama_rn_context
void rewind()
{
params.antiprompt.clear();
params.grammar.clear();
num_prompt_tokens = 0;
num_tokens_predicted = 0;
generated_text = "";
Expand All @@ -176,9 +181,13 @@ struct llama_rn_context
stopped_limit = false;
stopping_word = "";
multibyte_pending = 0;

n_remain = 0;
n_past = 0;

if (grammar != nullptr) {
llama_grammar_free(grammar);
grammar = nullptr;
}
}

bool loadModel(gpt_params &params_)
Expand All @@ -196,6 +205,31 @@ struct llama_rn_context
return true;
}

bool loadGrammar()
{
if (!params.grammar.empty()) {
parsed_grammar = grammar_parser::parse(params.grammar.c_str());
// will be empty (default) if there are parse errors
if (parsed_grammar.rules.empty()) {
LOG_ERROR("grammar parse error, grammar: %s", params.grammar.c_str());
return false;
}
grammar_parser::print_grammar(stderr, parsed_grammar);

{
auto it = params.logit_bias.find(llama_token_eos());
if (it != params.logit_bias.end() && it->second == -INFINITY) {
LOG_WARNING("EOS token is disabled, which will cause most grammars to fail");
}
}

std::vector<const llama_grammar_element *> grammar_rules(parsed_grammar.c_rules());
grammar = llama_grammar_init(
grammar_rules.data(), grammar_rules.size(), parsed_grammar.symbol_ids.at("root"));
}
return true;
}

void loadPrompt()
{
params.prompt.insert(0, 1, ' '); // always add a first space
Expand Down Expand Up @@ -355,6 +389,10 @@ struct llama_rn_context
logits[llama_token_nl()] = nl_logit;
}

if (grammar != nullptr) {
llama_sample_grammar(ctx, &candidates_p, grammar);
}

if (temp <= 0)
{
// Greedy sampling
Expand Down Expand Up @@ -392,6 +430,10 @@ struct llama_rn_context
}
}

if (grammar != nullptr) {
llama_grammar_accept_token(ctx, grammar, result.tok);
}

for (size_t i = 0; i < std::min(candidates_p.size, (size_t)n_probs); ++i)
{
result.probs.push_back({candidates_p.data[i].id, candidates_p.data[i].p});
Expand Down
2 changes: 1 addition & 1 deletion example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default function App() {
initLlama({
model: file.uri,
use_mlock: true,
n_gpu_layers: 1, // > 0: enable metal
n_gpu_layers: 0, // > 0: enable metal
})
.then((ctx) => {
setContext(ctx)
Expand Down
11 changes: 11 additions & 0 deletions ios/RNLlamaContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ - (NSDictionary *)completion:(NSDictionary *)params

self->llama->params.prompt = [prompt UTF8String];

if (params[@"grammar"]) {
self->llama->params.grammar = [params[@"grammar"] UTF8String];
}

if (params[@"temperature"]) self->llama->params.temp = [params[@"temperature"] doubleValue];

if (params[@"n_threads"]) {
Expand Down Expand Up @@ -188,6 +192,10 @@ - (NSDictionary *)completion:(NSDictionary *)params
}
}
}

if (!self->llama->loadGrammar()) {
@throw [NSException exceptionWithName:@"LlamaException" reason:@"Failed to load grammar" userInfo:nil];
}

self->llama->loadPrompt();
self->llama->beginCompletion();
Expand Down Expand Up @@ -278,6 +286,9 @@ - (void)stopCompletion {
}

- (void)invalidate {
if (self->llama->grammar != nullptr) {
llama_grammar_free(self->llama->grammar);
}
delete self->llama;

// llama_backend_free();
Expand Down
2 changes: 2 additions & 0 deletions scripts/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ cp ./llama.cpp/k_quants.h ./cpp/k_quants.h
cp ./llama.cpp/k_quants.c ./cpp/k_quants.c
cp ./llama.cpp/examples/common.h ./cpp/common.h
cp ./llama.cpp/examples/common.cpp ./cpp/common.cpp
cp ./llama.cpp/examples/grammar-parser.h ./cpp/grammar-parser.h
cp ./llama.cpp/examples/grammar-parser.cpp ./cpp/grammar-parser.cpp

# List of files to process
files=(
Expand Down
1 change: 1 addition & 0 deletions src/NativeRNLlama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type NativeContextParams = {

export type NativeCompletionParams = {
prompt: string
grammar?: string
stop?: Array<string> // -> antiprompt

n_predict?: number
Expand Down