ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-metal / ggml-metal-common.cpp
| 1 | #include "ggml-metal-common.h" |
| 2 | #include "ggml-metal-fusion.h" |
| 3 | |
| 4 | #include "ggml.h" |
| 5 | #include "ggml-impl.h" |
| 6 | #include "ggml-backend-impl.h" |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | // must stay in sync with the kernel_fwht_<type>_<N> templates in misc.metal. Widths up to |
| 11 | // 512 run on the simdgroup kernel and need no threadgroup memory. The wider ones allocate |
| 12 | // float[N] per threadgroup, so they are only available where that fits. |
| 13 | static bool ggml_metal_fwht_supported_size(int64_t n, size_t max_tg_mem) { |
| 14 | if (n == 64 || n == 128 || n == 256 || n == 512) { |
| 15 | return true; |
| 16 | } |
| 17 | |
| 18 | if (n == 1024 || n == 2048 || n == 4096 || n == 8192) { |
| 19 | return (size_t) n * sizeof(float) <= max_tg_mem; |
| 20 | } |
| 21 | |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | // the FWHT kernels handle a Hadamard-hinted MUL_MAT only under these conditions. supports_op |
| 26 | // and the dispatch must ask the same question: an F16 src1 that is admitted but then falls |
| 27 | // through reaches the generic path, which has no F32 src0 by F16 src1 kernel. |
| 28 | bool ggml_metal_op_mul_mat_use_fwht(const struct ggml_tensor * op, size_t max_tg_mem) { |
| 29 | return ggml_get_op_params_i32(op, 1) == GGML_HINT_SRC0_IS_HADAMARD && op->type == GGML_TYPE_F32 && |
| 30 | (op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16) && ggml_is_contiguous(op->src[1]) && |
| 31 | ggml_is_contiguous(op) && ggml_are_same_shape(op->src[1], op) && |
| 32 | ggml_metal_fwht_supported_size(op->src[1]->ne[0], max_tg_mem); |
| 33 | } |
| 34 | |
| 35 | bool ggml_metal_op_mul_mat_use_mm(const struct ggml_tensor * op, bool has_simdgroup_mm) { |
| 36 | const int64_t ne00 = op->src[0]->ne[0]; |
| 37 | const int64_t ne11 = op->src[1]->ne[1]; |
| 38 | |
| 39 | return !ggml_is_transposed(op->src[0]) && |
| 40 | !ggml_is_transposed(op->src[1]) && |
| 41 | has_simdgroup_mm && ne00 >= 64 && ne11 > 8; |
| 42 | } |
| 43 | |
| 44 | bool ggml_metal_op_mul_mat_id_use_mm(const struct ggml_tensor * op, bool has_simdgroup_mm) { |
| 45 | const int64_t ne00 = op->src[0]->ne[0]; |
| 46 | const int64_t ne02 = op->src[0]->ne[2]; |
| 47 | const int64_t ne21 = op->src[2]->ne[1]; |
| 48 | |
| 49 | return has_simdgroup_mm && ne00 >= 64 && ne02 <= 1024 && ne21 >= 32; |
| 50 | } |
| 51 | |
| 52 | // the most src1 rows of the few-row MMA kernels |
| 53 | static constexpr int64_t GGML_METAL_MMA_ROWS_MAX = 16; |
| 54 | |
| 55 | // src1 rows per 8x8 simdgroup matrix tile of the few-row MMA kernels |
| 56 | static constexpr int64_t GGML_METAL_MMA_TILE_ROWS = 8; |
| 57 | |
| 58 | // weights per K step of the q5_K and generic few-row MMA kernels |
| 59 | static constexpr int64_t GGML_METAL_MMA_K_CHUNK = 64; |
| 60 | |
| 61 | enum ggml_metal_mma_kind ggml_metal_mul_mv_mma_kind(enum ggml_type type, int rt) { |
| 62 | if (type == GGML_TYPE_Q4_0 || (type == GGML_TYPE_Q8_0 && rt == 1)) { |
| 63 | return GGML_METAL_MMA_KIND_BLK; |
| 64 | } |
| 65 | return type == GGML_TYPE_Q5_K ? GGML_METAL_MMA_KIND_Q5_K : GGML_METAL_MMA_KIND_GEN; |
| 66 | } |
| 67 | |
| 68 | int ggml_metal_mul_mv_mma_rt(const struct ggml_tensor * op) { |
| 69 | return op->src[1]->ne[1] > GGML_METAL_MMA_TILE_ROWS ? 2 : 1; |
| 70 | } |
| 71 | |
| 72 | static bool ggml_metal_mul_mv_mma_type_supported(enum ggml_type type) { |
| 73 | switch (type) { |
| 74 | case GGML_TYPE_F32: |
| 75 | case GGML_TYPE_F16: |
| 76 | case GGML_TYPE_Q4_0: |
| 77 | case GGML_TYPE_Q4_1: |
| 78 | case GGML_TYPE_Q5_0: |
| 79 | case GGML_TYPE_Q5_1: |
| 80 | case GGML_TYPE_Q8_0: |
| 81 | case GGML_TYPE_Q4_K: |
| 82 | case GGML_TYPE_Q5_K: |
| 83 | case GGML_TYPE_Q6_K: |
| 84 | case GGML_TYPE_BF16: |
| 85 | case GGML_TYPE_Q1_0: |
| 86 | case GGML_TYPE_Q2_0: |
| 87 | case GGML_TYPE_MXFP4: |
| 88 | case GGML_TYPE_Q2_K: |
| 89 | case GGML_TYPE_Q3_K: |
| 90 | case GGML_TYPE_IQ2_XXS: |
| 91 | case GGML_TYPE_IQ2_XS: |
| 92 | case GGML_TYPE_IQ2_S: |
| 93 | case GGML_TYPE_IQ3_XXS: |
| 94 | case GGML_TYPE_IQ3_S: |
| 95 | case GGML_TYPE_IQ1_S: |
| 96 | case GGML_TYPE_IQ1_M: |
| 97 | case GGML_TYPE_IQ4_NL: |
| 98 | case GGML_TYPE_IQ4_XS: |
| 99 | case GGML_TYPE_TQ2_0: |
| 100 | return true; |
| 101 | default: |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | int64_t ggml_metal_mul_mv_mma_k_step(enum ggml_type type, int rt) { |
| 107 | if (!ggml_metal_mul_mv_mma_type_supported(type)) { |
| 108 | return 0; |
| 109 | } |
| 110 | return ggml_metal_mul_mv_mma_kind(type, rt) == GGML_METAL_MMA_KIND_BLK ? ggml_blck_size(type) : GGML_METAL_MMA_K_CHUNK; |
| 111 | } |
| 112 | |
| 113 | static bool ggml_metal_mul_mat_mma_type_ok(const struct ggml_tensor * op) { |
| 114 | const ggml_tensor * src0 = op->src[0]; |
| 115 | const int64_t step = ggml_metal_mul_mv_mma_k_step(src0->type, ggml_metal_mul_mv_mma_rt(op)); |
| 116 | |
| 117 | return step > 0 && src0->ne[0] % step == 0 && src0->nb[0] == ggml_type_size(src0->type); |
| 118 | } |
| 119 | |
| 120 | // the fewest src1 rows at which the few-row MMA kernels beat the mat-vec kernels (measured on an M3 Ultra) |
| 121 | static int64_t ggml_metal_mul_mv_mma_rows_min(enum ggml_type type) { |
| 122 | switch (type) { |
| 123 | case GGML_TYPE_F32: |
| 124 | return 6; |
| 125 | case GGML_TYPE_TQ2_0: |
| 126 | return 5; |
| 127 | case GGML_TYPE_BF16: |
| 128 | return 4; |
| 129 | case GGML_TYPE_F16: |
| 130 | case GGML_TYPE_Q2_0: |
| 131 | case GGML_TYPE_Q4_K: |
| 132 | case GGML_TYPE_Q5_0: |
| 133 | case GGML_TYPE_Q5_1: |
| 134 | case GGML_TYPE_Q2_K: |
| 135 | case GGML_TYPE_IQ4_NL: |
| 136 | case GGML_TYPE_MXFP4: |
| 137 | return 3; |
| 138 | default: |
| 139 | return 2; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | bool ggml_metal_op_mul_mat_use_mma(const struct ggml_tensor * op) { |
| 144 | const ggml_tensor * src0 = op->src[0]; |
| 145 | const ggml_tensor * src1 = op->src[1]; |
| 146 | |
| 147 | // the batch shape goes into int16 function constants |
| 148 | const bool batch_ok = src1->ne[2] <= INT16_MAX && src1->ne[2]/src0->ne[2] <= INT16_MAX && src1->ne[3]/src0->ne[3] <= INT16_MAX; |
| 149 | |
| 150 | return ggml_metal_mul_mat_mma_type_ok(op) && batch_ok && |
| 151 | src1->type == GGML_TYPE_F32 && src1->ne[1] >= ggml_metal_mul_mv_mma_rows_min(src0->type) && src1->ne[1] <= GGML_METAL_MMA_ROWS_MAX && |
| 152 | !ggml_is_transposed(src0) && !ggml_is_transposed(src1) && |
| 153 | src1->nb[0] == sizeof(float) && src1->nb[1] % 16 == 0 && src1->nb[2] % 16 == 0 && src1->nb[3] % 16 == 0; |
| 154 | } |
| 155 | |
| 156 | bool ggml_metal_op_mul_mat_may_use_mma(const struct ggml_tensor * op) { |
| 157 | return ggml_metal_mul_mv_mma_type_supported(op->src[0]->type) && op->src[1]->type == GGML_TYPE_F32; |
| 158 | } |
| 159 | |
| 160 | // represents a memory range (i.e. an interval from a starting address p0 to an ending address p1 in a given buffer pb) |
| 161 | // the type indicates whether it is a source range (i.e. ops read data from it) or a destination range (i.e. ops write data to it) |
| 162 | struct ggml_mem_range { |
| 163 | uint64_t pb; // buffer id |
| 164 | |
| 165 | uint64_t p0; // begin |
| 166 | uint64_t p1; // end |
| 167 | |
| 168 | ggml_mem_range_type pt; |
| 169 | }; |
| 170 | |
| 171 | struct ggml_mem_ranges { |
| 172 | std::vector<ggml_mem_range> ranges; |
| 173 | |
| 174 | int debug = 0; |
| 175 | }; |
| 176 | |
| 177 | ggml_mem_ranges_t ggml_mem_ranges_init(int debug) { |
| 178 | auto * res = new ggml_mem_ranges; |
| 179 | |
| 180 | res->ranges.reserve(256); |
| 181 | res->debug = debug; |
| 182 | |
| 183 | return res; |
| 184 | } |
| 185 | |
| 186 | void ggml_mem_ranges_free(ggml_mem_ranges_t mrs) { |
| 187 | delete mrs; |
| 188 | } |
| 189 | |
| 190 | void ggml_mem_ranges_reset(ggml_mem_ranges_t mrs) { |
| 191 | mrs->ranges.clear(); |
| 192 | } |
| 193 | |
| 194 | static bool ggml_mem_ranges_add(ggml_mem_ranges_t mrs, ggml_mem_range mr) { |
| 195 | mrs->ranges.push_back(mr); |
| 196 | |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | static ggml_mem_range ggml_mem_range_from_tensor(const ggml_tensor * tensor, ggml_mem_range_type pt) { |
| 201 | // always use the base tensor |
| 202 | tensor = tensor->view_src ? tensor->view_src : tensor; |
| 203 | |
| 204 | GGML_ASSERT(!tensor->view_src); |
| 205 | |
| 206 | ggml_mem_range mr; |
| 207 | |
| 208 | if (tensor->buffer) { |
| 209 | // when the tensor is allocated, use the actual memory address range in the buffer |
| 210 | // |
| 211 | // take the actual allocated size with ggml_backend_buft_get_alloc_size() |
| 212 | // this can be larger than the tensor size if the buffer type allocates extra memory |
| 213 | // ref: https://github.com/ggml-org/llama.cpp/pull/15966 |
| 214 | mr = { |
| 215 | /*.pb =*/ (uint64_t) tensor->buffer, |
| 216 | /*.p0 =*/ (uint64_t) tensor->data, |
| 217 | /*.p1 =*/ (uint64_t) tensor->data + ggml_backend_buft_get_alloc_size(tensor->buffer->buft, tensor), |
| 218 | /*.pt =*/ pt, |
| 219 | }; |
| 220 | } else { |
| 221 | // otherwise, the pointer address is used as an unique id of the memory ranges |
| 222 | // that the tensor will be using when it is allocated |
| 223 | mr = { |
| 224 | /*.pb =*/ (uint64_t) tensor, |
| 225 | /*.p0 =*/ 0, // |
| 226 | /*.p1 =*/ 1024, // [0, 1024) is a dummy range, not used |
| 227 | /*.pt =*/ pt, |
| 228 | }; |
| 229 | }; |
| 230 | |
| 231 | return mr; |
| 232 | } |
| 233 | |
| 234 | static ggml_mem_range ggml_mem_range_from_tensor_src(const ggml_tensor * tensor) { |
| 235 | return ggml_mem_range_from_tensor(tensor, MEM_RANGE_TYPE_SRC); |
| 236 | } |
| 237 | |
| 238 | static ggml_mem_range ggml_mem_range_from_tensor_dst(const ggml_tensor * tensor) { |
| 239 | return ggml_mem_range_from_tensor(tensor, MEM_RANGE_TYPE_DST); |
| 240 | } |
| 241 | |
| 242 | static bool ggml_mem_ranges_add_src(ggml_mem_ranges_t mrs, const ggml_tensor * tensor) { |
| 243 | GGML_ASSERT(tensor); |
| 244 | |
| 245 | ggml_mem_range mr = ggml_mem_range_from_tensor_src(tensor); |
| 246 | |
| 247 | if (mrs->debug > 2) { |
| 248 | GGML_LOG_DEBUG("%s: add src range buf=%lld, [%lld, %lld)\n", __func__, mr.pb, mr.p0, mr.p1); |
| 249 | } |
| 250 | |
| 251 | return ggml_mem_ranges_add(mrs, mr); |
| 252 | } |
| 253 | |
| 254 | static bool ggml_mem_ranges_add_dst(ggml_mem_ranges_t mrs, const ggml_tensor * tensor) { |
| 255 | GGML_ASSERT(tensor); |
| 256 | |
| 257 | ggml_mem_range mr = ggml_mem_range_from_tensor_dst(tensor); |
| 258 | |
| 259 | if (mrs->debug > 2) { |
| 260 | GGML_LOG_DEBUG("%s: add dst range buf=%lld, [%lld, %lld)\n", __func__, mr.pb, mr.p0, mr.p1); |
| 261 | } |
| 262 | |
| 263 | return ggml_mem_ranges_add(mrs, mr); |
| 264 | } |
| 265 | |
| 266 | bool ggml_mem_ranges_add(ggml_mem_ranges_t mrs, const ggml_tensor * tensor) { |
| 267 | for (int i = 0; i < GGML_MAX_SRC; i++) { |
| 268 | if (tensor->src[i]) { |
| 269 | ggml_mem_ranges_add_src(mrs, tensor->src[i]); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return ggml_mem_ranges_add_dst(mrs, tensor); |
| 274 | } |
| 275 | |
| 276 | static bool ggml_mem_ranges_check(ggml_mem_ranges_t mrs, ggml_mem_range mr) { |
| 277 | for (size_t i = 0; i < mrs->ranges.size(); i++) { |
| 278 | const auto & cmp = mrs->ranges[i]; |
| 279 | |
| 280 | // two memory ranges cannot intersect if they are in different buffers |
| 281 | if (mr.pb != cmp.pb) { |
| 282 | continue; |
| 283 | } |
| 284 | |
| 285 | // intersecting source ranges are allowed |
| 286 | if (mr.pt == MEM_RANGE_TYPE_SRC && cmp.pt == MEM_RANGE_TYPE_SRC) { |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | if (mr.p0 < cmp.p1 && mr.p1 >= cmp.p0) { |
| 291 | if (mrs->debug > 2) { |
| 292 | GGML_LOG_DEBUG("%s: the %s range buf=%lld, [%lld, %lld) overlaps with a previous %s range buf=%lld, [%lld, %lld)\n", |
| 293 | __func__, |
| 294 | mr.pt == MEM_RANGE_TYPE_SRC ? "src" : "dst", |
| 295 | mr.pb, mr.p0, mr.p1, |
| 296 | cmp.pt == MEM_RANGE_TYPE_SRC ? "src" : "dst", |
| 297 | cmp.pb, cmp.p0, cmp.p1); |
| 298 | } |
| 299 | |
| 300 | return false; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | static bool ggml_mem_ranges_check_src(ggml_mem_ranges_t mrs, const ggml_tensor * tensor) { |
| 308 | GGML_ASSERT(tensor); |
| 309 | |
| 310 | ggml_mem_range mr = ggml_mem_range_from_tensor_src(tensor); |
| 311 | |
| 312 | const bool res = ggml_mem_ranges_check(mrs, mr); |
| 313 | |
| 314 | return res; |
| 315 | } |
| 316 | |
| 317 | static bool ggml_mem_ranges_check_dst(ggml_mem_ranges_t mrs, const ggml_tensor * tensor) { |
| 318 | GGML_ASSERT(tensor); |
| 319 | |
| 320 | ggml_mem_range mr = ggml_mem_range_from_tensor_dst(tensor); |
| 321 | |
| 322 | const bool res = ggml_mem_ranges_check(mrs, mr); |
| 323 | |
| 324 | return res; |
| 325 | } |
| 326 | |
| 327 | bool ggml_mem_ranges_check(ggml_mem_ranges_t mrs, const ggml_tensor * tensor) { |
| 328 | for (int i = 0; i < GGML_MAX_SRC; i++) { |
| 329 | if (tensor->src[i]) { |
| 330 | if (!ggml_mem_ranges_check_src(mrs, tensor->src[i])) { |
| 331 | return false; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | return ggml_mem_ranges_check_dst(mrs, tensor); |
| 337 | } |
| 338 | |
| 339 | struct node_info { |
| 340 | ggml_tensor * node; |
| 341 | |
| 342 | std::vector<ggml_tensor *> fused; |
| 343 | |
| 344 | ggml_op op() const { |
| 345 | return node->op; |
| 346 | } |
| 347 | |
| 348 | const ggml_tensor * dst() const { |
| 349 | return fused.empty() ? node : fused.back(); |
| 350 | } |
| 351 | |
| 352 | bool is_empty() const { |
| 353 | return ggml_op_is_empty(node->op); |
| 354 | } |
| 355 | |
| 356 | void add_fused(ggml_tensor * t) { |
| 357 | fused.push_back(t); |
| 358 | } |
| 359 | |
| 360 | bool is_output(const ggml_tensor * t) const { |
| 361 | if (t == node) { |
| 362 | return true; |
| 363 | } |
| 364 | for (const auto * f : fused) { |
| 365 | if (t == f) { |
| 366 | return true; |
| 367 | } |
| 368 | } |
| 369 | return false; |
| 370 | } |
| 371 | }; |
| 372 | |
| 373 | static std::vector<int> ggml_metal_graph_optimize_reorder(const std::vector<node_info> & nodes) { |
| 374 | // helper to add node src and dst ranges |
| 375 | const auto & h_add = [](ggml_mem_ranges_t mrs, const node_info & node) { |
| 376 | // only external sources matter: sources produced by the fused group are internal |
| 377 | for (int i = 0; i < GGML_MAX_SRC; i++) { |
| 378 | const ggml_tensor * src = node.node->src[i]; |
| 379 | if (src && !node.is_output(src)) { |
| 380 | if (!ggml_mem_ranges_add_src(mrs, src)) { |
| 381 | return false; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | for (const auto * fused : node.fused) { |
| 387 | for (int i = 0; i < GGML_MAX_SRC; i++) { |
| 388 | const ggml_tensor * src = fused->src[i]; |
| 389 | if (src && !node.is_output(src)) { |
| 390 | if (!ggml_mem_ranges_add_src(mrs, src)) { |
| 391 | return false; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | // all fused tensors are produced by the fused kernel |
| 398 | if (!ggml_mem_ranges_add_dst(mrs, node.node)) { |
| 399 | return false; |
| 400 | } |
| 401 | for (const auto * fused : node.fused) { |
| 402 | if (!ggml_mem_ranges_add_dst(mrs, fused)) { |
| 403 | return false; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | return true; |
| 408 | }; |
| 409 | |
| 410 | // helper to check if a node can run concurrently with the existing set of nodes |
| 411 | const auto & h_check = [](ggml_mem_ranges_t mrs, const node_info & node) { |
| 412 | for (int i = 0; i < GGML_MAX_SRC; i++) { |
| 413 | const ggml_tensor * src = node.node->src[i]; |
| 414 | if (src && !node.is_output(src)) { |
| 415 | if (!ggml_mem_ranges_check_src(mrs, src)) { |
| 416 | return false; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | for (const auto * fused : node.fused) { |
| 422 | for (int i = 0; i < GGML_MAX_SRC; i++) { |
| 423 | const ggml_tensor * src = fused->src[i]; |
| 424 | if (src && !node.is_output(src)) { |
| 425 | if (!ggml_mem_ranges_check_src(mrs, src)) { |
| 426 | return false; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (!ggml_mem_ranges_check_dst(mrs, node.node)) { |
| 433 | return false; |
| 434 | } |
| 435 | for (const auto * fused : node.fused) { |
| 436 | if (!ggml_mem_ranges_check_dst(mrs, fused)) { |
| 437 | return false; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | return true; |
| 442 | }; |
| 443 | |
| 444 | // perform reorders only across these types of ops |
| 445 | // can be expanded when needed |
| 446 | const auto & h_safe = [](ggml_op op) { |
| 447 | switch (op) { |
| 448 | case GGML_OP_MUL_MAT: |
| 449 | case GGML_OP_MUL_MAT_ID: |
| 450 | case GGML_OP_ROPE: |
| 451 | case GGML_OP_NORM: |
| 452 | case GGML_OP_RMS_NORM: |
| 453 | case GGML_OP_GROUP_NORM: |
| 454 | case GGML_OP_L2_NORM: |
| 455 | case GGML_OP_SUM_ROWS: |
| 456 | case GGML_OP_SSM_CONV: |
| 457 | case GGML_OP_SSM_SCAN: |
| 458 | case GGML_OP_CLAMP: |
| 459 | case GGML_OP_TRI: |
| 460 | case GGML_OP_DIAG: |
| 461 | case GGML_OP_MUL: |
| 462 | case GGML_OP_ADD: |
| 463 | case GGML_OP_SUB: |
| 464 | case GGML_OP_DIV: |
| 465 | case GGML_OP_GLU: |
| 466 | case GGML_OP_SCALE: |
| 467 | case GGML_OP_UNARY: |
| 468 | case GGML_OP_GET_ROWS: |
| 469 | case GGML_OP_SET_ROWS: |
| 470 | case GGML_OP_SET: |
| 471 | case GGML_OP_CPY: |
| 472 | case GGML_OP_CONT: |
| 473 | case GGML_OP_REPEAT: |
| 474 | return true; |
| 475 | default: |
| 476 | return ggml_op_is_empty(op); |
| 477 | } |
| 478 | }; |
| 479 | |
| 480 | const int n = nodes.size(); |
| 481 | |
| 482 | std::vector<int> res; |
| 483 | res.reserve(n); |
| 484 | |
| 485 | std::vector<bool> used(n, false); |
| 486 | |
| 487 | // the memory ranges for the set of currently concurrent nodes |
| 488 | ggml_mem_ranges_t mrs0 = ggml_mem_ranges_init(0); |
| 489 | |
| 490 | // the memory ranges for the set of nodes that haven't been processed yet, when looking forward for a node to reorder |
| 491 | ggml_mem_ranges_t mrs1 = ggml_mem_ranges_init(0); |
| 492 | |
| 493 | for (int i0 = 0; i0 < n; i0++) { |
| 494 | if (used[i0]) { |
| 495 | continue; |
| 496 | } |
| 497 | |
| 498 | const auto & node0 = nodes[i0]; |
| 499 | |
| 500 | // the node is not concurrent with the existing concurrent set, so we have to "put a barrier" (i.e reset mrs0) |
| 501 | // but before we do that, look forward for some other nodes that can be added to the concurrent set mrs0 |
| 502 | // |
| 503 | // note: we can always add empty nodes to the concurrent set as they don't read nor write anything |
| 504 | if (!node0.is_empty() && !h_check(mrs0, node0)) { |
| 505 | // this will hold the set of memory ranges from the nodes that haven't been processed yet |
| 506 | // if a node is not concurrent with this set, we cannot reorder it |
| 507 | ggml_mem_ranges_reset(mrs1); |
| 508 | |
| 509 | // initialize it with the current node |
| 510 | h_add(mrs1, node0); |
| 511 | |
| 512 | // that many nodes forward to search for a concurrent node |
| 513 | constexpr int N_FORWARD = 64; |
| 514 | |
| 515 | for (int i1 = i0 + 1; i1 < i0 + N_FORWARD && i1 < n; i1++) { |
| 516 | if (used[i1]) { |
| 517 | continue; |
| 518 | } |
| 519 | |
| 520 | const auto & node1 = nodes[i1]; |
| 521 | |
| 522 | // disallow reordering of certain ops |
| 523 | if (!h_safe(node1.op())) { |
| 524 | break; |
| 525 | } |
| 526 | |
| 527 | const bool is_empty = node1.is_empty(); |
| 528 | |
| 529 | // to reorder a node and add it to the concurrent set, it has to be: |
| 530 | // + empty or concurrent with all nodes in the existing concurrent set (mrs0) |
| 531 | // + concurrent with all nodes prior to it that haven't been processed yet (mrs1) |
| 532 | if ((is_empty || h_check(mrs0, node1)) && h_check(mrs1, node1)) { |
| 533 | // add the node to the existing concurrent set (i.e. reorder it for early execution) |
| 534 | h_add(mrs0, node1); |
| 535 | res.push_back(i1); |
| 536 | |
| 537 | // mark as used, so we skip re-processing it later |
| 538 | used[i1] = true; |
| 539 | } else { |
| 540 | // expand the set of nodes that haven't been processed yet |
| 541 | h_add(mrs1, node1); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | // finalize the concurrent set and begin a new one |
| 546 | ggml_mem_ranges_reset(mrs0); |
| 547 | } |
| 548 | |
| 549 | // expand the concurrent set with the current node |
| 550 | { |
| 551 | h_add(mrs0, node0); |
| 552 | res.push_back(i0); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | ggml_mem_ranges_free(mrs0); |
| 557 | ggml_mem_ranges_free(mrs1); |
| 558 | |
| 559 | return res; |
| 560 | } |
| 561 | |
| 562 | void ggml_graph_optimize(ggml_cgraph * gf) { |
| 563 | const int n = gf->n_nodes; |
| 564 | |
| 565 | std::vector<node_info> nodes; |
| 566 | nodes.reserve(gf->n_nodes); |
| 567 | |
| 568 | // fuse nodes: |
| 569 | // we don't want to make reorders that break fusing, so we first pack all fusable tensors |
| 570 | // and perform the reorder over the fused nodes. after the reorder is done, we unfuse |
| 571 | // |
| 572 | // the fusable sequences are declared in the fusion table (ggml-metal-fuse.cpp), so the |
| 573 | // packing here is driven by the same patterns that the op encoders will later use |
| 574 | for (int i = 0; i < n; i++) { |
| 575 | node_info node = { |
| 576 | /*.node =*/ gf->nodes[i], |
| 577 | /*.fused =*/ {}, |
| 578 | }; |
| 579 | |
| 580 | const int f = ggml_metal_fusion_max(gf, i); |
| 581 | |
| 582 | // add the fused tensors into the node info so we can unfuse them later |
| 583 | for (int k = 1; k < f; k++) { |
| 584 | ++i; |
| 585 | |
| 586 | // the .dst() becomes the last fused tensor |
| 587 | node.add_fused(gf->nodes[i]); |
| 588 | } |
| 589 | |
| 590 | nodes.push_back(std::move(node)); |
| 591 | } |
| 592 | |
| 593 | #if 1 |
| 594 | // reorder to improve concurrency |
| 595 | const auto order = ggml_metal_graph_optimize_reorder(nodes); |
| 596 | #else |
| 597 | std::vector<int> order(nodes.size()); |
| 598 | for (size_t i = 0; i < nodes.size(); i++) { |
| 599 | order[i] = i; |
| 600 | } |
| 601 | #endif |
| 602 | |
| 603 | // unfuse |
| 604 | { |
| 605 | int j = 0; |
| 606 | for (const auto i : order) { |
| 607 | const auto & node = nodes[i]; |
| 608 | |
| 609 | gf->nodes[j++] = node.node; |
| 610 | |
| 611 | for (auto * fused : node.fused) { |
| 612 | gf->nodes[j++] = fused; |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | } |