forked from ggml-org/llama.cpp
-
Notifications
You must be signed in to change notification settings - Fork 31
vulkan: add Q1_0_g128 (1-bit ternary) shader support #9
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
Closed
Closed
Changes from all commits
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ vec2 dequantize(uint ib, uint iqs, uint a_offset) { | |
| } | ||
| #endif | ||
|
|
||
| #if defined(DATA_A_Q1_0)vec2 dequantize(uint ib, uint iqs, uint a_offset) { const uint byte_idx = iqs / 8; const uint bit_idx = iqs % 8; const uint bits = uint(data_a[a_offset + ib].qs[byte_idx]); const float sign0 = ((bits >> bit_idx) & 1) == 1 ? 1.0f : -1.0f; const uint byte_idx2 = (iqs + 1) / 8; const uint bit_idx2 = (iqs + 1) % 8; const uint bits2 = uint(data_a[a_offset + ib].qs[byte_idx2]); const float sign1 = ((bits2 >> bit_idx2) & 1) == 1 ? 1.0f : -1.0f; return vec2(sign0, sign1);}vec4 dequantize4(uint ib, uint iqs, uint a_offset) { const uint byte_idx0 = iqs / 8; const uint bit_idx0 = iqs % 8; const uint bits0 = uint(data_a[a_offset + ib].qs[byte_idx0]); const float s0 = ((bits0 >> bit_idx0) & 1) == 1 ? 1.0f : -1.0f; const uint byte_idx1 = (iqs + 1) / 8; const uint bit_idx1 = (iqs + 1) % 8; const uint bits1 = uint(data_a[a_offset + ib].qs[byte_idx1]); const float s1 = ((bits1 >> bit_idx1) & 1) == 1 ? 1.0f : -1.0f; const uint byte_idx2 = (iqs + 2) / 8; const uint bit_idx2 = (iqs + 2) % 8; const uint bits2 = uint(data_a[a_offset + ib].qs[byte_idx2]); const float s2 = ((bits2 >> bit_idx2) & 1) == 1 ? 1.0f : -1.0f; const uint byte_idx3 = (iqs + 3) / 8; const uint bit_idx3 = (iqs + 3) % 8; const uint bits3 = uint(data_a[a_offset + ib].qs[byte_idx3]); const float s3 = ((bits3 >> bit_idx3) & 1) == 1 ? 1.0f : -1.0f; return vec4(s0, s1, s2, s3);}#endif | ||
| #if defined(DATA_A_Q4_0) | ||
| vec2 dequantize(uint ib, uint iqs, uint a_offset) { | ||
| const uint vui = uint(data_a[a_offset + ib].qs[iqs]); | ||
|
|
@@ -448,7 +449,8 @@ vec2 get_dm(uint ib, uint a_offset) { | |
| } | ||
| #endif | ||
|
|
||
| #if defined(DATA_A_Q4_0) || defined(DATA_A_Q5_0) || defined(DATA_A_Q8_0) || defined(DATA_A_IQ1_S) || defined(DATA_A_IQ2_XXS) || defined(DATA_A_IQ2_XS) || defined(DATA_A_IQ2_S) || defined(DATA_A_IQ3_XXS) || defined(DATA_A_IQ3_S) || defined(DATA_A_IQ4_XS) || defined(DATA_A_IQ4_NL) | ||
| #if defined(DATA_A_Q1_0)vec2 dequantize(uint ib, uint iqs, uint a_offset) { const uint byte_idx = iqs / 8; const uint bit_idx = iqs % 8; const uint bits = uint(data_a[a_offset + ib].qs[byte_idx]); const float sign0 = ((bits >> bit_idx) & 1) == 1 ? 1.0f : -1.0f; const uint byte_idx2 = (iqs + 1) / 8; const uint bit_idx2 = (iqs + 1) % 8; const uint bits2 = uint(data_a[a_offset + ib].qs[byte_idx2]); const float sign1 = ((bits2 >> bit_idx2) & 1) == 1 ? 1.0f : -1.0f; return vec2(sign0, sign1);}vec4 dequantize4(uint ib, uint iqs, uint a_offset) { const uint byte_idx0 = iqs / 8; const uint bit_idx0 = iqs % 8; const uint bits0 = uint(data_a[a_offset + ib].qs[byte_idx0]); const float s0 = ((bits0 >> bit_idx0) & 1) == 1 ? 1.0f : -1.0f; const uint byte_idx1 = (iqs + 1) / 8; const uint bit_idx1 = (iqs + 1) % 8; const uint bits1 = uint(data_a[a_offset + ib].qs[byte_idx1]); const float s1 = ((bits1 >> bit_idx1) & 1) == 1 ? 1.0f : -1.0f; const uint byte_idx2 = (iqs + 2) / 8; const uint bit_idx2 = (iqs + 2) % 8; const uint bits2 = uint(data_a[a_offset + ib].qs[byte_idx2]); const float s2 = ((bits2 >> bit_idx2) & 1) == 1 ? 1.0f : -1.0f; const uint byte_idx3 = (iqs + 3) / 8; const uint bit_idx3 = (iqs + 3) % 8; const uint bits3 = uint(data_a[a_offset + ib].qs[byte_idx3]); const float s3 = ((bits3 >> bit_idx3) & 1) == 1 ? 1.0f : -1.0f; return vec4(s0, s1, s2, s3);}#endif | ||
|
Collaborator
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. similar here, was this meant to be minimized?
Author
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. No |
||
| #if defined(DATA_A_Q1_0) || defined(DATA_A_Q4_0) || defined(DATA_A_Q5_0) || defined(DATA_A_Q8_0) || defined(DATA_A_IQ1_S) || defined(DATA_A_IQ2_XXS) || defined(DATA_A_IQ2_XS) || defined(DATA_A_IQ2_S) || defined(DATA_A_IQ3_XXS) || defined(DATA_A_IQ3_S) || defined(DATA_A_IQ4_XS) || defined(DATA_A_IQ4_NL) | ||
| vec2 get_dm(uint ib, uint a_offset) { | ||
| return vec2(float(data_a[a_offset + ib].d), 0); | ||
| } | ||
|
|
||
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 @@ | ||
| #version 450 | ||
|
|
||
| #include "dequant_head.glsl" | ||
|
|
||
| layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in; | ||
|
|
||
| layout (binding = 0) readonly buffer A {block_q1_0 data_a[];}; | ||
| layout (binding = 1) writeonly buffer D {D_TYPE data_b[];}; | ||
|
|
||
| void main() { | ||
| // Each thread handles one 128-element block | ||
| const uint ib = gl_WorkGroupID.x * 256 + gl_LocalInvocationID.x; | ||
|
|
||
| if (ib >= p.nel / 128) { | ||
| return; | ||
| } | ||
|
|
||
| const uint b_idx = ib * 128; | ||
| const float d = float(data_a[ib].d); | ||
|
|
||
| // Each block has 16 bytes = 128 bits = 128 elements | ||
| [[unroll]] for (uint byte_idx = 0; byte_idx < 16; ++byte_idx) { | ||
| const uint bits = uint(data_a[ib].qs[byte_idx]); | ||
| [[unroll]] for (uint bit_idx = 0; bit_idx < 8; ++bit_idx) { | ||
| const float sign = ((bits >> bit_idx) & 1) == 1 ? 1.0f : -1.0f; | ||
| data_b[b_idx + byte_idx * 8 + bit_idx] = D_TYPE(d * sign); | ||
| } | ||
| } | ||
| } |
108 changes: 108 additions & 0 deletions
108
ggml/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_q1_0.comp
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,108 @@ | ||
| #version 450 | ||
| #extension GL_EXT_shader_explicit_arithmetic_types_int32 : require | ||
| #extension GL_EXT_shader_8bit_storage : require | ||
|
|
||
| #include "mul_mat_vec_base.glsl" | ||
|
|
||
| layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; | ||
|
|
||
| // Fused 1-bit matrix-vector multiply for Q1_0. | ||
| // 4 threads per block, each handles 32 elements (one uint32 of packed bits). | ||
| // Uses simple ternary sign selection which compiles to v_cndmask on RDNA. | ||
|
|
||
| FLOAT_TYPE temp[NUM_COLS][NUM_ROWS]; | ||
|
|
||
| void calc_block(const uint a_offset, const uint b_offset, const uint itid, const uint i, | ||
| const uint num_blocks_per_row, const uint first_row, const uint num_rows) { | ||
|
|
||
| const uint y_idx_base = i * 128 + itid * 32; | ||
|
|
||
| [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { | ||
| const uint base_b = (j * p.batch_stride_b + b_offset + y_idx_base) / 4; | ||
| const vec4 bv0 = vec4(data_b_v4[base_b]); | ||
| const vec4 bv1 = vec4(data_b_v4[base_b + 1]); | ||
| const vec4 bv2 = vec4(data_b_v4[base_b + 2]); | ||
| const vec4 bv3 = vec4(data_b_v4[base_b + 3]); | ||
| const vec4 bv4 = vec4(data_b_v4[base_b + 4]); | ||
| const vec4 bv5 = vec4(data_b_v4[base_b + 5]); | ||
| const vec4 bv6 = vec4(data_b_v4[base_b + 6]); | ||
| const vec4 bv7 = vec4(data_b_v4[base_b + 7]); | ||
|
|
||
| uint ibi = a_offset + first_row * num_blocks_per_row + i; | ||
|
|
||
| [[unroll]] for (uint n = 0; n < num_rows; ++n) { | ||
| const float d = float(data_a[ibi].d); | ||
|
|
||
| const uint byte_base = itid * 4; | ||
| const uint bits = uint(data_a[ibi].qs[byte_base]) | ||
| | (uint(data_a[ibi].qs[byte_base + 1]) << 8) | ||
| | (uint(data_a[ibi].qs[byte_base + 2]) << 16) | ||
| | (uint(data_a[ibi].qs[byte_base + 3]) << 24); | ||
|
|
||
| FLOAT_TYPE partial = FLOAT_TYPE(0); | ||
|
|
||
| partial += FLOAT_TYPE(dot(vec4( | ||
| (bits & 0x1u) != 0 ? 1.0 : -1.0, (bits & 0x2u) != 0 ? 1.0 : -1.0, | ||
| (bits & 0x4u) != 0 ? 1.0 : -1.0, (bits & 0x8u) != 0 ? 1.0 : -1.0), bv0)); | ||
| partial += FLOAT_TYPE(dot(vec4( | ||
| (bits & 0x10u) != 0 ? 1.0 : -1.0, (bits & 0x20u) != 0 ? 1.0 : -1.0, | ||
| (bits & 0x40u) != 0 ? 1.0 : -1.0, (bits & 0x80u) != 0 ? 1.0 : -1.0), bv1)); | ||
| partial += FLOAT_TYPE(dot(vec4( | ||
| (bits & 0x100u) != 0 ? 1.0 : -1.0, (bits & 0x200u) != 0 ? 1.0 : -1.0, | ||
| (bits & 0x400u) != 0 ? 1.0 : -1.0, (bits & 0x800u) != 0 ? 1.0 : -1.0), bv2)); | ||
| partial += FLOAT_TYPE(dot(vec4( | ||
| (bits & 0x1000u) != 0 ? 1.0 : -1.0, (bits & 0x2000u) != 0 ? 1.0 : -1.0, | ||
| (bits & 0x4000u) != 0 ? 1.0 : -1.0, (bits & 0x8000u) != 0 ? 1.0 : -1.0), bv3)); | ||
| partial += FLOAT_TYPE(dot(vec4( | ||
| (bits & 0x10000u) != 0 ? 1.0 : -1.0, (bits & 0x20000u) != 0 ? 1.0 : -1.0, | ||
| (bits & 0x40000u) != 0 ? 1.0 : -1.0, (bits & 0x80000u) != 0 ? 1.0 : -1.0), bv4)); | ||
| partial += FLOAT_TYPE(dot(vec4( | ||
| (bits & 0x100000u) != 0 ? 1.0 : -1.0, (bits & 0x200000u) != 0 ? 1.0 : -1.0, | ||
| (bits & 0x400000u) != 0 ? 1.0 : -1.0, (bits & 0x800000u) != 0 ? 1.0 : -1.0), bv5)); | ||
| partial += FLOAT_TYPE(dot(vec4( | ||
| (bits & 0x1000000u) != 0 ? 1.0 : -1.0, (bits & 0x2000000u) != 0 ? 1.0 : -1.0, | ||
| (bits & 0x4000000u) != 0 ? 1.0 : -1.0, (bits & 0x8000000u) != 0 ? 1.0 : -1.0), bv6)); | ||
| partial += FLOAT_TYPE(dot(vec4( | ||
| (bits & 0x10000000u) != 0 ? 1.0 : -1.0, (bits & 0x20000000u) != 0 ? 1.0 : -1.0, | ||
| (bits & 0x40000000u) != 0 ? 1.0 : -1.0, (bits & 0x80000000u) != 0 ? 1.0 : -1.0), bv7)); | ||
|
|
||
| temp[j][n] = fma(FLOAT_TYPE(d), partial, 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 / 128; | ||
| const uint blocks_per_wg = gl_WorkGroupSize.x / 4; | ||
| const uint tid = gl_LocalInvocationID.x; | ||
| const uint itid = tid % 4; | ||
| const uint ix = tid / 4; | ||
|
|
||
| [[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_block(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); | ||
|
|
||
| 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); | ||
| } | ||
| } |
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.
was this meant to be minimized?
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.
No, not intentional — that wasn’t meant to be minimized; it should be formatted normally like the surrounding
shader.