ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-hexagon / htp-fusion.cpp
| 1 | #include "htp-fusion.h" |
| 2 | #include "htp-kparams.h" |
| 3 | #include "ggml-impl.h" |
| 4 | |
| 5 | #include <algorithm> |
| 6 | #include <cstring> |
| 7 | |
| 8 | static inline bool htp_same_tensor_data(const ggml_tensor * a, const ggml_tensor * b) { |
| 9 | return a && b && (a == b || a->data == b->data); |
| 10 | } |
| 11 | |
| 12 | static inline bool htp_same_shape_and_contiguity(const ggml_tensor * a, const ggml_tensor * b) { |
| 13 | return ggml_are_same_shape(a, b) && (ggml_is_contiguous(a) == ggml_is_contiguous(b)); |
| 14 | } |
| 15 | |
| 16 | static inline bool htp_inplace_overlap_unsafe(const ggml_tensor * dst, const ggml_tensor * in) { |
| 17 | return htp_tensors_overlap(dst, in) && (dst->data != in->data || !ggml_are_same_stride(dst, in)); |
| 18 | } |
| 19 | |
| 20 | static inline bool htp_inputs_overlap_unsafe(const htp_opnode & prev, const ggml_tensor * dst, const ggml_tensor * extra = nullptr) { |
| 21 | if (extra && htp_inplace_overlap_unsafe(dst, extra)) return true; |
| 22 | for (const auto * in : prev.inputs) { |
| 23 | if (htp_inplace_overlap_unsafe(dst, in)) return true; |
| 24 | } |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | static inline bool htp_match_consumer( |
| 29 | const htp_opnode & prev, |
| 30 | const htp_opnode & cur, |
| 31 | bool commutative, |
| 32 | const ggml_tensor ** other_src = nullptr |
| 33 | ) { |
| 34 | if (prev.outputs.size() != 1) { |
| 35 | return false; |
| 36 | } |
| 37 | const ggml_tensor * prev_out = prev.dst(); |
| 38 | if (!htp_tensor_is_fuseable(prev_out)) { |
| 39 | return false; |
| 40 | } |
| 41 | if (htp_same_tensor_data(cur.src0(), prev_out)) { |
| 42 | if (other_src) *other_src = cur.src1(); |
| 43 | return true; |
| 44 | } |
| 45 | if (commutative && htp_same_tensor_data(cur.src1(), prev_out)) { |
| 46 | if (other_src) *other_src = cur.src0(); |
| 47 | return true; |
| 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | static bool is_supported_mul_mat_nx_kernel(const ggml_tensor * src0, const struct htp_mm_kernel_params * kparams) { |
| 53 | if (kparams->n_hmx) { |
| 54 | return kparams->kernel_type == HTP_MM_KERNEL_HMX_2D; |
| 55 | } |
| 56 | return htp_tensor_is_tiled(src0) && (kparams->kernel_type == HTP_MM_KERNEL_HVX_QUANT_ROW); |
| 57 | } |
| 58 | |
| 59 | static bool is_supported_mul_mat_id_nx_kernel(const ggml_tensor * src0, const struct htp_mm_kernel_params * kparams) { |
| 60 | if (kparams->n_hmx) { |
| 61 | return kparams->kernel_type == HTP_MM_KERNEL_HMX_2D; |
| 62 | } |
| 63 | return htp_tensor_is_tiled(src0) && |
| 64 | (kparams->kernel_type == HTP_MM_KERNEL_HVX_QUANT_ROW || kparams->kernel_type == HTP_MM_KERNEL_HVX_QUANT_BLOCK); |
| 65 | } |
| 66 | |
| 67 | // --- Fusion rules --- |
| 68 | |
| 69 | static bool fuse_allreduce_add(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) { |
| 70 | if (opt_ar_select != 2 || prev.opcode != HTP_OP_ALLREDUCE) return false; |
| 71 | |
| 72 | const auto * ar_kparams = prev.kparams<htp_allreduce_kernel_params>(); |
| 73 | const uint32_t rank = (uint32_t) ar_kparams->rank; |
| 74 | const uint32_t n_ranks = (uint32_t) ar_kparams->n_ranks; |
| 75 | |
| 76 | const ggml_tensor * ar_local = prev.inputs[rank]; |
| 77 | if (!htp_tensor_is_fuseable(ar_local)) return false; |
| 78 | |
| 79 | const ggml_tensor * add_dst = cur.dst(); |
| 80 | const ggml_tensor * res_tensor = nullptr; |
| 81 | if (htp_same_tensor_data(cur.src0(), ar_local)) { |
| 82 | res_tensor = cur.src1(); |
| 83 | } else if (htp_same_tensor_data(cur.src1(), ar_local)) { |
| 84 | res_tensor = cur.src0(); |
| 85 | } else { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | if (ar_local->type != res_tensor->type) return false; |
| 90 | |
| 91 | const bool is_same_shape = ggml_are_same_shape(ar_local, res_tensor); |
| 92 | const bool is_row_bcast = !is_same_shape && (ar_local->ne[0] == res_tensor->ne[0] && |
| 93 | res_tensor->ne[1] == 1 && res_tensor->ne[2] == 1 && res_tensor->ne[3] == 1); |
| 94 | if (!is_same_shape && !is_row_bcast) return false; |
| 95 | |
| 96 | if (is_same_shape && !ggml_are_same_stride(ar_local, res_tensor)) { |
| 97 | return false; |
| 98 | } |
| 99 | if (ggml_is_contiguous(ar_local) != ggml_is_contiguous(add_dst)) return false; |
| 100 | |
| 101 | const bool is_shard_ok = n_ranks <= HTP_OP_MAX_OUTPUTS && add_dst->data == ar_local->data; |
| 102 | |
| 103 | struct htp_allreduce_kernel_params kparams; |
| 104 | if (!htp_kparams_solve_allreduce(sess, add_dst, rank, n_ranks, true, is_row_bcast, is_shard_ok, &kparams)) { |
| 105 | HEX_VERBOSE("ggml-hex: %s skip ALLREDUCE_ADD fusion: solver failed\n", htp_session_name(sess)); |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | const bool scatter_ok = kparams.n_dsts > 1; |
| 110 | kparams.mode = scatter_ok ? HTP_ALLREDUCE_SHARDED_FANOUT : HTP_ALLREDUCE_FULL; |
| 111 | |
| 112 | for (uint32_t r = 0; r < n_ranks; r++) { |
| 113 | const ggml_tensor * ar_src = prev.inputs[r]; |
| 114 | if (!htp_tensors_overlap(add_dst, ar_src)) continue; |
| 115 | if (scatter_ok && r == rank) continue; |
| 116 | HEX_VERBOSE("ggml-hex: %s skip ALLREDUCE_ADD fusion: dst overlaps allreduce src %u\n", htp_session_name(sess), r); |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | prev.opcode = HTP_OP_ALLREDUCE_ADD; |
| 121 | prev.inputs.push_back(res_tensor); |
| 122 | if (kparams.mode != HTP_ALLREDUCE_SHARDED_FANOUT) { |
| 123 | prev.outputs = { add_dst }; |
| 124 | } |
| 125 | prev.fused.push_back(cur.node); |
| 126 | prev.set_kparams(kparams); |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | static bool fuse_rms_norm_mul(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) { |
| 131 | if (prev.opcode != HTP_OP_RMS_NORM) return false; |
| 132 | |
| 133 | const ggml_tensor * weight = nullptr; |
| 134 | if (!htp_match_consumer(prev, cur, /*commutative=*/ true, &weight)) return false; |
| 135 | |
| 136 | const ggml_tensor * src0 = prev.src0(); |
| 137 | const ggml_tensor * dst = cur.dst(); |
| 138 | if (src0->type != GGML_TYPE_F32 || src0->ne[0] != weight->ne[0] || !htp_same_shape_and_contiguity(prev.dst(), dst)) return false; |
| 139 | if (htp_inputs_overlap_unsafe(prev, dst, weight)) return false; |
| 140 | |
| 141 | const bool is_row_bcast = (weight->ne[1] == 1 && weight->ne[2] == 1 && weight->ne[3] == 1); |
| 142 | const bool is_same_shape = ggml_are_same_shape(src0, weight); |
| 143 | if (!is_row_bcast && !is_same_shape) return false; |
| 144 | |
| 145 | struct htp_unary_kernel_params kparams; |
| 146 | if (!htp_kparams_solve_rms_norm_mul(sess, src0, weight, dst, &kparams)) { |
| 147 | HEX_VERBOSE("ggml-hex: %s skip RMS_NORM_MUL fusion: VTCM needed (%d) > budget (%zu)\n", |
| 148 | htp_session_name(sess), kparams.vtcm_size, htp_session_vtcm_size(sess)); |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | prev.opcode = HTP_OP_RMS_NORM_MUL; |
| 153 | prev.inputs = { src0, weight }; |
| 154 | prev.outputs = { dst }; |
| 155 | prev.fused.push_back(cur.node); |
| 156 | prev.set_kparams(kparams); |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | static bool fuse_rms_norm_scale(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) { |
| 161 | GGML_UNUSED(sess); |
| 162 | if (prev.opcode != HTP_OP_RMS_NORM && prev.opcode != HTP_OP_RMS_NORM_MUL) return false; |
| 163 | if (!htp_match_consumer(prev, cur, /*commutative=*/ false)) return false; |
| 164 | |
| 165 | const ggml_tensor * src0 = prev.src0(); |
| 166 | const ggml_tensor * dst = cur.dst(); |
| 167 | if (src0->type != GGML_TYPE_F32 || !htp_same_shape_and_contiguity(prev.dst(), dst)) return false; |
| 168 | if (htp_inputs_overlap_unsafe(prev, dst)) return false; |
| 169 | |
| 170 | float scale = 0.0f; |
| 171 | float bias = 0.0f; |
| 172 | memcpy(&scale, (const float *) cur.params + 0, sizeof(float)); |
| 173 | memcpy(&bias, (const float *) cur.params + 1, sizeof(float)); |
| 174 | if (bias != 0.0f || scale == 0.0f) return false; |
| 175 | |
| 176 | float prev_scale = 0.0f; |
| 177 | memcpy(&prev_scale, &prev.params[1], sizeof(float)); |
| 178 | if (prev_scale != 0.0f) { |
| 179 | scale *= prev_scale; |
| 180 | } |
| 181 | memcpy(&prev.params[1], &scale, sizeof(float)); |
| 182 | |
| 183 | prev.outputs[0] = dst; |
| 184 | prev.fused.push_back(cur.node); |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | static bool fuse_mul_mat_add(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) { |
| 189 | if (prev.opcode != HTP_OP_MUL_MAT) return false; |
| 190 | |
| 191 | const ggml_tensor * src2 = nullptr; |
| 192 | if (!htp_match_consumer(prev, cur, /*commutative=*/ true, &src2)) return false; |
| 193 | if (src2->type != GGML_TYPE_F32) return false; |
| 194 | |
| 195 | const ggml_tensor * src0 = prev.node->src[0]; |
| 196 | const ggml_tensor * src1 = prev.node->src[1]; |
| 197 | const ggml_tensor * dst = cur.node; |
| 198 | if (!ggml_are_same_shape(prev.node, dst)) return false; |
| 199 | |
| 200 | const bool prev_collapsed = htp_matmul_can_collapse(src0, src1, prev.node); |
| 201 | const bool cur_collapsed = htp_matmul_add_can_collapse(src0, src1, src2, dst); |
| 202 | if (prev_collapsed != cur_collapsed) { |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | const auto * orig_kparams = prev.kparams<htp_mm_kernel_params>(); |
| 207 | struct htp_mm_kernel_params kparams; |
| 208 | if (!htp_kparams_solve_matmul_add(sess, src0, src1, src2, dst, &kparams)) { |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | const int src1_nrows = src1->ne[1] * src1->ne[2] * src1->ne[3]; |
| 213 | if (kparams.n_hmx == 0 && src1_nrows != 1) return false; |
| 214 | |
| 215 | if (kparams.n_hmx > 0 && orig_kparams->n_hmx > 0) { |
| 216 | if (kparams.m_chunk < orig_kparams->m_chunk || |
| 217 | kparams.n_chunk < orig_kparams->n_chunk || |
| 218 | kparams.n_act_threads < orig_kparams->n_act_threads) { |
| 219 | HEX_VERBOSE("ggml-hex: %s skip MUL_MAT_ADD fusion: HMX efficiency reduced (m %d->%d, n %d->%d, th %d->%d)\n", |
| 220 | htp_session_name(sess), orig_kparams->m_chunk, kparams.m_chunk, |
| 221 | orig_kparams->n_chunk, kparams.n_chunk, |
| 222 | orig_kparams->n_act_threads, kparams.n_act_threads); |
| 223 | return false; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | prev.opcode = HTP_OP_MUL_MAT_ADD; |
| 228 | prev.inputs = { src0, src1, src2 }; |
| 229 | prev.outputs = { dst }; |
| 230 | htp_opnode_collapse(sess, prev); |
| 231 | prev.fused.push_back(cur.node); |
| 232 | prev.set_kparams(kparams); |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | static bool fuse_mul_mat_nx(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) { |
| 237 | if (prev.opcode != HTP_OP_MUL_MAT && prev.opcode != HTP_OP_MUL_MAT_NX) return false; |
| 238 | |
| 239 | const uint32_t curr_n = (uint32_t) prev.outputs.size(); |
| 240 | if (curr_n >= HTP_OP_MAX_OUTPUTS || curr_n + 1 >= HTP_OP_MAX_INPUTS) return false; |
| 241 | |
| 242 | const ggml_tensor * w0 = prev.inputs[0]; |
| 243 | const ggml_tensor * x_orig = prev.node->src[1]; |
| 244 | const ggml_tensor * w_in = cur.node->src[0]; |
| 245 | const ggml_tensor * x_in = cur.node->src[1]; |
| 246 | const ggml_tensor * d_in = cur.node; |
| 247 | |
| 248 | auto * orig_kparams = prev.kparams<htp_mm_kernel_params>(); |
| 249 | const auto * cur_kparams = cur.kparams<htp_mm_kernel_params>(); |
| 250 | |
| 251 | if (x_in != x_orig || w_in->type != w0->type || w_in->ne[0] != w0->ne[0] || |
| 252 | w0->ne[2] != 1 || w0->ne[3] != 1 || w_in->ne[2] != 1 || w_in->ne[3] != 1 || |
| 253 | htp_tensor_is_tiled(w_in) != htp_tensor_is_tiled(w0) || |
| 254 | orig_kparams->n_hmx != cur_kparams->n_hmx) { |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | const bool collapse = htp_matmul_can_collapse(w0, x_orig, prev.node) && htp_matmul_can_collapse(w_in, x_in, d_in); |
| 259 | if ((x_orig->ne[2] > 1 || x_orig->ne[3] > 1) && !collapse) return false; |
| 260 | |
| 261 | if (prev.opcode == HTP_OP_MUL_MAT_NX) { |
| 262 | orig_kparams->n_weights = curr_n + 1; |
| 263 | } else { |
| 264 | struct htp_mm_kernel_params kparams; |
| 265 | const ggml_tensor x_collapsed = collapse ? htp_tensor_collapse_rows(x_orig) : *x_orig; |
| 266 | if (!htp_kparams_solve_mmnx(sess, w0, &x_collapsed, curr_n + 1, &kparams)) return false; |
| 267 | if (!is_supported_mul_mat_nx_kernel(w0, &kparams)) return false; |
| 268 | |
| 269 | prev.set_kparams(kparams); |
| 270 | prev.opcode = HTP_OP_MUL_MAT_NX; |
| 271 | } |
| 272 | |
| 273 | prev.inputs.insert(prev.inputs.begin() + curr_n, w_in); |
| 274 | prev.outputs.push_back(collapse ? prev.add_dummy(htp_tensor_collapse_rows(d_in)) : d_in); |
| 275 | prev.fused.push_back(cur.node); |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | static bool fuse_mul_mat_id_nx(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) { |
| 280 | if (prev.opcode != HTP_OP_MUL_MAT_ID && prev.opcode != HTP_OP_MUL_MAT_ID_NX) return false; |
| 281 | |
| 282 | const uint32_t curr_n = (uint32_t) prev.outputs.size(); |
| 283 | if (curr_n >= HTP_OP_MAX_OUTPUTS || curr_n + 2 >= HTP_OP_MAX_INPUTS) return false; |
| 284 | |
| 285 | const ggml_tensor * w0 = prev.inputs[0]; |
| 286 | const ggml_tensor * x = prev.inputs[curr_n]; |
| 287 | const ggml_tensor * ids = prev.inputs[curr_n + 1]; |
| 288 | const ggml_tensor * w_in = cur.src0(); |
| 289 | const ggml_tensor * x_in = cur.src1(); |
| 290 | const ggml_tensor * ids_in = cur.node->src[2]; |
| 291 | const ggml_tensor * d_in = cur.dst(); |
| 292 | |
| 293 | auto * orig_kparams = prev.kparams<htp_mm_kernel_params>(); |
| 294 | const auto * cur_kparams = cur.kparams<htp_mm_kernel_params>(); |
| 295 | |
| 296 | if (x_in != x || ids_in != ids || w_in->type != w0->type || |
| 297 | w_in->ne[0] != w0->ne[0] || w_in->ne[2] != w0->ne[2] || |
| 298 | htp_tensor_is_tiled(w_in) != htp_tensor_is_tiled(w0) || |
| 299 | orig_kparams->n_hmx != cur_kparams->n_hmx) { |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | struct htp_mm_kernel_params kparams; |
| 304 | if (d_in->nb[1] == prev.node->nb[1]) { |
| 305 | kparams = *orig_kparams; |
| 306 | kparams.n_weights = curr_n + 1; |
| 307 | } else if (!htp_kparams_solve_mmidnx(sess, w0, x, d_in, curr_n + 1, &kparams)) { |
| 308 | return false; |
| 309 | } |
| 310 | if (!is_supported_mul_mat_id_nx_kernel(w0, &kparams)) return false; |
| 311 | |
| 312 | prev.opcode = HTP_OP_MUL_MAT_ID_NX; |
| 313 | prev.inputs.insert(prev.inputs.begin() + curr_n, w_in); |
| 314 | prev.outputs.push_back(d_in); |
| 315 | prev.fused.push_back(cur.node); |
| 316 | prev.set_kparams(kparams); |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | static bool fuse_gdn_cpy(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) { |
| 321 | GGML_UNUSED(sess); |
| 322 | if (prev.opcode != HTP_OP_GATED_DELTA_NET || prev.outputs.size() != 1) return false; |
| 323 | |
| 324 | const ggml_tensor * gdn_out = prev.dst(); |
| 325 | const ggml_tensor * cpy_node = cur.node; |
| 326 | const ggml_tensor * cpy_src = cur.src0(); |
| 327 | const ggml_tensor * cpy_dst = cur.dst(); |
| 328 | |
| 329 | if (gdn_out->type != GGML_TYPE_F32 || cpy_src->type != GGML_TYPE_F32 || cpy_dst->type != GGML_TYPE_F32) return false; |
| 330 | if ((gdn_out->flags & GGML_TENSOR_FLAG_OUTPUT) || (cpy_node->flags & GGML_TENSOR_FLAG_OUTPUT)) return false; |
| 331 | |
| 332 | const ggml_tensor * v = prev.node->src[2]; |
| 333 | if (!v) return false; |
| 334 | |
| 335 | const int64_t S_v = v->ne[0]; |
| 336 | const int64_t H = v->ne[1]; |
| 337 | const int64_t n_tokens = v->ne[2]; |
| 338 | const int64_t n_seqs = v->ne[3]; |
| 339 | const int64_t K = ggml_get_op_params_i32(prev.node, 0); |
| 340 | const size_t tail_off = (size_t) S_v * H * n_tokens * n_seqs * sizeof(float); |
| 341 | const int64_t D = S_v * S_v * H; |
| 342 | const int64_t n_written = std::min<int64_t>(n_tokens, K); |
| 343 | |
| 344 | if (cpy_src->op != GGML_OP_VIEW || !htp_same_tensor_data(cpy_src->view_src, gdn_out) || |
| 345 | cpy_src->view_offs != tail_off || !ggml_is_contiguous(cpy_src)) { |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | if (cpy_dst->ne[0] != D || cpy_dst->ne[1] != n_seqs || cpy_dst->ne[2] != n_written || !ggml_is_contiguous(cpy_dst)) { |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | prev.outputs.push_back(cpy_dst); |
| 354 | prev.fused.push_back(cur.node); |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | static bool fuse_ssm_conv_silu(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) { |
| 359 | GGML_UNUSED(sess); |
| 360 | if (prev.opcode != HTP_OP_SSM_CONV) return false; |
| 361 | |
| 362 | auto * kparams = prev.kparams<htp_ssm_conv_kernel_params>(); |
| 363 | if (kparams->silu) return false; |
| 364 | if (!htp_match_consumer(prev, cur, /*commutative=*/ false)) return false; |
| 365 | |
| 366 | const ggml_tensor * conv_dst = prev.dst(); |
| 367 | const ggml_tensor * dst = cur.dst(); |
| 368 | if (!ggml_are_same_layout(conv_dst, dst)) return false; |
| 369 | if (htp_tensors_overlap(dst, prev.src0()) || htp_tensors_overlap(dst, prev.src1())) return false; |
| 370 | |
| 371 | kparams->silu = 1; |
| 372 | prev.outputs[0] = dst; |
| 373 | prev.fused.push_back(cur.node); |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | static bool fuse_repeat_get_rows(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) { |
| 378 | if (prev.opcode != HTP_OP_REPEAT) return false; |
| 379 | if (!htp_match_consumer(prev, cur, /*commutative=*/ false)) return false; |
| 380 | |
| 381 | const ggml_tensor * repeat_out = prev.dst(); |
| 382 | const ggml_tensor * repeat_src0 = prev.src0(); |
| 383 | const ggml_tensor * gr_src1 = cur.src1(); |
| 384 | const ggml_tensor * gr_dst = cur.dst(); |
| 385 | |
| 386 | if (repeat_src0->ne[0] != repeat_out->ne[0] || repeat_src0->ne[1] != repeat_out->ne[1]) return false; |
| 387 | if (repeat_src0->type != repeat_out->type) return false; |
| 388 | |
| 389 | struct htp_get_rows_kernel_params kparams; |
| 390 | if (!htp_kparams_solve_repeat_get_rows(sess, repeat_src0, gr_src1, gr_dst, &kparams) || kparams.vtcm_size == 0) return false; |
| 391 | |
| 392 | prev.opcode = HTP_OP_GET_ROWS; |
| 393 | prev.inputs = { repeat_src0, gr_src1 }; |
| 394 | prev.outputs = { gr_dst }; |
| 395 | prev.fused.push_back(cur.node); |
| 396 | prev.set_kparams(kparams); |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | static bool fuse_unary_mul_glu(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) { |
| 401 | htp_op_code glu_op = HTP_OP_INVALID; |
| 402 | switch (prev.opcode) { |
| 403 | case HTP_OP_UNARY_SILU: glu_op = HTP_OP_GLU_SWIGLU; break; |
| 404 | case HTP_OP_UNARY_GELU: glu_op = HTP_OP_GLU_GEGLU; break; |
| 405 | case HTP_OP_UNARY_GELU_QUICK: glu_op = HTP_OP_GLU_GEGLU_QUICK; break; |
| 406 | case HTP_OP_UNARY_GELU_ERF: glu_op = HTP_OP_GLU_GEGLU_ERF; break; |
| 407 | case HTP_OP_UNARY_RELU: glu_op = HTP_OP_GLU_REGLU; break; |
| 408 | case HTP_OP_UNARY_SIGMOID: glu_op = HTP_OP_GLU_SIGMOID; break; |
| 409 | default: return false; |
| 410 | } |
| 411 | |
| 412 | const ggml_tensor * up = nullptr; |
| 413 | if (!htp_match_consumer(prev, cur, /*commutative=*/ true, &up)) return false; |
| 414 | |
| 415 | const ggml_tensor * gate = prev.src0(); |
| 416 | const ggml_tensor * dst = cur.dst(); |
| 417 | if (htp_inputs_overlap_unsafe(prev, dst, up)) return false; |
| 418 | |
| 419 | prev.opcode = glu_op; |
| 420 | prev.inputs = { gate, up }; |
| 421 | prev.outputs = { dst }; |
| 422 | if (!htp_kparams_solve(sess, prev)) return false; |
| 423 | |
| 424 | prev.fused.push_back(cur.node); |
| 425 | memset(prev.params, 0, sizeof(prev.params)); |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | static bool fuse_mul_mul_scalar(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) { |
| 430 | if (prev.opcode != HTP_OP_MUL || prev.inputs.size() != 2) return false; |
| 431 | |
| 432 | const auto * k0 = prev.kparams<htp_binary_kernel_params>(); |
| 433 | const auto * k1 = cur.kparams<htp_binary_kernel_params>(); |
| 434 | if (k0->kernel_type != HTP_BINARY_KERNEL_SCALAR || k1->kernel_type != HTP_BINARY_KERNEL_SCALAR) return false; |
| 435 | if (k0->src1_strided || k1->src1_strided) return false; |
| 436 | |
| 437 | const ggml_tensor * src2 = nullptr; |
| 438 | if (!htp_match_consumer(prev, cur, /*commutative=*/ false, &src2)) return false; |
| 439 | |
| 440 | const ggml_tensor * src1 = prev.src1(); |
| 441 | const ggml_tensor * dst = cur.dst(); |
| 442 | if (!ggml_are_same_shape(src1, src2) || !htp_same_shape_and_contiguity(prev.dst(), dst)) return false; |
| 443 | if (htp_inputs_overlap_unsafe(prev, dst, src2)) return false; |
| 444 | |
| 445 | const size_t vtcm_size = htp_session_vtcm_size(sess); |
| 446 | struct htp_binary_kernel_params kparams = *k0; |
| 447 | kparams.src2_size = kparams.src1_size; |
| 448 | kparams.rows_per_buffer = 0; |
| 449 | if (kparams.src1_size > 65536 || kparams.src1_size + kparams.src2_size > vtcm_size) return false; |
| 450 | |
| 451 | struct htp_binary_vtcm_layout L; |
| 452 | htp_binary_vtcm_layout_build(&L, &kparams, vtcm_size); |
| 453 | if (L.rows_per_buffer == 0 || L.total_bytes > vtcm_size) return false; |
| 454 | |
| 455 | kparams.rows_per_buffer = L.rows_per_buffer; |
| 456 | kparams.vtcm_size = L.total_bytes; |
| 457 | |
| 458 | prev.inputs.push_back(src2); |
| 459 | prev.outputs[0] = dst; |
| 460 | prev.fused.push_back(cur.node); |
| 461 | prev.set_kparams(kparams); |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | static bool fuse_binary_nx(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) { |
| 466 | if (prev.opcode != cur.opcode) return false; |
| 467 | |
| 468 | const uint32_t curr_n = (uint32_t) prev.inputs.size(); |
| 469 | if (curr_n < 2 || curr_n >= HTP_OP_MAX_INPUTS) return false; |
| 470 | |
| 471 | const auto * k0 = prev.kparams<htp_binary_kernel_params>(); |
| 472 | const auto * k1 = cur.kparams<htp_binary_kernel_params>(); |
| 473 | const bool k0_ok = (k0->kernel_type == HTP_BINARY_KERNEL_SAME_SHAPE) || |
| 474 | (k0->kernel_type == HTP_BINARY_KERNEL_CHUNKED && !k0->is_scalar); |
| 475 | const bool k1_ok = (k1->kernel_type == HTP_BINARY_KERNEL_SAME_SHAPE) || |
| 476 | (k1->kernel_type == HTP_BINARY_KERNEL_CHUNKED && !k1->is_scalar); |
| 477 | if (!k0_ok || !k1_ok) return false; |
| 478 | |
| 479 | const bool is_comm = (cur.opcode == HTP_OP_ADD || cur.opcode == HTP_OP_MUL); |
| 480 | const ggml_tensor * new_src = nullptr; |
| 481 | if (!htp_match_consumer(prev, cur, is_comm, &new_src)) return false; |
| 482 | |
| 483 | const ggml_tensor * src0 = prev.inputs[0]; |
| 484 | const ggml_tensor * dst = cur.dst(); |
| 485 | if (!ggml_are_same_shape(src0, prev.inputs[1]) || |
| 486 | !ggml_are_same_shape(src0, new_src) || |
| 487 | !htp_same_shape_and_contiguity(prev.dst(), dst)) { |
| 488 | return false; |
| 489 | } |
| 490 | if (htp_inputs_overlap_unsafe(prev, dst, new_src)) return false; |
| 491 | |
| 492 | const size_t vtcm_size = htp_session_vtcm_size(sess); |
| 493 | struct htp_binary_kernel_params kparams = *k0; |
| 494 | struct htp_binary_vtcm_layout L; |
| 495 | memset(&L, 0, sizeof(L)); |
| 496 | |
| 497 | const uint32_t n_srcs = curr_n + 1; |
| 498 | if (k0->kernel_type == HTP_BINARY_KERNEL_SAME_SHAPE || k1->kernel_type == HTP_BINARY_KERNEL_SAME_SHAPE) { |
| 499 | if (k0->kernel_type != HTP_BINARY_KERNEL_SAME_SHAPE) { |
| 500 | kparams = *k1; |
| 501 | } |
| 502 | kparams.n_srcs = (uint8_t) n_srcs; |
| 503 | kparams.rows_per_buffer = 0; |
| 504 | |
| 505 | auto check_split = [&](const ggml_tensor * t) { |
| 506 | if (src0->ne[2] > 1 && t->nb[2] != t->ne[1] * t->nb[1]) kparams.split_at_ne01 = 1; |
| 507 | if (src0->ne[3] > 1 && t->nb[3] != t->ne[2] * t->nb[2]) kparams.split_at_ne02 = 1; |
| 508 | }; |
| 509 | for (const auto * in : prev.inputs) check_split(in); |
| 510 | check_split(new_src); |
| 511 | check_split(dst); |
| 512 | |
| 513 | htp_binary_vtcm_layout_build(&L, &kparams, vtcm_size); |
| 514 | } |
| 515 | |
| 516 | if (L.rows_per_buffer == 0 || L.total_bytes == 0 || L.total_bytes > vtcm_size) { |
| 517 | bool all_contig = ggml_is_contiguous(new_src) && ggml_is_contiguous(dst); |
| 518 | for (const auto * in : prev.inputs) { |
| 519 | all_contig = all_contig && ggml_is_contiguous(in); |
| 520 | } |
| 521 | if (!all_contig) return false; |
| 522 | |
| 523 | const uint32_t total_elems = (uint32_t) ggml_nelements(src0); |
| 524 | const uint32_t elem_size = (uint32_t) ggml_type_size(src0->type); |
| 525 | const uint32_t max_threads = htp_session_n_threads(sess); |
| 526 | if (!htp_binary_solve_chunked(&L, &kparams, total_elems, elem_size, max_threads, n_srcs, false, vtcm_size)) { |
| 527 | return false; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | kparams.rows_per_buffer = L.rows_per_buffer; |
| 532 | kparams.vtcm_size = (uint32_t) L.total_bytes; |
| 533 | |
| 534 | prev.inputs.push_back(new_src); |
| 535 | prev.outputs[0] = dst; |
| 536 | prev.fused.push_back(cur.node); |
| 537 | prev.set_kparams(kparams); |
| 538 | return true; |
| 539 | } |
| 540 | |
| 541 | bool htp_fusion_can_stack(const struct ggml_tensor * a) { |
| 542 | if (!a || !a->src[0] || !a->src[1]) return false; |
| 543 | switch (a->op) { |
| 544 | case GGML_OP_MUL_MAT: |
| 545 | if (!htp_fusion_enabled(HTP_FUSE_MUL_MAT_NX)) return false; |
| 546 | if (htp_is_hadamard(a)) return false; |
| 547 | if (a->src[0]->ne[2] != 1 || a->src[0]->ne[3] != 1) return false; |
| 548 | return ggml_is_quantized(a->src[0]->type); |
| 549 | case GGML_OP_MUL_MAT_ID: |
| 550 | if (!htp_fusion_enabled(HTP_FUSE_MUL_MAT_ID_NX)) return false; |
| 551 | return a->src[2] != nullptr && ggml_is_quantized(a->src[0]->type); |
| 552 | default: |
| 553 | return false; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | bool htp_fusion_can_stack(const struct ggml_tensor * a, const struct ggml_tensor * b) { |
| 558 | if (!htp_fusion_can_stack(b) || a->op != b->op || a->src[1] != b->src[1]) { |
| 559 | return false; |
| 560 | } |
| 561 | if (a->op == GGML_OP_MUL_MAT_ID && a->src[2] != b->src[2]) { |
| 562 | return false; |
| 563 | } |
| 564 | const ggml_tensor * w0 = a->src[0]; |
| 565 | const ggml_tensor * w1 = b->src[0]; |
| 566 | return w0->type == w1->type && w0->ne[0] == w1->ne[0] && w0->ne[2] == w1->ne[2]; |
| 567 | } |
| 568 | |
| 569 | int htp_fusion_max(const struct ggml_cgraph * graph, int idx) { |
| 570 | const int n = graph->n_nodes; |
| 571 | const ggml_op op0 = graph->nodes[idx]->op; |
| 572 | if (op0 != GGML_OP_ADD && op0 != GGML_OP_NORM && op0 != GGML_OP_RMS_NORM) { |
| 573 | return 1; |
| 574 | } |
| 575 | |
| 576 | enum ggml_op ops[HTP_OP_MAX_FUSED]; |
| 577 | ops[0] = op0; |
| 578 | |
| 579 | int f = idx + 1; |
| 580 | while (f < n && f < idx + HTP_OP_MAX_FUSED) { |
| 581 | const ggml_op op = graph->nodes[f]->op; |
| 582 | if (op != GGML_OP_ADD && op != GGML_OP_MUL && op != GGML_OP_SCALE && |
| 583 | op != GGML_OP_NORM && op != GGML_OP_RMS_NORM) { |
| 584 | break; |
| 585 | } |
| 586 | ops[f - idx] = op; |
| 587 | f++; |
| 588 | } |
| 589 | |
| 590 | f -= idx; |
| 591 | for (; f > 1; f--) { |
| 592 | if (ggml_can_fuse(graph, idx, ops, f)) { |
| 593 | break; |
| 594 | } |
| 595 | } |
| 596 | return f; |
| 597 | } |
| 598 | |
| 599 | void htp_fusion_tag_graph(struct ggml_cgraph * graph) { |
| 600 | for (int i = 0; i < graph->n_nodes; i++) { |
| 601 | auto * extra = (htp_tensor_extra *) graph->nodes[i]->extra; |
| 602 | if (!extra) continue; |
| 603 | |
| 604 | extra->flags &= ~HTP_TENSOR_FUSEABLE; |
| 605 | |
| 606 | const ggml_op op = graph->nodes[i]->op; |
| 607 | if ((op == GGML_OP_RMS_NORM || op == GGML_OP_SCALE || op == GGML_OP_REPEAT || |
| 608 | op == GGML_OP_UNARY || op == GGML_OP_ADD || op == GGML_OP_SUB || |
| 609 | op == GGML_OP_MUL || op == GGML_OP_DIV || op == GGML_OP_MUL_MAT_ID || |
| 610 | op == GGML_OP_SSM_CONV || |
| 611 | (op == GGML_OP_MUL_MAT && !htp_is_hadamard(graph->nodes[i]))) && |
| 612 | ggml_node_has_n_uses(graph, i, 1)) { |
| 613 | extra->flags |= HTP_TENSOR_FUSEABLE; |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | bool htp_fusion_try_fuse(const struct ggml_hexagon_session * sess, struct htp_opnode & prev, const struct htp_opnode & cur) { |
| 619 | if (!opt_opfusion) return false; |
| 620 | |
| 621 | switch (cur.opcode) { |
| 622 | case HTP_OP_ADD: |
| 623 | return (htp_fusion_enabled(HTP_FUSE_ALLREDUCE_ADD) && fuse_allreduce_add(sess, prev, cur)) || |
| 624 | (htp_fusion_enabled(HTP_FUSE_MUL_MAT_ADD) && fuse_mul_mat_add(sess, prev, cur)) || |
| 625 | (htp_fusion_enabled(HTP_FUSE_BINARY_NX) && fuse_binary_nx(sess, prev, cur)); |
| 626 | case HTP_OP_MUL: |
| 627 | return (htp_fusion_enabled(HTP_FUSE_RMS_NORM_MUL) && fuse_rms_norm_mul(sess, prev, cur)) || |
| 628 | (htp_fusion_enabled(HTP_FUSE_UNARY_MUL_GLU) && fuse_unary_mul_glu(sess, prev, cur)) || |
| 629 | (htp_fusion_enabled(HTP_FUSE_MUL_MUL_SCALAR) && fuse_mul_mul_scalar(sess, prev, cur)) || |
| 630 | (htp_fusion_enabled(HTP_FUSE_BINARY_NX) && fuse_binary_nx(sess, prev, cur)); |
| 631 | case HTP_OP_SUB: |
| 632 | case HTP_OP_DIV: |
| 633 | return htp_fusion_enabled(HTP_FUSE_BINARY_NX) && fuse_binary_nx(sess, prev, cur); |
| 634 | case HTP_OP_SCALE: |
| 635 | return htp_fusion_enabled(HTP_FUSE_RMS_NORM_SCALE) && fuse_rms_norm_scale(sess, prev, cur); |
| 636 | case HTP_OP_UNARY_SILU: |
| 637 | return htp_fusion_enabled(HTP_FUSE_SSM_CONV_SILU) && fuse_ssm_conv_silu(sess, prev, cur); |
| 638 | case HTP_OP_GET_ROWS: |
| 639 | return htp_fusion_enabled(HTP_FUSE_REPEAT_GET_ROWS) && fuse_repeat_get_rows(sess, prev, cur); |
| 640 | case HTP_OP_CPY: |
| 641 | return htp_fusion_enabled(HTP_FUSE_GDN_CPY) && fuse_gdn_cpy(sess, prev, cur); |
| 642 | case HTP_OP_MUL_MAT: |
| 643 | return htp_fusion_enabled(HTP_FUSE_MUL_MAT_NX) && fuse_mul_mat_nx(sess, prev, cur); |
| 644 | case HTP_OP_MUL_MAT_ID: |
| 645 | return htp_fusion_enabled(HTP_FUSE_MUL_MAT_ID_NX) && fuse_mul_mat_id_nx(sess, prev, cur); |
| 646 | default: |
| 647 | return false; |
| 648 | } |
| 649 | } |