ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-opencl / kernels / gemm_noshuffle_q3_k_q8_1_dp4a.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | #pragma OPENCL EXTENSION cl_khr_subgroups : enable |
| 3 | #ifdef cl_khr_integer_dot_product |
| 4 | #pragma OPENCL EXTENSION cl_khr_integer_dot_product : enable |
| 5 | #endif |
| 6 | |
| 7 | #define QK_K 256 |
| 8 | |
| 9 | #ifndef TILESIZE_N |
| 10 | #define TILESIZE_N 32 |
| 11 | #endif |
| 12 | |
| 13 | inline int q3_k_scale(uint sc0, uint sc1, uint sc2, uint is) { |
| 14 | const uint w = is >> 2; |
| 15 | const uint sh = 8u * (is & 3u); |
| 16 | const uint tb = (sc2 >> sh) & 0xFFu; |
| 17 | uint v; |
| 18 | if (w == 0u) { v = ( (sc0 >> sh) & 0xFu) | (((tb >> 0) & 3u) << 4); } |
| 19 | else if (w == 1u) { v = ( (sc1 >> sh) & 0xFu) | (((tb >> 2) & 3u) << 4); } |
| 20 | else if (w == 2u) { v = (((sc0 >> sh) >> 4) & 0xFu) | (((tb >> 4) & 3u) << 4); } |
| 21 | else { v = (((sc1 >> sh) >> 4) & 0xFu) | (((tb >> 6) & 3u) << 4); } |
| 22 | return (int)v - 32; |
| 23 | } |
| 24 | |
| 25 | inline uint q3_k_pack(uint pk, uint hb) { |
| 26 | int v0 = (int)((pk ) & 3u) - 4 + 4*(int)((hb ) & 1u); |
| 27 | int v1 = (int)((pk >> 2) & 3u) - 4 + 4*(int)((hb >> 1) & 1u); |
| 28 | int v2 = (int)((pk >> 4) & 3u) - 4 + 4*(int)((hb >> 2) & 1u); |
| 29 | int v3 = (int)((pk >> 6) & 3u) - 4 + 4*(int)((hb >> 3) & 1u); |
| 30 | return ((uint)v0 & 0xFFu) | (((uint)v1 & 0xFFu) << 8) |
| 31 | | (((uint)v2 & 0xFFu) << 16) | (((uint)v3 & 0xFFu) << 24); |
| 32 | } |
| 33 | |
| 34 | // Pass staged operands by value; local dp4a operands miscompile on X2. |
| 35 | #define KQ_STAGE(p) vload4(0, (p)) |
| 36 | |
| 37 | inline int dot4_q8a_v(uint4 qw, uint4 a) { |
| 38 | int r = 0; |
| 39 | r = dot_acc_sat_4x8packed_ss_int(qw.s0, a.x, r); |
| 40 | r = dot_acc_sat_4x8packed_ss_int(qw.s1, a.y, r); |
| 41 | r = dot_acc_sat_4x8packed_ss_int(qw.s2, a.z, r); |
| 42 | r = dot_acc_sat_4x8packed_ss_int(qw.s3, a.w, r); |
| 43 | return r; |
| 44 | } |
| 45 | |
| 46 | __attribute__((qcom_wave_pair_mode(1))) |
| 47 | kernel void kernel_gemm_noshuffle_q3_k_q8_1_dp4a( |
| 48 | __global const uchar * src0_qs, |
| 49 | __global const uchar * src0_hm, |
| 50 | __global const uint * src0_sc, |
| 51 | __global const half * src0_d, |
| 52 | __global const uint * src1_qa, // q8_1 activations int8 (as uint, 4/elem) [N, K] |
| 53 | __global const half * src1_da, // q8_1 per-block scale [N, K/32] |
| 54 | __global float * dst, |
| 55 | ulong offsetd, |
| 56 | int m, // output features (rows) |
| 57 | int n_no_padding, // tokens (cols) |
| 58 | int k // K (== ne00) |
| 59 | ) { |
| 60 | dst = (global float *)((global char *)dst + offsetd); |
| 61 | |
| 62 | const uint lid = get_local_id(0); |
| 63 | const uint block_id_m = get_global_id(1); |
| 64 | const uint block_id_n = get_global_id(2); |
| 65 | |
| 66 | const uint row = block_id_m * 64 + lid; |
| 67 | const uint col_base = block_id_n * TILESIZE_N; |
| 68 | const bool row_valid = row < (uint)m; |
| 69 | const uint rrow = row_valid ? row : 0; |
| 70 | |
| 71 | const uint k_u = (uint)k >> 2; |
| 72 | const uint k_b = (uint)k >> 5; |
| 73 | |
| 74 | __local uint4 sh_qa4[TILESIZE_N][2]; |
| 75 | __local half sh_d[TILESIZE_N]; |
| 76 | |
| 77 | #define NGROUPS (TILESIZE_N / 4) |
| 78 | float4 acc[NGROUPS]; |
| 79 | #pragma unroll |
| 80 | for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f); |
| 81 | |
| 82 | for (uint step = 0; step < (uint)k; step += 32) { |
| 83 | const uint sub = step >> 5; |
| 84 | const uint ib = sub >> 3; // super-block along K |
| 85 | const uint sb = sub & 7u; |
| 86 | |
| 87 | const uint scb = rrow + (3u * ib) * (uint)m; |
| 88 | const uint sc0 = src0_sc[scb + 0u * (uint)m]; |
| 89 | const uint sc1 = src0_sc[scb + 1u * (uint)m]; |
| 90 | const uint sc2 = src0_sc[scb + 2u * (uint)m]; |
| 91 | |
| 92 | const float d_w = (float)src0_d[rrow + ib * (uint)m]; |
| 93 | const float dl0 = d_w * (float)q3_k_scale(sc0, sc1, sc2, 2u*sb + 0u); |
| 94 | const float dl1 = d_w * (float)q3_k_scale(sc0, sc1, sc2, 2u*sb + 1u); |
| 95 | |
| 96 | const uint qsb = rrow + (step >> 2) * (uint)m; |
| 97 | const uint hmb = rrow + (step >> 3) * (uint)m; |
| 98 | |
| 99 | uint4 qlo, qhi; |
| 100 | { |
| 101 | const uint h0 = (uint)src0_hm[hmb + 0u * (uint)m]; |
| 102 | const uint h1 = (uint)src0_hm[hmb + 1u * (uint)m]; |
| 103 | const uint h2 = (uint)src0_hm[hmb + 2u * (uint)m]; |
| 104 | const uint h3 = (uint)src0_hm[hmb + 3u * (uint)m]; |
| 105 | qlo.s0 = q3_k_pack((uint)src0_qs[qsb + 0u * (uint)m], h0 & 0xFu); |
| 106 | qlo.s1 = q3_k_pack((uint)src0_qs[qsb + 1u * (uint)m], (h0 >> 4) & 0xFu); |
| 107 | qlo.s2 = q3_k_pack((uint)src0_qs[qsb + 2u * (uint)m], h1 & 0xFu); |
| 108 | qlo.s3 = q3_k_pack((uint)src0_qs[qsb + 3u * (uint)m], (h1 >> 4) & 0xFu); |
| 109 | qhi.s0 = q3_k_pack((uint)src0_qs[qsb + 4u * (uint)m], h2 & 0xFu); |
| 110 | qhi.s1 = q3_k_pack((uint)src0_qs[qsb + 5u * (uint)m], (h2 >> 4) & 0xFu); |
| 111 | qhi.s2 = q3_k_pack((uint)src0_qs[qsb + 6u * (uint)m], h3 & 0xFu); |
| 112 | qhi.s3 = q3_k_pack((uint)src0_qs[qsb + 7u * (uint)m], (h3 >> 4) & 0xFu); |
| 113 | } |
| 114 | |
| 115 | for (uint idx = lid; idx < TILESIZE_N * 2; idx += 64) { |
| 116 | const uint t = idx >> 1; |
| 117 | const uint v = idx & 1; |
| 118 | const uint c = col_base + t; |
| 119 | sh_qa4[t][v] = (c < (uint)n_no_padding) |
| 120 | ? KQ_STAGE(src1_qa + c * k_u + (step >> 2) + (v << 2)) |
| 121 | : (uint4)(0u); |
| 122 | } |
| 123 | if (lid < TILESIZE_N) { |
| 124 | const uint c = col_base + lid; |
| 125 | sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0; |
| 126 | } |
| 127 | barrier(CLK_LOCAL_MEM_FENCE); |
| 128 | |
| 129 | #define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3])) |
| 130 | #pragma unroll |
| 131 | for (int g = 0; g < NGROUPS; ++g) { |
| 132 | const int b = g * 4; |
| 133 | float4 rf; |
| 134 | #define Q3_K_COL(T) (dl0 * (float)dot4_q8a_v(qlo, (uint4)(sh_qa4[T][0])) \ |
| 135 | + dl1 * (float)dot4_q8a_v(qhi, (uint4)(sh_qa4[T][1]))) |
| 136 | rf.s0 = Q3_K_COL(b+0); rf.s1 = Q3_K_COL(b+1); |
| 137 | rf.s2 = Q3_K_COL(b+2); rf.s3 = Q3_K_COL(b+3); |
| 138 | #undef Q3_K_COL |
| 139 | acc[g] += LD4(sh_d, b) * rf; |
| 140 | } |
| 141 | #undef LD4 |
| 142 | barrier(CLK_LOCAL_MEM_FENCE); |
| 143 | } |
| 144 | |
| 145 | if (!row_valid) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | #pragma unroll |
| 150 | for (int g = 0; g < NGROUPS; ++g) { |
| 151 | const uint b = (uint)(g * 4); |
| 152 | const float4 a = acc[g]; |
| 153 | const uint c0 = col_base + b; |
| 154 | if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0; |
| 155 | if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1; |
| 156 | if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2; |
| 157 | if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3; |
| 158 | } |
| 159 | #undef NGROUPS |
| 160 | } |