Commit e339c7d31 for llama.cpp
commit e339c7d31d5c8eeba9d874277289071a4600c6f3
Author: Todor Boinovski <todorb@qti.qualcomm.com>
Date: Fri Dec 4 15:08:14 2026 -0800
hexagon: add support for DSV4_HC_COMB (#33565)
* hexagon: add DSV4_HC_COMB op
* hexagon: speed up DSV4_HC_PRE gated sigmoid and fix COMB mdev split
* hexagon: fix DSV4_HC_COMB DMA-only data path and zero in-loop spills
* hexagon: convert new DSV4 ops to use dma_pipeline
* hexagon: dsv4 support for mixed f16/f32 and f16 lowering
* hexagon: dsv4 minor fixes for sigmoid accuracy
---------
Co-authored-by: Max Krasnyansky <maxk@qti.qualcomm.com>
diff --git a/ggml/src/ggml-hexagon/htp-kparams.cpp b/ggml/src/ggml-hexagon/htp-kparams.cpp
index 84e1075f3..3c7961974 100644
--- a/ggml/src/ggml-hexagon/htp-kparams.cpp
+++ b/ggml/src/ggml-hexagon/htp-kparams.cpp
@@ -57,6 +57,7 @@ htp_op_code htp_op_remap(const struct ggml_tensor * t) {
case GGML_OP_SOLVE_TRI: return HTP_OP_SOLVE_TRI;
case GGML_OP_DSV4_HC_PRE: return HTP_OP_DSV4_HC_PRE;
case GGML_OP_DSV4_HC_POST: return HTP_OP_DSV4_HC_POST;
+ case GGML_OP_DSV4_HC_COMB: return HTP_OP_DSV4_HC_COMB;
case GGML_OP_TRI: return HTP_OP_TRI;
case GGML_OP_PAD: return HTP_OP_PAD;
case GGML_OP_IM2COL: return HTP_OP_IM2COL;
@@ -5846,6 +5847,85 @@ static inline bool htp_kparams_solve_dsv4_hc_post(
return htp_kparams_solve_dsv4_hc_post_impl(sess, op->src[0], op->src[1], op->src[2], op->src[3], op, kparams);
}
+static bool htp_kparams_solve_dsv4_hc_comb_impl(
+ const struct ggml_hexagon_session * sess,
+ const struct ggml_tensor * mixes,
+ const struct ggml_tensor * scale,
+ const struct ggml_tensor * base,
+ const struct ggml_tensor * dst,
+ struct htp_dsv4_hc_comb_kernel_params * kparams
+) {
+ memset(kparams, 0, sizeof(*kparams));
+
+ const auto is_f32_or_f16 = [](const ggml_tensor * t) {
+ return t->type == GGML_TYPE_F32 || t->type == GGML_TYPE_F16;
+ };
+ if (!is_f32_or_f16(mixes) || !is_f32_or_f16(scale) || !is_f32_or_f16(base) || !is_f32_or_f16(dst)) {
+ return false;
+ }
+ if (mixes->ne[0] != 24 || mixes->ne[1] < 0 || mixes->ne[1] > (int64_t) (UINT32_MAX - HTP_DSV4_HC_COMB_LANES) ||
+ mixes->ne[2] != 1 || mixes->ne[3] != 1) {
+ return false;
+ }
+ if (scale->ne[0] < 3 || ggml_nrows(scale) != 1) {
+ return false;
+ }
+ if (base->ne[0] != 24 || ggml_nrows(base) != 1 || !ggml_is_contiguous(base)) {
+ return false;
+ }
+ if (dst->ne[0] != 4 || dst->ne[1] != 4 || dst->ne[2] != mixes->ne[1] || dst->ne[3] != 1) {
+ return false;
+ }
+ const size_t mix_es = ggml_type_size(mixes->type);
+ const size_t dst_es = ggml_type_size(dst->type);
+ if (!ggml_is_contiguous_to_2(dst)) {
+ return false;
+ }
+ if (mixes->ne[1] > 1 && dst->nb[2] < 16 * dst_es) {
+ return false;
+ }
+
+ if (ggml_get_op_params_i32(dst, 1) <= 0) {
+ return false;
+ }
+
+ const uint32_t n_tokens = (uint32_t) mixes->ne[1];
+ if (n_tokens == 0) {
+ return true;
+ }
+
+ if (ggml_is_contiguous_rows(mixes)) {
+ kparams->in_layout = HTP_DSV4_HC_COMB_IN_ROWS;
+ } else if (mixes->nb[1] == mix_es) {
+ kparams->in_layout = HTP_DSV4_HC_COMB_IN_LANES;
+ } else {
+ kparams->in_layout = HTP_DSV4_HC_COMB_IN_GATHER;
+ }
+
+ const uint32_t n_blocks = (n_tokens + HTP_DSV4_HC_COMB_LANES - 1) / HTP_DSV4_HC_COMB_LANES;
+ const uint32_t vtcm_hvx = (uint32_t) (htp_session_vtcm_size(sess) / HTP_DSV4_HC_COMB_VTCM_PER_HVX);
+ const uint32_t n_hvx = (std::min)((std::min)(htp_session_n_hvx(sess), n_blocks), vtcm_hvx);
+ if (n_hvx == 0) {
+ return false;
+ }
+
+ kparams->n_hvx = n_hvx;
+ kparams->n_blocks = n_blocks;
+ kparams->blocks_per_hvx = (n_blocks + n_hvx - 1) / n_hvx;
+ kparams->vtcm_size = HTP_DSV4_HC_COMB_VTCM_PER_HVX * n_hvx;
+ kparams->div_n_hvx = init_fastdiv_values(n_hvx);
+
+ return true;
+}
+
+static inline bool htp_kparams_solve_dsv4_hc_comb(
+ const struct ggml_hexagon_session * sess,
+ const struct ggml_tensor * op,
+ struct htp_dsv4_hc_comb_kernel_params * kparams
+) {
+ return htp_kparams_solve_dsv4_hc_comb_impl(sess, op->src[0], op->src[1], op->src[2], op, kparams);
+}
+
static bool htp_kparams_solve_lightning_indexer_impl(
const struct ggml_hexagon_session * sess,
const struct ggml_tensor * q,
@@ -6332,6 +6412,8 @@ bool htp_kparams_solve(
return commit(htp_kparams_solve_dsv4_hc_pre_impl(sess, src0, src1, dst, tmp.kparams<htp_dsv4_hc_pre_kernel_params>()));
case HTP_OP_DSV4_HC_POST:
return commit(htp_kparams_solve_dsv4_hc_post_impl(sess, src0, src1, src2, src3, dst, tmp.kparams<htp_dsv4_hc_post_kernel_params>()));
+ case HTP_OP_DSV4_HC_COMB:
+ return commit(htp_kparams_solve_dsv4_hc_comb_impl(sess, src0, src1, src2, dst, tmp.kparams<htp_dsv4_hc_comb_kernel_params>()));
case HTP_OP_LIGHTNING_INDEXER:
return commit(htp_kparams_solve_lightning_indexer_impl(sess, src0, src1, src2, src3, dst, tmp.kparams<htp_lid_kernel_params>()));
case HTP_OP_GET_ROWS:
@@ -6493,6 +6575,9 @@ bool htp_kparams_solve(
case HTP_OP_DSV4_HC_POST:
return htp_kparams_invoke<htp_kparams_solve_dsv4_hc_post>(sess, op, kparams);
+ case HTP_OP_DSV4_HC_COMB:
+ return htp_kparams_invoke<htp_kparams_solve_dsv4_hc_comb>(sess, op, kparams);
+
default:
return false;
}
diff --git a/ggml/src/ggml-hexagon/htp-opnode.cpp b/ggml/src/ggml-hexagon/htp-opnode.cpp
index 761bf65e9..f41c4e0f4 100644
--- a/ggml/src/ggml-hexagon/htp-opnode.cpp
+++ b/ggml/src/ggml-hexagon/htp-opnode.cpp
@@ -248,6 +248,12 @@ static std::string format_kernel_params(const htp_opnode & node) {
const char * ktype_str = kparams->kernel_type == HTP_DSV4_HC_POST_KERNEL_COMB ? "comb" : "identity";
snprintf(str, max_size, "%s nhvx %u vtcm %u", ktype_str,
(unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
+ } else if (node.opcode == HTP_OP_DSV4_HC_COMB) {
+ const auto * kparams = node.kparams<htp_dsv4_hc_comb_kernel_params>();
+ static const char * layout_str[] = { "rows", "lanes", "gather" };
+ const char * in_str = kparams->in_layout < 3 ? layout_str[kparams->in_layout] : "?";
+ snprintf(str, max_size, "in %s nhvx %u vtcm %u", in_str,
+ (unsigned int) kparams->n_hvx, (unsigned int) kparams->vtcm_size);
} else if (node.opcode == HTP_OP_LIGHTNING_INDEXER) {
const auto * kparams = node.kparams<htp_lid_kernel_params>();
snprintf(str, max_size, "%s nhvx %u tok %u bc %u vtcm %u",
diff --git a/ggml/src/ggml-hexagon/htp/dsv4-hc-ops.c b/ggml/src/ggml-hexagon/htp/dsv4-hc-ops.c
index 4190ae7f9..9dc8a7b57 100644
--- a/ggml/src/ggml-hexagon/htp/dsv4-hc-ops.c
+++ b/ggml/src/ggml-hexagon/htp/dsv4-hc-ops.c
@@ -5,6 +5,7 @@
#include <math.h>
#include "dma-queue.h"
+#include "dma-pipeline.h"
#include "hex-common.h"
#include "htp-trace.h"
#include "htp-ctx.h"
@@ -18,34 +19,44 @@
// hvx_vec_fast_sigmoid_f32_guard_2it with fewer live constants and predicates
static inline HVX_Vector dsv4_hc_sigmoid_f32(HVX_Vector v, HVX_Vector max_exp, HVX_Vector min_exp) {
+ const HVX_Vector zero = Q6_V_vzero();
+ const HVX_Vector rnd = hvx_vec_splat_f32(12582912.0f); // 1.5 * 2^23
+ const HVX_Vector c2_qf = Q6_Vqf32_vadd_VsfVsf(Q6_V_vsplat_R(FAST_SIGMOID_C2), zero);
+
v = Q6_Vsf_vmin_VsfVsf(Q6_Vsf_vmax_VsfVsf(v, min_exp), max_exp);
v = Q6_Vqf32_vmpy_VsfVsf(v, hvx_vec_splat_f32(0.5f * 1.442695022f));
- HVX_Vector in_int = Q6_Vw_equals_Vsf(Q6_Vsf_equals_Vqf32(v));
- HVX_Vector x = Q6_Vqf32_vsub_Vqf32Vsf(v, Q6_Vsf_equals_Vw(in_int));
- HVX_Vector xx = Q6_Vqf32_vmpy_Vqf32Vqf32(x, x);
+ // n = round(v) via magic add: int from the low mantissa bits, float as (t - rnd)
+ HVX_Vector t = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(v, rnd));
+ HVX_Vector in_int = Q6_Vw_vsub_VwVw(t, rnd);
+ HVX_Vector x_sf = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vqf32(v, Q6_Vqf32_vsub_VsfVsf(t, rnd)));
+ HVX_Vector xx = Q6_Vqf32_vmpy_VsfVsf(x_sf, x_sf);
- HVX_Vector v1 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(xx), Q6_V_vsplat_R(FAST_SIGMOID_C2));
+ HVX_Vector v1 = Q6_Vqf32_vmpy_Vqf32Vqf32(xx, c2_qf);
v1 = Q6_Vqf32_vadd_Vqf32Vsf(v1, Q6_V_vsplat_R(FAST_SIGMOID_LOG2F));
- HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(x), Q6_V_vsplat_R(FAST_SIGMOID_C1));
+ HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(x_sf, Q6_V_vsplat_R(FAST_SIGMOID_C1));
v2 = Q6_Vqf32_vmpy_Vqf32Vqf32(v2, xx);
- v2 = Q6_Vqf32_vadd_Vqf32Vqf32(v2, x);
+ v2 = Q6_Vqf32_vadd_Vqf32Vsf(v2, x_sf);
HVX_Vector v3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(v2, v1));
v3 = Q6_Vw_vaslacc_VwVwR(v3, in_int, 24);
- HVX_Vector v4 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vqf32(v2, v1));
- HVX_Vector v5 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_VsfVsf(v3, v4));
-
- // Newton-Raphson with 2 iterations
- HVX_Vector r = Q6_Vw_vsub_VwVw(Q6_V_vsplat_R(0x7EEEEBB3), v5);
- for (int i = 0; i < 2; ++i) {
- const HVX_Vector rv = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(r, v5));
- r = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vqf32(Q6_Vqf32_vadd_VsfVsf(r, r), Q6_Vqf32_vmpy_VsfVsf(r, rv)));
- }
-
- return Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(v3, r));
+ // dn = v4 - v3 = -(v3 - v4) < 0; result = v3 / -dn
+ HVX_Vector dn = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vsf(Q6_Vqf32_vsub_Vqf32Vqf32(v2, v1), v3));
+
+ // seed r0 = 0x7EEEEBB3 - |dn|, e = 1 - r0 * |dn|; two Newton-Raphson steps in closed form:
+ // 1/|dn| ~= r0 * (1 + e) * (1 + e^2), with v3 folded into the (1 + e) branch
+ const HVX_Vector one = hvx_vec_splat_f32(1.0f);
+ const HVX_Vector two = hvx_vec_splat_f32(2.0f);
+ HVX_Vector r = Q6_Vw_vsub_VwVw(Q6_V_vsplat_R(0xFEEEEBB3), dn);
+ HVX_Vector rd = Q6_Vqf32_vmpy_VsfVsf(r, dn); // -r0 * |dn|
+ HVX_Vector w = Q6_Vqf32_vmpy_VsfVsf(v3, r);
+ HVX_Vector e_sf = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(rd, one));
+ HVX_Vector p = Q6_Vqf32_vmpy_Vqf32Vqf32(w, Q6_Vqf32_vadd_Vqf32Vsf(rd, two));
+ HVX_Vector q = Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_VsfVsf(e_sf, e_sf), one);
+
+ return Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_Vqf32Vqf32(p, q));
}
typedef void (*dsv4_hc_unpack_fn_t)(uint8_t * p);
@@ -333,7 +344,7 @@ static __attribute__((noinline)) void dsv4_hc_pre_gated_block_##SUFFIX(
uint8_t * d_spad) { \
const uint32_t row_stride = dctx->dma_row_stride; \
const HVX_Vector scale_v = hvx_vec_splat_f32(dctx->scale); \
- const HVX_Vector max_exp = hvx_vec_splat_f32(87.0f); \
+ const HVX_Vector max_exp = hvx_vec_splat_f32(86.0f); \
const HVX_Vector min_exp = hvx_vec_splat_f32(-87.0f); \
const uint32_t count = block->count; \
uint32_t c = 0; \
@@ -1119,3 +1130,605 @@ int op_dsv4_hc_post(struct htp_ops_context * octx) {
work_queue_run(octx->ctx->hvx_queue, worker, &dctx, n_hvx);
return HTP_STATUS_OK;
}
+
+// ---------------------------------------------------------------------------
+// DSV4_HC_COMB
+// ---------------------------------------------------------------------------
+
+struct dsv4_hc_comb_context;
+
+typedef void (*dsv4_hc_comb_block_fn_t)(uint8_t * src, uint8_t * dst, const struct dsv4_hc_comb_context * dctx);
+
+struct dsv4_hc_comb_context {
+ struct htp_ops_context * octx;
+ dma_pipe_push_in_fn push_in;
+ dma_pipe_compute_io_fn compute;
+ dma_pipe_push_out_fn push_out;
+ dsv4_hc_comb_block_fn_t block_fn;
+ uint32_t mix_es;
+ uint32_t dst_row_size;
+ uint32_t token_start;
+ uint32_t n_tokens;
+ uint32_t n_blocks;
+ uint32_t blocks_per_hvx;
+ float eps;
+ int32_t n_iter;
+ float scale_comb;
+ float base_comb[16];
+ uint8_t * vtcm;
+};
+
+struct dsv4_hc_comb_pipe_state {
+ const struct dsv4_hc_comb_context * dctx;
+ dma_addr_t mixes_data;
+ dma_addr_t dst_data;
+ size_t mixes_nb0;
+ size_t mixes_nb1;
+ size_t dst_nb2;
+ uint32_t blk;
+ uint32_t blk_end;
+ uint32_t token_end;
+};
+
+// 1/v with v and result in qf32; only the seed needs sf.
+// Newton step r = r*(2 + (-v)*r) uses only qf32 ops present on all arches.
+static inline HVX_Vector dsv4_hc_comb_inverse_qf32(HVX_Vector v, HVX_Vector two) {
+ const HVX_Vector zero = Q6_V_vzero();
+ const HVX_Vector v_sf = Q6_Vsf_equals_Vqf32(v);
+ const HVX_Vector nv = Q6_Vqf32_vsub_VsfVsf(zero, v_sf);
+
+ HVX_Vector r = Q6_Vqf32_vadd_VsfVsf(Q6_Vw_vsub_VwVw(Q6_V_vsplat_R(0x7EEEEBB3), v_sf), zero);
+ #pragma unroll
+ for (int i = 0; i < 3; ++i) {
+ r = Q6_Vqf32_vmpy_Vqf32Vqf32(r, Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_Vqf32Vqf32(nv, r), two));
+ }
+#if __HVX_ARCH__ < 79
+ r = Q6_Vqf32_vadd_Vqf32Vsf(r, zero);
+#endif
+ return r;
+}
+
+// Lane per token. c[idst + 4*isrc] holds one comb entry for 32 tokens, in qf32.
+static inline void dsv4_hc_comb_norm_rows_hvx(HVX_Vector c[16], HVX_Vector eps_v, HVX_Vector two) {
+ #pragma unroll
+ for (int isrc = 0; isrc < 4; ++isrc) {
+ HVX_Vector * r = c + 4*isrc;
+ const HVX_Vector sum = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(r[0], eps_v), Q6_Vqf32_vadd_Vqf32Vqf32(r[1], r[2]));
+ const HVX_Vector inv = dsv4_hc_comb_inverse_qf32(Q6_Vqf32_vadd_Vqf32Vqf32(sum, r[3]), two);
+ #pragma unroll
+ for (int j = 0; j < 4; ++j) {
+ r[j] = Q6_Vqf32_vmpy_Vqf32Vqf32(r[j], inv);
+ }
+ }
+}
+
+static inline void dsv4_hc_comb_norm_cols_hvx(HVX_Vector c[16], HVX_Vector eps_v, HVX_Vector two) {
+ #pragma unroll
+ for (int idst = 0; idst < 4; ++idst) {
+ const HVX_Vector sum = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(c[idst], eps_v), Q6_Vqf32_vadd_Vqf32Vqf32(c[idst + 4], c[idst + 8]));
+ const HVX_Vector inv = dsv4_hc_comb_inverse_qf32(Q6_Vqf32_vadd_Vqf32Vqf32(sum, c[idst + 12]), two);
+ #pragma unroll
+ for (int j = 0; j < 4; ++j) {
+ c[idst + 4*j] = Q6_Vqf32_vmpy_Vqf32Vqf32(c[idst + 4*j], inv);
+ }
+ }
+}
+
+// In place on w[16]: raw mixes (sf) in, softmax(mixes*scale + base) + eps (qf32) out.
+// Three rolled passes over VTCM keep one exp in flight, so its constants stay in registers.
+// WARNING: do not unroll the row loops. A fully unrolled variant (~9.7 KB stack frame) produced -inf on device (suspected overflow of the 16 KB WORK_QUEUE_STACK_SIZE in htp/main.c). Re-test on device if this changes.
+static __attribute__((noinline)) void dsv4_hc_comb_softmax_hvx(HVX_Vector * w, const struct dsv4_hc_comb_context * dctx) {
+ const HVX_Vector scale_v = hvx_vec_splat_f32(dctx->scale_comb);
+
+ #pragma unroll(1)
+ for (int isrc = 0; isrc < 4; ++isrc) {
+ HVX_Vector * r = w + 4*isrc;
+ HVX_Vector x[4];
+ #pragma unroll
+ for (int j = 0; j < 4; ++j) {
+ x[j] = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(r[j], scale_v), hvx_vec_splat_f32(dctx->base_comb[4*isrc + j]));
+ }
+ const HVX_Vector m = Q6_Vsf_vmax_VsfVsf(Q6_Vsf_vmax_VsfVsf(x[0], x[1]), Q6_Vsf_vmax_VsfVsf(x[2], x[3]));
+ #pragma unroll
+ for (int j = 0; j < 4; ++j) {
+ r[j] = hvx_vec_sub_f32_f32(x[j], m);
+ }
+ }
+
+ #pragma unroll(1)
+ for (int i = 0; i < 16; ++i) {
+ w[i] = hvx_vec_exp_f32(w[i]);
+ }
+
+ const HVX_Vector eps_v = hvx_vec_splat_f32(dctx->eps);
+
+ #pragma unroll(1)
+ for (int isrc = 0; isrc < 4; ++isrc) {
+ HVX_Vector * r = w + 4*isrc;
+ const HVX_Vector inv = hvx_vec_inverse_f32(hvx_vec_add_f32_f32(hvx_vec_add_f32_f32(r[0], r[1]), hvx_vec_add_f32_f32(r[2], r[3])));
+ #pragma unroll
+ for (int j = 0; j < 4; ++j) {
+ r[j] = Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_VsfVsf(r[j], inv), eps_v);
+ }
+ }
+}
+
+// in place on w[16]: qf32 in, sf out
+static __attribute__((noinline)) void dsv4_hc_comb_sinkhorn_hvx(HVX_Vector * w, float eps, int32_t n_iter) {
+ const HVX_Vector eps_v = hvx_vec_splat_f32(eps);
+ const HVX_Vector two = hvx_vec_splat_f32(2.0f);
+
+ HVX_Vector c[16];
+ #pragma unroll
+ for (int i = 0; i < 16; ++i) {
+ c[i] = w[i];
+ }
+
+ dsv4_hc_comb_norm_cols_hvx(c, eps_v, two);
+ for (int32_t it = 1; it < n_iter; ++it) {
+ dsv4_hc_comb_norm_rows_hvx(c, eps_v, two);
+ dsv4_hc_comb_norm_cols_hvx(c, eps_v, two);
+ }
+
+ #pragma unroll
+ for (int i = 0; i < 16; ++i) {
+ w[i] = Q6_Vsf_equals_Vqf32(c[i]);
+ }
+}
+
+// src: 32 rows of 16 floats (one row per token) -> c[k] lane t = src[t][k]
+static inline void dsv4_hc_comb_load_transpose(HVX_Vector c[16], const uint8_t * src) {
+ #pragma unroll
+ for (int i = 0; i < 16; ++i) {
+ c[i] = ((const HVX_Vector *) src)[i];
+ }
+ #pragma unroll
+ for (int s = 1; s < 16; s *= 2) {
+ #pragma unroll
+ for (int g = 0; g < 16; g += 2*s) {
+ #pragma unroll
+ for (int i = g; i < g + s; ++i) {
+ const HVX_VectorPair p = Q6_W_vdeal_VVR(c[i + s], c[i], -4);
+ c[i] = Q6_V_lo_W(p);
+ c[i + s] = Q6_V_hi_W(p);
+ }
+ }
+ }
+}
+
+// 32 rows of 16 fp16 (8 vectors) -> 32 rows of 16 floats (16 vectors) in place
+static inline void dsv4_hc_comb_unpack_rows_f16(uint8_t * src) {
+ HVX_Vector * w = (HVX_Vector *) src;
+ for (int i = 7; i >= 0; --i) {
+ const HVX_VectorPair p = hvx_vec_f16_to_f32(w[i]);
+ w[2*i + 1] = Q6_V_hi_W(p);
+ w[2*i] = Q6_V_lo_W(p);
+ }
+}
+
+// 16 vectors of 32 fp16 (lower 64 B each) -> 16 vectors of 32 floats in place
+static inline void dsv4_hc_comb_unpack_lanes_f16(uint8_t * src) {
+ HVX_Vector * w = (HVX_Vector *) src;
+ for (int i = 0; i < 16; ++i) {
+ w[i] = Q6_V_lo_W(hvx_vec_f16_to_f32(w[i]));
+ }
+}
+
+// inverse of dsv4_hc_comb_load_transpose
+static inline void dsv4_hc_comb_store_transpose_butterfly(HVX_Vector c[16]) {
+ #pragma unroll
+ for (int s = 8; s > 0; s /= 2) {
+ #pragma unroll
+ for (int g = 0; g < 16; g += 2*s) {
+ #pragma unroll
+ for (int i = g; i < g + s; ++i) {
+ const HVX_VectorPair p = Q6_W_vshuff_VVR(c[i + s], c[i], -4);
+ c[i] = Q6_V_lo_W(p);
+ c[i + s] = Q6_V_hi_W(p);
+ }
+ }
+ }
+}
+
+static inline void dsv4_hc_comb_store_transpose_f32(uint8_t * dst, HVX_Vector c[16]) {
+ dsv4_hc_comb_store_transpose_butterfly(c);
+ #pragma unroll
+ for (int i = 0; i < 16; ++i) {
+ ((HVX_Vector *) dst)[i] = c[i];
+ }
+}
+
+static inline void dsv4_hc_comb_store_transpose_f16(uint8_t * dst, HVX_Vector c[16]) {
+ dsv4_hc_comb_store_transpose_butterfly(c);
+ #pragma unroll
+ for (int i = 0; i < 8; ++i) {
+ ((HVX_Vector *) dst)[i] = hvx_vec_f32_to_f16(c[2*i], c[2*i + 1]);
+ }
+}
+
+// src: 16 entry vectors of 32 tokens in VTCM, also used as scratch; dst: 32 token rows
+static inline void dsv4_hc_comb_block_core_f32(uint8_t * src, uint8_t * dst, const struct dsv4_hc_comb_context * dctx) {
+ HVX_Vector * w = (HVX_Vector *) src;
+
+ dsv4_hc_comb_softmax_hvx(w, dctx);
+ dsv4_hc_comb_sinkhorn_hvx(w, dctx->eps, dctx->n_iter);
+
+ HVX_Vector c[16];
+ #pragma unroll
+ for (int i = 0; i < 16; ++i) {
+ c[i] = w[i];
+ }
+ dsv4_hc_comb_store_transpose_f32(dst, c);
+}
+
+static inline void dsv4_hc_comb_block_core_f16(uint8_t * src, uint8_t * dst, const struct dsv4_hc_comb_context * dctx) {
+ HVX_Vector * w = (HVX_Vector *) src;
+
+ dsv4_hc_comb_softmax_hvx(w, dctx);
+ dsv4_hc_comb_sinkhorn_hvx(w, dctx->eps, dctx->n_iter);
+
+ HVX_Vector c[16];
+ #pragma unroll
+ for (int i = 0; i < 16; ++i) {
+ c[i] = w[i];
+ }
+ dsv4_hc_comb_store_transpose_f16(dst, c);
+}
+
+static void dsv4_hc_comb_block_lanes_f32_f32(uint8_t * src, uint8_t * dst, const struct dsv4_hc_comb_context * dctx) {
+ dsv4_hc_comb_block_core_f32(src, dst, dctx);
+}
+
+static void dsv4_hc_comb_block_lanes_f32_f16(uint8_t * src, uint8_t * dst, const struct dsv4_hc_comb_context * dctx) {
+ dsv4_hc_comb_block_core_f16(src, dst, dctx);
+}
+
+static void dsv4_hc_comb_block_lanes_f16_f32(uint8_t * src, uint8_t * dst, const struct dsv4_hc_comb_context * dctx) {
+ dsv4_hc_comb_unpack_lanes_f16(src);
+ dsv4_hc_comb_block_core_f32(src, dst, dctx);
+}
+
+static void dsv4_hc_comb_block_lanes_f16_f16(uint8_t * src, uint8_t * dst, const struct dsv4_hc_comb_context * dctx) {
+ dsv4_hc_comb_unpack_lanes_f16(src);
+ dsv4_hc_comb_block_core_f16(src, dst, dctx);
+}
+
+static inline void dsv4_hc_comb_transpose_rows_inplace(uint8_t * src) {
+ HVX_Vector c[16];
+ dsv4_hc_comb_load_transpose(c, src);
+ #pragma unroll
+ for (int i = 0; i < 16; ++i) {
+ ((HVX_Vector *) src)[i] = c[i];
+ }
+}
+
+static void dsv4_hc_comb_block_rows_f32_f32(uint8_t * src, uint8_t * dst, const struct dsv4_hc_comb_context * dctx) {
+ dsv4_hc_comb_transpose_rows_inplace(src);
+ dsv4_hc_comb_block_core_f32(src, dst, dctx);
+}
+
+static void dsv4_hc_comb_block_rows_f32_f16(uint8_t * src, uint8_t * dst, const struct dsv4_hc_comb_context * dctx) {
+ dsv4_hc_comb_transpose_rows_inplace(src);
+ dsv4_hc_comb_block_core_f16(src, dst, dctx);
+}
+
+static void dsv4_hc_comb_block_rows_f16_f32(uint8_t * src, uint8_t * dst, const struct dsv4_hc_comb_context * dctx) {
+ dsv4_hc_comb_unpack_rows_f16(src);
+ dsv4_hc_comb_transpose_rows_inplace(src);
+ dsv4_hc_comb_block_core_f32(src, dst, dctx);
+}
+
+static void dsv4_hc_comb_block_rows_f16_f16(uint8_t * src, uint8_t * dst, const struct dsv4_hc_comb_context * dctx) {
+ dsv4_hc_comb_unpack_rows_f16(src);
+ dsv4_hc_comb_transpose_rows_inplace(src);
+ dsv4_hc_comb_block_core_f16(src, dst, dctx);
+}
+
+#define DEFINE_DSV4_HC_COMB_COMPUTE(NAME) \
+static void dsv4_hc_comb_compute_##NAME(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in_spad) { \
+ (void) task; \
+ const struct dsv4_hc_comb_pipe_state * s = (const struct dsv4_hc_comb_pipe_state *) ctx; \
+ dsv4_hc_comb_block_##NAME((uint8_t *) in_spad, (uint8_t *) out_spad, s->dctx); \
+}
+
+DEFINE_DSV4_HC_COMB_COMPUTE(lanes_f32_f32)
+DEFINE_DSV4_HC_COMB_COMPUTE(lanes_f32_f16)
+DEFINE_DSV4_HC_COMB_COMPUTE(lanes_f16_f32)
+DEFINE_DSV4_HC_COMB_COMPUTE(lanes_f16_f16)
+DEFINE_DSV4_HC_COMB_COMPUTE(rows_f32_f32)
+DEFINE_DSV4_HC_COMB_COMPUTE(rows_f32_f16)
+DEFINE_DSV4_HC_COMB_COMPUTE(rows_f16_f32)
+DEFINE_DSV4_HC_COMB_COMPUTE(rows_f16_f16)
+
+static bool dsv4_hc_comb_next_task(void * ctx, struct dma_pipe_task * task) {
+ struct dsv4_hc_comb_pipe_state * s = (struct dsv4_hc_comb_pipe_state *) ctx;
+ if (s->blk >= s->blk_end) {
+ return false;
+ }
+
+ const uint32_t blk = s->blk++;
+ const uint32_t t0 = s->dctx->token_start + blk*HTP_DSV4_HC_COMB_LANES;
+
+ task->row = blk;
+ task->off = t0;
+ task->len = hex_smin(HTP_DSV4_HC_COMB_LANES, s->token_end - t0);
+ task->aux = 0;
+ return true;
+}
+
+// mixes nb0 == elem_size: one 16-elem row per token
+static void dsv4_hc_comb_push_in_rows_f32(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in_spad) {
+ const struct dsv4_hc_comb_pipe_state * s = (const struct dsv4_hc_comb_pipe_state *) ctx;
+ const size_t row_size = 16*sizeof(float);
+ dma_queue_push(dma_q, dma_make_data(in_spad, s->mixes_data + task->off*s->mixes_nb1 + 8*sizeof(float)),
+ row_size, s->mixes_nb1, row_size, task->len);
+}
+
+static void dsv4_hc_comb_push_in_rows_f16(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in_spad) {
+ const struct dsv4_hc_comb_pipe_state * s = (const struct dsv4_hc_comb_pipe_state *) ctx;
+ const size_t row_size = 16*sizeof(__fp16);
+ dma_queue_push(dma_q, dma_make_data(in_spad, s->mixes_data + task->off*s->mixes_nb1 + 8*sizeof(__fp16)),
+ row_size, s->mixes_nb1, row_size, task->len);
+}
+
+// mixes nb1 == elem_size: tokens are contiguous per entry, DMA straight into lanes
+static void dsv4_hc_comb_push_in_lanes_f32(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in_spad) {
+ const struct dsv4_hc_comb_pipe_state * s = (const struct dsv4_hc_comb_pipe_state *) ctx;
+ dma_queue_push(dma_q, dma_make_data(in_spad, s->mixes_data + 8*s->mixes_nb0 + task->off*sizeof(float)),
+ sizeof(HVX_Vector), s->mixes_nb0, task->len*sizeof(float), 16);
+}
+
+static void dsv4_hc_comb_push_in_lanes_f16(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in_spad) {
+ const struct dsv4_hc_comb_pipe_state * s = (const struct dsv4_hc_comb_pipe_state *) ctx;
+ dma_queue_push(dma_q, dma_make_data(in_spad, s->mixes_data + 8*s->mixes_nb0 + task->off*sizeof(__fp16)),
+ sizeof(HVX_Vector), s->mixes_nb0, task->len*sizeof(__fp16), 16);
+}
+
+static void dsv4_hc_comb_push_out_f32(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * out_spad) {
+ const struct dsv4_hc_comb_pipe_state * s = (const struct dsv4_hc_comb_pipe_state *) ctx;
+ const size_t row_size = 16*sizeof(float);
+ dma_queue_push(dma_q, dma_make_data(s->dst_data + task->off*s->dst_nb2, out_spad),
+ s->dst_nb2, row_size, row_size, task->len);
+}
+
+static void dsv4_hc_comb_push_out_f16(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * out_spad) {
+ const struct dsv4_hc_comb_pipe_state * s = (const struct dsv4_hc_comb_pipe_state *) ctx;
+ const size_t row_size = 16*sizeof(__fp16);
+ dma_queue_push(dma_q, dma_make_data(s->dst_data + task->off*s->dst_nb2, out_spad),
+ s->dst_nb2, row_size, row_size, task->len);
+}
+
+static void dsv4_hc_comb_thread(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
+ const struct dsv4_hc_comb_context * dctx = (const struct dsv4_hc_comb_context *) data;
+
+ const struct htp_tensor * mixes = dctx->octx->src[0];
+ const struct htp_tensor * dst = dctx->octx->dst;
+
+ const uint32_t blk0 = ith * dctx->blocks_per_hvx;
+ const uint32_t blk1 = hex_smin(blk0 + dctx->blocks_per_hvx, dctx->n_blocks);
+ if (blk0 >= blk1) {
+ return;
+ }
+
+ uint8_t * spad = dctx->vtcm + ith*HTP_DSV4_HC_COMB_VTCM_PER_HVX;
+ void * in_buf[2] = { spad, spad + 2*HTP_DSV4_HC_COMB_BLOCK_SIZE };
+ void * out_buf[2] = { spad + HTP_DSV4_HC_COMB_BLOCK_SIZE, spad + 3*HTP_DSV4_HC_COMB_BLOCK_SIZE };
+
+ struct dsv4_hc_comb_pipe_state s = {
+ .dctx = dctx,
+ .mixes_data = mixes->data,
+ .dst_data = dst->data,
+ .mixes_nb0 = mixes->nb[0],
+ .mixes_nb1 = mixes->nb[1],
+ .dst_nb2 = dst->nb[2],
+ .blk = blk0,
+ .blk_end = blk1,
+ .token_end = dctx->token_start + dctx->n_tokens,
+ };
+
+ dma_pipe_io_run(th->dma_q, th->trace, &s, in_buf, out_buf,
+ dsv4_hc_comb_next_task, dctx->push_in, dctx->compute, dctx->push_out);
+
+ (void) nth;
+}
+
+// generic strides: gather each entry into its lane vector
+static void dsv4_hc_comb_push_src_gather(dma_queue * dma_q, const struct htp_tensor * mixes, uint32_t t0, uint32_t n, uint8_t * spad, uint32_t mix_es) {
+ for (uint32_t k = 0; k < 16; ++k) {
+ dma_queue_push(dma_q, dma_make_data(spad + k*sizeof(HVX_Vector), mixes->data + (8 + k)*mixes->nb[0] + t0*mixes->nb[1]),
+ mix_es, mixes->nb[1], mix_es, n);
+ }
+}
+
+static void dsv4_hc_comb_thread_gather(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
+ const struct dsv4_hc_comb_context * dctx = (const struct dsv4_hc_comb_context *) data;
+
+ const struct htp_tensor * mixes = dctx->octx->src[0];
+ const struct htp_tensor * dst = dctx->octx->dst;
+
+ const uint32_t blk0 = ith * dctx->blocks_per_hvx;
+ const uint32_t blk1 = hex_smin(blk0 + dctx->blocks_per_hvx, dctx->n_blocks);
+ if (blk0 >= blk1) {
+ return;
+ }
+
+ // per slot: src block, then dst block
+ uint8_t * spad = dctx->vtcm + ith*HTP_DSV4_HC_COMB_VTCM_PER_HVX;
+ uint8_t * slot[2] = { spad, spad + 2*HTP_DSV4_HC_COMB_BLOCK_SIZE };
+
+ const uint32_t token_end = dctx->token_start + dctx->n_tokens;
+ const uint32_t mix_es = dctx->mix_es;
+ const size_t row_size = dctx->dst_row_size;
+
+ dma_queue * dma_q = th->dma_q;
+ uint32_t prefetch_blk = blk0;
+ for (uint32_t i = 0; i < 2 && prefetch_blk < blk1; ++i, ++prefetch_blk) {
+ const uint32_t t0 = dctx->token_start + prefetch_blk*HTP_DSV4_HC_COMB_LANES;
+ // empty dst writeback keeps pop order dst, src in loop
+ dma_queue_push(dma_q, dma_make_data(dst->data, slot[i] + HTP_DSV4_HC_COMB_BLOCK_SIZE), row_size, row_size, 0, 0);
+ dsv4_hc_comb_push_src_gather(dma_q, mixes, t0, hex_smin(HTP_DSV4_HC_COMB_LANES, token_end - t0), slot[i], mix_es);
+ }
+
+ struct htp_thread_trace * tr = th->trace;
+ const dsv4_hc_comb_block_fn_t block_fn = dctx->block_fn;
+
+ uint32_t curr = 0;
+ for (uint32_t blk = blk0; blk < blk1; ++blk) {
+ dma_queue_pop(dma_q);
+ for (uint32_t i = 0; i < 16; ++i) {
+ dma_queue_pop(dma_q);
+ }
+
+ const uint32_t t0 = dctx->token_start + blk*HTP_DSV4_HC_COMB_LANES;
+ const uint32_t n = hex_smin(HTP_DSV4_HC_COMB_LANES, token_end - t0);
+ uint8_t * d_spad = slot[curr] + HTP_DSV4_HC_COMB_BLOCK_SIZE;
+
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) blk);
+ block_fn(slot[curr], d_spad, dctx);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) blk);
+
+ dma_queue_push(dma_q, dma_make_data(dst->data + t0*dst->nb[2], d_spad), dst->nb[2], row_size, row_size, n);
+
+ if (prefetch_blk < blk1) {
+ const uint32_t tp = dctx->token_start + prefetch_blk*HTP_DSV4_HC_COMB_LANES;
+ dsv4_hc_comb_push_src_gather(dma_q, mixes, tp, hex_smin(HTP_DSV4_HC_COMB_LANES, token_end - tp), slot[curr], mix_es);
+ prefetch_blk++;
+ }
+
+ curr ^= 1;
+ }
+
+ dma_queue_flush(dma_q);
+
+ (void) nth;
+}
+
+int op_dsv4_hc_comb(struct htp_ops_context * octx) {
+ const struct htp_tensor * mixes = octx->src[0];
+ const struct htp_tensor * dst = octx->dst;
+
+ const struct htp_dsv4_hc_comb_kernel_params * kparams =
+ (const struct htp_dsv4_hc_comb_kernel_params *) octx->kernel_params;
+
+ const uint32_t mixes_f16 = (mixes->type == HTP_TYPE_F16) ? 1u : 0u;
+ const uint32_t dst_f16 = (dst->type == HTP_TYPE_F16) ? 1u : 0u;
+ const uint32_t fn_idx = (mixes_f16 << 1) | dst_f16;
+
+ static const dma_pipe_compute_io_fn rows_compute_fns[4] = {
+ dsv4_hc_comb_compute_rows_f32_f32,
+ dsv4_hc_comb_compute_rows_f32_f16,
+ dsv4_hc_comb_compute_rows_f16_f32,
+ dsv4_hc_comb_compute_rows_f16_f16,
+ };
+
+ static const dma_pipe_compute_io_fn lanes_compute_fns[4] = {
+ dsv4_hc_comb_compute_lanes_f32_f32,
+ dsv4_hc_comb_compute_lanes_f32_f16,
+ dsv4_hc_comb_compute_lanes_f16_f32,
+ dsv4_hc_comb_compute_lanes_f16_f16,
+ };
+
+ static const dsv4_hc_comb_block_fn_t lanes_block_fns[4] = {
+ dsv4_hc_comb_block_lanes_f32_f32,
+ dsv4_hc_comb_block_lanes_f32_f16,
+ dsv4_hc_comb_block_lanes_f16_f32,
+ dsv4_hc_comb_block_lanes_f16_f16,
+ };
+
+ struct dsv4_hc_comb_context dctx = {
+ .octx = octx,
+ .push_out = dst_f16 ? dsv4_hc_comb_push_out_f16 : dsv4_hc_comb_push_out_f32,
+ .mix_es = mixes_f16 ? sizeof(__fp16) : sizeof(float),
+ .dst_row_size = 16u * (dst_f16 ? sizeof(__fp16) : sizeof(float)),
+ .token_start = 0,
+ .n_tokens = mixes->ne[1],
+ .n_blocks = kparams->n_blocks,
+ .blocks_per_hvx = kparams->blocks_per_hvx,
+ .vtcm = (uint8_t *) octx->ctx->vtcm_base,
+ };
+
+ work_queue_func_t worker = NULL;
+ switch (kparams->in_layout) {
+ case HTP_DSV4_HC_COMB_IN_ROWS:
+ worker = dsv4_hc_comb_thread;
+ dctx.push_in = mixes_f16 ? dsv4_hc_comb_push_in_rows_f16 : dsv4_hc_comb_push_in_rows_f32;
+ dctx.compute = rows_compute_fns[fn_idx];
+ break;
+ case HTP_DSV4_HC_COMB_IN_LANES:
+ worker = dsv4_hc_comb_thread;
+ dctx.push_in = mixes_f16 ? dsv4_hc_comb_push_in_lanes_f16 : dsv4_hc_comb_push_in_lanes_f32;
+ dctx.compute = lanes_compute_fns[fn_idx];
+ break;
+ case HTP_DSV4_HC_COMB_IN_GATHER:
+ worker = dsv4_hc_comb_thread_gather;
+ dctx.block_fn = lanes_block_fns[fn_idx];
+ break;
+ default:
+ return HTP_STATUS_INVAL_PARAMS;
+ }
+
+ if (mixes->ne[1] == 0) {
+ return HTP_STATUS_OK;
+ }
+
+ const uint32_t n_hvx = kparams->n_hvx;
+ if (!htp_ops_context_set_n_hvx_div(octx, n_hvx, kparams->div_n_hvx)) {
+ return HTP_STATUS_INVAL_PARAMS;
+ }
+
+ // all writes go through DMA, so any token split is safe
+ if (octx->ctx->mdev.count > 1) {
+ const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(dctx.n_tokens, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
+ dctx.token_start = range.start;
+ dctx.n_tokens = range.count;
+ if (dctx.n_tokens == 0) {
+ return HTP_STATUS_OK;
+ }
+ dctx.n_blocks = (dctx.n_tokens + HTP_DSV4_HC_COMB_LANES - 1) / HTP_DSV4_HC_COMB_LANES;
+ dctx.blocks_per_hvx = fastdiv(dctx.n_blocks + n_hvx - 1, &octx->n_hvx_div);
+ }
+
+ // only scale[2] and base[8:24] are used; read them once for all threads
+ if (octx->ctx->ddr_spad_size < 2*128) {
+ return HTP_STATUS_INTERNAL_ERR;
+ }
+ uint8_t * spad = (uint8_t *) octx->ctx->ddr_spad_base;
+
+ struct htp_tensor base_v = *octx->src[2];
+ base_v.data += 8*base_v.nb[0];
+ base_v.ne[0] = 16;
+ struct htp_tensor scale_v = *octx->src[1];
+ scale_v.data += 2*scale_v.nb[0];
+ scale_v.ne[0] = 1;
+ for (int i = 1; i < HTP_OP_MAX_DIMS; ++i) {
+ base_v.ne[i] = 1;
+ scale_v.ne[i] = 1;
+ }
+
+ if (htp_tensor_read(&octx->ctx->main_th, spad, 128, &base_v) == 0 ||
+ htp_tensor_read(&octx->ctx->main_th, spad + 128, 128, &scale_v) == 0) {
+ return HTP_STATUS_INTERNAL_ERR;
+ }
+ if (base_v.type == HTP_TYPE_F16) {
+ const __fp16 * bp = (const __fp16 *) spad;
+ for (int i = 0; i < 16; ++i) {
+ dctx.base_comb[i] = (float) bp[i];
+ }
+ } else {
+ memcpy(dctx.base_comb, spad, sizeof(dctx.base_comb));
+ }
+ if (scale_v.type == HTP_TYPE_F16) {
+ dctx.scale_comb = (float) *(const __fp16 *) (spad + 128);
+ } else {
+ memcpy(&dctx.scale_comb, spad + 128, sizeof(float));
+ }
+ memcpy(&dctx.eps, &octx->op_params[0], sizeof(float));
+ memcpy(&dctx.n_iter, &octx->op_params[1], sizeof(int32_t));
+
+ FARF(HIGH, "dsv4-hc-comb: (%ux%u) : in %u tokens %u:%u nhvx %u n_iter %d\n",
+ mixes->ne[0], mixes->ne[1], kparams->in_layout,
+ dctx.token_start, dctx.token_start + dctx.n_tokens, n_hvx, dctx.n_iter);
+
+ work_queue_run(octx->ctx->hvx_queue, worker, &dctx, n_hvx);
+ return HTP_STATUS_OK;
+}
diff --git a/ggml/src/ggml-hexagon/htp/dsv4-hc-ops.h b/ggml/src/ggml-hexagon/htp/dsv4-hc-ops.h
index 58a3d0d3c..55d61e193 100644
--- a/ggml/src/ggml-hexagon/htp/dsv4-hc-ops.h
+++ b/ggml/src/ggml-hexagon/htp/dsv4-hc-ops.h
@@ -69,9 +69,36 @@ static_assert(sizeof(struct htp_dsv4_hc_post_kernel_params) <= 128, "htp_dsv4_hc
_Static_assert(sizeof(struct htp_dsv4_hc_post_kernel_params) <= 128, "htp_dsv4_hc_post_kernel_params is too large for kernel_params blob");
#endif
+#define HTP_DSV4_HC_COMB_LANES 32 // tokens per HVX block
+#define HTP_DSV4_HC_COMB_BLOCK_SIZE (HTP_DSV4_HC_COMB_LANES * 16 * 4) // 32 tokens x 16 comb floats
+#define HTP_DSV4_HC_COMB_VTCM_PER_HVX (2 * 2 * HTP_DSV4_HC_COMB_BLOCK_SIZE) // 2 slots of in + out
+
+// how mixes is DMA'd into VTCM
+enum htp_dsv4_hc_comb_in_layout {
+ HTP_DSV4_HC_COMB_IN_ROWS = 0, // nb0 == elem_size: one 2D DMA of 16-elem rows per token
+ HTP_DSV4_HC_COMB_IN_LANES = 1, // nb1 == elem_size: one 2D DMA of token-contiguous entries
+ HTP_DSV4_HC_COMB_IN_GATHER = 2, // other strides: one 2D DMA of elem_size rows per entry
+};
+
+struct htp_dsv4_hc_comb_kernel_params {
+ uint32_t in_layout;
+ uint32_t n_hvx;
+ uint32_t n_blocks; // ceil(n_tokens / HTP_DSV4_HC_COMB_LANES)
+ uint32_t blocks_per_hvx;
+ uint32_t vtcm_size;
+ struct fastdiv_values div_n_hvx;
+};
+
+#if defined(__cplusplus)
+static_assert(sizeof(struct htp_dsv4_hc_comb_kernel_params) <= 128, "htp_dsv4_hc_comb_kernel_params is too large for kernel_params blob");
+#else
+_Static_assert(sizeof(struct htp_dsv4_hc_comb_kernel_params) <= 128, "htp_dsv4_hc_comb_kernel_params is too large for kernel_params blob");
+#endif
+
struct htp_ops_context;
int op_dsv4_hc_pre(struct htp_ops_context * octx);
int op_dsv4_hc_post(struct htp_ops_context * octx);
+int op_dsv4_hc_comb(struct htp_ops_context * octx);
#endif // HTP_DSV4_HC_OPS_H
diff --git a/ggml/src/ggml-hexagon/htp/htp-ops.h b/ggml/src/ggml-hexagon/htp/htp-ops.h
index c7098209e..7f3885a23 100644
--- a/ggml/src/ggml-hexagon/htp/htp-ops.h
+++ b/ggml/src/ggml-hexagon/htp/htp-ops.h
@@ -126,6 +126,7 @@ enum htp_op_code {
HTP_OP_CONV_3D,
HTP_OP_DSV4_HC_PRE,
HTP_OP_DSV4_HC_POST,
+ HTP_OP_DSV4_HC_COMB,
HTP_OP_TOP_K_MASK,
HTP_OP_LIGHTNING_INDEXER,
HTP_OP_SSM_SCAN,
diff --git a/ggml/src/ggml-hexagon/htp/main.c b/ggml/src/ggml-hexagon/htp/main.c
index 34fa37330..5fa3b186a 100644
--- a/ggml/src/ggml-hexagon/htp/main.c
+++ b/ggml/src/ggml-hexagon/htp/main.c
@@ -1099,6 +1099,9 @@ static int execute_op(struct htp_ops_context * octx) {
case HTP_OP_DSV4_HC_POST:
return op_dsv4_hc_post(octx);
+ case HTP_OP_DSV4_HC_COMB:
+ return op_dsv4_hc_comb(octx);
+
case HTP_OP_CONCAT:
return op_concat(octx);