MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / encode

Method encode

smallthinker/src/llama-context.cpp:704–872  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

702}
703
704int llama_context::encode(llama_batch & inp_batch) {
705 if (inp_batch.n_tokens == 0) {
706 LLAMA_LOG_ERROR("%s: n_tokens == 0\n", __func__);
707 return -1;
708 }
709
710 // temporary allocate memory for the input batch if needed
711 // note: during encode, we always pass the full sequence starting from pos = 0
712 llama_batch_allocr batch_allocr(inp_batch, inp_batch.pos ? -1 : 0);
713
714 const llama_batch & batch = batch_allocr.batch;
715 const int32_t n_tokens = batch.n_tokens;
716
717 const auto & hparams = model.hparams;
718
719 GGML_ASSERT((!batch.token && batch.embd) || (batch.token && !batch.embd)); // NOLINT
720
721 // TODO: move the validation to the llama_batch_allocr
722 if (batch.token) {
723 for (int32_t i = 0; i < n_tokens; ++i) {
724 if (batch.token[i] < 0 || (uint32_t) batch.token[i] >= model.vocab.n_tokens()) {
725 LLAMA_LOG_ERROR("%s: invalid token[%d] = %d\n", __func__, i, batch.token[i]);
726 return -1;
727 }
728
729 if (batch.seq_id && (batch.seq_id[i][0] < 0 || batch.seq_id[i][0] >= LLAMA_MAX_PARALLEL_SEQUENCES)) {
730 LLAMA_LOG_ERROR("%s: invalid seq_id[%d] = %d > %d\n", __func__, i, batch.seq_id[i][0], LLAMA_MAX_PARALLEL_SEQUENCES);
731 throw -1;
732 }
733 }
734 }
735
736 // micro-batching is not possible for non-causal encoding, so we process the batch in a single shot
737 GGML_ASSERT(cparams.n_ubatch >= (uint32_t) n_tokens && "encoder requires n_ubatch >= n_tokens");
738
739 if (t_compute_start_us == 0) {
740 t_compute_start_us = ggml_time_us();
741 }
742
743 embd_seq.clear();
744
745 n_queued_tokens += n_tokens;
746
747 const int64_t n_embd = hparams.n_embd;
748
749 llama_sbatch sbatch = llama_sbatch(batch, n_embd, /* simple_split */ true, /* logits_all */ true);
750
751 const llama_ubatch ubatch = sbatch.split_simple(n_tokens);
752
753 // reserve output buffer
754 if (output_reserve(n_tokens) < n_tokens) {
755 LLAMA_LOG_ERROR("%s: could not reserve space for batch with %u outputs\n", __func__, n_tokens);
756 return -2;
757 };
758
759 for (int32_t i = 0; i < n_tokens; ++i) {
760 output_ids[i] = i;
761 }

Callers 15

mainFunction · 0.45
get_vocab_baseMethod · 0.45
get_vocab_base_preMethod · 0.45
_set_vocab_rwkv_worldMethod · 0.45
set_vocabMethod · 0.45
set_vocabMethod · 0.45
set_vocabMethod · 0.45
_xlmroberta_set_vocabMethod · 0.45
set_vocabMethod · 0.45
set_vocabMethod · 0.45

Calls 15

llama_sbatchClass · 0.85
ggml_backend_sched_resetFunction · 0.85
split_simpleMethod · 0.80
get_embd_pooledMethod · 0.80
get_embdMethod · 0.80
findMethod · 0.80
ggml_time_usFunction · 0.50
ggml_nbytesFunction · 0.50
n_tokensMethod · 0.45

Tested by

no test coverage detected