-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[MLX] Per-token positions and a shared pool #22037
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
Merged
kiymetakdemir
merged 2 commits into
pytorch:main
from
kiymetakdemir:mlx-byte-layer-prep
Aug 25, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
|
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. This is just a move of the code that used to be in backends/mlx/runtime/MLXSequenceCache.h, right?
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. Yes, correct. |
||
| * 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 <algorithm> | ||
| #include <stdexcept> | ||
| #include <vector> | ||
|
|
||
| #include "MLXExecutor.h" // Tensor, StreamOrDevice | ||
|
|
||
| namespace executorch { | ||
| namespace backends { | ||
| namespace mlx { | ||
|
|
||
| // Per-layer K or V store, SDPA-major [1, H, slots, D] (cells on axis 2). The | ||
| // caller hands down physical slot ranges (it has already applied any ring | ||
| // modulo), so the pool is layout-agnostic: policies differ only in how many | ||
| // slots the layer asks for and how many ranges a step produces. | ||
| class Pool { | ||
| public: | ||
| // initial_slots above max_slots is clamped, not rejected: the config default | ||
| // exceeds the cap of any smaller cache, so this is the normal path. | ||
| Pool(int initial_slots, int max_slots, int H, int D, ::mlx::core::Dtype dtype) | ||
| : dtype_(dtype), | ||
| max_slots_(max_slots), | ||
| buf_(::mlx::core::zeros( | ||
| ::mlx::core::Shape{1, H, std::min(initial_slots, max_slots), D}, | ||
| dtype)) {} | ||
|
|
||
| // Place `update` at slot `start`, casting to the storage dtype if it differs. | ||
| void write(int start, int len, const Tensor& update, StreamOrDevice s) { | ||
| const int H = static_cast<int>(buf_.shape(1)); | ||
| const int D = static_cast<int>(buf_.shape(3)); | ||
| if (start < 0 || start + len > max_slots_) { | ||
| throw std::runtime_error("Pool::write: run out of bounds"); | ||
| } | ||
| if (static_cast<int>(update.shape(2)) != len) { | ||
| throw std::runtime_error("Pool::write: update length != run length"); | ||
| } | ||
| if (static_cast<int>(update.shape(1)) != H || | ||
| static_cast<int>(update.shape(3)) != D) { | ||
| throw std::runtime_error("Pool::write: K/V heads/dim mismatch"); | ||
| } | ||
| maybe_grow(start + len, s); | ||
| const Tensor u = update.dtype() == dtype_ | ||
| ? update | ||
| : ::mlx::core::astype(update, dtype_, s); | ||
| buf_ = ::mlx::core::slice_update( | ||
| buf_, | ||
| u, | ||
| ::mlx::core::Shape{0, 0, start, 0}, | ||
| ::mlx::core::Shape{1, H, start + len, D}, | ||
| s); | ||
| } | ||
|
|
||
| // Slots [start, start+len). A ring read starts mid-pool, so the start matters | ||
| // here as much as it does for a write. | ||
| Tensor read(int start, int len, StreamOrDevice s) const { | ||
| const int H = static_cast<int>(buf_.shape(1)); | ||
| const int D = static_cast<int>(buf_.shape(3)); | ||
| if (start < 0 || start + len > slots()) { | ||
| throw std::runtime_error("Pool::read: run out of bounds"); | ||
| } | ||
| return ::mlx::core::slice( | ||
| buf_, | ||
| ::mlx::core::Shape{0, 0, start, 0}, | ||
| ::mlx::core::Shape{1, H, start + len, D}, | ||
| ::mlx::core::Shape{1, 1, 1, 1}, | ||
| s); | ||
| } | ||
|
|
||
| // Slots currently allocated; grows toward max_slots on demand. | ||
| int slots() const { | ||
| return static_cast<int>(buf_.shape(2)); | ||
| } | ||
|
|
||
| private: | ||
| // Make room for `needed` slots, growing only if the pool is short: double | ||
| // until it fits, never past max_slots_. Cells keep their index, so growth is | ||
| // a zero-pad on the cell axis. | ||
| void maybe_grow(int needed, StreamOrDevice s) { | ||
| const int cur = slots(); | ||
| if (needed <= cur) { | ||
| return; | ||
| } | ||
| int next = std::max(cur, 1); // an empty pool has nothing to double | ||
| while (next < needed) { | ||
| next *= 2; | ||
| } | ||
| // The last doubling can overshoot; write() already bounds `needed` by | ||
| // max_slots_, so clamping here cannot undershoot it. | ||
| next = std::min(next, max_slots_); | ||
| const int H = static_cast<int>(buf_.shape(1)); | ||
| const int D = static_cast<int>(buf_.shape(3)); | ||
| Tensor pad = | ||
| ::mlx::core::zeros(::mlx::core::Shape{1, H, next - cur, D}, dtype_); | ||
| buf_ = ::mlx::core::concatenate(std::vector<Tensor>{buf_, pad}, 2, s); | ||
| } | ||
|
|
||
| ::mlx::core::Dtype dtype_; | ||
| int max_slots_; | ||
| Tensor buf_; | ||
| }; | ||
|
|
||
| } // namespace mlx | ||
| } // namespace backends | ||
| } // namespace executorch | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why the raw pointer? Can we pass a const vec ref?