|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +// A simple LLaVA runner that includes preprocessing and post processing logic. |
| 10 | +// The runner takes in a prompt string as well as a list of images as input and |
| 11 | +// emits a string as output. |
| 12 | + |
| 13 | +#include <executorch/examples/models/llava/runner/llava_image_prefiller.h> |
| 14 | +#include <executorch/examples/models/llava/runner/llava_runner.h> |
| 15 | +#include <executorch/examples/models/llava/runner/llava_text_decoder_runner.h> |
| 16 | +#include <executorch/extension/llm/tokenizer/bpe_tokenizer.h> |
| 17 | + |
| 18 | +#include <ctime> |
| 19 | +#include <memory> |
| 20 | +#include <sstream> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +namespace torch::executor { |
| 24 | + |
| 25 | +bool LlavaRunner::is_loaded() { |
| 26 | + Result<std::unordered_set<std::string>> methods_res = module_->method_names(); |
| 27 | + if (methods_res.error() != Error::Ok) { |
| 28 | + ET_LOG(Error, "Failed to get method names"); |
| 29 | + ET_CHECK_MSG(false, "Failed to get method names"); |
| 30 | + } |
| 31 | + std::unordered_set<std::string> methods = methods_res.get(); |
| 32 | + bool methods_exist = methods.find("image_encoder") != methods.end() && |
| 33 | + methods.find("token_embedding") != methods.end() && |
| 34 | + methods.find("text_decoder") != methods.end(); |
| 35 | + if (!methods_exist) { |
| 36 | + for (const auto& method : methods) { |
| 37 | + ET_LOG(Error, "Method: %s", method.c_str()); |
| 38 | + } |
| 39 | + ET_CHECK_MSG( |
| 40 | + methods_exist, |
| 41 | + "Missing required methods (image_encoder, token_embedding, text_decoder) in the model"); |
| 42 | + } |
| 43 | + bool methods_loaded = module_->is_method_loaded("image_encoder") && |
| 44 | + module_->is_method_loaded("token_embedding") && |
| 45 | + module_->is_method_loaded("text_decoder"); |
| 46 | + return methods_loaded && tokenizer_ && text_decoder_runner_ && |
| 47 | + text_prefiller_ && image_prefiller_ && text_token_generator_; |
| 48 | +} |
| 49 | + |
| 50 | +Error LlavaRunner::load() { |
| 51 | + if (is_loaded()) { |
| 52 | + return Error::Ok; |
| 53 | + } |
| 54 | + stats_.model_load_start_ms = util::time_in_ms(); |
| 55 | + |
| 56 | + ET_CHECK_OK_OR_RETURN_ERROR(module_->load_method("image_encoder")); |
| 57 | + ET_CHECK_OK_OR_RETURN_ERROR(module_->load_method("token_embedding")); |
| 58 | + ET_CHECK_OK_OR_RETURN_ERROR(module_->load_method("text_decoder")); |
| 59 | + |
| 60 | + // Load the tokenizer |
| 61 | + tokenizer_ = std::make_unique<BPETokenizer>(); |
| 62 | + tokenizer_->load(tokenizer_path_); |
| 63 | + |
| 64 | + // Load the text decoder runner |
| 65 | + text_decoder_runner_ = std::make_unique<LlavaTextDecoderRunner>( |
| 66 | + module_.get(), tokenizer_->vocab_size(), temperature_); |
| 67 | + |
| 68 | + // Load the text prefiller |
| 69 | + text_prefiller_ = std::make_unique<TextPrefiller>( |
| 70 | + tokenizer_.get(), |
| 71 | + text_decoder_runner_.get(), |
| 72 | + /*use_kv_cache=*/true, |
| 73 | + /*enable_parallel_prefill=*/true); |
| 74 | + |
| 75 | + // Load the image prefiller |
| 76 | + image_prefiller_ = std::make_unique<LlavaImagePrefiller>(module_.get()); |
| 77 | + |
| 78 | + // Load the text token generator |
| 79 | + text_token_generator_ = std::make_unique<TextTokenGenerator>( |
| 80 | + tokenizer_.get(), |
| 81 | + text_decoder_runner_.get(), |
| 82 | + /*use_kv_cache=*/true, |
| 83 | + tokenizer_->eos_tok(), |
| 84 | + &stats_); |
| 85 | + |
| 86 | + stats_.model_load_end_ms = util::time_in_ms(); |
| 87 | + return Error::Ok; |
| 88 | +} |
| 89 | + |
| 90 | +Error LlavaRunner::generate( |
| 91 | + std::vector<Image>& images, |
| 92 | + const std::string& prompt, |
| 93 | + int32_t seq_len, |
| 94 | + std::function<void(const std::string&)> token_callback, |
| 95 | + std::function<void(const Stats&)> stats_callback) { |
| 96 | + ET_CHECK_MSG(!prompt.empty(), "Prompt cannot be null"); |
| 97 | + if (!is_loaded()) { |
| 98 | + ET_CHECK_OK_OR_RETURN_ERROR(load()); |
| 99 | + } |
| 100 | + |
| 101 | + // Wrap the token_callback with print function |
| 102 | + std::function<void(const std::string&)> wrapped_callback = |
| 103 | + [token_callback](const std::string& piece) { |
| 104 | + util::safe_printf(piece.c_str()); |
| 105 | + fflush(stdout); |
| 106 | + if (token_callback) { |
| 107 | + token_callback(piece); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + int64_t pos = 0; |
| 112 | + |
| 113 | + // prefill preset prompt |
| 114 | + std::vector<uint64_t> preset_prompt_tokens = |
| 115 | + ET_UNWRAP(tokenizer_->encode(kPresetPrompt, /*bos=*/1, /*eos=*/0)); |
| 116 | + size_t num_preset_tokens = preset_prompt_tokens.size(); |
| 117 | + |
| 118 | + ET_UNWRAP(text_prefiller_->prefill(preset_prompt_tokens, pos)); |
| 119 | + pos += num_preset_tokens; |
| 120 | + |
| 121 | + // prefill images |
| 122 | + for (auto& image : images) { |
| 123 | + auto logits = ET_UNWRAP(image_prefiller_->prefill(image, pos)); |
| 124 | + pos += logits.size(1); |
| 125 | + } |
| 126 | + |
| 127 | + // prefill user prompt. No BOS because preset prompt already has it. |
| 128 | + std::vector<uint64_t> user_prompt_tokens = |
| 129 | + ET_UNWRAP(tokenizer_->encode(prompt, /*bos=*/0, /*eos=*/0)); |
| 130 | + size_t num_user_tokens = user_prompt_tokens.size(); |
| 131 | + |
| 132 | + uint64_t prefill_next_token = ET_UNWRAP( |
| 133 | + text_prefiller_->prefill(user_prompt_tokens, pos, wrapped_callback)); |
| 134 | + pos += num_user_tokens; |
| 135 | + |
| 136 | + // Generate tokens |
| 137 | + int64_t num_generated_tokens = ET_UNWRAP(text_token_generator_->generate( |
| 138 | + {prefill_next_token}, pos, seq_len, wrapped_callback)); |
| 139 | + |
| 140 | + // Bookkeeping |
| 141 | + stats_.num_prompt_tokens = num_preset_tokens + num_user_tokens; |
| 142 | + stats_.num_generated_tokens = num_generated_tokens; |
| 143 | + ::executorch::llm::print_report(stats_); |
| 144 | + if (stats_callback) { |
| 145 | + stats_callback(stats_); |
| 146 | + } |
| 147 | + |
| 148 | + return Error::Ok; |
| 149 | +} |
| 150 | + |
| 151 | +} // namespace torch::executor |
0 commit comments