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-sycl/concat.cpp # 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-sycl / concat.cpp` 298 lines ยท 12190 bytes Raw Blame History 1 // 2 // MIT license 3 // Copyright (C) 2024 Intel Corporation 4 // SPDX-License-Identifier: MIT 5 // 6 7 // 8 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 9 // See https://llvm.org/LICENSE.txt for license information. 10 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 11 // 12 13 #include "ggml.h" 14 15 #include "concat.hpp" 16 17 static inline size_t elem_size(ggml_type t) { 18 return ggml_type_size(t) / ggml_blck_size(t); 19 } 20 21 template 22 static void concat_T_dim0(const T *x, const T *y, T *dst, 23 const int ne0, const int ne00, 24 const sycl::nd_item<3> &item_ct1) { 25 int nidx = item_ct1.get_local_id(2) + 26 item_ct1.get_group(2) * item_ct1.get_local_range(2); 27 if (nidx >= ne0) { 28 return; 29 } 30 // operation 31 int offset_dst = nidx + item_ct1.get_group(1) * ne0 + 32 item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1); 33 if (nidx < ne00) { // src0 34 int offset_src = nidx + item_ct1.get_group(1) * ne00 + 35 item_ct1.get_group(0) * ne00 * item_ct1.get_group_range(1); 36 dst[offset_dst] = x[offset_src]; 37 } else { 38 int offset_src = 39 nidx - ne00 + item_ct1.get_group(1) * (ne0 - ne00) + 40 item_ct1.get_group(0) * (ne0 - ne00) * item_ct1.get_group_range(1); 41 dst[offset_dst] = y[offset_src]; 42 } 43 } 44 45 template 46 static void concat_T_dim1(const T *x, const T *y, T *dst, 47 const int ne0, const int ne01, 48 const sycl::nd_item<3> &item_ct1) { 49 int nidx = item_ct1.get_local_id(2) + 50 item_ct1.get_group(2) * item_ct1.get_local_range(2); 51 if (nidx >= ne0) { 52 return; 53 } 54 // operation 55 int offset_dst = nidx + item_ct1.get_group(1) * ne0 + 56 item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1); 57 if (item_ct1.get_group(1) < (size_t) ne01) { // src0 58 int offset_src = 59 nidx + item_ct1.get_group(1) * ne0 + item_ct1.get_group(0) * ne0 * ne01; 60 dst[offset_dst] = x[offset_src]; 61 } else { 62 int offset_src = 63 nidx + (item_ct1.get_group(1) - ne01) * ne0 + 64 item_ct1.get_group(0) * ne0 * (item_ct1.get_group_range(1) - ne01); 65 dst[offset_dst] = y[offset_src]; 66 } 67 } 68 69 template 70 static void concat_T_dim2(const T *x, const T *y, T *dst, 71 const int ne0, const int ne02, 72 const sycl::nd_item<3> &item_ct1) { 73 int nidx = item_ct1.get_local_id(2) + 74 item_ct1.get_group(2) * item_ct1.get_local_range(2); 75 if (nidx >= ne0) { 76 return; 77 } 78 // operation 79 int offset_dst = nidx + item_ct1.get_group(1) * ne0 + 80 item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1); 81 if (item_ct1.get_group(0) < (size_t) ne02) { // src0 82 int offset_src = nidx + item_ct1.get_group(1) * ne0 + 83 item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1); 84 dst[offset_dst] = x[offset_src]; 85 } else { 86 int offset_src = 87 nidx + item_ct1.get_group(1) * ne0 + 88 (item_ct1.get_group(0) - ne02) * ne0 * item_ct1.get_group_range(1); 89 dst[offset_dst] = y[offset_src]; 90 } 91 } 92 93 template 94 static void concat_T_sycl(const T *x, const T *y, T *dst, 95 int ne00, int ne01, int ne02, int ne0, int ne1, 96 int ne2, int dim, queue_ptr stream) { 97 int num_blocks = (ne0 + SYCL_CONCAT_BLOCK_SIZE - 1) / SYCL_CONCAT_BLOCK_SIZE; 98 sycl::range<3> gridDim(ne2, ne1, num_blocks); 99 switch (dim) { 100 case 0: 101 stream->parallel_for(sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE), 102 sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE)), 103 [=](sycl::nd_item<3> item_ct1) { concat_T_dim0(x, y, dst, ne0, ne00, item_ct1); }); 104 break; 105 case 1: 106 stream->parallel_for(sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE), 107 sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE)), 108 [=](sycl::nd_item<3> item_ct1) { concat_T_dim1(x, y, dst, ne0, ne01, item_ct1); }); 109 break; 110 // dim >=2 will be dispatched to the default path 111 default: 112 stream->parallel_for(sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE), 113 sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE)), 114 [=](sycl::nd_item<3> item_ct1) { concat_T_dim2(x, y, dst, ne0, ne02, item_ct1); }); 115 break; 116 } 117 } 118 119 // non-contiguous kernel (slow) 120 template 121 static void concat_T_sycl_non_cont( 122 queue_ptr stream, const char *src0, const char *src1, char *dst, 123 int64_t ne00, int64_t ne01, int64_t ne02, int64_t ne03, uint64_t nb00, 124 uint64_t nb01, uint64_t nb02, uint64_t nb03, int64_t /*ne10*/, 125 int64_t /*ne11*/, int64_t /*ne12*/, int64_t /*ne13*/, uint64_t nb10, 126 uint64_t nb11, uint64_t nb12, uint64_t nb13, int64_t ne0, int64_t ne1, 127 int64_t ne2, int64_t ne3, uint64_t nb0, uint64_t nb1, uint64_t nb2, 128 uint64_t nb3, int32_t dim) { 129 sycl::range<3> gridDim(ne3, ne2, ne1); 130 131 // Avoid oversubscribing device when there is not enough elements along the innermost dim to 132 // fill a full SYCL_CONCAT_BLOCK_SIZE. For larger # of elements, the full SYCL_CONCAT_BLOCK_SIZE 133 // is used. 134 const int64_t ne0_pad = GGML_PAD(ne0, WARP_SIZE); 135 const int64_t block_ne0 = ne0_pad < SYCL_CONCAT_BLOCK_SIZE ? ne0_pad : (int64_t) SYCL_CONCAT_BLOCK_SIZE; 136 sycl::range<3> blockDim(1, 1, block_ne0); 137 138 stream->parallel_for(sycl::nd_range<3>(gridDim * blockDim, blockDim), [=](sycl::nd_item<3> item_ct1) { 139 int64_t i3 = item_ct1.get_group(0); 140 int64_t i2 = item_ct1.get_group(1); 141 int64_t i1 = item_ct1.get_group(2); 142 143 int64_t o[4] = { 0, 0, 0, 0 }; 144 o[dim] = dim == 0 ? ne00 : (dim == 1 ? ne01 : (dim == 2 ? ne02 : ne03)); 145 146 const T * x; 147 148 for (int i0 = item_ct1.get_local_id(2); i0 < ne0; i0 += item_ct1.get_local_range(2)) { 149 if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) { 150 x = (const T *) (src0 + (i3) *nb03 + (i2) *nb02 + (i1) *nb01 + (i0) *nb00); 151 } else { 152 x = (const T *) (src1 + (i3 - o[3]) * nb13 + (i2 - o[2]) * nb12 + (i1 - o[1]) * nb11 + 153 (i0 - o[0]) * nb10); 154 } 155 156 T *y = (T *)(dst + i3 * nb3 + i2 * nb2 + i1 * nb1 + i0 * nb0); 157 158 *y = *x; 159 } 160 }); 161 } 162 163 template 164 void concat_impl_sycl(ggml_backend_sycl_context & ctx, ggml_tensor *dst) { 165 scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/2); 166 const ggml_tensor * src0 = dst->src[0]; 167 const ggml_tensor * src1 = dst->src[1]; 168 queue_ptr stream = ctx.stream(); 169 170 const int32_t dim = ((int32_t *) dst->op_params)[0]; 171 172 if (ggml_is_contiguous(src0) && ggml_is_contiguous(src1)) { 173 const T * src0_d = (const T *) src0->data; 174 const T * src1_d = (const T *) src1->data; 175 T * dst_d = (T *) dst->data; 176 size_t type_size = elem_size(dst->type); 177 if (dim != 3) { 178 for (int i3 = 0; i3 < dst->ne[3]; i3++) { 179 concat_T_sycl(src0_d + i3 * (src0->nb[3] / type_size), src1_d + i3 * (src1->nb[3] / type_size), 180 dst_d + i3 * (dst->nb[3] / type_size), src0->ne[0], src0->ne[1], src0->ne[2], dst->ne[0], 181 dst->ne[1], dst->ne[2], dim, stream); 182 } 183 } else { 184 const size_t size0 = ggml_nbytes(src0); 185 const size_t size1 = ggml_nbytes(src1); 186 187 SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(dst_d, src0_d, size0))); 188 SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(dst_d + size0 / type_size, src1_d, size1))); 189 } 190 } else { 191 concat_T_sycl_non_cont(stream, (const char *) src0->data, (const char *) src1->data, (char *) dst->data, 192 src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3], src0->nb[0], src0->nb[1], 193 src0->nb[2], src0->nb[3], src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3], 194 src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3], dst->ne[0], dst->ne[1], dst->ne[2], 195 dst->ne[3], dst->nb[0], dst->nb[1], dst->nb[2], dst->nb[3], dim); 196 } 197 } 198 199 template 200 static void concat_impl_q_sycl(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { 201 scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/2); 202 const ggml_tensor * src0 = dst->src[0]; 203 const ggml_tensor * src1 = dst->src[1]; 204 queue_ptr stream = ctx.stream(); 205 206 const int32_t dim = ((int32_t *) dst->op_params)[0]; 207 208 GGML_ASSERT(src0->type == qtype); 209 GGML_ASSERT(src1->type == qtype); 210 GGML_ASSERT(dst->type == qtype); 211 GGML_ASSERT(src0->ne[0] % qk == 0); 212 GGML_ASSERT(src1->ne[0] % qk == 0); 213 GGML_ASSERT(dst->ne[0] % qk == 0); 214 215 const int ne00_blk = src0->ne[0] / qk; 216 const int ne0_blk = dst->ne[0] / qk; 217 218 if (ggml_is_contiguous(src0) && ggml_is_contiguous(src1)) { 219 const block_t * src0_d = (const block_t *) src0->data; 220 const block_t * src1_d = (const block_t *) src1->data; 221 block_t * dst_d = (block_t *) dst->data; 222 const size_t type_size = sizeof(block_t); 223 224 if (dim != 3) { 225 for (int i3 = 0; i3 < dst->ne[3]; i3++) { 226 concat_T_sycl( 227 src0_d + i3 * (src0->nb[3] / type_size), 228 src1_d + i3 * (src1->nb[3] / type_size), 229 dst_d + i3 * (dst->nb[3] / type_size), 230 ne00_blk, src0->ne[1], src0->ne[2], ne0_blk, 231 dst->ne[1], dst->ne[2], dim, stream); 232 } 233 } else { 234 const size_t size0 = ggml_nbytes(src0); 235 const size_t size1 = ggml_nbytes(src1); 236 237 SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(dst_d, src0_d, size0))); 238 SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy((char *) dst_d + size0, src1_d, size1))); 239 } 240 } else { 241 concat_T_sycl_non_cont( 242 stream, (const char *) src0->data, (const char *) src1->data, 243 (char *) dst->data, 244 ne00_blk, src0->ne[1], src0->ne[2], src0->ne[3], 245 src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3], 246 src1->ne[0] / qk, src1->ne[1], src1->ne[2], src1->ne[3], 247 src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3], 248 ne0_blk, dst->ne[1], dst->ne[2], dst->ne[3], 249 dst->nb[0], dst->nb[1], dst->nb[2], dst->nb[3], dim); 250 } 251 } 252 253 void ggml_sycl_op_concat(ggml_backend_sycl_context & ctx, ggml_tensor *dst) { 254 255 switch (dst->type) { 256 case GGML_TYPE_F32: 257 concat_impl_sycl(ctx, dst); 258 break; 259 case GGML_TYPE_F16: 260 concat_impl_sycl(ctx, dst); 261 break; 262 #ifdef GGML_SYCL_HAS_BF16 263 case GGML_TYPE_BF16: 264 concat_impl_sycl(ctx, dst); 265 break; 266 #endif 267 case GGML_TYPE_I32: 268 concat_impl_sycl(ctx, dst); 269 break; 270 case GGML_TYPE_I16: 271 concat_impl_sycl(ctx, dst); 272 break; 273 case GGML_TYPE_I64: 274 concat_impl_sycl(ctx, dst); 275 break; 276 case GGML_TYPE_I8: 277 concat_impl_sycl(ctx, dst); 278 break; 279 case GGML_TYPE_Q4_0: 280 concat_impl_q_sycl(ctx, dst); 281 break; 282 case GGML_TYPE_Q4_1: 283 concat_impl_q_sycl(ctx, dst); 284 break; 285 case GGML_TYPE_Q5_0: 286 concat_impl_q_sycl(ctx, dst); 287 break; 288 case GGML_TYPE_Q5_1: 289 concat_impl_q_sycl(ctx, dst); 290 break; 291 case GGML_TYPE_Q8_0: 292 concat_impl_q_sycl(ctx, dst); 293 break; 294 default: 295 fprintf(stderr, "%s: unsupported types: dst: %s\n", __func__, ggml_type_name(dst->type)); 296 GGML_ASSERT(false); 297 } 298 }