|
| 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 | +#pragma once |
| 10 | + |
| 11 | +#include <executorch/extension/llm/runner/io_manager/io_manager.h> |
| 12 | +#include <executorch/extension/tensor/tensor.h> |
| 13 | + |
| 14 | +#include <map> |
| 15 | + |
| 16 | +namespace executorch { |
| 17 | +namespace extension { |
| 18 | +namespace llm { |
| 19 | + |
| 20 | +/** |
| 21 | + * @brief Drives Hugging Face's static-cache ABI ("hf_static"). |
| 22 | + * |
| 23 | + * Each method is HF's decode graph at a static query length N. Inputs: |
| 24 | + * input_ids [1, N], attention_mask(s) [1, 1, N, L] (bool), position_ids |
| 25 | + * [1, N], then per layer keys and values [1, H, L, D] and cumulative_length. |
| 26 | + * Outputs: the caches and counters updated in place, logits [1, N, V], and |
| 27 | + * optionally the duplicate HF user cache state. |
| 28 | + * A program may carry `prefill` (N tokens per step, for the prompt) and |
| 29 | + * `decode` (one token), or a single `forward`. |
| 30 | + * |
| 31 | + * Version 1 supports float32 and float16 caches. See the versioned contract |
| 32 | + * in docs/source/llm-hf-static-io-contract.md for metadata and padding rules. |
| 33 | + * |
| 34 | + * Sliding-window layers may hold window-sized caches (HF's): get_max_seq_len |
| 35 | + * defines the context, and a window's mask also marks a prompt chunk's padding |
| 36 | + * rows (all false), so the program shifts the window by the real rows only. |
| 37 | + * |
| 38 | + * This IOManager owns the caches and counters, one set shared by every method |
| 39 | + * (from the I/O buffer allocator when there is one, so a delegate reads them in |
| 40 | + * place), and builds each step's tokens, masks and positions as HF's generator |
| 41 | + * does. The program writes each step's rows and counters into those buffers. |
| 42 | + */ |
| 43 | +class ET_EXPERIMENTAL HfStaticIOManager : public IOManager { |
| 44 | + public: |
| 45 | + static constexpr const char* kContract = "hf_static"; |
| 46 | + static constexpr int64_t kContractVersion = 1; |
| 47 | + |
| 48 | + explicit HfStaticIOManager(ET_MODULE_NAMESPACE::Module& module); |
| 49 | + ~HfStaticIOManager() override; |
| 50 | + |
| 51 | + ET_NODISCARD runtime::Error load( |
| 52 | + const std::string& prefill_method, |
| 53 | + const std::string& decode_method) override; |
| 54 | + |
| 55 | + ET_NODISCARD runtime::Error reset( |
| 56 | + const std::string& prefill_method, |
| 57 | + const std::string& decode_method) override; |
| 58 | + |
| 59 | + runtime::Result<std::vector<runtime::EValue>> prepare_prefill( |
| 60 | + const TensorPtr& input, |
| 61 | + const TensorPtr& start_pos, |
| 62 | + const std::string& prefill_method) override; |
| 63 | + |
| 64 | + runtime::Result<std::vector<runtime::EValue>> prepare_decode( |
| 65 | + const TensorPtr& input, |
| 66 | + const TensorPtr& start_pos, |
| 67 | + const std::string& decode_method) override; |
| 68 | + |
| 69 | + ET_NODISCARD runtime::Error update_prefill( |
| 70 | + const std::vector<runtime::EValue>& model_outputs, |
| 71 | + const std::string& prefill_method) override; |
| 72 | + |
| 73 | + ET_NODISCARD runtime::Error update_decode( |
| 74 | + const std::vector<runtime::EValue>& model_outputs, |
| 75 | + const std::string& decode_method) override; |
| 76 | + |
| 77 | + runtime::Result<executorch::aten::Tensor> logits( |
| 78 | + const std::vector<runtime::EValue>& model_outputs, |
| 79 | + const std::string& method) override; |
| 80 | + |
| 81 | + std::pair<std::string, std::string> method_names( |
| 82 | + const std::unordered_set<std::string>& available) const override; |
| 83 | + |
| 84 | + int64_t max_tokens_per_step(const std::string& method) const override; |
| 85 | + int64_t max_tokens_per_step(const std::string& method, int64_t start_pos) |
| 86 | + const override; |
| 87 | + |
| 88 | + private: |
| 89 | + struct Buffer { |
| 90 | + void* data = nullptr; |
| 91 | + size_t nbytes = 0; |
| 92 | + TensorPtr tensor; |
| 93 | + }; |
| 94 | + // A method's per-step inputs; the caches are shared. |
| 95 | + struct Method { |
| 96 | + int64_t query_len = 1; |
| 97 | + size_t num_inputs = 0; |
| 98 | + // Input index -> buffer, for input_ids, the masks and position_ids. |
| 99 | + std::map<size_t, Buffer> step_inputs; |
| 100 | + size_t input_ids = 0; |
| 101 | + size_t position_ids = 0; |
| 102 | + std::vector<size_t> masks; |
| 103 | + size_t first_cache = 0; |
| 104 | + // The logits, when the program leaves them to the runtime. |
| 105 | + Buffer logits; |
| 106 | + // The last step's real (unpadded) token count. |
| 107 | + int64_t valid = 0; |
| 108 | + }; |
| 109 | + |
| 110 | + runtime::Error load_metadata(); |
| 111 | + runtime::Error load_method(const std::string& method); |
| 112 | + runtime::Result<std::vector<runtime::EValue>> prepare( |
| 113 | + const TensorPtr& input, |
| 114 | + const TensorPtr& start_pos, |
| 115 | + const std::string& method); |
| 116 | + void allocate( |
| 117 | + const std::vector<std::pair<Buffer*, runtime::TensorInfo>>& buffers); |
| 118 | + |
| 119 | + ET_MODULE_NAMESPACE::Module& module_; |
| 120 | + std::unique_ptr<IOBufferAllocator> allocator_; |
| 121 | + struct Block { |
| 122 | + void* data; |
| 123 | + bool from_allocator; |
| 124 | + }; |
| 125 | + std::vector<Block> blocks_; |
| 126 | + std::map<std::string, Method> methods_; |
| 127 | + // Per layer keys, values, cumulative length; shared by every method. |
| 128 | + std::vector<Buffer> caches_; |
| 129 | + bool metadata_loaded_ = false; |
| 130 | + executorch::aten::ScalarType cache_dtype_ = |
| 131 | + executorch::aten::ScalarType::Float; |
| 132 | + int32_t num_layers_ = 0; |
| 133 | + int64_t context_len_ = 0; |
| 134 | + std::vector<float> logits_f_; |
| 135 | + TensorPtr logits_f_tensor_; |
| 136 | +}; |
| 137 | + |
| 138 | +void register_hf_static_io_manager(); |
| 139 | + |
| 140 | +} // namespace llm |
| 141 | +} // namespace extension |
| 142 | +} // namespace executorch |
0 commit comments