-
Notifications
You must be signed in to change notification settings - Fork 12.4k
OpenCL: add mul_mat_f16_f32_image
kernel
#14635
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
rmatif
wants to merge
3
commits into
master
Choose a base branch
from
opencl-add-mul-mat-f16-f32-image
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.
Open
Changes from 2 commits
Commits
Show all changes
3 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
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,61 @@ | ||
#pragma OPENCL EXTENSION cl_khr_fp16 : enable | ||
|
||
__constant sampler_t SAMPLER = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST; | ||
|
||
__kernel void mul_mat_f16_f32_image( | ||
__read_only image2d_t A_img, | ||
__read_only image2d_t B_img, | ||
__global float* C_buf, | ||
const ulong c_offset, | ||
const int M, | ||
const int N, | ||
const int K | ||
) { | ||
const int n_4_idx = get_global_id(0); | ||
const int m_idx = get_global_id(1); | ||
|
||
const int n_base = n_4_idx << 2; | ||
|
||
if (n_base >= N || m_idx >= M) { | ||
return; | ||
} | ||
|
||
float4 c_vals = (float4)(0.0f); | ||
const int K_4 = (K + 3) / 4; | ||
|
||
for (int k_4_idx = 0; k_4_idx < K_4; ++k_4_idx) { | ||
int k_base = k_4_idx << 2; | ||
|
||
float4 a_vals = convert_float4(read_imageh(A_img, SAMPLER, (int2)(k_4_idx, m_idx))); | ||
|
||
if (k_base < K) { | ||
float4 b0 = convert_float4(read_imageh(B_img, SAMPLER, (int2)(n_4_idx, k_base + 0))); | ||
c_vals = mad(a_vals.x, b0, c_vals); | ||
} | ||
if (k_base + 1 < K) { | ||
float4 b1 = convert_float4(read_imageh(B_img, SAMPLER, (int2)(n_4_idx, k_base + 1))); | ||
c_vals = mad(a_vals.y, b1, c_vals); | ||
} | ||
if (k_base + 2 < K) { | ||
float4 b2 = convert_float4(read_imageh(B_img, SAMPLER, (int2)(n_4_idx, k_base + 2))); | ||
c_vals = mad(a_vals.z, b2, c_vals); | ||
} | ||
if (k_base + 3 < K) { | ||
float4 b3 = convert_float4(read_imageh(B_img, SAMPLER, (int2)(n_4_idx, k_base + 3))); | ||
c_vals = mad(a_vals.w, b3, c_vals); | ||
} | ||
} | ||
|
||
__global float* C = (__global float*)((__global char*)C_buf + c_offset); | ||
|
||
if (n_base + 3 < N) { | ||
C[(n_base + 0) * M + m_idx] = c_vals.x; | ||
C[(n_base + 1) * M + m_idx] = c_vals.y; | ||
C[(n_base + 2) * M + m_idx] = c_vals.z; | ||
C[(n_base + 3) * M + m_idx] = c_vals.w; | ||
} else { | ||
if (n_base < N) C[n_base * M + m_idx] = c_vals.x; | ||
if (n_base + 1 < N) C[(n_base + 1) * M + m_idx] = c_vals.y; | ||
if (n_base + 2 < N) C[(n_base + 2) * M + m_idx] = c_vals.z; | ||
} | ||
} |
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,29 @@ | ||
#pragma OPENCL EXTENSION cl_khr_fp16 : enable | ||
|
||
__kernel void pack_a_for_image( | ||
__global const half* src_a, | ||
const ulong a_offset, | ||
__write_only image2d_t dest_img, | ||
const int M, | ||
const int K | ||
) { | ||
const int k_4_idx = get_global_id(0); | ||
const int m_idx = get_global_id(1); | ||
|
||
const int k_base = k_4_idx << 2; | ||
|
||
if (k_base >= K || m_idx >= M) { | ||
return; | ||
} | ||
|
||
__global const half* a_ptr = (__global const half*)((__global const char*)src_a + a_offset); | ||
const int a_idx_base = m_idx * K + k_base; | ||
|
||
half4 vals; | ||
vals.x = a_ptr[a_idx_base]; | ||
vals.y = (k_base + 1 < K) ? a_ptr[a_idx_base + 1] : (half)0.0h; | ||
vals.z = (k_base + 2 < K) ? a_ptr[a_idx_base + 2] : (half)0.0h; | ||
vals.w = (k_base + 3 < K) ? a_ptr[a_idx_base + 3] : (half)0.0h; | ||
|
||
write_imageh(dest_img, (int2)(k_4_idx, m_idx), vals); | ||
} |
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,28 @@ | ||
#pragma OPENCL EXTENSION cl_khr_fp16 : enable | ||
|
||
__kernel void pack_b_for_image( | ||
__global const float* src_b, | ||
const ulong b_offset, | ||
__write_only image2d_t dest_img, | ||
const int K, | ||
const int N | ||
) { | ||
const int n_4_idx = get_global_id(0); | ||
const int k_idx = get_global_id(1); | ||
|
||
const int n_base = n_4_idx << 2; | ||
|
||
if (n_base >= N || k_idx >= K) { | ||
return; | ||
} | ||
|
||
__global const float* b_ptr = (__global const float*)((__global const char*)src_b + b_offset); | ||
|
||
half4 vals; | ||
vals.x = convert_half(b_ptr[n_base * K + k_idx]); | ||
vals.y = (n_base + 1 < N) ? convert_half(b_ptr[(n_base + 1) * K + k_idx]) : (half)0.0h; | ||
vals.z = (n_base + 2 < N) ? convert_half(b_ptr[(n_base + 2) * K + k_idx]) : (half)0.0h; | ||
vals.w = (n_base + 3 < N) ? convert_half(b_ptr[(n_base + 3) * K + k_idx]) : (half)0.0h; | ||
|
||
write_imageh(dest_img, (int2)(n_4_idx, k_idx), vals); | ||
} |
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.