Skip to content

Commit db0fc2d

Browse files
committed
simple : improve comments + free batch
1 parent b377bf2 commit db0fc2d

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

examples/simple/simple.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ int main(int argc, char ** argv) {
6868

6969
LOG_TEE("\n%s: n_len = %d, n_ctx = %d, n_parallel = %d, n_kv_req = %d\n", __func__, n_len, n_ctx, n_parallel, n_kv_req);
7070

71-
// make sure wi
71+
// make sure the KV cache is big enough to hold all the prompt and generated tokens
7272
if (n_kv_req > n_ctx) {
7373
LOG_TEE("%s: error: n_kv_req > n_ctx, the required KV cache size is not big enough\n", __func__);
7474
LOG_TEE("%s: either reduce n_parallel or increase n_ctx\n", __func__);
7575
return 1;
7676
}
7777

78+
// print the prompt token-by-token
79+
7880
fprintf(stderr, "\n");
7981

8082
for (auto id : tokens_list) {
@@ -107,6 +109,7 @@ int main(int argc, char ** argv) {
107109
}
108110

109111
// assign the system KV cache to all parallel sequences
112+
// this way, the parallel sequences will "reuse" the prompt tokens without having to copy them
110113
for (int32_t i = 1; i < n_parallel; ++i) {
111114
llama_kv_cache_seq_cp(ctx, 0, i, 0, batch.n_tokens);
112115
}
@@ -120,8 +123,8 @@ int main(int argc, char ** argv) {
120123
// we will store the parallel decoded sequences in this vector
121124
std::vector<std::string> streams(n_parallel);
122125

123-
// remember the batch index of the last tokenn for each parallel sequence
124-
// we will use this to know which logits to sample from
126+
// remember the batch index of the last token for each parallel sequence
127+
// we need this to determine which logits to sample from
125128
std::vector<int32_t> i_batch(n_parallel, batch.n_tokens - 1);
126129

127130
int n_cur = batch.n_tokens;
@@ -170,8 +173,7 @@ int main(int argc, char ** argv) {
170173

171174
//const llama_token new_token_id = llama_sample_token_greedy(ctx, &candidates_p);
172175

173-
// is it an end of stream ?
174-
// mark this stream as finished
176+
// is it an end of stream? -> mark the stream as finished
175177
if (new_token_id == llama_token_eos(ctx) || n_cur == n_len) {
176178
i_batch[i] = -1;
177179
LOG_TEE("\n");
@@ -182,8 +184,8 @@ int main(int argc, char ** argv) {
182184
continue;
183185
}
184186

187+
// if there is only one stream, we print immediately to stdout
185188
if (n_parallel == 1) {
186-
// print the new token :
187189
LOG_TEE("%s", llama_token_to_piece(ctx, new_token_id).c_str());
188190
fflush(stdout);
189191
}
@@ -203,8 +205,8 @@ int main(int argc, char ** argv) {
203205
n_decode += 1;
204206
}
205207

208+
// all streams are finished
206209
if (batch.n_tokens == 0) {
207-
// all streams are finished
208210
break;
209211
}
210212

@@ -230,6 +232,8 @@ int main(int argc, char ** argv) {
230232

231233
fprintf(stderr, "\n");
232234

235+
llama_batch_free(batch);
236+
233237
llama_free(ctx);
234238
llama_free_model(model);
235239

0 commit comments

Comments
 (0)