|
| 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 | +// Implementation of MultimodalRunner for multimodal input and text output LLMs |
| 10 | + |
| 11 | +#include <executorch/extension/llm/runner/constants.h> |
| 12 | +#include <executorch/extension/llm/runner/multimodal_runner.h> |
| 13 | +#include <executorch/extension/llm/runner/util.h> |
| 14 | +#include <executorch/runtime/platform/runtime.h> |
| 15 | +#include <pytorch/tokenizers/hf_tokenizer.h> |
| 16 | +#include <pytorch/tokenizers/sentencepiece.h> |
| 17 | + |
| 18 | +namespace executorch::extension::llm { |
| 19 | + |
| 20 | +using ::executorch::extension::Module; |
| 21 | +using ::executorch::runtime::Error; |
| 22 | +using ::executorch::runtime::Result; |
| 23 | + |
| 24 | +MultimodalRunner::MultimodalRunner( |
| 25 | + std::unordered_map<std::string, int64_t> metadata, |
| 26 | + std::unique_ptr<::tokenizers::Tokenizer> tokenizer, |
| 27 | + std::unique_ptr<Module> module, |
| 28 | + std::unique_ptr<MultimodalDecoderRunner> text_decoder_runner, |
| 29 | + std::unique_ptr<MultimodalPrefiller> multimodal_prefiller, |
| 30 | + std::unique_ptr<IOManager> io_manager, |
| 31 | + std::unique_ptr<TextTokenGenerator> text_token_generator, |
| 32 | + std::unique_ptr<Stats> stats) |
| 33 | + : metadata_(std::move(metadata)), |
| 34 | + tokenizer_(std::move(tokenizer)), |
| 35 | + module_(std::move(module)), |
| 36 | + text_decoder_runner_(std::move(text_decoder_runner)), |
| 37 | + multimodal_prefiller_(std::move(multimodal_prefiller)), |
| 38 | + io_manager_(std::move(io_manager)), |
| 39 | + text_token_generator_(std::move(text_token_generator)), |
| 40 | + stats_(std::move(stats)), |
| 41 | + pos_(0) {} |
| 42 | + |
| 43 | +bool MultimodalRunner::is_loaded() { |
| 44 | + return multimodal_prefiller_->is_method_loaded() && |
| 45 | + text_token_generator_->is_loaded(); |
| 46 | +} |
| 47 | + |
| 48 | +Error MultimodalRunner::load() { |
| 49 | + if (is_loaded()) { |
| 50 | + return Error::Ok; |
| 51 | + } |
| 52 | + ET_CHECK_OK_OR_RETURN_ERROR(multimodal_prefiller_->load()); |
| 53 | + ET_CHECK_OK_OR_RETURN_ERROR(text_token_generator_->load()); |
| 54 | + return Error::Ok; |
| 55 | +} |
| 56 | + |
| 57 | +// Don't print with the same priority during warmup |
| 58 | +#define RUNNER_ET_LOG(warmup, format, ...) \ |
| 59 | + if (warmup) { \ |
| 60 | + ET_LOG(Debug, format, __VA_ARGS__); \ |
| 61 | + } else { \ |
| 62 | + ET_LOG(Info, format, __VA_ARGS__); \ |
| 63 | + } |
| 64 | + |
| 65 | +Error MultimodalRunner::generate( |
| 66 | + const std::vector<MultimodalInput>& inputs, |
| 67 | + const GenerationConfig& config, |
| 68 | + std::function<void(const std::string&)>& token_callback, |
| 69 | + std::function<void(const Stats&)>& stats_callback) { |
| 70 | + if (inputs.empty()) { |
| 71 | + ET_LOG(Error, "MultimodalInput vector cannot be empty"); |
| 72 | + return Error::InvalidArgument; |
| 73 | + } |
| 74 | + |
| 75 | + if (!is_loaded()) { |
| 76 | + stats_->model_load_start_ms = time_in_ms(); |
| 77 | + ET_CHECK_OK_OR_RETURN_ERROR(load()); |
| 78 | + stats_->model_load_end_ms = time_in_ms(); |
| 79 | + } |
| 80 | + |
| 81 | + if (config.warming) { |
| 82 | + ET_LOG(Info, "Doing a warmup run..."); |
| 83 | + } |
| 84 | + |
| 85 | + RUNNER_ET_LOG( |
| 86 | + config.warming, |
| 87 | + "RSS after loading model: %f MiB (0 if unsupported)", |
| 88 | + get_rss_bytes() / 1024.0 / 1024.0); |
| 89 | + |
| 90 | + // Wrap the token_callback with print function |
| 91 | + std::function<void(const std::string&)> wrapped_callback = |
| 92 | + [token_callback, config](const std::string& piece) { |
| 93 | + if (!config.warming) { |
| 94 | + safe_printf(piece.c_str()); |
| 95 | + fflush(stdout); |
| 96 | + } |
| 97 | + if (token_callback) { |
| 98 | + token_callback(piece); |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + // Reset internal state and start inference |
| 103 | + stats_->inference_start_ms = time_in_ms(); |
| 104 | + |
| 105 | + uint64_t prefill_next_token = 0; |
| 106 | + // Process multimodal inputs in order |
| 107 | + for (const MultimodalInput& input : inputs) { |
| 108 | + prefill_next_token = ET_UNWRAP(multimodal_prefiller_->prefill(input, pos_)); |
| 109 | + } |
| 110 | + |
| 111 | + stats_->first_token_ms = time_in_ms(); |
| 112 | + stats_->prompt_eval_end_ms = time_in_ms(); |
| 113 | + stats_->num_prompt_tokens = pos_; |
| 114 | + |
| 115 | + wrapped_callback(ET_UNWRAP_TOKENIZER( |
| 116 | + tokenizer_->decode(prefill_next_token, prefill_next_token))); |
| 117 | + |
| 118 | + RUNNER_ET_LOG( |
| 119 | + config.warming, |
| 120 | + "RSS after multimodal input processing: %f MiB (0 if unsupported)", |
| 121 | + get_rss_bytes() / 1024.0 / 1024.0); |
| 122 | + |
| 123 | + // Resolve max_new_tokens based on config |
| 124 | + int64_t max_context_len = |
| 125 | + metadata_.at(kMaxContextLen) - 0; // No start_pos offset |
| 126 | + int32_t max_new_tokens = config.resolve_max_new_tokens(max_context_len, pos_); |
| 127 | + |
| 128 | + ET_LOG( |
| 129 | + Info, |
| 130 | + "Max new tokens resolved: %d, pos_ %" PRId64 ", max_context_len %" PRId64, |
| 131 | + max_new_tokens, |
| 132 | + pos_, |
| 133 | + max_context_len); |
| 134 | + |
| 135 | + ET_CHECK_OR_RETURN_ERROR( |
| 136 | + max_new_tokens > 0, |
| 137 | + InvalidArgument, |
| 138 | + "Max new tokens %d is less than or equal to 0", |
| 139 | + max_new_tokens); |
| 140 | + |
| 141 | + // Generate tokens using the text token generator |
| 142 | + std::vector<uint64_t> prompt_tokens = {prefill_next_token}; |
| 143 | + int64_t num_generated_tokens = ET_UNWRAP(text_token_generator_->generate( |
| 144 | + /*tokens=*/prompt_tokens, |
| 145 | + /*start_pos=*/pos_, |
| 146 | + /*max_new_tokens=*/max_new_tokens - |
| 147 | + 1, // Subtract 1 because prefill already generated 1 token |
| 148 | + /*temperature=*/config.temperature, |
| 149 | + /*token_callback=*/wrapped_callback)); |
| 150 | + |
| 151 | + pos_ += num_generated_tokens; |
| 152 | + // Update stats |
| 153 | + stats_->num_generated_tokens = num_generated_tokens; |
| 154 | + // Finalize stats and call callback |
| 155 | + stats_->inference_end_ms = time_in_ms(); |
| 156 | + if (!config.warming) { |
| 157 | + printf("\n"); |
| 158 | + } |
| 159 | + |
| 160 | + if (config.warming) { |
| 161 | + ET_LOG(Info, "Warmup run finished!"); |
| 162 | + } else { |
| 163 | + // Do not print report during warmup |
| 164 | + print_report(*stats_); |
| 165 | + } |
| 166 | + |
| 167 | + if (stats_callback) { |
| 168 | + stats_callback(*stats_); |
| 169 | + } |
| 170 | + |
| 171 | + return Error::Ok; |
| 172 | +} |
| 173 | + |
| 174 | +} // namespace executorch::extension::llm |
0 commit comments