Skip to content

Support INT8 SDPA template for CPU #2148

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
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Valentine233
Copy link
Collaborator

@Valentine233 Valentine233 commented Apr 29, 2025

A follow-up of #1372, with RFC pytorch/pytorch#144941.

Here, we support the template-based method for INT8 SDPA, for potential better performance. With the template, we can generate the corresponding template during compiling and implement the kernel with high flexibility. By taking advantage of this, it would be more flexible to tune the optimized kernel with different parallel strategies or block sizes through benchmarking in the future.

This PR creates a CPP Int8 SDPA template CppInt8SdpaTemplate by inheriting CppFlexAttentionTemplate, and implement a template-based kernel. For max-autotune, we have two choices for Int8 SDPA: the template one by default, and the explicit OP torch.ops.torchao.scaled_dot_product_int8 as fallback. As the two algorithms are almost the same, there is no need to do the actual tuning and choose one. We assume the template one has better perf, and we will keep optimizing it in the future.

Copy link

pytorch-bot bot commented Apr 29, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/2148

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 71e17d0 with merge base 137b079 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 29, 2025
@Valentine233 Valentine233 added the topic: not user facing Use this tag if you don't want this PR to show up in release notes label Apr 29, 2025
@Valentine233 Valentine233 marked this pull request as draft April 29, 2025 05:42
@Valentine233 Valentine233 marked this pull request as ready for review April 29, 2025 08:15
@leslie-fang-intel
Copy link
Collaborator

Support the template-based method for INT8 SDPA, for further better performance.

Could you further elaborate the benefit in the summary for why the template based solution needed in this PR?

@Valentine233
Copy link
Collaborator Author

Could you further elaborate the benefit in the summary for why the template based solution needed in this PR?

Thanks, done.

@@ -56,7 +56,7 @@
tags=[torch._C.Tag.needs_fixed_stride_order],
)
lib.define(
"scaled_dot_product_int8(Tensor query, Tensor key, Tensor value, Tensor? attn_mask=None, float dropout_p=0.0, bool is_causal=False, float scale=0.0, float q_scale=1.0, int q_zp=0, float k_scale=1.0, int k_zp=0, float v_scale=1.0, int v_zp=0, float a_scale=1.0, int a_zp=0, float o_scale=1.0, int o_zp=0) -> Tensor"
"scaled_dot_product_int8(Tensor query, Tensor key, Tensor value, Tensor? attn_mask=None, float dropout_p=0.0, bool is_causal=False, float? scale=None, float q_scale=1.0, int q_zp=0, float k_scale=1.0, int k_zp=0, float v_scale=1.0, int v_zp=0, float a_scale=1.0, int a_zp=0, float o_scale=1.0, int o_zp=0) -> Tensor"
Copy link
Collaborator

Choose a reason for hiding this comment

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

why need to change the OP schema? It seems not related to this PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it's an additional modification.
This is to keep consistent with the SDPA schema in pytorch func: scaled_dot_product_attention(Tensor query, Tensor key, Tensor value, Tensor? attn_mask=None, float dropout_p=0.0, bool is_causal=False, *, float? scale=None, bool enable_gqa=False) -> Tensor.

from torchao.utils import TORCH_VERSION_AT_LEAST_2_7

if TORCH_VERSION_AT_LEAST_2_7:
# TORCH_VERSION_AT_LEAST_2_7 is needed for int8 sdpa lowering
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add more notes for why at least 2.7 is needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Some functions used in INT8 SDPA lowering are only available for higher version PyTorch, like get_fill_order.

o_zp=o_zp,
)

if len(choices) == 0:
Copy link
Collaborator

@leslie-fang-intel leslie-fang-intel Apr 30, 2025

Choose a reason for hiding this comment

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

Just to confirm, does that mean prioritizing using a template-based solution when available, rather than relying on tuning?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, you are absolutely right. The two algorithms are almost the same now. In the future, the template one is expected to have a better perf.

@Valentine233 Valentine233 requested a review from jansel April 30, 2025 02:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. topic: not user facing Use this tag if you don't want this PR to show up in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants