Skip to content
Merged
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
17 changes: 16 additions & 1 deletion examples/models/llama/runner/runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
const std::string& tokenizer_path,
std::optional<const std::string> data_path,
float temperature) {
if (data_path.has_value()) {
std::vector<std::string> data_files;
data_files.push_back(data_path.value());
return create_llama_runner(
model_path, tokenizer_path, std::move(data_files), temperature);
}
return create_llama_runner(
model_path, tokenizer_path, std::vector<std::string>(), temperature);
}

std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
const std::string& model_path,
const std::string& tokenizer_path,
std::vector<std::string> data_files,
float temperature) {
ET_LOG(
Info,
"Creating LLaMa runner: model_path=%s, tokenizer_path=%s",
Expand All @@ -55,7 +70,7 @@ std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
return nullptr;
}
return llm::create_text_llm_runner(
model_path, std::move(tokenizer), data_path);
model_path, std::move(tokenizer), data_files);
}

} // namespace example
11 changes: 7 additions & 4 deletions examples/models/llama/runner/runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@

#pragma once

#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>

#include <executorch/examples/models/llama/tokenizer/llama_tiktoken.h>
#include <executorch/extension/llm/runner/irunner.h>
Expand All @@ -30,7 +27,13 @@ namespace llm = ::executorch::extension::llm;
std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
const std::string& model_path,
const std::string& tokenizer_path,
std::optional<const std::string> data_path = std::nullopt,
std::optional<const std::string> data_path,
float temperature = -1.0f);

std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
const std::string& model_path,
const std::string& tokenizer_path,
std::vector<std::string> data_files = {},
float temperature = -1.0f);

std::unique_ptr<tokenizers::Tokenizer> load_llama_tokenizer(
Expand Down
22 changes: 20 additions & 2 deletions extension/llm/runner/llm_runner_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ std::unique_ptr<TextLLMRunner> create_text_llm_runner(
std::unique_ptr<::tokenizers::Tokenizer> tokenizer,
std::optional<const std::string> data_path,
float temperature) {
if (data_path.has_value()) {
std::vector<std::string> data_files;
data_files.push_back(data_path.value());
return create_text_llm_runner(
model_path, std::move(tokenizer), std::move(data_files), temperature);
}
return create_text_llm_runner(
model_path,
std::move(tokenizer),
std::vector<std::string>(),
temperature);
}

std::unique_ptr<TextLLMRunner> create_text_llm_runner(
const std::string& model_path,
std::unique_ptr<::tokenizers::Tokenizer> tokenizer,
std::vector<std::string> data_files,
float temperature) {
// Sanity check tokenizer
if (!tokenizer || !tokenizer->is_loaded()) {
ET_LOG(Error, "Tokenizer is null or not loaded");
Expand All @@ -191,9 +209,9 @@ std::unique_ptr<TextLLMRunner> create_text_llm_runner(

// Create the Module
std::unique_ptr<Module> module;
if (data_path.has_value()) {
if (data_files.size() > 0) {
module = std::make_unique<Module>(
model_path, data_path.value(), Module::LoadMode::File);
model_path, data_files, Module::LoadMode::File);
} else {
module = std::make_unique<Module>(model_path, Module::LoadMode::File);
}
Expand Down
23 changes: 22 additions & 1 deletion extension/llm/runner/llm_runner_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,28 @@ ET_EXPERIMENTAL std::unordered_set<uint64_t> get_eos_ids(
ET_EXPERIMENTAL std::unique_ptr<TextLLMRunner> create_text_llm_runner(
const std::string& model_path,
std::unique_ptr<::tokenizers::Tokenizer> tokenizer,
std::optional<const std::string> data_path = std::nullopt,
std::optional<const std::string> data_path,
float temperature = -1.0f);

/**
* @brief Creates a TextLLMRunner instance with dependency injection
*
* This factory function creates and initializes a TextLLMRunner with all
* necessary components for text generation using the specified model and
* tokenizer.
*
* @param model_path Path to the model file
* @param tokenizer Initialized tokenizer instance
* @param data_files Vector of paths to additional data required by the model
* @param temperature Optional temperature parameter for controlling randomness
* (deprecated)
* @return std::unique_ptr<TextLLMRunner> Initialized TextLLMRunner instance, or
* nullptr on failure
*/
ET_EXPERIMENTAL std::unique_ptr<TextLLMRunner> create_text_llm_runner(
const std::string& model_path,
std::unique_ptr<::tokenizers::Tokenizer> tokenizer,
std::vector<std::string> data_files = {},
float temperature = -1.0f);

/**
Expand Down
Loading