16
16
#include < executorch/extension/llm/runner/util.h>
17
17
#include < executorch/extension/runner_util/managed_tensor.h>
18
18
19
- #if ET_USE_TIKTOKEN
20
19
#include < executorch/examples/models/llama2/tokenizer/llama_tiktoken.h>
21
- #else /* BPE */
22
20
#include < executorch/extension/llm/tokenizer/bpe_tokenizer.h>
23
- #endif /* ET_USE_TIKTOKEN*/
24
21
25
22
namespace torch ::executor {
26
23
namespace {
@@ -46,13 +43,6 @@ Runner::Runner(
46
43
: temperature_(temperature),
47
44
module_ (std::make_unique<Module>(model_path, Module::LoadMode::File)),
48
45
tokenizer_path_(tokenizer_path),
49
- tokenizer_(
50
- #if ET_USE_TIKTOKEN
51
- get_tiktoken_for_llama ()
52
- #else
53
- std::make_unique<BPETokenizer>()
54
- #endif
55
- ),
56
46
metadata_({
57
47
{kAppendEosToPrompt , false },
58
48
{kEnableDynamicShape , false },
@@ -79,8 +69,19 @@ Error Runner::load() {
79
69
return Error::Ok;
80
70
}
81
71
ET_CHECK_OK_OR_RETURN_ERROR (module_->load_method (" forward" ));
82
-
83
- tokenizer_->load (tokenizer_path_);
72
+ // load tokenizer
73
+ tokenizer_ = nullptr ;
74
+ tokenizer_ = std::make_unique<BPETokenizer>();
75
+ Error err = tokenizer_->load (tokenizer_path_);
76
+ if (err == Error::InvalidArgument) {
77
+ ET_LOG (
78
+ Info,
79
+ " Failed to load %s as a BPETokenizer artifact, trying Tiktoken" ,
80
+ tokenizer_path_.c_str ());
81
+ tokenizer_.reset ();
82
+ tokenizer_ = get_tiktoken_for_llama ();
83
+ tokenizer_->load (tokenizer_path_);
84
+ }
84
85
85
86
ET_LOG (Info, " Reading metadata from model" );
86
87
0 commit comments