| 337 | } |
| 338 | |
| 339 | struct llama_batch llama_batch_init(int32_t n_tokens_alloc, int32_t embd, int32_t n_seq_max) { |
| 340 | llama_batch batch = { |
| 341 | /*n_tokens =*/ 0, |
| 342 | /*tokens =*/ nullptr, |
| 343 | /*embd =*/ nullptr, |
| 344 | /*pos =*/ nullptr, |
| 345 | /*n_seq_id =*/ nullptr, |
| 346 | /*seq_id =*/ nullptr, |
| 347 | /*logits =*/ nullptr, |
| 348 | }; |
| 349 | |
| 350 | if (embd) { |
| 351 | batch.embd = (float *) malloc(sizeof(float) * n_tokens_alloc * embd); |
| 352 | } else { |
| 353 | batch.token = (llama_token *) malloc(sizeof(llama_token) * n_tokens_alloc); |
| 354 | } |
| 355 | |
| 356 | batch.pos = (llama_pos *) malloc(sizeof(llama_pos) * n_tokens_alloc); |
| 357 | batch.n_seq_id = (int32_t *) malloc(sizeof(int32_t) * n_tokens_alloc); |
| 358 | batch.seq_id = (llama_seq_id **) malloc(sizeof(llama_seq_id *) * (n_tokens_alloc + 1)); |
| 359 | for (int i = 0; i < n_tokens_alloc; ++i) { |
| 360 | batch.seq_id[i] = (llama_seq_id *) malloc(sizeof(llama_seq_id) * n_seq_max); |
| 361 | } |
| 362 | batch.seq_id[n_tokens_alloc] = nullptr; |
| 363 | |
| 364 | batch.logits = (int8_t *) malloc(sizeof(int8_t) * n_tokens_alloc); |
| 365 | |
| 366 | return batch; |
| 367 | } |
| 368 | |
| 369 | void llama_batch_free(struct llama_batch batch) { |
| 370 | if (batch.token) free(batch.token); |
no outgoing calls