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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.facebook.jni.HybridData;
import com.facebook.jni.annotations.DoNotStrip;
import java.io.File;
import java.util.List;
import org.pytorch.executorch.ExecuTorchRuntime;
import org.pytorch.executorch.annotations.Experimental;

Expand All @@ -32,14 +33,22 @@ public class LlmModule {

@DoNotStrip
private static native HybridData initHybrid(
int modelType, String modulePath, String tokenizerPath, float temperature, String dataPath);
int modelType,
String modulePath,
String tokenizerPath,
float temperature,
List<String> dataFiles);

/**
* Constructs a LLM Module for a model with given type, model path, tokenizer, temperature, and
* data path.
* dataFiles.
*/
public LlmModule(
int modelType, String modulePath, String tokenizerPath, float temperature, String dataPath) {
int modelType,
String modulePath,
String tokenizerPath,
float temperature,
List<String> dataFiles) {
ExecuTorchRuntime runtime = ExecuTorchRuntime.getRuntime();

File modelFile = new File(modulePath);
Expand All @@ -50,25 +59,35 @@ public LlmModule(
if (!tokenizerFile.canRead() || !tokenizerFile.isFile()) {
throw new RuntimeException("Cannot load tokenizer path " + tokenizerPath);
}
mHybridData = initHybrid(modelType, modulePath, tokenizerPath, temperature, dataPath);

mHybridData = initHybrid(modelType, modulePath, tokenizerPath, temperature, dataFiles);
}

/**
* Constructs a LLM Module for a model with given type, model path, tokenizer, temperature, and
* data path.
*/
public LlmModule(
int modelType, String modulePath, String tokenizerPath, float temperature, String dataPath) {
this(modelType, modulePath, tokenizerPath, temperature, List.of(dataPath));
}

/** Constructs a LLM Module for a model with given model path, tokenizer, temperature. */
public LlmModule(String modulePath, String tokenizerPath, float temperature) {
this(MODEL_TYPE_TEXT, modulePath, tokenizerPath, temperature, null);
this(MODEL_TYPE_TEXT, modulePath, tokenizerPath, temperature, List.of());
}

/**
* Constructs a LLM Module for a model with given model path, tokenizer, temperature and data
* path.
*/
public LlmModule(String modulePath, String tokenizerPath, float temperature, String dataPath) {
this(MODEL_TYPE_TEXT, modulePath, tokenizerPath, temperature, dataPath);
this(MODEL_TYPE_TEXT, modulePath, tokenizerPath, temperature, List.of(dataPath));
}

/** Constructs a LLM Module for a model with given path, tokenizer, and temperature. */
public LlmModule(int modelType, String modulePath, String tokenizerPath, float temperature) {
this(modelType, modulePath, tokenizerPath, temperature, null);
this(modelType, modulePath, tokenizerPath, temperature, List.of());
}

/** Constructs a LLM Module for a model with the given LlmModuleConfig */
Expand Down
29 changes: 21 additions & 8 deletions extension/android/jni/jni_layer_llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,21 @@ class ExecuTorchLlmJni : public facebook::jni::HybridClass<ExecuTorchLlmJni> {
facebook::jni::alias_ref<jstring> model_path,
facebook::jni::alias_ref<jstring> tokenizer_path,
jfloat temperature,
facebook::jni::alias_ref<jstring> data_path) {
facebook::jni::alias_ref<jobject> data_files) {
return makeCxxInstance(
model_type_category,
model_path,
tokenizer_path,
temperature,
data_path);
data_files);
}

ExecuTorchLlmJni(
jint model_type_category,
facebook::jni::alias_ref<jstring> model_path,
facebook::jni::alias_ref<jstring> tokenizer_path,
jfloat temperature,
facebook::jni::alias_ref<jstring> data_path = nullptr) {
facebook::jni::alias_ref<jobject> data_files = nullptr) {
temperature_ = temperature;
#if defined(ET_USE_THREADPOOL)
// Reserve 1 thread for the main thread.
Expand All @@ -173,26 +173,39 @@ class ExecuTorchLlmJni : public facebook::jni::HybridClass<ExecuTorchLlmJni> {
model_path->toStdString().c_str(),
llm::load_tokenizer(tokenizer_path->toStdString()));
} else if (model_type_category == MODEL_TYPE_CATEGORY_LLM) {
std::optional<const std::string> data_path_str = data_path
? std::optional<const std::string>{data_path->toStdString()}
: std::nullopt;
std::vector<std::string> data_files_vector;
if (data_files != nullptr) {
// Convert Java List<String> to C++ std::vector<string>
auto list_class = facebook::jni::findClassStatic("java/util/List");
auto size_method = list_class->getMethod<jint()>("size");
auto get_method =
list_class->getMethod<facebook::jni::local_ref<jobject>(jint)>(
"get");

jint size = size_method(data_files);
for (jint i = 0; i < size; ++i) {
auto str_obj = get_method(data_files, i);
auto jstr = facebook::jni::static_ref_cast<jstring>(str_obj);
data_files_vector.push_back(jstr->toStdString());
}
}
runner_ = executorch::extension::llm::create_text_llm_runner(
model_path->toStdString(),
llm::load_tokenizer(tokenizer_path->toStdString()),
data_path_str);
data_files_vector);
#if defined(EXECUTORCH_BUILD_QNN)
} else if (model_type_category == MODEL_TYPE_QNN_LLAMA) {
std::unique_ptr<executorch::extension::Module> module = std::make_unique<
executorch::extension::Module>(
model_path->toStdString().c_str(),
data_files_set,
executorch::extension::Module::LoadMode::MmapUseMlockIgnoreErrors);
std::string decoder_model = "llama3"; // use llama3 for now
runner_ = std::make_unique<example::Runner<uint16_t>>( // QNN runner
std::move(module),
decoder_model.c_str(),
model_path->toStdString().c_str(),
tokenizer_path->toStdString().c_str(),
data_path->toStdString().c_str(),
"");
model_type_category_ = MODEL_TYPE_CATEGORY_LLM;
#endif
Expand Down
Loading