| 1467 | } |
| 1468 | |
| 1469 | int llama_context::decode(const llama_batch & batch_inp) { |
| 1470 | GGML_ASSERT((!batch_inp.token && batch_inp.embd) || (batch_inp.token && !batch_inp.embd)); // NOLINT |
| 1471 | |
| 1472 | if (!memory) { |
| 1473 | LLAMA_LOG_DEBUG("%s: cannot decode batches with this context (calling encode() instead)\n", __func__); |
| 1474 | return encode(batch_inp); |
| 1475 | } |
| 1476 | |
| 1477 | if (batch_inp.n_tokens == 0) { |
| 1478 | LLAMA_LOG_ERROR("%s: n_tokens == 0\n", __func__); |
| 1479 | return -1; |
| 1480 | } |
| 1481 | |
| 1482 | const auto & vocab = model.vocab; |
| 1483 | const auto & hparams = model.hparams; |
| 1484 | |
| 1485 | const int64_t n_vocab = vocab.n_tokens(); |
| 1486 | const int64_t n_embd = hparams.n_embd_inp(); |
| 1487 | |
| 1488 | // when computing embeddings, all tokens are output |
| 1489 | const bool output_all = cparams.embeddings; |
| 1490 | const bool has_samplers = !sampling.samplers.empty(); |
| 1491 | |
| 1492 | const uint32_t n_seq_max = cparams.kv_unified ? LLAMA_MAX_SEQ : cparams.n_seq_max; |
| 1493 | |
| 1494 | // TODO: avoid this workaround in the future |
| 1495 | if (has_samplers && batch_inp.logits) { |
| 1496 | std::vector<int32_t> seq_output_count(n_seq_max, 0); |
| 1497 | |
| 1498 | for (int32_t i = 0; i < batch_inp.n_tokens; ++i) { |
| 1499 | if (batch_inp.logits[i] == 0) { |
| 1500 | continue; |
| 1501 | } |
| 1502 | |
| 1503 | const int ns = batch_inp.n_seq_id ? batch_inp.n_seq_id[i] : 1; |
| 1504 | |
| 1505 | for (int32_t s = 0; s < ns; ++s) { |
| 1506 | const llama_seq_id seq_id = batch_inp.seq_id ? batch_inp.seq_id[i][s] : 0; |
| 1507 | |
| 1508 | seq_output_count[seq_id]++; |
| 1509 | if (seq_output_count[seq_id] > 1) { |
| 1510 | LLAMA_LOG_ERROR("%s: backend sampling requires at most one output token per sequence (seq_id %d had %d)\n", |
| 1511 | __func__, seq_id, seq_output_count[seq_id]); |
| 1512 | return -1; |
| 1513 | } |
| 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | if (!balloc->init(batch_inp, vocab, memory.get(), n_embd, n_seq_max, output_all)) { |
| 1519 | LLAMA_LOG_ERROR("%s: failed to initialize batch\n", __func__); |
| 1520 | return -1; |
| 1521 | } |
| 1522 | |
| 1523 | const uint32_t n_tokens_all = balloc->get_n_tokens(); |
| 1524 | const uint32_t n_outputs_all = balloc->get_n_outputs(); |
| 1525 | |
| 1526 | if (output_all) { |
no test coverage detected