Skip to content

Add Paged Attention Op for CUDA SM80 support - #24595

Merged
aciddelgado merged 16 commits into
mainfrom
aciddelgado/paged_attention
Jun 12, 2025
Merged

Add Paged Attention Op for CUDA SM80 support#24595
aciddelgado merged 16 commits into
mainfrom
aciddelgado/paged_attention

Conversation

@aciddelgado

Copy link
Copy Markdown
Contributor

Description

Adds Paged Attention Op which enables of Paged KV Cache. Inputs to this op are unpadded (packed / varlen) so Cumulative Sequence Lengths are a required input.

Motivation and Context

Adding this op to ONNXRuntime is necessary to allow the GenAI team to enable a continuous batching server API.

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/contrib_ops/cpu/bert/attention_parameters.h Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/flash_attention/flash_api.h
Comment thread onnxruntime/contrib_ops/cuda/bert/paged_attention.cc
Comment thread onnxruntime/contrib_ops/cuda/bert/paged_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/paged_attention.cc
Comment thread onnxruntime/test/python/transformers/test_paged_attention_cuda.py
Comment thread onnxruntime/test/python/transformers/test_paged_attention_cuda.py
Comment thread onnxruntime/test/python/transformers/test_paged_attention_cuda.py
Comment thread onnxruntime/test/python/transformers/test_paged_attention_cuda.py
Comment thread onnxruntime/test/python/transformers/test_paged_attention_cuda.py
Comment thread onnxruntime/contrib_ops/cuda/bert/paged_attention.cc Fixed
Comment thread onnxruntime/contrib_ops/cuda/bert/paged_attention_helper.h Fixed
Comment thread onnxruntime/test/python/transformers/test_paged_attention_cuda.py Fixed
Comment thread onnxruntime/test/python/transformers/test_paged_attention_cuda.py Fixed
Comment thread onnxruntime/test/python/transformers/test_paged_attention_cuda.py Fixed
Comment thread onnxruntime/test/python/transformers/test_paged_attention_cuda.py Fixed

# Some rows might be completely masked out so we fill them with zero instead of NaN
if window_size[0] >= 0 or window_size[1] >= 0:
attention = attention.masked_fill(torch.all(local_mask, dim=-1, keepdim=True), 0.0)

Check failure

Code scanning / CodeQL

Potentially uninitialized local variable

Local variable 'local_mask' may be used before it is initialized.

Copilot Autofix

AI about 1 year ago

To fix the issue, we need to ensure that local_mask is always initialized before it is used. The best approach is to initialize local_mask to a default value (e.g., None) at the start of the function or relevant block. Then, before using local_mask on line 410, we should check whether it has been initialized (i.e., not None) and only use it if it is valid. This ensures that the code does not attempt to use an uninitialized variable.


Suggested changeset 1
onnxruntime/test/python/transformers/test_paged_attention_cuda.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/onnxruntime/test/python/transformers/test_paged_attention_cuda.py b/onnxruntime/test/python/transformers/test_paged_attention_cuda.py
--- a/onnxruntime/test/python/transformers/test_paged_attention_cuda.py
+++ b/onnxruntime/test/python/transformers/test_paged_attention_cuda.py
@@ -391,2 +391,3 @@
         scores.masked_fill_(rearrange(~key_padding_mask, "b s -> b 1 1 s"), float("-inf"))
+    local_mask = None
     if window_size[0] >= 0 or window_size[1] >= 0:
@@ -408,3 +409,3 @@
     # Some rows might be completely masked out so we fill them with zero instead of NaN
-    if window_size[0] >= 0 or window_size[1] >= 0:
+    if local_mask is not None and (window_size[0] >= 0 or window_size[1] >= 0):
         attention = attention.masked_fill(torch.all(local_mask, dim=-1, keepdim=True), 0.0)
EOF
@@ -391,2 +391,3 @@
scores.masked_fill_(rearrange(~key_padding_mask, "b s -> b 1 1 s"), float("-inf"))
local_mask = None
if window_size[0] >= 0 or window_size[1] >= 0:
@@ -408,3 +409,3 @@
# Some rows might be completely masked out so we fill them with zero instead of NaN
if window_size[0] >= 0 or window_size[1] >= 0:
if local_mask is not None and (window_size[0] >= 0 or window_size[1] >= 0):
attention = attention.masked_fill(torch.all(local_mask, dim=-1, keepdim=True), 0.0)
Copilot is powered by AI and may make mistakes. Always verify output.
slot_mappings = get_slot_mappings(config, block_table, total_seqlens, cum_seqlens)

# Set window size for local / causal
window_size = (-1, -1)

Check warning

Code scanning / CodeQL

Variable defined multiple times

This assignment to 'window_size' is unnecessary as it is [redefined](1) before this value is used. This assignment to 'window_size' is unnecessary as it is [redefined](2) before this value is used.

Copilot Autofix

AI about 1 year ago

To fix the issue, we will remove the unnecessary assignment to window_size on line 548. This will ensure that the variable is only defined when it is actually needed, either on line 552 or 555, depending on the branch of the if statement. No additional changes are required, as the logic of the code remains intact.


Suggested changeset 1
onnxruntime/test/python/transformers/test_paged_attention_cuda.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/onnxruntime/test/python/transformers/test_paged_attention_cuda.py b/onnxruntime/test/python/transformers/test_paged_attention_cuda.py
--- a/onnxruntime/test/python/transformers/test_paged_attention_cuda.py
+++ b/onnxruntime/test/python/transformers/test_paged_attention_cuda.py
@@ -547,3 +547,2 @@
     # Set window size for local / causal
-    window_size = (-1, -1)
     left_window_size = -1
EOF
@@ -547,3 +547,2 @@
# Set window size for local / causal
window_size = (-1, -1)
left_window_size = -1
Copilot is powered by AI and may make mistakes. Always verify output.

# Set window size for local / causal
window_size = (-1, -1)
left_window_size = -1

Check warning

Code scanning / CodeQL

Variable defined multiple times

This assignment to 'left_window_size' is unnecessary as it is [redefined](1) before this value is used. This assignment to 'left_window_size' is unnecessary as it is [redefined](2) before this value is used.

Copilot Autofix

AI about 1 year ago

To fix the issue, we should remove the redundant assignment to left_window_size on line 549. This will eliminate unnecessary code and improve clarity without altering the functionality of the program. The subsequent if-else block already ensures that left_window_size is assigned an appropriate value based on the condition.

Suggested changeset 1
onnxruntime/test/python/transformers/test_paged_attention_cuda.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/onnxruntime/test/python/transformers/test_paged_attention_cuda.py b/onnxruntime/test/python/transformers/test_paged_attention_cuda.py
--- a/onnxruntime/test/python/transformers/test_paged_attention_cuda.py
+++ b/onnxruntime/test/python/transformers/test_paged_attention_cuda.py
@@ -548,3 +548,2 @@
     window_size = (-1, -1)
-    left_window_size = -1
     if config.local:
EOF
@@ -548,3 +548,2 @@
window_size = (-1, -1)
left_window_size = -1
if config.local:
Copilot is powered by AI and may make mistakes. Always verify output.

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/test/python/transformers/test_paged_attention_cuda.py
Comment thread onnxruntime/test/python/transformers/test_paged_attention_cuda.py
Comment thread onnxruntime/contrib_ops/cpu/bert/attention_parameters.h Outdated
Comment thread onnxruntime/contrib_ops/cpu/bert/attention_parameters.h Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/paged_attention.cc
Comment thread onnxruntime/contrib_ops/cuda/bert/paged_attention_helper.h Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/paged_attention_helper.h Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/paged_attention_impl.cu Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc
Comment thread onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc
Comment thread onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc
Comment thread onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc
Comment thread onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc
Comment thread onnxruntime/contrib_ops/cpu/bert/attention_parameters.h Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/attention_data.h Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/paged_attention_helper.h Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc
Comment thread onnxruntime/contrib_ops/cuda/bert/paged_attention_impl.cu Outdated

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc
Comment thread onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc
Comment thread onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc
Comment thread onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc
Comment thread onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc
Comment thread onnxruntime/contrib_ops/cpu/bert/attention_parameters.h Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc

@tianleiwu tianleiwu 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.

What design change needed if we want to support FP8 or FP4 paged attention in the future?

Comment thread onnxruntime/contrib_ops/cuda/bert/paged_attention_helper.h Outdated
@aciddelgado

Copy link
Copy Markdown
Contributor Author

What design change needed if we want to support FP8 or FP4 paged attention in the future?

New kernel necessary

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.

4 participants