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 / mul_mv_q4_k_f32_flat.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | |
| 3 | #ifdef cl_intel_subgroups |
| 4 | #pragma OPENCL EXTENSION cl_intel_subgroups : enable |
| 5 | #else |
| 6 | #pragma OPENCL EXTENSION cl_khr_subgroups : enable |
| 7 | #endif |
| 8 | |
| 9 | #ifdef cl_intel_required_subgroup_size |
| 10 | #pragma OPENCL EXTENSION cl_intel_required_subgroup_size : enable |
| 11 | #define INTEL_GPU 1 |
| 12 | #define REQD_SUBGROUP_SIZE_16 __attribute__((intel_reqd_sub_group_size(16))) |
| 13 | #define REQD_SUBGROUP_SIZE_32 __attribute__((intel_reqd_sub_group_size(32))) |
| 14 | #elif defined(cl_qcom_reqd_sub_group_size) |
| 15 | #pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable |
| 16 | #define ADRENO_GPU 1 |
| 17 | #define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half"))) |
| 18 | #define REQD_SUBGROUP_SIZE_128 __attribute__((qcom_reqd_sub_group_size("full"))) |
| 19 | #endif |
| 20 | |
| 21 | //------------------------------------------------------------------------------ |
| 22 | // block_q4_K |
| 23 | //------------------------------------------------------------------------------ |
| 24 | #define QK_K 256 |
| 25 | #define BLOCK_Q4K_SIZE 144 |
| 26 | #define K_SCALE_SIZE 12 |
| 27 | |
| 28 | // 8 blocks of 32 elements each |
| 29 | // weight is represented as x = a * q + b |
| 30 | typedef struct { |
| 31 | half d; // super-block scale for quantized scales |
| 32 | half dmin; // super-block scale for quantized mins |
| 33 | |
| 34 | uchar scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits |
| 35 | uchar qs[QK_K/2]; // 4-bit quants |
| 36 | } block_q4_K; |
| 37 | |
| 38 | #undef N_DST |
| 39 | #undef N_SIMDGROUP |
| 40 | #undef N_SIMDWIDTH |
| 41 | |
| 42 | #ifdef INTEL_GPU |
| 43 | #define N_DST 16 // number of rows each SIMD group works on (Intel: 8->16, 2x further activation reuse; 32 spills registers) |
| 44 | #define N_SIMDGROUP 1 // number of SIMD groups in a thread group |
| 45 | #define N_SIMDWIDTH 16 // SIMD group size |
| 46 | #elif defined (ADRENO_GPU) |
| 47 | #define N_DST 16 |
| 48 | #define N_SIMDGROUP 2 |
| 49 | #define N_SIMDWIDTH 64 |
| 50 | #endif |
| 51 | |
| 52 | #undef BLOCK_STRIDE |
| 53 | // number of (super) blocks each subgroup processes |
| 54 | // each thread in a subgroup processes a block (32 weights) |
| 55 | #define BLOCK_STRIDE (N_SIMDWIDTH/8) |
| 56 | |
| 57 | #ifdef INTEL_GPU |
| 58 | REQD_SUBGROUP_SIZE_16 |
| 59 | #elif defined (ADRENO_GPU) |
| 60 | REQD_SUBGROUP_SIZE_64 |
| 61 | #endif |
| 62 | kernel void kernel_mul_mv_q4_K_f32_flat( |
| 63 | global uchar * src0_q, |
| 64 | global uchar * src0_s, |
| 65 | global half * src0_d, |
| 66 | global half * src0_dm, |
| 67 | global char * src1, |
| 68 | int offset1, |
| 69 | global char * dst, |
| 70 | int offsetd, |
| 71 | int ne00, |
| 72 | int ne01, |
| 73 | ulong nb01, |
| 74 | ulong nb02, |
| 75 | ulong nb03, |
| 76 | int ne12, |
| 77 | ulong nb11, |
| 78 | ulong nb12, |
| 79 | ulong nb13, |
| 80 | int ne0, |
| 81 | int ne1, |
| 82 | int r2, |
| 83 | int r3 |
| 84 | ) { |
| 85 | src1 = src1 + offset1; |
| 86 | dst = dst + offsetd; |
| 87 | |
| 88 | ushort kmask1 = 0x3f3f; |
| 89 | ushort kmask2 = 0x0f0f; |
| 90 | ushort kmask3 = 0xc0c0; |
| 91 | |
| 92 | int ix = get_sub_group_local_id()/8; |
| 93 | int it = get_sub_group_local_id()%8; |
| 94 | int iq = it/4; |
| 95 | int ir = it%4; |
| 96 | |
| 97 | int nb = ne00/QK_K; |
| 98 | |
| 99 | int r0 = get_group_id(0); |
| 100 | int r1 = get_group_id(1); |
| 101 | int im = get_group_id(2); |
| 102 | int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST; |
| 103 | |
| 104 | int i12 = im%ne12; |
| 105 | int i13 = im/ne12; |
| 106 | |
| 107 | int offset_src0 = (first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03)/BLOCK_Q4K_SIZE; |
| 108 | uint blk = nb01 / BLOCK_Q4K_SIZE; |
| 109 | global uchar * blk_q = (global uchar *)src0_q + offset_src0*(QK_K/2); |
| 110 | global uchar * blk_s = (global uchar *)src0_s + offset_src0*K_SCALE_SIZE; |
| 111 | global half * blk_d = (global half *)src0_d + offset_src0; |
| 112 | global half * blk_dm = (global half *)src0_dm + offset_src0; |
| 113 | |
| 114 | int offset_src1 = r1*nb11 + (i12)*nb12 + (i13)*nb13; |
| 115 | global float * y = (global float *)(src1 + offset_src1); |
| 116 | |
| 117 | float yl[16]; |
| 118 | float yh[16]; |
| 119 | float sumf[N_DST] = {0.f}; |
| 120 | float all_sum; |
| 121 | |
| 122 | global float * y4 = y + ix * QK_K + 64 * iq + 8 * ir; |
| 123 | |
| 124 | ushort sc16[4]; |
| 125 | uchar * sc8 = (uchar *)sc16; |
| 126 | |
| 127 | for (int ib = ix; ib < nb; ib += BLOCK_STRIDE) { |
| 128 | float4 sumy = {0.f, 0.f, 0.f, 0.f}; |
| 129 | for (int i = 0; i < 8; ++i) { |
| 130 | yl[i+0] = y4[i+0]; |
| 131 | sumy.s0 += yl[i+0]; |
| 132 | |
| 133 | yl[i+8] = y4[i+32]; |
| 134 | sumy.s1 += yl[i+8]; |
| 135 | |
| 136 | yh[i+0] = y4[i+128]; |
| 137 | sumy.s2 += yh[i+0]; |
| 138 | |
| 139 | yh[i+8] = y4[i+160]; |
| 140 | sumy.s3 += yh[i+8]; |
| 141 | } |
| 142 | |
| 143 | global ushort * q1 = (global ushort *)(blk_q + ib * (QK_K/2)) + (16 * iq + 4 * ir); |
| 144 | global ushort * sc = (global ushort *)(blk_s + ib * K_SCALE_SIZE) + iq; |
| 145 | global half * d = blk_d + ib; |
| 146 | global half * dm = blk_dm + ib; |
| 147 | |
| 148 | for (int row = 0; row < N_DST; row++) { |
| 149 | sc16[0] = sc[0] & kmask1; |
| 150 | sc16[1] = sc[2] & kmask1; |
| 151 | sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2); |
| 152 | sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2); |
| 153 | |
| 154 | global ushort * q2 = q1 + 32; |
| 155 | |
| 156 | // Load the 4 q1 / 4 q2 quant ushorts as 2 uints each. 16-bit integer ops are |
| 157 | // disproportionately slow on the A7X (E031.41) compiler; keeping the dequant |
| 158 | // operands in 32-bit registers avoids the ushort path. q1/q2 are 4-byte aligned |
| 159 | // (ib*128 + (32*iq+8*ir) bytes; q1 += blk*128 bytes/row). Math is unchanged: |
| 160 | // w & 0x0F00 on the low/high halves equals the original ushort mask value. |
| 161 | global uint * q1u = (global uint *)q1; |
| 162 | global uint * q2u = (global uint *)q2; |
| 163 | uint a0 = q1u[0], a1 = q1u[1]; |
| 164 | uint b0 = q2u[0], b1 = q2u[1]; |
| 165 | uint w0 = a0 & 0xFFFF, w1 = a0 >> 16, w2 = a1 & 0xFFFF, w3 = a1 >> 16; |
| 166 | uint v0 = b0 & 0xFFFF, v1 = b0 >> 16, v2 = b1 & 0xFFFF, v3 = b1 >> 16; |
| 167 | |
| 168 | float4 acc1, acc2; |
| 169 | acc1.s0 = yl[0]*(w0&0x000F) + yl[ 2]*(w1&0x000F) + yl[ 4]*(w2&0x000F) + yl[ 6]*(w3&0x000F); |
| 170 | acc1.s1 = yl[1]*(w0&0x0F00) + yl[ 3]*(w1&0x0F00) + yl[ 5]*(w2&0x0F00) + yl[ 7]*(w3&0x0F00); |
| 171 | acc1.s2 = yl[8]*(w0&0x00F0) + yl[10]*(w1&0x00F0) + yl[12]*(w2&0x00F0) + yl[14]*(w3&0x00F0); |
| 172 | acc1.s3 = yl[9]*(w0&0xF000) + yl[11]*(w1&0xF000) + yl[13]*(w2&0xF000) + yl[15]*(w3&0xF000); |
| 173 | acc2.s0 = yh[0]*(v0&0x000F) + yh[ 2]*(v1&0x000F) + yh[ 4]*(v2&0x000F) + yh[ 6]*(v3&0x000F); |
| 174 | acc2.s1 = yh[1]*(v0&0x0F00) + yh[ 3]*(v1&0x0F00) + yh[ 5]*(v2&0x0F00) + yh[ 7]*(v3&0x0F00); |
| 175 | acc2.s2 = yh[8]*(v0&0x00F0) + yh[10]*(v1&0x00F0) + yh[12]*(v2&0x00F0) + yh[14]*(v3&0x00F0); |
| 176 | acc2.s3 = yh[9]*(v0&0xF000) + yh[11]*(v1&0xF000) + yh[13]*(v2&0xF000) + yh[15]*(v3&0xF000); |
| 177 | |
| 178 | float dall = *d; |
| 179 | float dmin = *dm; |
| 180 | sumf[row] += dall * ((acc1.s0 + 1.f/256.f * acc1.s1) * sc8[0] + |
| 181 | (acc1.s2 + 1.f/256.f * acc1.s3) * sc8[1] * 1.f/16.f + |
| 182 | (acc2.s0 + 1.f/256.f * acc2.s1) * sc8[4] + |
| 183 | (acc2.s2 + 1.f/256.f * acc2.s3) * sc8[5] * 1.f/16.f) - |
| 184 | dmin * (sumy.s0 * sc8[2] + sumy.s1 * sc8[3] + sumy.s2 * sc8[6] + sumy.s3 * sc8[7]); |
| 185 | |
| 186 | q1 += blk*64; |
| 187 | sc += blk*6; |
| 188 | d += blk; |
| 189 | dm += blk; |
| 190 | } |
| 191 | |
| 192 | y4 += BLOCK_STRIDE * QK_K; |
| 193 | } |
| 194 | |
| 195 | global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0; |
| 196 | |
| 197 | for (int row = 0; row < N_DST; ++row) { |
| 198 | all_sum = sub_group_reduce_add(sumf[row]); |
| 199 | if (first_row + row < ne01) { |
| 200 | if (get_sub_group_local_id() == 0) { |
| 201 | dst_f32[first_row + row] = all_sum; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |