-
Notifications
You must be signed in to change notification settings - Fork 312
Add StaticScatterKeyValueCache: drive mobius bias-aware external-KV (TensorScatter + nonpad_kv_seqlen) static cache #2235
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
Open
titaiwangms
wants to merge
8
commits into
main
Choose a base branch
from
static-scatter-kv-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6b1ba5b
Add StaticScatterKeyValueCache for mobius static-cache decoders (#349)
titaiwangms 5ee880d
Enable StaticScatterKeyValueCache e2e fixture-parity test
titaiwangms d3a3837
Address StaticScatterKeyValueCache review: fail-loud RewindTo, predic…
titaiwangms 4753c69
Reconcile static-scatter tests: restore e2e parity + enable RewindTo …
titaiwangms 345800b
Fix CI build + lint for static-scatter KV cache test
titaiwangms 7c0e90a
Address Copilot review nits on static-scatter KV cache
titaiwangms 2fd84e8
Skip StaticScatter fixture tests on the DirectML EP
titaiwangms 72e7199
Address review feedback on static-scatter KV cache
titaiwangms 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
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
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,57 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| namespace Generators { | ||
|
|
||
| // The per-step index pair a static-scatter (TensorScatter) KV cache needs. | ||
| // | ||
| // A mobius-exported static-cache decoder consumes a pre-allocated KV buffer of | ||
| // shape [batch, max_seq_len, kv_hidden] and writes each step's new key/value | ||
| // rows into it in place via TensorScatter, then reads them back through | ||
| // Attention. Two int64 [batch] inputs drive that: | ||
| // * write_indices - the cache row offset TensorScatter writes this step's | ||
| // rows at (i.e. how many valid tokens are already cached | ||
| // BEFORE this step). | ||
| // * nonpad_kv_seqlen - the number of valid cached tokens AFTER this step, | ||
| // which Attention reads as the per-batch seqlens_k. | ||
| struct StaticScatterIndices { | ||
| int64_t write_index; // valid cache tokens before this step (scatter offset) | ||
| int64_t nonpad_kv_seqlen; // valid cache tokens after this step (Attention seqlens_k) | ||
| }; | ||
|
|
||
| // Tracks the running static-scatter cache indices for a single (batch==1) | ||
| // generation stream. Kept as a standalone, dependency-free helper so the | ||
| // off-by-one / init behaviour can be unit-tested without standing up a Model. | ||
| // | ||
| // Sequencing contract (the crux mobius and genai must agree on): | ||
| // * The very first step (prefill) writes at row 0 and reports nonpad equal to | ||
| // the number of prefill tokens. | ||
| // * Each subsequent step's write_index is the PREVIOUS step's nonpad_kv_seqlen, | ||
| // so rows are appended contiguously with no gap or overlap. | ||
| // This deliberately does NOT reuse genai's existing past_sequence_length scalar | ||
| // (which inits to -1 and is consumed differently); mixing the two would yield | ||
| // nonpad = 2N-1 after a length-N prefill instead of N. | ||
| class StaticScatterIndexTracker { | ||
| public: | ||
| // Advance one generation step that appended new_unpadded_tokens valid tokens | ||
| // (the prompt length on prefill, normally 1 per decode step). Returns the | ||
| // index pair to bind for THIS step, then folds the new tokens into the | ||
| // running total for the next step. | ||
| StaticScatterIndices Advance(int64_t new_unpadded_tokens) { | ||
| const int64_t write_index = valid_tokens_; | ||
| valid_tokens_ += new_unpadded_tokens; | ||
| return {write_index, valid_tokens_}; | ||
| } | ||
|
|
||
| // Valid cached tokens before the next step. Zero before any Advance(). | ||
| int64_t valid_tokens() const { return valid_tokens_; } | ||
|
|
||
| private: | ||
| int64_t valid_tokens_{0}; | ||
| }; | ||
|
|
||
| } // namespace Generators |
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.
Uh oh!
There was an error while loading. Please reload this page.