forked from ggml-org/llama.cpp
-
Notifications
You must be signed in to change notification settings - Fork 31
initial Q1_0 Metal backend #14
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
5 commits
Select commit
Hold shift + click to select a range
b0c8c89
initial Q1_0 Metal backend
khosravipasha 0776cd2
tuning q1_0 metal kernels
khosravipasha 6a55d70
add Q1_0 to test-backend-ops
khosravipasha 74c9bdd
add Q1_0<->F32 copy test
khosravipasha a1517c2
Apply suggestions from code review
khosravipasha 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
Some comments aren't visible on the classic Files Changed page.
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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -118,6 +118,56 @@ void dequantize_bf16_t4(device const bfloat4 * src, short il, thread type4 & reg | |||||
| } | ||||||
| #endif | ||||||
|
|
||||||
| template <typename type4x4> | ||||||
| void dequantize_q1_0(device const block_q1_0 * xb, short il, thread type4x4 & reg) { | ||||||
| device const uint8_t * qs = xb->qs; | ||||||
| const float d = xb->d; | ||||||
| const float neg_d = -d; | ||||||
|
|
||||||
| const int byte_offset = il * 2; // il*16 bits = il*2 bytes | ||||||
| const uint8_t b0 = qs[byte_offset]; | ||||||
| const uint8_t b1 = qs[byte_offset + 1]; | ||||||
|
|
||||||
| float4x4 reg_f; | ||||||
|
|
||||||
| reg_f[0][0] = select(neg_d, d, bool(b0 & 0x01)); | ||||||
| reg_f[0][1] = select(neg_d, d, bool(b0 & 0x02)); | ||||||
| reg_f[0][2] = select(neg_d, d, bool(b0 & 0x04)); | ||||||
| reg_f[0][3] = select(neg_d, d, bool(b0 & 0x08)); | ||||||
| reg_f[1][0] = select(neg_d, d, bool(b0 & 0x10)); | ||||||
| reg_f[1][1] = select(neg_d, d, bool(b0 & 0x20)); | ||||||
| reg_f[1][2] = select(neg_d, d, bool(b0 & 0x40)); | ||||||
| reg_f[1][3] = select(neg_d, d, bool(b0 & 0x80)); | ||||||
|
|
||||||
| reg_f[2][0] = select(neg_d, d, bool(b1 & 0x01)); | ||||||
| reg_f[2][1] = select(neg_d, d, bool(b1 & 0x02)); | ||||||
| reg_f[2][2] = select(neg_d, d, bool(b1 & 0x04)); | ||||||
| reg_f[2][3] = select(neg_d, d, bool(b1 & 0x08)); | ||||||
| reg_f[3][0] = select(neg_d, d, bool(b1 & 0x10)); | ||||||
| reg_f[3][1] = select(neg_d, d, bool(b1 & 0x20)); | ||||||
| reg_f[3][2] = select(neg_d, d, bool(b1 & 0x40)); | ||||||
| reg_f[3][3] = select(neg_d, d, bool(b1 & 0x80)); | ||||||
|
|
||||||
| reg = (type4x4) reg_f; | ||||||
| } | ||||||
|
|
||||||
| template <typename type4> | ||||||
| void dequantize_q1_0_t4(device const block_q1_0 * xb, short il, thread type4 & reg) { | ||||||
| const float d = xb->d; | ||||||
| const float neg_d = -d; | ||||||
| const int base = il * 4; | ||||||
| const uint8_t byte = xb->qs[base / 8]; | ||||||
| const int s = base % 8; | ||||||
|
|
||||||
| float4 reg_f; | ||||||
| reg_f[0] = select(neg_d, d, bool((byte >> (s )) & 1)); | ||||||
| reg_f[1] = select(neg_d, d, bool((byte >> (s + 1)) & 1)); | ||||||
| reg_f[2] = select(neg_d, d, bool((byte >> (s + 2)) & 1)); | ||||||
| reg_f[3] = select(neg_d, d, bool((byte >> (s + 3)) & 1)); | ||||||
|
|
||||||
| reg = (type4) reg_f; | ||||||
| } | ||||||
|
|
||||||
| template <typename type4x4> | ||||||
| void dequantize_q4_0(device const block_q4_0 * xb, short il, thread type4x4 & reg) { | ||||||
| device const uint16_t * qs = ((device const uint16_t *)xb + 1); | ||||||
|
|
@@ -152,6 +202,23 @@ void dequantize_q4_0_t4(device const block_q4_0 * xb, short il, thread type4 & r | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| void quantize_q1_0(device const float * src, device block_q1_0 & dst) { | ||||||
| float sum_abs = 0.0f; | ||||||
| for (int j = 0; j < QK1_0; j++) { | ||||||
| sum_abs += fabs(src[j]); | ||||||
| } | ||||||
| dst.d = sum_abs / QK1_0; | ||||||
|
|
||||||
| for (int j = 0; j < QK1_0 / 8; j++) { | ||||||
| dst.qs[j] = 0; | ||||||
| } | ||||||
| for (int j = 0; j < QK1_0; j++) { | ||||||
| if (src[j] >= 0.0f) { | ||||||
| dst.qs[j / 8] |= (1 << (j % 8)); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| void quantize_q4_0(device const float * src, device block_q4_0 & dst) { | ||||||
| #pragma METAL fp math_mode(safe) | ||||||
| float amax = 0.0f; // absolute max | ||||||
|
|
@@ -3116,6 +3183,35 @@ kernel void kernel_group_norm_f32( | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Q1_0 dot product: dot = d * (2 * Σ(yl[i] where bit=1) - sumy) | ||||||
| inline float block_q_n_dot_y(device const block_q1_0 * qb_curr, float sumy, thread float * yl, int il) { | ||||||
|
||||||
| inline float block_q_n_dot_y(device const block_q1_0 * qb_curr, float sumy, thread float * yl, int il) { | |
| inline float block_q_n_dot_y(device const block_q1_0 * qb_curr, float sumy, thread const float * yl, int il) { |
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.
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.