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/gemm_xmem_f16_f32_os8.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 / gemm_xmem_f16_f32_os8.cl` 238 lines ยท 9021 bytes Raw Blame History 1 #pragma OPENCL EXTENSION cl_khr_fp16 : enable 2 #pragma OPENCL EXTENSION cl_qcom_subgroup_uniform_load : enable 3 #pragma OPENCL EXTENSION cl_qcom_subgroup_constant_load : enable 4 5 __constant sampler_t smp_zero = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST; 6 7 __kernel void adreno_xmem_pack_src_f32( 8 __global const void * src_void, 9 ulong offset, 10 __write_only image2d_t src_img, 11 int K, 12 int N) { 13 const int x = get_global_id(0); 14 const int y = get_global_id(1); 15 const int kpack = K / 4; 16 17 if (x >= N || y >= kpack) { 18 return; 19 } 20 21 __global const float * src = (__global const float *)((__global const char *)src_void + offset); 22 const int base = x*K + y*4; 23 const half4 v = (half4)((half)src[base + 0], (half)src[base + 1], (half)src[base + 2], (half)src[base + 3]); 24 write_imageh(src_img, (int2)(x, y), v); 25 } 26 27 __kernel void adreno_xmem_prepack_weight_f16( 28 __global half4 * dst, 29 __global const void * src_void, 30 ulong offset, 31 int K, 32 int M, 33 int kpack, 34 int npack, 35 int os) { 36 const int linear = get_global_id(0); 37 const int total = kpack*npack; 38 if (linear >= total) { 39 return; 40 } 41 42 __global const half * src = (__global const half *)((__global const char *)src_void + offset); 43 44 const int dst_ogroup = linear % os; 45 const int dst_o_sp_i = linear / os; 46 const int dst_i = dst_o_sp_i % kpack; 47 const int dst_o = dst_o_sp_i / kpack; 48 const int o_slice = dst_o*os + dst_ogroup; 49 const int k_base = dst_i*4; 50 51 half4 w0 = (half4)(0.0h); 52 half4 w1 = (half4)(0.0h); 53 half4 w2 = (half4)(0.0h); 54 half4 w3 = (half4)(0.0h); 55 56 const int o0 = o_slice*4 + 0; 57 const int o1 = o_slice*4 + 1; 58 const int o2 = o_slice*4 + 2; 59 const int o3 = o_slice*4 + 3; 60 61 if (k_base + 0 < K) { 62 if (o0 < M) w0.s0 = src[o0*K + k_base + 0]; 63 if (o1 < M) w0.s1 = src[o1*K + k_base + 0]; 64 if (o2 < M) w0.s2 = src[o2*K + k_base + 0]; 65 if (o3 < M) w0.s3 = src[o3*K + k_base + 0]; 66 } 67 if (k_base + 1 < K) { 68 if (o0 < M) w1.s0 = src[o0*K + k_base + 1]; 69 if (o1 < M) w1.s1 = src[o1*K + k_base + 1]; 70 if (o2 < M) w1.s2 = src[o2*K + k_base + 1]; 71 if (o3 < M) w1.s3 = src[o3*K + k_base + 1]; 72 } 73 if (k_base + 2 < K) { 74 if (o0 < M) w2.s0 = src[o0*K + k_base + 2]; 75 if (o1 < M) w2.s1 = src[o1*K + k_base + 2]; 76 if (o2 < M) w2.s2 = src[o2*K + k_base + 2]; 77 if (o3 < M) w2.s3 = src[o3*K + k_base + 2]; 78 } 79 if (k_base + 3 < K) { 80 if (o0 < M) w3.s0 = src[o0*K + k_base + 3]; 81 if (o1 < M) w3.s1 = src[o1*K + k_base + 3]; 82 if (o2 < M) w3.s2 = src[o2*K + k_base + 3]; 83 if (o3 < M) w3.s3 = src[o3*K + k_base + 3]; 84 } 85 86 dst[linear*4 + 0] = w0; 87 dst[linear*4 + 1] = w1; 88 dst[linear*4 + 2] = w2; 89 dst[linear*4 + 3] = w3; 90 } 91 92 __attribute__((qcom_max_concurrent_subgroups(12))) 93 __kernel void kernel_gemm_xmem_f16_f32_os8( 94 __constant half8 * weights_buffer __attribute__((sub_group_uniform)), 95 __constant half8 * xmem_buffer __attribute__((max_constant_size((6144)))), 96 __read_only image2d_t src_img, 97 __write_only image2d_t dst_img, 98 int N, 99 int npack, 100 int kpack) { 101 const int X = get_group_id(1)*get_local_size(0) + get_local_id(0); 102 const int Z = get_group_id(0)*get_local_size(2) + get_local_id(2); 103 104 if (Z*8 >= npack) { 105 return; 106 } 107 108 half4 r0 = (half4)(0.0h); 109 half4 r1 = (half4)(0.0h); 110 half4 r2 = (half4)(0.0h); 111 half4 r3 = (half4)(0.0h); 112 half4 r4 = (half4)(0.0h); 113 half4 r5 = (half4)(0.0h); 114 half4 r6 = (half4)(0.0h); 115 half4 r7 = (half4)(0.0h); 116 117 int f_offset = Z*kpack*32; 118 int subgroup_id = (int)(0x1F & qcom_get_physical_sub_group_id()); 119 subgroup_id = subgroup_id % 12; 120 const int c_offset = subgroup_id*32; 121 __constant half16 * weights_cache = (__constant half16 *)&xmem_buffer[c_offset]; 122 123 int coord_s = 0; 124 do { 125 const half4 src0 = read_imageh(src_img, smp_zero, (int2)(X, coord_s)); 126 coord_s++; 127 const half4 src1 = read_imageh(src_img, smp_zero, (int2)(X, coord_s)); 128 coord_s++; 129 130 qcom_sub_group_constant_load8(xmem_buffer, weights_buffer, c_offset, f_offset >> 1, 32); 131 f_offset += 64; 132 qcom_sub_group_sync(QCOM_CLK_CONST_LOAD_SYNC); 133 134 r0 += src0.x * weights_cache[0].s0123; 135 r0 += src0.y * weights_cache[0].s4567; 136 r0 += src0.z * weights_cache[0].s89ab; 137 r0 += src0.w * weights_cache[0].scdef; 138 r1 += src0.x * weights_cache[1].s0123; 139 r1 += src0.y * weights_cache[1].s4567; 140 r1 += src0.z * weights_cache[1].s89ab; 141 r1 += src0.w * weights_cache[1].scdef; 142 r2 += src0.x * weights_cache[2].s0123; 143 r2 += src0.y * weights_cache[2].s4567; 144 r2 += src0.z * weights_cache[2].s89ab; 145 r2 += src0.w * weights_cache[2].scdef; 146 r3 += src0.x * weights_cache[3].s0123; 147 r3 += src0.y * weights_cache[3].s4567; 148 r3 += src0.z * weights_cache[3].s89ab; 149 r3 += src0.w * weights_cache[3].scdef; 150 r4 += src0.x * weights_cache[4].s0123; 151 r4 += src0.y * weights_cache[4].s4567; 152 r4 += src0.z * weights_cache[4].s89ab; 153 r4 += src0.w * weights_cache[4].scdef; 154 r5 += src0.x * weights_cache[5].s0123; 155 r5 += src0.y * weights_cache[5].s4567; 156 r5 += src0.z * weights_cache[5].s89ab; 157 r5 += src0.w * weights_cache[5].scdef; 158 r6 += src0.x * weights_cache[6].s0123; 159 r6 += src0.y * weights_cache[6].s4567; 160 r6 += src0.z * weights_cache[6].s89ab; 161 r6 += src0.w * weights_cache[6].scdef; 162 r7 += src0.x * weights_cache[7].s0123; 163 r7 += src0.y * weights_cache[7].s4567; 164 r7 += src0.z * weights_cache[7].s89ab; 165 r7 += src0.w * weights_cache[7].scdef; 166 167 r0 += src1.x * weights_cache[8].s0123; 168 r0 += src1.y * weights_cache[8].s4567; 169 r0 += src1.z * weights_cache[8].s89ab; 170 r0 += src1.w * weights_cache[8].scdef; 171 r1 += src1.x * weights_cache[9].s0123; 172 r1 += src1.y * weights_cache[9].s4567; 173 r1 += src1.z * weights_cache[9].s89ab; 174 r1 += src1.w * weights_cache[9].scdef; 175 r2 += src1.x * weights_cache[10].s0123; 176 r2 += src1.y * weights_cache[10].s4567; 177 r2 += src1.z * weights_cache[10].s89ab; 178 r2 += src1.w * weights_cache[10].scdef; 179 r3 += src1.x * weights_cache[11].s0123; 180 r3 += src1.y * weights_cache[11].s4567; 181 r3 += src1.z * weights_cache[11].s89ab; 182 r3 += src1.w * weights_cache[11].scdef; 183 r4 += src1.x * weights_cache[12].s0123; 184 r4 += src1.y * weights_cache[12].s4567; 185 r4 += src1.z * weights_cache[12].s89ab; 186 r4 += src1.w * weights_cache[12].scdef; 187 r5 += src1.x * weights_cache[13].s0123; 188 r5 += src1.y * weights_cache[13].s4567; 189 r5 += src1.z * weights_cache[13].s89ab; 190 r5 += src1.w * weights_cache[13].scdef; 191 r6 += src1.x * weights_cache[14].s0123; 192 r6 += src1.y * weights_cache[14].s4567; 193 r6 += src1.z * weights_cache[14].s89ab; 194 r6 += src1.w * weights_cache[14].scdef; 195 r7 += src1.x * weights_cache[15].s0123; 196 r7 += src1.y * weights_cache[15].s4567; 197 r7 += src1.z * weights_cache[15].s89ab; 198 r7 += src1.w * weights_cache[15].scdef; 199 } while (coord_s < kpack); 200 201 // Keep all lanes active until the subgroup loads and syncs are done. 202 if (X >= N) { 203 return; 204 } 205 206 int coord_s_out = Z*8; 207 if (coord_s_out < npack) { write_imageh(dst_img, (int2)(X, coord_s_out), r0); coord_s_out++; } 208 if (coord_s_out < npack) { write_imageh(dst_img, (int2)(X, coord_s_out), r1); coord_s_out++; } 209 if (coord_s_out < npack) { write_imageh(dst_img, (int2)(X, coord_s_out), r2); coord_s_out++; } 210 if (coord_s_out < npack) { write_imageh(dst_img, (int2)(X, coord_s_out), r3); coord_s_out++; } 211 if (coord_s_out < npack) { write_imageh(dst_img, (int2)(X, coord_s_out), r4); coord_s_out++; } 212 if (coord_s_out < npack) { write_imageh(dst_img, (int2)(X, coord_s_out), r5); coord_s_out++; } 213 if (coord_s_out < npack) { write_imageh(dst_img, (int2)(X, coord_s_out), r6); coord_s_out++; } 214 if (coord_s_out < npack) { write_imageh(dst_img, (int2)(X, coord_s_out), r7); } 215 } 216 217 __kernel void adreno_xmem_store_dst_f32( 218 __read_only image2d_t dst_img, 219 __global void * dst_void, 220 ulong offset, 221 int M, 222 int N) { 223 const int x = get_global_id(0); 224 const int y = get_global_id(1); 225 const int npack = (M + 3) / 4; 226 227 if (x >= N || y >= npack) { 228 return; 229 } 230 231 __global float * dst = (__global float *)((__global char *)dst_void + offset); 232 const half4 hv = read_imageh(dst_img, smp_zero, (int2)(x, y)); 233 const int m = y*4; 234 if (m + 0 < M) dst[x*M + m + 0] = (float)hv.s0; 235 if (m + 1 < M) dst[x*M + m + 1] = (float)hv.s1; 236 if (m + 2 < M) dst[x*M + m + 2] = (float)hv.s2; 237 if (m + 3 < M) dst[x*M + m + 3] = (float)hv.s3; 238 }