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_q4_K_f32_tiled_dp4a.cl
| 1 | // DP4A GEMM for the canonical 64-row Q4_K layout. |
| 2 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 3 | #ifdef cl_khr_integer_dot_product |
| 4 | #pragma OPENCL EXTENSION cl_khr_integer_dot_product : enable |
| 5 | #endif |
| 6 | |
| 7 | #ifdef cl_qcom_reqd_sub_group_size |
| 8 | #pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable |
| 9 | #define ADRENO_GPU 1 |
| 10 | #define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half"))) |
| 11 | #endif |
| 12 | |
| 13 | #define QK_K 256 |
| 14 | #define TILE_ROWS 64 |
| 15 | #define NTILES 4 |
| 16 | #define BN_DP 8 |
| 17 | #define WG_THREADS (NTILES * TILE_ROWS) |
| 18 | |
| 19 | inline void q4k_scale_min(int j, __global const uchar * q, uchar * d, uchar * m) { |
| 20 | if (j < 4) { |
| 21 | *d = q[j] & 0x3F; |
| 22 | *m = q[j+4] & 0x3F; |
| 23 | } else { |
| 24 | *d = (q[j+4] & 0x0F) | ((q[j-4] & 0xC0) >> 2); |
| 25 | *m = ((q[j+4] >> 4) & 0x0F) | ((q[j] & 0xC0) >> 2); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // dp4a GEMM over the tiled q4_K layout (ne1 > 1) with a q8_1 activation; per 32-element sub-block: |
| 30 | // acc += d*sd*qad*dp4a(q, qa) - dm*sm*qas |
| 31 | #if defined(ADRENO_GPU) |
| 32 | REQD_SUBGROUP_SIZE_64 |
| 33 | #endif |
| 34 | kernel void kernel_gemm_noshuffle_q4_K_f32_tiled_dp4a( |
| 35 | __global uint4 * src0_q, |
| 36 | __global half * src0_d, |
| 37 | __global half * src0_dm, |
| 38 | __global uchar * src0_s, |
| 39 | __global uint * qa, // q8_1 int8 activation, 4/uint [ne11][ne00/4] |
| 40 | __global half * qad, // q8_1 per-32-block scale [ne11][ne00/32] |
| 41 | __global half * qas, // q8_1 per-32-block sum (qad*Sum)[ne11][ne00/32] |
| 42 | global float * dst, |
| 43 | ulong offsetd, |
| 44 | int ne00, |
| 45 | int ne01, |
| 46 | int ne11 |
| 47 | ) { |
| 48 | int rit = get_local_id(0); |
| 49 | int sg = get_local_id(1); |
| 50 | int lid = sg * TILE_ROWS + rit; |
| 51 | int row = get_group_id(0) * WG_THREADS + lid; |
| 52 | int rt = row / TILE_ROWS; |
| 53 | int col0 = get_global_id(2) * BN_DP; |
| 54 | int nb = ne00 / QK_K; |
| 55 | int qcs = ne00 / 4; |
| 56 | int dcs = ne00 / 32; |
| 57 | |
| 58 | const bool row_ok = row < ne01; |
| 59 | __local uint sh_qa[BN_DP * 64]; |
| 60 | __local half sh_qad[BN_DP * 8]; |
| 61 | __local half sh_qas[BN_DP * 8]; |
| 62 | |
| 63 | float acc[BN_DP]; |
| 64 | #pragma unroll |
| 65 | for (int j = 0; j < BN_DP; ++j) acc[j] = 0.0f; |
| 66 | |
| 67 | for (int sb = 0; sb < nb; ++sb) { |
| 68 | for (int p = lid; p < BN_DP * 64; p += WG_THREADS) { |
| 69 | int j = p >> 6, u = p & 63, c = col0 + j; |
| 70 | sh_qa[p] = (c < ne11) ? qa[c * qcs + sb * 64 + u] : 0u; |
| 71 | } |
| 72 | for (int p = lid; p < BN_DP * 8; p += WG_THREADS) { |
| 73 | int j = p >> 3, b = p & 7, c = col0 + j; |
| 74 | sh_qad[p] = (c < ne11) ? qad[c * dcs + sb * 8 + b] : (half)0; |
| 75 | sh_qas[p] = (c < ne11) ? qas[c * dcs + sb * 8 + b] : (half)0; |
| 76 | } |
| 77 | barrier(CLK_LOCAL_MEM_FENCE); |
| 78 | |
| 79 | if (row_ok) { |
| 80 | int tile_blk = rt * nb + sb; |
| 81 | float dval = (float)src0_d [tile_blk * TILE_ROWS + rit]; |
| 82 | float dmval = (float)src0_dm[tile_blk * TILE_ROWS + rit]; |
| 83 | __global uchar * sc = src0_s + (tile_blk * TILE_ROWS + rit) * 12; |
| 84 | |
| 85 | uint q[32]; |
| 86 | #pragma unroll |
| 87 | for (int g = 0; g < 8; ++g) { |
| 88 | uint4 v = src0_q[(tile_blk * 8 + g) * TILE_ROWS + rit]; |
| 89 | q[g*4+0] = v.x; q[g*4+1] = v.y; q[g*4+2] = v.z; q[g*4+3] = v.w; |
| 90 | } |
| 91 | |
| 92 | for (int is = 0; is < 8; ++is) { |
| 93 | uchar sd, sm; |
| 94 | q4k_scale_min(is, sc, &sd, &sm); |
| 95 | float scl = dval * (float)sd, mnv = dmval * (float)sm; |
| 96 | |
| 97 | uint cw[8]; |
| 98 | #pragma unroll |
| 99 | for (int t = 0; t < 8; ++t) { |
| 100 | uint pk = 0; |
| 101 | #pragma unroll |
| 102 | for (int u = 0; u < 4; ++u) { |
| 103 | int e = is * 32 + t * 4 + u; |
| 104 | uint code = (q[e >> 3] >> ((e & 7) * 4)) & 0xF; |
| 105 | pk |= (code & 0xFF) << (u * 8); |
| 106 | } |
| 107 | cw[t] = pk; |
| 108 | } |
| 109 | #pragma unroll |
| 110 | for (int j = 0; j < BN_DP; ++j) { |
| 111 | __local uint * a = sh_qa + j * 64 + is * 8; |
| 112 | int raw = 0; |
| 113 | #pragma unroll |
| 114 | for (int t = 0; t < 8; ++t) raw = dot_acc_sat_4x8packed_ss_int(cw[t], a[t], raw); |
| 115 | acc[j] += scl * (float)sh_qad[j*8 + is] * (float)raw |
| 116 | - mnv * (float)sh_qas[j*8 + is]; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | barrier(CLK_LOCAL_MEM_FENCE); |
| 121 | } |
| 122 | |
| 123 | if (row_ok) { |
| 124 | dst = (global float*)((global char*)dst + offsetd); |
| 125 | #pragma unroll |
| 126 | for (int j = 0; j < BN_DP; ++j) { |
| 127 | int c = col0 + j; |
| 128 | if (c < ne11) dst[(ulong)c * ne01 + row] = acc[j]; |
| 129 | } |
| 130 | } |
| 131 | } |