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_0_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 | #define QK2_0 64 |
| 16 | |
| 17 | // 00=-1, 01=0, 10=+1, 11=+2 |
| 18 | typedef struct { |
| 19 | half d; |
| 20 | uchar qs[QK2_0/4]; |
| 21 | } block_q2_0; |
| 22 | |
| 23 | #undef N_DST |
| 24 | #undef N_SIMDGROUP |
| 25 | #undef N_SIMDWIDTH |
| 26 | |
| 27 | #ifdef INTEL_GPU |
| 28 | #define N_DST 4 // number of rows each SIMD group works on |
| 29 | #define N_SIMDGROUP 1 // number of SIMD groups in a thread group |
| 30 | #define N_SIMDWIDTH 16 // SIMD group size |
| 31 | #elif defined (ADRENO_GPU) |
| 32 | #define N_DST 4 |
| 33 | #define N_SIMDGROUP 1 |
| 34 | #define N_SIMDWIDTH 64 |
| 35 | #endif |
| 36 | |
| 37 | #undef BLOCK_STRIDE |
| 38 | // 4 threads cover one block |
| 39 | #define BLOCK_STRIDE (N_SIMDWIDTH/4) |
| 40 | |
| 41 | #ifdef INTEL_GPU |
| 42 | REQD_SUBGROUP_SIZE_16 |
| 43 | #elif defined (ADRENO_GPU) |
| 44 | REQD_SUBGROUP_SIZE_64 |
| 45 | #endif |
| 46 | kernel void kernel_mul_mv_q2_0_f32( |
| 47 | global char * src0, |
| 48 | ulong offset0, |
| 49 | global char * src1, |
| 50 | ulong offset1, |
| 51 | global char * dst, |
| 52 | ulong offsetd, |
| 53 | int ne00, |
| 54 | int ne01, |
| 55 | ulong nb01, |
| 56 | ulong nb02, |
| 57 | ulong nb03, |
| 58 | int ne12, |
| 59 | ulong nb11, |
| 60 | ulong nb12, |
| 61 | ulong nb13, |
| 62 | int ne0, |
| 63 | int ne1, |
| 64 | int r2, |
| 65 | int r3 |
| 66 | ) { |
| 67 | src0 = src0 + offset0; |
| 68 | src1 = src1 + offset1; |
| 69 | dst = dst + offsetd; |
| 70 | |
| 71 | int ix = get_sub_group_local_id()/4; // block index |
| 72 | int it = get_sub_group_local_id()%4; // part of the block |
| 73 | |
| 74 | int nb = ne00/QK2_0; |
| 75 | |
| 76 | int r0 = get_group_id(0); |
| 77 | int r1 = get_group_id(1); |
| 78 | int im = get_group_id(2); |
| 79 | int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST; |
| 80 | |
| 81 | int i12 = im%ne12; |
| 82 | int i13 = im/ne12; |
| 83 | |
| 84 | ulong offset_src0 = first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03; |
| 85 | ulong offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13; |
| 86 | |
| 87 | global block_q2_0 * x = (global block_q2_0 *) (src0 + offset_src0); |
| 88 | global float * y = (global float *) (src1 + offset_src1); |
| 89 | |
| 90 | float yl[16]; |
| 91 | float sumf[N_DST] = {0.f}; |
| 92 | float all_sum; |
| 93 | |
| 94 | global float * y4 = y + ix * QK2_0 + 16 * it; |
| 95 | |
| 96 | for (int ib = ix; ib < nb; ib += BLOCK_STRIDE) { |
| 97 | for (int i = 0; i < 16; ++i) { |
| 98 | yl[i] = y4[i]; |
| 99 | } |
| 100 | |
| 101 | global char * xrow = (global char *)(x + ib); |
| 102 | |
| 103 | // only ne01 output rows exist; reading past them can pull in an inf scale |
| 104 | for (int row = 0; row < N_DST && first_row + row < ne01; row++) { |
| 105 | global block_q2_0 * xb = (global block_q2_0 *)(xrow + row*nb01); |
| 106 | |
| 107 | global uchar * q = xb->qs + 4*it; |
| 108 | |
| 109 | float acc = 0.f; |
| 110 | for (int i = 0; i < 4; ++i) { |
| 111 | uchar b = q[i]; |
| 112 | acc += yl[4*i+0] * (float)(((b >> 0) & 3) - 1); |
| 113 | acc += yl[4*i+1] * (float)(((b >> 2) & 3) - 1); |
| 114 | acc += yl[4*i+2] * (float)(((b >> 4) & 3) - 1); |
| 115 | acc += yl[4*i+3] * (float)(((b >> 6) & 3) - 1); |
| 116 | } |
| 117 | |
| 118 | sumf[row] += (float)xb->d * acc; |
| 119 | } |
| 120 | |
| 121 | y4 += BLOCK_STRIDE * QK2_0; |
| 122 | } |
| 123 | |
| 124 | global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0; |
| 125 | |
| 126 | for (int row = 0; row < N_DST; ++row) { |
| 127 | all_sum = sub_group_reduce_add(sumf[row]); |
| 128 | if (first_row + row < ne01) { |
| 129 | if (get_sub_group_local_id() == 0) { |
| 130 | dst_f32[first_row + row] = all_sum; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |