Skip to content

[KV cache] support-past-present-share-buffer - #268

Merged
rui-ren merged 2 commits into
mainfrom
ruiren/support-past-present-share-buffer
May 6, 2026
Merged

rui-ren merged 2 commits into
mainfrom
ruiren/support-past-present-share-buffer

Conversation

@rui-ren

@rui-ren rui-ren commented May 6, 2026

Copy link
Copy Markdown
Contributor

Support past present share buffer

Add supports_past_present_share_buffer=True

Benefit:

Save the memcpy overhead.

@github-actions

github-actions Bot commented May 6, 2026 •

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing a0b7ce1 → 61e30f0

Model Sub-model Changes Status
gemma4 (gemma4) decoder 9 🟡
gemma4 (gemma4) embedding 0 ⚪
gemma4 (gemma4) vision_encoder 95 🟡
gemma4_text model 9 🟡
gemma4 (gemma4) / decoder — 9 change(s)

Op summary: 127 → 125 nodes

--- base
+++ head
@@ -47,14 +47,13 @@
 MatMul
 Constant
 Reshape
-Cast
 Mul
 ReduceMean
 Constant
+CastLike
 Add
 Sqrt
 Div
-CastLike
 Reshape
 Attention
 Transpose
@@ -91,14 +90,13 @@
 MatMul
 Constant
 Reshape
-Cast
 Mul
 ReduceMean
 Constant
+CastLike
 Add
 Sqrt
 Div
-CastLike
 Reshape
 Attention
 Transpose

Added nodes:

  • + CastLike
  • + CastLike

Removed nodes:

  • - Cast
  • - CastLike
  • - Cast
  • - CastLike

Connectivity changes:

  • node[53] Add: input_ids [102, 103] → [101, 103]
  • node[55] Div: input_ids [100, 105] → [99, 105]
  • node[105] RMSNormalization: input_ids [159, 42] → [159, 43]
gemma4 (gemma4) / vision_encoder — 95 change(s)

Op summary: 190 → 202 nodes

--- base
+++ head
@@ -50,8 +50,14 @@
 Reshape
 RMSNormalization
 RMSNormalization
-CastLike
-RMSNormalization
+Mul
+Constant
+ReduceMean
+Constant
+CastLike
+Add
+Sqrt
+Div
 Constant
 Gather
 Constant
@@ -175,16 +181,22 @@
 Mul
 Add
 Constant
+CastLike
 OneHot
-CastLike
-Transpose
-MatMul
-Constant
-CastLike
-Mul
-CastLike
-RMSNormalization
-Transpose
-MatMul
-Constant
-Reshape
+Transpose
+MatMul
+Constant
+CastLike
+Mul
+Mul
+Constant
+ReduceMean
+Constant
+CastLike
+Add
+Sqrt
+Div
+Transpose
+MatMul
+Constant
+Reshape

Added nodes:

  • + Mul
  • + Constant
  • + ReduceMean
  • + Constant
  • + CastLike
  • + Add
  • + Sqrt
  • + Div
  • + CastLike
  • + Transpose
  • + MatMul
  • + Constant
  • + CastLike
  • + Mul
  • + Mul
  • + Constant
  • + ReduceMean
  • + Constant
  • + CastLike
  • + Add
  • + Sqrt
  • + Div
  • + Transpose
  • + MatMul
  • + Constant
  • + Reshape

Removed nodes:

  • - CastLike
  • - RMSNormalization
  • - CastLike
  • - Transpose
  • - MatMul
  • - Constant
  • - CastLike
  • - Mul
  • - CastLike
  • - RMSNormalization
  • - Transpose
  • - MatMul
  • - Constant
  • - Reshape

Modified attributes:

  • node[65] Gather: axis: 0 → 2
  • node[144] Constant: value_float: None → 0.0, value_int: 1 → None
  • node[160] Constant: value_int: 1 → 0
  • node[162] Constant: value_int: None → 0, value_ints: [1] → None
  • node[164] Constant: value_int: 1 → None, value_ints: None → [1]
  • node[166] Constant: value_int: None → 1, value_ints: [3] → None
  • node[170] Constant: value_int: None → 1, value_ints: [0, 1] → None
  • node[176] Constant: value_floats: [0.0, 0.1111111111111111] → None, value_ints: None → [0, 1]

Connectivity changes:

  • node[1] CastLike: input_ids [61, 0] → [59, 0]
  • node[3] CastLike: input_ids [63, 0] → [61, 0]
  • node[4] Mul: input_ids [0, 62] → [0, 60]
  • node[5] Sub: input_ids [65, 64] → [63, 62]
  • node[7] MatMul: input_ids [66, 67] → [64, 65]
  • node[9] Gather: input_ids [1, 69] → [1, 67]
  • node[11] Equal: input_ids [70, 71] → [68, 69]
  • node[13] Clip: input_ids [1, 73] → [1, 71]
  • node[15] Gather: input_ids [74, 75] → [72, 73]
  • node[17] Gather: input_ids [74, 77] → [72, 75]
  • node[19] Gather: input_ids [2, 79] → [2, 77]
  • node[21] Gather: input_ids [2, 81] → [2, 79]
  • node[22] Gather: input_ids [80, 76] → [78, 74]
  • node[23] Gather: input_ids [82, 78] → [80, 76]
  • node[24] Add: input_ids [83, 84] → [81, 82]
  • node[25] CastLike: input_ids [4, 85] → [4, 83]
  • node[26] Not: input_ids [72] → [70]
  • node[27] Unsqueeze: input_ids [87, 5] → [85, 5]
  • node[28] Where: input_ids [88, 85, 86] → [86, 83, 84]
  • node[29] Add: input_ids [68, 89] → [66, 87]
  • node[30] CastLike: input_ids [6, 90] → [6, 88]
  • node[31] CastLike: input_ids [4, 90] → [4, 88]
  • node[32] Where: input_ids [72, 91, 92] → [70, 89, 90]
  • node[33] Unsqueeze: input_ids [93, 7] → [91, 7]
  • node[34] RMSNormalization: input_ids [90, 8] → [88, 8]
  • node[35] Clip: input_ids [95, 12, 13] → [93, 12, 13]
  • node[37] MatMul: input_ids [96, 97] → [94, 95]
  • node[38] Clip: input_ids [98, 14, 15] → [96, 14, 15]
  • node[39] Clip: input_ids [95, 17, 18] → [93, 17, 18]
  • node[41] MatMul: input_ids [100, 101] → [98, 99]
  • node[42] Clip: input_ids [102, 19, 20] → [100, 19, 20]
  • node[43] Clip: input_ids [95, 22, 23] → [93, 22, 23]
  • node[45] MatMul: input_ids [104, 105] → [102, 103]
  • node[46] Clip: input_ids [106, 24, 25] → [104, 24, 25]
  • node[47] Reshape: input_ids [99, 26] → [97, 26]
  • node[48] Reshape: input_ids [103, 26] → [101, 26]
  • node[49] Reshape: input_ids [107, 26] → [105, 26]
  • node[50] RMSNormalization: input_ids [108, 27] → [106, 27]
  • node[51] RMSNormalization: input_ids [109, 28] → [107, 28]
  • node[65] Gather: input_ids [10, 122] → [1, 123]
  • node[81] Slice: input_ids [132, 30, 34, 32] → [135, 33, 30, 31]
  • node[85] Mul: input_ids [132, 129] → [142, 132]
  • node[96] Slice: input_ids [134, 30, 34, 32] → [137, 33, 30, 31]
  • node[100] Mul: input_ids [134, 129] → [157, 132]
  • node[112] Transpose: input_ids [172] → [118]
  • node[173] Div: input_ids [218, 227] → [226, 231]

Initializer changes:

  • initializer count 59 → 57
gemma4_text / model — 9 change(s)

Op summary: 129 → 127 nodes

--- base
+++ head
@@ -49,14 +49,13 @@
 MatMul
 Constant
 Reshape
-Cast
 Mul
 ReduceMean
 Constant
+CastLike
 Add
 Sqrt
 Div
-CastLike
 Reshape
 Attention
 Transpose
@@ -93,14 +92,13 @@
 MatMul
 Constant
 Reshape
-Cast
 Mul
 ReduceMean
 Constant
+CastLike
 Add
 Sqrt
 Div
-CastLike
 Reshape
 Attention
 Transpose

Added nodes:

  • + CastLike
  • + CastLike

Removed nodes:

  • - Cast
  • - CastLike
  • - Cast
  • - CastLike

Connectivity changes:

  • node[55] Add: input_ids [105, 106] → [104, 106]
  • node[57] Div: input_ids [103, 108] → [102, 108]
  • node[107] RMSNormalization: input_ids [162, 43] → [162, 44]

Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed)

@github-actions

github-actions Bot commented May 6, 2026 •

Copy link
Copy Markdown

Performance Comparison

Comparing a0b7ce1 → 61e30f0

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0% ⚪
bert (feature-extraction) num_nodes 60 60 +0.0% ⚪
falcon model_size_bytes 364 KB 364 KB +0.0% ⚪
falcon num_nodes 66 66 +0.0% ⚪
gemma2 model_size_bytes 428 KB 428 KB +0.0% ⚪
gemma2 num_nodes 107 107 +0.0% ⚪
gpt2 model_size_bytes 388 KB 388 KB +0.0% ⚪
gpt2 num_nodes 53 53 +0.0% ⚪
llama model_size_bytes 425 KB 425 KB +0.0% ⚪
llama num_nodes 61 61 +0.0% ⚪
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
llama (static-cache) num_nodes 58 58 +0.0% ⚪
mamba (ssm-text-generation) model_size_bytes 296 KB 296 KB +0.0% ⚪
mamba (ssm-text-generation) num_nodes 98 98 +0.0% ⚪
phi3 model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 num_nodes 59 59 +0.0% ⚪
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 (static-cache) num_nodes 56 56 +0.0% ⚪
qwen2 model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 num_nodes 61 61 +0.0% ⚪
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 (static-cache) num_nodes 58 58 +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) model_size_bytes 506 KB 506 KB +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) num_nodes 275 275 +0.0% ⚪
qwen3_5_text (hybrid-text-generation) model_size_bytes 458 KB 458 KB +0.0% ⚪
qwen3_5_text (hybrid-text-generation) num_nodes 129 129 +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) model_size_bytes 977 KB 977 KB +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) num_nodes 408 408 +0.0% ⚪
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0% ⚪
t5 (seq2seq) num_nodes 166 166 +0.0% ⚪
whisper (speech-to-text) model_size_bytes 1008 KB 1008 KB +0.0% ⚪
whisper (speech-to-text) num_nodes 128 128 +0.0% ⚪

No performance regressions.

@codecov

codecov Bot commented May 6, 2026

Copy link
Copy Markdown

The author of this PR, rui-ren, is not an activated member of this organization on Codecov.
Please activate this user on Codecov to display this PR comment.
Coverage data is still being uploaded to Codecov.io for purposes of overall coverage calculations.
Please don't hesitate to email us at [email protected] with any questions.

@rui-ren
rui-ren marked this pull request as ready for review May 6, 2026 01:38
@rui-ren
rui-ren requested review from a team, Copilot, hanbitmyths and justinchuby May 6, 2026 01:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the ORT GenAI config generation path to enable past_present_share_buffer by default for more execution providers (via EpCapabilities), and refines the logic that caps max_length for memory-constrained EPs (e.g., WebGPU) to reduce KV-cache-related memcpy overhead.

Changes:

  • Extend EpCapabilities with cap_kv_buffer_max_length and set supports_past_present_share_buffer=True for several built-in EPs.
  • Update genai_config default search params to cap max_length based on the new capability flag rather than tying it directly to buffer-sharing.
  • Update/add unit tests to reflect the new defaults and to cover the new max-length capping behavior for custom EPs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/mobius/integrations/ort_genai/genai_config.py Switch max_length capping logic to a dedicated EP capability while propagating past_present_share_buffer from EP caps.
src/mobius/integrations/ort_genai/genai_config_test.py Update expectations for CPU/CUDA defaults; add tests for custom EP max-length capping behavior.
src/mobius/_execution_providers.py Add cap_kv_buffer_max_length to EpCapabilities and enable supports_past_present_share_buffer across more built-in EP entries.
Comments suppressed due to low confidence (1)

src/mobius/_execution_providers.py:106

  • EpCapabilities now has cap_kv_buffer_max_length but there is no invariant enforcement that it only makes sense when supports_past_present_share_buffer=True (as described in the docstring). Consider adding a post_init check that raises when cap_kv_buffer_max_length is True but supports_past_present_share_buffer is False, to prevent silent misconfiguration by custom EP registrations.
    name: str
    gqa_dtypes: frozenset[ir.DataType] = dataclasses.field(default_factory=frozenset)
    qkv_pack_dtypes: frozenset[ir.DataType] = dataclasses.field(default_factory=frozenset)
    supports_fused_rope: bool = True
    supports_skip_layer_norm: bool = True
    supports_fused_moe: bool = True
    supports_packed_multi_head_attention: bool = False
    default_int4_accuracy_level: int = 0
    provider_options: dict[str, str] = dataclasses.field(default_factory=dict)
    enable_graph_capture: bool = False
    supports_past_present_share_buffer: bool = False
    cap_kv_buffer_max_length: bool = False

    def __post_init__(self) -> None:
        if not self.supports_fused_rope and self.qkv_pack_dtypes:
            raise ValueError(

Comment thread src/mobius/_execution_providers.py Resolved
@justinchuby

Copy link
Copy Markdown
Member

Thanks!

@rui-ren
rui-ren merged commit 3523cbb into main May 6, 2026
22 of 23 checks passed
@rui-ren
rui-ren deleted the ruiren/support-past-present-share-buffer branch May 6, 2026 03:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants