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_iq1_m_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 uint iq1_m_pack(uint g, uint shift, uint negative_delta) { |
| 14 | // 8*(grid + delta) is an exact signed integer in [-9, 9]. |
| 15 | const int bias = negative_delta ? -9 : -7; |
| 16 | const int4 values = 8*convert_int4(as_uchar4((g >> shift) & 0x0F0F0F0Fu)) + bias; |
| 17 | return as_uint(convert_char4(values)); |
| 18 | } |
| 19 | |
| 20 | // Pass staged operands by value; local dp4a operands miscompile on X2. |
| 21 | inline int dot4_q8a_v(uint4 qw, uint4 a) { |
| 22 | int r = 0; |
| 23 | r = dot_acc_sat_4x8packed_ss_int(qw.s0, a.x, r); |
| 24 | r = dot_acc_sat_4x8packed_ss_int(qw.s1, a.y, r); |
| 25 | r = dot_acc_sat_4x8packed_ss_int(qw.s2, a.z, r); |
| 26 | r = dot_acc_sat_4x8packed_ss_int(qw.s3, a.w, r); |
| 27 | return r; |
| 28 | } |
| 29 | |
| 30 | __attribute__((qcom_wave_pair_mode(1))) |
| 31 | kernel void kernel_gemm_noshuffle_iq1_m_q8_1_dp4a( |
| 32 | __global const uchar * src0_qs, |
| 33 | __global const uchar * src0_qh, |
| 34 | __global const ushort * src0_sc, |
| 35 | __global const uint * src1_qa, |
| 36 | __global const half * src1_da, |
| 37 | __global float * dst, |
| 38 | ulong offsetd, |
| 39 | int m, |
| 40 | int n_no_padding, |
| 41 | int k |
| 42 | ) { |
| 43 | dst = (global float *)((global char *)dst + offsetd); |
| 44 | |
| 45 | const uint lid = get_local_id(0); |
| 46 | const uint block_id_m = get_global_id(1); |
| 47 | const uint block_id_n = get_global_id(2); |
| 48 | |
| 49 | const uint row = block_id_m * 64 + lid; |
| 50 | const uint col_base = block_id_n * TILESIZE_N; |
| 51 | const bool row_valid = row < (uint)m; |
| 52 | const uint rrow = row_valid ? row : 0; |
| 53 | |
| 54 | const uint k_u = (uint)k >> 2; |
| 55 | const uint k_b = (uint)k >> 5; |
| 56 | |
| 57 | __local uint4 sh_qa4[TILESIZE_N][2]; |
| 58 | __local half sh_d[TILESIZE_N]; |
| 59 | |
| 60 | #define IQ1_M_GRID(i) iq1_s_grid[(i)] |
| 61 | |
| 62 | #define NGROUPS (TILESIZE_N / 4) |
| 63 | float4 acc[NGROUPS]; |
| 64 | #pragma unroll |
| 65 | for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f); |
| 66 | |
| 67 | float dsuper = 0.f; |
| 68 | for (uint step = 0; step < (uint)k; step += 32) { |
| 69 | const uint sub = step >> 5; |
| 70 | const uint ib = sub >> 3; |
| 71 | |
| 72 | if ((sub & 7u) == 0u) { |
| 73 | const uint sb = rrow + 4u*ib*(uint)m; |
| 74 | const uint w0 = src0_sc[sb + 0u*(uint)m]; |
| 75 | const uint w1 = src0_sc[sb + 1u*(uint)m]; |
| 76 | const uint w2 = src0_sc[sb + 2u*(uint)m]; |
| 77 | const uint w3 = src0_sc[sb + 3u*(uint)m]; |
| 78 | const ushort bits = (ushort)((w0 >> 12) | ((w1 >> 8) & 0x00f0u) |
| 79 | | ((w2 >> 4) & 0x0f00u) | (w3 & 0xf000u)); |
| 80 | dsuper = (float)as_half(bits); |
| 81 | } |
| 82 | const uint sc = src0_sc[rrow + (4u*ib + (sub & 7u)/2u)*(uint)m]; |
| 83 | const uint sh = 6u*(sub & 1u); |
| 84 | const float dl0 = dsuper * (float)(2u*((sc >> sh) & 7u) + 1u) * 0.125f; |
| 85 | const float dl1 = dsuper * (float)(2u*((sc >> (sh + 3u)) & 7u) + 1u) * 0.125f; |
| 86 | const uint h0 = src0_qh[rrow + (2u*sub + 0u)*(uint)m]; |
| 87 | const uint h1 = src0_qh[rrow + (2u*sub + 1u)*(uint)m]; |
| 88 | const uint gb = rrow + 4u*sub*(uint)m; |
| 89 | const uint g0 = IQ1_M_GRID((uint)src0_qs[gb + 0u*(uint)m] | (( h0 & 7u) << 8)); |
| 90 | const uint g1 = IQ1_M_GRID((uint)src0_qs[gb + 1u*(uint)m] | (((h0 >> 4) & 7u) << 8)); |
| 91 | const uint g2 = IQ1_M_GRID((uint)src0_qs[gb + 2u*(uint)m] | (( h1 & 7u) << 8)); |
| 92 | const uint g3 = IQ1_M_GRID((uint)src0_qs[gb + 3u*(uint)m] | (((h1 >> 4) & 7u) << 8)); |
| 93 | const uint4 qlo = (uint4)(iq1_m_pack(g0, 0, h0 & 0x08u), iq1_m_pack(g0, 4, h0 & 0x08u), |
| 94 | iq1_m_pack(g1, 0, h0 & 0x80u), iq1_m_pack(g1, 4, h0 & 0x80u)); |
| 95 | const uint4 qhi = (uint4)(iq1_m_pack(g2, 0, h1 & 0x08u), iq1_m_pack(g2, 4, h1 & 0x08u), |
| 96 | iq1_m_pack(g3, 0, h1 & 0x80u), iq1_m_pack(g3, 4, h1 & 0x80u)); |
| 97 | |
| 98 | for (uint idx = lid; idx < TILESIZE_N * 2; idx += 64) { |
| 99 | const uint t = idx >> 1; |
| 100 | const uint v = idx & 1; |
| 101 | const uint c = col_base + t; |
| 102 | sh_qa4[t][v] = (c < (uint)n_no_padding) |
| 103 | ? vload4(0, src1_qa + c * k_u + (step >> 2) + (v << 2)) |
| 104 | : (uint4)(0u); |
| 105 | } |
| 106 | if (lid < TILESIZE_N) { |
| 107 | const uint c = col_base + lid; |
| 108 | sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0; |
| 109 | } |
| 110 | barrier(CLK_LOCAL_MEM_FENCE); |
| 111 | |
| 112 | #define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3])) |
| 113 | #define IQ1_M_COL(b) (dl0 * (float)dot4_q8a_v(qlo, (uint4)(sh_qa4[b][0])) \ |
| 114 | + dl1 * (float)dot4_q8a_v(qhi, (uint4)(sh_qa4[b][1]))) |
| 115 | #pragma unroll |
| 116 | for (int g = 0; g < NGROUPS; ++g) { |
| 117 | const int b = g * 4; |
| 118 | float4 rf; |
| 119 | rf.s0 = IQ1_M_COL(b+0); rf.s1 = IQ1_M_COL(b+1); |
| 120 | rf.s2 = IQ1_M_COL(b+2); rf.s3 = IQ1_M_COL(b+3); |
| 121 | acc[g] += LD4(sh_d, b) * rf; |
| 122 | } |
| 123 | #undef IQ1_M_COL |
| 124 | #undef LD4 |
| 125 | barrier(CLK_LOCAL_MEM_FENCE); |
| 126 | } |
| 127 | |
| 128 | if (!row_valid) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | #pragma unroll |
| 133 | for (int g = 0; g < NGROUPS; ++g) { |
| 134 | const uint b = (uint)(g * 4); |
| 135 | const float4 a = acc[g]; |
| 136 | const uint c0 = col_base + b; |
| 137 | if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0; |
| 138 | if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1; |
| 139 | if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2; |
| 140 | if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3; |
| 141 | } |
| 142 | #undef NGROUPS |
| 143 | #undef IQ1_M_GRID |
| 144 | } |