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_iq2_xs_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_iq2_xs_f32.cl` 143 lines ยท 4259 bytes Raw Blame History 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 qs[QK_K/8]; 20 uchar scales[QK_K/32]; 21 } block_iq2_xs; 22 23 #undef N_DST 24 #undef N_SIMDGROUP 25 #undef N_SIMDWIDTH 26 27 #ifdef INTEL_GPU 28 #define N_DST 4 29 #define N_SIMDGROUP 1 30 #define N_SIMDWIDTH 16 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 // 8 threads cover one super block, one 32 element sub block each 39 #define BLOCK_STRIDE (N_SIMDWIDTH/8) 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_iq2_xs_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()/8; // super block index 72 int it = get_sub_group_local_id()%8; // sub block inside the super block 73 74 int nb = ne00/QK_K; 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_iq2_xs * x = (global block_iq2_xs *) (src0 + offset_src0); 88 global float * y = (global float *) (src1 + offset_src1); 89 90 float yl[32]; 91 float sumf[N_DST] = {0.f}; 92 float all_sum; 93 94 global float * y4 = y + ix * QK_K + 32 * it; 95 96 for (int ib = ix; ib < nb; ib += BLOCK_STRIDE) { 97 for (int i = 0; i < 32; ++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_iq2_xs * xb = (global block_iq2_xs *)(xrow + row*nb01); 106 107 uchar scb = xb->scales[it]; 108 float dlo = (float)xb->d * (0.5f + (float)(scb & 0xf)) * 0.25f; 109 float dhi = (float)xb->d * (0.5f + (float)(scb >> 4)) * 0.25f; 110 111 global ushort * q16 = xb->qs + 4*it; 112 113 float alo = 0.f, ahi = 0.f; 114 for (int l = 0; l < 4; ++l) { 115 ushort q = q16[l]; 116 uchar sg = iq2_xs_signs(q >> 9); 117 uint lo = iq2_xs_grid[2*(q & 511)+0]; 118 uint hi = iq2_xs_grid[2*(q & 511)+1]; 119 float a = 0.f; 120 for (int j = 0; j < 4; ++j) { 121 a += yl[8*l+j+0] * (float)((lo >> (8*j)) & 0xFF) * ((sg & (1 << j)) ? -1.f : 1.f); 122 a += yl[8*l+j+4] * (float)((hi >> (8*j)) & 0xFF) * ((sg & (1 << (j+4))) ? -1.f : 1.f); 123 } 124 if (l < 2) alo += a; else ahi += a; 125 } 126 127 sumf[row] += dlo * alo + dhi * ahi; 128 } 129 130 y4 += BLOCK_STRIDE * QK_K; 131 } 132 133 global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0; 134 135 for (int row = 0; row < N_DST; ++row) { 136 all_sum = sub_group_reduce_add(sumf[row]); 137 if (first_row + row < ne01) { 138 if (get_sub_group_local_id() == 0) { 139 dst_f32[first_row + row] = all_sum; 140 } 141 } 142 } 143 }