forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 66
[Release 2.6] Triton/inductor related optimisations #2008
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
Draft
jataylo
wants to merge
6
commits into
ROCm:release/2.6
Choose a base branch
from
jataylo:rel-26-flex-exhaust
base: release/2.6
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.
+153
−74
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0a775c4
[ROCm] Experimental flag for flex attn exhaustive tuning
jataylo a1c6a72
26 tuning updates
jataylo 0df99be
[ROCm] Incorporate ROCm triton specific tuning parameters (#148437)
jataylo 13625d1
Fixes
jataylo 8bf46b0
Updates
jataylo 7d6c8e0
typo
jithunnair-amd 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
6da9e66008b58a7b8553f96c69021cca0d0028f0 | ||
a34a79dbd711ea9f8fb5090bcaf24a7717574206 |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# mypy: allow-untyped-defs | ||
""" Triton Implementation of the flex_attention Kernel""" | ||
import os | ||
import itertools | ||
|
||
import logging | ||
import math | ||
|
@@ -1206,10 +1208,24 @@ def flex_attention( | |
if torch.version.hip: | ||
configs = [(c[0], c[1], c[2], 1) for c in configs] | ||
|
||
# Check if the environment variable is set | ||
if os.getenv("TORCHINDUCTOR_EXHAUSTIVE_FLEX_ATTENTION_EXPERIMENTAL") == "1": | ||
param1 = [16, 32, 64, 128, 256, 512] | ||
param2 = [16, 32, 64, 128, 256, 512] | ||
param3 = [2, 4, 8, 16] | ||
param4 = [1] | ||
|
||
# Generate full search space | ||
configs = list(itertools.product(param1, param2, param3, param4)) | ||
|
||
# Mark SPARSE_KV_BLOCK_SIZE & SPARSE_Q_BLOCK_SIZE as static shapes and add guards. | ||
SPARSE_KV_BLOCK_SIZE = V.graph.sizevars.evaluate_static_shape(SPARSE_KV_BLOCK_SIZE) | ||
SPARSE_Q_BLOCK_SIZE = V.graph.sizevars.evaluate_static_shape(SPARSE_Q_BLOCK_SIZE) | ||
|
||
# ROCm specific considerations | ||
if torch.version.hip: | ||
kernel_options["kpack"] = 2 | ||
|
||
# Note, we don't need to pass in the captured buffers explicitly | ||
# because they're implicitly added by the score_mod function | ||
# We do need to explicitly pass it in for autotuning though. | ||
|
@@ -1234,33 +1250,67 @@ def flex_attention( | |
cur_kernel_options.setdefault("SPARSE_Q_BLOCK_SIZE", SPARSE_Q_BLOCK_SIZE) | ||
cur_kernel_options.setdefault("SPARSE_KV_BLOCK_SIZE", SPARSE_KV_BLOCK_SIZE) | ||
|
||
error = flex_attention_template.maybe_append_choice( | ||
choices=choices, | ||
input_nodes=[ | ||
query, | ||
key, | ||
value, | ||
logsumexp, | ||
kv_num_blocks, | ||
kv_indices, | ||
full_kv_num_blocks, | ||
full_kv_indices, | ||
], | ||
layout=layout, | ||
subgraphs=[ | ||
subgraph_buffer, | ||
mask_graph_buffer, | ||
], | ||
mutated_inputs=[ | ||
logsumexp, | ||
], | ||
num_stages=num_stages, | ||
num_warps=num_warps, | ||
call_sizes=query.get_size(), | ||
**cur_kernel_options, | ||
) | ||
if error is not None and len(configs) == 1: | ||
raise error | ||
if os.getenv("TORCHINDUCTOR_EXHAUSTIVE_FLEX_ATTENTION_EXPERIMENTAL") == "1": | ||
for mfma in [0, 16]: | ||
for wpeu in [0, 1, 2, 4, 8]: | ||
cur_kernel_options["waves_per_eu"] = wpeu | ||
cur_kernel_options["matrix_instr_non_kdim"] = mfma | ||
error = flex_attention_template.maybe_append_choice( | ||
choices=choices, | ||
input_nodes=[ | ||
query, | ||
key, | ||
value, | ||
logsumexp, | ||
kv_num_blocks, | ||
kv_indices, | ||
full_kv_num_blocks, | ||
full_kv_indices, | ||
], | ||
layout=layout, | ||
subgraphs=[ | ||
subgraph_buffer, | ||
mask_graph_buffer, | ||
], | ||
mutated_inputs=[ | ||
logsumexp, | ||
], | ||
num_stages=num_stages, | ||
num_warps=num_warps, | ||
call_sizes=query.get_size(), | ||
**cur_kernel_options, | ||
) | ||
if error is not None and len(configs) == 1: | ||
raise error | ||
else: | ||
error = flex_attention_template.maybe_append_choice( | ||
choices=choices, | ||
input_nodes=[ | ||
query, | ||
key, | ||
value, | ||
logsumexp, | ||
kv_num_blocks, | ||
kv_indices, | ||
full_kv_num_blocks, | ||
full_kv_indices, | ||
], | ||
layout=layout, | ||
subgraphs=[ | ||
subgraph_buffer, | ||
mask_graph_buffer, | ||
], | ||
mutated_inputs=[ | ||
logsumexp, | ||
], | ||
num_stages=num_stages, | ||
num_warps=num_warps, | ||
call_sizes=query.get_size(), | ||
**cur_kernel_options, | ||
) | ||
if error is not None and len(configs) == 1: | ||
raise error | ||
|
||
inputs_for_autotuning = ( | ||
[ | ||
query, | ||
|
@@ -2257,13 +2307,15 @@ def flex_attention_backward(*args, **kwargs): | |
configs.extend( | ||
[ | ||
(BLOCK1, BLOCK2, w, s) | ||
for BLOCK1 in [32, 64] | ||
for BLOCK2 in [32, 64, 128] | ||
for w in ([4, 8] if BLOCK1 >= 128 or BLOCK2 >= 128 else [4]) | ||
for BLOCK1 in [16, 32, 64, 128, 256, 512] | ||
for BLOCK2 in [16, 32, 64, 128, 256, 512] | ||
for w in ([4, 8] if BLOCK1 >= 128 or BLOCK2 >= 128 else [4, 8]) | ||
for s in num_stages_list | ||
if BLOCK2 % BLOCK1 == 0 | ||
] | ||
) | ||
|
||
|
||
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. extra newlines? |
||
original_kernel_options = kernel_options.copy() | ||
for BLOCK1, BLOCK2, num_warps, num_stages in configs: | ||
if ( | ||
|
@@ -2273,9 +2325,6 @@ def flex_attention_backward(*args, **kwargs): | |
or SPARSE_Q_BLOCK_SIZE % BLOCK2 != 0 | ||
): | ||
continue | ||
if num_warps == 8: | ||
# Working around https://github.com/pytorch/pytorch/issues/141603 | ||
continue | ||
|
||
# Performance tuning | ||
cur_kernel_options = original_kernel_options.copy() | ||
|
@@ -2287,43 +2336,47 @@ def flex_attention_backward(*args, **kwargs): | |
cur_kernel_options.setdefault("SPARSE_Q_BLOCK_SIZE", SPARSE_Q_BLOCK_SIZE) | ||
cur_kernel_options.setdefault("SPARSE_KV_BLOCK_SIZE", SPARSE_KV_BLOCK_SIZE) | ||
|
||
flex_attention_backward_template.maybe_append_choice( | ||
choices=choices, | ||
input_nodes=[ | ||
query, | ||
key, | ||
value, | ||
logsumexp, | ||
delta, | ||
grad_out, | ||
grad_query, | ||
broadcasted_grad_value, | ||
kv_num_blocks, | ||
kv_indices, | ||
q_num_blocks, | ||
q_indices, | ||
full_kv_num_blocks, | ||
full_kv_indices, | ||
full_q_num_blocks, | ||
full_q_indices, | ||
], | ||
layout=layout_broadcasted_k, # We use store_output only for grad_key | ||
subgraphs=[ | ||
fw_subgraph_buffer, | ||
joint_outputs.grad_input, | ||
mask_graph_buffer, | ||
joint_outputs.captured_grads_compute, | ||
], | ||
mutated_inputs=[ | ||
grad_query, | ||
broadcasted_grad_value, | ||
*joint_outputs.mutated_grads, | ||
], | ||
call_sizes=query.get_size() + key.get_size()[1:3], | ||
num_stages=num_stages, | ||
num_warps=num_warps, | ||
**cur_kernel_options, | ||
) | ||
for wpeu in [0, 1, 2, 4, 8]: | ||
for mfma in [0, 16]: | ||
cur_kernel_options["waves_per_eu"] = wpeu | ||
cur_kernel_options["matrix_instr_non_kdim"] = mfma | ||
flex_attention_backward_template.maybe_append_choice( | ||
choices=choices, | ||
input_nodes=[ | ||
query, | ||
key, | ||
value, | ||
logsumexp, | ||
delta, | ||
grad_out, | ||
grad_query, | ||
broadcasted_grad_value, | ||
kv_num_blocks, | ||
kv_indices, | ||
q_num_blocks, | ||
q_indices, | ||
full_kv_num_blocks, | ||
full_kv_indices, | ||
full_q_num_blocks, | ||
full_q_indices, | ||
], | ||
layout=layout_broadcasted_k, # We use store_output only for grad_key | ||
subgraphs=[ | ||
fw_subgraph_buffer, | ||
joint_outputs.grad_input, | ||
mask_graph_buffer, | ||
joint_outputs.captured_grads_compute, | ||
], | ||
mutated_inputs=[ | ||
grad_query, | ||
broadcasted_grad_value, | ||
*joint_outputs.mutated_grads, | ||
], | ||
call_sizes=query.get_size() + key.get_size()[1:3], | ||
num_stages=num_stages, | ||
num_warps=num_warps, | ||
**cur_kernel_options, | ||
) | ||
inputs_for_autotuning = ( | ||
[ | ||
query, | ||
|
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
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.
@jataylo Consider avoiding repetition by doing something like: