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_q5_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_q5_k_f32.cl` 187 lines ยท 6261 bytes Raw Blame History 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 #define QK_K 256 22 #define K_SCALE_SIZE 12 23 24 typedef struct { 25 half d; // super-block scale for quantized scales 26 half dmin; // super-block scale for quantized mins 27 uchar scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits 28 uchar qh[QK_K/8]; // quants, high bit (1 bit per value, packed 8 per byte) 29 uchar qs[QK_K/2]; // quants, low 4 bits (2 values per byte) 30 } block_q5_K; 31 32 #undef N_DST 33 #undef N_SIMDGROUP 34 #undef N_SIMDWIDTH 35 36 #ifdef INTEL_GPU 37 #define N_DST 4 38 #define N_SIMDGROUP 1 39 #define N_SIMDWIDTH 16 40 #elif defined(ADRENO_GPU) 41 #define N_DST 4 42 #define N_SIMDGROUP 1 43 #define N_SIMDWIDTH 64 44 #endif 45 46 #define BLOCK_STRIDE (N_SIMDWIDTH/8) 47 48 #ifdef INTEL_GPU 49 REQD_SUBGROUP_SIZE_16 50 #elif defined (ADRENO_GPU) 51 REQD_SUBGROUP_SIZE_64 52 #endif 53 kernel void kernel_mul_mv_q5_K_f32( 54 global char * src0, 55 int offset0, 56 global char * src1, 57 int offset1, 58 global char * dst, 59 int offsetd, 60 int ne00, 61 int ne01, 62 ulong nb01, 63 ulong nb02, 64 ulong nb03, 65 int ne12, 66 ulong nb11, 67 ulong nb12, 68 ulong nb13, 69 int ne0, 70 int ne1, 71 int r2, 72 int r3 73 ) { 74 src0 = src0 + offset0; 75 src1 = src1 + offset1; 76 dst = dst + offsetd; 77 78 ushort kmask1 = 0x3f3f; 79 ushort kmask2 = 0x0f0f; 80 ushort kmask3 = 0xc0c0; 81 82 int ix = get_sub_group_local_id()/8; // super block index 83 int it = get_sub_group_local_id()%8; // block index (inside super block) 84 int iq = it/4; // 0 or 1 - first or second half of the super block 85 int ir = it%4; // 0...3 - block index in the half super block 86 87 int nb = ne00/QK_K; 88 89 int r0 = get_group_id(0); 90 int r1 = get_group_id(1); 91 int im = get_group_id(2); 92 int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST; 93 94 int i12 = im%ne12; 95 int i13 = im/ne12; 96 97 int offset_src0 = first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03; 98 int offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13; 99 100 global block_q5_K * x = (global block_q5_K *) (src0 + offset_src0); 101 global float * y = (global float *) (src1 + offset_src1); 102 103 float yl[16]; 104 float yh[16]; 105 float sumf[N_DST] = {0.f}; 106 float all_sum; 107 108 global float * y4 = y + ix * QK_K + 64 * iq + 8 * ir; 109 110 uchar u1_lo = (uchar)(1 << (2*iq)); 111 uchar u2_lo = (uchar)(2 << (2*iq)); 112 uchar u1_hi = (uchar)(1 << (2*iq + 4)); 113 uchar u2_hi = (uchar)(2 << (2*iq + 4)); 114 115 ushort sc16[4]; 116 uchar * sc8 = (uchar *)sc16; 117 118 for (int ib = ix; ib < nb; ib += BLOCK_STRIDE) { 119 float4 sumy = {0.f, 0.f, 0.f, 0.f}; 120 for (int i = 0; i < 8; ++i) { 121 yl[i+0] = y4[i+0]; 122 sumy.s0 += yl[i+0]; 123 124 yl[i+8] = y4[i+32]; 125 sumy.s1 += yl[i+8]; 126 127 yh[i+0] = y4[i+128]; 128 sumy.s2 += yh[i+0]; 129 130 yh[i+8] = y4[i+160]; 131 sumy.s3 += yh[i+8]; 132 } 133 134 global ushort * sc = (global ushort *)x[ib].scales + iq; 135 global ushort * q1 = (global ushort *)x[ib].qs + 16 * iq + 4 * ir; 136 global uchar * qh = x[ib].qh + 8 * ir; 137 global half * dh = &x[ib].d; 138 139 for (int row = 0; row < N_DST; row++) { 140 sc16[0] = sc[0] & kmask1; 141 sc16[1] = sc[2] & kmask1; 142 sc16[2] = ((sc[4] >> 0) & kmask2) | ((sc[0] & kmask3) >> 2); 143 sc16[3] = ((sc[4] >> 4) & kmask2) | ((sc[2] & kmask3) >> 2); 144 145 global ushort * q2 = q1 + 32; 146 147 float4 acc1 = {0.f, 0.f, 0.f, 0.f}; 148 float4 acc2 = {0.f, 0.f, 0.f, 0.f}; 149 for (int i = 0; i < 8; i += 2) { 150 acc1.s0 += yl[i+0] * ((q1[i/2] & 0x000F) + (qh[i+0] & u1_lo ? 16.f : 0.f)); 151 acc1.s1 += yl[i+1] * ((q1[i/2] & 0x0F00) + (qh[i+1] & u1_lo ? 16.f*256.f : 0.f)); 152 acc1.s2 += yl[i+8] * ((q1[i/2] & 0x00F0) + (qh[i+0] & u2_lo ? 16.f*16.f : 0.f)); 153 acc1.s3 += yl[i+9] * ((q1[i/2] & 0xF000) + (qh[i+1] & u2_lo ? 16.f*4096.f: 0.f)); 154 acc2.s0 += yh[i+0] * ((q2[i/2] & 0x000F) + (qh[i+0] & u1_hi ? 16.f : 0.f)); 155 acc2.s1 += yh[i+1] * ((q2[i/2] & 0x0F00) + (qh[i+1] & u1_hi ? 16.f*256.f : 0.f)); 156 acc2.s2 += yh[i+8] * ((q2[i/2] & 0x00F0) + (qh[i+0] & u2_hi ? 16.f*16.f : 0.f)); 157 acc2.s3 += yh[i+9] * ((q2[i/2] & 0xF000) + (qh[i+1] & u2_hi ? 16.f*4096.f: 0.f)); 158 } 159 160 float dall = dh[0]; 161 float dmin = dh[1]; 162 sumf[row] += dall * ((acc1.s0 + 1.f/256.f * acc1.s1) * sc8[0] + 163 (acc1.s2 + 1.f/256.f * acc1.s3) * sc8[1] * 1.f/16.f + 164 (acc2.s0 + 1.f/256.f * acc2.s1) * sc8[4] + 165 (acc2.s2 + 1.f/256.f * acc2.s3) * sc8[5] * 1.f/16.f) - 166 dmin * (sumy.s0 * sc8[2] + sumy.s1 * sc8[3] + sumy.s2 * sc8[6] + sumy.s3 * sc8[7]); 167 168 q1 += nb01/2; 169 sc += nb01/2; 170 dh += nb01/2; 171 qh += nb01; 172 } 173 174 y4 += BLOCK_STRIDE * QK_K; 175 } 176 177 global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0; 178 179 for (int row = 0; row < N_DST; ++row) { 180 all_sum = sub_group_reduce_add(sumf[row]); 181 if (first_row + row < ne01) { 182 if (get_sub_group_local_id() == 0) { 183 dst_f32[first_row + row] = all_sum; 184 } 185 } 186 } 187 }