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_iq1_s_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_iq1_s_f32.cl` 140 lines ยท 4038 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 #define IQ1_S_DELTA 0.125f 17 18 typedef struct { 19 half d; 20 uchar qs[QK_K/8]; 21 ushort qh[QK_K/32]; 22 } block_iq1_s; 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_iq1_s_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_iq1_s * x = (global block_iq1_s *) (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_iq1_s * xb = (global block_iq1_s *)(xrow + row*nb01); 107 108 ushort qhb = xb->qh[it]; 109 float dl = (float)xb->d * (float)(2*((qhb >> 12) & 7) + 1); 110 float dlt = (qhb & 0x8000) ? (-1.f - IQ1_S_DELTA) : (-1.f + IQ1_S_DELTA); 111 112 global uchar * qsb = xb->qs + 4*it; 113 114 float acc = 0.f; 115 for (int l = 0; l < 4; ++l) { 116 uint gi = (uint)qsb[l] | ((((uint)qhb >> (3*l)) & 7) << 8); 117 uint g = iq1_s_grid[gi]; 118 for (int j = 0; j < 4; ++j) { 119 acc += yl[8*l+j+0] * ((float)((g >> (8*j+0)) & 0xF) + dlt); 120 acc += yl[8*l+j+4] * ((float)((g >> (8*j+4)) & 0xF) + dlt); 121 } 122 } 123 124 sumf[row] += dl * acc; 125 } 126 127 y4 += BLOCK_STRIDE * QK_K; 128 } 129 130 global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0; 131 132 for (int row = 0; row < N_DST; ++row) { 133 all_sum = sub_group_reduce_add(sumf[row]); 134 if (first_row + row < ne01) { 135 if (get_sub_group_local_id() == 0) { 136 dst_f32[first_row + row] = all_sum; 137 } 138 } 139 } 140 }