Skip to content

Commit 66328fc

Browse files
committed
Merge branch 'master' into concedo_experimental
# Conflicts: # Makefile
2 parents 94499db + 84e09a7 commit 66328fc

15 files changed

+977
-2
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ llama.o: llama.cpp ggml.h ggml-cuda.h llama.h llama-util.h
331331
$(CXX) $(CXXFLAGS) -c $< -o $@
332332
common.o: examples/common.cpp examples/common.h
333333
$(CXX) $(CXXFLAGS) -c $< -o $@
334+
grammar-parser.o: examples/grammar-parser.cpp examples/grammar-parser.h
335+
$(CXX) $(CXXFLAGS) -c $< -o $@
334336
expose.o: expose.cpp expose.h
335337
$(CXX) $(CXXFLAGS) -c $< -o $@
336338
gpttype_adapter_failsafe.o: gpttype_adapter.cpp
@@ -345,7 +347,7 @@ gpttype_adapter_cublas.o: gpttype_adapter.cpp
345347
clean:
346348
rm -vf *.o main quantize_llama quantize_gpt2 quantize_gptj quantize_neox quantize_mpt quantize-stats perplexity embedding benchmark-matmult save-load-state main.exe quantize_llama.exe quantize_gptj.exe quantize_gpt2.exe quantize_neox.exe quantize_mpt.exe koboldcpp.dll koboldcpp_openblas.dll koboldcpp_failsafe.dll koboldcpp_openblas_noavx2.dll koboldcpp_clblast.dll koboldcpp_cublas.dll koboldcpp.so koboldcpp_openblas.so koboldcpp_failsafe.so koboldcpp_openblas_noavx2.so koboldcpp_clblast.so koboldcpp_cublas.so
347349

348-
main: examples/main/main.cpp build-info.h ggml.o k_quants.o llama.o common.o $(OBJS)
350+
main: examples/main/main.cpp build-info.h ggml.o k_quants.o llama.o common.o grammar-parser.o $(OBJS)
349351
$(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
350352
@echo
351353
@echo '==== Run ./main -h for help. ===='

examples/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ set(TARGET common)
1313
add_library(${TARGET} OBJECT
1414
common.h
1515
common.cpp
16+
grammar-parser.h
17+
grammar-parser.cpp
1618
)
1719

1820
if (BUILD_SHARED_LIBS)

examples/common.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,28 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
438438
break;
439439
}
440440
params.input_suffix = argv[i];
441+
} else if (arg == "--grammar") {
442+
if (++i >= argc) {
443+
invalid_param = true;
444+
break;
445+
}
446+
params.grammar = argv[i];
447+
} else if (arg == "--grammar-file") {
448+
if (++i >= argc) {
449+
invalid_param = true;
450+
break;
451+
}
452+
std::ifstream file(argv[i]);
453+
if (!file) {
454+
fprintf(stderr, "error: failed to open file '%s'\n", argv[i]);
455+
invalid_param = true;
456+
break;
457+
}
458+
std::copy(
459+
std::istreambuf_iterator<char>(file),
460+
std::istreambuf_iterator<char>(),
461+
std::back_inserter(params.grammar)
462+
);
441463
} else {
442464
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
443465
gpt_print_usage(argc, argv, default_params);
@@ -514,6 +536,8 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
514536
fprintf(stdout, " modifies the likelihood of token appearing in the completion,\n");
515537
fprintf(stdout, " i.e. `--logit-bias 15043+1` to increase likelihood of token ' Hello',\n");
516538
fprintf(stdout, " or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'\n");
539+
fprintf(stdout, " --grammar GRAMMAR BNF-like grammar to constrain generations (see samples in grammars/ dir)\n");
540+
fprintf(stdout, " --grammar-file FNAME file to read grammar from\n");
517541
fprintf(stdout, " --cfg-negative-prompt PROMPT \n");
518542
fprintf(stdout, " negative prompt to use for guidance. (default: empty)\n");
519543
fprintf(stdout, " --cfg-scale N strength of guidance (default: %f, 1.0 = disable)\n", params.cfg_scale);

examples/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct gpt_params {
6363
std::string path_prompt_cache = ""; // path to file for saving/loading prompt eval state
6464
std::string input_prefix = ""; // string to prefix user inputs with
6565
std::string input_suffix = ""; // string to suffix user inputs with
66+
std::string grammar = ""; // optional BNF-like grammar to constrain sampling
6667
std::vector<std::string> antiprompt; // string upon seeing which more user input is prompted
6768

6869
std::string lora_adapter = ""; // lora adapter path

0 commit comments

Comments
 (0)