-
Notifications
You must be signed in to change notification settings - Fork 336
metal lowbit kernels: qmv_fast optimization #2167
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
Merged
Merged
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -21,3 +21,6 @@ | |
|
||
- func: int7mm | ||
file: int7mm.metal | ||
|
||
- func: qmv_fast | ||
file: qmv_fast.metal |
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 |
---|---|---|
|
@@ -64,12 +64,11 @@ using namespace metal; | |
@param [in] B is weight matrix of size M x K. Each byte contains 2 4-bit | ||
values, along K dim, packed together. | ||
@param [in] scales_ptr is scales ptr corresponding each | ||
output channel x groups. These are packed as [num_groups = ceil(K / group_size), N]. N = output | ||
output channel x groups. These are packed as [N, num_groups = ceil(K / group_size)]. N = output | ||
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. would this work for gemm as well? |
||
channels. | ||
@param [in] zeros_ptr is zero points corresponding each | ||
output channel x groups. These are packed as [num_groups = ceil(K / group_size), N]. N = output | ||
output channel x groups. These are packed as [N, num_groups = ceil(K / group_size)]. N = output | ||
channels. | ||
output channel x groups. These are packed as [num_groups = ceil(K / group_size), N, 2]. N = output | ||
@param [out] output_data is output matrix of size M x N. | ||
@param [in] sizes array contains values of M, K and N. | ||
@param [in] thread_index is global thread id. | ||
|
@@ -89,6 +88,7 @@ kernel void int4pack_mm(constant T *A [[buffer(0)]], | |
constexpr uint k_pack_factor = 2; | ||
const uint K = sizes.y; | ||
const uint N = sizes.z; | ||
const uint num_groups = (K + group_size - 1) / group_size; | ||
uint n = thread_index.x; // 0..N/4-1 | ||
uint m = thread_index.z; // 0..M | ||
n = n / threads_per_channel; | ||
|
@@ -113,13 +113,19 @@ kernel void int4pack_mm(constant T *A [[buffer(0)]], | |
// Find specific group to which channels handled by this thread | ||
// belong. | ||
uint k_block_index = k / group_size; | ||
uint scales_group_offset = (k_block_index * N + n); | ||
uint scales_group_offset = (n * num_groups + k_block_index); | ||
|
||
vecT scales = | ||
(reinterpret_cast<constant vecT *>(scales_ptr + scales_group_offset))[0]; | ||
vecT(scales_ptr[scales_group_offset], | ||
scales_ptr[scales_group_offset + num_groups], | ||
scales_ptr[scales_group_offset + 2 * num_groups], | ||
scales_ptr[scales_group_offset + 3 * num_groups]); | ||
// Adding zero point results in 10% perf penalty. | ||
vecT zeros = | ||
(reinterpret_cast<constant vecT *>(zeros_ptr + scales_group_offset))[0]; | ||
vecT(zeros_ptr[scales_group_offset], | ||
zeros_ptr[scales_group_offset + num_groups], | ||
zeros_ptr[scales_group_offset + 2 * num_groups], | ||
zeros_ptr[scales_group_offset + 3 * num_groups]); | ||
float4 zeros_float = float4(zeros); | ||
|
||
float4 a_val = float4(A_ptr[k / 4]); | ||
|
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
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.
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 am definitely surprised that this is better