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_iq3_s_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 | #define IQ3_S_N_SCALE (QK_K/64) |
| 17 | |
| 18 | typedef struct { |
| 19 | half d; |
| 20 | uchar qs[QK_K/4]; |
| 21 | uchar qh[QK_K/32]; |
| 22 | uchar signs[QK_K/8]; |
| 23 | uchar scales[IQ3_S_N_SCALE]; |
| 24 | } block_iq3_s; |
| 25 | |
| 26 | #undef N_DST |
| 27 | #undef N_SIMDGROUP |
| 28 | #undef N_SIMDWIDTH |
| 29 | |
| 30 | #ifdef INTEL_GPU |
| 31 | #define N_DST 4 |
| 32 | #define N_SIMDGROUP 1 |
| 33 | #define N_SIMDWIDTH 16 |
| 34 | #elif defined (ADRENO_GPU) |
| 35 | #define N_DST 4 |
| 36 | #define N_SIMDGROUP 1 |
| 37 | #define N_SIMDWIDTH 64 |
| 38 | #endif |
| 39 | |
| 40 | #undef BLOCK_STRIDE |
| 41 | // 8 threads cover one super block, one 32 element sub block each |
| 42 | #define BLOCK_STRIDE (N_SIMDWIDTH/8) |
| 43 | |
| 44 | #ifdef INTEL_GPU |
| 45 | REQD_SUBGROUP_SIZE_16 |
| 46 | #elif defined (ADRENO_GPU) |
| 47 | REQD_SUBGROUP_SIZE_64 |
| 48 | #endif |
| 49 | kernel void kernel_mul_mv_iq3_s_f32( |
| 50 | global char * src0, |
| 51 | ulong offset0, |
| 52 | global char * src1, |
| 53 | ulong offset1, |
| 54 | global char * dst, |
| 55 | ulong offsetd, |
| 56 | int ne00, |
| 57 | int ne01, |
| 58 | ulong nb01, |
| 59 | ulong nb02, |
| 60 | ulong nb03, |
| 61 | int ne12, |
| 62 | ulong nb11, |
| 63 | ulong nb12, |
| 64 | ulong nb13, |
| 65 | int ne0, |
| 66 | int ne1, |
| 67 | int r2, |
| 68 | int r3 |
| 69 | ) { |
| 70 | src0 = src0 + offset0; |
| 71 | src1 = src1 + offset1; |
| 72 | dst = dst + offsetd; |
| 73 | |
| 74 | int ix = get_sub_group_local_id()/8; // super block index |
| 75 | int it = get_sub_group_local_id()%8; // sub block inside the super block |
| 76 | |
| 77 | int nb = ne00/QK_K; |
| 78 | |
| 79 | int r0 = get_group_id(0); |
| 80 | int r1 = get_group_id(1); |
| 81 | int im = get_group_id(2); |
| 82 | int first_row = (r0 * N_SIMDGROUP + get_sub_group_id()) * N_DST; |
| 83 | |
| 84 | int i12 = im%ne12; |
| 85 | int i13 = im/ne12; |
| 86 | |
| 87 | ulong offset_src0 = first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03; |
| 88 | ulong offset_src1 = r1*nb11 + (i12 )*nb12 + (i13 )*nb13; |
| 89 | |
| 90 | global block_iq3_s * x = (global block_iq3_s *) (src0 + offset_src0); |
| 91 | global float * y = (global float *) (src1 + offset_src1); |
| 92 | |
| 93 | float yl[32]; |
| 94 | float sumf[N_DST] = {0.f}; |
| 95 | float all_sum; |
| 96 | |
| 97 | global float * y4 = y + ix * QK_K + 32 * it; |
| 98 | |
| 99 | for (int ib = ix; ib < nb; ib += BLOCK_STRIDE) { |
| 100 | for (int i = 0; i < 32; ++i) { |
| 101 | yl[i] = y4[i]; |
| 102 | } |
| 103 | |
| 104 | global char * xrow = (global char *)(x + ib); |
| 105 | |
| 106 | // only ne01 output rows exist; reading past them can pull in an inf scale |
| 107 | for (int row = 0; row < N_DST && first_row + row < ne01; row++) { |
| 108 | global block_iq3_s * xb = (global block_iq3_s *)(xrow + row*nb01); |
| 109 | |
| 110 | uchar nib = (it & 1) ? (xb->scales[it >> 1] >> 4) : (xb->scales[it >> 1] & 0xf); |
| 111 | float db = (float)xb->d * (float)(1 + 2*nib); |
| 112 | |
| 113 | uchar qhb = xb->qh[it]; |
| 114 | global uchar * qsb = xb->qs + 8*it; |
| 115 | global uchar * sgb = xb->signs + 4*it; |
| 116 | |
| 117 | float acc = 0.f; |
| 118 | for (int l = 0; l < 4; ++l) { |
| 119 | uint i1 = (uint)qsb[2*l+0] | (((uint)qhb << (8-2*l)) & 256); |
| 120 | uint i2 = (uint)qsb[2*l+1] | (((uint)qhb << (7-2*l)) & 256); |
| 121 | uint g1 = iq3_s_grid[i1]; |
| 122 | uint g2 = iq3_s_grid[i2]; |
| 123 | uchar sg = sgb[l]; |
| 124 | for (int j = 0; j < 4; ++j) { |
| 125 | acc += yl[8*l+j+0] * (float)((g1 >> (8*j)) & 0xFF) * ((sg & (1 << j)) ? -1.f : 1.f); |
| 126 | acc += yl[8*l+j+4] * (float)((g2 >> (8*j)) & 0xFF) * ((sg & (1 << (j+4))) ? -1.f : 1.f); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | sumf[row] += db * acc; |
| 131 | } |
| 132 | |
| 133 | y4 += BLOCK_STRIDE * QK_K; |
| 134 | } |
| 135 | |
| 136 | global float * dst_f32 = (global float *) dst + im*ne0*ne1 + r1*ne0; |
| 137 | |
| 138 | for (int row = 0; row < N_DST; ++row) { |
| 139 | all_sum = sub_group_reduce_add(sumf[row]); |
| 140 | if (first_row + row < ne01) { |
| 141 | if (get_sub_group_local_id() == 0) { |
| 142 | dst_f32[first_row + row] = all_sum; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |