Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions extension/llm/runner/text_llm_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,28 @@ Error TextLLMRunner::generate(
return Error::Ok;
}

Error TextLLMRunner::prefill(
const std::string& prompt,
const GenerationConfig& config) {
if (!is_loaded()) {
ET_CHECK_OK_OR_RETURN_ERROR(load());
}

::tokenizers::Result<std::vector<uint64_t>> encode_res = tokenizer_->encode(
prompt,
/*bos=*/config.num_bos,
/*eos=*/config.num_eos);

ET_CHECK_TK_OK_OR_RETURN_ERROR(
encode_res.error(), "Failed to encode prompt %s", prompt.c_str());

// encode the (string) prompt into tokens sequence
std::vector<uint64_t> prompt_tokens = encode_res.get();
auto prefill_res = text_prefiller_->prefill(prompt_tokens, pos_);
ET_CHECK_OK_OR_RETURN_ERROR(prefill_res.error());
return Error::Ok;
}

Error TextLLMRunner::warmup(const std::string& prompt, int32_t max_new_tokens) {
// Create a GenerationConfig for warmup
GenerationConfig config{
Expand Down
11 changes: 11 additions & 0 deletions extension/llm/runner/text_llm_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ class ET_EXPERIMENTAL TextLLMRunner : public IRunner {
std::function<void(const std::string&)> token_callback = {},
std::function<void(const Stats&)> stats_callback = {}) override;

/**
* Prefill text inputs, for example to reload chat history.
* @param prompt Text prompt to prefill.
* @param config Configuration parameters for text generation (e.g.,
* max_new_tokens, temperature)
* @return The error code. KV cache position is tracked internally in pos_.
*/
::executorch::runtime::Error prefill(
const std::string& prompt,
const GenerationConfig& config);

/**
* @brief Warms up the model with a sample prompt
*
Expand Down
Loading