Skip to content

Commit 33d0b3b

Browse files
committed
Not depending on examples/models/llama/runner/ APIs
1 parent 1a1886a commit 33d0b3b

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

examples/demo-apps/apple_ios/LLaMA/LLaMARunner/LLaMARunner/Exported/LLaMARunner.mm

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#import <ExecuTorch/ExecuTorchLog.h>
1212
#import <executorch/examples/models/llama/runner/runner.h>
13+
#import <executorch/examples/models/llama/tokenizer/llama_tiktoken.h>
1314
#import <executorch/examples/models/llava/runner/llava_runner.h>
1415

1516
using executorch::extension::llm::GenerationConfig;
@@ -32,8 +33,17 @@ - (instancetype)initWithModelPath:(NSString*)modelPath
3233
self = [super init];
3334
if (self) {
3435
[ExecuTorchLog.sharedLog addSink:self];
35-
_runner = example::create_llama_runner(
36-
modelPath.UTF8String, tokenizerPath.UTF8String);
36+
// Create and load tokenizer
37+
auto special_tokens = example::get_special_tokens(example::Version::Default);
38+
std::unique_ptr<::tokenizers::Tokenizer> tokenizer =
39+
executorch::extension::llm::load_tokenizer(tokenizerPath.UTF8String, std::move(special_tokens));
40+
41+
if (tokenizer == nullptr) {
42+
_runner = nullptr;
43+
} else {
44+
_runner = executorch::extension::llm::create_text_llm_runner(
45+
modelPath.UTF8String, std::move(tokenizer), std::nullopt);
46+
}
3747
}
3848
return self;
3949
}

examples/models/llama/main.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <gflags/gflags.h>
1111

1212
#include <executorch/examples/models/llama/runner/runner.h>
13+
#include <executorch/examples/models/llama/tokenizer/llama_tiktoken.h>
1314

1415
#if defined(ET_USE_THREADPOOL)
1516
#include <executorch/extension/threadpool/cpuinfo_utils.h>
@@ -91,8 +92,27 @@ int32_t main(int32_t argc, char** argv) {
9192
}
9293
#endif
9394
// create llama runner
94-
std::unique_ptr<::executorch::extension::llm::TextLLMRunner> runner =
95-
example::create_llama_runner(model_path, tokenizer_path, data_path);
95+
ET_LOG(
96+
Info,
97+
"Creating LLaMa runner: model_path=%s, tokenizer_path=%s",
98+
model_path,
99+
tokenizer_path);
100+
101+
// Create and load tokenizer
102+
auto special_tokens = example::get_special_tokens(example::Version::Default);
103+
std::unique_ptr<::tokenizers::Tokenizer> tokenizer =
104+
::executorch::extension::llm::load_tokenizer(tokenizer_path, std::move(special_tokens));
105+
106+
std::unique_ptr<::executorch::extension::llm::TextLLMRunner> runner = nullptr;
107+
if (tokenizer == nullptr) {
108+
ET_LOG(
109+
Info,
110+
"Failed to load %s as a Tiktoken, Sentencepiece or Llama2.c tokenizer, make sure the artifact is one of these types",
111+
tokenizer_path);
112+
} else {
113+
runner = ::executorch::extension::llm::create_text_llm_runner(
114+
model_path, std::move(tokenizer), data_path);
115+
}
96116

97117
if (runner == nullptr) {
98118
ET_LOG(Error, "Failed to create llama runner");

examples/models/llama/runner/runner.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ namespace example {
2626

2727
namespace llm = ::executorch::extension::llm;
2828

29+
[[deprecated("Use load_llama_tokenizer and llm::create_text_llm_runner directly")]]
2930
std::unique_ptr<llm::TextLLMRunner> create_llama_runner(
3031
const std::string& model_path,
3132
const std::string& tokenizer_path,
3233
std::optional<const std::string> data_path = std::nullopt,
3334
float temperature = -1.0f);
3435

36+
[[deprecated("Use get_special_tokens and llm::load_tokenizer directly")]]
3537
std::unique_ptr<tokenizers::Tokenizer> load_llama_tokenizer(
3638
const std::string& tokenizer_path);
3739

extension/android/jni/jni_layer_llama.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <vector>
1515

1616
#include <executorch/examples/models/llama/runner/runner.h>
17+
#include <executorch/examples/models/llama/tokenizer/llama_tiktoken.h>
1718
#include <executorch/examples/models/llava/runner/llava_runner.h>
1819
#include <executorch/extension/llm/runner/image.h>
1920
#include <executorch/extension/llm/runner/irunner.h>
@@ -170,10 +171,17 @@ class ExecuTorchLlmJni : public facebook::jni::HybridClass<ExecuTorchLlmJni> {
170171
: std::nullopt;
171172
// TODO(larryliu0820): Use the API in text_llm_runner.h to create the
172173
// runner.
173-
runner_ = example::create_llama_runner(
174-
model_path->toStdString(),
175-
tokenizer_path->toStdString(),
176-
data_path_str);
174+
// Create and load tokenizer
175+
auto special_tokens = example::get_special_tokens(example::Version::Default);
176+
std::unique_ptr<::tokenizers::Tokenizer> tokenizer =
177+
llm::load_tokenizer(tokenizer_path->toStdString(), std::move(special_tokens));
178+
179+
if (tokenizer == nullptr) {
180+
runner_ = nullptr;
181+
} else {
182+
runner_ = llm::create_text_llm_runner(
183+
model_path->toStdString(), std::move(tokenizer), data_path_str);
184+
}
177185
#if defined(EXECUTORCH_BUILD_MEDIATEK)
178186
} else if (model_type_category == MODEL_TYPE_MEDIATEK_LLAMA) {
179187
runner_ = std::make_unique<MTKLlamaRunner>(

extension/benchmark/apple/Benchmark/Tests/LLaMA/LLaMATests.mm

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "ResourceTestCase.h"
1010

1111
#import <executorch/examples/models/llama/runner/runner.h>
12+
#import <executorch/examples/models/llama/tokenizer/llama_tiktoken.h>
1213

1314
using namespace ::executorch::extension;
1415
using namespace ::executorch::runtime;
@@ -74,8 +75,16 @@ @implementation LLaMATests
7475
NSString *tokenizerPath = resources[@"tokenizer"];
7576
return @{
7677
@"generate" : ^(XCTestCase *testCase){
77-
auto __block runner = example::create_llama_runner(
78-
modelPath.UTF8String, tokenizerPath.UTF8String);
78+
// Create and load tokenizer
79+
auto special_tokens = example::get_special_tokens(example::Version::Default);
80+
std::unique_ptr<::tokenizers::Tokenizer> tokenizer =
81+
::executorch::extension::llm::load_tokenizer(tokenizerPath.UTF8String, std::move(special_tokens));
82+
83+
std::unique_ptr<::executorch::extension::llm::TextLLMRunner> runner = nullptr;
84+
if (tokenizer != nullptr) {
85+
runner = ::executorch::extension::llm::create_text_llm_runner(
86+
modelPath.UTF8String, std::move(tokenizer), std::nullopt);
87+
}
7988
if (!runner) {
8089
XCTFail("Failed to create runner");
8190
return;

0 commit comments

Comments
 (0)