Skip to content

Commit c4276b0

Browse files
committed
examples : do not use common library in simple example
1 parent 3dc48fe commit c4276b0

File tree

2 files changed

+55
-60
lines changed

2 files changed

+55
-60
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 common llama ${CMAKE_THREAD_LIBS_INIT})
4+
target_link_libraries(${TARGET} PRIVATE llama ${CMAKE_THREAD_LIBS_INIT})
55
target_compile_features(${TARGET} PRIVATE cxx_std_11)

examples/simple/simple.cpp

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,38 @@
1-
#include "arg.h"
2-
#include "common.h"
3-
#include "log.h"
41
#include "llama.h"
5-
2+
#include <cstdio>
3+
#include <string>
64
#include <vector>
75

86
static void print_usage(int, char ** argv) {
9-
LOG("\nexample usage:\n");
10-
LOG("\n %s -m model.gguf -p \"Hello my name is\" -n 32\n", argv[0]);
11-
LOG("\n");
7+
printf("\nexample usage:\n");
8+
printf("\n %s <model.gguf> [prompt]\n", argv[0]);
9+
printf("\n");
1210
}
1311

1412
int main(int argc, char ** argv) {
15-
gpt_params params;
16-
17-
params.prompt = "Hello my name is";
18-
params.n_predict = 32;
13+
std::string model_path;
14+
std::string prompt = "Hello my name is";
15+
int n_predict = 32;
1916

20-
if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON, print_usage)) {
17+
if (argc < 2) {
18+
print_usage(argc, argv);
2119
return 1;
2220
}
21+
model_path = argv[1];
2322

24-
gpt_init();
25-
26-
// total length of the sequence including the prompt
27-
const int n_predict = params.n_predict;
28-
29-
// init LLM
30-
31-
llama_backend_init();
32-
llama_numa_init(params.numa);
23+
if (argc > 2) {
24+
prompt = argv[2];
25+
for (int i = 3; i < argc; i++) {
26+
prompt += " ";
27+
prompt += argv[i];
28+
}
29+
}
3330

3431
// initialize the model
3532

36-
llama_model_params model_params = llama_model_params_from_gpt_params(params);
37-
38-
llama_model * model = llama_load_model_from_file(params.model.c_str(), model_params);
33+
llama_model_params model_params = llama_model_default_params();
34+
model_params.n_gpu_layers = 99; // offload all layers to GPU
35+
llama_model * model = llama_load_model_from_file(model_path.c_str(), model_params);
3936

4037
if (model == NULL) {
4138
fprintf(stderr , "%s: error: unable to load model\n" , __func__);
@@ -44,8 +41,9 @@ int main(int argc, char ** argv) {
4441

4542
// initialize the context
4643

47-
llama_context_params ctx_params = llama_context_params_from_gpt_params(params);
48-
44+
llama_context_params ctx_params = llama_context_default_params();
45+
ctx_params.n_ctx = 512; // maximum context size
46+
ctx_params.no_perf = false;
4947
llama_context * ctx = llama_new_context_with_model(model, ctx_params);
5048

5149
if (ctx == NULL) {
@@ -54,54 +52,54 @@ int main(int argc, char ** argv) {
5452
}
5553

5654
auto sparams = llama_sampler_chain_default_params();
57-
5855
sparams.no_perf = false;
59-
6056
llama_sampler * smpl = llama_sampler_chain_init(sparams);
6157

6258
llama_sampler_chain_add(smpl, llama_sampler_init_greedy());
6359

6460
// tokenize the prompt
6561

6662
std::vector<llama_token> tokens_list;
67-
tokens_list = ::llama_tokenize(ctx, params.prompt, true);
63+
int n_tokens = llama_tokenize(model, prompt.c_str(), prompt.size(), NULL, 0, true, true);
64+
tokens_list.resize(-n_tokens);
65+
if (llama_tokenize(model, prompt.c_str(), prompt.size(), tokens_list.data(), tokens_list.size(), true, true) <= 0) {
66+
fprintf(stderr, "%s: error: failed to tokenize the prompt\n", __func__);
67+
return 1;
68+
}
6869

6970
const int n_ctx = llama_n_ctx(ctx);
7071
const int n_kv_req = tokens_list.size() + (n_predict - tokens_list.size());
7172

72-
LOG("\n");
73-
LOG_INF("%s: n_predict = %d, n_ctx = %d, n_kv_req = %d\n", __func__, n_predict, n_ctx, n_kv_req);
73+
fprintf(stderr, "%s: n_predict = %d, n_ctx = %d, n_kv_req = %d\n", __func__, n_predict, n_ctx, n_kv_req);
74+
7475

7576
// make sure the KV cache is big enough to hold all the prompt and generated tokens
7677
if (n_kv_req > n_ctx) {
77-
LOG_ERR("%s: error: n_kv_req > n_ctx, the required KV cache size is not big enough\n", __func__);
78-
LOG_ERR("%s: either reduce n_predict or increase n_ctx\n", __func__);
78+
fprintf(stderr, "%s: error: n_kv_req > n_ctx, the required KV cache size is not big enough\n", __func__);
79+
fprintf(stderr, "%s: either reduce n_predict or increase n_ctx\n", __func__);
7980
return 1;
8081
}
8182

8283
// print the prompt token-by-token
8384

84-
LOG("\n");
85+
fprintf(stderr, "\n");
8586

8687
for (auto id : tokens_list) {
87-
LOG("%s", llama_token_to_piece(ctx, id).c_str());
88+
char buf[128];
89+
int n = llama_token_to_piece(model, id, buf, sizeof(buf), 0, true);
90+
std::string s(buf, n);
91+
fprintf(stderr, "%s", s.c_str());
8892
}
8993

9094
// create a llama_batch with size 512
9195
// we use this object to submit token data for decoding
9296

93-
llama_batch batch = llama_batch_init(512, 0, 1);
97+
llama_batch batch = llama_batch_get_one(tokens_list.data(), tokens_list.size(), 0, 0);
9498

9599
// evaluate the initial prompt
96-
for (size_t i = 0; i < tokens_list.size(); i++) {
97-
llama_batch_add(batch, tokens_list[i], i, { 0 }, false);
98-
}
99-
100-
// llama_decode will output logits only for the last token of the prompt
101-
batch.logits[batch.n_tokens - 1] = true;
102100

103101
if (llama_decode(ctx, batch) != 0) {
104-
LOG("%s: llama_decode() failed\n", __func__);
102+
fprintf(stderr, "%s: llama_decode() failed\n", __func__);
105103
return 1;
106104
}
107105

@@ -114,24 +112,25 @@ int main(int argc, char ** argv) {
114112

115113
while (n_cur <= n_predict) {
116114
// sample the next token
115+
llama_token new_token_id = llama_sampler_sample(smpl, ctx, -1);
117116
{
118-
const llama_token new_token_id = llama_sampler_sample(smpl, ctx, -1);
119117

120118
// is it an end of generation?
121119
if (llama_token_is_eog(model, new_token_id) || n_cur == n_predict) {
122-
LOG("\n");
120+
fprintf(stderr, "\n");
123121

124122
break;
125123
}
126124

127-
LOG("%s", llama_token_to_piece(ctx, new_token_id).c_str());
125+
char buf[128];
126+
int n = llama_token_to_piece(model, new_token_id, buf, sizeof(buf), 0, true);
127+
std::string s(buf, n);
128+
fprintf(stderr, "%s", s.c_str());
128129
fflush(stdout);
129130

130131
// prepare the next batch
131-
llama_batch_clear(batch);
132-
133-
// push this new token for next evaluation
134-
llama_batch_add(batch, new_token_id, n_cur, { 0 }, true);
132+
//llama_batch_add(batch, new_token_id, n_cur, { 0 }, true);
133+
batch = llama_batch_get_one(&new_token_id, 1, n_cur, 0);
135134

136135
n_decode += 1;
137136
}
@@ -140,30 +139,26 @@ int main(int argc, char ** argv) {
140139

141140
// evaluate the current batch with the transformer model
142141
if (llama_decode(ctx, batch)) {
143-
LOG_ERR("%s : failed to eval, return code %d\n", __func__, 1);
142+
fprintf(stderr, "%s : failed to eval, return code %d\n", __func__, 1);
144143
return 1;
145144
}
146145
}
147146

148-
LOG("\n");
147+
fprintf(stderr, "\n");
149148

150149
const auto t_main_end = ggml_time_us();
151150

152-
LOG_INF("%s: decoded %d tokens in %.2f s, speed: %.2f t/s\n",
151+
fprintf(stderr, "%s: decoded %d tokens in %.2f s, speed: %.2f t/s\n",
153152
__func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f));
154153

155-
LOG("\n");
154+
fprintf(stderr, "\n");
156155
llama_perf_sampler_print(smpl);
157156
llama_perf_context_print(ctx);
157+
fprintf(stderr, "\n");
158158

159-
LOG("\n");
160-
161-
llama_batch_free(batch);
162159
llama_sampler_free(smpl);
163160
llama_free(ctx);
164161
llama_free_model(model);
165162

166-
llama_backend_free();
167-
168163
return 0;
169164
}

0 commit comments

Comments
 (0)