-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[MLX] Add off-graph KV-cache flat runtime #21501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <optional> | ||
|
|
||
| #include "MLXExecutor.h" // Tensor, StreamOrDevice | ||
|
|
||
| namespace executorch { | ||
| namespace backends { | ||
| namespace mlx { | ||
|
|
||
| // The K/V window to attend over + how to mask it; the cache owns the semantic. | ||
| // `kind` mirrors MLX SDPA's mask forms (no mask / "causal" / explicit tensor). | ||
| // Will be added: a `window` on Causal (sliding-window -- the | ||
| // mask_mod axis) and a `softcap`/bias field (ALiBi, softcap -- the score_mod | ||
| // axis). | ||
| struct AttendSpec { | ||
| Tensor K; | ||
| Tensor V; | ||
| enum class Mask { None, Causal, Explicit } kind; | ||
| std::optional<Tensor> mask; // Explicit only | ||
| }; | ||
|
|
||
| // Op face of the off-graph KV cache; ExecutionState holds one (cross-cast from | ||
| // the registry's CacheBase*). Separate from CacheBase to avoid a diamond. | ||
| class MLXCacheImpl { | ||
| public: | ||
| virtual ~MLXCacheImpl() = default; | ||
|
|
||
| // Write this step's K/V for `layer` at `position` (the run's logical start); | ||
| // return the window + mask kind. k/v are BHSD. `position` is a host int -- | ||
| // the caller reads it off the graph so the cache stays pure graph + integer | ||
| // bookkeeping. The cache owns the mask: a multi-token chain is Causal, a | ||
| // single decode token is None. | ||
| virtual AttendSpec update_and_fetch( | ||
| int layer, | ||
| int position, | ||
| const Tensor& k, | ||
| const Tensor& v, | ||
| StreamOrDevice s) = 0; | ||
| }; | ||
|
|
||
| } // namespace mlx | ||
| } // namespace backends | ||
| } // namespace executorch |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <optional> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "MLXCacheImpl.h" // AttendSpec, MLXCacheImpl | ||
| #include "MLXExecutor.h" // to_shape | ||
|
|
||
| #include <executorch/extension/llm/cache/cache.h> | ||
| #include <executorch/extension/llm/cache/sequence_cache.h> | ||
|
|
||
| namespace executorch { | ||
| namespace backends { | ||
| namespace mlx { | ||
|
|
||
| namespace cache = ::executorch::extension::llm::cache; | ||
|
|
||
| // Per-layer K or V store, SDPA-major [1, H, capacity, D] (cells on axis 2), | ||
| // allocated at construction from config H/D/dtype. A write is a slice_update | ||
| // run (casting the update to the storage dtype if it differs); a read is a [0, | ||
| // len) slice. | ||
| class FlatPool { | ||
| public: | ||
| FlatPool(int capacity, int H, int D, ::mlx::core::Dtype dtype) | ||
| : dtype_(dtype), | ||
| buf_(::mlx::core::zeros( | ||
| to_shape(std::vector<int>{1, H, capacity, D}), | ||
| dtype)) {} | ||
|
|
||
| void write(int start, const Tensor& update, StreamOrDevice s) { | ||
| const int cap = static_cast<int>(buf_.shape(2)); | ||
| const int H = static_cast<int>(buf_.shape(1)); | ||
| const int D = static_cast<int>(buf_.shape(3)); | ||
| const int T = static_cast<int>(update.shape(2)); | ||
| if (start < 0 || start + T > cap) { | ||
| throw std::runtime_error("FlatPool::write: run out of bounds"); | ||
| } | ||
| if (static_cast<int>(update.shape(1)) != H || | ||
| static_cast<int>(update.shape(3)) != D) { | ||
| throw std::runtime_error("FlatPool::write: K/V heads/dim mismatch"); | ||
| } | ||
| const Tensor u = update.dtype() == dtype_ | ||
| ? update | ||
| : ::mlx::core::astype(update, dtype_, s); | ||
| std::vector<int> lo{0, 0, start, 0}; | ||
| std::vector<int> hi{1, H, start + T, D}; | ||
| buf_ = ::mlx::core::slice_update(buf_, u, to_shape(lo), to_shape(hi), s); | ||
| } | ||
|
|
||
| Tensor read(int len, StreamOrDevice s) const { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is return type Tensor or ::mlx::array?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tensor is the runtime's existing alias for ::mlx::core::array (in MLXExecutor.h), so I used it as in there. |
||
| const int H = static_cast<int>(buf_.shape(1)); | ||
| const int D = static_cast<int>(buf_.shape(3)); | ||
| std::vector<int> lo{0, 0, 0, 0}; | ||
| std::vector<int> hi{1, H, len, D}; | ||
| std::vector<int> step{1, 1, 1, 1}; | ||
| return ::mlx::core::slice( | ||
| buf_, to_shape(lo), to_shape(hi), to_shape(step), s); | ||
| } | ||
|
|
||
| private: | ||
| ::mlx::core::Dtype dtype_; | ||
| Tensor buf_; | ||
| }; | ||
|
|
||
| // Single-sequence, full-history cache: the neutral SequenceCache bookkeeping | ||
| // over per-layer FlatPools. update_and_fetch plans the step (integer runs), | ||
| // writes the new K/V, reads the retained window, and declares the mask; the op | ||
| // handler owns q/scale and calls SDPA. | ||
| class MLXFlatCache : public cache::SequenceCache, public MLXCacheImpl { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious: what is the rational for the indirection here? Why have MLXCacheImpl? Isn't MLXFlatCache an impl itself? |
||
| public: | ||
| explicit MLXFlatCache(const cache::CacheConfig& cfg) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something about this seems a bit off. In sequence cache we have have ring/flat/etc as different policies on the same class b/c different layers of the cache have different policies. But in the implementation here, you've made flatcache as the top-level (instead of something like mlxsequencecache), and ignored the layer-by-layer policy on cfg, and assumed all are flat. How do you intend to extend this to ring in the next PR? |
||
| : cache::SequenceCache(cfg) { | ||
| if (!cfg.kv_dtype) { | ||
| throw std::runtime_error( | ||
| "MLXFlatCache: CacheConfig::kv_dtype (storage precision) must be set"); | ||
| } | ||
| const ::mlx::core::Dtype dt = | ||
| resolve_dtype(static_cast<int8_t>(*cfg.kv_dtype)); | ||
| kpool_.reserve(static_cast<size_t>(cfg.n_layers)); | ||
| vpool_.reserve(static_cast<size_t>(cfg.n_layers)); | ||
| for (int l = 0; l < cfg.n_layers; ++l) { | ||
| // layers size 1 = one config broadcast to every layer, else per-layer. | ||
| const cache::LayerConfig& lc = | ||
| cfg.layers.size() == 1 ? cfg.layers.front() : cfg.layers[l]; | ||
| kpool_.emplace_back(cfg.capacity, lc.n_kv_heads, lc.head_dim, dt); | ||
| vpool_.emplace_back(cfg.capacity, lc.n_kv_heads, lc.head_dim, dt); | ||
| } | ||
| } | ||
|
|
||
| AttendSpec update_and_fetch( | ||
| int layer, | ||
| int position, | ||
| const Tensor& k, | ||
| const Tensor& v, | ||
| StreamOrDevice s) override { | ||
| if (layer < 0 || layer >= static_cast<int>(kpool_.size())) { | ||
| throw std::out_of_range("update_and_fetch: layer out of range"); | ||
| } | ||
| const int T = static_cast<int>(k.shape(2)); // BHSD: seq axis is 2 | ||
| const int start = position; | ||
|
|
||
| std::optional<cache::SeqStepPlan> p = this->plan(layer, start, T); | ||
| if (!p) { | ||
| throw std::runtime_error( | ||
| "update_and_fetch: step exceeds capacity or invalid layer"); | ||
| } | ||
| // Flat is single-run: one write [start, start+T), one read [0, end). A | ||
| // wrapping (two-run) plan means a ring layer, which the ring cache handles; | ||
| // reject it here rather than silently drop the second run. | ||
| if (p->n_write != 1 || p->n_read != 1) { | ||
| throw std::runtime_error( | ||
| "update_and_fetch: expected a flat single-run plan"); | ||
| } | ||
| const size_t l = static_cast<size_t>(layer); | ||
| kpool_[l].write(p->write[0].start, k, s); | ||
| vpool_[l].write(p->write[0].start, v, s); | ||
| Tensor K = kpool_[l].read(p->read[0].len, s); | ||
| Tensor V = vpool_[l].read(p->read[0].len, s); | ||
| this->commit(*p); | ||
|
|
||
| // A multi-token chain is Causal (MLX "causal" is lower-right aligned, so | ||
| // fresh and chunked prefill are both correct with new tokens at the tail); | ||
| // a single decode token needs no mask. | ||
| const AttendSpec::Mask kind = | ||
| (T > 1) ? AttendSpec::Mask::Causal : AttendSpec::Mask::None; | ||
| return AttendSpec{K, V, kind, std::nullopt}; | ||
| } | ||
|
|
||
| private: | ||
| std::vector<FlatPool> kpool_; | ||
| std::vector<FlatPool> vpool_; | ||
| }; | ||
|
|
||
| } // namespace mlx | ||
| } // namespace backends | ||
| } // namespace executorch | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,6 +153,17 @@ table SdpaNode { | |
| causal: bool = false; | ||
| } | ||
|
|
||
| table UpdateAndAttendNode { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't the op have an output dtype? |
||
| q: Tid (required); | ||
| k: Tid (required); | ||
| v: Tid (required); | ||
| position: Tid (required); | ||
| out: Tid (required); | ||
| layer_id: int32; | ||
| scale: float; | ||
| out_dtype: int8 = null; // ET ScalarType; null = keep SDPA's native output | ||
| } | ||
|
|
||
| table AddNode { | ||
| a: Tid (required); | ||
| b: Tid (required); | ||
|
|
@@ -1170,7 +1181,8 @@ union OpNode { | |
| BitwiseOrNode, | ||
| BitwiseXorNode, | ||
| IfNode, | ||
| RandomBitsNode | ||
| RandomBitsNode, | ||
| UpdateAndAttendNode | ||
| // BC: Add new op nodes here (append only) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
construct shape directly? Why go through vector?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it is a leftover from lazy first write, fixing it.