-
Notifications
You must be signed in to change notification settings - Fork 10.6k
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
vulkan: initial support for IQ1_S and IQ1_M quantizations #11528
Draft
remyoudompheng
wants to merge
3
commits into
ggerganov:master
Choose a base branch
from
remyoudompheng:vulkan-iq1
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.
+683
−10
Draft
Changes from all 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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,42 @@ | ||
#version 450 | ||
|
||
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require | ||
|
||
#include "dequant_head.comp" | ||
|
||
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in; | ||
|
||
layout (binding = 0) readonly buffer A {block_iq1_m data_a[];}; | ||
layout (binding = 1) writeonly buffer D {D_TYPE data_b[];}; | ||
|
||
void main() { | ||
// Each thread handles 1 subblock (32 values with 2 scales) | ||
const uint ib = gl_WorkGroupID.x * 32 + gl_LocalInvocationID.x / 8; | ||
|
||
init_iq_shmem(gl_WorkGroupSize); | ||
|
||
if (ib >= p.nel / 256) { | ||
return; | ||
} | ||
|
||
const uint ib32 = gl_LocalInvocationID.x % 8; | ||
const uint ib64 = ib32 / 2; | ||
const uint b_idx = 256 * ib + 32 * ib32; | ||
|
||
const uint16_t[4] scales = data_a[ib].scales; | ||
const u16vec4 s = u16vec4(scales[0], scales[1], scales[2], scales[3]) >> 12; | ||
const float d = float(uint16BitsToHalf(s.x | (s.y << 4) | (s.z << 8) | (s.w << 12))); | ||
|
||
const uint sc = data_a[ib].scales[ib64]; | ||
[[unroll]] for (int l = 0; l < 4; ++l) { | ||
const uint ib16 = 2 * ib32 + l / 2; | ||
const float dl = d * (2 * bitfieldExtract(sc, 3 * int(ib16 & 3), 3) + 1); | ||
const uint qh = data_a[ib].qh[ib16] >> (4 * (l & 1)); | ||
const uint qs = data_a[ib].qs[4 * ib32 + l]; | ||
const float delta = ((qh & 8) != 0) ? -IQ1M_DELTA : IQ1M_DELTA; | ||
const int16_t grid = int16_t(iq1s_grid[qs | ((qh & 7) << 8)]); | ||
[[unroll]] for (int j = 0; j < 8; ++j) { | ||
data_b[b_idx + 8 * l + j] = D_TYPE(dl * (bitfieldExtract(grid, 2*j, 2) + delta)); | ||
} | ||
} | ||
} |
This file contains 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,35 @@ | ||
#version 450 | ||
|
||
#include "dequant_head.comp" | ||
|
||
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in; | ||
|
||
layout (binding = 0) readonly buffer A {block_iq1_s data_a[];}; | ||
layout (binding = 1) writeonly buffer D {D_TYPE data_b[];}; | ||
|
||
void main() { | ||
// Each thread handles 1 subblock (32 values with 2 scales) | ||
const uint ib = gl_WorkGroupID.x * 32 + gl_LocalInvocationID.x / 8; | ||
|
||
init_iq_shmem(gl_WorkGroupSize); | ||
|
||
if (ib >= p.nel / 256) { | ||
return; | ||
} | ||
|
||
const uint ib32 = gl_LocalInvocationID.x % 8; | ||
const uint b_idx = 256 * ib + 32 * ib32; | ||
|
||
uint qh = data_a[ib].qh[ib32]; | ||
const float d = float(data_a[ib].d); | ||
const float dl = d * float(2 * bitfieldExtract(qh, 12, 3) + 1); | ||
const float delta = ((qh & 0x8000) != 0) ? -IQ1S_DELTA : IQ1S_DELTA; | ||
[[unroll]] for (uint l = 0; l < 4; ++l) { | ||
const uint qs = data_a[ib].qs[4 * ib32 + l]; | ||
const uint hi = bitfieldExtract(qh, 3 * int(l), 3); | ||
const int16_t grid = int16_t(iq1s_grid[qs | (hi << 8)]); | ||
[[unroll]] for (int j = 0; j < 8; ++j) { | ||
data_b[b_idx + 8 * l + j] = D_TYPE(dl * (bitfieldExtract(grid, 2*j, 2) + delta)); | ||
} | ||
} | ||
} |
This file contains 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 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 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
82 changes: 82 additions & 0 deletions
82
ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_iq1_m.comp
This file contains 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,82 @@ | ||
#version 450 | ||
#extension GL_EXT_shader_explicit_arithmetic_types_int32 : require | ||
|
||
#include "mul_mat_vec_base.comp" | ||
|
||
layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; | ||
|
||
FLOAT_TYPE temp[NUM_COLS][NUM_ROWS]; | ||
|
||
void calc_superblock(const uint a_offset, const uint b_offset, const uint ib32, const uint i, const uint num_blocks_per_row, const uint first_row, const uint num_rows) { | ||
const uint y_idx = i * QUANT_K + 32 * ib32; | ||
|
||
uint ibi = a_offset / QUANT_K + first_row * num_blocks_per_row + i; | ||
[[unroll]] for (uint n = 0; n < num_rows; ++n) { | ||
const uint16_t[4] scales = data_a[ibi].scales; | ||
const u16vec4 s = u16vec4(scales[0], scales[1], scales[2], scales[3]) >> 12; | ||
const float d = float(uint16BitsToHalf(s.x | (s.y << 4) | (s.z << 8) | (s.w << 12))); | ||
|
||
const uint sc = data_a[ibi].scales[ib32 / 2] >> (6 * (ib32 & 1)); | ||
[[unroll]] for (uint l = 0; l < 4; ++l) { | ||
const uint qh = data_a[ibi].qh[2 * ib32 + l / 2] >> (4 * (l&1)); | ||
const uint qs = data_a[ibi].qs[4 * ib32 + l]; | ||
const float delta = ((qh & 8) != 0) ? -IQ1M_DELTA : IQ1M_DELTA; | ||
const float dl = d * (2 * bitfieldExtract(sc, 3 * int(l / 2), 3) + 1); | ||
|
||
const int16_t grid = int16_t(iq1s_grid[qs | ((qh & 7) << 8)]); | ||
|
||
[[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { | ||
vec4 b0 = vec4(data_b_v4[(j*p.batch_stride_b + b_offset + y_idx) / 4 + 2*l + 0]); | ||
vec4 b4 = vec4(data_b_v4[(j*p.batch_stride_b + b_offset + y_idx) / 4 + 2*l + 1]); | ||
|
||
FLOAT_TYPE sum = FLOAT_TYPE(0.0); | ||
[[unroll]] for (int k = 0; k < 4; ++k) { | ||
sum = fma(FLOAT_TYPE(b0[k]), bitfieldExtract(grid, 2 * k, 2) + delta, | ||
fma(FLOAT_TYPE(b4[k]), bitfieldExtract(grid, 8 + 2 * k, 2) + delta, sum)); | ||
} | ||
temp[j][n] = fma(dl, sum, temp[j][n]); | ||
} | ||
} | ||
ibi += num_blocks_per_row; | ||
} | ||
} | ||
|
||
void compute_outputs(const uint32_t first_row, const uint32_t num_rows) { | ||
uint a_offset, b_offset, d_offset; | ||
get_offsets(a_offset, b_offset, d_offset); | ||
|
||
const uint num_blocks_per_row = p.ncols / QUANT_K; | ||
|
||
// 8 threads are used to process each block | ||
const uint blocks_per_wg = gl_WorkGroupSize.x/8; | ||
const uint tid = gl_LocalInvocationID.x; | ||
const uint itid = tid % 8; // 0...7 | ||
const uint ix = tid / 8; | ||
|
||
[[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { | ||
[[unroll]] for (uint i = 0; i < NUM_ROWS; ++i) { | ||
temp[j][i] = FLOAT_TYPE(0); | ||
} | ||
} | ||
|
||
[[unroll]] for (uint i = ix; i < num_blocks_per_row; i += blocks_per_wg) | ||
calc_superblock(a_offset, b_offset, itid, i, num_blocks_per_row, first_row, num_rows); | ||
|
||
reduce_result(temp, d_offset, first_row, num_rows, tid); | ||
} | ||
|
||
void main() { | ||
const uint first_row = NUM_ROWS * (gl_WorkGroupID.x + gl_NumWorkGroups.x * gl_WorkGroupID.z); | ||
|
||
init_iq_shmem(gl_WorkGroupSize); | ||
|
||
// do NUM_ROWS at a time, unless there aren't enough remaining rows | ||
if (first_row + NUM_ROWS <= p.stride_d) { | ||
compute_outputs(first_row, NUM_ROWS); | ||
} else { | ||
if (first_row >= p.stride_d) { | ||
return; | ||
} | ||
compute_outputs(first_row, p.stride_d - first_row); | ||
} | ||
} |
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.
It would be nice to defined a "NEED_INIT_IQ_SHMEM" macro each place init_iq_shmem is defined, and then all the #ifs can be simple.