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_1_f32.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 | #define QK4_1 32 |
| 22 | |
| 23 | struct block_q4_1 { |
| 24 | half d; // delta |
| 25 | half m; // min |
| 26 | uchar qs[QK4_1 / 2]; // nibbles / quants |
| 27 | }; |
| 28 | |
| 29 | inline float block_q4_1_dot_y( |
| 30 | global const struct block_q4_1 * qb_curr, |
| 31 | float sumy, |
| 32 | float16 yl, |
| 33 | int il |
| 34 | ) { |
| 35 | float d = qb_curr->d; |
| 36 | float m = qb_curr->m; |
| 37 | |
| 38 | float4 acc = (float4)(0.0f, 0.0f, 0.0f, 0.0f); |
| 39 | |
| 40 | global const ushort * qs = ((global const ushort *) qb_curr + 2 + il/2); |
| 41 | |
| 42 | acc.s0 += yl.s0 * (qs[0] & 0x000F); |
| 43 | acc.s0 += yl.s1 * (qs[0] & 0x0F00); |
| 44 | acc.s0 += yl.s8 * (qs[0] & 0x00F0); |
| 45 | acc.s3 += yl.s9 * (qs[0] & 0xF000); |
| 46 | |
| 47 | acc.s0 += yl.s2 * (qs[1] & 0x000F); |
| 48 | acc.s1 += yl.s3 * (qs[1] & 0x0F00); |
| 49 | acc.s2 += yl.sa * (qs[1] & 0x00F0); |
| 50 | acc.s3 += yl.sb * (qs[1] & 0xF000); |
| 51 | |
| 52 | acc.s0 += yl.s4 * (qs[2] & 0x000F); |
| 53 | acc.s1 += yl.s5 * (qs[2] & 0x0F00); |
| 54 | acc.s2 += yl.sc * (qs[2] & 0x00F0); |
| 55 | acc.s3 += yl.sd * (qs[2] & 0xF000); |
| 56 | |
| 57 | acc.s0 += yl.s6 * (qs[3] & 0x000F); |
| 58 | acc.s1 += yl.s7 * (qs[3] & 0x0F00); |
| 59 | acc.s2 += yl.se * (qs[3] & 0x00F0); |
| 60 | acc.s3 += yl.sf * (qs[3] & 0xF000); |
| 61 | |
| 62 | return d * (acc.s0 + acc.s1 + acc.s2 + acc.s3) + sumy * m; |
| 63 | } |
| 64 | |
| 65 | #undef N_DST |
| 66 | #undef N_SIMDGROUP |
| 67 | #undef N_SIMDWIDTH |
| 68 | |
| 69 | #ifdef INTEL_GPU |
| 70 | #define N_DST 4 // each subgroup works on 4 rows |
| 71 | #define N_SIMDGROUP 1 // number of subgroups in a thread group |
| 72 | #define N_SIMDWIDTH 16 // assuming subgroup size is 16 |
| 73 | #elif defined (ADRENO_GPU) |
| 74 | #define N_DST 4 |
| 75 | #define N_SIMDGROUP 1 |
| 76 | #define N_SIMDWIDTH 64 |
| 77 | #endif |
| 78 | |
| 79 | inline void mul_vec_q_n_f32( |
| 80 | global void * src0, |
| 81 | global float * src1, |
| 82 | global float * dst, |
| 83 | int ne00, |
| 84 | int ne01, |
| 85 | int ne02, |
| 86 | int ne10, |
| 87 | int ne12, |
| 88 | int ne0, |
| 89 | int ne1, |
| 90 | int r2, |
| 91 | int r3 |
| 92 | ) { |
| 93 | const ulong nb = ne00/QK4_1; |
| 94 | |
| 95 | int r0 = get_group_id(0); |
| 96 | int r1 = get_group_id(1); |
| 97 | int im = get_group_id(2); |
| 98 | |
| 99 | int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST; |
| 100 | |
| 101 | int i12 = im%ne12; |
| 102 | int i13 = im/ne12; |
| 103 | |
| 104 | ulong offset0 = first_row * nb + (i12/r2)*(nb*ne01) + (i13/r3)*(nb*ne01*ne02); |
| 105 | |
| 106 | global struct block_q4_1 * x = (global struct block_q4_1 *) src0 + offset0; |
| 107 | global float * y = (global float *) src1 + r1*ne10 + im*ne00*ne1; |
| 108 | |
| 109 | float16 yl; |
| 110 | float4 sumf = (float4)(0.f, 0.f, 0.f, 0.f); |
| 111 | |
| 112 | int ix = get_sub_group_local_id()/2; |
| 113 | int il = 8*(get_sub_group_local_id()%2); |
| 114 | |
| 115 | global float * yb = y + ix * QK4_1 + il; |
| 116 | |
| 117 | for (int ib = ix; ib < nb; ib += N_SIMDWIDTH/2) { |
| 118 | float sumy = 0; |
| 119 | |
| 120 | sumy += yb[0]; |
| 121 | sumy += yb[1]; |
| 122 | sumy += yb[2]; |
| 123 | sumy += yb[3]; |
| 124 | sumy += yb[4]; |
| 125 | sumy += yb[5]; |
| 126 | sumy += yb[6]; |
| 127 | sumy += yb[7]; |
| 128 | |
| 129 | sumy += yb[16]; |
| 130 | sumy += yb[17]; |
| 131 | sumy += yb[18]; |
| 132 | sumy += yb[19]; |
| 133 | sumy += yb[20]; |
| 134 | sumy += yb[21]; |
| 135 | sumy += yb[22]; |
| 136 | sumy += yb[23]; |
| 137 | |
| 138 | |
| 139 | yl.s0 = yb[0]; |
| 140 | yl.s1 = yb[1]/256.f; |
| 141 | |
| 142 | yl.s2 = yb[2]; |
| 143 | yl.s3 = yb[3]/256.f; |
| 144 | |
| 145 | yl.s4 = yb[4]; |
| 146 | yl.s5 = yb[5]/256.f; |
| 147 | |
| 148 | yl.s6 = yb[6]; |
| 149 | yl.s7 = yb[7]/256.f; |
| 150 | |
| 151 | yl.s8 = yb[16]/16.f; |
| 152 | yl.s9 = yb[17]/4096.f; |
| 153 | |
| 154 | yl.sa = yb[18]/16.f; |
| 155 | yl.sb = yb[19]/4096.f; |
| 156 | |
| 157 | yl.sc = yb[20]/16.f; |
| 158 | yl.sd = yb[21]/4096.f; |
| 159 | |
| 160 | yl.se = yb[22]/16.f; |
| 161 | yl.sf = yb[23]/4096.f; |
| 162 | |
| 163 | sumf.s0 += block_q4_1_dot_y(x+ib+0*nb, sumy, yl, il); |
| 164 | sumf.s1 += block_q4_1_dot_y(x+ib+1*nb, sumy, yl, il); |
| 165 | sumf.s2 += block_q4_1_dot_y(x+ib+2*nb, sumy, yl, il); |
| 166 | sumf.s3 += block_q4_1_dot_y(x+ib+3*nb, sumy, yl, il); |
| 167 | |
| 168 | yb += QK4_1 * (N_SIMDWIDTH/2); |
| 169 | } |
| 170 | |
| 171 | float4 tot = (float4)( |
| 172 | sub_group_reduce_add(sumf.s0), sub_group_reduce_add(sumf.s1), |
| 173 | sub_group_reduce_add(sumf.s2), sub_group_reduce_add(sumf.s3) |
| 174 | ); |
| 175 | |
| 176 | if (get_sub_group_local_id() == 0) { |
| 177 | if (first_row + 0 < ne01) { |
| 178 | dst[r1*ne0 + im*ne0*ne1 + first_row + 0] = tot.s0; |
| 179 | } |
| 180 | if (first_row + 1 < ne01) { |
| 181 | dst[r1*ne0 + im*ne0*ne1 + first_row + 1] = tot.s1; |
| 182 | } |
| 183 | if (first_row + 2 < ne01) { |
| 184 | dst[r1*ne0 + im*ne0*ne1 + first_row + 2] = tot.s2; |
| 185 | } |
| 186 | if (first_row + 3 < ne01) { |
| 187 | dst[r1*ne0 + im*ne0*ne1 + first_row + 3] = tot.s3; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | #ifdef INTEL_GPU |
| 193 | REQD_SUBGROUP_SIZE_16 |
| 194 | #elif defined (ADRENO_GPU) |
| 195 | REQD_SUBGROUP_SIZE_64 |
| 196 | #endif |
| 197 | kernel void kernel_mul_mv_q4_1_f32( |
| 198 | global void * src0, |
| 199 | ulong offset0, |
| 200 | global float * src1, |
| 201 | ulong offset1, |
| 202 | global float * dst, |
| 203 | ulong offsetd, |
| 204 | int ne00, |
| 205 | int ne01, |
| 206 | int ne02, |
| 207 | int ne10, |
| 208 | int ne12, |
| 209 | int ne0, |
| 210 | int ne1, |
| 211 | int r2, |
| 212 | int r3 |
| 213 | ) { |
| 214 | src0 = (global void*)((global char*)src0 + offset0); |
| 215 | src1 = (global float*)((global char*)src1 + offset1); |
| 216 | dst = (global float*)((global char*)dst + offsetd); |
| 217 | |
| 218 | mul_vec_q_n_f32(src0, src1, dst, ne00, ne01, ne02, ne10, ne12, ne0, ne1, r2, r3); |
| 219 | } |