-
Notifications
You must be signed in to change notification settings - Fork 12.4k
metal: SSM_SCAN performance #14743
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
gabe-l-hart
wants to merge
10
commits into
ggml-org:master
Choose a base branch
from
gabe-l-hart:GraniteFourPerf
base: master
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.
+215
−51
Open
metal: SSM_SCAN performance #14743
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ba74a24
feat: Add s_off as a parameter in the args struct
gabe-l-hart 8d5a25d
perf: Parallelize mamba2 SSM_SCAN metal kernel over d_state
gabe-l-hart e16e24b
fix: Update logic to correctly do the multi-layer parallel sum
gabe-l-hart 21db0b5
fix: Correctly size the shared memory bufer and assert expected size …
gabe-l-hart a5334f9
refactor: Compute block offsets once rather than once per token
gabe-l-hart 3866f76
feat: Use local variable for state recursion
gabe-l-hart 641276a
feat: Use a secondary simd_sum instead of a for loop
gabe-l-hart d06d087
feat: Add assertion and comment about relationship between simd size …
gabe-l-hart 80545ef
feat: Parallelize of d_state for mamba-1
gabe-l-hart 16bc059
feat: Parallel sum in SSM_CONV
gabe-l-hart 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2909,7 +2909,26 @@ static bool ggml_metal_encode_node( | |
[encoder setBuffer:id_dst offset:offs_dst atIndex:2]; | ||
[encoder setBytes:&args length:sizeof(args) atIndex:3]; | ||
|
||
[encoder dispatchThreadgroups:MTLSizeMake(ne01, ne1, ne02) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)]; | ||
const int64_t d_state = ne10; | ||
|
||
// One shared memory bucket for each simd group in the threadgroup | ||
if (d_state >= 32) { | ||
const int64_t shmem_size = d_state / 32; | ||
Comment on lines
+2912
to
+2916
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. Should probably be called This is With such small sums, does it still make a measurable difference to use |
||
|
||
// The final simd_sum won't work if the number of simd groups is | ||
// larger than the size of a single simd group. If this case is | ||
// hit at some point, the logic in the second simd_sum could be | ||
// expanded to handle this with one more sequential simd_sum to | ||
// collapse simd group sums another time. | ||
GGML_ASSERT(shmem_size <= 32); | ||
|
||
// One thread pre element in d_state | ||
GGML_ASSERT(d_state <= (int64_t)pipeline.maxTotalThreadsPerThreadgroup); | ||
|
||
[encoder setThreadgroupMemoryLength:(shmem_size)*sizeof(float) atIndex:0]; | ||
} | ||
|
||
[encoder dispatchThreadgroups:MTLSizeMake(ne01, ne1, ne02) threadsPerThreadgroup:MTLSizeMake(d_state, 1, 1)]; | ||
} break; | ||
case GGML_OP_SSM_SCAN: | ||
{ | ||
|
@@ -2986,6 +3005,7 @@ static bool ggml_metal_encode_node( | |
/*.n_group =*/ n_group, | ||
/*.n_seq_tokens =*/ n_seq_tokens, | ||
/*.n_seqs =*/ n_seqs, | ||
/*.s_off =*/ ggml_nelements(src1) * sizeof(float), | ||
/*.nb01 =*/ nb01, | ||
/*.nb02 =*/ nb02, | ||
/*.nb03 =*/ nb03, | ||
|
@@ -3014,12 +3034,29 @@ static bool ggml_metal_encode_node( | |
[encoder setBuffer:id_dst offset:offs_dst atIndex:7]; | ||
[encoder setBytes:&args length:sizeof(args) atIndex:8]; | ||
|
||
// One shared memory bucket for each simd group in the threadgroup | ||
if (d_state >= 32) { | ||
const int64_t shmem_size = d_state / 32; | ||
|
||
// The final simd_sum won't work if the number of simd groups is | ||
// larger than the size of a single simd group. If this case is | ||
// hit at some point, the logic in the second simd_sum could be | ||
// expanded to handle this with one more sequential simd_sum to | ||
// collapse simd group sums another time. | ||
GGML_ASSERT(shmem_size <= 32); | ||
|
||
// One thread pre element in d_state | ||
GGML_ASSERT(d_state <= (int64_t)pipeline.maxTotalThreadsPerThreadgroup); | ||
|
||
[encoder setThreadgroupMemoryLength:(shmem_size)*sizeof(float) atIndex:0]; | ||
} | ||
|
||
if (ne30 == 1) { | ||
// Mamba-2 | ||
[encoder dispatchThreadgroups:MTLSizeMake(d_inner, n_head, n_seqs) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)]; | ||
[encoder dispatchThreadgroups:MTLSizeMake(d_inner, n_head, n_seqs) threadsPerThreadgroup:MTLSizeMake(d_state, 1, 1)]; | ||
} else { | ||
GGML_ASSERT(d_inner == 1); | ||
[encoder dispatchThreadgroups:MTLSizeMake(n_head, n_seqs, 1) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)]; | ||
[encoder dispatchThreadgroups:MTLSizeMake(n_head, n_seqs, 1) threadsPerThreadgroup:MTLSizeMake(d_state, 1, 1)]; | ||
} | ||
} break; | ||
case GGML_OP_RWKV_WKV6: | ||
|
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
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.
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.
I'm not sure this is actually necessary. I did this when initially trying to emulate the CUDA kernel better which passes this as an arg. It does seem like it might be slightly faster to avoid computing this in the kernel, though that could be offset by the latency of an additional
int64_t
being passed to the device?