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_q1_0_f32_flat.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | |
| 3 | #ifdef cl_intel_subgroups |
| 4 | #pragma OPENCL EXTENSION cl_intel_subgroups : enable |
| 5 | #else |
| 6 | #pragma OPENCL EXTENSION cl_khr_subgroups : enable |
| 7 | #endif |
| 8 | |
| 9 | #ifdef cl_intel_required_subgroup_size |
| 10 | #pragma OPENCL EXTENSION cl_intel_required_subgroup_size : enable |
| 11 | #define INTEL_GPU 1 |
| 12 | #define REQD_SUBGROUP_SIZE_16 __attribute__((intel_reqd_sub_group_size(16))) |
| 13 | #define REQD_SUBGROUP_SIZE_32 __attribute__((intel_reqd_sub_group_size(32))) |
| 14 | #elif defined(cl_qcom_reqd_sub_group_size) |
| 15 | #pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable |
| 16 | #define ADRENO_GPU 1 |
| 17 | #define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half"))) |
| 18 | #define REQD_SUBGROUP_SIZE_128 __attribute__((qcom_reqd_sub_group_size("full"))) |
| 19 | #endif |
| 20 | |
| 21 | #define QK1_0 128 |
| 22 | #define QK1_0_BYTES (QK1_0/8) // 16 quant bytes per block |
| 23 | #define QK1_0_BLK_BYTES (QK1_0_BYTES + 2) // d + qs in original tensor = 18 |
| 24 | |
| 25 | #define NB_Q1_0 16 // quants handled per thread (two qs bytes) |
| 26 | |
| 27 | #ifdef INTEL_GPU |
| 28 | #define N_R0_Q1_0 4 // number of rows each subgroup works on |
| 29 | #define N_SG_Q1_0 2 // number of subgroups in a work group |
| 30 | #define N_SIMDWIDTH 16 // subgroup size |
| 31 | #elif defined (ADRENO_GPU) |
| 32 | #define N_R0_Q1_0 4 |
| 33 | #define N_SG_Q1_0 2 |
| 34 | #define N_SIMDWIDTH 64 |
| 35 | #endif |
| 36 | |
| 37 | #ifdef INTEL_GPU |
| 38 | REQD_SUBGROUP_SIZE_16 |
| 39 | #elif defined (ADRENO_GPU) |
| 40 | REQD_SUBGROUP_SIZE_64 |
| 41 | #endif |
| 42 | kernel void kernel_mul_mv_q1_0_f32_flat( |
| 43 | global char * src0_q, |
| 44 | global half * src0_d, |
| 45 | global char * src1, |
| 46 | ulong offset1, |
| 47 | global char * dst, |
| 48 | ulong offsetd, |
| 49 | int ne00, |
| 50 | int ne01, |
| 51 | ulong nb01, |
| 52 | ulong nb02, |
| 53 | ulong nb03, |
| 54 | int ne12, |
| 55 | ulong nb11, |
| 56 | ulong nb12, |
| 57 | ulong nb13, |
| 58 | int ne0, |
| 59 | int ne1, |
| 60 | int r2, |
| 61 | int r3 |
| 62 | ) { |
| 63 | src1 = (global char*)((global char*)src1 + offset1); |
| 64 | dst = (global char*)((global char*)dst + offsetd); |
| 65 | |
| 66 | int nb = ne00/QK1_0; |
| 67 | |
| 68 | int r0 = get_group_id(0); |
| 69 | int r1 = get_group_id(1); |
| 70 | int im = get_group_id(2); |
| 71 | |
| 72 | int first_row = (r0*N_SG_Q1_0 + get_sub_group_id()) * N_R0_Q1_0; |
| 73 | |
| 74 | uint i12 = im%ne12; |
| 75 | uint i13 = im/ne12; |
| 76 | |
| 77 | ulong offset_src1 = r1*nb11 + i12*nb12 + i13*nb13; |
| 78 | global float * y = (global float *) (src1 + offset_src1); |
| 79 | |
| 80 | // pointers to src0 rows (flat: q bytes + scales) |
| 81 | uint offset_src0_base = first_row*nb01 + (i12/r2)*nb02 + (i13/r3)*nb03; |
| 82 | |
| 83 | global uchar * ax0, * ax1, * ax2, * ax3; |
| 84 | global half * ad0, * ad1, * ad2, * ad3; |
| 85 | uint offset_src0; |
| 86 | |
| 87 | offset_src0 = (offset_src0_base + 0*nb01) / QK1_0_BLK_BYTES; |
| 88 | ax0 = (global uchar *) ((global char *) src0_q + offset_src0*QK1_0_BYTES); |
| 89 | ad0 = (global half *) ((global char *) src0_d + offset_src0*sizeof(half)); |
| 90 | |
| 91 | offset_src0 = (offset_src0_base + 1*nb01) / QK1_0_BLK_BYTES; |
| 92 | ax1 = (global uchar *) ((global char *) src0_q + offset_src0*QK1_0_BYTES); |
| 93 | ad1 = (global half *) ((global char *) src0_d + offset_src0*sizeof(half)); |
| 94 | |
| 95 | offset_src0 = (offset_src0_base + 2*nb01) / QK1_0_BLK_BYTES; |
| 96 | ax2 = (global uchar *) ((global char *) src0_q + offset_src0*QK1_0_BYTES); |
| 97 | ad2 = (global half *) ((global char *) src0_d + offset_src0*sizeof(half)); |
| 98 | |
| 99 | offset_src0 = (offset_src0_base + 3*nb01) / QK1_0_BLK_BYTES; |
| 100 | ax3 = (global uchar *) ((global char *) src0_q + offset_src0*QK1_0_BYTES); |
| 101 | ad3 = (global half *) ((global char *) src0_d + offset_src0*sizeof(half)); |
| 102 | |
| 103 | const short ix = get_sub_group_local_id()/8; |
| 104 | const short il = get_sub_group_local_id()%8; |
| 105 | |
| 106 | global float * yb = y + ix*QK1_0 + il*NB_Q1_0; |
| 107 | |
| 108 | float8 yl_lo; |
| 109 | float8 yl_hi; |
| 110 | float4 sumf = 0.f; |
| 111 | |
| 112 | // each thread handles NB_Q1_0 = 16 quants (two qs bytes) at a time |
| 113 | for (int ib = ix; ib < nb; ib += N_SIMDWIDTH/8) { |
| 114 | yl_lo = vload8(0, yb); |
| 115 | yl_hi = vload8(0, yb + 8); |
| 116 | float sumy = yl_lo.s0 + yl_lo.s1 + yl_lo.s2 + yl_lo.s3 |
| 117 | + yl_lo.s4 + yl_lo.s5 + yl_lo.s6 + yl_lo.s7 |
| 118 | + yl_hi.s0 + yl_hi.s1 + yl_hi.s2 + yl_hi.s3 |
| 119 | + yl_hi.s4 + yl_hi.s5 + yl_hi.s6 + yl_hi.s7; |
| 120 | |
| 121 | uint b0, b1; |
| 122 | float acc; |
| 123 | |
| 124 | b0 = ax0[ib*QK1_0_BYTES + il*2 + 0]; |
| 125 | b1 = ax0[ib*QK1_0_BYTES + il*2 + 1]; |
| 126 | acc = yl_lo.s0*(float)((b0 >> 0) & 1) + yl_lo.s1*(float)((b0 >> 1) & 1) |
| 127 | + yl_lo.s2*(float)((b0 >> 2) & 1) + yl_lo.s3*(float)((b0 >> 3) & 1) |
| 128 | + yl_lo.s4*(float)((b0 >> 4) & 1) + yl_lo.s5*(float)((b0 >> 5) & 1) |
| 129 | + yl_lo.s6*(float)((b0 >> 6) & 1) + yl_lo.s7*(float)((b0 >> 7) & 1) |
| 130 | + yl_hi.s0*(float)((b1 >> 0) & 1) + yl_hi.s1*(float)((b1 >> 1) & 1) |
| 131 | + yl_hi.s2*(float)((b1 >> 2) & 1) + yl_hi.s3*(float)((b1 >> 3) & 1) |
| 132 | + yl_hi.s4*(float)((b1 >> 4) & 1) + yl_hi.s5*(float)((b1 >> 5) & 1) |
| 133 | + yl_hi.s6*(float)((b1 >> 6) & 1) + yl_hi.s7*(float)((b1 >> 7) & 1); |
| 134 | sumf.s0 += (float)ad0[ib] * (2.0f*acc - sumy); |
| 135 | |
| 136 | b0 = ax1[ib*QK1_0_BYTES + il*2 + 0]; |
| 137 | b1 = ax1[ib*QK1_0_BYTES + il*2 + 1]; |
| 138 | acc = yl_lo.s0*(float)((b0 >> 0) & 1) + yl_lo.s1*(float)((b0 >> 1) & 1) |
| 139 | + yl_lo.s2*(float)((b0 >> 2) & 1) + yl_lo.s3*(float)((b0 >> 3) & 1) |
| 140 | + yl_lo.s4*(float)((b0 >> 4) & 1) + yl_lo.s5*(float)((b0 >> 5) & 1) |
| 141 | + yl_lo.s6*(float)((b0 >> 6) & 1) + yl_lo.s7*(float)((b0 >> 7) & 1) |
| 142 | + yl_hi.s0*(float)((b1 >> 0) & 1) + yl_hi.s1*(float)((b1 >> 1) & 1) |
| 143 | + yl_hi.s2*(float)((b1 >> 2) & 1) + yl_hi.s3*(float)((b1 >> 3) & 1) |
| 144 | + yl_hi.s4*(float)((b1 >> 4) & 1) + yl_hi.s5*(float)((b1 >> 5) & 1) |
| 145 | + yl_hi.s6*(float)((b1 >> 6) & 1) + yl_hi.s7*(float)((b1 >> 7) & 1); |
| 146 | sumf.s1 += (float)ad1[ib] * (2.0f*acc - sumy); |
| 147 | |
| 148 | b0 = ax2[ib*QK1_0_BYTES + il*2 + 0]; |
| 149 | b1 = ax2[ib*QK1_0_BYTES + il*2 + 1]; |
| 150 | acc = yl_lo.s0*(float)((b0 >> 0) & 1) + yl_lo.s1*(float)((b0 >> 1) & 1) |
| 151 | + yl_lo.s2*(float)((b0 >> 2) & 1) + yl_lo.s3*(float)((b0 >> 3) & 1) |
| 152 | + yl_lo.s4*(float)((b0 >> 4) & 1) + yl_lo.s5*(float)((b0 >> 5) & 1) |
| 153 | + yl_lo.s6*(float)((b0 >> 6) & 1) + yl_lo.s7*(float)((b0 >> 7) & 1) |
| 154 | + yl_hi.s0*(float)((b1 >> 0) & 1) + yl_hi.s1*(float)((b1 >> 1) & 1) |
| 155 | + yl_hi.s2*(float)((b1 >> 2) & 1) + yl_hi.s3*(float)((b1 >> 3) & 1) |
| 156 | + yl_hi.s4*(float)((b1 >> 4) & 1) + yl_hi.s5*(float)((b1 >> 5) & 1) |
| 157 | + yl_hi.s6*(float)((b1 >> 6) & 1) + yl_hi.s7*(float)((b1 >> 7) & 1); |
| 158 | sumf.s2 += (float)ad2[ib] * (2.0f*acc - sumy); |
| 159 | |
| 160 | b0 = ax3[ib*QK1_0_BYTES + il*2 + 0]; |
| 161 | b1 = ax3[ib*QK1_0_BYTES + il*2 + 1]; |
| 162 | acc = yl_lo.s0*(float)((b0 >> 0) & 1) + yl_lo.s1*(float)((b0 >> 1) & 1) |
| 163 | + yl_lo.s2*(float)((b0 >> 2) & 1) + yl_lo.s3*(float)((b0 >> 3) & 1) |
| 164 | + yl_lo.s4*(float)((b0 >> 4) & 1) + yl_lo.s5*(float)((b0 >> 5) & 1) |
| 165 | + yl_lo.s6*(float)((b0 >> 6) & 1) + yl_lo.s7*(float)((b0 >> 7) & 1) |
| 166 | + yl_hi.s0*(float)((b1 >> 0) & 1) + yl_hi.s1*(float)((b1 >> 1) & 1) |
| 167 | + yl_hi.s2*(float)((b1 >> 2) & 1) + yl_hi.s3*(float)((b1 >> 3) & 1) |
| 168 | + yl_hi.s4*(float)((b1 >> 4) & 1) + yl_hi.s5*(float)((b1 >> 5) & 1) |
| 169 | + yl_hi.s6*(float)((b1 >> 6) & 1) + yl_hi.s7*(float)((b1 >> 7) & 1); |
| 170 | sumf.s3 += (float)ad3[ib] * (2.0f*acc - sumy); |
| 171 | |
| 172 | yb += N_SIMDWIDTH*NB_Q1_0; |
| 173 | } |
| 174 | |
| 175 | global float * dst_f32 = (global float *) dst + (ulong)im*ne0*ne1 + (ulong)r1*ne0; |
| 176 | |
| 177 | float4 tot = (float4)( |
| 178 | sub_group_reduce_add(sumf.s0), |
| 179 | sub_group_reduce_add(sumf.s1), |
| 180 | sub_group_reduce_add(sumf.s2), |
| 181 | sub_group_reduce_add(sumf.s3) |
| 182 | ); |
| 183 | |
| 184 | if (get_sub_group_local_id() == 0) { |
| 185 | if (first_row + 0 < ne01) dst_f32[first_row + 0] = tot.s0; |
| 186 | if (first_row + 1 < ne01) dst_f32[first_row + 1] = tot.s1; |
| 187 | if (first_row + 2 < ne01) dst_f32[first_row + 2] = tot.s2; |
| 188 | if (first_row + 3 < ne01) dst_f32[first_row + 3] = tot.s3; |
| 189 | } |
| 190 | } |