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_iq2_s_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 iq2_s_pack(uint gv, uint sg, uint base) { |
| 14 | int v0 = (int)((gv >> 0) & 0xFF); if (sg & (1u << (base + 0))) { v0 = -v0; } |
| 15 | int v1 = (int)((gv >> 8) & 0xFF); if (sg & (1u << (base + 1))) { v1 = -v1; } |
| 16 | int v2 = (int)((gv >> 16) & 0xFF); if (sg & (1u << (base + 2))) { v2 = -v2; } |
| 17 | int v3 = (int)((gv >> 24) & 0xFF); if (sg & (1u << (base + 3))) { v3 = -v3; } |
| 18 | return ((uint)v0 & 0xFFu) | (((uint)v1 & 0xFFu) << 8) |
| 19 | | (((uint)v2 & 0xFFu) << 16) | (((uint)v3 & 0xFFu) << 24); |
| 20 | } |
| 21 | |
| 22 | // Pass staged operands by value; local dp4a operands miscompile on X2. |
| 23 | inline int dot4_q8a_v(uint4 qw, uint4 a) { |
| 24 | int r = 0; |
| 25 | r = dot_acc_sat_4x8packed_ss_int(qw.s0, a.x, r); |
| 26 | r = dot_acc_sat_4x8packed_ss_int(qw.s1, a.y, r); |
| 27 | r = dot_acc_sat_4x8packed_ss_int(qw.s2, a.z, r); |
| 28 | r = dot_acc_sat_4x8packed_ss_int(qw.s3, a.w, r); |
| 29 | return r; |
| 30 | } |
| 31 | |
| 32 | __attribute__((qcom_wave_pair_mode(1))) |
| 33 | kernel void kernel_gemm_noshuffle_iq2_s_q8_1_dp4a( |
| 34 | __global const uchar * src0_qs, |
| 35 | __global const uchar * src0_sg, |
| 36 | __global const uchar * src0_qh, |
| 37 | __global const uchar * src0_sc, |
| 38 | __global const half * src0_d, |
| 39 | __global const uint * src1_qa, |
| 40 | __global const half * src1_da, |
| 41 | __global float * dst, |
| 42 | ulong offsetd, |
| 43 | int m, |
| 44 | int n_no_padding, |
| 45 | int k |
| 46 | ) { |
| 47 | dst = (global float *)((global char *)dst + offsetd); |
| 48 | |
| 49 | const uint lid = get_local_id(0); |
| 50 | const uint block_id_m = get_global_id(1); |
| 51 | const uint block_id_n = get_global_id(2); |
| 52 | |
| 53 | const uint row = block_id_m * 64 + lid; |
| 54 | const uint col_base = block_id_n * TILESIZE_N; |
| 55 | const bool row_valid = row < (uint)m; |
| 56 | const uint rrow = row_valid ? row : 0; |
| 57 | |
| 58 | const uint k_u = (uint)k >> 2; |
| 59 | const uint k_b = (uint)k >> 5; |
| 60 | |
| 61 | __local uint4 sh_qa4[TILESIZE_N][2]; |
| 62 | __local half sh_d[TILESIZE_N]; |
| 63 | |
| 64 | #define IQ2_S_GRID(i) iq2_s_grid[(i)] |
| 65 | |
| 66 | #define NGROUPS (TILESIZE_N / 4) |
| 67 | float4 acc[NGROUPS]; |
| 68 | #pragma unroll |
| 69 | for (int g = 0; g < NGROUPS; ++g) acc[g] = (float4)(0.0f); |
| 70 | |
| 71 | for (uint step = 0; step < (uint)k; step += 32) { |
| 72 | const uint sub = step >> 5; |
| 73 | const uint ib = sub >> 3; |
| 74 | |
| 75 | const uint qhv = (uint)src0_qh[rrow + sub * (uint)m]; |
| 76 | const uint scv = (uint)src0_sc[rrow + sub * (uint)m]; |
| 77 | const float dv = (float)src0_d[rrow + ib * (uint)m]; |
| 78 | const float dl0 = dv * (0.5f + (float)( scv & 0xFu)) * 0.25f; |
| 79 | const float dl1 = dv * (0.5f + (float)( scv >> 4) ) * 0.25f; |
| 80 | |
| 81 | const uint gb = rrow + (step >> 3) * (uint)m; |
| 82 | |
| 83 | uint4 qlo, qhi; |
| 84 | { |
| 85 | const uint gi0 = (uint)src0_qs[gb + 0u * (uint)m] | ((( qhv & 3u) << 8)); |
| 86 | const uint gi1 = (uint)src0_qs[gb + 1u * (uint)m] | ((((qhv >> 2) & 3u) << 8)); |
| 87 | const uint gi2 = (uint)src0_qs[gb + 2u * (uint)m] | ((((qhv >> 4) & 3u) << 8)); |
| 88 | const uint gi3 = (uint)src0_qs[gb + 3u * (uint)m] | ((((qhv >> 6) & 3u) << 8)); |
| 89 | const uint s0 = (uint)src0_sg[gb + 0u * (uint)m]; |
| 90 | const uint s1 = (uint)src0_sg[gb + 1u * (uint)m]; |
| 91 | const uint s2 = (uint)src0_sg[gb + 2u * (uint)m]; |
| 92 | const uint s3 = (uint)src0_sg[gb + 3u * (uint)m]; |
| 93 | qlo.s0 = iq2_s_pack(IQ2_S_GRID(2u*gi0 + 0u), s0, 0u); |
| 94 | qlo.s1 = iq2_s_pack(IQ2_S_GRID(2u*gi0 + 1u), s0, 4u); |
| 95 | qlo.s2 = iq2_s_pack(IQ2_S_GRID(2u*gi1 + 0u), s1, 0u); |
| 96 | qlo.s3 = iq2_s_pack(IQ2_S_GRID(2u*gi1 + 1u), s1, 4u); |
| 97 | qhi.s0 = iq2_s_pack(IQ2_S_GRID(2u*gi2 + 0u), s2, 0u); |
| 98 | qhi.s1 = iq2_s_pack(IQ2_S_GRID(2u*gi2 + 1u), s2, 4u); |
| 99 | qhi.s2 = iq2_s_pack(IQ2_S_GRID(2u*gi3 + 0u), s3, 0u); |
| 100 | qhi.s3 = iq2_s_pack(IQ2_S_GRID(2u*gi3 + 1u), s3, 4u); |
| 101 | } |
| 102 | |
| 103 | for (uint idx = lid; idx < TILESIZE_N * 2; idx += 64) { |
| 104 | const uint t = idx >> 1; |
| 105 | const uint v = idx & 1; |
| 106 | const uint c = col_base + t; |
| 107 | sh_qa4[t][v] = (c < (uint)n_no_padding) |
| 108 | ? vload4(0, src1_qa + c * k_u + (step >> 2) + (v << 2)) |
| 109 | : (uint4)(0u); |
| 110 | } |
| 111 | if (lid < TILESIZE_N) { |
| 112 | const uint c = col_base + lid; |
| 113 | sh_d[lid] = (c < (uint)n_no_padding) ? src1_da[c * k_b + sub] : (half)0; |
| 114 | } |
| 115 | barrier(CLK_LOCAL_MEM_FENCE); |
| 116 | |
| 117 | #define LD4(arr, b) ((float4)((float)arr[(b)+0], (float)arr[(b)+1], (float)arr[(b)+2], (float)arr[(b)+3])) |
| 118 | #define IQ2_S_COL(b) (dl0 * (float)dot4_q8a_v(qlo, (uint4)(sh_qa4[b][0])) \ |
| 119 | + dl1 * (float)dot4_q8a_v(qhi, (uint4)(sh_qa4[b][1]))) |
| 120 | #pragma unroll |
| 121 | for (int g = 0; g < NGROUPS; ++g) { |
| 122 | const int b = g * 4; |
| 123 | float4 rf; |
| 124 | rf.s0 = IQ2_S_COL(b+0); rf.s1 = IQ2_S_COL(b+1); |
| 125 | rf.s2 = IQ2_S_COL(b+2); rf.s3 = IQ2_S_COL(b+3); |
| 126 | acc[g] += LD4(sh_d, b) * rf; |
| 127 | } |
| 128 | #undef IQ2_S_COL |
| 129 | #undef LD4 |
| 130 | barrier(CLK_LOCAL_MEM_FENCE); |
| 131 | } |
| 132 | |
| 133 | if (!row_valid) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | #pragma unroll |
| 138 | for (int g = 0; g < NGROUPS; ++g) { |
| 139 | const uint b = (uint)(g * 4); |
| 140 | const float4 a = acc[g]; |
| 141 | const uint c0 = col_base + b; |
| 142 | if (c0 + 0 < (uint)n_no_padding) dst[(c0 + 0) * (uint)m + row] = a.s0; |
| 143 | if (c0 + 1 < (uint)n_no_padding) dst[(c0 + 1) * (uint)m + row] = a.s1; |
| 144 | if (c0 + 2 < (uint)n_no_padding) dst[(c0 + 2) * (uint)m + row] = a.s2; |
| 145 | if (c0 + 3 < (uint)n_no_padding) dst[(c0 + 3) * (uint)m + row] = a.s3; |
| 146 | } |
| 147 | #undef NGROUPS |
| 148 | #undef IQ2_S_GRID |
| 149 | } |