| 20 | }; |
| 21 | |
| 22 | class TransformerKVCache { |
| 23 | public: |
| 24 | TransformerKVCache() = default; |
| 25 | TransformerKVCache( |
| 26 | int64_t cache_steps, |
| 27 | int64_t step_elems, |
| 28 | std::vector<core::TensorValue> keys, |
| 29 | std::vector<core::TensorValue> values); |
| 30 | |
| 31 | void import_state(const TransformerKVState & state); |
| 32 | TransformerKVState export_state() const; |
| 33 | |
| 34 | void advance_after_direct_append(int64_t steps); |
| 35 | void retain_prefix(int64_t prefix_steps); |
| 36 | |
| 37 | int64_t valid_steps() const noexcept; |
| 38 | int64_t current_end() const noexcept; |
| 39 | int64_t cache_steps() const noexcept; |
| 40 | |
| 41 | const core::TensorValue & key_tensor(size_t layer) const; |
| 42 | const core::TensorValue & value_tensor(size_t layer) const; |
| 43 | |
| 44 | void trace_log_state(const std::string & name, int64_t num_heads, int64_t head_dim) const; |
| 45 | |
| 46 | private: |
| 47 | struct LayerCache { |
| 48 | core::TensorValue key_tensor; |
| 49 | core::TensorValue value_tensor; |
| 50 | std::vector<float> import_key_scratch; |
| 51 | std::vector<float> import_value_scratch; |
| 52 | }; |
| 53 | |
| 54 | int64_t cache_steps_ = 0; |
| 55 | int64_t step_elems_ = 0; |
| 56 | int64_t valid_steps_ = 0; |
| 57 | int64_t current_end_ = 0; |
| 58 | std::vector<LayerCache> layers_; |
| 59 | }; |
| 60 | |
| 61 | core::TensorValue view_transformer_kv_cache_steps( |
| 62 | core::ModuleBuildContext & ctx, |
no outgoing calls
no test coverage detected