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_q2_k_f32.cl
| 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_q2_K |
| 17 | //------------------------------------------------------------------------------ |
| 18 | #define QK_K 256 |
| 19 | |
| 20 | // 16 blocks of 16 elements each |
| 21 | // weight is represented as x = a * q + b |
| 22 | typedef struct { |
| 23 | uchar scales[QK_K/16]; // scales and mins, quantized with 4 bits |
| 24 | uchar qs[QK_K/4]; // 2-bit quants |
| 25 | half d; // super-block scale for quantized scales |
| 26 | half dmin; // super-block scale for quantized mins |
| 27 | } block_q2_K; |
| 28 | |
| 29 | #undef N_DST |
| 30 | #undef N_SIMDGROUP |
| 31 | #undef N_SIMDWIDTH |
| 32 | |
| 33 | #ifdef INTEL_GPU |
| 34 | #define N_DST 4 // number of rows each SIMD group works on |
| 35 | #define N_SIMDGROUP 1 // number of SIMD groups in a thread group |
| 36 | #define N_SIMDWIDTH 16 // SIMD group size |
| 37 | #elif defined (ADRENO_GPU) |
| 38 | #define N_DST 4 |
| 39 | #define N_SIMDGROUP 1 |
| 40 | #define N_SIMDWIDTH 64 |
| 41 | #endif |
| 42 | |
| 43 | #undef BLOCK_STRIDE |
| 44 | // number of (super) blocks each subgroup processes |
| 45 | // 8 threads cover one super block, so a wave covers N_SIMDWIDTH/8 of them |
| 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_q2_K_f32( |
| 54 | global char * src0, |
| 55 | ulong offset0, |
| 56 | global char * src1, |
| 57 | ulong offset1, |
| 58 | global char * dst, |
| 59 | ulong 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 | int ix = get_sub_group_local_id()/8; // super block index |
| 79 | int it = get_sub_group_local_id()%8; // block index (inside super block) |
| 80 | int iq = it/4; // 0 or 1 - first or second half of the super block |
| 81 | int ir = it%4; // 0...3 - block index in the half super block |
| 82 | int is = (8*ir)/16; // 0 or 1 - scale pair |
| 83 | |
| 84 | int nb = ne00/QK_K; |
| 85 | |
| 86 | int r0 = get_group_id(0); |
| 87 | int r1 = get_group_id(1); |
| 88 | int im = get_group_id(2); |
| 89 | int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST; |
| 90 | |
| 91 | int i12 = im%ne12; |
| 92 | int i13 = im/ne12; |
| 93 | |
| 94 | ulong offset_src0 = first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03; |
| 95 | ulong offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13; |
| 96 | |
| 97 | global block_q2_K * x = (global block_q2_K *) (src0 + offset_src0); |
| 98 | global float * y = (global float *) (src1 + offset_src1); |
| 99 | |
| 100 | float yl[32]; |
| 101 | float sumf[N_DST] = {0.f}; |
| 102 | float all_sum; |
| 103 | |
| 104 | global float * y4 = y + ix * QK_K + 128 * iq + 8 * ir; |
| 105 | |
| 106 | for (int ib = ix; ib < nb; ib += BLOCK_STRIDE) { |
| 107 | float4 sumy = {0.f, 0.f, 0.f, 0.f}; |
| 108 | for (int i = 0; i < 8; ++i) { |
| 109 | yl[i+ 0] = y4[i+ 0]; sumy.s0 += yl[i+ 0]; |
| 110 | yl[i+ 8] = y4[i+32]; sumy.s1 += yl[i+ 8]; |
| 111 | yl[i+16] = y4[i+64]; sumy.s2 += yl[i+16]; |
| 112 | yl[i+24] = y4[i+96]; sumy.s3 += yl[i+24]; |
| 113 | } |
| 114 | |
| 115 | global uchar * sc = (global uchar *)x[ib].scales + 8*iq + is; |
| 116 | global ushort * qs = (global ushort *)x[ib].qs + 16 * iq + 4 * ir; |
| 117 | global half * dh = &x[ib].d; |
| 118 | |
| 119 | // only ne01 output rows exist; reading past them can pull in an inf scale |
| 120 | for (int row = 0; row < N_DST && first_row + row < ne01; row++) { |
| 121 | float4 acc1 = {0.f, 0.f, 0.f, 0.f}; |
| 122 | float4 acc2 = {0.f, 0.f, 0.f, 0.f}; |
| 123 | for (int i = 0; i < 8; i += 2) { |
| 124 | acc1.s0 += yl[i+ 0] * (qs[i/2] & 0x0003); |
| 125 | acc2.s0 += yl[i+ 1] * (qs[i/2] & 0x0300); |
| 126 | acc1.s1 += yl[i+ 8] * (qs[i/2] & 0x000c); |
| 127 | acc2.s1 += yl[i+ 9] * (qs[i/2] & 0x0c00); |
| 128 | acc1.s2 += yl[i+16] * (qs[i/2] & 0x0030); |
| 129 | acc2.s2 += yl[i+17] * (qs[i/2] & 0x3000); |
| 130 | acc1.s3 += yl[i+24] * (qs[i/2] & 0x00c0); |
| 131 | acc2.s3 += yl[i+25] * (qs[i/2] & 0xc000); |
| 132 | } |
| 133 | |
| 134 | float dall = dh[0]; |
| 135 | float dmin = dh[1] * 1.f/16.f; |
| 136 | sumf[row] += dall * ((acc1.s0 + 1.f/256.f * acc2.s0) * (sc[0] & 0xF) * 1.f/ 1.f + |
| 137 | (acc1.s1 + 1.f/256.f * acc2.s1) * (sc[2] & 0xF) * 1.f/ 4.f + |
| 138 | (acc1.s2 + 1.f/256.f * acc2.s2) * (sc[4] & 0xF) * 1.f/16.f + |
| 139 | (acc1.s3 + 1.f/256.f * acc2.s3) * (sc[6] & 0xF) * 1.f/64.f) - |
| 140 | dmin * (sumy.s0 * (sc[0] & 0xF0) + sumy.s1 * (sc[2] & 0xF0) + |
| 141 | sumy.s2 * (sc[4] & 0xF0) + sumy.s3 * (sc[6] & 0xF0)); |
| 142 | |
| 143 | qs += nb01/2; |
| 144 | sc += nb01; |
| 145 | dh += nb01/2; |
| 146 | } |
| 147 | |
| 148 | y4 += BLOCK_STRIDE * QK_K; |
| 149 | } |
| 150 | |
| 151 | global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0; |
| 152 | |
| 153 | for (int row = 0; row < N_DST; ++row) { |
| 154 | all_sum = sub_group_reduce_add(sumf[row]); |
| 155 | if (first_row + row < ne01) { |
| 156 | if (get_sub_group_local_id() == 0) { |
| 157 | dst_f32[first_row + row] = all_sum; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |