Skip to content

Commit 0105549

Browse files
committed
[aux] Memory example (simple)
Adapt simple example to showcase how to load from memory. Can be configured with environment variables. Qwen3, for example, can be used with the simple example.
1 parent 54fbeb2 commit 0105549

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

examples/simple/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
set(TARGET llama-simple)
22
add_executable(${TARGET} simple.cpp)
33
install(TARGETS ${TARGET} RUNTIME)
4-
target_link_libraries(${TARGET} PRIVATE llama ${CMAKE_THREAD_LIBS_INIT})
4+
target_link_libraries(${TARGET} PRIVATE llama llama-common-test ${CMAKE_THREAD_LIBS_INIT})
55
target_compile_features(${TARGET} PRIVATE cxx_std_17)

examples/simple/simple.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1+
#include "llama-cpp.h"
12
#include "llama.h"
23
#include <cstdio>
34
#include <cstring>
45
#include <string>
5-
#include <vector>
66

77
static void print_usage(int, char ** argv) {
88
printf("\nexample usage:\n");
99
printf("\n %s -m model.gguf [-n n_predict] [-ngl n_gpu_layers] [prompt]\n", argv[0]);
10+
printf("\n Optional environment variables: LLAMA_EXAMPLE_MEMORY_BUFFER LLAMA_EXAMPLE_MEMORY_BUFFER_SPLIT");
1011
printf("\n");
1112
}
1213

14+
#ifdef LLAMA_COMMON_TEST_HEADERS
15+
#include "load_into_memory.h"
16+
#endif
17+
1318
int main(int argc, char ** argv) {
1419
// path to the model gguf file
1520
std::string model_path;
@@ -83,12 +88,13 @@ int main(int argc, char ** argv) {
8388
llama_model_params model_params = llama_model_default_params();
8489
model_params.n_gpu_layers = ngl;
8590

91+
#ifdef LLAMA_COMMON_TEST_HEADERS
92+
llama_model * model = memory_configuration_env_is_set() ?
93+
load_model_from_memory_configuration(model_path.c_str(), model_params) :
94+
llama_model_load_from_file(model_path.c_str(), model_params);
95+
#else
8696
llama_model * model = llama_model_load_from_file(model_path.c_str(), model_params);
87-
88-
if (model == NULL) {
89-
fprintf(stderr , "%s: error: unable to load model\n" , __func__);
90-
return 1;
91-
}
97+
#endif
9298

9399
const llama_vocab * vocab = llama_model_get_vocab(model);
94100
// tokenize the prompt

0 commit comments

Comments
 (0)