-
Notifications
You must be signed in to change notification settings - Fork 12.4k
opencl: add set_rows
for f16
and f32
#14547
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -88,6 +88,7 @@ set(GGML_OPENCL_KERNELS | |
rms_norm | ||
rope | ||
scale | ||
set_rows | ||
sigmoid | ||
silu | ||
softmax_4_f32 | ||
|
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,87 @@ | ||
#pragma OPENCL EXTENSION cl_khr_fp16 : enable | ||
|
||
kernel void kernel_set_rows_f32( | ||
global char * src0, | ||
ulong offset0, | ||
global char * src1, | ||
ulong offset1, | ||
global char * dst, | ||
ulong offsetd, | ||
int ne01, | ||
ulong nb01, | ||
ulong nb02, | ||
ulong nb03, | ||
int ne11, | ||
int ne12, | ||
ulong nb10, | ||
ulong nb11, | ||
ulong nb12, | ||
int nblk0, | ||
ulong nb1, | ||
ulong nb2, | ||
ulong nb3 | ||
) { | ||
src0 = src0 + offset0; | ||
src1 = src1 + offset1; | ||
dst = dst + offsetd; | ||
|
||
int i03 = get_group_id(2); | ||
int i02 = get_group_id(1); | ||
int i01 = get_group_id(0); | ||
|
||
int i12 = i03%ne12; | ||
int i11 = i02%ne11; | ||
|
||
int i10 = i01; | ||
long i1 = ((global long *)(src1 + i10*nb10 + i11*nb11 + i12*nb12))[0]; | ||
|
||
global float * dst_row = (global float *) (dst + i1*nb1 + i02*nb2 + i03*nb3); | ||
global float * src_row = (global float *) (src0 + i01*nb01 + i02*nb02 + i03*nb03); | ||
|
||
for (int ind = get_local_id(0); ind < nblk0; ind += get_local_size(0)) { | ||
dst_row[ind] = (float)src_row[ind]; | ||
} | ||
} | ||
|
||
kernel void kernel_set_rows_f16( | ||
global char * src0, | ||
ulong offset0, | ||
global char * src1, | ||
ulong offset1, | ||
global char * dst, | ||
ulong offsetd, | ||
int ne01, | ||
ulong nb01, | ||
ulong nb02, | ||
ulong nb03, | ||
int ne11, | ||
int ne12, | ||
ulong nb10, | ||
ulong nb11, | ||
ulong nb12, | ||
int nblk0, | ||
ulong nb1, | ||
ulong nb2, | ||
ulong nb3 | ||
) { | ||
src0 = src0 + offset0; | ||
src1 = src1 + offset1; | ||
dst = dst + offsetd; | ||
|
||
int i03 = get_group_id(2); | ||
int i02 = get_group_id(1); | ||
int i01 = get_group_id(0); | ||
|
||
int i12 = i03%ne12; | ||
int i11 = i02%ne11; | ||
|
||
int i10 = i01; | ||
long i1 = ((global long *)(src1 + i10*nb10 + i11*nb11 + i12*nb12))[0]; | ||
|
||
global half * dst_row = (global half *) (dst + i1*nb1 + i02*nb2 + i03*nb3); | ||
global float * src_row = (global float *) (src0 + i01*nb01 + i02*nb02 + i03*nb03); | ||
|
||
for (int ind = get_local_id(0); ind < nblk0; ind += get_local_size(0)) { | ||
dst_row[ind] = src_row[ind]; | ||
} | ||
} |
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.
Note that implementing it like this won't be very efficient. This dedicates 256 threads for each row of data. So for small rows with less than 256 elements, there will be wasted resources. For example, when FA is disabled,
ggml_set_rows()
is used with rows of 1 element (due to the V cache being transposed), so 255 out of the 256 local threads will be idle.That's why in the Metal implementation I did "threadgroup batching" so that the local threads can work on multiple rows. Might want to consider implementing it here too for improved performance.
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.
Thanks a lot for the suggestion - it makes a good point. Looking into this.