Skip to content

Commit b936b86

Browse files
fix: requested changes
1 parent 7d9897a commit b936b86

File tree

8 files changed

+46
-39
lines changed

8 files changed

+46
-39
lines changed

packages/react-native-executorch/common/runner/base_llm_runner.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,23 @@ BaseLLMRunner::BaseLLMRunner(std::unique_ptr<Module> module,
2424
}) {}
2525

2626
Error BaseLLMRunner::load() {
27-
if (is_loaded())
27+
if (is_loaded()) {
2828
return Error::Ok;
29+
}
2930

3031
auto status = tokenizer_->load(tokenizer_path_);
3132
if (status != tokenizers::Error::Ok) {
3233
throw rnexecutorch::RnExecutorchError(
3334
rnexecutorch::RnExecutorchErrorCode::TokenizerError,
3435
"Unexpected issue occurred while loading tokenizer (error code: " +
35-
std::to_string(static_cast<int>(status)) + ")");
36+
std::to_string(static_cast<int32_t>(status)) + ")");
3637
}
3738

3839
const auto method_names =
3940
ET_UNWRAP(module_->method_names(), "Failed reading method names");
4041

4142
metadata_[kVocabSize] = tokenizer_->vocab_size();
42-
for (auto &pair : metadata_) {
43-
const auto &method_name = pair.first;
44-
auto &value = pair.second;
43+
for (auto &[method_name, value] : metadata_) {
4544
if (method_names.count(method_name)) {
4645
value = ET_UNWRAP(module_->get(method_name))
4746
.toScalar()
@@ -71,7 +70,10 @@ Error BaseLLMRunner::load() {
7170
}
7271
}
7372
if (eos_ids_->empty()) {
74-
eos_ids_->emplace(7); // fallback <|im_end|>
73+
throw rnexecutorch::RnExecutorchError(
74+
rnexecutorch::RnExecutorchErrorCode::InvalidModelOutput,
75+
"Model did not provide any EOS token IDs via 'get_eos_ids'. "
76+
"The .pte file may be misconfigured.");
7577
}
7678

7779
io_manager_ = std::make_unique<IOManager>(*module_);
@@ -89,8 +91,9 @@ Error BaseLLMRunner::generate(
8991
std::vector<MultimodalInput> inputs = {make_text_input(prompt)};
9092
auto err = generate_internal(inputs, token_callback);
9193

92-
if (stats_callback)
94+
if (stats_callback) {
9395
stats_callback(stats_);
96+
}
9497

9598
return err;
9699
}
@@ -102,8 +105,9 @@ Error BaseLLMRunner::generate(
102105

103106
auto err = generate_internal(inputs, token_callback);
104107

105-
if (stats_callback)
108+
if (stats_callback) {
106109
stats_callback(stats_);
110+
}
107111

108112
return err;
109113
}

packages/react-native-executorch/common/runner/encoders/vision_encoder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bool VisionEncoder::is_loaded() const noexcept {
4040
return module_->is_method_loaded(kVisionEncoderMethod);
4141
}
4242

43-
int32_t VisionEncoder::encoderTokenCount() const noexcept {
43+
int32_t VisionEncoder::encoderTokenCount() const {
4444
if (!is_loaded()) {
4545
return 0;
4646
}

packages/react-native-executorch/common/runner/encoders/vision_encoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class VisionEncoder : public IEncoder {
1818
bool is_loaded() const noexcept override;
1919
::executorch::runtime::Result<::executorch::runtime::EValue>
2020
encode(const MultimodalInput &input) override;
21-
int32_t encoderTokenCount() const noexcept override;
21+
int32_t encoderTokenCount() const override;
2222

2323
private:
2424
struct ImageShape {

packages/react-native-executorch/common/runner/multimodal_input.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ struct ImagePath {
2222

2323
class MultimodalInput {
2424
public:
25-
explicit MultimodalInput(const std::string &text) : data_(text) {}
26-
explicit MultimodalInput(std::string &&text) : data_(std::move(text)) {}
27-
explicit MultimodalInput(const std::vector<uint64_t> &tokens)
28-
: data_(tokens) {}
29-
explicit MultimodalInput(std::vector<uint64_t> &&tokens)
25+
explicit MultimodalInput(std::string text) : data_(std::move(text)) {}
26+
explicit MultimodalInput(std::vector<uint64_t> tokens)
3027
: data_(std::move(tokens)) {}
3128
explicit MultimodalInput(ImagePath image_path)
3229
: data_(std::move(image_path)) {}

packages/react-native-executorch/common/runner/multimodal_prefiller.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "multimodal_prefiller.h"
1313
#include "constants.h"
1414
#include "util.h"
15+
#include <algorithm>
1516

1617
namespace executorch::extension::llm {
1718

@@ -21,11 +22,10 @@ using ::executorch::runtime::EValue;
2122
using ::executorch::runtime::Result;
2223

2324
MultimodalPrefiller::MultimodalPrefiller(
24-
Module *module, MultimodalDecoderRunner *decoder_runner,
25-
tokenizers::HFTokenizer *tokenizer, IOManager *io_manager,
26-
IEncoder *image_encoder)
27-
: module_(module), decoder_runner_(decoder_runner), tokenizer_(tokenizer),
28-
io_manager_(io_manager), image_encoder_(image_encoder) {}
25+
Module &module, MultimodalDecoderRunner &decoder_runner,
26+
tokenizers::HFTokenizer &tokenizer, IEncoder *image_encoder)
27+
: module_(&module), decoder_runner_(&decoder_runner),
28+
tokenizer_(&tokenizer), image_encoder_(image_encoder) {}
2929

3030
Result<uint64_t> MultimodalPrefiller::prefill(const MultimodalInput &input,
3131
int64_t &start_pos) {
@@ -65,7 +65,7 @@ Result<uint64_t> MultimodalPrefiller::prefill(const MultimodalInput &input,
6565
}
6666

6767
padded_tokens_storage.assign(max_seq_len, 0);
68-
std::copy(tokens.begin(), tokens.end(), padded_tokens_storage.begin());
68+
std::ranges::copy(tokens, padded_tokens_storage.begin());
6969

7070
auto text_tensor = ::executorch::extension::from_blob(
7171
padded_tokens_storage.data(), {1, static_cast<SizesType>(max_seq_len)},

packages/react-native-executorch/common/runner/multimodal_prefiller.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ namespace executorch::extension::llm {
2020

2121
class MultimodalPrefiller {
2222
public:
23-
explicit MultimodalPrefiller(Module *module,
24-
MultimodalDecoderRunner *decoder_runner,
25-
tokenizers::HFTokenizer *tokenizer,
26-
IOManager *io_manager,
23+
explicit MultimodalPrefiller(Module &module,
24+
MultimodalDecoderRunner &decoder_runner,
25+
tokenizers::HFTokenizer &tokenizer,
2726
IEncoder *image_encoder = nullptr);
2827

2928
// Prefill one input segment. Updates start_pos in-place.
@@ -38,7 +37,6 @@ class MultimodalPrefiller {
3837
Module *module_;
3938
MultimodalDecoderRunner *decoder_runner_;
4039
tokenizers::HFTokenizer *tokenizer_;
41-
IOManager *io_manager_;
4240
IEncoder *image_encoder_;
4341
};
4442

packages/react-native-executorch/common/runner/multimodal_runner.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ int32_t MultimodalRunner::get_visual_token_count() const {
2626
}
2727

2828
bool MultimodalRunner::is_loaded() const {
29-
if (!mm_prefiller_ || !mm_token_generator_)
29+
if (!mm_prefiller_ || !mm_token_generator_) {
3030
return false;
31-
if (!mm_prefiller_->is_method_loaded() || !mm_token_generator_->is_loaded())
31+
}
32+
if (!mm_prefiller_->is_method_loaded() || !mm_token_generator_->is_loaded()) {
3233
return false;
34+
}
3335
for (const auto &[type, encoder] : encoders_) {
34-
if (!encoder->is_loaded())
36+
if (!encoder->is_loaded()) {
3537
return false;
38+
}
3639
}
3740
return true;
3841
}
@@ -52,8 +55,7 @@ Error MultimodalRunner::load_subcomponents() {
5255
image_encoder = enc_it->second.get();
5356
}
5457
mm_prefiller_ = std::make_unique<MultimodalPrefiller>(
55-
module_.get(), mm_decoder_runner_.get(), tokenizer_.get(),
56-
io_manager_.get(), image_encoder);
58+
*module_, *mm_decoder_runner_, *tokenizer_, image_encoder);
5759
mm_token_generator_ = std::make_unique<TextTokenGenerator>(
5860
tokenizer_.get(), mm_decoder_runner_.get(), /*use_kv_cache=*/true,
5961
std::move(eos_ids_), stats_ptr);
@@ -68,10 +70,12 @@ Error MultimodalRunner::generate_internal(
6870
const std::vector<MultimodalInput> &inputs,
6971
std::function<void(const std::string &)> token_callback) {
7072

71-
if (inputs.empty())
73+
if (inputs.empty()) {
7274
return Error::InvalidArgument;
73-
if (!is_loaded())
75+
}
76+
if (!is_loaded()) {
7477
ET_CHECK_OK_OR_RETURN_ERROR(load());
78+
}
7579

7680
stats_.inference_start_ms = time_in_ms();
7781

@@ -116,18 +120,21 @@ Error MultimodalRunner::generate_internal(
116120
}
117121

118122
void MultimodalRunner::stop_impl() {
119-
if (mm_token_generator_)
123+
if (mm_token_generator_) {
120124
mm_token_generator_->stop();
125+
}
121126
}
122127

123128
void MultimodalRunner::set_count_interval_impl(size_t count_interval) {
124-
if (mm_token_generator_)
129+
if (mm_token_generator_) {
125130
mm_token_generator_->set_count_interval(count_interval);
131+
}
126132
}
127133

128134
void MultimodalRunner::set_time_interval_impl(size_t time_interval) {
129-
if (mm_token_generator_)
135+
if (mm_token_generator_) {
130136
mm_token_generator_->set_time_interval(time_interval);
137+
}
131138
}
132139

133140
} // namespace executorch::extension::llm

packages/react-native-executorch/common/runner/text_runner.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "util.h"
55
#include <cstdint>
66
#include <rnexecutorch/Error.h>
7-
#include <rnexecutorch/Log.h>
87

98
namespace executorch::extension::llm {
109

@@ -140,13 +139,15 @@ void TextRunner::set_topp_impl(float topp) {
140139
}
141140

142141
void TextRunner::set_count_interval_impl(size_t count_interval) {
143-
if (text_token_generator_)
142+
if (text_token_generator_) {
144143
text_token_generator_->set_count_interval(count_interval);
144+
}
145145
}
146146

147147
void TextRunner::set_time_interval_impl(size_t time_interval) {
148-
if (text_token_generator_)
148+
if (text_token_generator_) {
149149
text_token_generator_->set_time_interval(time_interval);
150+
}
150151
}
151152

152153
} // namespace executorch::extension::llm

0 commit comments

Comments
 (0)