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