llama
250 строк · 7.8 Кб
1#include "arg.h"
2#include "common.h"
3#include "llama.h"
4
5#include <vector>
6#include <cstdio>
7
8int main(int argc, char ** argv) {
9gpt_params params;
10
11params.prompt = "The quick brown fox";
12params.sparams.seed = 1234;
13
14if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) {
15return 1;
16}
17
18print_build_info();
19
20if (params.n_predict < 0) {
21params.n_predict = 16;
22}
23
24auto n_past = 0;
25
26std::string result0;
27std::string result1;
28std::string result2;
29
30// init
31llama_init_result llama_init = llama_init_from_gpt_params(params);
32
33llama_model * model = llama_init.model;
34llama_context * ctx = llama_init.context;
35
36if (model == nullptr || ctx == nullptr) {
37fprintf(stderr, "%s : failed to init\n", __func__);
38return 1;
39}
40
41auto sparams = llama_sampler_chain_default_params();
42
43llama_sampler * smpl = llama_sampler_chain_init(sparams);
44
45llama_sampler_chain_add(smpl, llama_sampler_init_softmax());
46llama_sampler_chain_add(smpl, llama_sampler_init_dist(params.sparams.seed));
47
48// tokenize prompt
49auto tokens = llama_tokenize(ctx, params.prompt, true);
50
51// evaluate prompt
52llama_decode(ctx, llama_batch_get_one(tokens.data(), tokens.size(), n_past, 0));
53n_past += tokens.size();
54
55// save state (rng, logits, embedding and kv_cache) to file
56{
57std::vector<uint8_t> state_mem(llama_state_get_size(ctx));
58const size_t written = llama_state_get_data(ctx, state_mem.data(), state_mem.size());
59
60FILE *fp_write = fopen("dump_state.bin", "wb");
61fwrite(state_mem.data(), 1, written, fp_write);
62fclose(fp_write);
63
64fprintf(stderr, "%s : serialized state into %zd out of a maximum of %zd bytes\n", __func__, written, state_mem.size());
65}
66
67// save state (last tokens)
68const auto n_past_saved = n_past;
69
70// first run
71printf("\nfirst run: %s", params.prompt.c_str());
72
73for (auto i = 0; i < params.n_predict; i++) {
74auto next_token = llama_sampler_sample(smpl, ctx, -1);
75auto next_token_str = llama_token_to_piece(ctx, next_token);
76
77printf("%s", next_token_str.c_str());
78result0 += next_token_str;
79
80if (llama_decode(ctx, llama_batch_get_one(&next_token, 1, n_past, 0))) {
81fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
82llama_free(ctx);
83llama_free_model(model);
84return 1;
85}
86n_past += 1;
87}
88
89printf("\n\n");
90
91// free old context
92llama_free(ctx);
93
94// make new context
95auto * ctx2 = llama_new_context_with_model(model, llama_context_params_from_gpt_params(params));
96
97llama_sampler * smpl2 = llama_sampler_chain_init(sparams);
98
99llama_sampler_chain_add(smpl2, llama_sampler_init_softmax());
100llama_sampler_chain_add(smpl2, llama_sampler_init_dist(params.sparams.seed));
101
102printf("\nsecond run: %s", params.prompt.c_str());
103
104// load state (rng, logits, embedding and kv_cache) from file
105{
106std::vector<uint8_t> state_mem;
107
108FILE * fp_read = fopen("dump_state.bin", "rb");
109fseek(fp_read, 0, SEEK_END);
110state_mem.resize(ftell(fp_read));
111fseek(fp_read, 0, SEEK_SET);
112const size_t read = fread(state_mem.data(), 1, state_mem.size(), fp_read);
113fclose(fp_read);
114
115if (read != llama_state_set_data(ctx2, state_mem.data(), state_mem.size())) {
116fprintf(stderr, "\n%s : failed to read state\n", __func__);
117llama_free(ctx2);
118llama_free_model(model);
119return 1;
120}
121
122fprintf(stderr, "%s : deserialized state from %zd out of a maximum of %zd bytes\n", __func__, read, state_mem.size());
123}
124
125// restore state (last tokens)
126n_past = n_past_saved;
127
128// second run
129for (auto i = 0; i < params.n_predict; i++) {
130auto next_token = llama_sampler_sample(smpl2, ctx2, -1);
131auto next_token_str = llama_token_to_piece(ctx2, next_token);
132
133printf("%s", next_token_str.c_str());
134result1 += next_token_str;
135
136if (llama_decode(ctx2, llama_batch_get_one(&next_token, 1, n_past, 0))) {
137fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
138llama_free(ctx2);
139llama_free_model(model);
140return 1;
141}
142n_past += 1;
143}
144
145printf("\n\n");
146
147llama_free(ctx2);
148
149if (result0 != result1) {
150fprintf(stderr, "\n%s : error : the 2 generations are different\n", __func__);
151return 1;
152}
153
154// make new context
155auto * ctx3 = llama_new_context_with_model(model, llama_context_params_from_gpt_params(params));
156
157llama_sampler * smpl3 = llama_sampler_chain_init(sparams);
158
159llama_sampler_chain_add(smpl3, llama_sampler_init_softmax());
160llama_sampler_chain_add(smpl3, llama_sampler_init_dist(params.sparams.seed));
161
162printf("\nsingle seq run: %s", params.prompt.c_str());
163
164// load state (rng, logits, embedding and kv_cache) from file
165{
166std::vector<uint8_t> state_mem;
167
168FILE * fp_read = fopen("dump_state.bin", "rb");
169fseek(fp_read, 0, SEEK_END);
170state_mem.resize(ftell(fp_read));
171fseek(fp_read, 0, SEEK_SET);
172const size_t read = fread(state_mem.data(), 1, state_mem.size(), fp_read);
173fclose(fp_read);
174
175if (read != llama_state_set_data(ctx3, state_mem.data(), state_mem.size())) {
176fprintf(stderr, "\n%s : failed to read state\n", __func__);
177llama_free(ctx3);
178llama_free_model(model);
179return 1;
180}
181
182fprintf(stderr, "%s : deserialized state from %zd out of a maximum of %zd bytes\n", __func__, read, state_mem.size());
183}
184
185// restore state (last tokens)
186n_past = n_past_saved;
187
188// save seq 0 and load into seq 1
189{
190// save kv of seq 0
191std::vector<uint8_t> seq_store(llama_state_seq_get_size(ctx3, 0));
192const size_t ncopy = llama_state_seq_get_data(ctx3, seq_store.data(), seq_store.size(), 0);
193if (ncopy != seq_store.size()) {
194fprintf(stderr, "\n%s : seq copy data length %zd does not match expected length %zd\n", __func__, ncopy, seq_store.size());
195llama_free(ctx3);
196llama_free_model(model);
197return 1;
198}
199fprintf(stderr, "%s : seq 0 copied, %zd bytes\n", __func__, ncopy);
200
201// erase whole kv
202llama_kv_cache_clear(ctx3);
203fprintf(stderr, "%s : kv cache cleared\n", __func__);
204
205// restore kv into seq 1
206const size_t nset = llama_state_seq_set_data(ctx3, seq_store.data(), seq_store.size(), 1);
207if (nset != seq_store.size()) {
208fprintf(stderr, "\n%s : seq set data length %zd does not match expected length %zd\n", __func__, nset, seq_store.size());
209llama_free(ctx3);
210llama_free_model(model);
211return 1;
212}
213fprintf(stderr, "%s : seq 1 restored, %zd bytes\n", __func__, nset);
214}
215
216// third run with seq 1 instead of 0
217for (auto i = 0; i < params.n_predict; i++) {
218auto next_token = llama_sampler_sample(smpl3, ctx3, -1);
219auto next_token_str = llama_token_to_piece(ctx3, next_token);
220
221printf("%s", next_token_str.c_str());
222result2 += next_token_str;
223
224if (llama_decode(ctx3, llama_batch_get_one(&next_token, 1, n_past, 1))) {
225fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
226llama_free(ctx3);
227llama_free_model(model);
228return 1;
229}
230n_past += 1;
231}
232
233printf("\n");
234
235llama_sampler_free(smpl);
236llama_sampler_free(smpl2);
237llama_sampler_free(smpl3);
238
239llama_free(ctx3);
240llama_free_model(model);
241
242if (result0 != result2) {
243fprintf(stderr, "\n%s : error : the seq restore generation is different\n", __func__);
244return 1;
245}
246
247fprintf(stderr, "\n%s : success\n", __func__);
248
249return 0;
250}
251