Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-opencl/kernels/mul_mv_q4_k_f32.cl # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-opencl / kernels / mul_mv_q4_k_f32.cl` 182 lines ยท 5876 bytes Raw Blame History 1 #pragma OPENCL EXTENSION cl_khr_fp16 : enable 2 3 #ifdef cl_intel_required_subgroup_size 4 #pragma OPENCL EXTENSION cl_intel_required_subgroup_size : enable 5 #define INTEL_GPU 1 6 #define REQD_SUBGROUP_SIZE_16 __attribute__((intel_reqd_sub_group_size(16))) 7 #define REQD_SUBGROUP_SIZE_32 __attribute__((intel_reqd_sub_group_size(32))) 8 #elif defined(cl_qcom_reqd_sub_group_size) 9 #pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable 10 #define ADRENO_GPU 1 11 #define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half"))) 12 #define REQD_SUBGROUP_SIZE_128 __attribute__((qcom_reqd_sub_group_size("full"))) 13 #endif 14 15 //------------------------------------------------------------------------------ 16 // block_q4_K 17 //------------------------------------------------------------------------------ 18 #define QK_K 256 19 #define K_SCALE_SIZE 12 20 21 // 8 blocks of 32 elements each 22 // weight is represented as x = a * q + b 23 typedef struct { 24 half d; // super-block scale for quantized scales 25 half dmin; // super-block scale for quantized mins 26 27 uchar scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits 28 uchar qs[QK_K/2]; // 4-bit quants 29 } block_q4_K; 30 31 #undef N_DST 32 #undef N_SIMDGROUP 33 #undef N_SIMDWIDTH 34 35 #ifdef INTEL_GPU 36 #define N_DST 4 // number of rows each SIMD group works on 37 #define N_SIMDGROUP 1 // number of SIMD groups in a thread group 38 #define N_SIMDWIDTH 16 // SIMD group size 39 #elif defined (ADRENO_GPU) 40 #define N_DST 4 41 #define N_SIMDGROUP 1 42 #define N_SIMDWIDTH 64 43 #endif 44 45 #undef BLOCK_STRIDE 46 // number of (super) blocks each subgroup processes 47 // each thread in a subgroup processes a block (32 weights) 48 #define BLOCK_STRIDE (N_SIMDWIDTH/8) 49 50 #ifdef INTEL_GPU 51 REQD_SUBGROUP_SIZE_16 52 #elif defined (ADRENO_GPU) 53 REQD_SUBGROUP_SIZE_64 54 #endif 55 kernel void kernel_mul_mv_q4_K_f32( 56 global char * src0, 57 int offset0, 58 global char * src1, 59 int offset1, 60 global char * dst, 61 int offsetd, 62 int ne00, 63 int ne01, 64 ulong nb01, 65 ulong nb02, 66 ulong nb03, 67 int ne12, 68 ulong nb11, 69 ulong nb12, 70 ulong nb13, 71 int ne0, 72 int ne1, 73 int r2, 74 int r3 75 ) { 76 src0 = src0 + offset0; 77 src1 = src1 + offset1; 78 dst = dst + offsetd; 79 80 ushort kmask1 = 0x3f3f; 81 ushort kmask2 = 0x0f0f; 82 ushort kmask3 = 0xc0c0; 83 84 int ix = get_sub_group_local_id()/8; // super block index 85 int it = get_sub_group_local_id()%8; // block index (inside super block) 86 int iq = it/4; // 0 or 1 - first or second half of the super block 87 int ir = it%4; // 0...3 - block index in the half super block 88 89 int nb = ne00/QK_K; 90 91 int r0 = get_group_id(0); 92 int r1 = get_group_id(1); 93 int im = get_group_id(2); 94 int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST; 95 96 int i12 = im%ne12; 97 int i13 = im/ne12; 98 99 int offset_src0 = first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03; 100 int offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13; 101 102 global block_q4_K * x = (global block_q4_K *) (src0 + offset_src0); 103 global float * y = (global float *) (src1 + offset_src1); 104 105 float yl[16]; 106 float yh[16]; 107 float sumf[N_DST] = {0.f}; 108 float all_sum; 109 110 global float * y4 = y + ix * QK_K + 64 * iq + 8 * ir; 111 112 ushort sc16[4]; 113 uchar * sc8 = (uchar *)sc16; 114 115 for (int ib = ix; ib < nb; ib += BLOCK_STRIDE) { 116 float4 sumy = {0.f, 0.f, 0.f, 0.f}; 117 for (int i = 0; i < 8; ++i) { 118 yl[i+0] = y4[i+0]; 119 sumy.s0 += yl[i+0]; 120 121 yl[i+8] = y4[i+32]; 122 sumy.s1 += yl[i+8]; 123 124 yh[i+0] = y4[i+128]; 125 sumy.s2 += yh[i+0]; 126 127 yh[i+8] = y4[i+160]; 128 sumy.s3 += yh[i+8]; 129 } 130 131 global ushort * sc = (global ushort *)x[ib].scales + iq; 132 global ushort * q1 = (global ushort *)x[ib].qs + 16 * iq + 4 * ir; 133 global half * dh = &x[ib].d; 134 135 for (int row = 0; row < N_DST; row++) { 136 sc16[0] = sc[0] & kmask1; 137 sc16[1] = sc[2] & kmask1; 138 sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2); 139 sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2); 140 141 global ushort * q2 = q1 + 32; 142 143 float4 acc1 = {0.f, 0.f, 0.f, 0.f}; 144 float4 acc2 = {0.f, 0.f, 0.f, 0.f}; 145 for (int i = 0; i < 8; i += 2) { 146 acc1.s0 += yl[i+0] * (q1[i/2] & 0x000F); 147 acc1.s1 += yl[i+1] * (q1[i/2] & 0x0F00); 148 acc1.s2 += yl[i+8] * (q1[i/2] & 0x00F0); 149 acc1.s3 += yl[i+9] * (q1[i/2] & 0xF000); 150 acc2.s0 += yh[i+0] * (q2[i/2] & 0x000F); 151 acc2.s1 += yh[i+1] * (q2[i/2] & 0x0F00); 152 acc2.s2 += yh[i+8] * (q2[i/2] & 0x00F0); 153 acc2.s3 += yh[i+9] * (q2[i/2] & 0xF000); 154 } 155 156 float dall = dh[0]; 157 float dmin = dh[1]; 158 sumf[row] += dall * ((acc1.s0 + 1.f/256.f * acc1.s1) * sc8[0] + 159 (acc1.s2 + 1.f/256.f * acc1.s3) * sc8[1] * 1.f/16.f + 160 (acc2.s0 + 1.f/256.f * acc2.s1) * sc8[4] + 161 (acc2.s2 + 1.f/256.f * acc2.s3) * sc8[5] * 1.f/16.f) - 162 dmin * (sumy.s0 * sc8[2] + sumy.s1 * sc8[3] + sumy.s2 * sc8[6] + sumy.s3 * sc8[7]); 163 164 q1 += nb01/2; 165 sc += nb01/2; 166 dh += nb01/2; 167 } 168 169 y4 += BLOCK_STRIDE * QK_K; 170 } 171 172 global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0; 173 174 for (int row = 0; row < N_DST; ++row) { 175 all_sum = sub_group_reduce_add(sumf[row]); 176 if (first_row + row < ne01) { 177 if (get_sub_group_local_id() == 0) { 178 dst_f32[first_row + row] = all_sum; 179 } 180 } 181 } 182 }