Repository navigation
[ASM] [HIP] [JIT] [gfx942] Optimize long-context FMHA with split-KV #5592
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
amd-yashagar
wants to merge
17
commits into
main
Choose a base branch
from
feat/gfx942-fmha-hd192-split-asm
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 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b320752
Optimize gfx942 FMHA with integrated ASM split-K
amd-yashagar b38b082
Fix split-KV review comments and ruff pre-check failures
amd-yashagar 3cff79f
Check split-KV producer status before the LSE combine
amd-yashagar c610f82
Align gfx942 split-KV tests with the aiter op-test format
amd-yashagar ade9d6a
Assert public auto-select and skip MI308 in split-KV tests
amd-yashagar 3469ac0
Expose num_splits on fmha_v3_varlen_fwd and drop the narrow split-KV op
amd-yashagar febadee
Fail split-KV mismatches and cover graph plus non-LSE paths
amd-yashagar d7c3e42
Share packed-varlen test inputs across split-KV checks
amd-yashagar 328246d
Merge branch 'main' into feat/gfx942-fmha-hd192-split-asm
amd-yashagar d0b16eb
Merge branch 'main' into feat/gfx942-fmha-hd192-split-asm
amd-yashagar e75b4c1
Merge branch 'main' into feat/gfx942-fmha-hd192-split-asm
amd-yashagar e27b84d
Merge branch 'main' into feat/gfx942-fmha-hd192-split-asm
amd-yashagar 26e1adf
fix(mha): address hd192 split-KV review comments
amd-yashagar a366e32
test(mha): cover split-KV auto upper bound and drop ticket wording
amd-yashagar 087526a
fix(mha): clamp empty hd192 split-KV partitions in ASM
amd-yashagar c434f36
Merge branch 'main' into feat/gfx942-fmha-hd192-split-asm
amd-yashagar 24eb312
fix(mha): handle empty split-KV inputs
amd-yashagar 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // Copyright (C) 2024-2026, Advanced Micro Devices, Inc. All rights reserved. | ||
|
|
||
| #include <hip/hip_bfloat16.h> | ||
|
|
||
| #include "aiter_hip_common.h" | ||
| #include "mha_fwd.h" | ||
|
|
||
| namespace aiter { | ||
| namespace { | ||
|
|
||
| constexpr int kValueDim = 128; | ||
| constexpr int kCombineBlock = 256; | ||
| constexpr int kWaveSize = 64; | ||
| constexpr int kRowsPerBlock = kCombineBlock / kWaveSize; | ||
|
|
||
| template <int NumSplits> | ||
| __global__ __launch_bounds__(kCombineBlock) void fmha_fwd_v3_splitkv_combine_kernel( | ||
| const hip_bfloat16* __restrict__ partial_out, | ||
| const float* __restrict__ partial_lse, | ||
| hip_bfloat16* __restrict__ out, | ||
| float* __restrict__ lse_out, | ||
| int seqlen_q, | ||
| int num_heads, | ||
| int64_t out_token_stride, | ||
| int64_t out_head_stride) | ||
| { | ||
| const int lane = threadIdx.x & (kWaveSize - 1); | ||
| const int wave = threadIdx.x / kWaveSize; | ||
| const int row = blockIdx.x * kRowsPerBlock + wave; | ||
| const int row_count = seqlen_q * num_heads; | ||
| if(row >= row_count) | ||
| return; | ||
|
|
||
| const int token = row / num_heads; | ||
| const int head = row - token * num_heads; | ||
| const int64_t partial_plane = static_cast<int64_t>(row_count) * kValueDim; | ||
| const int64_t lse_plane = static_cast<int64_t>(num_heads) * seqlen_q; | ||
| const int64_t lse_index = static_cast<int64_t>(head) * seqlen_q + token; | ||
|
|
||
| float weights[NumSplits]; | ||
| if(lane == 0) | ||
| { | ||
| float lse_max = partial_lse[lse_index]; | ||
| #pragma unroll | ||
| for(int split = 1; split < NumSplits; ++split) | ||
| lse_max = fmaxf(lse_max, partial_lse[split * lse_plane + lse_index]); | ||
|
|
||
| float denominator = 0.0f; | ||
| #pragma unroll | ||
| for(int split = 0; split < NumSplits; ++split) | ||
| { | ||
| weights[split] = expf(partial_lse[split * lse_plane + lse_index] - lse_max); | ||
| denominator += weights[split]; | ||
| } | ||
| denominator = fmaxf(denominator, 1.0e-20f); | ||
|
|
||
| const float inverse = 1.0f / denominator; | ||
| #pragma unroll | ||
| for(int split = 0; split < NumSplits; ++split) | ||
| weights[split] *= inverse; | ||
|
|
||
| if(lse_out != nullptr) | ||
| lse_out[lse_index] = lse_max + logf(denominator); | ||
| } | ||
|
|
||
| #pragma unroll | ||
| for(int split = 0; split < NumSplits; ++split) | ||
| weights[split] = __shfl(weights[split], 0); | ||
|
|
||
| const int64_t partial_index = static_cast<int64_t>(row) * kValueDim; | ||
| const int64_t out_index = static_cast<int64_t>(token) * out_token_stride + | ||
| static_cast<int64_t>(head) * out_head_stride; | ||
| for(int value = lane; value < kValueDim; value += kWaveSize) | ||
| { | ||
| float result = 0.0f; | ||
| #pragma unroll | ||
| for(int split = 0; split < NumSplits; ++split) | ||
| { | ||
| result += | ||
| weights[split] * | ||
| static_cast<float>(partial_out[split * partial_plane + partial_index + value]); | ||
| } | ||
| out[out_index + value] = hip_bfloat16(result); | ||
| } | ||
| } | ||
|
|
||
| template <int NumSplits> | ||
| void launch_combine(const hip_bfloat16* partial_out, | ||
| const float* partial_lse, | ||
| hip_bfloat16* out, | ||
| float* lse, | ||
| int seqlen_q, | ||
| int num_heads, | ||
| int64_t out_token_stride, | ||
| int64_t out_head_stride, | ||
| dim3 grid, | ||
| hipStream_t stream) | ||
| { | ||
| hipLaunchKernelGGL((fmha_fwd_v3_splitkv_combine_kernel<NumSplits>), | ||
| grid, | ||
| dim3(kCombineBlock), | ||
| 0, | ||
| stream, | ||
| partial_out, | ||
| partial_lse, | ||
| out, | ||
| lse, | ||
| seqlen_q, | ||
| num_heads, | ||
| out_token_stride, | ||
| out_head_stride); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| void launch_fmha_fwd_v3_splitkv_combine(const void* partial_out, | ||
| const void* partial_lse, | ||
| void* out, | ||
| void* lse, | ||
| int seqlen_q, | ||
| int num_heads, | ||
| int num_splits, | ||
| int64_t out_token_stride, | ||
| int64_t out_head_stride, | ||
| hipStream_t stream) | ||
| { | ||
| AITER_CHECK(num_splits >= kFmhaHd192SplitKvMinSplits && | ||
| num_splits <= kFmhaHd192SplitKvMaxSplits, | ||
| "unsupported fmha_fwd_v3_splitkv combine split count"); | ||
|
|
||
| const int row_count = seqlen_q * num_heads; | ||
| const dim3 grid((row_count + kRowsPerBlock - 1) / kRowsPerBlock); | ||
| const auto* partial_out_ptr = static_cast<const hip_bfloat16*>(partial_out); | ||
| const auto* partial_lse_ptr = static_cast<const float*>(partial_lse); | ||
| auto* out_ptr = static_cast<hip_bfloat16*>(out); | ||
| auto* lse_ptr = static_cast<float*>(lse); | ||
|
|
||
| using combine_launcher = decltype(&launch_combine<2>); | ||
| static constexpr combine_launcher launchers[] = { | ||
| nullptr, | ||
| nullptr, | ||
| &launch_combine<2>, | ||
| &launch_combine<3>, | ||
| &launch_combine<4>, | ||
| &launch_combine<5>, | ||
| &launch_combine<6>, | ||
| &launch_combine<7>, | ||
| &launch_combine<8>, | ||
| }; | ||
| launchers[num_splits](partial_out_ptr, | ||
| partial_lse_ptr, | ||
| out_ptr, | ||
| lse_ptr, | ||
| seqlen_q, | ||
| num_heads, | ||
| out_token_stride, | ||
| out_head_stride, | ||
| grid, | ||
| stream); | ||
| HIP_CALL(hipGetLastError()); | ||
| } | ||
|
|
||
| } // namespace aiter | ||
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.