Skip to content

Commit a316a42

Browse files
committed
Overhaul the examples structure
- main -> examples - utils -> examples (renamed to "common") - quantize -> examples - separate tools for "perplexity" and "embedding" Hope I didn't break something !
1 parent ecbe466 commit a316a42

21 files changed

+361
-161
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ models/*
1919
/main
2020
/quantize
2121
/result
22+
/perplexity
2223

2324
arm_neon.h
2425
compile_commands.json

CMakeLists.txt

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,6 @@ endif()
211211
# Build libraries
212212
#
213213

214-
add_library(utils OBJECT
215-
utils.cpp
216-
utils.h)
217-
218-
target_include_directories(utils PUBLIC .)
219-
target_compile_features(utils PUBLIC cxx_std_11) # don't bump
220-
target_link_libraries(utils PRIVATE ${LLAMA_EXTRA_LIBS})
221-
if (BUILD_SHARED_LIBS)
222-
set_target_properties(utils PROPERTIES POSITION_INDEPENDENT_CODE ON)
223-
endif()
224-
225214
add_library(ggml OBJECT
226215
ggml.c
227216
ggml.h)
@@ -239,22 +228,12 @@ add_library(llama
239228

240229
target_include_directories(llama PUBLIC .)
241230
target_compile_features(llama PUBLIC cxx_std_11) # don't bump
242-
target_link_libraries(llama PRIVATE utils ggml ${LLAMA_EXTRA_LIBS})
231+
target_link_libraries(llama PRIVATE ggml ${LLAMA_EXTRA_LIBS})
243232
if (BUILD_SHARED_LIBS)
244233
set_target_properties(llama PROPERTIES POSITION_INDEPENDENT_CODE ON)
245234
target_compile_definitions(llama PRIVATE LLAMA_SHARED LLAMA_BUILD)
246235
endif()
247236

248-
#
249-
# Executables
250-
#
251-
252-
add_executable(main main.cpp)
253-
target_link_libraries(main PRIVATE llama ggml utils)
254-
255-
add_executable(quantize quantize.cpp)
256-
target_link_libraries(quantize PRIVATE llama ggml utils)
257-
258237
#
259238
# programs, examples and tests
260239
#
@@ -264,6 +243,6 @@ if (LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION)
264243
add_subdirectory(tests)
265244
endif ()
266245

267-
#if (LLAMA_BUILD_EXAMPLES)
268-
# add_subdirectory(examples)
269-
#endif()
246+
if (LLAMA_BUILD_EXAMPLES)
247+
add_subdirectory(examples)
248+
endif()

Makefile

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ $(info I CC: $(CCV))
212212
$(info I CXX: $(CXXV))
213213
$(info )
214214

215-
default: main quantize
215+
default: main quantize perplexity
216216

217217
#
218218
# Build library
@@ -224,20 +224,23 @@ ggml.o: ggml.c ggml.h
224224
llama.o: llama.cpp llama.h
225225
$(CXX) $(CXXFLAGS) -c llama.cpp -o llama.o
226226

227-
utils.o: utils.cpp utils.h
228-
$(CXX) $(CXXFLAGS) -c utils.cpp -o utils.o
227+
common.o: examples/common.cpp examples/common.h
228+
$(CXX) $(CXXFLAGS) -c examples/common.cpp -o common.o
229229

230230
clean:
231-
rm -f *.o main quantize
231+
rm -vf *.o main quantize perplexity
232232

233-
main: main.cpp ggml.o llama.o utils.o
234-
$(CXX) $(CXXFLAGS) main.cpp ggml.o llama.o utils.o -o main $(LDFLAGS)
233+
main: examples/main/main.cpp ggml.o llama.o common.o
234+
$(CXX) $(CXXFLAGS) examples/main/main.cpp ggml.o llama.o common.o -o main $(LDFLAGS)
235235
@echo
236236
@echo '==== Run ./main -h for help. ===='
237237
@echo
238238

239-
quantize: quantize.cpp ggml.o llama.o utils.o
240-
$(CXX) $(CXXFLAGS) quantize.cpp ggml.o llama.o utils.o -o quantize $(LDFLAGS)
239+
quantize: examples/quantize/quantize.cpp ggml.o llama.o
240+
$(CXX) $(CXXFLAGS) examples/quantize/quantize.cpp ggml.o llama.o -o quantize $(LDFLAGS)
241+
242+
perplexity: examples/perplexity/perplexity.cpp ggml.o llama.o common.o
243+
$(CXX) $(CXXFLAGS) examples/perplexity/perplexity.cpp ggml.o llama.o common.o -o perplexity $(LDFLAGS)
241244

242245
#
243246
# Tests

examples/CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# dependencies
2+
3+
find_package(Threads REQUIRED)
4+
5+
# third-party
6+
7+
# ...
8+
9+
# common
10+
11+
set(TARGET common)
12+
13+
add_library(${TARGET} OBJECT
14+
common.h
15+
common.cpp
16+
)
17+
18+
if (BUILD_SHARED_LIBS)
19+
set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
20+
endif()
21+
22+
target_include_directories(${TARGET} PUBLIC .)
23+
target_compile_features(${TARGET} PUBLIC cxx_std_11)
24+
target_link_libraries(${TARGET} PRIVATE llama)
25+
26+
# examples
27+
28+
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
29+
30+
if (EMSCRIPTEN)
31+
else()
32+
add_subdirectory(main)
33+
add_subdirectory(quantize)
34+
add_subdirectory(perplexity)
35+
add_subdirectory(embedding)
36+
endif()

utils.cpp renamed to examples/common.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "ggml.h"
1+
#include "common.h"
22

3-
#include "utils.h"
3+
#include "ggml.h"
44

55
#include <cassert>
66
#include <cstring>
File renamed without changes.

examples/embedding/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(TARGET embedding)
2+
add_executable(${TARGET} embedding.cpp)
3+
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
4+
target_compile_features(${TARGET} PRIVATE cxx_std_11)

examples/embedding/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# embedding
2+
3+
TODO

examples/embedding/embedding.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include "common.h"
2+
#include "llama.h"
3+
4+
#include <cassert>
5+
#include <cinttypes>
6+
#include <cmath>
7+
#include <cstdio>
8+
#include <cstring>
9+
#include <fstream>
10+
#include <string>
11+
#include <vector>
12+
13+
int main(int argc, char ** argv) {
14+
gpt_params params;
15+
params.model = "models/llama-7B/ggml-model.bin";
16+
17+
if (gpt_params_parse(argc, argv, params) == false) {
18+
return 1;
19+
}
20+
21+
params.embedding = true;
22+
23+
if (params.n_ctx > 2048) {
24+
fprintf(stderr, "%s: warning: model does not support context sizes greater than 2048 tokens (%d specified);"
25+
"expect poor results\n", __func__, params.n_ctx);
26+
}
27+
28+
if (params.seed <= 0) {
29+
params.seed = time(NULL);
30+
}
31+
32+
fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
33+
34+
std::mt19937 rng(params.seed);
35+
if (params.random_prompt) {
36+
params.prompt = gpt_random_prompt(rng);
37+
}
38+
39+
llama_context * ctx;
40+
41+
// load the model
42+
{
43+
auto lparams = llama_context_default_params();
44+
45+
lparams.n_ctx = params.n_ctx;
46+
lparams.n_parts = params.n_parts;
47+
lparams.seed = params.seed;
48+
lparams.f16_kv = params.memory_f16;
49+
lparams.logits_all = params.perplexity;
50+
lparams.use_mlock = params.use_mlock;
51+
lparams.embedding = params.embedding;
52+
53+
ctx = llama_init_from_file(params.model.c_str(), lparams);
54+
55+
if (ctx == NULL) {
56+
fprintf(stderr, "%s: error: failed to load model '%s'\n", __func__, params.model.c_str());
57+
return 1;
58+
}
59+
}
60+
61+
// print system information
62+
{
63+
fprintf(stderr, "\n");
64+
fprintf(stderr, "system_info: n_threads = %d / %d | %s\n",
65+
params.n_threads, std::thread::hardware_concurrency(), llama_print_system_info());
66+
}
67+
68+
int n_past = 0;
69+
70+
// Add a space in front of the first character to match OG llama tokenizer behavior
71+
params.prompt.insert(0, 1, ' ');
72+
73+
// tokenize the prompt
74+
auto embd_inp = ::llama_tokenize(ctx, params.prompt, true);
75+
76+
// determine newline token
77+
auto llama_token_newline = ::llama_tokenize(ctx, "\n", false);
78+
79+
if (params.verbose_prompt) {
80+
fprintf(stderr, "\n");
81+
fprintf(stderr, "%s: prompt: '%s'\n", __func__, params.prompt.c_str());
82+
fprintf(stderr, "%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
83+
for (int i = 0; i < (int) embd_inp.size(); i++) {
84+
fprintf(stderr, "%6d -> '%s'\n", embd_inp[i], llama_token_to_str(ctx, embd_inp[i]));
85+
}
86+
fprintf(stderr, "\n");
87+
}
88+
89+
if (params.embedding){
90+
if (embd_inp.size() > 0) {
91+
if (llama_eval(ctx, embd_inp.data(), embd_inp.size(), n_past, params.n_threads)) {
92+
fprintf(stderr, "%s : failed to eval\n", __func__);
93+
return 1;
94+
}
95+
}
96+
97+
const auto embeddings = llama_get_embeddings(ctx);
98+
99+
// TODO: print / use the embeddings
100+
}
101+
102+
llama_print_timings(ctx);
103+
llama_free(ctx);
104+
105+
return 0;
106+
}

examples/main/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(TARGET main)
2+
add_executable(${TARGET} main.cpp)
3+
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
4+
target_compile_features(${TARGET} PRIVATE cxx_std_11)

examples/main/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# main
2+
3+
TODO

0 commit comments

Comments
 (0)