Commit 7258196bf for llama.cpp
commit 7258196bf3df9cd7d78e8a00111f5412cf8c55da
Author: Max Krasnyansky <maxk@qti.qualcomm.com>
Date: Sat Dec 5 14:22:00 2026 -0800
hexagon: optimized Gated Delta Net and improved fusion (#33831)
* hexagon : optimize chunked HMX GATED_DELTA_NET prefill
Eliminate vscatter/vextract, add 2-group HMX/HVX ping-pong, odd-stride VTCM bank padding, Wave 4 HVX micro-kernels, deep HMX GEMMs, dual_hvx, and ddr_spad_base staging.
Assisted-by: Gemini
* hexagon: cleanup gated-delta kparams and solver
* hexagon: fix missed ssm_conv fusion cases
* hexagon: another pass at fusion refactoring
* hexagon: optimize RMS_NORM F16
* hexagon: optimize RMS_NORM + MUL
* hexagon: support for fusing rms_norm_mul with GLU ops
* hexagon: further improve/generalize GLU fusion with common code between mat_mul and rms_norm_mul
* hexagon: remove remaining legacy 128byte checks for mdev splits
* hexagon: fix corner cases in mdev splits
* hexagon: add support for strided-src2 to unary ops
* hexagon: more fixes/cases for GLU fusions
diff --git a/ggml/src/ggml-hexagon/htp-fusion.cpp b/ggml/src/ggml-hexagon/htp-fusion.cpp
index 3ec84acfc..c828f1319 100644
--- a/ggml/src/ggml-hexagon/htp-fusion.cpp
+++ b/ggml/src/ggml-hexagon/htp-fusion.cpp
@@ -15,7 +15,16 @@ static inline bool htp_same_shape_and_contiguity(const ggml_tensor * a, const gg
}
static inline bool htp_inplace_overlap_unsafe(const ggml_tensor * dst, const ggml_tensor * in) {
- return htp_tensors_overlap(dst, in) && (dst->data != in->data || !ggml_are_same_stride(dst, in));
+ return htp_tensors_overlap(dst, in) &&
+ (dst->data != in->data || dst->type != in->type ||
+ !ggml_are_same_shape(dst, in) || !ggml_are_same_stride(dst, in));
+}
+
+static inline size_t htp_mm_act_idx(const htp_opnode & node) {
+ if (node.opcode == HTP_OP_MUL_MAT_NX || node.opcode == HTP_OP_MUL_MAT_ID_NX) {
+ return node.outputs.size();
+ }
+ return (node.opcode == HTP_OP_MUL_MAT_NX_GLU) ? 2 : 1;
}
static inline bool htp_inputs_overlap_unsafe(const htp_opnode & prev, const ggml_tensor * dst, const ggml_tensor * extra = nullptr) {
@@ -68,7 +77,6 @@ static bool fuse_allreduce_add(const ggml_hexagon_session * sess, htp_opnode & p
const ggml_tensor * ar_local = prev.inputs[rank];
if (!ggml_hexagon_tensor_is_fuseable(ar_local)) return false;
- if (cur.inputs.size() < 2) return false;
const ggml_tensor * cur_src0 = cur.inputs[0];
const ggml_tensor * cur_src1 = cur.inputs[1];
const ggml_tensor * add_dst = cur.dst();
@@ -159,12 +167,11 @@ static bool fuse_rms_norm_mul_add(const ggml_hexagon_session * sess, htp_opnode
const ggml_tensor * r = nullptr;
if (!htp_match_consumer(prev, cur, /*commutative=*/ true, &r)) return false;
- const ggml_tensor * src0 = prev.src0();
- const ggml_tensor * weight = prev.inputs[1];
- const ggml_tensor * dst = cur.dst();
- if ((r->type != GGML_TYPE_F32 && r->type != GGML_TYPE_F16) ||
- (weight->type != GGML_TYPE_F32 && weight->type != GGML_TYPE_F16)) return false;
- if (!ggml_are_same_shape(src0, r) || (ggml_is_contiguous(src0) != ggml_is_contiguous(r))) return false;
+ const ggml_tensor * src0 = prev.src0();
+ const ggml_tensor * dst = cur.dst();
+ if (r->type != GGML_TYPE_F32 && r->type != GGML_TYPE_F16) return false;
+ if (!ggml_are_same_shape(src0, r) || (ggml_is_contiguous(src0) != ggml_is_contiguous(r)) ||
+ !htp_same_shape_and_contiguity(prev.dst(), dst)) return false;
if (htp_inputs_overlap_unsafe(prev, dst, r)) return false;
htp_opnode cand = prev;
@@ -239,7 +246,7 @@ static bool fuse_mul_mat_add(const ggml_hexagon_session * sess, htp_opnode & pre
}
const auto * orig_kparams = prev.kparams<htp_mm_kernel_params>();
- const ggml_tensor * scale_w = (orig_kparams->scale_mask != 0 && prev.inputs.size() >= 3) ? prev.inputs[2] : nullptr;
+ const ggml_tensor * scale_w = (orig_kparams->scale_mask != 0) ? prev.inputs[2] : nullptr;
if (scale_w && htp_tensors_overlap(dst, scale_w)) {
return false;
}
@@ -276,38 +283,47 @@ static bool fuse_mul_mat_add(const ggml_hexagon_session * sess, htp_opnode & pre
return true;
}
-static bool fuse_mul_mat_nx(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) {
- if (prev.opcode != HTP_OP_MUL_MAT && prev.opcode != HTP_OP_MUL_MAT_NX) return false;
+static bool fuse_mm_nx(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur, bool is_id) {
+ const htp_op_code base_op = is_id ? HTP_OP_MUL_MAT_ID : HTP_OP_MUL_MAT;
+ const htp_op_code nx_op = is_id ? HTP_OP_MUL_MAT_ID_NX : HTP_OP_MUL_MAT_NX;
+ if (prev.opcode != base_op && prev.opcode != nx_op) return false;
- const uint32_t curr_n = (uint32_t) prev.outputs.size();
- const uint32_t new_n = curr_n + 1;
+ const uint32_t curr_n = (uint32_t) prev.outputs.size();
+ const uint32_t new_n = curr_n + 1;
+ const uint32_t extra_in = is_id ? 2u : 1u;
auto * orig_kparams = prev.kparams<htp_mm_kernel_params>();
const auto * cur_kparams = cur.kparams<htp_mm_kernel_params>();
- const bool prev_has_scale = (orig_kparams->scale_mask != 0);
- const bool cur_has_scale = (cur_kparams->scale_mask != 0);
- const bool any_scale = prev_has_scale || cur_has_scale;
- const size_t needed_in = any_scale ? (2 * new_n + 1) : (new_n + 1);
+ const bool prev_has_scale = (orig_kparams->scale_mask != 0);
+ const bool cur_has_scale = (cur_kparams->scale_mask != 0);
+ const bool any_scale = prev_has_scale || cur_has_scale;
+ const size_t needed_in = any_scale ? (2 * new_n + extra_in) : (new_n + extra_in);
if (curr_n >= HTP_OP_MAX_OUTPUTS || needed_in > HTP_OP_MAX_INPUTS) return false;
- const ggml_tensor * w0 = prev.inputs[0];
- const ggml_tensor * x_orig = prev.node->src[1];
- const ggml_tensor * w_in = cur.node->src[0];
- const ggml_tensor * x_in = cur.node->src[1];
- const ggml_tensor * d_in = cur.node;
+ const ggml_tensor * w0 = prev.inputs[0];
+ const ggml_tensor * x = prev.inputs[curr_n];
+ const ggml_tensor * ids = is_id ? prev.inputs[curr_n + 1] : nullptr;
+ const ggml_tensor * w_in = cur.src0();
+ const ggml_tensor * x_in = cur.src1();
+ const ggml_tensor * d_in = cur.dst();
- if (x_in != x_orig || w_in->type != w0->type || w_in->ne[0] != w0->ne[0] ||
+ if (x_in != x || w_in->type != w0->type || w_in->ne[0] != w0->ne[0] ||
(ggml_needs_scale(w0->type) && prev_has_scale != cur_has_scale) ||
- !ggml_is_matrix(w0) || !ggml_is_matrix(w_in) ||
ggml_hexagon_tensor_is_tiled(w_in) != ggml_hexagon_tensor_is_tiled(w0) ||
- orig_kparams->n_hmx != cur_kparams->n_hmx ||
- orig_kparams->kernel_type != cur_kparams->kernel_type) {
+ orig_kparams->n_hmx != cur_kparams->n_hmx) {
return false;
}
-
- const bool collapse = htp_matmul_can_collapse(w0, x_orig, prev.node) && htp_matmul_can_collapse(w_in, x_in, d_in);
- if (!ggml_is_matrix(x_orig) && !collapse) return false;
+ if (is_id) {
+ if (cur.node->src[2] != ids || w_in->ne[2] != w0->ne[2]) return false;
+ } else {
+ if (!ggml_is_matrix(w0) || !ggml_is_matrix(w_in) ||
+ orig_kparams->kernel_type != cur_kparams->kernel_type) {
+ return false;
+ }
+ const bool collapse = htp_matmul_can_collapse(w0, x, prev.node) && htp_matmul_can_collapse(w_in, x_in, d_in);
+ if (!ggml_is_matrix(x) && !collapse) return false;
+ }
const ggml_tensor * weights[HTP_OP_MAX_OUTPUTS] = {};
const ggml_tensor * scales[HTP_OP_MAX_OUTPUTS] = {};
@@ -315,14 +331,13 @@ static bool fuse_mul_mat_nx(const ggml_hexagon_session * sess, htp_opnode & prev
for (uint32_t i = 0; i < curr_n; ++i) {
weights[i] = prev.inputs[i];
if ((orig_kparams->scale_mask >> i) & 1u) {
- const size_t sidx = (prev.opcode == HTP_OP_MUL_MAT) ? 2 : (curr_n + 1 + i);
- scales[i] = (sidx < prev.inputs.size()) ? prev.inputs[sidx] : nullptr;
+ scales[i] = prev.inputs[curr_n + extra_in + i];
smodes[i] = (uint8_t) htp_mm_scale_mode_get(orig_kparams->scale_mode, i);
}
}
weights[curr_n] = w_in;
- if (cur_has_scale && cur.inputs.size() >= 3) {
- scales[curr_n] = cur.inputs[2];
+ if (cur_has_scale) {
+ scales[curr_n] = cur.inputs[1 + extra_in];
smodes[curr_n] = (uint8_t) htp_mm_scale_mode_get(cur_kparams->scale_mode, 0);
}
@@ -344,7 +359,7 @@ static bool fuse_mul_mat_nx(const ggml_hexagon_session * sess, htp_opnode & prev
const ggml_tensor * prev_w = prev.inputs[0];
ggml_type prev_dst_type = prev.outputs[0]->type;
- for (size_t i = 1; i < curr_n; ++i) {
+ for (uint32_t i = 1; i < curr_n; ++i) {
if (prev.inputs[i]->ne[1] > prev_w->ne[1]) prev_w = prev.inputs[i];
if (ggml_type_size(prev.outputs[i]->type) > ggml_type_size(prev_dst_type)) {
prev_dst_type = prev.outputs[i]->type;
@@ -360,7 +375,10 @@ static bool fuse_mul_mat_nx(const ggml_hexagon_session * sess, htp_opnode & prev
for (uint32_t i = 0; i < new_n; ++i) {
target.inputs.push_back(weights[i]);
}
- target.inputs.push_back(x_orig);
+ target.inputs.push_back(x);
+ if (is_id) {
+ target.inputs.push_back(ids);
+ }
for (uint32_t i = 0; i < new_n; ++i) {
target.inputs.push_back(scales[i]);
}
@@ -371,7 +389,7 @@ static bool fuse_mul_mat_nx(const ggml_hexagon_session * sess, htp_opnode & prev
target.fused.push_back(cur.node);
};
- if (prev.opcode == HTP_OP_MUL_MAT_NX && w_for_solve == prev_w &&
+ if (prev.opcode == nx_op && w_for_solve == prev_w &&
ggml_type_size(max_dst_type) == ggml_type_size(prev_dst_type) &&
req_scale_sz <= orig_kparams->vtcm_scale_size) {
orig_kparams->n_weights = new_n;
@@ -382,7 +400,7 @@ static bool fuse_mul_mat_nx(const ggml_hexagon_session * sess, htp_opnode & prev
}
htp_opnode cand = prev;
- cand.opcode = HTP_OP_MUL_MAT_NX;
+ cand.opcode = nx_op;
append_to(cand);
auto * kp = cand.kparams<htp_mm_kernel_params>();
kp->scale_mask = new_scale_mask;
@@ -400,13 +418,8 @@ static bool htp_is_fuseable_mmnx_glu_op(uint32_t op) {
static bool fuse_mul_mat_nx_glu(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) {
if (prev.opcode != HTP_OP_MUL_MAT_NX || prev.outputs.size() != 2 ||
- (prev.inputs.size() != 3 && prev.inputs.size() != 5)) {
- return false;
- }
- if (!htp_is_fuseable_mmnx_glu_op(cur.opcode) || cur.inputs.size() != 2 || cur.outputs.size() != 1 || !cur.src1()) {
- return false;
- }
- if (prev.fused.size() + 1 + cur.fused.size() > HTP_OP_MAX_FUSED) {
+ !htp_is_fuseable_mmnx_glu_op(cur.opcode) ||
+ prev.fused.size() + 1 + cur.fused.size() > HTP_OP_MAX_FUSED) {
return false;
}
@@ -449,10 +462,8 @@ static bool fuse_mul_mat_nx_glu(const ggml_hexagon_session * sess, htp_opnode &
return false;
}
- const ggml_tensor * s_gate = (((orig_kparams->scale_mask >> gate_idx) & 1u) != 0 && prev.inputs.size() == 5)
- ? prev.inputs[3 + gate_idx] : nullptr;
- const ggml_tensor * s_up = (((orig_kparams->scale_mask >> up_idx) & 1u) != 0 && prev.inputs.size() == 5)
- ? prev.inputs[3 + up_idx] : nullptr;
+ const ggml_tensor * s_gate = ((orig_kparams->scale_mask >> gate_idx) & 1u) ? prev.inputs[3 + gate_idx] : nullptr;
+ const ggml_tensor * s_up = ((orig_kparams->scale_mask >> up_idx) & 1u) ? prev.inputs[3 + up_idx] : nullptr;
const uint32_t smode_gate = s_gate ? htp_mm_scale_mode_get(orig_kparams->scale_mode, gate_idx) : 0;
const uint32_t smode_up = s_up ? htp_mm_scale_mode_get(orig_kparams->scale_mode, up_idx) : 0;
const uint8_t glu_smask = (uint8_t) ((s_gate ? 1u : 0u) | (s_up ? 2u : 0u));
@@ -505,126 +516,6 @@ static bool fuse_mul_mat_nx_glu(const ggml_hexagon_session * sess, htp_opnode &
return true;
}
-static bool fuse_mul_mat_id_nx(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) {
- if (prev.opcode != HTP_OP_MUL_MAT_ID && prev.opcode != HTP_OP_MUL_MAT_ID_NX) return false;
-
- const uint32_t curr_n = (uint32_t) prev.outputs.size();
- const uint32_t new_n = curr_n + 1;
-
- auto * orig_kparams = prev.kparams<htp_mm_kernel_params>();
- const auto * cur_kparams = cur.kparams<htp_mm_kernel_params>();
-
- const bool prev_has_scale = (orig_kparams->scale_mask != 0);
- const bool cur_has_scale = (cur_kparams->scale_mask != 0);
- const bool any_scale = prev_has_scale || cur_has_scale;
- const size_t needed_in = any_scale ? (2 * new_n + 2) : (new_n + 2);
- if (curr_n >= HTP_OP_MAX_OUTPUTS || needed_in > HTP_OP_MAX_INPUTS) return false;
-
- const ggml_tensor * w0 = prev.inputs[0];
- const ggml_tensor * x = prev.inputs[curr_n];
- const ggml_tensor * ids = prev.inputs[curr_n + 1];
- const ggml_tensor * w_in = cur.src0();
- const ggml_tensor * x_in = cur.src1();
- const ggml_tensor * ids_in = cur.node->src[2];
- const ggml_tensor * d_in = cur.dst();
-
- if (x_in != x || ids_in != ids || w_in->type != w0->type ||
- (ggml_needs_scale(w0->type) && prev_has_scale != cur_has_scale) ||
- w_in->ne[0] != w0->ne[0] || w_in->ne[2] != w0->ne[2] ||
- ggml_hexagon_tensor_is_tiled(w_in) != ggml_hexagon_tensor_is_tiled(w0) ||
- orig_kparams->n_hmx != cur_kparams->n_hmx) {
- return false;
- }
-
- const ggml_tensor * weights[HTP_OP_MAX_OUTPUTS] = {};
- const ggml_tensor * scales[HTP_OP_MAX_OUTPUTS] = {};
- uint8_t smodes[HTP_OP_MAX_OUTPUTS] = {};
- for (uint32_t i = 0; i < curr_n; ++i) {
- weights[i] = prev.inputs[i];
- if ((orig_kparams->scale_mask >> i) & 1u) {
- const size_t sidx = (prev.opcode == HTP_OP_MUL_MAT_ID) ? 3 : (curr_n + 2 + i);
- scales[i] = (sidx < prev.inputs.size()) ? prev.inputs[sidx] : nullptr;
- smodes[i] = (uint8_t) htp_mm_scale_mode_get(orig_kparams->scale_mode, i);
- }
- }
- weights[curr_n] = w_in;
- if (cur_has_scale && cur.inputs.size() >= 4) {
- scales[curr_n] = cur.inputs[3];
- smodes[curr_n] = (uint8_t) htp_mm_scale_mode_get(cur_kparams->scale_mode, 0);
- }
-
- uint8_t new_scale_mask = 0;
- uint16_t new_scale_mode = 0;
- size_t hmx_scale_size = 0;
- size_t hvx_scale_size = 0;
- for (uint32_t i = 0; i < new_n; ++i) {
- if (scales[i]) {
- new_scale_mask |= (uint8_t) (1u << i);
- new_scale_mode |= (uint16_t) (smodes[i] << (2u * i));
- const size_t sb = htp_mm_fused_scale_vtcm_bytes((uint32_t) weights[i]->ne[1], (uint32_t) weights[i]->ne[2], smodes[i], true);
- if (sb > hmx_scale_size) {
- hmx_scale_size = sb;
- }
- hvx_scale_size += htp_mm_fused_scale_vtcm_bytes((uint32_t) weights[i]->ne[1], (uint32_t) weights[i]->ne[2], smodes[i], false);
- }
- }
-
- const ggml_tensor * prev_w = prev.inputs[0];
- ggml_type prev_dst_type = prev.outputs[0]->type;
- for (uint32_t i = 1; i < curr_n; ++i) {
- if (prev.inputs[i]->ne[1] > prev_w->ne[1]) {
- prev_w = prev.inputs[i];
- }
- if (ggml_type_size(prev.outputs[i]->type) > ggml_type_size(prev_dst_type)) {
- prev_dst_type = prev.outputs[i]->type;
- }
- }
- const ggml_tensor * w_for_solve = (w_in->ne[1] > prev_w->ne[1]) ? w_in : prev_w;
- const ggml_type max_dst_type = (ggml_type_size(d_in->type) > ggml_type_size(prev_dst_type)) ? d_in->type : prev_dst_type;
- const size_t req_scale_sz = orig_kparams->n_hmx ? hmx_scale_size : hvx_scale_size;
-
- auto append_to = [&](htp_opnode & target) {
- if (any_scale) {
- target.inputs.clear();
- for (uint32_t i = 0; i < new_n; ++i) {
- target.inputs.push_back(weights[i]);
- }
- target.inputs.push_back(x);
- target.inputs.push_back(ids);
- for (uint32_t i = 0; i < new_n; ++i) {
- target.inputs.push_back(scales[i]);
- }
- } else {
- target.inputs.insert(target.inputs.begin() + curr_n, w_in);
- }
- target.outputs.push_back(d_in);
- target.fused.push_back(cur.node);
- };
-
- if (prev.opcode == HTP_OP_MUL_MAT_ID_NX && w_for_solve == prev_w &&
- ggml_type_size(max_dst_type) == ggml_type_size(prev_dst_type) &&
- req_scale_sz <= orig_kparams->vtcm_scale_size) {
- orig_kparams->n_weights = new_n;
- orig_kparams->scale_mask = new_scale_mask;
- orig_kparams->scale_mode = new_scale_mode;
- append_to(prev);
- return true;
- }
-
- htp_opnode cand = prev;
- cand.opcode = HTP_OP_MUL_MAT_ID_NX;
- append_to(cand);
- auto * kp = cand.kparams<htp_mm_kernel_params>();
- kp->scale_mask = new_scale_mask;
- kp->scale_mode = new_scale_mode;
- if (!htp_kparams_solve(sess, cand)) {
- return false;
- }
-
- prev = std::move(cand);
- return true;
-}
-
static bool fuse_gdn_cpy(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) {
GGML_UNUSED(sess);
if (prev.opcode != HTP_OP_GATED_DELTA_NET || prev.outputs.size() != 1) return false;
@@ -638,7 +529,6 @@ static bool fuse_gdn_cpy(const ggml_hexagon_session * sess, htp_opnode & prev, c
if ((gdn_out->flags & GGML_TENSOR_FLAG_OUTPUT) || (cpy_node->flags & GGML_TENSOR_FLAG_OUTPUT)) return false;
const ggml_tensor * v = prev.node->src[2];
- if (!v) return false;
const int64_t S_v = v->ne[0];
const int64_t H = v->ne[1];
@@ -725,7 +615,7 @@ static inline bool htp_is_scale_bias_unary(uint32_t opcode) {
}
static bool fuse_scale_unary(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) {
- if (prev.opcode != HTP_OP_SCALE || !prev.fused.empty() || !htp_is_scale_bias_unary(cur.opcode)) return false;
+ if (prev.opcode != HTP_OP_SCALE || !prev.fused.empty()) return false;
if (cur.kparams<htp_unary_kernel_params>()->flags & HTP_UNARY_FLAG_SCALE_BIAS) return false;
if (!htp_match_consumer(prev, cur, /*commutative=*/ false)) return false;
@@ -761,7 +651,7 @@ static bool fuse_scale_unary(const ggml_hexagon_session * sess, htp_opnode & pre
}
static bool fuse_unary_scale(const ggml_hexagon_session * sess, htp_opnode & prev, const htp_opnode & cur) {
- if (!htp_is_scale_bias_unary(prev.opcode) || prev.fused.size() >= HTP_OP_MAX_FUSED) return false;
+ if (!htp_is_scale_bias_unary(prev.opcode)) return false;
if (!htp_match_consumer(prev, cur, /*commutative=*/ false)) return false;
const ggml_tensor * src0 = prev.src0();
@@ -879,9 +769,9 @@ static bool fuse_binary_nx(const ggml_hexagon_session * sess, htp_opnode & prev,
const ggml_tensor * src0 = prev.inputs[0];
const ggml_tensor * dst = cur.dst();
- if (prev.dst()->type != src0->type || new_src->type != src0->type || dst->type != src0->type) return false;
- for (const auto * in : prev.inputs) {
- if (in->type != src0->type) return false;
+ if (prev.dst()->type != src0->type || prev.inputs[1]->type != src0->type ||
+ new_src->type != src0->type || dst->type != src0->type) {
+ return false;
}
if (!ggml_are_same_shape(src0, prev.inputs[1]) ||
!ggml_are_same_shape(src0, new_src) ||
@@ -902,7 +792,6 @@ static bool fuse_binary_nx(const ggml_hexagon_session * sess, htp_opnode & prev,
}
bool htp_fusion_can_stack(const struct ggml_tensor * a) {
- if (!a || !a->src[0] || !a->src[1]) return false;
switch (a->op) {
case GGML_OP_MUL_MAT:
if (!htp_fusion_enabled(HTP_FUSE_MUL_MAT_NX)) return false;
@@ -912,7 +801,7 @@ bool htp_fusion_can_stack(const struct ggml_tensor * a) {
(a->src[0]->type == GGML_TYPE_F16 && a->src[0]->ne[0] % 32 == 0);
case GGML_OP_MUL_MAT_ID:
if (!htp_fusion_enabled(HTP_FUSE_MUL_MAT_ID_NX)) return false;
- return a->src[2] != nullptr && htp_is_quantized_weight_type(a->src[0]->type);
+ return htp_is_quantized_weight_type(a->src[0]->type);
default:
return false;
}
@@ -988,7 +877,7 @@ void htp_fusion_tag_graph(struct ggml_cgraph * graph) {
bool htp_fusion_try_fuse(const struct ggml_hexagon_session * sess, struct htp_opnode & prev, const struct htp_opnode & cur) {
if (!opt_opfusion) return false;
if (!prev.node || !cur.node) return false;
- if (!cur.fused.empty()) return false;
+ if (!cur.fused.empty() || prev.fused.size() >= HTP_OP_MAX_FUSED) return false;
switch (cur.opcode) {
case HTP_OP_ADD:
@@ -1023,9 +912,9 @@ bool htp_fusion_try_fuse(const struct ggml_hexagon_session * sess, struct htp_op
case HTP_OP_CPY:
return htp_fusion_enabled(HTP_FUSE_GDN_CPY) && fuse_gdn_cpy(sess, prev, cur);
case HTP_OP_MUL_MAT:
- return htp_fusion_enabled(HTP_FUSE_MUL_MAT_NX) && fuse_mul_mat_nx(sess, prev, cur);
+ return htp_fusion_enabled(HTP_FUSE_MUL_MAT_NX) && fuse_mm_nx(sess, prev, cur, false);
case HTP_OP_MUL_MAT_ID:
- return htp_fusion_enabled(HTP_FUSE_MUL_MAT_ID_NX) && fuse_mul_mat_id_nx(sess, prev, cur);
+ return htp_fusion_enabled(HTP_FUSE_MUL_MAT_ID_NX) && fuse_mm_nx(sess, prev, cur, true);
case HTP_OP_GLU_SWIGLU:
case HTP_OP_GLU_GEGLU:
case HTP_OP_GLU_GEGLU_QUICK:
@@ -1071,9 +960,6 @@ static bool htp_has_intervening_write(
continue;
}
for (const ggml_tensor * out : nodes[k].outputs) {
- if (!out) {
- continue;
- }
for (size_t i = 0; i < n_protected; ++i) {
if (protected_tensors[i] && htp_tensors_overlap(out, protected_tensors[i])) {
return true;
@@ -1097,7 +983,7 @@ static bool htp_post_fuse_is_safe(
uint32_t c_skip_input_mask
) {
for (size_t i = 0; i < n_targets; ++i) {
- if (!targets[i] || ggml_is_view(targets[i])) {
+ if (ggml_is_view(targets[i])) {
return false;
}
}
@@ -1194,7 +1080,7 @@ static void htp_fuse_gdn_state(const struct ggml_hexagon_session * sess, std::ve
}
static bool htp_unwrap_transpose_01(const ggml_tensor * t, const ggml_tensor ** out_src) {
- if (!t || !t->view_src || t->view_offs != 0) {
+ if (!t->view_src || t->view_offs != 0) {
return false;
}
if (t->op == GGML_OP_TRANSPOSE) {
@@ -1213,10 +1099,208 @@ static bool htp_unwrap_transpose_01(const ggml_tensor * t, const ggml_tensor **
return false;
}
+static inline bool htp_op_is_collective(htp_op_code opcode) {
+ return opcode == HTP_OP_ALLREDUCE ||
+ opcode == HTP_OP_ALLREDUCE_ADD ||
+ opcode == HTP_OP_FENCE ||
+ opcode == HTP_OP_CPY_FENCE;
+}
+
+// Check if intervening ops M = (p+1..c-1) \ {skip_q} can be hoisted before ins (< p)
+// so producer p, optional cpy q, and consumer c can fuse without memory-alias hazards.
+static bool htp_can_hoist_intervening(
+ const std::vector<struct htp_opnode> & nodes,
+ size_t ins,
+ size_t p,
+ size_t c,
+ size_t skip_q,
+ const ggml_tensor * const * cand_inputs,
+ size_t n_cand_inputs,
+ const ggml_tensor * conv_x,
+ const ggml_tensor * cpy_dst
+) {
+ for (size_t b = ins; b < p; ++b) {
+ if (htp_op_is_collective(nodes[b].opcode)) {
+ return false;
+ }
+ }
+
+ auto produced_in_range = [&](const ggml_tensor * t, size_t lo, size_t hi, size_t skip) {
+ for (size_t j = lo; j < hi; ++j) {
+ if (j == skip) {
+ continue;
+ }
+ for (const ggml_tensor * out : nodes[j].outputs) {
+ if (htp_tensor_is_view_of(t, out)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ };
+
+ for (size_t m = p + 1; m < c; ++m) {
+ if (m == skip_q) {
+ continue;
+ }
+ if (htp_op_is_collective(nodes[m].opcode)) {
+ return false;
+ }
+
+ for (const ggml_tensor * in_m : nodes[m].inputs) {
+ if (!in_m) {
+ continue;
+ }
+ if (htp_tensors_overlap(in_m, conv_x) && !produced_in_range(in_m, p + 1, m, skip_q)) {
+ return false;
+ }
+ for (size_t b = ins; b < p; ++b) {
+ for (const ggml_tensor * out_b : nodes[b].outputs) {
+ if (!htp_tensors_overlap(in_m, out_b)) {
+ continue;
+ }
+ if (htp_tensor_is_view_of(in_m, out_b) || !produced_in_range(in_m, p + 1, m, skip_q)) {
+ return false;
+ }
+ }
+ }
+ }
+
+ for (const ggml_tensor * out_m : nodes[m].outputs) {
+ if (htp_tensors_overlap(out_m, conv_x) || (cpy_dst && htp_tensors_overlap(out_m, cpy_dst))) {
+ return false;
+ }
+ for (size_t i = 0; i < n_cand_inputs; ++i) {
+ if (cand_inputs[i] && htp_tensors_overlap(out_m, cand_inputs[i]) &&
+ !produced_in_range(cand_inputs[i], ins, p, nodes.size())) {
+ return false;
+ }
+ }
+
+ bool waw_with_b = false;
+ for (size_t b = ins; b < p; ++b) {
+ for (const ggml_tensor * in_b : nodes[b].inputs) {
+ if (!in_b || !htp_tensors_overlap(out_m, in_b)) {
+ continue;
+ }
+ if (htp_tensor_is_view_of(in_b, out_m) || !produced_in_range(in_b, ins, b, nodes.size())) {
+ return false;
+ }
+ }
+ for (const ggml_tensor * out_b : nodes[b].outputs) {
+ if (htp_tensors_overlap(out_m, out_b)) {
+ waw_with_b = true;
+ }
+ }
+ }
+
+ // If B overwrites out_m, out_m must be a standalone scratch tensor with no readers outside M.
+ if (waw_with_b) {
+ if (ggml_is_view(out_m) || (out_m->flags & GGML_TENSOR_FLAG_OUTPUT)) {
+ return false;
+ }
+ for (size_t k = 0; k < nodes.size(); ++k) {
+ if (k > p && k < c && k != skip_q) {
+ continue;
+ }
+ for (const ggml_tensor * in_k : nodes[k].inputs) {
+ if (htp_tensor_is_view_of(in_k, out_m)) {
+ return false;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return true;
+}
+
+static size_t htp_find_hoist_point(
+ const std::vector<struct htp_opnode> & nodes,
+ size_t p,
+ size_t c,
+ size_t skip_q,
+ const ggml_tensor * const * cand_inputs,
+ size_t n_cand_inputs,
+ const ggml_tensor * conv_x,
+ const ggml_tensor * cpy_dst
+) {
+ size_t ins = p;
+ for (size_t i = 0; i < n_cand_inputs; ++i) {
+ if (!htp_has_intervening_write(nodes, p + 1, c, skip_q, &cand_inputs[i], 1)) {
+ continue;
+ }
+ size_t prod = p;
+ for (size_t j = p; j > 0; --j) {
+ for (const ggml_tensor * out : nodes[j - 1].outputs) {
+ if (htp_tensor_is_view_of(cand_inputs[i], out)) {
+ prod = j - 1;
+ break;
+ }
+ }
+ if (prod < p) {
+ break;
+ }
+ }
+ if (prod == p) {
+ return p;
+ }
+ if (prod < ins) {
+ ins = prod;
+ }
+ }
+ if (ins == p || !htp_can_hoist_intervening(nodes, ins, p, c, skip_q, cand_inputs, n_cand_inputs, conv_x, cpy_dst)) {
+ return p;
+ }
+ return ins;
+}
+
+static void htp_apply_concat_ssm_conv(
+ std::vector<struct htp_opnode> & nodes,
+ htp_opnode && cand,
+ size_t p,
+ size_t & c,
+ bool has_cpy,
+ size_t q,
+ size_t hoist_ins
+) {
+ if (hoist_ins < p) {
+ if (has_cpy && q > c) {
+ nodes.erase(nodes.begin() + (ptrdiff_t) q);
+ }
+ nodes[c] = std::move(cand);
+ std::vector<htp_opnode> hoisted;
+ hoisted.reserve(c - p - 1);
+ for (size_t k = p + 1; k < c; ++k) {
+ if (has_cpy && k == q) {
+ continue;
+ }
+ hoisted.push_back(std::move(nodes[k]));
+ }
+ nodes.erase(nodes.begin() + (ptrdiff_t) p, nodes.begin() + (ptrdiff_t) c);
+ const size_t n_hoisted = hoisted.size();
+ nodes.insert(nodes.begin() + (ptrdiff_t) hoist_ins,
+ std::make_move_iterator(hoisted.begin()),
+ std::make_move_iterator(hoisted.end()));
+ c = p + n_hoisted;
+ } else {
+ nodes[c] = std::move(cand);
+ if (has_cpy) {
+ nodes.erase(nodes.begin() + (ptrdiff_t) q);
+ if (q < c) {
+ --c;
+ }
+ }
+ nodes.erase(nodes.begin() + (ptrdiff_t) p);
+ --c;
+ }
+}
+
static void htp_fuse_concat_ssm_conv(const struct ggml_hexagon_session * sess, std::vector<struct htp_opnode> & nodes) {
for (size_t c = 0; c < nodes.size(); ++c) {
if (nodes[c].opcode != HTP_OP_SSM_CONV || nodes[c].inputs.size() != 2 ||
- nodes[c].outputs.size() != 1 || nodes[c].fused.size() >= HTP_OP_MAX_FUSED) {
+ nodes[c].fused.size() >= HTP_OP_MAX_FUSED) {
continue;
}
@@ -1230,8 +1314,7 @@ static void htp_fuse_concat_ssm_conv(const struct ggml_hexagon_session * sess, s
size_t p = c;
for (size_t i = 0; i < c; ++i) {
if (nodes[i].opcode == HTP_OP_CONCAT && nodes[i].fused.empty() &&
- nodes[i].outputs.size() == 1 && nodes[i].dst() == conv_x &&
- ggml_get_op_params_i32(nodes[i].node, 0) == 0) {
+ nodes[i].dst() == conv_x && ggml_get_op_params_i32(nodes[i].node, 0) == 0) {
p = i;
break;
}
@@ -1253,23 +1336,19 @@ static void htp_fuse_concat_ssm_conv(const struct ggml_hexagon_session * sess, s
const int64_t n_s = conv_dst->ne[2];
const int64_t S = d_conv - 1;
- size_t q = nodes.size();
- size_t n_other = 0;
+ size_t q = nodes.size();
+ size_t n_other = 0;
for (size_t k = 0; k < nodes.size(); ++k) {
if (k == p || k == c) {
continue;
}
- bool uses_conv_x = false;
for (const ggml_tensor * in : nodes[k].inputs) {
if (htp_tensor_is_view_of(in, conv_x)) {
- uses_conv_x = true;
+ q = k;
+ ++n_other;
break;
}
}
- if (uses_conv_x) {
- q = k;
- ++n_other;
- }
}
if (n_other > 1) {
continue;
@@ -1279,10 +1358,8 @@ static void htp_fuse_concat_ssm_conv(const struct ggml_hexagon_session * sess, s
const ggml_tensor * cpy_dst = nullptr;
if (has_cpy) {
if (q <= p || nodes[q].opcode != HTP_OP_CPY || !nodes[q].fused.empty() ||
- nodes[q].outputs.size() != 1 || (nodes[q].node->flags & GGML_TENSOR_FLAG_OUTPUT)) {
- continue;
- }
- if (nodes[c].fused.size() + 2 > HTP_OP_MAX_FUSED) {
+ (nodes[q].node->flags & GGML_TENSOR_FLAG_OUTPUT) ||
+ nodes[c].fused.size() + 2 > HTP_OP_MAX_FUSED) {
continue;
}
const ggml_tensor * cpy_src = nodes[q].src0();
@@ -1324,9 +1401,16 @@ static void htp_fuse_concat_ssm_conv(const struct ggml_hexagon_session * sess, s
}
const ggml_tensor * protected_t[] = { cat_state, cat_x, conv_x, conv_weight, has_cpy ? cpy_dst : nullptr };
- if (htp_has_intervening_write(nodes, p + 1, c, has_cpy ? q : nodes.size(), protected_t, 5)) {
- continue;
+ const size_t skip_q = has_cpy ? q : nodes.size();
+ size_t hoist_ins = p;
+ if (htp_has_intervening_write(nodes, p + 1, c, skip_q, protected_t, 5)) {
+ const ggml_tensor * cand_inputs[] = { cat_state, cat_x, conv_weight };
+ hoist_ins = htp_find_hoist_point(nodes, p, c, skip_q, cand_inputs, 3, conv_x, has_cpy ? cpy_dst : nullptr);
+ if (hoist_ins == p) {
+ continue;
+ }
}
+
bool safe = true;
if (has_cpy) {
const size_t lo = (q < c) ? (q + 1) : (c + 1);
@@ -1339,7 +1423,7 @@ static void htp_fuse_concat_ssm_conv(const struct ggml_hexagon_session * sess, s
}
}
for (const ggml_tensor * out : nodes[k].outputs) {
- if (out && (htp_tensors_overlap(out, cpy_dst) || (q > c && htp_tensors_overlap(out, conv_x)))) {
+ if (htp_tensors_overlap(out, cpy_dst) || (q > c && htp_tensors_overlap(out, conv_x))) {
safe = false;
break;
}
@@ -1361,22 +1445,13 @@ static void htp_fuse_concat_ssm_conv(const struct ggml_hexagon_session * sess, s
continue;
}
- nodes[c] = std::move(cand);
- if (has_cpy) {
- nodes.erase(nodes.begin() + (ptrdiff_t) q);
- if (q < c) {
- --c;
- }
- }
- nodes.erase(nodes.begin() + (ptrdiff_t) p);
- --c;
+ htp_apply_concat_ssm_conv(nodes, std::move(cand), p, c, has_cpy, q, hoist_ins);
}
}
static void htp_fuse_mul_mat_nx_glu(const struct ggml_hexagon_session * sess, std::vector<struct htp_opnode> & nodes) {
for (size_t c = 0; c < nodes.size(); ++c) {
- if (!htp_is_fuseable_mmnx_glu_op(nodes[c].opcode) || nodes[c].inputs.size() != 2 ||
- nodes[c].outputs.size() != 1 || !nodes[c].src1()) {
+ if (!htp_is_fuseable_mmnx_glu_op(nodes[c].opcode)) {
continue;
}
@@ -1384,16 +1459,16 @@ static void htp_fuse_mul_mat_nx_glu(const struct ggml_hexagon_session * sess, st
const ggml_tensor * s1 = nodes[c].src1();
size_t p = c;
- for (size_t i = 0; i < c; ++i) {
- if (nodes[i].opcode != HTP_OP_MUL_MAT_NX || nodes[i].outputs.size() != 2 ||
- (nodes[i].inputs.size() != 3 && nodes[i].inputs.size() != 5)) {
+ for (size_t i = c; i > 0; --i) {
+ const size_t idx = i - 1;
+ if (nodes[idx].opcode != HTP_OP_MUL_MAT_NX || nodes[idx].outputs.size() != 2) {
continue;
}
- const ggml_tensor * o0 = nodes[i].outputs[0];
- const ggml_tensor * o1 = nodes[i].outputs[1];
+ const ggml_tensor * o0 = nodes[idx].outputs[0];
+ const ggml_tensor * o1 = nodes[idx].outputs[1];
if ((htp_same_tensor_data(s0, o0) && htp_same_tensor_data(s1, o1)) ||
(htp_same_tensor_data(s0, o1) && htp_same_tensor_data(s1, o0))) {
- p = i;
+ p = idx;
break;
}
}
@@ -1434,29 +1509,20 @@ static void htp_fuse_rms_norm_mul_mm(const struct ggml_hexagon_session * sess, s
continue;
}
const auto * cur_kp = nodes[c].kparams<htp_mm_kernel_params>();
- if (!cur_kp->n_hmx || cur_kp->norm_w_idx != 0) {
- continue;
- }
- if (nodes[c].inputs.size() >= HTP_OP_MAX_INPUTS) {
+ if (!cur_kp->n_hmx || cur_kp->norm_w_idx != 0 || nodes[c].inputs.size() >= HTP_OP_MAX_INPUTS) {
continue;
}
- const size_t act_idx = (op == HTP_OP_MUL_MAT_NX) ? nodes[c].outputs.size()
- : (op == HTP_OP_MUL_MAT_NX_GLU ? 2 : 1);
- if (act_idx >= nodes[c].inputs.size()) {
- continue;
- }
+ const size_t act_idx = htp_mm_act_idx(nodes[c]);
const ggml_tensor * act = nodes[c].inputs[act_idx];
- if (!act || (act->flags & GGML_TENSOR_FLAG_OUTPUT)) {
+ if (act->flags & GGML_TENSOR_FLAG_OUTPUT) {
continue;
}
size_t p = c;
for (size_t i = c; i > 0; --i) {
const size_t idx = i - 1;
- if (nodes[idx].opcode == HTP_OP_RMS_NORM_MUL &&
- nodes[idx].inputs.size() == 2 && nodes[idx].outputs.size() == 1 &&
- nodes[idx].outputs[0] == act) {
+ if (nodes[idx].opcode == HTP_OP_RMS_NORM_MUL && nodes[idx].outputs[0] == act) {
p = idx;
break;
}
@@ -1485,7 +1551,7 @@ static void htp_fuse_rms_norm_mul_mm(const struct ggml_hexagon_session * sess, s
bool out_overlap = false;
for (const ggml_tensor * out : nodes[c].outputs) {
- if (out && (htp_tensors_overlap(out, norm_x) || htp_tensors_overlap(out, norm_w))) {
+ if (htp_tensors_overlap(out, norm_x) || htp_tensors_overlap(out, norm_w)) {
out_overlap = true;
break;
}
@@ -1521,6 +1587,178 @@ static void htp_fuse_rms_norm_mul_mm(const struct ggml_hexagon_session * sess, s
}
}
+static bool htp_is_fuseable_rms_norm_glu_op(uint32_t op) {
+ return op == HTP_OP_GLU_SWIGLU || op == HTP_OP_GLU_SIGMOID ||
+ op == HTP_OP_GLU_GEGLU || op == HTP_OP_GLU_GEGLU_QUICK || op == HTP_OP_GLU_REGLU;
+}
+
+static uint32_t htp_unary_to_rms_norm_glu_op(uint32_t op) {
+ switch (op) {
+ case HTP_OP_UNARY_SILU: return HTP_OP_GLU_SWIGLU;
+ case HTP_OP_UNARY_SIGMOID: return HTP_OP_GLU_SIGMOID;
+ case HTP_OP_UNARY_GELU: return HTP_OP_GLU_GEGLU;
+ case HTP_OP_UNARY_GELU_QUICK: return HTP_OP_GLU_GEGLU_QUICK;
+ case HTP_OP_UNARY_RELU: return HTP_OP_GLU_REGLU;
+ default: return HTP_OP_INVALID;
+ }
+}
+
+static void htp_fuse_rms_norm_mul_glu(const struct ggml_hexagon_session * sess, std::vector<struct htp_opnode> & nodes) {
+ for (size_t c = 0; c < nodes.size(); ++c) {
+ if (htp_is_fuseable_rms_norm_glu_op(nodes[c].opcode) && nodes[c].inputs.size() == 2) {
+ const ggml_tensor * z = nodes[c].inputs[0];
+ const ggml_tensor * norm_out = nodes[c].inputs[1];
+ const ggml_tensor * dst = nodes[c].outputs[0];
+ if (!ggml_hexagon_tensor_is_fuseable(norm_out) || (norm_out->flags & GGML_TENSOR_FLAG_OUTPUT)) {
+ continue;
+ }
+
+ size_t p = c;
+ for (size_t i = c; i > 0; --i) {
+ const size_t idx = i - 1;
+ if (nodes[idx].opcode == HTP_OP_RMS_NORM_MUL && nodes[idx].outputs[0] == norm_out) {
+ p = idx;
+ break;
+ }
+ }
+ if (p == c) {
+ continue;
+ }
+ if (nodes[p].fused.size() + 1 + nodes[c].fused.size() > HTP_OP_MAX_FUSED) {
+ continue;
+ }
+
+ const ggml_tensor * src0 = nodes[p].inputs[0];
+ const ggml_tensor * src1 = nodes[p].inputs[1];
+ if (htp_inputs_overlap_unsafe(nodes[p], dst, z) || htp_tensors_overlap(dst, src1)) {
+ continue;
+ }
+
+ const ggml_tensor * targets[] = { norm_out };
+ const ggml_tensor * protected_t[] = { src0, src1, norm_out };
+ if (!htp_post_fuse_is_safe(nodes, p, c, targets, 1, protected_t, 3, 1u << 1)) {
+ continue;
+ }
+
+ htp_opnode cand = nodes[p];
+ cand.opcode = HTP_OP_RMS_NORM_MUL_GLU;
+ cand.inputs.push_back(z);
+ cand.outputs[0] = dst;
+ cand.fused.push_back(nodes[c].node);
+ for (const ggml_tensor * f : nodes[c].fused) {
+ cand.fused.push_back(f);
+ }
+ cand.kparams<htp_unary_kernel_params>()->glu_op = nodes[c].opcode;
+ if (!htp_kparams_solve(sess, cand)) {
+ continue;
+ }
+
+ nodes[c] = std::move(cand);
+ nodes.erase(nodes.begin() + (ptrdiff_t) p);
+ --c;
+ continue;
+ }
+
+ // Also match unfused RMS_NORM_MUL + UNARY + MUL (e.g. when strided z prevented standalone GLU fusion).
+ if (nodes[c].opcode != HTP_OP_MUL || nodes[c].inputs.size() != 2) {
+ continue;
+ }
+
+ size_t p = c;
+ size_t u = c;
+ uint32_t norm_idx = 0;
+ uint32_t act_idx = 0;
+ uint32_t glu_op = HTP_OP_INVALID;
+
+ for (uint32_t side = 0; side < 2; ++side) {
+ const ggml_tensor * t_norm = nodes[c].inputs[side];
+ const ggml_tensor * t_act = nodes[c].inputs[1 - side];
+ if (!ggml_hexagon_tensor_is_fuseable(t_norm) || (t_norm->flags & GGML_TENSOR_FLAG_OUTPUT) ||
+ !ggml_hexagon_tensor_is_fuseable(t_act) || (t_act->flags & GGML_TENSOR_FLAG_OUTPUT)) {
+ continue;
+ }
+ size_t cand_p = c;
+ size_t cand_u = c;
+ uint32_t cand_g = HTP_OP_INVALID;
+ for (size_t i = c; i > 0; --i) {
+ const size_t idx = i - 1;
+ if (cand_p == c && nodes[idx].opcode == HTP_OP_RMS_NORM_MUL && nodes[idx].outputs[0] == t_norm) {
+ cand_p = idx;
+ }
+ if (cand_u == c && nodes[idx].inputs.size() == 1 && nodes[idx].outputs[0] == t_act) {
+ const uint32_t g = htp_unary_to_rms_norm_glu_op(nodes[idx].opcode);
+ if (g != HTP_OP_INVALID && !(nodes[idx].kparams<htp_unary_kernel_params>()->flags & HTP_UNARY_FLAG_SCALE_BIAS)) {
+ cand_u = idx;
+ cand_g = g;
+ }
+ }
+ if (cand_p != c && cand_u != c) {
+ break;
+ }
+ }
+ if (cand_p != c && cand_u != c) {
+ p = cand_p;
+ u = cand_u;
+ norm_idx = side;
+ act_idx = 1 - side;
+ glu_op = cand_g;
+ break;
+ }
+ }
+ if (p == c || u == c) {
+ continue;
+ }
+ if (nodes[p].fused.size() + 1 + nodes[u].fused.size() + 1 + nodes[c].fused.size() > HTP_OP_MAX_FUSED) {
+ continue;
+ }
+
+ const ggml_tensor * src0 = nodes[p].inputs[0];
+ const ggml_tensor * src1 = nodes[p].inputs[1];
+ const ggml_tensor * norm_out = nodes[p].outputs[0];
+ const ggml_tensor * z = nodes[u].inputs[0];
+ const ggml_tensor * act_out = nodes[u].outputs[0];
+ const ggml_tensor * dst = nodes[c].outputs[0];
+ if (htp_inputs_overlap_unsafe(nodes[p], dst, z) || htp_tensors_overlap(dst, src1)) {
+ continue;
+ }
+
+ const ggml_tensor * targets_p[] = { norm_out };
+ const ggml_tensor * protected_p[] = { src0, src1, norm_out };
+ if (!htp_post_fuse_is_safe(nodes, p, c, targets_p, 1, protected_p, 3, 1u << norm_idx)) {
+ continue;
+ }
+ const ggml_tensor * targets_u[] = { act_out };
+ const ggml_tensor * protected_u[] = { z, act_out };
+ if (!htp_post_fuse_is_safe(nodes, u, c, targets_u, 1, protected_u, 2, 1u << act_idx)) {
+ continue;
+ }
+
+ htp_opnode cand = nodes[p];
+ cand.opcode = HTP_OP_RMS_NORM_MUL_GLU;
+ cand.inputs.push_back(z);
+ cand.outputs[0] = dst;
+ cand.fused.push_back(nodes[u].node);
+ for (const ggml_tensor * f : nodes[u].fused) {
+ cand.fused.push_back(f);
+ }
+ cand.fused.push_back(nodes[c].node);
+ for (const ggml_tensor * f : nodes[c].fused) {
+ cand.fused.push_back(f);
+ }
+ cand.kparams<htp_unary_kernel_params>()->glu_op = glu_op;
+ if (!htp_kparams_solve(sess, cand)) {
+ continue;
+ }
+
+ nodes[c] = std::move(cand);
+ const size_t hi = p > u ? p : u;
+ const size_t lo = p > u ? u : p;
+ nodes.erase(nodes.begin() + (ptrdiff_t) hi);
+ nodes.erase(nodes.begin() + (ptrdiff_t) lo);
+ c -= 2;
+ }
+}
+
void htp_fusion_optimize_nodes(const struct ggml_hexagon_session * sess, const struct ggml_cgraph * graph, std::vector<struct htp_opnode> & nodes) {
GGML_UNUSED(graph);
@@ -1530,6 +1768,9 @@ void htp_fusion_optimize_nodes(const struct ggml_hexagon_session * sess, const s
if (htp_fusion_enabled(HTP_FUSE_RMS_NORM_MUL_MM)) {
htp_fuse_rms_norm_mul_mm(sess, nodes);
}
+ if (htp_fusion_enabled(HTP_FUSE_RMS_NORM_MUL_GLU)) {
+ htp_fuse_rms_norm_mul_glu(sess, nodes);
+ }
if (htp_fusion_enabled(HTP_FUSE_GDN_STATE)) {
htp_fuse_gdn_state(sess, nodes);
}
diff --git a/ggml/src/ggml-hexagon/htp-fusion.h b/ggml/src/ggml-hexagon/htp-fusion.h
index e1ef620d1..f24f979af 100644
--- a/ggml/src/ggml-hexagon/htp-fusion.h
+++ b/ggml/src/ggml-hexagon/htp-fusion.h
@@ -22,6 +22,7 @@ enum htp_fusion_flags {
HTP_FUSE_CONCAT_SSM_CONV = (1 << 16), // 65536
HTP_FUSE_MUL_MAT_NX_GLU = (1 << 17), // 131072
HTP_FUSE_RMS_NORM_MUL_MM = (1 << 18), // 262144
+ HTP_FUSE_RMS_NORM_MUL_GLU = (1 << 19), // 524288
};
static inline bool htp_fusion_enabled(int flag) {
diff --git a/ggml/src/ggml-hexagon/htp-kparams.cpp b/ggml/src/ggml-hexagon/htp-kparams.cpp
index 3c7961974..e299c8a0e 100644
--- a/ggml/src/ggml-hexagon/htp-kparams.cpp
+++ b/ggml/src/ggml-hexagon/htp-kparams.cpp
@@ -613,11 +613,13 @@ static bool htp_kparams_solve_gated_delta_net(
}
}
- const uint32_t rq3 = (uint32_t) (n_seqs / q->ne[3]);
- const uint32_t rk3 = (uint32_t) (n_seqs / k->ne[3]);
- const uint32_t total_rows = (uint32_t) (H * n_seqs);
- const uint32_t n_hvx = (std::min)(htp_session_n_hvx(sess), total_rows);
- const size_t vtcm_size = htp_session_vtcm_size(sess);
+ const uint32_t mdev_count = htp_session_mdev_count(sess);
+ const uint32_t rq3 = (uint32_t) (n_seqs / q->ne[3]);
+ const uint32_t rk3 = (uint32_t) (n_seqs / k->ne[3]);
+ const uint32_t total_rows = (uint32_t) (H * n_seqs);
+ const uint32_t nrows_per_mdev = (total_rows < mdev_count) ? total_rows : (total_rows + mdev_count - 1) / mdev_count;
+ const uint32_t n_hvx = (std::min)(htp_session_n_hvx(sess), (std::max)(1u, nrows_per_mdev));
+ const size_t vtcm_size = htp_session_vtcm_size(sess);
const bool can_use_hmx = (opt_gdn_select >= 2) &&
(htp_session_n_hmx(sess) > 0) &&
@@ -626,15 +628,26 @@ static bool htp_kparams_solve_gated_delta_net(
(g->ne[0] == 1) &&
(K == 1);
+ const size_t q_elem_sz = (q->type == GGML_TYPE_F16) ? sizeof(uint16_t) : sizeof(float);
+ const size_t k_elem_sz = (k->type == GGML_TYPE_F16) ? sizeof(uint16_t) : sizeof(float);
+ const size_t v_elem_sz = (v->type == GGML_TYPE_F16) ? sizeof(uint16_t) : sizeof(float);
+ const size_t o_elem_sz = (dst->type == GGML_TYPE_F16) ? sizeof(uint16_t) : sizeof(float);
+
struct htp_gdn_hmx_vtcm_layout hmx_layout;
struct htp_gdn_vtcm_layout hvx_layout;
- uint32_t n_heads_batch = 1;
+ uint32_t n_heads_group = 1;
+ uint32_t n_groups = 1;
+ uint8_t hmx_flags = 0;
- if (can_use_hmx && htp_gdn_hmx_solve_layout(&hmx_layout, (uint32_t) S_v, HTP_GDN_CHUNK_SIZE, total_rows, vtcm_size, n_hvx, &n_heads_batch)) {
+ if (can_use_hmx && htp_gdn_hmx_solve_layout(&hmx_layout, (uint32_t) S_v, HTP_GDN_CHUNK_SIZE, nrows_per_mdev,
+ q_elem_sz, k_elem_sz, v_elem_sz, o_elem_sz,
+ vtcm_size, n_hvx, &n_heads_group, &n_groups, &hmx_flags)) {
kparams->kernel_type = HTP_GDN_KERNEL_HMX_CHUNKED;
+ kparams->hmx_flags = hmx_flags;
kparams->chunk_size = HTP_GDN_CHUNK_SIZE;
kparams->n_chunks = ((uint32_t) n_tokens + HTP_GDN_CHUNK_SIZE - 1) / HTP_GDN_CHUNK_SIZE;
- kparams->n_heads_batch = (uint16_t) n_heads_batch;
+ kparams->n_heads_group = (uint8_t) n_heads_group;
+ kparams->n_groups = (uint8_t) n_groups;
kparams->vtcm_size = (uint32_t) hmx_layout.total_bytes;
} else {
htp_gdn_vtcm_layout_build(&hvx_layout, (uint32_t) S_v, n_hvx);
@@ -642,7 +655,8 @@ static bool htp_kparams_solve_gated_delta_net(
return false;
}
kparams->kernel_type = HTP_GDN_KERNEL_HVX_RECURRENT;
- kparams->n_heads_batch = 1;
+ kparams->n_heads_group = 1;
+ kparams->n_groups = 1;
kparams->vtcm_size = (uint32_t) hvx_layout.total_bytes;
}
@@ -1821,7 +1835,7 @@ static bool htp_kparams_solve_unary_impl(
return false;
}
- const bool is_rms_norm = (op == HTP_OP_RMS_NORM || op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_RMS_NORM_MUL_ADD);
+ const bool is_rms_norm = (op == HTP_OP_RMS_NORM || op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_RMS_NORM_MUL_ADD || op == HTP_OP_RMS_NORM_MUL_GLU);
if (op == HTP_OP_HADAMARD) {
const int64_t n = src0->ne[0];
@@ -1837,13 +1851,13 @@ static bool htp_kparams_solve_unary_impl(
(dst->type != GGML_TYPE_F32 && dst->type != GGML_TYPE_F16)) {
return false;
}
- if (op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_RMS_NORM_MUL_ADD) {
+ if (op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_RMS_NORM_MUL_ADD || op == HTP_OP_RMS_NORM_MUL_GLU) {
if ((src1->type != GGML_TYPE_F32 && src1->type != GGML_TYPE_F16) ||
!ggml_is_contiguous_rows(src1) || src1->ne[0] != src0->ne[0]) {
return false;
}
}
- if (op == HTP_OP_RMS_NORM_MUL_ADD) {
+ if (op == HTP_OP_RMS_NORM_MUL_ADD || op == HTP_OP_RMS_NORM_MUL_GLU) {
if ((src2->type != GGML_TYPE_F32 && src2->type != GGML_TYPE_F16) ||
!ggml_is_contiguous_rows(src2) || !ggml_are_same_shape(src0, src2)) {
return false;
@@ -1896,16 +1910,16 @@ static bool htp_kparams_solve_unary_impl(
const size_t dst_data_row_size = dst->ne[0] * dst_elem_size;
- const uint32_t src0_nrows = (uint32_t) ggml_nrows(src0);
- const uint32_t min_rows_per_hvx = (uint32_t) (std::max<size_t>(1, (128 + dst_data_row_size - 1) / dst_data_row_size));
- const uint32_t max_hvx = (std::max)(1u, src0_nrows / min_rows_per_hvx);
- const uint32_t n_hvx = (std::min)(htp_session_n_hvx(sess), max_hvx);
- const size_t vtcm_size = htp_session_vtcm_size(sess);
+ const uint32_t mdev_count = htp_session_mdev_count(sess);
+ const uint32_t src0_nrows = (uint32_t) ggml_nrows(src0);
+ const uint32_t nrows_per_mdev = (src0_nrows < mdev_count) ? src0_nrows : (src0_nrows + mdev_count - 1) / mdev_count;
+ const uint32_t n_hvx = (std::min)(htp_session_n_hvx(sess), (std::max)(1u, nrows_per_mdev));
+ const size_t vtcm_size = htp_session_vtcm_size(sess);
kparams->n_hvx = n_hvx;
- const bool has_src1_mul = (op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_RMS_NORM_MUL_ADD);
- const bool has_src2_add = (op == HTP_OP_RMS_NORM_MUL_ADD);
+ const bool has_src1_mul = (op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_RMS_NORM_MUL_ADD || op == HTP_OP_RMS_NORM_MUL_GLU);
+ const bool has_src2 = (op == HTP_OP_RMS_NORM_MUL_ADD || op == HTP_OP_RMS_NORM_MUL_GLU);
const bool is_row_bcast = has_src1_mul && ggml_is_vector(src1);
bool broadcast_src1 = false;
if (op == HTP_OP_RMS_NORM_MUL) {
@@ -1914,7 +1928,7 @@ static bool htp_kparams_solve_unary_impl(
return false;
}
broadcast_src1 = is_row_bcast || is_dim1_bcast;
- } else if (op == HTP_OP_RMS_NORM_MUL_ADD) {
+ } else if (has_src2) {
if (!is_row_bcast && !(src1->ne[1] == src0->ne[1] && ggml_is_matrix(src1))) {
return false;
}
@@ -1925,29 +1939,37 @@ static bool htp_kparams_solve_unary_impl(
kparams->flags |= HTP_UNARY_FLAG_BCAST_SRC1;
}
+ const size_t src1_elem_size = has_src1_mul ? ggml_type_size(src1->type) : sizeof(float);
+ const size_t src2_elem_size = has_src2 ? ggml_type_size(src2->type) : sizeof(float);
const size_t src0_data_row_size = (size_t) src0->ne[0] * src0_elem_size;
+ const size_t src2_data_row_size = has_src2 ? (size_t) src2->ne[0] * src2_elem_size : 0;
const uint32_t ne01 = (uint32_t) src0->ne[1];
- const bool src0_contig = ggml_is_contiguous_1(src0) && (!has_src2_add || ggml_is_contiguous_1(src2));
+ const auto is_slice_dma = [](const ggml_tensor * t, size_t row_bytes, bool is_contig) {
+ return !is_contig && t->ne[1] > 1 && t->ne[2] > 1 && ggml_is_3d(t) &&
+ ggml_is_contiguous_to_2(t) && (row_bytes & 127) == 0;
+ };
+ const bool src0_contig = ggml_is_contiguous_1(src0);
const bool src1_contig = (has_src1_mul && !broadcast_src1) ? ggml_is_contiguous_1(src1) : true;
- const bool src0_slice_dma = !src0_contig && !has_src2_add && (!has_src1_mul || broadcast_src1) &&
- ne01 > 1 && src0->ne[2] > 1 && src0->ne[3] == 1 &&
- src0->nb[1] == src0_data_row_size && (src0_data_row_size & 127) == 0;
- const uint32_t src0_nrows_per_hvx = (src0_nrows + n_hvx - 1) / n_hvx;
+ const bool src2_contig = has_src2 ? ggml_is_contiguous_1(src2) : true;
+ const bool src0_slice_dma = (!has_src1_mul || broadcast_src1) && is_slice_dma(src0, src0_data_row_size, src0_contig);
+ const bool src2_slice_dma = has_src2 && is_slice_dma(src2, src2_data_row_size, src2_contig);
+ const bool all_contig = src0_contig && src1_contig && src2_contig;
+ const bool block_slice_dma = !all_contig && (src0_contig || src0_slice_dma) && src1_contig && (src2_contig || src2_slice_dma);
+ const uint32_t src0_nrows_per_hvx = (nrows_per_mdev + n_hvx - 1) / n_hvx;
const uint32_t min_block_rows = (uint32_t) (std::max<size_t>(1, 2048 / dst_data_row_size));
- uint32_t max_block = ((src0_contig && src1_contig) || src0_slice_dma) ? src0_nrows : ne01;
+ uint32_t max_block = (all_contig || block_slice_dma) ? src0_nrows : ne01;
if (src0_nrows_per_hvx >= 2 * min_block_rows) {
max_block = (std::min)(max_block, (std::max)(min_block_rows, src0_nrows_per_hvx / 4));
}
- if (src0_slice_dma && max_block > ne01) {
+ if (block_slice_dma && max_block > ne01) {
max_block = (max_block / ne01) * ne01;
}
if (src0_contig) { kparams->flags |= HTP_UNARY_FLAG_SRC0_CONTIG; }
if (src1_contig) { kparams->flags |= HTP_UNARY_FLAG_SRC1_CONTIG; }
+ if (src2_contig) { kparams->flags |= HTP_UNARY_FLAG_SRC2_CONTIG; }
if (src0_slice_dma) { kparams->flags |= HTP_UNARY_FLAG_SRC0_SLICE_DMA; }
-
- const size_t src1_elem_size = has_src1_mul ? ggml_type_size(src1->type) : sizeof(float);
- const size_t src2_elem_size = has_src2_add ? ggml_type_size(src2->type) : sizeof(float);
+ if (src2_slice_dma) { kparams->flags |= HTP_UNARY_FLAG_SRC2_SLICE_DMA; }
struct htp_unary_vtcm_layout L;
uint32_t col_tile = 0;
@@ -1969,6 +1991,10 @@ static bool htp_kparams_solve_unary_impl(
kparams->div_ne02 = init_fastdiv_values(src0->ne[2]);
kparams->div_ne012 = init_fastdiv_values(src0->ne[1] * src0->ne[2]);
+ if (op == HTP_OP_RMS_NORM_MUL_GLU && col_tile > 0) {
+ return false;
+ }
+
return (col_tile > 0 || vtcm_row_per_hvx > 0) && L.total_bytes <= vtcm_size;
}
@@ -2549,12 +2575,12 @@ static bool htp_kparams_solve_unary_mul_glu(
return false;
}
- const size_t dst_row_size = dst->ne[0] * elem_size;
- const uint32_t src0_nrows = ggml_nrows(src0);
- const uint32_t min_rows_per_hvx = (uint32_t) (std::max<size_t>(1, (128 + dst_row_size - 1) / dst_row_size));
- const uint32_t max_hvx = (std::max)(1u, src0_nrows / min_rows_per_hvx);
- const uint32_t n_hvx = (std::min)(htp_session_n_hvx(sess), max_hvx);
- const size_t vtcm_size = htp_session_vtcm_size(sess);
+ const uint32_t mdev_count = htp_session_mdev_count(sess);
+ const size_t dst_row_size = dst->ne[0] * elem_size;
+ const uint32_t src0_nrows = (uint32_t) ggml_nrows(src0);
+ const uint32_t nrows_per_mdev = (src0_nrows < mdev_count) ? src0_nrows : (src0_nrows + mdev_count - 1) / mdev_count;
+ const uint32_t n_hvx = (std::min)(htp_session_n_hvx(sess), (std::max)(1u, nrows_per_mdev));
+ const size_t vtcm_size = htp_session_vtcm_size(sess);
const size_t row_size_aligned = hex_round_up(dst_row_size, 128);
uint32_t vtcm_row_per_hvx = (uint32_t) (vtcm_size / (n_hvx * 3 * row_size_aligned));
@@ -2563,7 +2589,7 @@ static bool htp_kparams_solve_unary_mul_glu(
return false;
}
- const uint32_t src0_nrows_per_hvx = (src0_nrows + n_hvx - 1) / n_hvx;
+ const uint32_t src0_nrows_per_hvx = (nrows_per_mdev + n_hvx - 1) / n_hvx;
if (src0_nrows_per_hvx >= 2 && vtcm_row_per_hvx > src0_nrows_per_hvx) {
uint32_t target_rpt = (src0_nrows_per_hvx >= 4) ? (src0_nrows_per_hvx / 2) & ~1u : 2;
vtcm_row_per_hvx = (std::max)(2u, (std::min)(vtcm_row_per_hvx, target_rpt));
@@ -6092,6 +6118,7 @@ static bool htp_kparams_solve_unary_node(
const float in_bias = kp->in_bias;
const float out_scale = kp->out_scale;
const float out_bias = kp->out_bias;
+ const uint32_t glu_op = kp->glu_op;
const ggml_tensor * src0 = tmp.inputs.empty() ? nullptr : tmp.inputs[0];
const ggml_tensor * src1 = tmp.inputs.size() > 1 ? tmp.inputs[1] : nullptr;
const ggml_tensor * src2 = tmp.inputs.size() > 2 ? tmp.inputs[2] : nullptr;
@@ -6103,6 +6130,9 @@ static bool htp_kparams_solve_unary_node(
kp->out_scale = out_scale;
kp->out_bias = out_bias;
}
+ if (tmp.opcode == HTP_OP_RMS_NORM_MUL_GLU) {
+ kp->glu_op = glu_op;
+ }
return ok;
}
diff --git a/ggml/src/ggml-hexagon/htp-lower.cpp b/ggml/src/ggml-hexagon/htp-lower.cpp
index ed4913813..a326c3311 100644
--- a/ggml/src/ggml-hexagon/htp-lower.cpp
+++ b/ggml/src/ggml-hexagon/htp-lower.cpp
@@ -39,9 +39,6 @@ bool htp_matmul_add_can_collapse(
if (!htp_matmul_can_collapse(src0, src1, dst)) {
return false;
}
- if (!src2) {
- return true;
- }
const int64_t src2_nrows = src2->ne[1] * src2->ne[2] * src2->ne[3];
if (src2_nrows == 1) {
return true;
@@ -67,11 +64,8 @@ ggml_tensor htp_tensor_collapse_rows(const struct ggml_tensor * t) {
// Collapse contiguous dims of elementwise ops so that rows fill at least one HVX vector
static void htp_collapse_dims(struct ggml_tensor ** tensors, size_t n_tensors, size_t elem_size, uint32_t n_hvx) {
- if (n_tensors == 0 || !tensors[0]) {
- return;
- }
for (size_t t = 0; t < n_tensors; t++) {
- if (!tensors[t] || tensors[t]->nb[0] != elem_size) {
+ if (tensors[t]->nb[0] != elem_size) {
return;
}
if (t > 0 && !ggml_are_same_shape(tensors[0], tensors[t])) {
@@ -346,32 +340,37 @@ void htp_dsv4_hc_post_collapse_tensors(
}
}
-bool htp_lightning_indexer_can_collapse(
+static bool htp_lightning_indexer_can_collapse_stream(
const struct ggml_tensor * q,
const struct ggml_tensor * k,
const struct ggml_tensor * w,
const struct ggml_tensor * m,
const struct ggml_tensor * dst
) {
- // 2D K normalization: ne[1] is n_kv, ne[2] is 1
- const bool can_collapse_k = (k->ne[1] > 1 && k->ne[2] == 1);
-
- // Stream-to-token collapse across q, w, dst (and m)
- bool can_collapse_stream = false;
- if (q->ne[3] > 1 && k->ne[3] == 1) {
- const bool q_ok = (q->nb[3] == (size_t) q->ne[2] * q->nb[2]);
- const bool dst_ok = (dst->ne[3] == q->ne[3] && dst->ne[2] == 1 && dst->ne[1] == q->ne[2] &&
- dst->nb[3] == (size_t) dst->ne[1] * dst->nb[1]);
- const bool w_ok = (w->ne[3] == q->ne[3] && w->ne[2] == 1 && w->ne[1] == q->ne[2] &&
- w->nb[3] == (size_t) w->ne[1] * w->nb[1]) ||
- (w->ne[3] == 1 && w->ne[1] == q->ne[2] * q->ne[3]);
- const bool m_ok = (m->ne[3] == q->ne[3] && m->ne[2] == 1 && m->ne[1] == q->ne[2] &&
- m->nb[3] == (size_t) m->ne[1] * m->nb[1]) ||
- (m->ne[3] == 1 && m->ne[1] == q->ne[2] * q->ne[3]);
- can_collapse_stream = q_ok && dst_ok && w_ok && m_ok;
+ if (q->ne[3] <= 1 || k->ne[3] != 1) {
+ return false;
}
+ const bool q_ok = (q->nb[3] == (size_t) q->ne[2] * q->nb[2]);
+ const bool dst_ok = (dst->ne[3] == q->ne[3] && dst->ne[2] == 1 && dst->ne[1] == q->ne[2] &&
+ dst->nb[3] == (size_t) dst->ne[1] * dst->nb[1]);
+ const bool w_ok = (w->ne[3] == q->ne[3] && w->ne[2] == 1 && w->ne[1] == q->ne[2] &&
+ w->nb[3] == (size_t) w->ne[1] * w->nb[1]) ||
+ (w->ne[3] == 1 && w->ne[1] == q->ne[2] * q->ne[3]);
+ const bool m_ok = (m->ne[3] == q->ne[3] && m->ne[2] == 1 && m->ne[1] == q->ne[2] &&
+ m->nb[3] == (size_t) m->ne[1] * m->nb[1]) ||
+ (m->ne[3] == 1 && m->ne[1] == q->ne[2] * q->ne[3]);
+ return q_ok && dst_ok && w_ok && m_ok;
+}
- return can_collapse_k || can_collapse_stream;
+bool htp_lightning_indexer_can_collapse(
+ const struct ggml_tensor * q,
+ const struct ggml_tensor * k,
+ const struct ggml_tensor * w,
+ const struct ggml_tensor * m,
+ const struct ggml_tensor * dst
+) {
+ const bool can_collapse_k = (k->ne[1] > 1 && k->ne[2] == 1);
+ return can_collapse_k || htp_lightning_indexer_can_collapse_stream(q, k, w, m, dst);
}
void htp_lightning_indexer_collapse_tensors(
@@ -400,7 +399,7 @@ void htp_lightning_indexer_collapse_tensors(
k_c->nb[3] = k->nb[2];
}
- if (q->ne[3] > 1 && k->ne[3] == 1) {
+ if (htp_lightning_indexer_can_collapse_stream(q, k, w, m, dst)) {
const int64_t n_tokens_collapsed = (int64_t) q->ne[2] * q->ne[3];
q_c->ne[2] = n_tokens_collapsed;
@@ -439,58 +438,56 @@ static void htp_collapse_opnode_tensors(
const size_t n_in = node.inputs.size();
const size_t n_out = node.outputs.size();
+ auto collapse_elemwise = [&](struct ggml_tensor ** tensors, size_t n_t) -> bool {
+ const int64_t old_ne0 = tensors[0]->ne[0];
+ const int64_t old_ne1 = tensors[0]->ne[1];
+ htp_collapse_dims(tensors, n_t, ggml_type_size(tensors[0]->type), n_hvx);
+ return tensors[0]->ne[0] != old_ne0 || tensors[0]->ne[1] != old_ne1;
+ };
+
if (htp_op_is_unary(node.opcode) && htp_unary_is_elementwise(node.opcode)) {
- if (n_in >= 1 && n_out >= 1 &&
- ggml_are_same_shape(&in_mat[0], &out_mat[0])) {
- const int64_t old_ne0 = in_mat[0].ne[0];
- const int64_t old_ne1 = in_mat[0].ne[1];
- const size_t elem_size = ggml_type_size(in_mat[0].type);
- struct ggml_tensor * tensors[2] = { &in_mat[0], &out_mat[0] };
- htp_collapse_dims(tensors, 2, elem_size, n_hvx);
- if (in_mat[0].ne[0] != old_ne0 || in_mat[0].ne[1] != old_ne1) {
- in_dirty[0] = true;
- out_dirty[0] = true;
- }
- }
- } else if (htp_op_is_glu(node.opcode)) {
- if (n_in >= 2 && n_out >= 1 &&
- ggml_are_same_shape(&in_mat[0], &out_mat[0]) &&
- ggml_are_same_shape(&in_mat[1], &out_mat[0])) {
- const int64_t old_ne0 = in_mat[0].ne[0];
- const int64_t old_ne1 = in_mat[0].ne[1];
- const size_t elem_size = ggml_type_size(in_mat[0].type);
- struct ggml_tensor * tensors[3] = { &in_mat[0], &in_mat[1], &out_mat[0] };
- htp_collapse_dims(tensors, 3, elem_size, n_hvx);
- if (in_mat[0].ne[0] != old_ne0 || in_mat[0].ne[1] != old_ne1) {
- in_dirty[0] = true;
- in_dirty[1] = true;
- out_dirty[0] = true;
- }
- }
- } else if (node.opcode == HTP_OP_MUL_MAT) {
- if (n_in >= 2 && n_out >= 1 &&
- htp_matmul_can_collapse(&in_mat[0], &in_mat[1], &out_mat[0])) {
- in_mat[1] = htp_tensor_collapse_rows(&in_mat[1]);
- out_mat[0] = htp_tensor_collapse_rows(&out_mat[0]);
- in_dirty[1] = true;
+ struct ggml_tensor * tensors[2] = { &in_mat[0], &out_mat[0] };
+ if (ggml_are_same_shape(&in_mat[0], &out_mat[0]) && collapse_elemwise(tensors, 2)) {
+ in_dirty[0] = true;
out_dirty[0] = true;
}
- } else if (node.opcode == HTP_OP_MUL_MAT_ADD) {
- if (n_in >= 3 && n_out >= 1 &&
- htp_matmul_add_can_collapse(&in_mat[0], &in_mat[1], &in_mat[2], &out_mat[0])) {
- in_mat[1] = htp_tensor_collapse_rows(&in_mat[1]);
- out_mat[0] = htp_tensor_collapse_rows(&out_mat[0]);
+ return;
+ }
+ if (htp_op_is_glu(node.opcode)) {
+ struct ggml_tensor * tensors[3] = { &in_mat[0], &in_mat[1], &out_mat[0] };
+ if (ggml_are_same_shape(&in_mat[0], &out_mat[0]) &&
+ ggml_are_same_shape(&in_mat[1], &out_mat[0]) &&
+ collapse_elemwise(tensors, 3)) {
+ in_dirty[0] = true;
in_dirty[1] = true;
out_dirty[0] = true;
- if (in_mat[2].ne[1] * in_mat[2].ne[2] * in_mat[2].ne[3] > 1) {
- in_mat[2] = htp_tensor_collapse_rows(&in_mat[2]);
- in_dirty[2] = true;
- }
}
- } else if (node.opcode == HTP_OP_MUL_MAT_NX) {
- const size_t n_w = n_out;
- const size_t norm_extra = (node.kparams<htp_mm_kernel_params>()->norm_w_idx != 0) ? 1 : 0;
- if (n_w > 0 && (n_in == n_w + 1 + norm_extra || n_in == 2 * n_w + 1 + norm_extra)) {
+ return;
+ }
+
+ switch (node.opcode) {
+ case HTP_OP_MUL_MAT:
+ if (htp_matmul_can_collapse(&in_mat[0], &in_mat[1], &out_mat[0])) {
+ in_mat[1] = htp_tensor_collapse_rows(&in_mat[1]);
+ out_mat[0] = htp_tensor_collapse_rows(&out_mat[0]);
+ in_dirty[1] = true;
+ out_dirty[0] = true;
+ }
+ break;
+ case HTP_OP_MUL_MAT_ADD:
+ if (htp_matmul_add_can_collapse(&in_mat[0], &in_mat[1], &in_mat[2], &out_mat[0])) {
+ in_mat[1] = htp_tensor_collapse_rows(&in_mat[1]);
+ out_mat[0] = htp_tensor_collapse_rows(&out_mat[0]);
+ in_dirty[1] = true;
+ out_dirty[0] = true;
+ if (in_mat[2].ne[1] * in_mat[2].ne[2] * in_mat[2].ne[3] > 1) {
+ in_mat[2] = htp_tensor_collapse_rows(&in_mat[2]);
+ in_dirty[2] = true;
+ }
+ }
+ break;
+ case HTP_OP_MUL_MAT_NX: {
+ const size_t n_w = n_out;
bool can_collapse = true;
for (size_t i = 0; i < n_w; ++i) {
if (!htp_matmul_can_collapse(&in_mat[i], &in_mat[n_w], &out_mat[i])) {
@@ -506,19 +503,18 @@ static void htp_collapse_opnode_tensors(
out_dirty[i] = true;
}
}
+ break;
}
- } else if (node.opcode == HTP_OP_MUL_MAT_NX_GLU) {
- const size_t norm_extra = (node.kparams<htp_mm_kernel_params>()->norm_w_idx != 0) ? 1 : 0;
- if ((n_in == 3 + norm_extra || n_in == 5 + norm_extra) && n_out == 1 &&
- htp_matmul_can_collapse(&in_mat[0], &in_mat[2], &out_mat[0]) &&
- htp_matmul_can_collapse(&in_mat[1], &in_mat[2], &out_mat[0])) {
- in_mat[2] = htp_tensor_collapse_rows(&in_mat[2]);
- in_dirty[2] = true;
- out_mat[0] = htp_tensor_collapse_rows(&out_mat[0]);
- out_dirty[0] = true;
- }
- } else if (node.opcode == HTP_OP_UPSCALE) {
- if (n_in >= 1 && n_out >= 1) {
+ case HTP_OP_MUL_MAT_NX_GLU:
+ if (htp_matmul_can_collapse(&in_mat[0], &in_mat[2], &out_mat[0]) &&
+ htp_matmul_can_collapse(&in_mat[1], &in_mat[2], &out_mat[0])) {
+ in_mat[2] = htp_tensor_collapse_rows(&in_mat[2]);
+ out_mat[0] = htp_tensor_collapse_rows(&out_mat[0]);
+ in_dirty[2] = true;
+ out_dirty[0] = true;
+ }
+ break;
+ case HTP_OP_UPSCALE:
if ((in_mat[0].ne[3] > 1 || out_mat[0].ne[3] > 1) &&
in_mat[0].ne[2] == out_mat[0].ne[2] && in_mat[0].ne[3] == out_mat[0].ne[3] &&
in_mat[0].nb[3] == (size_t) in_mat[0].ne[2] * in_mat[0].nb[2] &&
@@ -532,21 +528,20 @@ static void htp_collapse_opnode_tensors(
in_dirty[0] = true;
out_dirty[0] = true;
}
- }
- } else if (node.opcode == HTP_OP_DSV4_HC_PRE) {
- if (n_in >= 2 && n_out >= 1 &&
- htp_dsv4_hc_pre_can_collapse(&in_mat[0], &in_mat[1], &out_mat[0])) {
- struct ggml_tensor x_c, w_c, d_c;
- htp_dsv4_hc_pre_collapse_tensors(&in_mat[0], &in_mat[1], &out_mat[0], &x_c, &w_c, &d_c);
- in_mat[0] = x_c;
- in_mat[1] = w_c;
- out_mat[0] = d_c;
- in_dirty[0] = true;
- in_dirty[1] = true;
- out_dirty[0] = true;
- }
- } else if (node.opcode == HTP_OP_DSV4_HC_POST) {
- if (n_in >= 3 && n_out >= 1) {
+ break;
+ case HTP_OP_DSV4_HC_PRE:
+ if (htp_dsv4_hc_pre_can_collapse(&in_mat[0], &in_mat[1], &out_mat[0])) {
+ struct ggml_tensor x_c, w_c, d_c;
+ htp_dsv4_hc_pre_collapse_tensors(&in_mat[0], &in_mat[1], &out_mat[0], &x_c, &w_c, &d_c);
+ in_mat[0] = x_c;
+ in_mat[1] = w_c;
+ out_mat[0] = d_c;
+ in_dirty[0] = true;
+ in_dirty[1] = true;
+ out_dirty[0] = true;
+ }
+ break;
+ case HTP_OP_DSV4_HC_POST: {
const struct ggml_tensor * comb = (n_in > 3 && node.inputs[3]) ? &in_mat[3] : nullptr;
if (htp_dsv4_hc_post_can_collapse(&in_mat[0], &in_mat[1], &in_mat[2], comb, &out_mat[0])) {
struct ggml_tensor x_c, r_c, p_c, c_c, d_c;
@@ -565,24 +560,27 @@ static void htp_collapse_opnode_tensors(
in_dirty[3] = true;
}
}
+ break;
}
- } else if (node.opcode == HTP_OP_LIGHTNING_INDEXER) {
- if (n_in >= 4 && n_out >= 1 &&
- htp_lightning_indexer_can_collapse(&in_mat[0], &in_mat[1], &in_mat[2], &in_mat[3], &out_mat[0])) {
- struct ggml_tensor q_c, k_c, w_c, m_c, d_c;
- htp_lightning_indexer_collapse_tensors(&in_mat[0], &in_mat[1], &in_mat[2], &in_mat[3], &out_mat[0],
- &q_c, &k_c, &w_c, &m_c, &d_c);
- in_mat[0] = q_c;
- in_mat[1] = k_c;
- in_mat[2] = w_c;
- in_mat[3] = m_c;
- out_mat[0] = d_c;
- in_dirty[0] = true;
- in_dirty[1] = true;
- in_dirty[2] = true;
- in_dirty[3] = true;
- out_dirty[0] = true;
- }
+ case HTP_OP_LIGHTNING_INDEXER:
+ if (htp_lightning_indexer_can_collapse(&in_mat[0], &in_mat[1], &in_mat[2], &in_mat[3], &out_mat[0])) {
+ struct ggml_tensor q_c, k_c, w_c, m_c, d_c;
+ htp_lightning_indexer_collapse_tensors(&in_mat[0], &in_mat[1], &in_mat[2], &in_mat[3], &out_mat[0],
+ &q_c, &k_c, &w_c, &m_c, &d_c);
+ in_mat[0] = q_c;
+ in_mat[1] = k_c;
+ in_mat[2] = w_c;
+ in_mat[3] = m_c;
+ out_mat[0] = d_c;
+ in_dirty[0] = true;
+ in_dirty[1] = true;
+ in_dirty[2] = true;
+ in_dirty[3] = true;
+ out_dirty[0] = true;
+ }
+ break;
+ default:
+ break;
}
}
@@ -710,14 +708,18 @@ static bool htp_lower_inplace_types_ok(const htp_opnode & node) {
continue;
}
for (const ggml_tensor * out : node.outputs) {
- if (!out) {
- continue;
- }
const ggml_type out_type = htp_effective_type(out);
if (out_type != GGML_TYPE_F32 && out_type != GGML_TYPE_F16) {
continue;
}
- if (inp_type != out_type && htp_tensors_overlap(inp, out)) {
+ if (!htp_tensors_overlap(inp, out)) {
+ continue;
+ }
+ if (inp_type != out_type) {
+ return false;
+ }
+ // Reject exact in-place pairs whose lowered view offsets diverged
+ if (inp->data == out->data && htp_tensor_data_ptr(inp) != htp_tensor_data_ptr(out)) {
return false;
}
}
@@ -731,19 +733,22 @@ static bool htp_lower_is_mm_opcode(uint32_t opcode) {
opcode == HTP_OP_MUL_MAT_NX_GLU || opcode == HTP_OP_MUL_MAT_ID_NX;
}
-static const ggml_tensor * htp_lower_orig_mm_for_output(const htp_opnode & node, const ggml_tensor * tensor, uint32_t * out_w_idx) {
- if (out_w_idx) {
- *out_w_idx = 0;
+static size_t htp_lower_mm_act_idx(const htp_opnode & node) {
+ if (node.opcode == HTP_OP_MUL_MAT_NX || node.opcode == HTP_OP_MUL_MAT_ID_NX) {
+ return node.outputs.size();
}
+ return (node.opcode == HTP_OP_MUL_MAT_NX_GLU) ? 2 : 1;
+}
+
+static const ggml_tensor * htp_lower_orig_mm_for_output(const htp_opnode & node, const ggml_tensor * tensor, uint32_t * out_w_idx) {
+ *out_w_idx = 0;
if (!htp_lower_is_mm_opcode(node.opcode)) {
return nullptr;
}
if (node.opcode == HTP_OP_MUL_MAT_NX || node.opcode == HTP_OP_MUL_MAT_ID_NX) {
for (size_t p = 0; p < node.outputs.size(); ++p) {
if (node.outputs[p] == tensor) {
- if (out_w_idx) {
- *out_w_idx = (uint32_t) p;
- }
+ *out_w_idx = (uint32_t) p;
if (p == 0) {
return node.node;
}
@@ -770,191 +775,165 @@ static bool htp_lower_mm_blocks_output_f16(const htp_opnode & owner, const ggml_
return src1_prec == GGML_PREC_BF16 || src1_prec == GGML_PREC_F32;
}
-static bool htp_lower_respect_prec_hints(
- const htp_opnode & owner,
- const ggml_tensor * r,
- const std::vector<const ggml_tensor *> & cls,
- const std::vector<htp_opnode> & nodes,
- const std::unordered_map<const ggml_tensor *, size_t> & owner_idx,
- const std::unordered_map<const ggml_tensor *, std::vector<size_t>> & root_users
-) {
- if (htp_lower_is_mm_opcode(owner.opcode)) {
- uint32_t w_idx = 0;
- const ggml_tensor * orig_mm = htp_lower_orig_mm_for_output(owner, r, &w_idx);
- if (owner.opcode == HTP_OP_MUL_MAT_NX_GLU && orig_mm && owner.inputs.size() >= 2 && orig_mm->src[0] == owner.inputs[1]) {
- w_idx = 1;
- }
- if (htp_lower_mm_blocks_output_f16(owner, orig_mm, w_idx)) {
+static bool htp_lower_is_seed_owner(const htp_opnode & owner, const ggml_tensor * root) {
+ switch (owner.opcode) {
+ case HTP_OP_MUL_MAT:
+ case HTP_OP_MUL_MAT_ADD:
+ case HTP_OP_MUL_MAT_ID:
+ case HTP_OP_MUL_MAT_NX:
+ case HTP_OP_MUL_MAT_NX_GLU:
+ case HTP_OP_MUL_MAT_ID_NX:
+ case HTP_OP_FLASH_ATTN_EXT:
+ case HTP_OP_RMS_NORM:
+ case HTP_OP_RMS_NORM_MUL:
+ case HTP_OP_RMS_NORM_MUL_ADD:
+ case HTP_OP_RMS_NORM_MUL_GLU:
+ case HTP_OP_REPEAT:
+ return true;
+ case HTP_OP_SSM_CONV:
+ return owner.outputs[0] == root;
+ case HTP_OP_GET_ROWS:
+ return owner.inputs[0]->type != GGML_TYPE_F32 || ggml_hexagon_tensor_is_tiled(owner.inputs[0]);
+ default:
return false;
- }
- if (owner.opcode == HTP_OP_MUL_MAT_NX_GLU) {
- for (const ggml_tensor * f : owner.fused) {
- if (f && f->op == GGML_OP_MUL_MAT) {
- const uint32_t f_idx = (owner.inputs.size() >= 2 && f->src[0] == owner.inputs[1]) ? 1u : 0u;
- if (htp_lower_mm_blocks_output_f16(owner, f, f_idx)) {
- return false;
+ }
+}
+
+struct htp_lower_ctx {
+ std::vector<htp_opnode> & nodes;
+ std::vector<const ggml_tensor *> roots;
+ std::unordered_map<const ggml_tensor *, std::vector<const ggml_tensor *>> classes;
+ std::unordered_map<const ggml_tensor *, size_t> owner_idx;
+ std::unordered_map<const ggml_tensor *, std::vector<size_t>> root_users;
+ std::unordered_map<const ggml_tensor *, bool> root_safe_class;
+
+ htp_opnode * find_owner(const ggml_tensor * r) {
+ auto it = owner_idx.find(r);
+ return (it != owner_idx.end()) ? &nodes[it->second] : nullptr;
+ }
+
+ const htp_opnode * find_owner(const ggml_tensor * r) const {
+ auto it = owner_idx.find(r);
+ return (it != owner_idx.end()) ? &nodes[it->second] : nullptr;
+ }
+
+ bool respect_prec_hints(const htp_opnode & owner, const ggml_tensor * r) const {
+ if (htp_lower_is_mm_opcode(owner.opcode)) {
+ uint32_t w_idx = 0;
+ const ggml_tensor * orig_mm = htp_lower_orig_mm_for_output(owner, r, &w_idx);
+ if (owner.opcode == HTP_OP_MUL_MAT_NX_GLU && orig_mm && orig_mm->src[0] == owner.inputs[1]) {
+ w_idx = 1;
+ }
+ if (htp_lower_mm_blocks_output_f16(owner, orig_mm, w_idx)) {
+ return false;
+ }
+ if (owner.opcode == HTP_OP_MUL_MAT_NX_GLU) {
+ for (const ggml_tensor * f : owner.fused) {
+ if (f->op == GGML_OP_MUL_MAT) {
+ const uint32_t f_idx = (f->src[0] == owner.inputs[1]) ? 1u : 0u;
+ if (htp_lower_mm_blocks_output_f16(owner, f, f_idx)) {
+ return false;
+ }
}
}
}
}
- }
- if (owner.opcode == HTP_OP_SOFTMAX && !owner.inputs.empty() && owner.inputs[0]) {
- const ggml_tensor * in_root = htp_tensor_root(owner.inputs[0]);
- auto oit = owner_idx.find(in_root);
- if (oit != owner_idx.end()) {
- const htp_opnode & node = nodes[oit->second];
- if (htp_lower_is_mm_opcode(node.opcode)) {
- uint32_t w_idx = 0;
- const ggml_tensor * orig_mm = htp_lower_orig_mm_for_output(node, in_root, &w_idx);
- if (htp_lower_mm_blocks_output_f16(node, orig_mm, w_idx)) {
- return false;
+ if (owner.opcode == HTP_OP_SOFTMAX) {
+ const ggml_tensor * in_root = htp_tensor_root(owner.inputs[0]);
+ if (const htp_opnode * src_owner = find_owner(in_root)) {
+ if (htp_lower_is_mm_opcode(src_owner->opcode)) {
+ uint32_t w_idx = 0;
+ const ggml_tensor * orig_mm = htp_lower_orig_mm_for_output(*src_owner, in_root, &w_idx);
+ if (htp_lower_mm_blocks_output_f16(*src_owner, orig_mm, w_idx)) {
+ return false;
+ }
}
}
}
- }
- auto uit = root_users.find(r);
- if (uit == root_users.end()) {
- return true;
- }
- for (size_t idx : uit->second) {
- const htp_opnode & node = nodes[idx];
- if (!htp_lower_is_mm_opcode(node.opcode)) {
- continue;
- }
- const size_t act_idx = (node.opcode == HTP_OP_MUL_MAT_NX || node.opcode == HTP_OP_MUL_MAT_ID_NX)
- ? node.outputs.size()
- : (node.opcode == HTP_OP_MUL_MAT_NX_GLU ? 2 : 1);
- if (act_idx >= node.inputs.size()) {
- continue;
- }
- const ggml_tensor * act = node.inputs[act_idx];
- bool uses_cls = false;
- for (const ggml_tensor * tensor : cls) {
- if (act == tensor) {
- uses_cls = true;
- break;
- }
- }
- if (!uses_cls && (!act || htp_tensor_root(act) != r)) {
- continue;
+ auto uit = root_users.find(r);
+ if (uit == root_users.end()) {
+ return true;
}
auto blocks_src1 = [](const ggml_tensor * orig_mm) -> bool {
- if (!orig_mm) {
- return false;
- }
const ggml_prec src1_prec = ggml_prec_get_src(orig_mm, 1);
return src1_prec == GGML_PREC_BF16 || src1_prec == GGML_PREC_F32;
};
- if (blocks_src1(node.node)) {
+ for (size_t idx : uit->second) {
+ const htp_opnode & node = nodes[idx];
+ if (!htp_lower_is_mm_opcode(node.opcode)) {
+ continue;
+ }
+ const ggml_tensor * act = node.inputs[htp_lower_mm_act_idx(node)];
+ if (htp_tensor_root(act) != r) {
+ continue;
+ }
+ if (node.node && blocks_src1(node.node)) {
+ return false;
+ }
+ if (node.opcode == HTP_OP_MUL_MAT_NX || node.opcode == HTP_OP_MUL_MAT_NX_GLU || node.opcode == HTP_OP_MUL_MAT_ID_NX) {
+ for (const ggml_tensor * f : node.fused) {
+ if ((f->op == GGML_OP_MUL_MAT || f->op == GGML_OP_MUL_MAT_ID) && blocks_src1(f)) {
+ return false;
+ }
+ }
+ }
+ }
+ return true;
+ }
+
+ bool safe_gdn_output(const htp_opnode & owner, const ggml_tensor * root) const {
+ if (owner.outputs.size() != 2 || root != owner.outputs[0]) {
return false;
}
- if (node.opcode == HTP_OP_MUL_MAT_NX || node.opcode == HTP_OP_MUL_MAT_NX_GLU || node.opcode == HTP_OP_MUL_MAT_ID_NX) {
- for (const ggml_tensor * f : node.fused) {
- if (f && (f->op == GGML_OP_MUL_MAT || f->op == GGML_OP_MUL_MAT_ID) && blocks_src1(f)) {
+ auto uit = root_users.find(root);
+ if (uit == root_users.end()) {
+ return false;
+ }
+ const ggml_tensor * v = owner.inputs[2];
+ const size_t tail_off = (size_t) v->ne[0] * v->ne[1] * v->ne[2] * v->ne[3] * sizeof(float);
+ bool has_consumer = false;
+ for (size_t idx : uit->second) {
+ for (const ggml_tensor * input : nodes[idx].inputs) {
+ if (!input || htp_tensor_root(input) != root) {
+ continue;
+ }
+ const ptrdiff_t offset = (const uint8_t *) input->data - (const uint8_t *) root->data;
+ if (offset < 0 || (size_t) offset + ggml_nbytes(input) > tail_off) {
return false;
}
+ has_consumer = true;
}
}
+ return has_consumer;
}
- return true;
-}
-
-static bool htp_lower_is_mm_root(
- const std::vector<htp_opnode> & nodes,
- const std::unordered_map<const ggml_tensor *, size_t> & owner_idx,
- const ggml_tensor * root
-) {
- auto it = owner_idx.find(root);
- return it != owner_idx.end() && htp_lower_is_mm_opcode(nodes[it->second].opcode);
-}
-
-static bool htp_lower_is_fa_root(
- const std::vector<htp_opnode> & nodes,
- const std::unordered_map<const ggml_tensor *, size_t> & owner_idx,
- const ggml_tensor * root
-) {
- auto it = owner_idx.find(root);
- return it != owner_idx.end() && nodes[it->second].opcode == HTP_OP_FLASH_ATTN_EXT;
-}
-
-static bool htp_lower_is_rms_root(
- const std::vector<htp_opnode> & nodes,
- const std::unordered_map<const ggml_tensor *, size_t> & owner_idx,
- const ggml_tensor * root
-) {
- auto it = owner_idx.find(root);
- if (it == owner_idx.end()) {
- return false;
- }
- const uint32_t opcode = nodes[it->second].opcode;
- return opcode == HTP_OP_RMS_NORM || opcode == HTP_OP_RMS_NORM_MUL ||
- opcode == HTP_OP_RMS_NORM_MUL_ADD;
-}
-
-static bool htp_lower_is_ssm_conv_root(
- const std::vector<htp_opnode> & nodes,
- const std::unordered_map<const ggml_tensor *, size_t> & owner_idx,
- const ggml_tensor * root
-) {
- auto it = owner_idx.find(root);
- return it != owner_idx.end() && nodes[it->second].opcode == HTP_OP_SSM_CONV &&
- !nodes[it->second].outputs.empty() && nodes[it->second].outputs[0] == root;
-}
-static bool htp_lower_is_get_rows_root(
- const std::vector<htp_opnode> & nodes,
- const std::unordered_map<const ggml_tensor *, size_t> & owner_idx,
- const ggml_tensor * root
-) {
- auto it = owner_idx.find(root);
- if (it == owner_idx.end()) {
- return false;
- }
- const htp_opnode & owner = nodes[it->second];
- return owner.opcode == HTP_OP_GET_ROWS && !owner.inputs.empty() && owner.inputs[0] &&
- (owner.inputs[0]->type != GGML_TYPE_F32 || ggml_hexagon_tensor_is_tiled(owner.inputs[0]));
-}
-
-static bool htp_lower_is_repeat_root(
- const std::vector<htp_opnode> & nodes,
- const std::unordered_map<const ggml_tensor *, size_t> & owner_idx,
- const ggml_tensor * root
-) {
- auto it = owner_idx.find(root);
- return it != owner_idx.end() && nodes[it->second].opcode == HTP_OP_REPEAT;
-}
-
-static bool htp_lower_safe_gdn_output(
- const htp_opnode & owner,
- const ggml_tensor * root,
- const std::vector<htp_opnode> & nodes,
- const std::unordered_map<const ggml_tensor *, std::vector<size_t>> & root_users
-) {
- if (owner.outputs.size() != 2 || root != owner.outputs[0] || owner.inputs.size() < 3) {
- return false;
- }
- auto uit = root_users.find(root);
- if (uit == root_users.end()) {
- return false;
- }
- const ggml_tensor * v = owner.inputs[2];
- const size_t tail_off = (size_t) v->ne[0] * v->ne[1] * v->ne[2] * v->ne[3] * sizeof(float);
- bool has_consumer = false;
- for (size_t idx : uit->second) {
- const htp_opnode & node = nodes[idx];
- for (const ggml_tensor * input : node.inputs) {
- if (!input || htp_tensor_root(input) != root) {
- continue;
- }
- const ptrdiff_t offset = (const uint8_t *) input->data - (const uint8_t *) root->data;
- if (offset < 0 || (size_t) offset + ggml_nbytes(input) > tail_off) {
- return false;
- }
- has_consumer = true;
+ bool can_lower(const ggml_tensor * r, const htp_opnode ** out_owner = nullptr) const {
+ if (ggml_hexagon_tensor_is_lowered_f16(r)) {
+ return false;
+ }
+ auto sit = root_safe_class.find(r);
+ if (sit == root_safe_class.end() || !sit->second) {
+ return false;
+ }
+ const htp_opnode * owner = find_owner(r);
+ if (!owner) {
+ return false;
+ }
+ if (!respect_prec_hints(*owner, r)) {
+ return false;
+ }
+ if (owner->opcode == HTP_OP_GATED_DELTA_NET && !safe_gdn_output(*owner, r)) {
+ return false;
}
+ if (owner->opcode == HTP_OP_SSM_CONV && r != owner->outputs[0]) {
+ return false;
+ }
+ if (out_owner) {
+ *out_owner = owner;
+ }
+ return true;
}
- return has_consumer;
-}
+};
void htp_lower_reset_graph(const struct ggml_cgraph * graph) {
for (int i = 0; i < graph->n_nodes; ++i) {
@@ -1012,8 +991,7 @@ void htp_lower_graph(const struct ggml_hexagon_session * sess, const struct ggml
return;
}
- std::vector<const ggml_tensor *> roots;
- std::unordered_map<const ggml_tensor *, std::vector<const ggml_tensor *>> classes;
+ htp_lower_ctx ctx{ nodes, {}, {}, {}, {}, {} };
std::unordered_set<const ggml_tensor *> seen;
std::unordered_map<const ggml_tensor *, int> node_idx;
std::unordered_map<const ggml_tensor *, int> internal_uses;
@@ -1025,9 +1003,9 @@ void htp_lower_graph(const struct ggml_hexagon_session * sess, const struct ggml
}
ggml_hexagon_tensor_clear_flags(const_cast<ggml_tensor *>(tensor), HTP_TENSOR_LOWERED_F16);
const ggml_tensor * root = htp_tensor_root(tensor);
- auto & cls = classes[root];
+ auto & cls = ctx.classes[root];
if (cls.empty()) {
- roots.push_back(root);
+ ctx.roots.push_back(root);
}
cls.push_back(tensor);
};
@@ -1045,16 +1023,13 @@ void htp_lower_graph(const struct ggml_hexagon_session * sess, const struct ggml
add_to_class(tensor);
}
- std::unordered_map<const ggml_tensor *, size_t> owner_idx;
- std::unordered_map<const ggml_tensor *, std::vector<size_t>> root_users;
-
for (size_t idx = 0; idx < nodes.size(); ++idx) {
const htp_opnode & node = nodes[idx];
auto add_user = [&](const ggml_tensor * t) {
if (!t) {
return;
}
- auto & vec = root_users[htp_tensor_root(t)];
+ auto & vec = ctx.root_users[htp_tensor_root(t)];
if (vec.empty() || vec.back() != idx) {
vec.push_back(idx);
}
@@ -1063,10 +1038,8 @@ void htp_lower_graph(const struct ggml_hexagon_session * sess, const struct ggml
add_user(inp);
}
for (const ggml_tensor * out : node.outputs) {
- if (out) {
- owner_idx.emplace(out, idx);
- add_user(out);
- }
+ ctx.owner_idx.emplace(out, idx);
+ add_user(out);
}
}
@@ -1075,19 +1048,11 @@ void htp_lower_graph(const struct ggml_hexagon_session * sess, const struct ggml
size_t n_lowered_mm = 0;
size_t bytes_saved = 0;
- auto find_owner = [&](const ggml_tensor * r) -> htp_opnode * {
- auto it = owner_idx.find(r);
- return (it != owner_idx.end()) ? &nodes[it->second] : nullptr;
- };
-
- std::unordered_map<const ggml_tensor *, bool> root_safe_class;
- for (const ggml_tensor * root : roots) {
- auto it = classes.find(root);
- const bool safe = it != classes.end() &&
- htp_lower_is_root_candidate(root, produced) &&
- htp_lower_safe_class(graph, root, it->second, node_idx, internal_uses);
- root_safe_class[root] = safe;
- if (safe && find_owner(root)) {
+ for (const ggml_tensor * root : ctx.roots) {
+ const bool safe = htp_lower_is_root_candidate(root, produced) &&
+ htp_lower_safe_class(graph, root, ctx.classes[root], node_idx, internal_uses);
+ ctx.root_safe_class[root] = safe;
+ if (safe && ctx.find_owner(root)) {
++n_candidates;
}
}
@@ -1096,45 +1061,13 @@ void htp_lower_graph(const struct ggml_hexagon_session * sess, const struct ggml
return t && (htp_effective_type(t) == GGML_TYPE_F16 || ggml_hexagon_tensor_is_lowered_f16(htp_tensor_root(t)));
};
- auto can_lower = [&](const ggml_tensor * r) -> bool {
- auto it = classes.find(r);
- if (it == classes.end() || ggml_hexagon_tensor_is_lowered_f16(r)) {
- return false;
- }
- auto sit = root_safe_class.find(r);
- if (sit == root_safe_class.end() || !sit->second) {
- return false;
- }
- htp_opnode * owner = find_owner(r);
- if (!owner) {
- return false;
- }
- if (!htp_lower_respect_prec_hints(*owner, r, it->second, nodes, owner_idx, root_users)) {
- return false;
- }
- if (owner->opcode == HTP_OP_GATED_DELTA_NET && !htp_lower_safe_gdn_output(*owner, r, nodes, root_users)) {
- return false;
- }
- if (owner->opcode == HTP_OP_SSM_CONV && (owner->outputs.empty() || r != owner->outputs[0])) {
- return false;
- }
- return true;
- };
-
- auto is_selected_root = [&](const ggml_tensor * r) -> bool {
- if (htp_lower_is_mm_root(nodes, owner_idx, r) || htp_lower_is_fa_root(nodes, owner_idx, r) ||
- htp_lower_is_rms_root(nodes, owner_idx, r) || htp_lower_is_ssm_conv_root(nodes, owner_idx, r) ||
- htp_lower_is_get_rows_root(nodes, owner_idx, r) || htp_lower_is_repeat_root(nodes, owner_idx, r)) {
+ auto is_selected_root = [&](const ggml_tensor * r, const htp_opnode & owner) -> bool {
+ if (htp_lower_is_seed_owner(owner, r)) {
return true;
}
- for (const ggml_tensor * tensor : classes[r]) {
- auto oit = owner_idx.find(tensor);
- if (oit != owner_idx.end()) {
- for (const ggml_tensor * input : nodes[oit->second].inputs) {
- if (is_f16_tensor(input)) {
- return true;
- }
- }
+ for (const ggml_tensor * input : owner.inputs) {
+ if (is_f16_tensor(input)) {
+ return true;
}
}
return false;
@@ -1149,18 +1082,17 @@ void htp_lower_graph(const struct ggml_hexagon_session * sess, const struct ggml
return false;
};
- auto is_f16_consumer_seed = [&](const ggml_tensor * r) -> bool {
- const htp_opnode * owner = find_owner(r);
- if (!owner || has_real_inputs(*owner)) {
+ auto is_f16_consumer_seed = [&](const ggml_tensor * r, const htp_opnode & owner) -> bool {
+ if (has_real_inputs(owner)) {
return false;
}
- auto uit = root_users.find(r);
- if (uit == root_users.end()) {
+ auto uit = ctx.root_users.find(r);
+ if (uit == ctx.root_users.end()) {
return false;
}
for (size_t idx : uit->second) {
const htp_opnode & user = nodes[idx];
- if (&user == owner) {
+ if (&user == &owner) {
continue;
}
for (const ggml_tensor * out : user.outputs) {
@@ -1190,12 +1122,12 @@ void htp_lower_graph(const struct ggml_hexagon_session * sess, const struct ggml
return false;
}
tx_roots.push_back(r);
- for (const ggml_tensor * tensor : classes[r]) {
+ for (const ggml_tensor * tensor : ctx.classes[r]) {
ggml_hexagon_tensor_set_flags(const_cast<ggml_tensor *>(tensor), HTP_TENSOR_LOWERED_F16);
}
- auto uit = root_users.find(r);
- if (uit != root_users.end()) {
+ auto uit = ctx.root_users.find(r);
+ if (uit != ctx.root_users.end()) {
for (size_t idx : uit->second) {
const int pos = touched_pos[idx];
if (pos < 0) {
@@ -1231,14 +1163,14 @@ void htp_lower_graph(const struct ggml_hexagon_session * sess, const struct ggml
}
solved = false;
for (const ggml_tensor * out : tnode->outputs) {
- const ggml_tensor * out_root = out ? htp_tensor_root(out) : nullptr;
- if (out_root && can_lower(out_root) && apply_root(out_root)) {
+ const ggml_tensor * out_root = htp_tensor_root(out);
+ if (ctx.can_lower(out_root) && apply_root(out_root)) {
expanded = true;
}
}
for (const ggml_tensor * inp : tnode->inputs) {
const ggml_tensor * inp_root = inp ? htp_tensor_root(inp) : nullptr;
- if (inp_root && can_lower(inp_root) && apply_root(inp_root)) {
+ if (inp_root && ctx.can_lower(inp_root) && apply_root(inp_root)) {
expanded = true;
}
}
@@ -1257,7 +1189,7 @@ void htp_lower_graph(const struct ggml_hexagon_session * sess, const struct ggml
if (!solved) {
for (const ggml_tensor * r : tx_roots) {
- for (const ggml_tensor * tensor : classes[r]) {
+ for (const ggml_tensor * tensor : ctx.classes[r]) {
ggml_hexagon_tensor_clear_flags(const_cast<ggml_tensor *>(tensor), HTP_TENSOR_LOWERED_F16);
}
}
@@ -1270,20 +1202,23 @@ void htp_lower_graph(const struct ggml_hexagon_session * sess, const struct ggml
for (const ggml_tensor * r : tx_roots) {
++n_lowered_roots;
bytes_saved += ggml_nbytes(r) / 2;
- if (htp_lower_is_mm_root(nodes, owner_idx, r)) {
+ const htp_opnode * owner = ctx.find_owner(r);
+ if (owner && htp_lower_is_mm_opcode(owner->opcode)) {
++n_lowered_mm;
}
}
};
- for (const ggml_tensor * root : roots) {
- if (can_lower(root) && is_selected_root(root)) {
+ for (const ggml_tensor * root : ctx.roots) {
+ const htp_opnode * owner = nullptr;
+ if (ctx.can_lower(root, &owner) && is_selected_root(root, *owner)) {
try_lower_root(root);
}
}
- for (const ggml_tensor * root : roots) {
- if (can_lower(root) && is_f16_consumer_seed(root)) {
+ for (const ggml_tensor * root : ctx.roots) {
+ const htp_opnode * owner = nullptr;
+ if (ctx.can_lower(root, &owner) && is_f16_consumer_seed(root, *owner)) {
try_lower_root(root);
}
}
diff --git a/ggml/src/ggml-hexagon/htp/gated-delta-net-ops.c b/ggml/src/ggml-hexagon/htp/gated-delta-net-ops.c
index 159a12a7e..e11f6a68c 100644
--- a/ggml/src/ggml-hexagon/htp/gated-delta-net-ops.c
+++ b/ggml/src/ggml-hexagon/htp/gated-delta-net-ops.c
@@ -1,5 +1,6 @@
#include <stdint.h>
#include <stdbool.h>
+#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
@@ -1883,17 +1884,65 @@ static void gated_delta_net_f32_tg_thread(const htp_thread_t * th, unsigned int
dma_queue_flush(dma_q);
}
+// Asymmetric master-worker barrier in L2-cached DDR (separate cache lines, zero syncht)
+typedef struct __attribute__((aligned(128))) {
+ atomic_uint done_count;
+ uint8_t pad0[124];
+ atomic_uint go_seq;
+ uint8_t pad1[124];
+} gdn_barrier_t;
+
+static inline void gdn_barrier_init(gdn_barrier_t * b) {
+ atomic_init(&b->done_count, 0u);
+ atomic_init(&b->go_seq, 0u);
+}
+
+static inline void gdn_barrier_wait_workers(gdn_barrier_t * b, uint32_t n_workers) {
+ if (n_workers <= 1u) {
+ return;
+ }
+ const uint32_t target = n_workers - 1u;
+ while (atomic_load_explicit(&b->done_count, memory_order_acquire) != target) {
+ hex_pause();
+ }
+ atomic_store_explicit(&b->done_count, 0u, memory_order_relaxed);
+}
+
+static inline void gdn_barrier_release_workers(gdn_barrier_t * b, uint32_t * seq) {
+ const uint32_t next = *seq + 1u;
+ *seq = next;
+ atomic_store_explicit(&b->go_seq, next, memory_order_release);
+}
+
+static inline void gdn_barrier_worker_sync(gdn_barrier_t * b, uint32_t * seq) {
+ const uint32_t next = *seq + 1u;
+ *seq = next;
+ atomic_fetch_add_explicit(&b->done_count, 1u, memory_order_release);
+ while (atomic_load_explicit(&b->go_seq, memory_order_acquire) != next) {
+ hex_pause();
+ }
+}
+
+static inline void gdn_barrier_all(gdn_barrier_t * b, uint32_t * seq, uint32_t i, uint32_t n_workers) {
+ if (i == 0u) {
+ gdn_barrier_wait_workers(b, n_workers);
+ gdn_barrier_release_workers(b, seq);
+ } else {
+ gdn_barrier_worker_sync(b, seq);
+ }
+}
+
struct htp_gdn_head_ptrs {
float * s_state;
__fp16 * s_col_tiles;
__fp16 * s_update_tiles;
- float * q_f32[2];
- float * k_f32[2];
- float * v_f32[2];
- float * g_f32[2];
- float * b_f32[2];
- float * o_f32[2];
+ void * q_buf[2];
+ void * k_buf[2];
+ void * v_buf[2];
+ void * o_buf[2];
+ float * g_f32;
+ float * b_f32;
__fp16 * k_f16;
@@ -1915,50 +1964,55 @@ struct htp_gdn_head_ptrs {
__fp16 * d_row_tiles;
__fp16 * gamma;
- float * lambda_init[2];
__fp16 * lambda_init_f16;
- __fp16 * decay_m;
+ float * lambda_init[2];
__fp16 * decay_a;
- HVX_Vector * rows_kk;
- HVX_Vector * rows_qk;
- HVX_Vector * rows_inv;
- HVX_Vector * rows_a;
-
HVX_Vector * vtcm_m;
HVX_Vector * vtcm_tmp;
- uint32_t iv1;
- uint32_t iv3;
- uint32_t iq1;
- uint32_t ik1;
- uint32_t iq3;
- uint32_t ik3;
+ uint32_t iv1;
+ uint32_t iv3;
+ uint32_t iq1;
+ uint32_t ik1;
+ uint32_t iq3;
+ uint32_t ik3;
dma_addr_t state_in_dma;
dma_addr_t state_out_dma;
};
-struct htp_gdn_batch_context {
- struct htp_gdn_head_ptrs * heads;
- float * vtcm_g_raw[2];
- float * vtcm_b_raw[2];
- uint8_t * hmx_scales;
- const int32_t * n_rep_data;
- uint32_t n_pend;
- bool replay;
- bool keep;
- uint32_t head_in_seq;
- uint32_t iv3;
- uint32_t curr_buf;
- uint32_t s_update_buf;
- uint32_t c;
- uint32_t n_batch;
- uint32_t S_v;
- float scale;
- struct htp_ops_context * octx;
- const struct htp_gdn_kernel_params * kparams;
+enum gdn_hmx_op {
+ GDN_HMX_OP_H1_FULL = 0,
+ GDN_HMX_OP_H1 = 1,
+ GDN_HMX_OP_H2 = 2,
+ GDN_HMX_OP_H3 = 3,
+ GDN_HMX_OP_H3_NEXT = 4,
};
+struct htp_gdn_hmx_batch_task {
+ const struct htp_gdn_head_ptrs * heads;
+ uint32_t n_batch;
+ uint32_t n_sv_tiles;
+ uint32_t op;
+ uint32_t buf_idx;
+ uint8_t * hmx_scales;
+};
+
+static inline void gdn_hmx_dot_tile_deep(
+ const __fp16 * row_tiles,
+ const __fp16 * col_tiles,
+ __fp16 * out_tile,
+ uint32_t dot_range
+) {
+ asm volatile(
+ HMX_LOAD_MPY_DEEP_F16("%1", "%2", "%0")
+ HMX_STORE_AFTER_F16("%3", "%4")
+ :
+ : "r"(dot_range), "r"(row_tiles), "r"(col_tiles), "r"(out_tile), "r"(0)
+ : "memory"
+ );
+}
+
static inline void gdn_hmx_run_gemm(
const __fp16 * row_tiles,
const __fp16 * col_tiles,
@@ -1967,181 +2021,247 @@ static inline void gdn_hmx_run_gemm(
uint32_t n_col_tiles,
uint32_t n_dot_tiles
) {
- const size_t dot_stride = (size_t) n_dot_tiles * HMX_FP16_TILE_N_ELMS;
+ const size_t dot_stride = (size_t) n_dot_tiles * HMX_FP16_TILE_N_ELMS;
+ const uint32_t dot_range = n_dot_tiles * HMX_FP16_TILE_SIZE - 1u;
for (uint32_t r = 0; r < n_row_tiles; ++r) {
const __fp16 * r_tiles = row_tiles + r * dot_stride;
const __fp16 * c_tiles = col_tiles;
__fp16 * o_tile = out_tiles + r * n_col_tiles * HMX_FP16_TILE_N_ELMS;
for (uint32_t c = 0; c < n_col_tiles; ++c) {
- hmx_fa_qk_dot_tile(r_tiles, c_tiles, o_tile, n_dot_tiles);
+ gdn_hmx_dot_tile_deep(r_tiles, c_tiles, o_tile, dot_range);
c_tiles += dot_stride;
o_tile += HMX_FP16_TILE_N_ELMS;
}
}
}
-static void gdn_hmx_kk_worker(const htp_thread_t * th, void * data) {
+static void htp_gdn_hmx_batch_worker(const htp_thread_t * th, void * data) {
(void) th;
- const struct htp_gdn_batch_context * bctx = (const struct htp_gdn_batch_context *) data;
- asm volatile(HMX_SET_BIAS("%0") :: "r"((unsigned int) bctx->hmx_scales));
- const uint32_t n_sv_tiles = bctx->S_v >> 5;
- for (uint32_t h = 0; h < bctx->n_batch; ++h) {
- gdn_hmx_run_gemm(bctx->heads[h].k_row_tiles, bctx->heads[h].k_col_tiles, bctx->heads[h].kk_tiles, 2u, 2u, n_sv_tiles);
- }
-}
-
-static void gdn_hmx_qk_worker(const htp_thread_t * th, void * data) {
- (void) th;
- const struct htp_gdn_batch_context * bctx = (const struct htp_gdn_batch_context *) data;
- asm volatile(HMX_SET_BIAS("%0") :: "r"((unsigned int) bctx->hmx_scales));
- const uint32_t n_sv_tiles = bctx->S_v >> 5;
- for (uint32_t h = 0; h < bctx->n_batch; ++h) {
- gdn_hmx_run_gemm(bctx->heads[h].q_row_tiles, bctx->heads[h].k_col_tiles, bctx->heads[h].qk_tiles, 2u, 2u, n_sv_tiles);
- }
-}
-
-static void gdn_hmx_ks_worker(const htp_thread_t * th, void * data) {
- (void) th;
- const struct htp_gdn_batch_context * bctx = (const struct htp_gdn_batch_context *) data;
- asm volatile(HMX_SET_BIAS("%0") :: "r"((unsigned int) bctx->hmx_scales));
- const uint32_t n_sv_tiles = bctx->S_v >> 5;
- for (uint32_t h = 0; h < bctx->n_batch; ++h) {
- gdn_hmx_run_gemm(bctx->heads[h].k_prime_row_tiles, bctx->heads[h].s_col_tiles, bctx->heads[h].v_inter_tiles, 2u, n_sv_tiles, n_sv_tiles);
- }
-}
-
-static void gdn_hmx_qs_worker(const htp_thread_t * th, void * data) {
- (void) th;
- const struct htp_gdn_batch_context * bctx = (const struct htp_gdn_batch_context *) data;
- asm volatile(HMX_SET_BIAS("%0") :: "r"((unsigned int) bctx->hmx_scales));
- const uint32_t n_sv_tiles = bctx->S_v >> 5;
- for (uint32_t h = 0; h < bctx->n_batch; ++h) {
- gdn_hmx_run_gemm(bctx->heads[h].q_prime_row_tiles, bctx->heads[h].s_col_tiles, bctx->heads[h].o_inter_tiles, 2u, n_sv_tiles, n_sv_tiles);
- }
-}
-
-static void gdn_hmx_inv10_worker(const htp_thread_t * th, void * data) {
- (void) th;
- const struct htp_gdn_batch_context * bctx = (const struct htp_gdn_batch_context *) data;
- asm volatile(HMX_SET_BIAS("%0") :: "r"((unsigned int) bctx->hmx_scales));
- for (uint32_t h = 0; h < bctx->n_batch; ++h) {
- const struct htp_gdn_head_ptrs * head = &bctx->heads[h];
- hmx_fa_qk_dot_tile(
- (const __fp16 *) head->vtcm_m,
- head->inv_row_tiles + 0 * HMX_FP16_TILE_N_ELMS,
- (__fp16 *) head->vtcm_tmp,
- 1u
- );
- hmx_fa_qk_dot_tile(
- (const __fp16 *) head->vtcm_m + HMX_FP16_TILE_N_ELMS,
- (const __fp16 *) head->vtcm_tmp,
- head->inv_row_tiles + 2 * HMX_FP16_TILE_N_ELMS,
- 1u
- );
- }
-}
+ const struct htp_gdn_hmx_batch_task * task = (const struct htp_gdn_hmx_batch_task *) data;
+ asm volatile(HMX_SET_BIAS("%0") :: "r"((unsigned int) task->hmx_scales));
+
+ const struct htp_gdn_head_ptrs * heads = task->heads;
+ const uint32_t n_batch = task->n_batch;
+ const uint32_t n_sv_tiles = task->n_sv_tiles;
+ const uint32_t buf_idx = task->buf_idx;
+
+ const size_t dot_stride_sv = (size_t) n_sv_tiles * HMX_FP16_TILE_N_ELMS;
+ const uint32_t dot_range_sv = n_sv_tiles * HMX_FP16_TILE_SIZE - 1u;
+
+ switch (task->op) {
+ case GDN_HMX_OP_H1_FULL:
+ for (uint32_t h = 0; h < n_batch; ++h) {
+ gdn_hmx_run_gemm(heads[h].k_row_tiles, heads[h].k_col_tiles, heads[h].kk_tiles, 2u, 2u, n_sv_tiles);
+ gdn_hmx_run_gemm(heads[h].k_prime_row_tiles, heads[h].s_col_tiles, heads[h].v_inter_tiles, 2u, n_sv_tiles, n_sv_tiles);
+ gdn_hmx_run_gemm(heads[h].q_row_tiles, heads[h].k_col_tiles, heads[h].qk_tiles, 2u, 2u, n_sv_tiles);
+ // Split O_inter col 0 into H1 to balance H1 vs H2
+ gdn_hmx_dot_tile_deep(heads[h].q_prime_row_tiles, heads[h].s_col_tiles, heads[h].o_inter_tiles, dot_range_sv);
+ gdn_hmx_dot_tile_deep(heads[h].q_prime_row_tiles + dot_stride_sv, heads[h].s_col_tiles, heads[h].o_inter_tiles + n_sv_tiles * HMX_FP16_TILE_N_ELMS, dot_range_sv);
+ }
+ break;
+ case GDN_HMX_OP_H1:
+ for (uint32_t h = 0; h < n_batch; ++h) {
+ gdn_hmx_run_gemm(heads[h].k_prime_row_tiles, heads[h].s_col_tiles, heads[h].v_inter_tiles, 2u, n_sv_tiles, n_sv_tiles);
+ gdn_hmx_run_gemm(heads[h].q_row_tiles, heads[h].k_col_tiles, heads[h].qk_tiles, 2u, 2u, n_sv_tiles);
+ gdn_hmx_dot_tile_deep(heads[h].q_prime_row_tiles, heads[h].s_col_tiles, heads[h].o_inter_tiles, dot_range_sv);
+ gdn_hmx_dot_tile_deep(heads[h].q_prime_row_tiles + dot_stride_sv, heads[h].s_col_tiles, heads[h].o_inter_tiles + n_sv_tiles * HMX_FP16_TILE_N_ELMS, dot_range_sv);
+ }
+ break;
+ case GDN_HMX_OP_H2:
+ for (uint32_t h = 0; h < n_batch; ++h) {
+ hmx_fa_qk_dot_tile(
+ (const __fp16 *) heads[h].vtcm_m,
+ heads[h].inv_row_tiles + 0 * HMX_FP16_TILE_N_ELMS,
+ (__fp16 *) heads[h].vtcm_tmp,
+ 1u
+ );
+ hmx_fa_qk_dot_tile(
+ (const __fp16 *) heads[h].vtcm_m + HMX_FP16_TILE_N_ELMS,
+ (const __fp16 *) heads[h].vtcm_tmp,
+ heads[h].inv_row_tiles + 2 * HMX_FP16_TILE_N_ELMS,
+ 1u
+ );
+
+ // Write delta_tiles in col-major tile order [c][r] so O_INTRA reads in place
+ const size_t dot_stride = 2u * HMX_FP16_TILE_N_ELMS;
+ const uint32_t dot_range = 2u * HMX_FP16_TILE_SIZE - 1u;
+ for (uint32_t r = 0; r < 2u; ++r) {
+ const __fp16 * r_tiles = heads[h].inv_row_tiles + r * dot_stride;
+ const __fp16 * c_tiles = heads[h].v_prime_col_tiles;
+ for (uint32_t c = 0; c < n_sv_tiles; ++c) {
+ __fp16 * o_tile = heads[h].delta_tiles + (c * 2u + r) * HMX_FP16_TILE_N_ELMS;
+ gdn_hmx_dot_tile_deep(r_tiles, c_tiles, o_tile, dot_range);
+ c_tiles += dot_stride;
+ }
+ }
-static void gdn_hmx_delta_worker(const htp_thread_t * th, void * data) {
- (void) th;
- const struct htp_gdn_batch_context * bctx = (const struct htp_gdn_batch_context *) data;
- asm volatile(HMX_SET_BIAS("%0") :: "r"((unsigned int) bctx->hmx_scales));
- const uint32_t n_sv_tiles = bctx->S_v >> 5;
- const size_t dot_stride = 2u * HMX_FP16_TILE_N_ELMS;
- // Write delta_tiles in col-major order [c][r] so O_INTRA reads in place
- for (uint32_t h = 0; h < bctx->n_batch; ++h) {
- const struct htp_gdn_head_ptrs * head = &bctx->heads[h];
- for (uint32_t r = 0; r < 2u; ++r) {
- const __fp16 * r_tiles = head->inv_row_tiles + r * dot_stride;
- const __fp16 * c_tiles = head->v_prime_col_tiles;
- for (uint32_t c = 0; c < n_sv_tiles; ++c) {
- __fp16 * o_tile = head->delta_tiles + (c * 2u + r) * HMX_FP16_TILE_N_ELMS;
- hmx_fa_qk_dot_tile(r_tiles, c_tiles, o_tile, 2u);
- c_tiles += dot_stride;
+ gdn_hmx_run_gemm(heads[h].a_row_tiles, heads[h].delta_tiles, heads[h].o_intra_tiles, 2u, n_sv_tiles, 2u);
+ for (uint32_t r = 0; r < 2u; ++r) {
+ const __fp16 * r_tiles = heads[h].q_prime_row_tiles + r * dot_stride_sv;
+ const __fp16 * c_tiles = heads[h].s_col_tiles + dot_stride_sv;
+ __fp16 * o_tile = heads[h].o_inter_tiles + (r * n_sv_tiles + 1u) * HMX_FP16_TILE_N_ELMS;
+ for (uint32_t c = 1u; c < n_sv_tiles; ++c) {
+ gdn_hmx_dot_tile_deep(r_tiles, c_tiles, o_tile, dot_range_sv);
+ c_tiles += dot_stride_sv;
+ o_tile += HMX_FP16_TILE_N_ELMS;
+ }
+ }
}
- }
+ break;
+ case GDN_HMX_OP_H3:
+ for (uint32_t h = 0; h < n_batch; ++h) {
+ gdn_hmx_run_gemm(heads[h].d_row_tiles, heads[h].k_col_tiles_64x128[buf_idx], heads[h].s_update_tiles, n_sv_tiles, n_sv_tiles, 2u);
+ }
+ break;
+ case GDN_HMX_OP_H3_NEXT:
+ for (uint32_t h = 0; h < n_batch; ++h) {
+ gdn_hmx_run_gemm(heads[h].d_row_tiles, heads[h].k_col_tiles_64x128[buf_idx], heads[h].s_update_tiles, n_sv_tiles, n_sv_tiles, 2u);
+ gdn_hmx_run_gemm(heads[h].k_row_tiles, heads[h].k_col_tiles, heads[h].kk_tiles, 2u, 2u, n_sv_tiles);
+ }
+ break;
+ default:
+ break;
}
}
-static void gdn_hmx_o_intra_worker(const htp_thread_t * th, void * data) {
- (void) th;
- const struct htp_gdn_batch_context * bctx = (const struct htp_gdn_batch_context *) data;
- asm volatile(HMX_SET_BIAS("%0") :: "r"((unsigned int) bctx->hmx_scales));
- const uint32_t n_sv_tiles = bctx->S_v >> 5;
- for (uint32_t h = 0; h < bctx->n_batch; ++h) {
- gdn_hmx_run_gemm(bctx->heads[h].a_row_tiles, bctx->heads[h].delta_tiles, bctx->heads[h].o_intra_tiles, 2u, n_sv_tiles, 2u);
- }
+static inline void htp_gdn_push_hmx_batch(
+ hmx_queue_t q,
+ struct htp_gdn_hmx_batch_task * task,
+ const struct htp_gdn_head_ptrs * heads,
+ uint32_t n_batch,
+ uint32_t n_sv_tiles,
+ uint32_t op,
+ uint32_t buf_idx,
+ uint8_t * scales
+) {
+ task->heads = heads;
+ task->n_batch = n_batch;
+ task->n_sv_tiles = n_sv_tiles;
+ task->op = op;
+ task->buf_idx = buf_idx;
+ task->hmx_scales = scales;
+
+ hmx_queue_push(q, hmx_queue_make_desc(htp_gdn_hmx_batch_worker, task));
}
-static void gdn_hmx_s_update_worker(const htp_thread_t * th, void * data) {
- (void) th;
- const struct htp_gdn_batch_context * bctx = (const struct htp_gdn_batch_context *) data;
- asm volatile(HMX_SET_BIAS("%0") :: "r"((unsigned int) bctx->hmx_scales));
- const uint32_t n_sv_tiles = bctx->S_v >> 5;
- const uint32_t buf_idx = bctx->s_update_buf;
- for (uint32_t h = 0; h < bctx->n_batch; ++h) {
- gdn_hmx_run_gemm(bctx->heads[h].d_row_tiles, bctx->heads[h].k_col_tiles_64x128[buf_idx], bctx->heads[h].s_update_tiles, n_sv_tiles, n_sv_tiles, 2u);
- }
+static inline void gdn_transpose_stage1_8rows(
+ HVX_Vector * restrict dst_tile0,
+ HVX_Vector * restrict dst_tile1,
+ uint32_t g,
+ HVX_Vector r0,
+ HVX_Vector r1,
+ HVX_Vector r2,
+ HVX_Vector r3,
+ HVX_Vector r4,
+ HVX_Vector r5,
+ HVX_Vector r6,
+ HVX_Vector r7
+) {
+ HVX_VectorPair p01 = Q6_W_vshuff_VVR(r1, r0, -4);
+ HVX_VectorPair p23 = Q6_W_vshuff_VVR(r3, r2, -4);
+ HVX_VectorPair p45 = Q6_W_vshuff_VVR(r5, r4, -4);
+ HVX_VectorPair p67 = Q6_W_vshuff_VVR(r7, r6, -4);
+ r0 = Q6_V_lo_W(p01); r1 = Q6_V_hi_W(p01);
+ r2 = Q6_V_lo_W(p23); r3 = Q6_V_hi_W(p23);
+ r4 = Q6_V_lo_W(p45); r5 = Q6_V_hi_W(p45);
+ r6 = Q6_V_lo_W(p67); r7 = Q6_V_hi_W(p67);
+
+ HVX_VectorPair p02 = Q6_W_vshuff_VVR(r2, r0, -8);
+ HVX_VectorPair p13 = Q6_W_vshuff_VVR(r3, r1, -8);
+ HVX_VectorPair p46 = Q6_W_vshuff_VVR(r6, r4, -8);
+ HVX_VectorPair p57 = Q6_W_vshuff_VVR(r7, r5, -8);
+ r0 = Q6_V_lo_W(p02); r2 = Q6_V_hi_W(p02);
+ r1 = Q6_V_lo_W(p13); r3 = Q6_V_hi_W(p13);
+ r4 = Q6_V_lo_W(p46); r6 = Q6_V_hi_W(p46);
+ r5 = Q6_V_lo_W(p57); r7 = Q6_V_hi_W(p57);
+
+ HVX_VectorPair p04 = Q6_W_vshuff_VVR(r4, r0, -16);
+ HVX_VectorPair p15 = Q6_W_vshuff_VVR(r5, r1, -16);
+ HVX_VectorPair p26 = Q6_W_vshuff_VVR(r6, r2, -16);
+ HVX_VectorPair p37 = Q6_W_vshuff_VVR(r7, r3, -16);
+
+ dst_tile0[ 0 + g] = Q6_V_lo_W(p04); dst_tile0[ 4 + g] = Q6_V_hi_W(p04);
+ dst_tile1[ 0 + g] = Q6_V_lo_W(p15); dst_tile1[ 4 + g] = Q6_V_hi_W(p15);
+ dst_tile0[ 8 + g] = Q6_V_lo_W(p26); dst_tile0[12 + g] = Q6_V_hi_W(p26);
+ dst_tile1[ 8 + g] = Q6_V_lo_W(p37); dst_tile1[12 + g] = Q6_V_hi_W(p37);
}
-static inline void gdn_unpack_64x64_tiles_to_vectors(
- HVX_Vector * restrict rows,
- const __fp16 * restrict tiles
+static inline void gdn_transpose_stage2_pair(
+ HVX_Vector * restrict dst_tile0,
+ HVX_Vector * restrict dst_tile1
) {
- const HVX_Vector * t00 = (const HVX_Vector *) (tiles + 0 * HMX_FP16_TILE_N_ELMS);
- const HVX_Vector * t01 = (const HVX_Vector *) (tiles + 1 * HMX_FP16_TILE_N_ELMS);
- const HVX_Vector * t10 = (const HVX_Vector *) (tiles + 2 * HMX_FP16_TILE_N_ELMS);
- const HVX_Vector * t11 = (const HVX_Vector *) (tiles + 3 * HMX_FP16_TILE_N_ELMS);
-
- for (uint32_t r = 0; r < 16; ++r) {
- HVX_VectorPair vp0 = Q6_W_vdeal_VVR(t01[r], t00[r], -2);
- rows[2 * r + 0] = Q6_V_lo_W(vp0);
- rows[2 * r + 1] = Q6_V_hi_W(vp0);
-
- HVX_VectorPair vp1 = Q6_W_vdeal_VVR(t11[r], t10[r], -2);
- rows[32 + 2 * r + 0] = Q6_V_lo_W(vp1);
- rows[32 + 2 * r + 1] = Q6_V_hi_W(vp1);
+ for (uint32_t t = 0; t < 2; ++t) {
+ HVX_Vector * dst = t ? dst_tile1 : dst_tile0;
+ for (uint32_t p0 = 0; p0 < 16; p0 += 4) {
+ HVX_Vector a = dst[p0 + 0];
+ HVX_Vector b = dst[p0 + 1];
+ HVX_Vector c = dst[p0 + 2];
+ HVX_Vector d = dst[p0 + 3];
+
+ HVX_VectorPair pab = Q6_W_vshuff_VVR(b, a, -32);
+ HVX_VectorPair pcd = Q6_W_vshuff_VVR(d, c, -32);
+ HVX_VectorPair out0 = Q6_W_vshuff_VVR(Q6_V_lo_W(pcd), Q6_V_lo_W(pab), -64);
+ HVX_VectorPair out1 = Q6_W_vshuff_VVR(Q6_V_hi_W(pcd), Q6_V_hi_W(pab), -64);
+
+ dst[p0 + 0] = Q6_V_lo_W(out0);
+ dst[p0 + 1] = Q6_V_hi_W(out0);
+ dst[p0 + 2] = Q6_V_lo_W(out1);
+ dst[p0 + 3] = Q6_V_hi_W(out1);
+ }
}
}
-static inline void gdn_pack_64x64_vectors_to_tiles(
- __fp16 * restrict tiles,
- const HVX_Vector * restrict rows
+static inline void gdn_transpose_k_pair_tiles_vshuff(
+ HVX_Vector * restrict dst_tile0,
+ HVX_Vector * restrict dst_tile1,
+ const uint8_t * restrict src_base,
+ size_t row_byte_stride
) {
- HVX_Vector * t00 = (HVX_Vector *) (tiles + 0 * HMX_FP16_TILE_N_ELMS);
- HVX_Vector * t01 = (HVX_Vector *) (tiles + 1 * HMX_FP16_TILE_N_ELMS);
- HVX_Vector * t10 = (HVX_Vector *) (tiles + 2 * HMX_FP16_TILE_N_ELMS);
- HVX_Vector * t11 = (HVX_Vector *) (tiles + 3 * HMX_FP16_TILE_N_ELMS);
-
- for (uint32_t r = 0; r < 16; ++r) {
- HVX_VectorPair vp0 = Q6_W_vshuff_VVR(rows[2 * r + 1], rows[2 * r + 0], -2);
- t00[r] = Q6_V_lo_W(vp0);
- t01[r] = Q6_V_hi_W(vp0);
-
- HVX_VectorPair vp1 = Q6_W_vshuff_VVR(rows[32 + 2 * r + 1], rows[32 + 2 * r + 0], -2);
- t10[r] = Q6_V_lo_W(vp1);
- t11[r] = Q6_V_hi_W(vp1);
+ for (uint32_t g = 0; g < 4; ++g) {
+ const uint8_t * p = src_base + (size_t) (8 * g) * row_byte_stride;
+ HVX_Vector r0 = hvx_vmem(p); p += row_byte_stride;
+ HVX_Vector r1 = hvx_vmem(p); p += row_byte_stride;
+ HVX_Vector r2 = hvx_vmem(p); p += row_byte_stride;
+ HVX_Vector r3 = hvx_vmem(p); p += row_byte_stride;
+ HVX_Vector r4 = hvx_vmem(p); p += row_byte_stride;
+ HVX_Vector r5 = hvx_vmem(p); p += row_byte_stride;
+ HVX_Vector r6 = hvx_vmem(p); p += row_byte_stride;
+ HVX_Vector r7 = hvx_vmem(p);
+
+ gdn_transpose_stage1_8rows(dst_tile0, dst_tile1, g, r0, r1, r2, r3, r4, r5, r6, r7);
}
-}
-static inline float gdn_vtcm_load_f32(const float * vtcm_arr, uint32_t idx) {
- const HVX_Vector v = hvx_vmem(&vtcm_arr[idx & ~31u]);
- union {
- int32_t i;
- float f;
- } u;
- u.i = Q6_R_vextract_VR(v, (idx & 31u) * 4u);
- return u.f;
+ gdn_transpose_stage2_pair(dst_tile0, dst_tile1);
}
-static inline uint16_t gdn_hvx_extract_u16(HVX_Vector v, uint32_t idx) {
- const uint32_t w = (uint32_t) Q6_R_vextract_VR(v, (idx & ~1u) * 2u);
- return (uint16_t) ((idx & 1u) ? (w >> 16) : (w & 0xFFFFu));
+static inline void gdn_interleave_rows_to_tiles_vshuff(
+ __fp16 * restrict vtcm_dst,
+ const __fp16 * restrict vtcm_src,
+ uint32_t n_rows,
+ uint32_t n_cols
+) {
+ const uint32_t n_k_tiles = n_cols >> 5;
+ const uint32_t n_c_iters = n_k_tiles >> 1;
+ const size_t row_byte_stride = (size_t) n_cols * sizeof(__fp16);
+ const size_t tile_pair_vecs = 2u * (HMX_FP16_TILE_SIZE / sizeof(HVX_Vector));
+
+ for (uint32_t r_base = 0; r_base < n_rows; r_base += HMX_FP16_TILE_N_ROWS) {
+ const uint32_t ct = r_base >> 5;
+ HVX_Vector * tile_base = (HVX_Vector *) (vtcm_dst + (size_t) ct * n_k_tiles * HMX_FP16_TILE_N_ELMS);
+ const uint8_t * row_src = (const uint8_t *) (vtcm_src + (size_t) r_base * n_cols);
+
+ for (uint32_t i = 0; i < n_c_iters; ++i) {
+ gdn_transpose_k_pair_tiles_vshuff(tile_base, tile_base + 16, row_src + (size_t) i * 128u, row_byte_stride);
+ tile_base += tile_pair_vecs;
+ }
+ }
}
static inline void gdn_f32_to_hmx_row_tiles_and_f16(
__fp16 * restrict dst_tiles,
__fp16 * restrict dst_prime_tiles,
+ __fp16 * restrict dst_col_tiles,
__fp16 * restrict dst_f16,
const float * restrict src,
const __fp16 * restrict scale_per_row,
@@ -2149,42 +2269,47 @@ static inline void gdn_f32_to_hmx_row_tiles_and_f16(
uint32_t n_cols,
const bool write_f16
) {
- const uint32_t n_col_tiles = n_cols >> 5;
- const HVX_Vector v_scale_f16 = hvx_vmem(scale_per_row);
-
- for (uint32_t r = 0; r < n_rows; r += 2) {
- uint32_t r0 = r >> 5;
- uint32_t r1 = (r & 31u) >> 1;
- const float * p0 = src + (r + 0) * n_cols;
- const float * p1 = src + (r + 1) * n_cols;
-
- const uint32_t pair = (uint32_t) Q6_R_vextract_VR(v_scale_f16, r * 2u);
- HVX_Vector v_scale = Q6_V_vsplat_R(pair);
-
- for (uint32_t c = 0; c < n_col_tiles; c += 2) {
- HVX_Vector v0_0 = hvx_vmem(p0 + (c + 0) * 32);
- HVX_Vector v1_0 = hvx_vmem(p1 + (c + 0) * 32);
- HVX_Vector v0_1 = hvx_vmem(p0 + (c + 1) * 32);
- HVX_Vector v1_1 = hvx_vmem(p1 + (c + 1) * 32);
-
- HVX_Vector vh0 = hvx_vec_f32_to_f16_shuff(v0_0, v1_0);
- HVX_Vector vh1 = hvx_vec_f32_to_f16_shuff(v0_1, v1_1);
- __fp16 * tile0 = dst_tiles + (r0 * n_col_tiles + c + 0) * HMX_FP16_TILE_N_ELMS;
- __fp16 * tile1 = dst_tiles + (r0 * n_col_tiles + c + 1) * HMX_FP16_TILE_N_ELMS;
- ((HVX_Vector *) tile0)[r1] = vh0;
- ((HVX_Vector *) tile1)[r1] = vh1;
-
- HVX_Vector vh0_s = hvx_vec_mul_f16_f16(vh0, v_scale);
- HVX_Vector vh1_s = hvx_vec_mul_f16_f16(vh1, v_scale);
- __fp16 * tile0_s = dst_prime_tiles + (r0 * n_col_tiles + c + 0) * HMX_FP16_TILE_N_ELMS;
- __fp16 * tile1_s = dst_prime_tiles + (r0 * n_col_tiles + c + 1) * HMX_FP16_TILE_N_ELMS;
- ((HVX_Vector *) tile0_s)[r1] = vh0_s;
- ((HVX_Vector *) tile1_s)[r1] = vh1_s;
-
- if (write_f16) {
- HVX_VectorPair vp01 = Q6_W_vdeal_VVR(vh1, vh0, -2);
- hvx_vmem(dst_f16 + (r + 0) * n_cols + c * 32) = Q6_V_lo_W(vp01);
- hvx_vmem(dst_f16 + (r + 1) * n_cols + c * 32) = Q6_V_hi_W(vp01);
+ const uint32_t n_col_tiles = n_cols >> 5;
+ const uint32_t n_row_tiles = n_rows >> 5;
+
+ uint32_t scale_pairs[32] __attribute__((aligned(128)));
+ hvx_vmem(scale_pairs) = hvx_vmem(scale_per_row);
+
+ for (uint32_t c = 0; c < n_col_tiles; c += 2) {
+ for (uint32_t r0 = 0; r0 < n_row_tiles; ++r0) {
+ HVX_Vector * restrict tile0 = (HVX_Vector *) (dst_tiles + (r0 * n_col_tiles + c + 0) * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict tile1 = (HVX_Vector *) (dst_tiles + (r0 * n_col_tiles + c + 1) * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict tile0_s = (HVX_Vector *) (dst_prime_tiles + (r0 * n_col_tiles + c + 0) * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict tile1_s = (HVX_Vector *) (dst_prime_tiles + (r0 * n_col_tiles + c + 1) * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict col0 = dst_col_tiles ? (HVX_Vector *) (dst_col_tiles + ((c + 0) * n_row_tiles + r0) * HMX_FP16_TILE_N_ELMS) : NULL;
+ HVX_Vector * restrict col1 = dst_col_tiles ? (HVX_Vector *) (dst_col_tiles + ((c + 1) * n_row_tiles + r0) * HMX_FP16_TILE_N_ELMS) : NULL;
+ const float * restrict p0 = src + (r0 * 32u) * n_cols + c * 32u;
+ const uint32_t * restrict sp = scale_pairs + r0 * 16u;
+
+ #pragma nounroll
+ for (uint32_t r1 = 0; r1 < 16u; ++r1) {
+ const HVX_Vector v_scale = Q6_V_vsplat_R(sp[r1]);
+ const float * p1 = p0 + n_cols;
+
+ const HVX_Vector vh0 = hvx_vec_f32_to_f16_shuff(hvx_vmem(p0 + 0), hvx_vmem(p1 + 0));
+ const HVX_Vector vh1 = hvx_vec_f32_to_f16_shuff(hvx_vmem(p0 + 32), hvx_vmem(p1 + 32));
+ p0 += 2u * n_cols;
+
+ tile0[r1] = vh0;
+ tile1[r1] = vh1;
+ if (dst_col_tiles) {
+ col0[r1] = vh0;
+ col1[r1] = vh1;
+ }
+ tile0_s[r1] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(vh0, v_scale));
+ tile1_s[r1] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(vh1, v_scale));
+
+ if (write_f16) {
+ const uint32_t r = r0 * 32u + 2u * r1;
+ HVX_VectorPair vp01 = Q6_W_vdeal_VVR(vh1, vh0, -2);
+ hvx_vmem(dst_f16 + (r + 0u) * n_cols + c * 32u) = Q6_V_lo_W(vp01);
+ hvx_vmem(dst_f16 + (r + 1u) * n_cols + c * 32u) = Q6_V_hi_W(vp01);
+ }
}
}
}
@@ -2193,94 +2318,141 @@ static inline void gdn_f32_to_hmx_row_tiles_and_f16(
static inline void gdn_f16_to_hmx_row_tiles(
__fp16 * restrict dst_tiles,
__fp16 * restrict dst_prime_tiles,
+ __fp16 * restrict dst_col_tiles,
const __fp16 * restrict src,
const __fp16 * restrict scale_per_row,
uint32_t n_rows,
uint32_t n_cols
) {
- const uint32_t n_col_tiles = n_cols >> 5;
- const HVX_Vector v_scale_f16 = hvx_vmem(scale_per_row);
-
- for (uint32_t r = 0; r < n_rows; r += 2) {
- uint32_t r0 = r >> 5;
- uint32_t r1 = (r & 31u) >> 1;
- const __fp16 * p0 = src + (r + 0) * n_cols;
- const __fp16 * p1 = src + (r + 1) * n_cols;
-
- const uint32_t pair = (uint32_t) Q6_R_vextract_VR(v_scale_f16, r * 2u);
- HVX_Vector v_scale = Q6_V_vsplat_R(pair);
-
- for (uint32_t c = 0; c < n_col_tiles; c += 2) {
- HVX_Vector vr0 = hvx_vmem(p0 + c * 32);
- HVX_Vector vr1 = hvx_vmem(p1 + c * 32);
- HVX_VectorPair vp = Q6_W_vshuff_VVR(vr1, vr0, -2);
- HVX_Vector vh0 = Q6_V_lo_W(vp);
- HVX_Vector vh1 = Q6_V_hi_W(vp);
-
- __fp16 * tile0 = dst_tiles + (r0 * n_col_tiles + c + 0) * HMX_FP16_TILE_N_ELMS;
- __fp16 * tile1 = dst_tiles + (r0 * n_col_tiles + c + 1) * HMX_FP16_TILE_N_ELMS;
- ((HVX_Vector *) tile0)[r1] = vh0;
- ((HVX_Vector *) tile1)[r1] = vh1;
-
- HVX_Vector vh0_s = hvx_vec_mul_f16_f16(vh0, v_scale);
- HVX_Vector vh1_s = hvx_vec_mul_f16_f16(vh1, v_scale);
- __fp16 * tile0_s = dst_prime_tiles + (r0 * n_col_tiles + c + 0) * HMX_FP16_TILE_N_ELMS;
- __fp16 * tile1_s = dst_prime_tiles + (r0 * n_col_tiles + c + 1) * HMX_FP16_TILE_N_ELMS;
- ((HVX_Vector *) tile0_s)[r1] = vh0_s;
- ((HVX_Vector *) tile1_s)[r1] = vh1_s;
+ const uint32_t n_col_tiles = n_cols >> 5;
+ const uint32_t n_row_tiles = n_rows >> 5;
+
+ uint32_t scale_pairs[32] __attribute__((aligned(128)));
+ hvx_vmem(scale_pairs) = hvx_vmem(scale_per_row);
+
+ for (uint32_t c = 0; c < n_col_tiles; c += 2) {
+ for (uint32_t r0 = 0; r0 < n_row_tiles; ++r0) {
+ HVX_Vector * restrict tile0 = (HVX_Vector *) (dst_tiles + (r0 * n_col_tiles + c + 0) * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict tile1 = (HVX_Vector *) (dst_tiles + (r0 * n_col_tiles + c + 1) * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict tile0_s = (HVX_Vector *) (dst_prime_tiles + (r0 * n_col_tiles + c + 0) * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict tile1_s = (HVX_Vector *) (dst_prime_tiles + (r0 * n_col_tiles + c + 1) * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict col0 = dst_col_tiles ? (HVX_Vector *) (dst_col_tiles + ((c + 0) * n_row_tiles + r0) * HMX_FP16_TILE_N_ELMS) : NULL;
+ HVX_Vector * restrict col1 = dst_col_tiles ? (HVX_Vector *) (dst_col_tiles + ((c + 1) * n_row_tiles + r0) * HMX_FP16_TILE_N_ELMS) : NULL;
+ const __fp16 * restrict p0 = src + (r0 * 32u) * n_cols + c * 32u;
+ const uint32_t * restrict sp = scale_pairs + r0 * 16u;
+
+ #pragma nounroll
+ for (uint32_t r1 = 0; r1 < 16u; ++r1) {
+ const HVX_Vector v_scale_pair = Q6_V_vsplat_R(sp[r1]);
+ const HVX_Vector v_in0 = hvx_vmem(p0);
+ const HVX_Vector v_in1 = hvx_vmem(p0 + n_cols);
+ p0 += 2u * n_cols;
+
+ const HVX_VectorPair vp = Q6_W_vshuff_VVR(v_in1, v_in0, -2);
+ const HVX_Vector lo = Q6_V_lo_W(vp);
+ const HVX_Vector hi = Q6_V_hi_W(vp);
+ tile0[r1] = lo;
+ tile1[r1] = hi;
+ if (dst_col_tiles) {
+ col0[r1] = lo;
+ col1[r1] = hi;
+ }
+ tile0_s[r1] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(lo, v_scale_pair));
+ tile1_s[r1] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(hi, v_scale_pair));
+ }
}
}
}
-// Solve (I + L) * A = I for 32x32 unit lower-triangular L in L1-cached stack, writing A rows to VTCM
+// Solve (I + L) * A = I for 32x32 unit lower-triangular L in L1-cached DDR
static __attribute__((noinline)) void gdn_invert_lower_32x32(
- HVX_Vector * restrict a_rows,
- const uint16_t * restrict l_l2
+ HVX_Vector * restrict dst_tile,
+ HVX_Vector * restrict neg_tile,
+ const uint16_t * restrict l_l2,
+ HVX_Vector * restrict a_l1
) {
- const HVX_Vector v_one_f16 = hvx_vec_splat_f16(1.0f);
- const HVX_VectorPred q_mask64 = Q6_Q_vsetq2_R(64);
-
- for (uint32_t t = 0; t < 32; ++t) {
- const uint32_t * restrict l_row_u32 = (const uint32_t *) (l_l2 + t * 32u);
- HVX_Vector v_inv0 = Q6_V_vzero();
- HVX_Vector v_inv1 = Q6_V_vzero();
- uint32_t k = 0;
+ const HVX_Vector v_zero = Q6_V_vzero();
+ const HVX_Vector v_one_f16 = hvx_vec_splat_f16(1.0f);
+ const HVX_Vector v_sign = Q6_Vh_vsplat_R(0x8000);
+
+ {
+ const HVX_Vector a0 = Q6_V_vand_QV(Q6_Q_vsetq2_R(2), v_one_f16);
+ const HVX_Vector neg_l1 = Q6_Vh_vsplat_R(l_l2[32u] ^ 0x8000u);
+ const HVX_VectorPred q1_0 = Q6_Q_and_QQn(Q6_Q_vsetq2_R(4), Q6_Q_vsetq2_R(2));
+ const HVX_VectorPred q1_1 = Q6_Q_and_QQn(Q6_Q_vsetq2_R(8), Q6_Q_vsetq2_R(6));
+ HVX_Vector t0_out = Q6_V_vmux_QVV(q1_0, neg_l1, a0);
+ t0_out = Q6_V_vmux_QVV(q1_1, v_one_f16, t0_out);
+ a_l1[0] = t0_out;
+ dst_tile[0] = t0_out;
+ if (neg_tile) {
+ neg_tile[0] = Q6_V_vxor_VV(t0_out, v_sign);
+ }
+ }
+
+ for (uint32_t r = 1u; r < 16u; ++r) {
+ const uint32_t t0 = 2u * r;
+ const uint32_t t1 = t0 + 1u;
+ const uint32_t * restrict l0_u32 = (const uint32_t *) (l_l2 + t0 * 32u);
+ const uint32_t * restrict l1_u32 = (const uint32_t *) (l_l2 + t1 * 32u);
+
+ HVX_Vector inv0_lo = v_zero;
+ HVX_Vector inv0_hi = v_zero;
+ HVX_Vector inv1_lo = v_zero;
+ HVX_Vector inv1_hi = v_zero;
+
#pragma nounroll
- for (; k + 1u < t; k += 2u) {
- const uint32_t w = l_row_u32[k >> 1];
- HVX_Vector v_lk0 = Q6_Vh_vsplat_R(w & 0xFFFFu);
- HVX_Vector v_lk1 = Q6_Vh_vsplat_R(w >> 16);
- v_inv0 = hvx_vec_sub_f16_f16(v_inv0, hvx_vec_mul_f16_f16(v_lk0, a_rows[k + 0u]));
- v_inv1 = hvx_vec_sub_f16_f16(v_inv1, hvx_vec_mul_f16_f16(v_lk1, a_rows[k + 1u]));
+ for (uint32_t k2 = 0u; k2 < r; ++k2) {
+ const HVX_Vector ak = a_l1[k2];
+ const HVX_Vector w0 = Q6_V_vsplat_R(l0_u32[k2]);
+ const HVX_Vector w1 = Q6_V_vsplat_R(l1_u32[k2]);
+ const HVX_VectorPair p0 = Q6_Wqf32_vmpy_VhfVhf(w0, ak);
+ const HVX_VectorPair p1 = Q6_Wqf32_vmpy_VhfVhf(w1, ak);
+ inv0_lo = Q6_Vqf32_vsub_Vqf32Vqf32(inv0_lo, Q6_V_lo_W(p0));
+ inv0_hi = Q6_Vqf32_vsub_Vqf32Vqf32(inv0_hi, Q6_V_hi_W(p0));
+ inv1_lo = Q6_Vqf32_vsub_Vqf32Vqf32(inv1_lo, Q6_V_lo_W(p1));
+ inv1_hi = Q6_Vqf32_vsub_Vqf32Vqf32(inv1_hi, Q6_V_hi_W(p1));
}
- HVX_Vector v_inv = hvx_vec_add_f16_f16(v_inv0, v_inv1);
- if (k < t) {
- HVX_Vector v_lk = Q6_Vh_vsplat_R(l_l2[t * 32u + k]);
- v_inv = hvx_vec_sub_f16_f16(v_inv, hvx_vec_mul_f16_f16(v_lk, a_rows[k]));
+
+ const HVX_Vector v_inv0_qf = Q6_Vqf32_vadd_Vqf32Vqf32(inv0_lo, inv0_hi);
+ const HVX_Vector v_row0 = Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(v_zero, v_inv0_qf));
+ const HVX_VectorPred q_diag0 = Q6_Q_and_QQn(Q6_Q_vsetq2_R(8u * r + 2u), Q6_Q_vsetq2_R(8u * r));
+ const HVX_Vector a_even = Q6_V_vmux_QVV(q_diag0, v_one_f16, v_row0);
+
+ const HVX_Vector lk_diag = Q6_Vh_vsplat_R(l_l2[t1 * 32u + t0]);
+ HVX_Vector v_inv1_qf = Q6_Vqf32_vadd_Vqf32Vqf32(inv1_lo, inv1_hi);
+ v_inv1_qf = Q6_Vqf32_vsub_Vqf32Vqf32(v_inv1_qf, Q6_V_lo_W(Q6_Wqf32_vmpy_VhfVhf(lk_diag, a_even)));
+ const HVX_Vector v_row1 = Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(v_inv1_qf, v_zero));
+ const HVX_VectorPred q_diag1 = (r == 15u) ? Q6_Q_not_Q(Q6_Q_vsetq2_R(126)) : Q6_Q_and_QQn(Q6_Q_vsetq2_R(8u * r + 8u), Q6_Q_vsetq2_R(8u * r + 6u));
+ const HVX_Vector t_out = Q6_V_vmux_QVV(q_diag1, v_one_f16, Q6_V_vor_VV(a_even, v_row1));
+
+ a_l1[r] = t_out;
+ dst_tile[r] = t_out;
+ if (neg_tile) {
+ neg_tile[r] = Q6_V_vxor_VV(t_out, v_sign);
}
- HVX_VectorPred q_diag = (t == 0) ? Q6_Q_vsetq2_R(2) : Q6_Q_and_QQn(Q6_Q_vsetq2_R(2 * (t + 1)), Q6_Q_vsetq2_R(2 * t));
- a_rows[t] = Q6_V_vand_QV(q_mask64, Q6_V_vmux_QVV(q_diag, v_one_f16, v_inv));
}
}
+// l_l2_buf holds [0..1023] l00/l11 (reused), [1024..1087] beta_u16, [1088..2111] a_l1 in L1-cached DDR
static __attribute__((noinline)) void gdn_build_inv_l_blocks(
__fp16 * restrict inv_row_tiles,
- const HVX_Vector * restrict rows_kk,
- const __fp16 * restrict decay_m,
+ const __fp16 * restrict kk_tiles,
+ const __fp16 * restrict decay_a,
const float * restrict beta,
__fp16 * restrict l10_tile,
__fp16 * restrict neg_a11_tile,
- HVX_Vector * restrict a_rows_vtcm
+ uint16_t * restrict l_l2_buf
) {
- // Keep l00, l11, and beta_u16 in L1-cached stack to avoid scalar VTCM loads in inversion
- uint16_t l_l2_buf[2048 + 64] __attribute__((aligned(128)));
-
- HVX_Vector * restrict a_rows = a_rows_vtcm;
- HVX_Vector * restrict p_l00 = (HVX_Vector *) (l_l2_buf + 0);
- HVX_Vector * restrict p_l11 = (HVX_Vector *) (l_l2_buf + 1024);
- uint16_t * restrict beta_u16 = l_l2_buf + 2048;
+ HVX_Vector * restrict p_l_blk = (HVX_Vector *) (l_l2_buf + 0);
+ uint16_t * restrict beta_u16 = l_l2_buf + 1024;
+ HVX_Vector * restrict a_l1 = (HVX_Vector *) (l_l2_buf + 1088);
HVX_Vector * restrict p_l10_tile = (HVX_Vector *) l10_tile;
+ const HVX_Vector * kk00 = (const HVX_Vector *) (kk_tiles + 0 * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * kk01 = (const HVX_Vector *) (kk_tiles + 1 * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * kk10 = (const HVX_Vector *) (kk_tiles + 2 * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * kk11 = (const HVX_Vector *) (kk_tiles + 3 * HMX_FP16_TILE_N_ELMS);
+
HVX_Vector * restrict tile00 = (HVX_Vector *) (inv_row_tiles + 0 * HMX_FP16_TILE_N_ELMS);
HVX_Vector * restrict tile01 = (HVX_Vector *) (inv_row_tiles + 1 * HMX_FP16_TILE_N_ELMS);
HVX_Vector * restrict tile11 = (HVX_Vector *) (inv_row_tiles + 3 * HMX_FP16_TILE_N_ELMS);
@@ -2292,70 +2464,55 @@ static __attribute__((noinline)) void gdn_build_inv_l_blocks(
tile01[r] = Q6_V_vzero();
}
- {
- const HVX_VectorPred q_mask64 = Q6_Q_vsetq2_R(64);
- const uint32_t * restrict beta_u32 = (const uint32_t *) beta_u16;
-
- for (uint32_t r = 0; r < 16; ++r) {
- uint32_t t0 = 2 * r;
- uint32_t t1 = t0 + 1;
-
- HVX_Vector v_d0 = hvx_vmem(decay_m + t0 * 64);
- HVX_Vector v_d1 = hvx_vmem(decay_m + t1 * 64);
- const uint32_t bw = beta_u32[r];
- HVX_Vector v_b0 = Q6_Vh_vsplat_R(bw & 0xFFFFu);
- HVX_Vector v_b1 = Q6_Vh_vsplat_R(bw >> 16);
-
- HVX_Vector r0 = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(rows_kk[t0], v_d0), v_b0);
- HVX_Vector r1 = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(rows_kk[t1], v_d1), v_b1);
+ const HVX_VectorPred q_mask64 = Q6_Q_vsetq2_R(64);
+ const uint32_t * restrict beta_u32 = (const uint32_t *) beta_u16;
- p_l00[r] = Q6_V_vmux_QVV(q_mask64, r0, Q6_V_vror_VR(r1, 64));
- }
-
- for (uint32_t r = 0; r < 16; ++r) {
- uint32_t t0 = 32 + 2 * r;
- uint32_t t1 = t0 + 1;
+ for (uint32_t r = 0; r < 16; ++r) {
+ uint32_t t0 = 2 * r;
+ uint32_t t1 = t0 + 1;
- HVX_Vector v_d0 = hvx_vmem(decay_m + t0 * 64);
- HVX_Vector v_d1 = hvx_vmem(decay_m + t1 * 64);
- const uint32_t bw = beta_u32[16 + r];
- HVX_Vector v_b0 = Q6_Vh_vsplat_R(bw & 0xFFFFu);
- HVX_Vector v_b1 = Q6_Vh_vsplat_R(bw >> 16);
+ const HVX_VectorPair kk_p = Q6_W_vdeal_VVR(kk01[r], kk00[r], -2);
+ HVX_Vector v_d0 = hvx_vmem(decay_a + t0 * 64);
+ HVX_Vector v_d1 = hvx_vmem(decay_a + t1 * 64);
+ const uint32_t bw = beta_u32[r];
+ HVX_Vector v_b0 = Q6_Vh_vsplat_R(bw & 0xFFFFu);
+ HVX_Vector v_b1 = Q6_Vh_vsplat_R(bw >> 16);
- HVX_Vector r0 = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(rows_kk[t0], v_d0), v_b0);
- HVX_Vector r1 = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(rows_kk[t1], v_d1), v_b1);
+ HVX_Vector r0 = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(Q6_V_lo_W(kk_p), v_d0), v_b0);
+ HVX_Vector r1 = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(Q6_V_hi_W(kk_p), v_d1), v_b1);
- HVX_VectorPair vp_l10 = Q6_W_vshuff_VVR(r1, r0, -2);
- p_l10_tile[r] = Q6_V_lo_W(vp_l10);
- p_l11[r] = Q6_V_vmux_QVV(q_mask64, Q6_V_vror_VR(r0, 64), r1);
- }
+ p_l_blk[r] = Q6_V_vmux_QVV(q_mask64, r0, Q6_V_vror_VR(r1, 64));
}
- gdn_invert_lower_32x32(a_rows, l_l2_buf + 0);
+ gdn_invert_lower_32x32(tile00, NULL, l_l2_buf + 0, a_l1);
for (uint32_t r = 0; r < 16; ++r) {
- HVX_VectorPair vp = Q6_W_vshuff_VVR(a_rows[2 * r + 1], a_rows[2 * r + 0], -2);
- tile00[r] = Q6_V_lo_W(vp);
+ uint32_t t0 = 32 + 2 * r;
+ uint32_t t1 = t0 + 1;
+
+ const HVX_VectorPair kk_p = Q6_W_vdeal_VVR(kk11[r], kk10[r], -2);
+ HVX_Vector v_d0 = hvx_vmem(decay_a + t0 * 64);
+ HVX_Vector v_d1 = hvx_vmem(decay_a + t1 * 64);
+ const uint32_t bw = beta_u32[16 + r];
+ HVX_Vector v_b0 = Q6_Vh_vsplat_R(bw & 0xFFFFu);
+ HVX_Vector v_b1 = Q6_Vh_vsplat_R(bw >> 16);
+
+ HVX_Vector r0 = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(Q6_V_lo_W(kk_p), v_d0), v_b0);
+ HVX_Vector r1 = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(Q6_V_hi_W(kk_p), v_d1), v_b1);
+
+ HVX_VectorPair vp_l10 = Q6_W_vshuff_VVR(r1, r0, -2);
+ p_l10_tile[r] = Q6_V_lo_W(vp_l10);
+ p_l_blk[r] = Q6_V_vmux_QVV(q_mask64, Q6_V_vror_VR(r0, 64), r1);
}
- gdn_invert_lower_32x32(a_rows, l_l2_buf + 1024);
-
- for (uint32_t r = 0; r < 16; ++r) {
- HVX_VectorPair vp = Q6_W_vshuff_VVR(a_rows[2 * r + 1], a_rows[2 * r + 0], -2);
- tile11[r] = Q6_V_lo_W(vp);
-
- HVX_Vector n0 = hvx_vec_sub_f16_f16(Q6_V_vzero(), a_rows[2 * r + 0]);
- HVX_Vector n1 = hvx_vec_sub_f16_f16(Q6_V_vzero(), a_rows[2 * r + 1]);
- HVX_VectorPair vp_neg = Q6_W_vshuff_VVR(n1, n0, -2);
- p_neg_a11[r] = Q6_V_lo_W(vp_neg);
- }
+ gdn_invert_lower_32x32(tile11, p_neg_a11, l_l2_buf + 0, a_l1);
}
static inline void gdn_dma_push_chunk_inputs(
dma_queue * dma_q,
- float * vtcm_q,
- float * vtcm_k,
- float * vtcm_v,
+ void * vtcm_q,
+ void * vtcm_k,
+ void * vtcm_v,
const struct htp_tensor * q,
const struct htp_tensor * k,
const struct htp_tensor * v,
@@ -2405,208 +2562,121 @@ static inline void gdn_pack_s_col_tiles(
const float * restrict vtcm_s_state,
uint32_t S_v
) {
- const uint32_t n_k_tiles = S_v >> 5;
- const HVX_Vector v_scat_base = hvx_vmem(hmx_transpose_scatter_offsets);
- const HVX_Vector v_scat_step = Q6_V_vsplat_R(4);
- const size_t pair_region = (size_t) (2 * HMX_FP16_TILE_SIZE - 1);
-
- for (uint32_t r = 0; r < S_v; r += 2) {
- const uint32_t ct = r >> 5;
- const uint32_t local_r = r & 31u;
- const HVX_Vector v_off0 = Q6_Vw_vadd_VwVw(v_scat_base, Q6_V_vsplat_R(local_r * 4));
- const HVX_Vector v_off1 = Q6_Vw_vadd_VwVw(v_off0, v_scat_step);
-
- __fp16 * tile_base = vtcm_s_col_tiles + (size_t) ct * n_k_tiles * HMX_FP16_TILE_N_ELMS;
- const float * s0 = vtcm_s_state + (r + 0) * S_v;
- const float * s1 = vtcm_s_state + (r + 1) * S_v;
-
+ const uint32_t n_k_tiles = S_v >> 5;
+ for (uint32_t r_base = 0; r_base < S_v; r_base += HMX_FP16_TILE_N_ROWS) {
+ const uint32_t ct = r_base >> 5;
for (uint32_t d = 0; d < (S_v >> 6); ++d) {
- const HVX_Vector v0 = hvx_vec_f32_to_f16(hvx_vmem(s0 + d * 64 + 0), hvx_vmem(s0 + d * 64 + 32));
- const HVX_Vector v1 = hvx_vec_f32_to_f16(hvx_vmem(s1 + d * 64 + 0), hvx_vmem(s1 + d * 64 + 32));
- Q6_vscatter_RMVwV((size_t) tile_base, pair_region, v_off0, v0);
- Q6_vscatter_RMVwV((size_t) tile_base, pair_region, v_off1, v1);
- tile_base += 2 * HMX_FP16_TILE_N_ELMS;
+ HVX_Vector * restrict tile_base = (HVX_Vector *) (vtcm_s_col_tiles + (size_t) (ct * n_k_tiles + 2u * d) * HMX_FP16_TILE_N_ELMS);
+ const float * restrict s_base = vtcm_s_state + (size_t) r_base * S_v + d * 64u;
+
+ #pragma nounroll
+ for (uint32_t g = 0u; g < 4u; ++g) {
+ HVX_Vector rows[8];
+ #pragma unroll(4)
+ for (uint32_t j = 0u; j < 4u; ++j) {
+ const float * s0 = s_base + (size_t) (8u * g + 2u * j + 0u) * S_v;
+ const float * s1 = s_base + (size_t) (8u * g + 2u * j + 1u) * S_v;
+ rows[2u * j + 0u] = hvx_vec_f32_to_f16(hvx_vmem(s0 + 0), hvx_vmem(s0 + 32));
+ rows[2u * j + 1u] = hvx_vec_f32_to_f16(hvx_vmem(s1 + 0), hvx_vmem(s1 + 32));
+ }
+ gdn_transpose_stage1_8rows(tile_base, tile_base + 16, g,
+ rows[0], rows[1], rows[2], rows[3],
+ rows[4], rows[5], rows[6], rows[7]);
+ }
+
+ gdn_transpose_stage2_pair(tile_base, tile_base + 16);
}
}
- hvx_gather_sync(vtcm_s_col_tiles);
}
static inline void gdn_init_head_ptrs(
struct htp_gdn_head_ptrs * head,
const struct htp_gdn_hmx_vtcm_layout * L,
uint8_t * vtcm_base,
- uint32_t h,
- uint32_t base_iv1,
+ uint32_t slot,
+ uint32_t group_idx,
+ uint32_t iv1,
uint32_t iv3,
uint32_t is3,
const struct htp_tensor * q,
const struct htp_tensor * k,
- const struct htp_tensor * v,
const struct htp_tensor * state,
const struct htp_tensor * dst,
const struct htp_tensor * dst_cache,
const struct htp_gdn_kernel_params * kparams,
uint32_t S_v,
uint32_t H,
- uint32_t n_tokens,
- uint32_t chunk_size
+ uint32_t n_tokens
) {
- (void) v;
- const size_t dma_scalar_sz = hex_round_up(chunk_size * sizeof(float), 128);
- const size_t decay_sz = 64 * 64 * sizeof(__fp16);
- const size_t row_vecs_sz = 64 * 128;
-
- head->s_state = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_s_state + h * L->state_f32_bytes);
- head->s_col_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_s_col_tiles + h * L->state_tiles_bytes);
- head->s_update_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_s_update_tiles + h * L->state_tiles_bytes);
-
- head->q_f32[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_q_f32[0] + h * L->dma_chunk_bytes);
- head->q_f32[1] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_q_f32[1] + h * L->dma_chunk_bytes);
- head->k_f32[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_k_f32[0] + h * L->dma_chunk_bytes);
- head->k_f32[1] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_k_f32[1] + h * L->dma_chunk_bytes);
- head->v_f32[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_v_f32[0] + h * L->dma_chunk_bytes);
- head->v_f32[1] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_v_f32[1] + h * L->dma_chunk_bytes);
- head->g_f32[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_g_f32[0] + h * dma_scalar_sz);
- head->g_f32[1] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_g_f32[1] + h * dma_scalar_sz);
- head->b_f32[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_b_f32[0] + h * dma_scalar_sz);
- head->b_f32[1] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_b_f32[1] + h * dma_scalar_sz);
- head->o_f32[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_o_f32[0] + h * L->dma_chunk_bytes);
- head->o_f32[1] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_o_f32[1] + h * L->dma_chunk_bytes);
-
- head->k_f16 = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_f16 + h * L->act_f16_bytes);
-
- head->q_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_q_row_tiles + h * L->tile_64xSv_bytes);
- head->q_prime_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_q_prime_row_tiles + h * L->tile_64xSv_bytes);
- head->k_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_row_tiles + h * L->tile_64xSv_bytes);
- head->k_col_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_col_tiles + h * L->tile_64xSv_bytes);
- head->k_prime_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_prime_row_tiles + h * L->tile_64xSv_bytes);
- head->k_col_tiles_64x128[0] = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_col_tiles_64x128[0] + h * L->tile_64xSv_bytes);
- head->k_col_tiles_64x128[1] = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_col_tiles_64x128[1] + h * L->tile_64xSv_bytes);
- head->kk_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_kk_tiles + h * L->tile_64x64_bytes);
- head->qk_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_qk_tiles + h * L->tile_64x64_bytes);
- head->v_inter_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_v_inter_tiles + h * L->tile_64xSv_bytes);
- head->o_inter_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_o_inter_tiles + h * L->tile_64xSv_bytes);
- head->inv_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_inv_row_tiles + h * L->tile_64x64_bytes);
- head->a_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_a_row_tiles + h * L->tile_64x64_bytes);
- head->v_prime_col_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_v_prime_col_tiles + h * L->tile_64xSv_bytes);
- head->delta_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_delta_tiles + h * L->tile_64xSv_bytes);
- head->o_intra_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_o_intra_tiles + h * L->tile_64xSv_bytes);
- head->d_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_d_row_tiles + h * L->tile_64xSv_bytes);
-
- head->gamma = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_gamma + h * dma_scalar_sz);
- head->lambda_init_f16 = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_gamma + h * dma_scalar_sz + 128);
- head->lambda_init[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_lambda_init[0] + h * dma_scalar_sz);
- head->lambda_init[1] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_lambda_init[1] + h * dma_scalar_sz);
- head->decay_m = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_decay_m + h * decay_sz);
- head->decay_a = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_decay_a + h * decay_sz);
-
- head->rows_kk = VTCM_LAYOUT_PTR(HVX_Vector, vtcm_base, L->off_rows_kk + h * row_vecs_sz);
- head->rows_qk = VTCM_LAYOUT_PTR(HVX_Vector, vtcm_base, L->off_rows_qk + h * row_vecs_sz);
- head->rows_inv = VTCM_LAYOUT_PTR(HVX_Vector, vtcm_base, L->off_rows_inv + h * row_vecs_sz);
- head->rows_a = VTCM_LAYOUT_PTR(HVX_Vector, vtcm_base, L->off_rows_a + h * row_vecs_sz);
-
- head->vtcm_m = VTCM_LAYOUT_PTR(HVX_Vector, vtcm_base, L->off_hvx_scratch + h * (64 * 128));
+ const size_t g_pad = (size_t) group_idx * L->hmx_group_pad;
+
+ head->s_state = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_s_state + slot * L->state_f32_bytes);
+ head->s_col_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_s_col_tiles + slot * L->state_tiles_bytes + g_pad);
+ head->s_update_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_s_update_tiles + slot * L->state_tiles_bytes + g_pad);
+
+ head->q_buf[0] = VTCM_LAYOUT_PTR(void, vtcm_base, L->off_q_buf[0] + slot * L->q_buf_bytes);
+ head->q_buf[1] = VTCM_LAYOUT_PTR(void, vtcm_base, L->off_q_buf[1] + slot * L->q_buf_bytes);
+ head->k_buf[0] = VTCM_LAYOUT_PTR(void, vtcm_base, L->off_k_buf[0] + slot * L->k_buf_bytes);
+ head->k_buf[1] = VTCM_LAYOUT_PTR(void, vtcm_base, L->off_k_buf[1] + slot * L->k_buf_bytes);
+ head->v_buf[0] = VTCM_LAYOUT_PTR(void, vtcm_base, L->off_v_buf[0] + slot * L->v_buf_bytes);
+ head->v_buf[1] = VTCM_LAYOUT_PTR(void, vtcm_base, L->off_v_buf[1] + slot * L->v_buf_bytes);
+ head->o_buf[0] = VTCM_LAYOUT_PTR(void, vtcm_base, L->off_o_buf[0] + slot * L->o_buf_bytes);
+ head->o_buf[1] = VTCM_LAYOUT_PTR(void, vtcm_base, L->off_o_buf[1] + slot * L->o_buf_bytes);
+ head->k_f16 = L->need_k_f16 ? VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_f16 + slot * L->act_f16_bytes) : NULL;
+ head->g_f32 = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_g_f32 + slot * L->dma_scalar_bytes);
+ head->b_f32 = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_b_f32 + slot * L->dma_scalar_bytes);
+
+ head->k_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_row_tiles + slot * L->tile_64xSv_bytes + g_pad);
+ head->k_prime_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_prime_row_tiles + slot * L->tile_64xSv_bytes + g_pad);
+ head->k_col_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_col_tiles + slot * L->tile_64xSv_bytes + g_pad);
+ head->k_col_tiles_64x128[0] = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_col_tiles_64x128[0] + slot * L->tile_64xSv_bytes + g_pad);
+ head->k_col_tiles_64x128[1] = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_col_tiles_64x128[1] + slot * L->tile_64xSv_bytes + g_pad);
+ head->q_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_q_row_tiles + slot * L->tile_64xSv_bytes + g_pad);
+ head->q_prime_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_q_prime_row_tiles + slot * L->tile_64xSv_bytes + g_pad);
+ head->v_inter_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_v_inter_tiles + slot * L->tile_64xSv_bytes + g_pad);
+ head->o_inter_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_o_inter_tiles + slot * L->tile_64xSv_bytes + g_pad);
+ head->d_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_d_row_tiles + slot * L->tile_64xSv_bytes + g_pad);
+
+ // Non-overlapping lifetime aliases (separate buffers when dual_hvx runs overlapping phases)
+ head->v_prime_col_tiles = head->q_row_tiles;
+ head->delta_tiles = head->v_inter_tiles;
+ head->o_intra_tiles = L->separate_aliases
+ ? VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_o_intra_tiles + slot * L->tile_64xSv_bytes + g_pad)
+ : head->k_prime_row_tiles;
+
+ head->kk_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_kk_tiles + slot * L->tile_64x64_bytes + g_pad);
+ head->qk_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_qk_tiles + slot * L->tile_64x64_bytes + g_pad);
+ head->inv_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_inv_row_tiles + slot * L->tile_64x64_bytes + g_pad);
+ head->a_row_tiles = L->separate_aliases
+ ? VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_a_row_tiles + slot * L->tile_64x64_bytes + g_pad)
+ : head->kk_tiles;
+
+ head->gamma = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_gamma + slot * L->dma_scalar_bytes);
+ head->lambda_init_f16 = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_gamma + slot * L->dma_scalar_bytes + 128);
+ head->lambda_init[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_lambda_init[0] + slot * L->dma_scalar_bytes);
+ head->lambda_init[1] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_lambda_init[1] + slot * L->dma_scalar_bytes);
+ head->decay_a = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_decay_a + slot * L->decay_bytes);
+
+ head->vtcm_m = VTCM_LAYOUT_PTR(HVX_Vector, vtcm_base, L->off_hvx_scratch + slot * L->hvx_scratch_bytes + g_pad);
head->vtcm_tmp = head->vtcm_m + 32;
- head->iv1 = base_iv1 + h;
+ head->iv1 = iv1;
head->iv3 = iv3;
- head->iq1 = fastmodulo(head->iv1, q->ne[1], &kparams->div_q1);
- head->ik1 = fastmodulo(head->iv1, k->ne[1], &kparams->div_k1);
- head->iq3 = fastdiv(head->iv3, &kparams->div_rq3);
- head->ik3 = fastdiv(head->iv3, &kparams->div_rk3);
+ head->iq1 = fastmodulo(iv1, q->ne[1], &kparams->div_q1);
+ head->ik1 = fastmodulo(iv1, k->ne[1], &kparams->div_k1);
+ head->iq3 = fastdiv(iv3, &kparams->div_rq3);
+ head->ik3 = fastdiv(iv3, &kparams->div_rk3);
head->state_in_dma = state->data +
- ((size_t) is3 * kparams->state_seq_stride + (size_t) head->iv1 * S_v * S_v) * sizeof(float);
+ ((size_t) is3 * kparams->state_seq_stride + (size_t) iv1 * S_v * S_v) * sizeof(float);
head->state_out_dma = dst_cache ?
- (dst_cache->data + (head->iv3 * H + head->iv1) * S_v * S_v * sizeof(float)) :
- (dst->data + (S_v * H * n_tokens * kparams->n_seqs + (head->iv3 * H + head->iv1) * S_v * S_v) * sizeof(float));
-}
-
-static void gdn_hvx_prologue_worker(const htp_thread_t * th, unsigned int n, unsigned int i, void * data) {
- (void) n;
- struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data;
- struct htp_ops_context * octx = bctx->octx;
- const struct htp_gdn_kernel_params * kparams = bctx->kparams;
- struct htp_thread_trace * tr = th->trace;
- dma_queue * dma_q = th->dma_q;
- struct htp_gdn_head_ptrs * head = &bctx->heads[i];
-
- const struct htp_tensor * q = octx->src[0];
- const struct htp_tensor * k = octx->src[1];
- const struct htp_tensor * v = octx->src[2];
- const struct htp_tensor * g = octx->src[3];
- const struct htp_tensor * beta = octx->src[4];
-
- const uint32_t S_v = bctx->S_v;
- const uint32_t chunk_size = kparams->chunk_size;
- const uint32_t n_tokens = kparams->n_tokens;
- const uint32_t n_chunks = kparams->n_chunks;
-
- dma_queue_push(dma_q, dma_make_data(head->s_state, head->state_in_dma),
- S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v);
-
- if (bctx->replay) {
- dma_queue_pop(dma_q);
- const int32_t raw_n_rep = bctx->n_rep_data[head->iv3];
- const uint32_t n_steps = raw_n_rep <= 0 ? 0 : hex_smin((uint32_t) raw_n_rep, bctx->n_pend);
- if (n_steps > 0) {
- htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, 0);
- gdn_replay_f32(octx, dma_q, head->s_state,
- head->k_f32[0], head->v_f32[0], head->g_f32[0], head->b_f32[0],
- head->vtcm_m, chunk_size,
- head->ik1, head->iv1, head->iv3, n_steps, bctx->n_pend,
- kparams->kda != 0, S_v);
- htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, 0);
- }
- if (bctx->keep) {
- dma_queue_push(dma_q, dma_make_data(head->state_out_dma, head->s_state),
- S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v);
- dma_queue_pop(dma_q);
- }
- }
-
- const uint32_t chunk0_tokens = hex_smin(chunk_size, n_tokens);
- gdn_dma_push_chunk_inputs(dma_q, head->q_f32[0], head->k_f32[0], head->v_f32[0],
- q, k, v, head->iq3, head->iq1, head->ik3, head->ik1,
- head->iv3, head->iv1, 0u, chunk0_tokens, S_v);
- if (i == 0u) {
- gdn_dma_push_chunk_gb(dma_q, bctx->vtcm_g_raw[0], bctx->vtcm_b_raw[0],
- g, beta, bctx->iv3, bctx->head_in_seq, 0u, chunk0_tokens, bctx->n_batch);
- }
-
- if (!bctx->replay) {
- dma_queue_pop(dma_q);
- }
- htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, 0);
- gdn_pack_s_col_tiles(head->s_col_tiles, head->s_state, S_v);
- htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, 0);
-
- dma_queue_pop(dma_q);
- dma_queue_pop(dma_q);
- dma_queue_pop(dma_q);
- if (i == 0u) {
- dma_queue_pop(dma_q);
- dma_queue_pop(dma_q);
- }
-
- if (n_chunks > 1u) {
- const uint32_t c1_tokens = hex_smin(chunk_size, n_tokens - chunk_size);
- gdn_dma_push_chunk_inputs(dma_q, head->q_f32[1], head->k_f32[1], head->v_f32[1],
- q, k, v, head->iq3, head->iq1, head->ik3, head->ik1,
- head->iv3, head->iv1, chunk_size, c1_tokens, S_v);
- if (i == 0u) {
- gdn_dma_push_chunk_gb(dma_q, bctx->vtcm_g_raw[1], bctx->vtcm_b_raw[1],
- g, beta, bctx->iv3, bctx->head_in_seq, chunk_size, c1_tokens, bctx->n_batch);
- }
- }
+ (dst_cache->data + ((size_t) iv3 * H + iv1) * S_v * S_v * sizeof(float)) :
+ (dst->data + ((size_t) S_v * H * n_tokens * kparams->n_seqs + ((size_t) iv3 * H + iv1) * S_v * S_v) * sizeof(float));
}
static inline __attribute__((unused)) HVX_Vector hvx_clamp_neg20_0(HVX_Vector v, HVX_Vector v_zero, HVX_Vector v_neg20) {
- HVX_VectorPred p_gt = Q6_Q_vcmp_gt_VsfVsf(v, v_zero);
- v = Q6_V_vmux_QVV(p_gt, v_zero, v);
- HVX_VectorPred p_lt = Q6_Q_vcmp_gt_VsfVsf(v_neg20, v);
- return Q6_V_vmux_QVV(p_lt, v_neg20, v);
+ return Q6_Vsf_vmax_VsfVsf(v_neg20, Q6_Vsf_vmin_VsfVsf(v_zero, v));
}
static inline HVX_Vector hvx_prefix_scan_f32(HVX_Vector v, HVX_Vector carry_in) {
@@ -2626,72 +2696,75 @@ static inline HVX_Vector hvx_splat_last_f32(HVX_Vector v) {
return hvx_vec_repl4(Q6_V_vror_VR(v, 124));
}
-static void gdn_hvx_phase1a_worker(const htp_thread_t * th, unsigned int n, unsigned int i, void * data) {
- (void) n;
- struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data;
- struct htp_thread_trace * tr = th->trace;
- const uint16_t info = (uint16_t) bctx->c;
- htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, info);
-
- struct htp_gdn_head_ptrs * head = &bctx->heads[i];
- const uint32_t curr_buf = bctx->curr_buf;
- const uint32_t S_v = bctx->S_v;
- const uint32_t n_batch = bctx->n_batch;
- const float * vtcm_g_raw = bctx->vtcm_g_raw[curr_buf];
- const float * vtcm_b_raw = bctx->vtcm_b_raw[curr_buf];
-
- if (n_batch == 1) {
- hvx_vmem(head->g_f32[curr_buf] + 0) = hvx_vmem(vtcm_g_raw + 0);
- hvx_vmem(head->g_f32[curr_buf] + 32) = hvx_vmem(vtcm_g_raw + 32);
- hvx_vmem(head->b_f32[curr_buf] + 0) = hvx_vmem(vtcm_b_raw + 0);
- hvx_vmem(head->b_f32[curr_buf] + 32) = hvx_vmem(vtcm_b_raw + 32);
+static __attribute__((noinline)) void gdn_hvx_phase1a(
+ struct htp_gdn_head_ptrs * head,
+ const float * vtcm_g_raw,
+ const float * vtcm_b_raw,
+ uint32_t buf_idx,
+ uint32_t chunk_idx,
+ uint32_t n_batch,
+ uint32_t n_tokens,
+ uint32_t S_v,
+ uint32_t head_idx,
+ bool q_is_f16,
+ bool k_is_f16,
+ bool v_is_f16
+) {
+ if (n_batch == 1u) {
+ hvx_vmem(head->g_f32 + 0) = hvx_vmem(vtcm_g_raw + 0);
+ hvx_vmem(head->g_f32 + 32) = hvx_vmem(vtcm_g_raw + 32);
+ hvx_vmem(head->b_f32 + 0) = hvx_vmem(vtcm_b_raw + 0);
+ hvx_vmem(head->b_f32 + 32) = hvx_vmem(vtcm_b_raw + 32);
} else {
- const uint32_t vtcm_stride = (n_batch + 1) * sizeof(float);
+ const uint32_t vtcm_stride = (n_batch + 1u) * sizeof(float);
const HVX_Vector vv = hvx_vec_gather_offsets_w(vtcm_stride);
- const size_t rt_g = (size_t) ((const uint8_t *) vtcm_g_raw + i * sizeof(float));
- const size_t rt_b = (size_t) ((const uint8_t *) vtcm_b_raw + i * sizeof(float));
- const size_t mu = 32 * vtcm_stride - 1;
-
- Q6_vgather_ARMVw((HVX_Vector *) (head->g_f32[curr_buf] + 0), rt_g, mu, vv);
- Q6_vgather_ARMVw((HVX_Vector *) (head->g_f32[curr_buf] + 32), rt_g + 32 * vtcm_stride, mu, vv);
- Q6_vgather_ARMVw((HVX_Vector *) (head->b_f32[curr_buf] + 0), rt_b, mu, vv);
- Q6_vgather_ARMVw((HVX_Vector *) (head->b_f32[curr_buf] + 32), rt_b + 32 * vtcm_stride, mu, vv);
- hvx_gather_sync(head->b_f32[curr_buf] + 32);
+ const size_t rt_g = (size_t) ((const uint8_t *) vtcm_g_raw + head_idx * sizeof(float));
+ const size_t rt_b = (size_t) ((const uint8_t *) vtcm_b_raw + head_idx * sizeof(float));
+ const size_t mu = 32u * vtcm_stride - 1u;
+
+ Q6_vgather_ARMVw((HVX_Vector *) (head->g_f32 + 0), rt_g, mu, vv);
+ Q6_vgather_ARMVw((HVX_Vector *) (head->g_f32 + 32), rt_g + 32u * vtcm_stride, mu, vv);
+ Q6_vgather_ARMVw((HVX_Vector *) (head->b_f32 + 0), rt_b, mu, vv);
+ Q6_vgather_ARMVw((HVX_Vector *) (head->b_f32 + 32), rt_b + 32u * vtcm_stride, mu, vv);
+ hvx_gather_sync(head->b_f32 + 32);
}
- const uint32_t t_chunk = bctx->c * 64;
- const uint32_t valid_tokens = hex_smin(64, bctx->kparams->n_tokens - t_chunk);
- if (valid_tokens < 64) {
+ const uint32_t t_chunk = chunk_idx * 64u;
+ const uint32_t valid_tokens = hex_smin(64u, n_tokens - t_chunk);
+ if (valid_tokens < 64u) {
const HVX_Vector vzero = Q6_V_vzero();
- if (valid_tokens <= 32) {
+ if (valid_tokens <= 32u) {
const HVX_VectorPred mask = Q6_Q_vsetq2_R(valid_tokens * sizeof(float));
- hvx_vmem(head->g_f32[curr_buf] + 0) = Q6_V_vand_QV(mask, hvx_vmem(head->g_f32[curr_buf] + 0));
- hvx_vmem(head->b_f32[curr_buf] + 0) = Q6_V_vand_QV(mask, hvx_vmem(head->b_f32[curr_buf] + 0));
- hvx_vmem(head->g_f32[curr_buf] + 32) = vzero;
- hvx_vmem(head->b_f32[curr_buf] + 32) = vzero;
+ hvx_vmem(head->g_f32 + 0) = (valid_tokens == 0u) ? vzero : Q6_V_vand_QV(mask, hvx_vmem(head->g_f32 + 0));
+ hvx_vmem(head->b_f32 + 0) = (valid_tokens == 0u) ? vzero : Q6_V_vand_QV(mask, hvx_vmem(head->b_f32 + 0));
+ hvx_vmem(head->g_f32 + 32) = vzero;
+ hvx_vmem(head->b_f32 + 32) = vzero;
} else {
- const HVX_VectorPred mask = Q6_Q_vsetq2_R((valid_tokens - 32) * sizeof(float));
- hvx_vmem(head->g_f32[curr_buf] + 32) = Q6_V_vand_QV(mask, hvx_vmem(head->g_f32[curr_buf] + 32));
- hvx_vmem(head->b_f32[curr_buf] + 32) = Q6_V_vand_QV(mask, hvx_vmem(head->b_f32[curr_buf] + 32));
+ const HVX_VectorPred mask = Q6_Q_vsetq2_R((valid_tokens - 32u) * sizeof(float));
+ hvx_vmem(head->g_f32 + 32) = Q6_V_vand_QV(mask, hvx_vmem(head->g_f32 + 32));
+ hvx_vmem(head->b_f32 + 32) = Q6_V_vand_QV(mask, hvx_vmem(head->b_f32 + 32));
}
- const uint32_t q_words = (bctx->octx->src[0]->type == HTP_TYPE_F16) ? (S_v / 2) : S_v;
- const uint32_t k_words = (bctx->octx->src[1]->type == HTP_TYPE_F16) ? (S_v / 2) : S_v;
- const uint32_t v_words = (bctx->octx->src[2]->type == HTP_TYPE_F16) ? (S_v / 2) : S_v;
- for (uint32_t t = valid_tokens; t < 64; ++t) {
- for (uint32_t j = 0; j < q_words; j += 32) {
- hvx_vmem(head->q_f32[curr_buf] + t * q_words + j) = vzero;
+ const uint32_t q_words = q_is_f16 ? (S_v >> 1) : S_v;
+ const uint32_t k_words = k_is_f16 ? (S_v >> 1) : S_v;
+ const uint32_t v_words = v_is_f16 ? (S_v >> 1) : S_v;
+ float * q_dst = (float *) head->q_buf[buf_idx];
+ float * k_dst = (float *) head->k_buf[buf_idx];
+ float * v_dst = (float *) head->v_buf[buf_idx];
+ for (uint32_t t = valid_tokens; t < 64u; ++t) {
+ for (uint32_t j = 0; j < q_words; j += 32u) {
+ hvx_vmem(q_dst + t * q_words + j) = vzero;
}
- for (uint32_t j = 0; j < k_words; j += 32) {
- hvx_vmem(head->k_f32[curr_buf] + t * k_words + j) = vzero;
+ for (uint32_t j = 0; j < k_words; j += 32u) {
+ hvx_vmem(k_dst + t * k_words + j) = vzero;
}
- for (uint32_t j = 0; j < v_words; j += 32) {
- hvx_vmem(head->v_f32[curr_buf] + t * v_words + j) = vzero;
+ for (uint32_t j = 0; j < v_words; j += 32u) {
+ hvx_vmem(v_dst + t * v_words + j) = vzero;
}
}
}
- const HVX_Vector v_g0 = hvx_vmem(head->g_f32[curr_buf] + 0);
- const HVX_Vector v_g1 = hvx_vmem(head->g_f32[curr_buf] + 32);
+ const HVX_Vector v_g0 = hvx_vmem(head->g_f32 + 0);
+ const HVX_Vector v_g1 = hvx_vmem(head->g_f32 + 32);
HVX_Vector v_gamma0 = hvx_prefix_scan_f32(v_g0, Q6_V_vzero());
HVX_Vector v_carry = hvx_splat_last_f32(v_gamma0);
@@ -2705,496 +2778,1049 @@ static void gdn_hvx_phase1a_worker(const htp_thread_t * th, unsigned int n, unsi
HVX_Vector v_l0 = hvx_vec_exp_f32(hvx_clamp_neg20_0(v_gamma0, v_zero, v_neg20));
HVX_Vector v_l1 = hvx_vec_exp_f32(hvx_clamp_neg20_0(v_gamma1, v_zero, v_neg20));
- hvx_vmem(head->lambda_init[curr_buf] + 0) = v_l0;
- hvx_vmem(head->lambda_init[curr_buf] + 32) = v_l1;
- hvx_vmem(head->lambda_init_f16) = hvx_vec_f32_to_f16(v_l0, v_l1);
+ hvx_vmem(head->lambda_init[buf_idx] + 0) = v_l0;
+ hvx_vmem(head->lambda_init[buf_idx] + 32) = v_l1;
+ hvx_vmem(head->lambda_init_f16) = hvx_vec_f32_to_f16(v_l0, v_l1);
- if (bctx->octx->src[1]->type == HTP_TYPE_F16) {
- const __fp16 * k_f16 = (const __fp16 *) head->k_f32[curr_buf];
- gdn_f16_to_hmx_row_tiles(head->k_row_tiles, head->k_prime_row_tiles,
- k_f16, head->lambda_init_f16, 64, S_v);
- hmx_interleave_rows_to_tiles(head->k_col_tiles, k_f16, 64, S_v, S_v, 0, 64);
+ if (k_is_f16) {
+ const __fp16 * k_f16 = (const __fp16 *) head->k_buf[buf_idx];
+ gdn_f16_to_hmx_row_tiles(head->k_row_tiles, head->k_prime_row_tiles, head->k_col_tiles_64x128[buf_idx],
+ k_f16, head->lambda_init_f16, 64u, S_v);
+ gdn_interleave_rows_to_tiles_vshuff(head->k_col_tiles, k_f16, 64u, S_v);
} else {
- gdn_f32_to_hmx_row_tiles_and_f16(head->k_row_tiles, head->k_prime_row_tiles, head->k_f16,
- head->k_f32[curr_buf], head->lambda_init_f16, 64, S_v, true);
- hmx_interleave_rows_to_tiles(head->k_col_tiles, head->k_f16, 64, S_v, S_v, 0, 64);
+ gdn_f32_to_hmx_row_tiles_and_f16(head->k_row_tiles, head->k_prime_row_tiles, head->k_col_tiles_64x128[buf_idx],
+ head->k_f16, (const float *) head->k_buf[buf_idx], head->lambda_init_f16, 64u, S_v, true);
+ gdn_interleave_rows_to_tiles_vshuff(head->k_col_tiles, head->k_f16, 64u, S_v);
}
-
- htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, info);
}
-static void gdn_hvx_phase1b_worker(const htp_thread_t * th, unsigned int n, unsigned int i, void * data) {
- (void) n;
- struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data;
- struct htp_thread_trace * tr = th->trace;
- const uint16_t info = (uint16_t) bctx->c;
- htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, info);
-
- struct htp_gdn_head_ptrs * head = &bctx->heads[i];
- const uint32_t curr_buf = bctx->curr_buf;
- const uint32_t S_v = bctx->S_v;
-
- if (bctx->octx->src[0]->type == HTP_TYPE_F16) {
- gdn_f16_to_hmx_row_tiles(head->q_row_tiles, head->q_prime_row_tiles,
- (const __fp16 *) head->q_f32[curr_buf], head->lambda_init_f16, 64, S_v);
+static __attribute__((noinline)) void gdn_hvx_phase1b(
+ struct htp_gdn_head_ptrs * head,
+ uint32_t buf_idx,
+ uint32_t S_v,
+ bool q_is_f16
+) {
+ if (q_is_f16) {
+ gdn_f16_to_hmx_row_tiles(head->q_row_tiles, head->q_prime_row_tiles, NULL,
+ (const __fp16 *) head->q_buf[buf_idx], head->lambda_init_f16, 64u, S_v);
} else {
- gdn_f32_to_hmx_row_tiles_and_f16(head->q_row_tiles, head->q_prime_row_tiles, NULL,
- head->q_f32[curr_buf], head->lambda_init_f16, 64, S_v, false);
+ gdn_f32_to_hmx_row_tiles_and_f16(head->q_row_tiles, head->q_prime_row_tiles, NULL, NULL,
+ (const float *) head->q_buf[buf_idx], head->lambda_init_f16, 64u, S_v, false);
}
- const __fp16 * k_f16_src = (bctx->octx->src[1]->type == HTP_TYPE_F16)
- ? (const __fp16 *) head->k_f32[curr_buf]
- : head->k_f16;
- hmx_interleave_cols_to_tiles(head->k_col_tiles_64x128[curr_buf], k_f16_src, 64, S_v, S_v, 2, 0, 64);
-
- const HVX_Vector v_gamma = hvx_vmem(head->gamma);
-
const HVX_Vector v_zero_f16 = Q6_V_vzero();
- const HVX_Vector v_neg20_f16 = hvx_vec_splat_f16(-20.0f);
- const HVX_Vector v_log2e_f16 = hvx_vec_splat_f16(1.4426950408889634f);
const HVX_Vector v_one_f16 = hvx_vec_splat_f16(1.0f);
+ const HVX_Vector v_log2e_f16 = hvx_vec_splat_f16(1.4426950408889634f);
+ const HVX_Vector v_neg20_l2e = hvx_vec_splat_f16(-28.853900817779268f);
+ const HVX_Vector v_gamma = hvx_vmem(head->gamma);
- hvx_vmem(head->decay_m + 0) = Q6_V_vzero();
- hvx_vmem(head->decay_a + 0) = Q6_V_vand_QV(Q6_Q_vsetq2_R(2), v_one_f16);
-
- for (uint32_t t = 1; t < 63; t += 2) {
- uint32_t t0 = t;
- uint32_t t1 = t + 1;
-
- HVX_Vector v_gamma_t0 = Q6_Vh_vsplat_R(gdn_hvx_extract_u16(v_gamma, t0));
- HVX_Vector v_gamma_t1 = Q6_Vh_vsplat_R(gdn_hvx_extract_u16(v_gamma, t1));
-
- HVX_Vector diff0 = hvx_vec_sub_f16_f16(v_gamma_t0, v_gamma);
- HVX_Vector diff1 = hvx_vec_sub_f16_f16(v_gamma_t1, v_gamma);
-
- HVX_VectorPred p_gt0 = Q6_Q_vcmp_gt_VhfVhf(diff0, v_zero_f16);
- HVX_VectorPred p_gt1 = Q6_Q_vcmp_gt_VhfVhf(diff1, v_zero_f16);
-
- diff0 = Q6_V_vmux_QVV(p_gt0, v_zero_f16, diff0);
- diff1 = Q6_V_vmux_QVV(p_gt1, v_zero_f16, diff1);
-
- diff0 = Q6_Vhf_vmax_VhfVhf(v_neg20_f16, diff0);
- diff1 = Q6_Vhf_vmax_VhfVhf(v_neg20_f16, diff1);
-
- HVX_Vector diff_log2e0 = hvx_vec_mul_f16_f16(diff0, v_log2e_f16);
- HVX_Vector diff_log2e1 = hvx_vec_mul_f16_f16(diff1, v_log2e_f16);
-
- HVX_Vector v_exp0 = hvx_vec_exp2_neg_f16(diff_log2e0);
- HVX_Vector v_exp1 = hvx_vec_exp2_neg_f16(diff_log2e1);
+ uint16_t gamma_l1[64] __attribute__((aligned(128)));
+ hvx_vmem(gamma_l1) = v_gamma;
- HVX_VectorPred mask_lt0 = Q6_Q_vsetq2_R(2 * t0);
- HVX_VectorPred mask_lt1 = Q6_Q_vsetq2_R(2 * t1);
+ HVX_Vector * restrict p_decay_a = (HVX_Vector *) head->decay_a;
- HVX_Vector v_m0 = Q6_V_vand_QV(mask_lt0, v_exp0);
- HVX_Vector v_m1 = Q6_V_vand_QV(mask_lt1, v_exp1);
+ p_decay_a[0] = Q6_V_vand_QV(Q6_Q_vsetq2_R(2), v_one_f16);
- HVX_VectorPred mask_le0 = Q6_Q_vsetq2_R(2 * (t0 + 1));
- HVX_VectorPred mask_le1 = Q6_Q_vsetq2_R(2 * (t1 + 1));
+ #pragma nounroll
+ for (uint32_t t = 1u; t < 63u; ++t) {
+ const HVX_Vector v_gamma_t = Q6_Vh_vsplat_R(gamma_l1[t]);
+ HVX_Vector diff_log2e = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vsub_VhfVhf(v_gamma_t, v_gamma), v_log2e_f16));
+ diff_log2e = Q6_Vhf_vmax_VhfVhf(v_neg20_l2e, Q6_Vhf_vmin_VhfVhf(v_zero_f16, diff_log2e));
- HVX_VectorPred mask_diag0 = Q6_Q_and_QQn(mask_le0, mask_lt0);
- HVX_VectorPred mask_diag1 = Q6_Q_and_QQn(mask_le1, mask_lt1);
+ const HVX_Vector v_exp = hvx_vec_exp2_neg_f16(diff_log2e);
- HVX_Vector v_a0 = Q6_V_vmux_QVV(mask_diag0, v_one_f16, v_m0);
- HVX_Vector v_a1 = Q6_V_vmux_QVV(mask_diag1, v_one_f16, v_m1);
+ const HVX_VectorPred mask_lt = Q6_Q_vsetq2_R(2u * t);
+ const HVX_VectorPred mask_le = Q6_Q_vsetq2_R(2u * (t + 1u));
+ const HVX_VectorPred mask_diag = Q6_Q_and_QQn(mask_le, mask_lt);
- hvx_vmem(head->decay_m + t0 * 64) = v_m0;
- hvx_vmem(head->decay_a + t0 * 64) = v_a0;
- hvx_vmem(head->decay_m + t1 * 64) = v_m1;
- hvx_vmem(head->decay_a + t1 * 64) = v_a1;
+ p_decay_a[t] = Q6_V_vmux_QVV(mask_diag, v_one_f16, Q6_V_vand_QV(mask_lt, v_exp));
}
{
- HVX_Vector v_gamma_t = Q6_Vh_vsplat_R(gdn_hvx_extract_u16(v_gamma, 63));
- HVX_Vector diff = hvx_vec_sub_f16_f16(v_gamma_t, v_gamma);
- HVX_VectorPred p_gt = Q6_Q_vcmp_gt_VhfVhf(diff, v_zero_f16);
- diff = Q6_V_vmux_QVV(p_gt, v_zero_f16, diff);
- diff = Q6_Vhf_vmax_VhfVhf(v_neg20_f16, diff);
+ const HVX_Vector v_gamma_t = Q6_Vh_vsplat_R(gamma_l1[63]);
+ HVX_Vector diff_log2e = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vsub_VhfVhf(v_gamma_t, v_gamma), v_log2e_f16));
+ diff_log2e = Q6_Vhf_vmax_VhfVhf(v_neg20_l2e, Q6_Vhf_vmin_VhfVhf(v_zero_f16, diff_log2e));
- HVX_Vector diff_log2e = hvx_vec_mul_f16_f16(diff, v_log2e_f16);
- HVX_Vector v_exp = hvx_vec_exp2_neg_f16(diff_log2e);
+ const HVX_Vector v_exp = hvx_vec_exp2_neg_f16(diff_log2e);
- HVX_VectorPred mask_lt_t = Q6_Q_vsetq2_R(2 * 63);
- HVX_Vector v_m = Q6_V_vand_QV(mask_lt_t, v_exp);
+ const HVX_VectorPred mask_lt_t = Q6_Q_vsetq2_R(2 * 63);
+ const HVX_Vector v_m = Q6_V_vand_QV(mask_lt_t, v_exp);
- HVX_VectorPred mask_le_t = Q6_Q_vcmp_eq_VhVh(v_zero_f16, v_zero_f16);
- HVX_VectorPred mask_diag = Q6_Q_and_QQn(mask_le_t, mask_lt_t);
- HVX_Vector v_a = Q6_V_vmux_QVV(mask_diag, v_one_f16, v_m);
+ const HVX_VectorPred mask_le_t = Q6_Q_vcmp_eq_VhVh(v_zero_f16, v_zero_f16);
+ const HVX_VectorPred mask_diag = Q6_Q_and_QQn(mask_le_t, mask_lt_t);
- hvx_vmem(head->decay_m + 63 * 64) = v_m;
- hvx_vmem(head->decay_a + 63 * 64) = v_a;
+ p_decay_a[63] = Q6_V_vmux_QVV(mask_diag, v_one_f16, v_m);
}
-
- htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, info);
}
-static void gdn_hvx_phase2_worker(const htp_thread_t * th, unsigned int n, unsigned int i, void * data) {
- (void) n;
- struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data;
- struct htp_thread_trace * tr = th->trace;
- const uint16_t info = (uint16_t) bctx->c;
- htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_SOLVE, info);
-
- struct htp_gdn_head_ptrs * head = &bctx->heads[i];
- const uint32_t curr_buf = bctx->curr_buf;
-
- gdn_unpack_64x64_tiles_to_vectors(head->rows_kk, head->kk_tiles);
-
+static __attribute__((noinline)) void gdn_hvx_phase2(
+ struct htp_gdn_head_ptrs * head,
+ uint16_t * restrict l_l2_buf
+) {
gdn_build_inv_l_blocks(
head->inv_row_tiles,
- head->rows_kk,
- head->decay_m,
- head->b_f32[curr_buf],
+ head->kk_tiles,
+ head->decay_a,
+ head->b_f32,
(__fp16 *) head->vtcm_m,
(__fp16 *) head->vtcm_m + HMX_FP16_TILE_N_ELMS,
- head->rows_inv
+ l_l2_buf
);
+}
- htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_SOLVE, info);
-}
-
-static void gdn_hvx_phase3_worker(const htp_thread_t * th, unsigned int n, unsigned int i, void * data) {
- (void) n;
- struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data;
- struct htp_ops_context * octx = bctx->octx;
- const struct htp_gdn_kernel_params * kparams = bctx->kparams;
- struct htp_thread_trace * tr = th->trace;
- const uint32_t c = bctx->c;
- const uint16_t info = (uint16_t) c;
- htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_V_PREP, info);
-
- struct htp_gdn_head_ptrs * head = &bctx->heads[i];
- const uint32_t curr_buf = bctx->curr_buf;
- const uint32_t S_v = bctx->S_v;
- const uint32_t n_col_tiles = S_v >> 5;
- const HVX_Vector v_b_lo = hvx_vmem(head->b_f32[curr_buf] + 0);
- const HVX_Vector v_b_hi = hvx_vmem(head->b_f32[curr_buf] + 32);
-
- if (octx->src[2]->type == HTP_TYPE_F16) {
- const __fp16 * v_f16 = (const __fp16 *) head->v_f32[curr_buf];
- for (uint32_t r = 0; r < 64; r += 2) {
- const uint32_t r0 = r >> 5;
- const uint32_t r1 = (r & 31u) >> 1;
- const HVX_Vector v_b = r0 ? v_b_hi : v_b_lo;
- const HVX_Vector vb0 = Q6_V_vsplat_R(Q6_R_vextract_VR(v_b, ((r + 0u) & 31u) * 4u));
- const HVX_Vector vb1 = Q6_V_vsplat_R(Q6_R_vextract_VR(v_b, ((r + 1u) & 31u) * 4u));
-
- for (uint32_t d = 0; d < (S_v >> 6); ++d) {
- const uint32_t c0 = 2 * d;
- const HVX_Vector * t_vi0 = (const HVX_Vector *) (head->v_inter_tiles + (r0 * n_col_tiles + c0 + 0) * HMX_FP16_TILE_N_ELMS);
- const HVX_Vector * t_vi1 = (const HVX_Vector *) (head->v_inter_tiles + (r0 * n_col_tiles + c0 + 1) * HMX_FP16_TILE_N_ELMS);
- const HVX_VectorPair vp_vi = Q6_W_vdeal_VVR(t_vi1[r1], t_vi0[r1], -2);
-
- const HVX_VectorPair p_vi0 = hvx_vec_f16_to_f32(Q6_V_lo_W(vp_vi));
- const HVX_VectorPair p_vv0 = hvx_vec_f16_to_f32(hvx_vmem(v_f16 + (r + 0) * S_v + d * 64));
- const HVX_Vector vp0_lo = hvx_vec_mul_f32_f32(hvx_vec_sub_f32_f32(Q6_V_lo_W(p_vv0), Q6_V_lo_W(p_vi0)), vb0);
- const HVX_Vector vp0_hi = hvx_vec_mul_f32_f32(hvx_vec_sub_f32_f32(Q6_V_hi_W(p_vv0), Q6_V_hi_W(p_vi0)), vb0);
-
- const HVX_VectorPair p_vi1 = hvx_vec_f16_to_f32(Q6_V_hi_W(vp_vi));
- const HVX_VectorPair p_vv1 = hvx_vec_f16_to_f32(hvx_vmem(v_f16 + (r + 1) * S_v + d * 64));
- const HVX_Vector vp1_lo = hvx_vec_mul_f32_f32(hvx_vec_sub_f32_f32(Q6_V_lo_W(p_vv1), Q6_V_lo_W(p_vi1)), vb1);
- const HVX_Vector vp1_hi = hvx_vec_mul_f32_f32(hvx_vec_sub_f32_f32(Q6_V_hi_W(p_vv1), Q6_V_hi_W(p_vi1)), vb1);
-
- HVX_Vector * t_vp0 = (HVX_Vector *) (head->v_prime_col_tiles + ((c0 + 0) * 2 + r0) * HMX_FP16_TILE_N_ELMS);
- HVX_Vector * t_vp1 = (HVX_Vector *) (head->v_prime_col_tiles + ((c0 + 1) * 2 + r0) * HMX_FP16_TILE_N_ELMS);
- t_vp0[r1] = hvx_vec_f32_to_f16_shuff(vp0_lo, vp1_lo);
- t_vp1[r1] = hvx_vec_f32_to_f16_shuff(vp0_hi, vp1_hi);
+static __attribute__((noinline)) void gdn_hvx_phase3(
+ struct htp_gdn_head_ptrs * head,
+ uint32_t curr_buf,
+ uint32_t S_v,
+ bool v_is_f16
+) {
+ const uint32_t n_col_tiles = S_v >> 5;
+ const __fp16 * restrict v_inter = head->v_inter_tiles;
+ __fp16 * restrict v_prime_col = head->v_prime_col_tiles;
+
+ if (v_is_f16) {
+ uint32_t b_pairs_l1[32] __attribute__((aligned(128)));
+ hvx_vmem(b_pairs_l1) = hvx_vec_f32_to_f16(hvx_vmem(head->b_f32 + 0), hvx_vmem(head->b_f32 + 32));
+ const __fp16 * restrict v_in = (const __fp16 *) head->v_buf[curr_buf];
+ for (uint32_t d = 0; d < (S_v >> 6); ++d) {
+ const uint32_t c0 = 2u * d;
+ for (uint32_t r0 = 0; r0 < 2u; ++r0) {
+ const HVX_Vector * restrict t_vi0 = (const HVX_Vector *) (v_inter + (r0 * n_col_tiles + c0 + 0) * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * restrict t_vi1 = (const HVX_Vector *) (v_inter + (r0 * n_col_tiles + c0 + 1) * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict t_vp0 = (HVX_Vector *) (v_prime_col + ((c0 + 0) * 2 + r0) * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict t_vp1 = (HVX_Vector *) (v_prime_col + ((c0 + 1) * 2 + r0) * HMX_FP16_TILE_N_ELMS);
+ const __fp16 * restrict p_v = v_in + (r0 * 32u) * S_v + d * 64u;
+ const uint32_t * restrict p_b = b_pairs_l1 + r0 * 16u;
+
+ #pragma nounroll
+ for (uint32_t r1 = 0; r1 < 16u; ++r1) {
+ const HVX_Vector v_beta_pair = Q6_V_vsplat_R(p_b[r1]);
+ const HVX_Vector v_row0 = hvx_vmem(p_v);
+ const HVX_Vector v_row1 = hvx_vmem(p_v + S_v);
+ p_v += 2u * S_v;
+
+ const HVX_VectorPair vp_v = Q6_W_vshuff_VVR(v_row1, v_row0, -2);
+ t_vp0[r1] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vsub_VhfVhf(Q6_V_lo_W(vp_v), t_vi0[r1]), v_beta_pair));
+ t_vp1[r1] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vsub_VhfVhf(Q6_V_hi_W(vp_v), t_vi1[r1]), v_beta_pair));
+ }
}
}
} else {
- const float * v_f32 = head->v_f32[curr_buf];
- for (uint32_t r = 0; r < 64; r += 2) {
- const uint32_t r0 = r >> 5;
- const uint32_t r1 = (r & 31u) >> 1;
- const HVX_Vector v_b = r0 ? v_b_hi : v_b_lo;
- const HVX_Vector vb0 = Q6_V_vsplat_R(Q6_R_vextract_VR(v_b, ((r + 0u) & 31u) * 4u));
- const HVX_Vector vb1 = Q6_V_vsplat_R(Q6_R_vextract_VR(v_b, ((r + 1u) & 31u) * 4u));
-
- for (uint32_t d = 0; d < (S_v >> 6); ++d) {
- const uint32_t c0 = 2 * d;
- const HVX_Vector * t_vi0 = (const HVX_Vector *) (head->v_inter_tiles + (r0 * n_col_tiles + c0 + 0) * HMX_FP16_TILE_N_ELMS);
- const HVX_Vector * t_vi1 = (const HVX_Vector *) (head->v_inter_tiles + (r0 * n_col_tiles + c0 + 1) * HMX_FP16_TILE_N_ELMS);
- const HVX_VectorPair vp_vi = Q6_W_vdeal_VVR(t_vi1[r1], t_vi0[r1], -2);
-
- const HVX_VectorPair p_vi0 = hvx_vec_f16_to_f32(Q6_V_lo_W(vp_vi));
- const HVX_Vector vv0_lo = hvx_vmem(v_f32 + (r + 0) * S_v + d * 64 + 0);
- const HVX_Vector vv0_hi = hvx_vmem(v_f32 + (r + 0) * S_v + d * 64 + 32);
- const HVX_Vector vp0_lo = hvx_vec_mul_f32_f32(hvx_vec_sub_f32_f32(vv0_lo, Q6_V_lo_W(p_vi0)), vb0);
- const HVX_Vector vp0_hi = hvx_vec_mul_f32_f32(hvx_vec_sub_f32_f32(vv0_hi, Q6_V_hi_W(p_vi0)), vb0);
-
- const HVX_VectorPair p_vi1 = hvx_vec_f16_to_f32(Q6_V_hi_W(vp_vi));
- const HVX_Vector vv1_lo = hvx_vmem(v_f32 + (r + 1) * S_v + d * 64 + 0);
- const HVX_Vector vv1_hi = hvx_vmem(v_f32 + (r + 1) * S_v + d * 64 + 32);
- const HVX_Vector vp1_lo = hvx_vec_mul_f32_f32(hvx_vec_sub_f32_f32(vv1_lo, Q6_V_lo_W(p_vi1)), vb1);
- const HVX_Vector vp1_hi = hvx_vec_mul_f32_f32(hvx_vec_sub_f32_f32(vv1_hi, Q6_V_hi_W(p_vi1)), vb1);
-
- HVX_Vector * t_vp0 = (HVX_Vector *) (head->v_prime_col_tiles + ((c0 + 0) * 2 + r0) * HMX_FP16_TILE_N_ELMS);
- HVX_Vector * t_vp1 = (HVX_Vector *) (head->v_prime_col_tiles + ((c0 + 1) * 2 + r0) * HMX_FP16_TILE_N_ELMS);
- t_vp0[r1] = hvx_vec_f32_to_f16_shuff(vp0_lo, vp1_lo);
- t_vp1[r1] = hvx_vec_f32_to_f16_shuff(vp0_hi, vp1_hi);
+ uint32_t b_f32_l1[64] __attribute__((aligned(128)));
+ hvx_vmem(b_f32_l1 + 0) = hvx_vmem(head->b_f32 + 0);
+ hvx_vmem(b_f32_l1 + 32) = hvx_vmem(head->b_f32 + 32);
+ const HVX_Vector v_one_hf = Q6_Vh_vsplat_R(0x3C00);
+ const float * restrict v_f32 = (const float *) head->v_buf[curr_buf];
+ for (uint32_t d = 0; d < (S_v >> 6); ++d) {
+ const uint32_t c0 = 2u * d;
+ for (uint32_t r0 = 0; r0 < 2u; ++r0) {
+ const HVX_Vector * restrict t_vi0 = (const HVX_Vector *) (v_inter + (r0 * n_col_tiles + c0 + 0) * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * restrict t_vi1 = (const HVX_Vector *) (v_inter + (r0 * n_col_tiles + c0 + 1) * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict t_vp0 = (HVX_Vector *) (v_prime_col + ((c0 + 0) * 2 + r0) * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict t_vp1 = (HVX_Vector *) (v_prime_col + ((c0 + 1) * 2 + r0) * HMX_FP16_TILE_N_ELMS);
+ const float * restrict p_v = v_f32 + (r0 * 32u) * S_v + d * 64u;
+ const uint32_t * restrict p_b = b_f32_l1 + r0 * 32u;
+
+ #pragma nounroll
+ for (uint32_t r1 = 0; r1 < 16u; ++r1) {
+ const HVX_Vector vb0 = Q6_V_vsplat_R(p_b[2u * r1 + 0u]);
+ const HVX_Vector vb1 = Q6_V_vsplat_R(p_b[2u * r1 + 1u]);
+ const float * p_v1 = p_v + S_v;
+ const HVX_VectorPair p_i0 = Q6_Wqf32_vmpy_VhfVhf(t_vi0[r1], v_one_hf);
+ const HVX_VectorPair p_i1 = Q6_Wqf32_vmpy_VhfVhf(t_vi1[r1], v_one_hf);
+
+ const HVX_Vector q0_lo = Q6_Vqf32_vmpy_VsfVsf(hvx_vec_sub_f32_f32(hvx_vmem(p_v + 0), Q6_Vsf_equals_Vqf32(Q6_V_lo_W(p_i0))), vb0);
+ const HVX_Vector q1_lo = Q6_Vqf32_vmpy_VsfVsf(hvx_vec_sub_f32_f32(hvx_vmem(p_v1 + 0), Q6_Vsf_equals_Vqf32(Q6_V_hi_W(p_i0))), vb1);
+ const HVX_Vector q0_hi = Q6_Vqf32_vmpy_VsfVsf(hvx_vec_sub_f32_f32(hvx_vmem(p_v + 32), Q6_Vsf_equals_Vqf32(Q6_V_lo_W(p_i1))), vb0);
+ const HVX_Vector q1_hi = Q6_Vqf32_vmpy_VsfVsf(hvx_vec_sub_f32_f32(hvx_vmem(p_v1 + 32), Q6_Vsf_equals_Vqf32(Q6_V_hi_W(p_i1))), vb1);
+ p_v += 2u * S_v;
+
+ t_vp0[r1] = Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(q1_lo, q0_lo));
+ t_vp1[r1] = Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(q1_hi, q0_hi));
+ }
}
}
}
- gdn_unpack_64x64_tiles_to_vectors(head->rows_qk, head->qk_tiles);
- for (uint32_t t = 0; t < 64; ++t) {
- HVX_Vector v_decay_a = hvx_vmem(head->decay_a + t * 64);
- head->rows_a[t] = hvx_vec_mul_f16_f16(head->rows_qk[t], v_decay_a);
+ const HVX_Vector * restrict p_decay_a = (const HVX_Vector *) head->decay_a;
+ const HVX_Vector * restrict qk00 = (const HVX_Vector *) (head->qk_tiles + 0 * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * restrict qk10 = (const HVX_Vector *) (head->qk_tiles + 2 * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * restrict qk11 = (const HVX_Vector *) (head->qk_tiles + 3 * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict a00 = (HVX_Vector *) (head->a_row_tiles + 0 * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict a01 = (HVX_Vector *) (head->a_row_tiles + 1 * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict a10 = (HVX_Vector *) (head->a_row_tiles + 2 * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict a11 = (HVX_Vector *) (head->a_row_tiles + 3 * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector vzero = Q6_V_vzero();
+
+ for (uint32_t r = 0; r < 16u; ++r) {
+ const HVX_VectorPair ds = Q6_W_vshuff_VVR(p_decay_a[2u * r + 1u], p_decay_a[2u * r + 0u], -2);
+ a00[r] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(qk00[r], Q6_V_lo_W(ds)));
+ a01[r] = vzero;
}
- gdn_pack_64x64_vectors_to_tiles(head->a_row_tiles, head->rows_a);
-
- htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_V_PREP, info);
-
- dma_queue * dma_q = th->dma_q;
- const uint32_t n_chunks = kparams->n_chunks;
- if (c + 1u < n_chunks) {
- dma_queue_pop(dma_q);
- dma_queue_pop(dma_q);
- dma_queue_pop(dma_q);
- if (i == 0u) {
- dma_queue_pop(dma_q);
- dma_queue_pop(dma_q);
- }
- }
- if (c > 0u) {
- dma_queue_pop(dma_q);
- }
- if (c + 2u < n_chunks) {
- const uint32_t chunk_size = kparams->chunk_size;
- const uint32_t t_c2 = (c + 2u) * chunk_size;
- const uint32_t c2_tokens = hex_smin(chunk_size, kparams->n_tokens - t_c2);
- gdn_dma_push_chunk_inputs(dma_q, head->q_f32[curr_buf], head->k_f32[curr_buf], head->v_f32[curr_buf],
- octx->src[0], octx->src[1], octx->src[2],
- head->iq3, head->iq1, head->ik3, head->ik1,
- head->iv3, head->iv1, t_c2, c2_tokens, S_v);
- if (i == 0u) {
- gdn_dma_push_chunk_gb(dma_q, bctx->vtcm_g_raw[curr_buf], bctx->vtcm_b_raw[curr_buf],
- octx->src[3], octx->src[4], bctx->iv3, bctx->head_in_seq,
- t_c2, c2_tokens, bctx->n_batch);
- }
+ for (uint32_t r = 0; r < 16u; ++r) {
+ const HVX_VectorPair ds = Q6_W_vshuff_VVR(p_decay_a[32u + 2u * r + 1u], p_decay_a[32u + 2u * r + 0u], -2);
+ a10[r] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(qk10[r], Q6_V_lo_W(ds)));
+ a11[r] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(qk11[r], Q6_V_hi_W(ds)));
}
}
-static void gdn_hvx_phase4_worker(const htp_thread_t * th, unsigned int n, unsigned int i, void * data) {
- (void) n;
- struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data;
- struct htp_thread_trace * tr = th->trace;
- const uint16_t info = (uint16_t) bctx->c;
- htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
-
- struct htp_gdn_head_ptrs * head = &bctx->heads[i];
- const uint32_t S_v = bctx->S_v;
-
- const HVX_Vector v_decay_last = hvx_vmem(head->decay_a + 63 * 64);
- const HVX_Vector vzero = Q6_V_vzero();
- HVX_Vector * restrict m = head->vtcm_m;
- HVX_Vector * restrict tmp = head->vtcm_tmp;
-
- for (uint32_t col_half = 0; col_half < (S_v >> 6); ++col_half) {
- const uint32_t r0_base = col_half * 2;
- for (uint32_t c0 = 0; c0 < 2; ++c0) {
- // delta_tiles is stored in col-major tile order [col_tile][row_tile]
- const HVX_Vector * src0 = (const HVX_Vector *) (head->delta_tiles + ((2 * col_half + 0) * 2 + c0) * HMX_FP16_TILE_N_ELMS);
- const HVX_Vector * src1 = (const HVX_Vector *) (head->delta_tiles + ((2 * col_half + 1) * 2 + c0) * HMX_FP16_TILE_N_ELMS);
-
- for (uint32_t r = 0; r < 16; ++r) {
- const uint32_t s0 = c0 * 32 + 2 * r + 0;
- const uint32_t s1 = c0 * 32 + 2 * r + 1;
- const HVX_VectorPair vp = Q6_W_vdeal_VVR(src1[r], src0[r], -2);
-
- const HVX_Vector vs0 = Q6_Vh_vsplat_R(gdn_hvx_extract_u16(v_decay_last, s0));
- const HVX_Vector vs1 = Q6_Vh_vsplat_R(gdn_hvx_extract_u16(v_decay_last, s1));
+static __attribute__((noinline)) void gdn_hvx_phase4_range(
+ struct htp_gdn_head_ptrs * head,
+ uint32_t tile_start,
+ uint32_t tile_end
+) {
+ uint32_t decay_last_u32[32] __attribute__((aligned(128)));
+ hvx_vmem(decay_last_u32) = hvx_vmem(head->decay_a + 63 * 64);
+
+ const HVX_Vector vzero = Q6_V_vzero();
+ const __fp16 * restrict delta = head->delta_tiles;
+ __fp16 * restrict d_row = head->d_row_tiles;
+
+ #pragma nounroll
+ for (uint32_t tile = tile_start; tile < tile_end; ++tile) {
+ const uint32_t col_half = tile >> 1;
+ const uint32_t c0 = tile & 1u;
+ const uint32_t r0_base = col_half * 2u;
+
+ // delta_tiles is stored in col-major tile order [col_tile][row_tile]
+ const HVX_Vector * restrict src0 = (const HVX_Vector *) (delta + ((2u * col_half + 0u) * 2u + c0) * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * restrict src1 = (const HVX_Vector *) (delta + ((2u * col_half + 1u) * 2u + c0) * HMX_FP16_TILE_N_ELMS);
+ const uint32_t * dl = decay_last_u32 + c0 * 16u;
+
+ const uint32_t tile0_idx = (r0_base + 0u) * 2u + c0;
+ const uint32_t tile1_idx = (r0_base + 1u) * 2u + c0;
+ HVX_Vector * restrict dst0 = (HVX_Vector *) (d_row + tile0_idx * HMX_FP16_TILE_N_ELMS);
+ HVX_Vector * restrict dst1 = (HVX_Vector *) (d_row + tile1_idx * HMX_FP16_TILE_N_ELMS);
+
+ // Fuse scaling + transpose stage 1 per 8 rows to avoid VTCM roundtrip through vtcm_m
+ #pragma nounroll
+ for (uint32_t g = 0u; g < 4u; ++g) {
+ HVX_Vector rows[8];
+ #pragma unroll(4)
+ for (uint32_t j = 0u; j < 4u; ++j) {
+ const uint32_t r = g * 4u + j;
+ const HVX_VectorPair vp = Q6_W_vdeal_VVR(src1[r], src0[r], -2);
+ const uint32_t w = dl[r];
+ const HVX_Vector vs0 = Q6_Vh_vsplat_R(w & 0xFFFFu);
+ const HVX_Vector vs1 = Q6_Vh_vsplat_R(w >> 16);
const HVX_VectorPred p_z0 = Q6_Q_vcmp_eq_VhVh(vs0, vzero);
const HVX_VectorPred p_z1 = Q6_Q_vcmp_eq_VhVh(vs1, vzero);
- m[2 * r + 0] = Q6_V_vmux_QVV(p_z0, vzero, hvx_vec_mul_f16_f16(Q6_V_lo_W(vp), vs0));
- m[2 * r + 1] = Q6_V_vmux_QVV(p_z1, vzero, hvx_vec_mul_f16_f16(Q6_V_hi_W(vp), vs1));
- }
-
- hvx_transpose_32x32_w_shuff_inplace(m, tmp);
-
- const uint32_t tile0_idx = (r0_base + 0) * 2 + c0;
- const uint32_t tile1_idx = (r0_base + 1) * 2 + c0;
- HVX_Vector * dst0 = (HVX_Vector *) (head->d_row_tiles + tile0_idx * HMX_FP16_TILE_N_ELMS);
- HVX_Vector * dst1 = (HVX_Vector *) (head->d_row_tiles + tile1_idx * HMX_FP16_TILE_N_ELMS);
-
- for (uint32_t r = 0; r < 16; ++r) {
- dst0[r] = m[r];
- dst1[r] = m[16 + r];
+ rows[2u * j + 0u] = Q6_V_vmux_QVV(p_z0, vzero, Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(Q6_V_lo_W(vp), vs0)));
+ rows[2u * j + 1u] = Q6_V_vmux_QVV(p_z1, vzero, Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_VhfVhf(Q6_V_hi_W(vp), vs1)));
}
+ gdn_transpose_stage1_8rows(dst0, dst1, g,
+ rows[0], rows[1], rows[2], rows[3],
+ rows[4], rows[5], rows[6], rows[7]);
}
- }
- htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
+ gdn_transpose_stage2_pair(dst0, dst1);
+ }
}
-static void gdn_hvx_phase5_worker(const htp_thread_t * th, unsigned int n, unsigned int i, void * data) {
- (void) n;
- struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data;
- const struct htp_gdn_kernel_params * kparams = bctx->kparams;
- const struct htp_tensor * dst = bctx->octx->dst;
- struct htp_thread_trace * tr = th->trace;
- const uint32_t c = bctx->c;
- const uint16_t info = (uint16_t) c;
- htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_OUT, info);
-
- struct htp_gdn_head_ptrs * head = &bctx->heads[i];
- const uint32_t curr_buf = bctx->curr_buf;
- const uint32_t S_v = bctx->S_v;
- const uint32_t n_col_tiles = S_v >> 5;
- const HVX_Vector vscale = hvx_vec_splat_f32(bctx->scale);
+static __attribute__((noinline)) void gdn_hvx_phase5(
+ struct htp_gdn_head_ptrs * head,
+ uint32_t curr_buf,
+ uint32_t S_v,
+ float scale,
+ bool dst_is_f16
+) {
+ const uint32_t n_col_tiles = S_v >> 5;
+ const HVX_Vector vscale_f32 = hvx_vec_splat_f32(scale);
+ const HVX_Vector vscale_hf = hvx_vec_f32_to_f16_shuff(vscale_f32, vscale_f32);
+ const __fp16 * restrict o_inter = head->o_inter_tiles;
+ const __fp16 * restrict o_intra = head->o_intra_tiles;
- if (dst->type == HTP_TYPE_F16) {
- __fp16 * o_f16 = (__fp16 *) head->o_f32[curr_buf];
- const HVX_Vector v_min = hvx_vec_splat_f32(-65504.0f);
- const HVX_Vector v_max = hvx_vec_splat_f32(65504.0f);
+ if (dst_is_f16) {
+ __fp16 * restrict o_out = (__fp16 *) head->o_buf[curr_buf];
for (uint32_t r0 = 0; r0 < 2; ++r0) {
for (uint32_t d = 0; d < (S_v >> 6); ++d) {
- const HVX_Vector * ti0 = (const HVX_Vector *) (head->o_inter_tiles + (r0 * n_col_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS);
- const HVX_Vector * ti1 = (const HVX_Vector *) (head->o_inter_tiles + (r0 * n_col_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS);
- const HVX_Vector * ta0 = (const HVX_Vector *) (head->o_intra_tiles + (r0 * n_col_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS);
- const HVX_Vector * ta1 = (const HVX_Vector *) (head->o_intra_tiles + (r0 * n_col_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS);
-
- for (uint32_t r = 0; r < 16; ++r) {
- const HVX_VectorPair vp_i = Q6_W_vdeal_VVR(ti1[r], ti0[r], -2);
- const HVX_VectorPair vp_a = Q6_W_vdeal_VVR(ta1[r], ta0[r], -2);
-
- const HVX_VectorPair pi0 = hvx_vec_f16_to_f32(Q6_V_lo_W(vp_i));
- const HVX_VectorPair pa0 = hvx_vec_f16_to_f32(Q6_V_lo_W(vp_a));
- const HVX_Vector vo0_lo = gdn_clamp_f16_range_f32(hvx_vec_mul_f32_f32(hvx_vec_add_f32_f32(Q6_V_lo_W(pi0), Q6_V_lo_W(pa0)), vscale), v_min, v_max);
- const HVX_Vector vo0_hi = gdn_clamp_f16_range_f32(hvx_vec_mul_f32_f32(hvx_vec_add_f32_f32(Q6_V_hi_W(pi0), Q6_V_hi_W(pa0)), vscale), v_min, v_max);
-
- const HVX_VectorPair pi1 = hvx_vec_f16_to_f32(Q6_V_hi_W(vp_i));
- const HVX_VectorPair pa1 = hvx_vec_f16_to_f32(Q6_V_hi_W(vp_a));
- const HVX_Vector vo1_lo = gdn_clamp_f16_range_f32(hvx_vec_mul_f32_f32(hvx_vec_add_f32_f32(Q6_V_lo_W(pi1), Q6_V_lo_W(pa1)), vscale), v_min, v_max);
- const HVX_Vector vo1_hi = gdn_clamp_f16_range_f32(hvx_vec_mul_f32_f32(hvx_vec_add_f32_f32(Q6_V_hi_W(pi1), Q6_V_hi_W(pa1)), vscale), v_min, v_max);
-
- __fp16 * out0 = o_f16 + (r0 * 32 + 2 * r + 0) * S_v + d * 64;
- __fp16 * out1 = o_f16 + (r0 * 32 + 2 * r + 1) * S_v + d * 64;
-
- hvx_vmem(out0) = hvx_vec_f32_to_f16(vo0_lo, vo0_hi);
- hvx_vmem(out1) = hvx_vec_f32_to_f16(vo1_lo, vo1_hi);
+ const HVX_Vector * restrict ti0 = (const HVX_Vector *) (o_inter + (r0 * n_col_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * restrict ti1 = (const HVX_Vector *) (o_inter + (r0 * n_col_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * restrict ta0 = (const HVX_Vector *) (o_intra + (r0 * n_col_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * restrict ta1 = (const HVX_Vector *) (o_intra + (r0 * n_col_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS);
+ __fp16 * restrict out0 = o_out + (r0 * 32u) * S_v + d * 64u;
+ __fp16 * restrict out1 = out0 + S_v;
+
+ #pragma nounroll
+ for (uint32_t r = 0; r < 16u; r += 2u) {
+ const HVX_Vector o0a = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vadd_VhfVhf(ti0[r + 0u], ta0[r + 0u]), vscale_hf));
+ const HVX_Vector o1a = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vadd_VhfVhf(ti1[r + 0u], ta1[r + 0u]), vscale_hf));
+ const HVX_Vector o0b = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vadd_VhfVhf(ti0[r + 1u], ta0[r + 1u]), vscale_hf));
+ const HVX_Vector o1b = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vadd_VhfVhf(ti1[r + 1u], ta1[r + 1u]), vscale_hf));
+ const HVX_VectorPair dealta = Q6_W_vdeal_VVR(o1a, o0a, -2);
+ const HVX_VectorPair dealtb = Q6_W_vdeal_VVR(o1b, o0b, -2);
+ hvx_vmem(out0) = Q6_V_lo_W(dealta);
+ hvx_vmem(out1) = Q6_V_hi_W(dealta);
+ hvx_vmem(out0 + 2u * S_v) = Q6_V_lo_W(dealtb);
+ hvx_vmem(out1 + 2u * S_v) = Q6_V_hi_W(dealtb);
+ out0 += 4u * S_v;
+ out1 += 4u * S_v;
}
}
}
} else {
- float * o_f32 = head->o_f32[curr_buf];
+ float * restrict o_f32 = (float *) head->o_buf[curr_buf];
+ const HVX_Vector v_one_hf = Q6_Vh_vsplat_R(0x3C00);
for (uint32_t r0 = 0; r0 < 2; ++r0) {
for (uint32_t d = 0; d < (S_v >> 6); ++d) {
- const HVX_Vector * ti0 = (const HVX_Vector *) (head->o_inter_tiles + (r0 * n_col_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS);
- const HVX_Vector * ti1 = (const HVX_Vector *) (head->o_inter_tiles + (r0 * n_col_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS);
- const HVX_Vector * ta0 = (const HVX_Vector *) (head->o_intra_tiles + (r0 * n_col_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS);
- const HVX_Vector * ta1 = (const HVX_Vector *) (head->o_intra_tiles + (r0 * n_col_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS);
-
- for (uint32_t r = 0; r < 16; ++r) {
- const HVX_VectorPair vp_i = Q6_W_vdeal_VVR(ti1[r], ti0[r], -2);
- const HVX_VectorPair vp_a = Q6_W_vdeal_VVR(ta1[r], ta0[r], -2);
-
- const HVX_VectorPair pi0 = hvx_vec_f16_to_f32(Q6_V_lo_W(vp_i));
- const HVX_VectorPair pa0 = hvx_vec_f16_to_f32(Q6_V_lo_W(vp_a));
- const HVX_VectorPair pi1 = hvx_vec_f16_to_f32(Q6_V_hi_W(vp_i));
- const HVX_VectorPair pa1 = hvx_vec_f16_to_f32(Q6_V_hi_W(vp_a));
-
- float * out0 = o_f32 + (r0 * 32 + 2 * r + 0) * S_v + d * 64;
- float * out1 = o_f32 + (r0 * 32 + 2 * r + 1) * S_v + d * 64;
-
- hvx_vmem(out0 + 0) = hvx_vec_mul_f32_f32(hvx_vec_add_f32_f32(Q6_V_lo_W(pi0), Q6_V_lo_W(pa0)), vscale);
- hvx_vmem(out0 + 32) = hvx_vec_mul_f32_f32(hvx_vec_add_f32_f32(Q6_V_hi_W(pi0), Q6_V_hi_W(pa0)), vscale);
- hvx_vmem(out1 + 0) = hvx_vec_mul_f32_f32(hvx_vec_add_f32_f32(Q6_V_lo_W(pi1), Q6_V_lo_W(pa1)), vscale);
- hvx_vmem(out1 + 32) = hvx_vec_mul_f32_f32(hvx_vec_add_f32_f32(Q6_V_hi_W(pi1), Q6_V_hi_W(pa1)), vscale);
+ const HVX_Vector * restrict ti0 = (const HVX_Vector *) (o_inter + (r0 * n_col_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * restrict ti1 = (const HVX_Vector *) (o_inter + (r0 * n_col_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * restrict ta0 = (const HVX_Vector *) (o_intra + (r0 * n_col_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * restrict ta1 = (const HVX_Vector *) (o_intra + (r0 * n_col_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS);
+ float * restrict out0 = o_f32 + (r0 * 32u) * S_v + d * 64u;
+ float * restrict out1 = out0 + S_v;
+
+ #pragma nounroll
+ for (uint32_t r = 0; r < 16u; ++r) {
+ const HVX_VectorPair pi0 = Q6_Wqf32_vmpy_VhfVhf(ti0[r], v_one_hf);
+ const HVX_VectorPair pa0 = Q6_Wqf32_vmpy_VhfVhf(ta0[r], v_one_hf);
+ const HVX_VectorPair pi1 = Q6_Wqf32_vmpy_VhfVhf(ti1[r], v_one_hf);
+ const HVX_VectorPair pa1 = Q6_Wqf32_vmpy_VhfVhf(ta1[r], v_one_hf);
+
+ hvx_vmem(out0 + 0) = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(Q6_V_lo_W(pi0), Q6_V_lo_W(pa0))), vscale_f32));
+ hvx_vmem(out0 + 32) = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(Q6_V_lo_W(pi1), Q6_V_lo_W(pa1))), vscale_f32));
+ hvx_vmem(out1 + 0) = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(Q6_V_hi_W(pi0), Q6_V_hi_W(pa0))), vscale_f32));
+ hvx_vmem(out1 + 32) = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(Q6_V_hi_W(pi1), Q6_V_hi_W(pa1))), vscale_f32));
+ out0 += 2u * S_v;
+ out1 += 2u * S_v;
}
}
}
}
+}
- htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_OUT, info);
+static __attribute__((noinline)) void gdn_hvx_phase6(
+ struct htp_gdn_head_ptrs * head,
+ uint32_t curr_buf,
+ uint32_t c,
+ uint32_t n_chunks,
+ uint32_t S_v
+) {
+ const uint32_t n_tiles = S_v >> 5;
+ const HVX_Vector v_l_final = hvx_splat_last_f32(hvx_vmem(head->lambda_init[curr_buf] + 32));
+ const HVX_Vector v_one_hf = Q6_Vh_vsplat_R(0x3C00);
+ const bool pack_next = (c + 1u < n_chunks);
+ const __fp16 * restrict s_upd = head->s_update_tiles;
+ float * restrict s_state = head->s_state;
- const uint32_t chunk_size = kparams->chunk_size;
- const uint32_t t_chunk = c * chunk_size;
- const uint32_t valid_tokens = hex_smin(chunk_size, kparams->n_tokens - t_chunk);
- const uint32_t row_out_bytes = S_v * ((dst->type == HTP_TYPE_F16) ? sizeof(__fp16) : sizeof(float));
- const dma_addr_t attn_chunk_dma = dst->data +
- ((size_t) head->iv3 * kparams->n_tokens * kparams->H + (size_t) t_chunk * kparams->H + head->iv1) * row_out_bytes;
- dma_queue_push(th->dma_q, dma_make_data(attn_chunk_dma, head->o_f32[curr_buf]),
- dst->nb[1], row_out_bytes, row_out_bytes, valid_tokens);
-}
+ if (pack_next) {
+ __fp16 * restrict s_col_base = head->s_col_tiles;
+ for (uint32_t r0 = 0; r0 < n_tiles; ++r0) {
+ for (uint32_t d = 0; d < (S_v >> 6); ++d) {
+ const HVX_Vector * restrict t0 = (const HVX_Vector *) (s_upd + (r0 * n_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * restrict t1 = (const HVX_Vector *) (s_upd + (r0 * n_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS);
+ float * restrict s0 = s_state + (r0 * 32u) * S_v + d * 64u;
+ float * restrict s1 = s0 + S_v;
+ HVX_Vector * restrict tile_base = (HVX_Vector *) (s_col_base + (size_t) (r0 * n_tiles + 2 * d) * HMX_FP16_TILE_N_ELMS);
+
+ // Fuse FP16 conversion + transpose stage 1 per 8 rows to eliminate vtcm_m roundtrip
+ #pragma nounroll
+ for (uint32_t g = 0u; g < 4u; ++g) {
+ HVX_Vector rows[8];
+ #pragma unroll(4)
+ for (uint32_t j = 0u; j < 4u; ++j) {
+ const uint32_t r = g * 4u + j;
+ const HVX_VectorPair p_t0 = Q6_Wqf32_vmpy_VhfVhf(t0[r], v_one_hf);
+ const HVX_VectorPair p_t1 = Q6_Wqf32_vmpy_VhfVhf(t1[r], v_one_hf);
+
+ const HVX_Vector q0_lo = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(hvx_vmem(s0 + 0), v_l_final), Q6_V_lo_W(p_t0));
+ const HVX_Vector q0_hi = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(hvx_vmem(s0 + 32), v_l_final), Q6_V_lo_W(p_t1));
+ const HVX_Vector q1_lo = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(hvx_vmem(s1 + 0), v_l_final), Q6_V_hi_W(p_t0));
+ const HVX_Vector q1_hi = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(hvx_vmem(s1 + 32), v_l_final), Q6_V_hi_W(p_t1));
+
+ hvx_vmem(s0 + 0) = Q6_Vsf_equals_Vqf32(q0_lo);
+ hvx_vmem(s0 + 32) = Q6_Vsf_equals_Vqf32(q0_hi);
+ hvx_vmem(s1 + 0) = Q6_Vsf_equals_Vqf32(q1_lo);
+ hvx_vmem(s1 + 32) = Q6_Vsf_equals_Vqf32(q1_hi);
+ s0 += 2u * S_v;
+ s1 += 2u * S_v;
+
+ rows[2u * j + 0u] = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(q0_hi, q0_lo)));
+ rows[2u * j + 1u] = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(q1_hi, q1_lo)));
+ }
+ gdn_transpose_stage1_8rows(tile_base, tile_base + 16, g,
+ rows[0], rows[1], rows[2], rows[3],
+ rows[4], rows[5], rows[6], rows[7]);
+ }
-static void gdn_hvx_phase6_worker(const htp_thread_t * th, unsigned int n, unsigned int i, void * data) {
- (void) n;
- struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data;
- struct htp_thread_trace * tr = th->trace;
- const uint16_t info = (uint16_t) bctx->c;
- htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_STATE, info);
-
- struct htp_gdn_head_ptrs * head = &bctx->heads[i];
- const uint32_t curr_buf = bctx->curr_buf;
- const uint32_t S_v = bctx->S_v;
- const uint32_t c = bctx->c;
- const uint32_t n_chunks = bctx->kparams->n_chunks;
- const uint32_t n_tiles = S_v >> 5;
- const HVX_Vector v_l_final = hvx_vec_splat_f32(gdn_vtcm_load_f32(head->lambda_init[curr_buf], 63));
- const bool pack_next = (c + 1 < n_chunks);
- const HVX_Vector v_scat_base = hvx_vmem(hmx_transpose_scatter_offsets);
- const HVX_Vector v_scat_step = Q6_V_vsplat_R(4);
- const size_t pair_region = (size_t) (2 * HMX_FP16_TILE_SIZE - 1);
-
- for (uint32_t r0 = 0; r0 < n_tiles; ++r0) {
- for (uint32_t d = 0; d < (S_v >> 6); ++d) {
- const HVX_Vector * t0 = (const HVX_Vector *) (head->s_update_tiles + (r0 * n_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS);
- const HVX_Vector * t1 = (const HVX_Vector *) (head->s_update_tiles + (r0 * n_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS);
- __fp16 * tile_base = head->s_col_tiles + (size_t) (r0 * n_tiles + 2 * d) * HMX_FP16_TILE_N_ELMS;
-
- for (uint32_t r = 0; r < 16; ++r) {
- const HVX_VectorPair vp01 = Q6_W_vdeal_VVR(t1[r], t0[r], -2);
- const HVX_VectorPair p0 = hvx_vec_f16_to_f32(Q6_V_lo_W(vp01));
- const HVX_VectorPair p1 = hvx_vec_f16_to_f32(Q6_V_hi_W(vp01));
-
- float * s0 = head->s_state + (r0 * 32 + 2 * r + 0) * S_v + d * 64;
- float * s1 = head->s_state + (r0 * 32 + 2 * r + 1) * S_v + d * 64;
-
- const HVX_Vector ns0_lo = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(hvx_vmem(s0 + 0), v_l_final), Q6_V_lo_W(p0));
- const HVX_Vector ns0_hi = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(hvx_vmem(s0 + 32), v_l_final), Q6_V_hi_W(p0));
- const HVX_Vector ns1_lo = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(hvx_vmem(s1 + 0), v_l_final), Q6_V_lo_W(p1));
- const HVX_Vector ns1_hi = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(hvx_vmem(s1 + 32), v_l_final), Q6_V_hi_W(p1));
-
- hvx_vmem(s0 + 0) = ns0_lo;
- hvx_vmem(s0 + 32) = ns0_hi;
- hvx_vmem(s1 + 0) = ns1_lo;
- hvx_vmem(s1 + 32) = ns1_hi;
-
- if (pack_next) {
- const HVX_Vector v_off0 = Q6_Vw_vadd_VwVw(v_scat_base, Q6_V_vsplat_R(2 * r * 4));
- const HVX_Vector v_off1 = Q6_Vw_vadd_VwVw(v_off0, v_scat_step);
- const HVX_Vector v0_f16 = hvx_vec_f32_to_f16(ns0_lo, ns0_hi);
- const HVX_Vector v1_f16 = hvx_vec_f32_to_f16(ns1_lo, ns1_hi);
- Q6_vscatter_RMVwV((size_t) tile_base, pair_region, v_off0, v0_f16);
- Q6_vscatter_RMVwV((size_t) tile_base, pair_region, v_off1, v1_f16);
+ gdn_transpose_stage2_pair(tile_base, tile_base + 16);
+ }
+ }
+ } else {
+ for (uint32_t r0 = 0; r0 < n_tiles; ++r0) {
+ for (uint32_t d = 0; d < (S_v >> 6); ++d) {
+ const HVX_Vector * restrict t0 = (const HVX_Vector *) (s_upd + (r0 * n_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS);
+ const HVX_Vector * restrict t1 = (const HVX_Vector *) (s_upd + (r0 * n_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS);
+ float * restrict s0 = s_state + (r0 * 32u) * S_v + d * 64u;
+ float * restrict s1 = s0 + S_v;
+
+ #pragma nounroll
+ for (uint32_t r = 0; r < 16u; ++r) {
+ const HVX_VectorPair p_t0 = Q6_Wqf32_vmpy_VhfVhf(t0[r], v_one_hf);
+ const HVX_VectorPair p_t1 = Q6_Wqf32_vmpy_VhfVhf(t1[r], v_one_hf);
+
+ const HVX_Vector q0_lo = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(hvx_vmem(s0 + 0), v_l_final), Q6_V_lo_W(p_t0));
+ const HVX_Vector q0_hi = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(hvx_vmem(s0 + 32), v_l_final), Q6_V_lo_W(p_t1));
+ const HVX_Vector q1_lo = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(hvx_vmem(s1 + 0), v_l_final), Q6_V_hi_W(p_t0));
+ const HVX_Vector q1_hi = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(hvx_vmem(s1 + 32), v_l_final), Q6_V_hi_W(p_t1));
+
+ hvx_vmem(s0 + 0) = Q6_Vsf_equals_Vqf32(q0_lo);
+ hvx_vmem(s0 + 32) = Q6_Vsf_equals_Vqf32(q0_hi);
+ hvx_vmem(s1 + 0) = Q6_Vsf_equals_Vqf32(q1_lo);
+ hvx_vmem(s1 + 32) = Q6_Vsf_equals_Vqf32(q1_hi);
+ s0 += 2u * S_v;
+ s1 += 2u * S_v;
}
}
}
}
+}
- if (pack_next) {
- hvx_gather_sync(head->s_col_tiles);
- }
+struct htp_gdn_persistent_ctx {
+ const struct htp_gdn_hmx_vtcm_layout * L;
+ uint8_t * vtcm_base;
+ uint16_t * ddr_spad;
+ float * vtcm_g_raw[2][2];
+ float * vtcm_b_raw[2][2];
+ uint8_t * vtcm_scales_1;
+ hmx_queue_t hmx_q;
+ gdn_barrier_t * barrier;
+ struct htp_gdn_head_ptrs * heads_a;
+ struct htp_gdn_head_ptrs * heads_b;
+ struct htp_gdn_hmx_batch_task * hmx_tasks;
- htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_STATE, info);
-}
+ struct htp_ops_context * octx;
+ const struct htp_gdn_kernel_params * kparams;
+ const void * ids_buf;
+ const int32_t * n_rep_buf;
+ uint32_t ids_type;
+ uint32_t n_pend;
+ bool replay;
+ bool keep;
+ bool q_is_f16;
+ bool k_is_f16;
+ bool v_is_f16;
+ bool dst_is_f16;
+
+ uint32_t row_start;
+ uint32_t nrows;
+ uint32_t n_heads_group;
+ uint32_t n_groups;
+ uint32_t n_sv_tiles;
+ uint32_t row_out_bytes;
+ uint32_t replay_cap;
+};
+
+static void gdn_hvx_persistent_worker(const htp_thread_t * th, unsigned int n, unsigned int i, void * data) {
+ struct htp_gdn_persistent_ctx * pctx = (struct htp_gdn_persistent_ctx *) data;
+ struct htp_ops_context * octx = pctx->octx;
+ const struct htp_gdn_kernel_params * kparams = pctx->kparams;
+ struct htp_thread_trace * tr = th->trace;
+ dma_queue * dma_q = th->dma_q;
+ hmx_queue_t hmx_q = pctx->hmx_q;
+ gdn_barrier_t * barrier = pctx->barrier;
+ const uint32_t n_workers = (uint32_t) n;
+ const uint32_t H = kparams->H;
+ const uint32_t S_v = kparams->S_v;
+ const uint32_t n_sv_t = pctx->n_sv_tiles;
+ const uint32_t half_sv_t = n_sv_t >> 1;
+ const uint32_t n_tokens = kparams->n_tokens;
+ const uint32_t chunk_size = kparams->chunk_size;
+ const uint32_t n_chunks = kparams->n_chunks;
+ const uint32_t max_group = pctx->n_heads_group;
+ const uint32_t n_groups = pctx->n_groups;
+ const uint32_t row_out_bytes = pctx->row_out_bytes;
+ const float scale = kparams->scale;
+ const bool q_is_f16 = pctx->q_is_f16;
+ const bool k_is_f16 = pctx->k_is_f16;
+ const bool v_is_f16 = pctx->v_is_f16;
+ const bool dst_is_f16 = pctx->dst_is_f16;
+ const bool replay = pctx->replay;
+ const bool keep = pctx->keep;
+ uint8_t * const scales_1 = pctx->vtcm_scales_1;
+ struct htp_gdn_head_ptrs * heads_a = pctx->heads_a;
+ struct htp_gdn_head_ptrs * heads_b = pctx->heads_b;
+ struct htp_gdn_hmx_batch_task * tasks = pctx->hmx_tasks;
+ uint32_t seq = 0u;
+ uint16_t * const l_l2_buf = pctx->ddr_spad + (size_t) i * 2176u;
+
+ const struct htp_tensor * q = octx->src[0];
+ const struct htp_tensor * k = octx->src[1];
+ const struct htp_tensor * v = octx->src[2];
+ const struct htp_tensor * g = octx->src[3];
+ const struct htp_tensor * beta = octx->src[4];
+ const struct htp_tensor * state = octx->src[5];
+ const struct htp_tensor * dst = octx->dst;
+ const struct htp_tensor * dst_cache = octx->dsts[1];
+
+ const bool dual_hvx = (n_workers >= 2u * max_group) && pctx->L->separate_aliases;
+ const uint32_t sub = (dual_hvx && i >= max_group) ? 1u : 0u;
+ const uint32_t h = dual_hvx ? (i - sub * max_group) : i;
+
+ const uint32_t row_end = pctx->row_start + pctx->nrows;
+ uint32_t step_heads = 1u;
+
+ for (uint32_t r = pctx->row_start; r < row_end; r += step_heads) {
+ const uint32_t head_in_seq = fastmodulo(r, H, &kparams->div_H);
+ const uint32_t iv3 = fastdiv(r, &kparams->div_H);
+ const uint32_t is3 = gdn_read_seq_idx(pctx->ids_buf, pctx->ids_type, iv3);
+ const uint32_t heads_left_in_seq = H - head_in_seq;
+ const uint32_t heads_left_in_range = row_end - r;
+ const uint32_t rem = hex_smin(heads_left_in_seq, heads_left_in_range);
+ const uint32_t cur_a = hex_smin(max_group, rem);
+ const uint32_t cur_b = (n_groups >= 2u) ? hex_smin(max_group, rem - cur_a) : 0u;
+ step_heads = cur_a + cur_b;
+
+ const bool active_a = (h < cur_a);
+ const bool active_b = (h < cur_b);
+ struct htp_gdn_head_ptrs * head_a = active_a ? &heads_a[h] : NULL;
+ struct htp_gdn_head_ptrs * head_b = active_b ? &heads_b[h] : NULL;
+
+ // Prologue: DMA state + chunk 0 for both groups, overlap B setup/phase1 with H1_FULL(A, 0)
+ const uint32_t chunk0_tokens = hex_smin(chunk_size, n_tokens);
+ const uint32_t chunk1_tokens = (n_chunks > 1u) ? hex_smin(chunk_size, n_tokens - chunk_size) : 0u;
+
+ if (replay) {
+ // Replay drains state DMAs sequentially before pushing chunk 0 inputs to preserve DMA FIFO order
+ if (active_a && sub == 0u) {
+ gdn_init_head_ptrs(head_a, pctx->L, pctx->vtcm_base, h, 0u, head_in_seq + h, iv3, is3,
+ q, k, state, dst, dst_cache, kparams, S_v, H, n_tokens);
+ dma_queue_push(dma_q, dma_make_data(head_a->s_state, head_a->state_in_dma),
+ S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v);
+ dma_queue_pop(dma_q);
+ const int32_t raw_n_rep = pctx->n_rep_buf[iv3];
+ const uint32_t n_steps = raw_n_rep <= 0 ? 0 : hex_smin((uint32_t) raw_n_rep, pctx->n_pend);
+ if (n_steps > 0u) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, 0);
+ gdn_replay_f32(octx, dma_q, head_a->s_state,
+ (float *) head_a->k_buf[0], (float *) head_a->v_buf[0], head_a->g_f32, head_a->b_f32,
+ head_a->vtcm_m, pctx->replay_cap,
+ head_a->ik1, head_a->iv1, head_a->iv3, n_steps, pctx->n_pend,
+ kparams->kda != 0, S_v);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, 0);
+ }
+ if (keep) {
+ dma_queue_push(dma_q, dma_make_data(head_a->state_out_dma, head_a->s_state),
+ S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v);
+ dma_queue_pop(dma_q);
+ }
+ }
+ if (active_b && sub == 0u) {
+ gdn_init_head_ptrs(head_b, pctx->L, pctx->vtcm_base, max_group + h, 1u, head_in_seq + cur_a + h, iv3, is3,
+ q, k, state, dst, dst_cache, kparams, S_v, H, n_tokens);
+ dma_queue_push(dma_q, dma_make_data(head_b->s_state, head_b->state_in_dma),
+ S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v);
+ dma_queue_pop(dma_q);
+ const int32_t raw_n_rep = pctx->n_rep_buf[iv3];
+ const uint32_t n_steps = raw_n_rep <= 0 ? 0 : hex_smin((uint32_t) raw_n_rep, pctx->n_pend);
+ if (n_steps > 0u) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, 0);
+ gdn_replay_f32(octx, dma_q, head_b->s_state,
+ (float *) head_b->k_buf[0], (float *) head_b->v_buf[0], head_b->g_f32, head_b->b_f32,
+ head_b->vtcm_m, pctx->replay_cap,
+ head_b->ik1, head_b->iv1, head_b->iv3, n_steps, pctx->n_pend,
+ kparams->kda != 0, S_v);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, 0);
+ }
+ if (keep) {
+ dma_queue_push(dma_q, dma_make_data(head_b->state_out_dma, head_b->s_state),
+ S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v);
+ dma_queue_pop(dma_q);
+ }
+ }
+ if (active_a && sub == 0u) {
+ gdn_dma_push_chunk_inputs(dma_q, head_a->q_buf[0], head_a->k_buf[0], head_a->v_buf[0],
+ q, k, v, head_a->iq3, head_a->iq1, head_a->ik3, head_a->ik1,
+ head_a->iv3, head_a->iv1, 0u, chunk0_tokens, S_v);
+ if (i == 0u) {
+ gdn_dma_push_chunk_gb(dma_q, pctx->vtcm_g_raw[0][0], pctx->vtcm_b_raw[0][0],
+ g, beta, iv3, head_in_seq, 0u, chunk0_tokens, cur_a);
+ }
+ }
+ if (active_b && sub == 0u) {
+ gdn_dma_push_chunk_inputs(dma_q, head_b->q_buf[0], head_b->k_buf[0], head_b->v_buf[0],
+ q, k, v, head_b->iq3, head_b->iq1, head_b->ik3, head_b->ik1,
+ head_b->iv3, head_b->iv1, 0u, chunk0_tokens, S_v);
+ if (i == 0u) {
+ gdn_dma_push_chunk_gb(dma_q, pctx->vtcm_g_raw[1][0], pctx->vtcm_b_raw[1][0],
+ g, beta, iv3, head_in_seq + cur_a, 0u, chunk0_tokens, cur_b);
+ }
+ }
+ } else {
+ if (active_a && sub == 0u) {
+ gdn_init_head_ptrs(head_a, pctx->L, pctx->vtcm_base, h, 0u, head_in_seq + h, iv3, is3,
+ q, k, state, dst, dst_cache, kparams, S_v, H, n_tokens);
+ dma_queue_push(dma_q, dma_make_data(head_a->s_state, head_a->state_in_dma),
+ S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v);
+ gdn_dma_push_chunk_inputs(dma_q, head_a->q_buf[0], head_a->k_buf[0], head_a->v_buf[0],
+ q, k, v, head_a->iq3, head_a->iq1, head_a->ik3, head_a->ik1,
+ head_a->iv3, head_a->iv1, 0u, chunk0_tokens, S_v);
+ if (i == 0u) {
+ gdn_dma_push_chunk_gb(dma_q, pctx->vtcm_g_raw[0][0], pctx->vtcm_b_raw[0][0],
+ g, beta, iv3, head_in_seq, 0u, chunk0_tokens, cur_a);
+ }
+ }
+ if (active_b && sub == 0u) {
+ gdn_init_head_ptrs(head_b, pctx->L, pctx->vtcm_base, max_group + h, 1u, head_in_seq + cur_a + h, iv3, is3,
+ q, k, state, dst, dst_cache, kparams, S_v, H, n_tokens);
+ dma_queue_push(dma_q, dma_make_data(head_b->s_state, head_b->state_in_dma),
+ S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v);
+ gdn_dma_push_chunk_inputs(dma_q, head_b->q_buf[0], head_b->k_buf[0], head_b->v_buf[0],
+ q, k, v, head_b->iq3, head_b->iq1, head_b->ik3, head_b->ik1,
+ head_b->iv3, head_b->iv1, 0u, chunk0_tokens, S_v);
+ if (i == 0u) {
+ gdn_dma_push_chunk_gb(dma_q, pctx->vtcm_g_raw[1][0], pctx->vtcm_b_raw[1][0],
+ g, beta, iv3, head_in_seq + cur_a, 0u, chunk0_tokens, cur_b);
+ }
+ }
+ }
+
+ if (active_a && sub == 0u) {
+ if (!replay) {
+ dma_queue_pop(dma_q); // state A
+ }
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, 0);
+ gdn_pack_s_col_tiles(head_a->s_col_tiles, head_a->s_state, S_v);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, 0);
+ dma_queue_pop(dma_q); // Q A0
+ dma_queue_pop(dma_q); // K A0
+ dma_queue_pop(dma_q); // V A0
+ if (i == 0u) {
+ dma_queue_pop(dma_q); // g A0
+ dma_queue_pop(dma_q); // b A0
+ }
+ if (n_chunks > 1u) {
+ gdn_dma_push_chunk_inputs(dma_q, head_a->q_buf[1], head_a->k_buf[1], head_a->v_buf[1],
+ q, k, v, head_a->iq3, head_a->iq1, head_a->ik3, head_a->ik1,
+ head_a->iv3, head_a->iv1, chunk_size, chunk1_tokens, S_v);
+ if (i == 0u) {
+ gdn_dma_push_chunk_gb(dma_q, pctx->vtcm_g_raw[0][1], pctx->vtcm_b_raw[0][1],
+ g, beta, iv3, head_in_seq, chunk_size, chunk1_tokens, cur_a);
+ }
+ }
+ }
+ gdn_barrier_all(barrier, &seq, i, n_workers);
+
+ if (active_a && sub == 0u) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, 0);
+ gdn_hvx_phase1a(head_a, pctx->vtcm_g_raw[0][0], pctx->vtcm_b_raw[0][0],
+ 0u, 0u, cur_a, n_tokens, S_v, h, q_is_f16, k_is_f16, v_is_f16);
+ gdn_hvx_phase1b(head_a, 0u, S_v, q_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, 0);
+ }
+ if (i == 0u) {
+ gdn_barrier_wait_workers(barrier, n_workers);
+ htp_gdn_push_hmx_batch(hmx_q, &tasks[0], heads_a, cur_a, n_sv_t, GDN_HMX_OP_H1_FULL, 0u, scales_1);
+ gdn_barrier_release_workers(barrier, &seq);
+ } else {
+ gdn_barrier_worker_sync(barrier, &seq);
+ }
+
+ if (cur_b > 0u) {
+ if (active_b && sub == 0u) {
+ if (!replay) {
+ dma_queue_pop(dma_q); // state B
+ }
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, 0);
+ gdn_pack_s_col_tiles(head_b->s_col_tiles, head_b->s_state, S_v);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, 0);
+ dma_queue_pop(dma_q); // Q B0
+ dma_queue_pop(dma_q); // K B0
+ dma_queue_pop(dma_q); // V B0
+ if (i == 0u) {
+ dma_queue_pop(dma_q); // g B0
+ dma_queue_pop(dma_q); // b B0
+ }
+ if (n_chunks > 1u) {
+ gdn_dma_push_chunk_inputs(dma_q, head_b->q_buf[1], head_b->k_buf[1], head_b->v_buf[1],
+ q, k, v, head_b->iq3, head_b->iq1, head_b->ik3, head_b->ik1,
+ head_b->iv3, head_b->iv1, chunk_size, chunk1_tokens, S_v);
+ if (i == 0u) {
+ gdn_dma_push_chunk_gb(dma_q, pctx->vtcm_g_raw[1][1], pctx->vtcm_b_raw[1][1],
+ g, beta, iv3, head_in_seq + cur_a, chunk_size, chunk1_tokens, cur_b);
+ }
+ }
+ }
+ gdn_barrier_all(barrier, &seq, i, n_workers);
+
+ if (active_b && sub == 0u) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, 0);
+ gdn_hvx_phase1a(head_b, pctx->vtcm_g_raw[1][0], pctx->vtcm_b_raw[1][0],
+ 0u, 0u, cur_b, n_tokens, S_v, h, q_is_f16, k_is_f16, v_is_f16);
+ gdn_hvx_phase1b(head_b, 0u, S_v, q_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, 0);
+ }
+ }
+ if (i == 0u) {
+ gdn_barrier_wait_workers(barrier, n_workers);
+ hmx_queue_pop(hmx_q); // H1_FULL(A, 0)
+ if (cur_b > 0u) {
+ htp_gdn_push_hmx_batch(hmx_q, &tasks[1], heads_b, cur_b, n_sv_t, GDN_HMX_OP_H1_FULL, 0u, scales_1);
+ }
+ gdn_barrier_release_workers(barrier, &seq);
+ } else {
+ gdn_barrier_worker_sync(barrier, &seq);
+ }
+
+ // 2-group ping-pong chunk loop: 6 barriers across both groups (3 per group)
+ for (uint32_t c = 0u; c < n_chunks; ++c) {
+ const uint32_t curr_buf = c & 1u;
+ const uint32_t next_buf = (c + 1u) & 1u;
+ const uint32_t t_chunk = c * chunk_size;
+ const uint32_t valid_tokens = hex_smin(chunk_size, n_tokens - t_chunk);
+ const bool has_next = (c + 1u < n_chunks);
+ const bool has_c2 = (c + 2u < n_chunks);
+ const uint32_t t_c2 = (c + 2u) * chunk_size;
+ const uint32_t c2_tokens = has_c2 ? hex_smin(chunk_size, n_tokens - t_c2) : 0u;
+ const uint16_t info = (uint16_t) c;
+
+ // Step 1: HVX phase2+3(A, c) + DMA(A) || HMX H1(B, c)
+ if (active_a) {
+ if (!dual_hvx) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_SOLVE, info);
+ gdn_hvx_phase2(head_a, l_l2_buf);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_SOLVE, info);
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_V_PREP, info);
+ gdn_hvx_phase3(head_a, curr_buf, S_v, v_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_V_PREP, info);
+ if (has_next) {
+ dma_queue_pop(dma_q);
+ dma_queue_pop(dma_q);
+ dma_queue_pop(dma_q);
+ if (i == 0u) {
+ dma_queue_pop(dma_q);
+ dma_queue_pop(dma_q);
+ }
+ }
+ if (has_c2) {
+ gdn_dma_push_chunk_inputs(dma_q, head_a->q_buf[curr_buf], head_a->k_buf[curr_buf], head_a->v_buf[curr_buf],
+ q, k, v, head_a->iq3, head_a->iq1, head_a->ik3, head_a->ik1,
+ head_a->iv3, head_a->iv1, t_c2, c2_tokens, S_v);
+ if (i == 0u) {
+ gdn_dma_push_chunk_gb(dma_q, pctx->vtcm_g_raw[0][curr_buf], pctx->vtcm_b_raw[0][curr_buf],
+ g, beta, iv3, head_in_seq, t_c2, c2_tokens, cur_a);
+ }
+ }
+ } else if (sub == 0u) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_SOLVE, info);
+ gdn_hvx_phase2(head_a, l_l2_buf);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_SOLVE, info);
+ if (has_next) {
+ dma_queue_pop(dma_q);
+ dma_queue_pop(dma_q);
+ dma_queue_pop(dma_q);
+ if (i == 0u) {
+ dma_queue_pop(dma_q);
+ dma_queue_pop(dma_q);
+ }
+ }
+ } else {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_V_PREP, info);
+ gdn_hvx_phase3(head_a, curr_buf, S_v, v_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_V_PREP, info);
+ }
+ }
+ if (i == 0u) {
+ gdn_barrier_wait_workers(barrier, n_workers);
+ if (cur_b > 0u) {
+ hmx_queue_pop(hmx_q); // H1(B, c)
+ }
+ htp_gdn_push_hmx_batch(hmx_q, &tasks[0], heads_a, cur_a, n_sv_t, GDN_HMX_OP_H2, curr_buf, scales_1);
+ gdn_barrier_release_workers(barrier, &seq);
+ } else {
+ gdn_barrier_worker_sync(barrier, &seq);
+ }
-static void gdn_hvx_epilogue_worker(const htp_thread_t * th, unsigned int n, unsigned int i, void * data) {
- (void) n;
- struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data;
- struct htp_gdn_head_ptrs * head = &bctx->heads[i];
- dma_queue * dma_q = th->dma_q;
- const uint32_t S_v = bctx->S_v;
+ // Step 2: HVX phase2+3(B, c) + DMA(B) + pop O(c-1) || HMX H2(A, c)
+ if (dual_hvx && active_a && sub == 0u && has_c2) {
+ // phase3(A, curr_buf) finished before Step 1 barrier; safe to reuse curr_buf for A(c+2)
+ gdn_dma_push_chunk_inputs(dma_q, head_a->q_buf[curr_buf], head_a->k_buf[curr_buf], head_a->v_buf[curr_buf],
+ q, k, v, head_a->iq3, head_a->iq1, head_a->ik3, head_a->ik1,
+ head_a->iv3, head_a->iv1, t_c2, c2_tokens, S_v);
+ if (i == 0u) {
+ gdn_dma_push_chunk_gb(dma_q, pctx->vtcm_g_raw[0][curr_buf], pctx->vtcm_b_raw[0][curr_buf],
+ g, beta, iv3, head_in_seq, t_c2, c2_tokens, cur_a);
+ }
+ }
+ if (active_b) {
+ if (!dual_hvx) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_SOLVE, info);
+ gdn_hvx_phase2(head_b, l_l2_buf);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_SOLVE, info);
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_V_PREP, info);
+ gdn_hvx_phase3(head_b, curr_buf, S_v, v_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_V_PREP, info);
+ if (has_next) {
+ dma_queue_pop(dma_q);
+ dma_queue_pop(dma_q);
+ dma_queue_pop(dma_q);
+ if (i == 0u) {
+ dma_queue_pop(dma_q);
+ dma_queue_pop(dma_q);
+ }
+ }
+ } else if (sub == 0u) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_SOLVE, info);
+ gdn_hvx_phase2(head_b, l_l2_buf);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_SOLVE, info);
+ if (has_next) {
+ dma_queue_pop(dma_q);
+ dma_queue_pop(dma_q);
+ dma_queue_pop(dma_q);
+ if (i == 0u) {
+ dma_queue_pop(dma_q);
+ dma_queue_pop(dma_q);
+ }
+ }
+ } else {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_V_PREP, info);
+ gdn_hvx_phase3(head_b, curr_buf, S_v, v_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_V_PREP, info);
+ }
+ }
+ if (c > 0u && sub == 0u) {
+ if (active_a) {
+ dma_queue_pop(dma_q); // O(A, c-1)
+ }
+ if (active_b) {
+ dma_queue_pop(dma_q); // O(B, c-1)
+ }
+ }
+ if (!dual_hvx && active_b && has_c2) {
+ gdn_dma_push_chunk_inputs(dma_q, head_b->q_buf[curr_buf], head_b->k_buf[curr_buf], head_b->v_buf[curr_buf],
+ q, k, v, head_b->iq3, head_b->iq1, head_b->ik3, head_b->ik1,
+ head_b->iv3, head_b->iv1, t_c2, c2_tokens, S_v);
+ if (i == 0u) {
+ gdn_dma_push_chunk_gb(dma_q, pctx->vtcm_g_raw[1][curr_buf], pctx->vtcm_b_raw[1][curr_buf],
+ g, beta, iv3, head_in_seq + cur_a, t_c2, c2_tokens, cur_b);
+ }
+ }
+ if (i == 0u) {
+ gdn_barrier_wait_workers(barrier, n_workers);
+ hmx_queue_pop(hmx_q); // H2(A, c)
+ if (cur_b > 0u) {
+ htp_gdn_push_hmx_batch(hmx_q, &tasks[1], heads_b, cur_b, n_sv_t, GDN_HMX_OP_H2, curr_buf, scales_1);
+ }
+ gdn_barrier_release_workers(barrier, &seq);
+ } else {
+ gdn_barrier_worker_sync(barrier, &seq);
+ }
- dma_queue_pop(dma_q);
- if (!bctx->keep) {
- dma_queue_push(dma_q, dma_make_data(head->state_out_dma, head->s_state),
- S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v);
+ // Step 3: HVX phase4+5(A, c) + DMA O(A, c) + phase1a(A, c+1) || HMX H2(B, c)
+ if (dual_hvx && active_b && sub == 0u && has_c2) {
+ // phase3(B, curr_buf) finished before Step 2 barrier; safe to reuse curr_buf for B(c+2)
+ gdn_dma_push_chunk_inputs(dma_q, head_b->q_buf[curr_buf], head_b->k_buf[curr_buf], head_b->v_buf[curr_buf],
+ q, k, v, head_b->iq3, head_b->iq1, head_b->ik3, head_b->ik1,
+ head_b->iv3, head_b->iv1, t_c2, c2_tokens, S_v);
+ if (i == 0u) {
+ gdn_dma_push_chunk_gb(dma_q, pctx->vtcm_g_raw[1][curr_buf], pctx->vtcm_b_raw[1][curr_buf],
+ g, beta, iv3, head_in_seq + cur_a, t_c2, c2_tokens, cur_b);
+ }
+ }
+ if (active_a) {
+ if (!dual_hvx) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
+ gdn_hvx_phase4_range(head_a, 0u, n_sv_t);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_OUT, info);
+ gdn_hvx_phase5(head_a, curr_buf, S_v, scale, dst_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_OUT, info);
+
+ const dma_addr_t attn_dma = dst->data +
+ ((size_t) head_a->iv3 * n_tokens * H + (size_t) t_chunk * H + head_a->iv1) * row_out_bytes;
+ dma_queue_push(dma_q, dma_make_data(attn_dma, head_a->o_buf[curr_buf]),
+ dst->nb[1], row_out_bytes, row_out_bytes, valid_tokens);
+
+ if (has_next) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ gdn_hvx_phase1a(head_a, pctx->vtcm_g_raw[0][next_buf], pctx->vtcm_b_raw[0][next_buf],
+ next_buf, c + 1u, cur_a, n_tokens, S_v, h, q_is_f16, k_is_f16, v_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ }
+ } else if (sub == 0u) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_OUT, info);
+ gdn_hvx_phase5(head_a, curr_buf, S_v, scale, dst_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_OUT, info);
+
+ const dma_addr_t attn_dma = dst->data +
+ ((size_t) head_a->iv3 * n_tokens * H + (size_t) t_chunk * H + head_a->iv1) * row_out_bytes;
+ dma_queue_push(dma_q, dma_make_data(attn_dma, head_a->o_buf[curr_buf]),
+ dst->nb[1], row_out_bytes, row_out_bytes, valid_tokens);
+
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
+ gdn_hvx_phase4_range(head_a, 0u, half_sv_t);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
+ } else {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
+ gdn_hvx_phase4_range(head_a, half_sv_t, n_sv_t);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
+ if (has_next) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ gdn_hvx_phase1a(head_a, pctx->vtcm_g_raw[0][next_buf], pctx->vtcm_b_raw[0][next_buf],
+ next_buf, c + 1u, cur_a, n_tokens, S_v, h, q_is_f16, k_is_f16, v_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ }
+ }
+ }
+ if (i == 0u) {
+ gdn_barrier_wait_workers(barrier, n_workers);
+ if (cur_b > 0u) {
+ hmx_queue_pop(hmx_q); // H2(B, c)
+ }
+ htp_gdn_push_hmx_batch(hmx_q, &tasks[0], heads_a, cur_a, n_sv_t,
+ has_next ? GDN_HMX_OP_H3_NEXT : GDN_HMX_OP_H3, curr_buf, scales_1);
+ gdn_barrier_release_workers(barrier, &seq);
+ } else {
+ gdn_barrier_worker_sync(barrier, &seq);
+ }
+
+ // Step 4: HVX phase4+5(B, c) + DMA O(B, c) + phase1a(B, c+1) || HMX H3(A, c) [+ KK(A, c+1)]
+ if (active_b) {
+ if (!dual_hvx) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
+ gdn_hvx_phase4_range(head_b, 0u, n_sv_t);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_OUT, info);
+ gdn_hvx_phase5(head_b, curr_buf, S_v, scale, dst_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_OUT, info);
+
+ const dma_addr_t attn_dma = dst->data +
+ ((size_t) head_b->iv3 * n_tokens * H + (size_t) t_chunk * H + head_b->iv1) * row_out_bytes;
+ dma_queue_push(dma_q, dma_make_data(attn_dma, head_b->o_buf[curr_buf]),
+ dst->nb[1], row_out_bytes, row_out_bytes, valid_tokens);
+
+ if (has_next) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ gdn_hvx_phase1a(head_b, pctx->vtcm_g_raw[1][next_buf], pctx->vtcm_b_raw[1][next_buf],
+ next_buf, c + 1u, cur_b, n_tokens, S_v, h, q_is_f16, k_is_f16, v_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ }
+ } else if (sub == 0u) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_OUT, info);
+ gdn_hvx_phase5(head_b, curr_buf, S_v, scale, dst_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_OUT, info);
+
+ const dma_addr_t attn_dma = dst->data +
+ ((size_t) head_b->iv3 * n_tokens * H + (size_t) t_chunk * H + head_b->iv1) * row_out_bytes;
+ dma_queue_push(dma_q, dma_make_data(attn_dma, head_b->o_buf[curr_buf]),
+ dst->nb[1], row_out_bytes, row_out_bytes, valid_tokens);
+
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
+ gdn_hvx_phase4_range(head_b, 0u, half_sv_t);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
+ } else {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
+ gdn_hvx_phase4_range(head_b, half_sv_t, n_sv_t);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info);
+ if (has_next) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ gdn_hvx_phase1a(head_b, pctx->vtcm_g_raw[1][next_buf], pctx->vtcm_b_raw[1][next_buf],
+ next_buf, c + 1u, cur_b, n_tokens, S_v, h, q_is_f16, k_is_f16, v_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ }
+ }
+ }
+ if (i == 0u) {
+ gdn_barrier_wait_workers(barrier, n_workers);
+ hmx_queue_pop(hmx_q); // H3(A, c)
+ if (cur_b > 0u) {
+ htp_gdn_push_hmx_batch(hmx_q, &tasks[1], heads_b, cur_b, n_sv_t,
+ has_next ? GDN_HMX_OP_H3_NEXT : GDN_HMX_OP_H3, curr_buf, scales_1);
+ }
+ gdn_barrier_release_workers(barrier, &seq);
+ } else {
+ gdn_barrier_worker_sync(barrier, &seq);
+ }
+
+ // Step 5: HVX phase6(A, c) + phase1b(A, c+1) [or state DMA store] || HMX H3(B, c) [+ KK(B, c+1)]
+ if (active_a) {
+ if (!dual_hvx) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_STATE, info);
+ gdn_hvx_phase6(head_a, curr_buf, c, n_chunks, S_v);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_STATE, info);
+ if (has_next) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ gdn_hvx_phase1b(head_a, next_buf, S_v, q_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ } else if (!keep) {
+ dma_queue_push(dma_q, dma_make_data(head_a->state_out_dma, head_a->s_state),
+ S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v);
+ }
+ } else if (sub == 0u) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_STATE, info);
+ gdn_hvx_phase6(head_a, curr_buf, c, n_chunks, S_v);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_STATE, info);
+ if (!has_next && !keep) {
+ dma_queue_push(dma_q, dma_make_data(head_a->state_out_dma, head_a->s_state),
+ S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v);
+ }
+ } else if (has_next) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ gdn_hvx_phase1b(head_a, next_buf, S_v, q_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ }
+ }
+ if (i == 0u) {
+ gdn_barrier_wait_workers(barrier, n_workers);
+ if (cur_b > 0u) {
+ hmx_queue_pop(hmx_q); // H3(B, c)
+ }
+ if (has_next) {
+ htp_gdn_push_hmx_batch(hmx_q, &tasks[0], heads_a, cur_a, n_sv_t, GDN_HMX_OP_H1, next_buf, scales_1);
+ }
+ gdn_barrier_release_workers(barrier, &seq);
+ } else {
+ gdn_barrier_worker_sync(barrier, &seq);
+ }
+
+ // Step 6: HVX phase6(B, c) + phase1b(B, c+1) [or state DMA store + flush] || HMX H1(A, c+1)
+ if (active_b) {
+ if (!dual_hvx) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_STATE, info);
+ gdn_hvx_phase6(head_b, curr_buf, c, n_chunks, S_v);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_STATE, info);
+ if (has_next) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ gdn_hvx_phase1b(head_b, next_buf, S_v, q_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ } else if (!keep) {
+ dma_queue_push(dma_q, dma_make_data(head_b->state_out_dma, head_b->s_state),
+ S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v);
+ }
+ } else if (sub == 0u) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_STATE, info);
+ gdn_hvx_phase6(head_b, curr_buf, c, n_chunks, S_v);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_STATE, info);
+ if (!has_next && !keep) {
+ dma_queue_push(dma_q, dma_make_data(head_b->state_out_dma, head_b->s_state),
+ S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v);
+ }
+ } else if (has_next) {
+ htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ gdn_hvx_phase1b(head_b, next_buf, S_v, q_is_f16);
+ htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, (uint16_t) (c + 1u));
+ }
+ }
+ if (!has_next && sub == 0u && (active_a || active_b)) {
+ dma_queue_flush(dma_q);
+ }
+ if (i == 0u) {
+ gdn_barrier_wait_workers(barrier, n_workers);
+ if (has_next) {
+ hmx_queue_pop(hmx_q); // H1(A, c+1)
+ if (cur_b > 0u) {
+ htp_gdn_push_hmx_batch(hmx_q, &tasks[1], heads_b, cur_b, n_sv_t, GDN_HMX_OP_H1, next_buf, scales_1);
+ }
+ }
+ gdn_barrier_release_workers(barrier, &seq);
+ } else {
+ gdn_barrier_worker_sync(barrier, &seq);
+ }
+ }
}
- dma_queue_flush(dma_q);
}
static int gated_delta_net_f32_hmx_chunked(
@@ -3207,35 +3833,48 @@ static int gated_delta_net_f32_hmx_chunked(
return HTP_STATUS_OK;
}
- const struct htp_tensor * q = octx->src[0];
- const struct htp_tensor * k = octx->src[1];
- const struct htp_tensor * v = octx->src[2];
- const struct htp_tensor * state = octx->src[5];
- const bool replay = gdn_has_replay(octx);
- const struct htp_tensor * ids = replay ? NULL : octx->src[6];
- const struct htp_tensor * n_rep = replay ? octx->src[9] : NULL;
- const struct htp_tensor * dst = octx->dst;
- const struct htp_tensor * dst_cache = octx->dsts[1];
+ const struct htp_tensor * q = octx->src[0];
+ const struct htp_tensor * k = octx->src[1];
+ const struct htp_tensor * v = octx->src[2];
+ const bool replay = gdn_has_replay(octx);
+ const struct htp_tensor * ids = replay ? NULL : octx->src[6];
+ const struct htp_tensor * n_rep = replay ? octx->src[9] : NULL;
+ const struct htp_tensor * dst = octx->dst;
- const uint32_t S_v = kparams->S_v;
- const uint32_t H = kparams->H;
- const uint32_t n_tokens = kparams->n_tokens;
- const float scale = kparams->scale;
- const uint32_t chunk_size = kparams->chunk_size;
- const uint32_t n_chunks = kparams->n_chunks;
- const uint32_t n_pend = replay ? (uint32_t) octx->op_params[1] : 0;
- const bool keep = replay && octx->op_params[2] != 0;
+ const uint32_t S_v = kparams->S_v;
+ const uint32_t chunk_size = kparams->chunk_size;
+ const uint32_t n_heads_group = kparams->n_heads_group;
+ const uint32_t n_groups = kparams->n_groups;
+ const bool pad_hmx_banks = (kparams->hmx_flags & HTP_GDN_HMX_FLAG_PAD_BANKS) != 0u;
+ const bool sep_aliases = (kparams->hmx_flags & HTP_GDN_HMX_FLAG_SEPARATE_ALIAS) != 0u;
+ const uint32_t n_pend = replay ? (uint32_t) octx->op_params[1] : 0;
+ const bool keep = replay && octx->op_params[2] != 0;
+
+ const bool q_is_f16 = (q->type == HTP_TYPE_F16);
+ const bool k_is_f16 = (k->type == HTP_TYPE_F16);
+ const bool v_is_f16 = (v->type == HTP_TYPE_F16);
+ const bool dst_is_f16 = (dst->type == HTP_TYPE_F16);
+ const size_t q_elem_sz = q_is_f16 ? sizeof(__fp16) : sizeof(float);
+ const size_t k_elem_sz = k_is_f16 ? sizeof(__fp16) : sizeof(float);
+ const size_t v_elem_sz = v_is_f16 ? sizeof(__fp16) : sizeof(float);
+ const size_t o_elem_sz = dst_is_f16 ? sizeof(__fp16) : sizeof(float);
struct htp_gdn_hmx_vtcm_layout L;
- htp_gdn_hmx_vtcm_layout_build(&L, S_v, chunk_size, kparams->n_heads_batch, kparams->n_hvx);
-
+ htp_gdn_hmx_vtcm_layout_build(&L, S_v, chunk_size, n_heads_group, n_groups,
+ q_elem_sz, k_elem_sz, v_elem_sz, o_elem_sz,
+ pad_hmx_banks, sep_aliases);
if (L.total_bytes > octx->ctx->vtcm_size) {
return HTP_STATUS_VTCM_TOO_SMALL;
}
+ const uint32_t n_workers = (octx->n_hvx >= 2u * n_heads_group && L.separate_aliases)
+ ? (2u * n_heads_group)
+ : n_heads_group;
+
uint8_t * const vtcm_base = (uint8_t *) octx->ctx->vtcm_base;
- void * aux_buf = NULL;
- const int aux_err = gdn_prepare_aux_data(octx, replay ? n_rep : ids, &aux_buf);
+ void * aux_buf = NULL;
+ const struct htp_tensor * aux_tensor = replay ? n_rep : ids;
+ const int aux_err = gdn_prepare_aux_data(octx, aux_tensor, &aux_buf);
if (aux_err != HTP_STATUS_OK) {
return aux_err;
}
@@ -3243,127 +3882,85 @@ static int gated_delta_net_f32_hmx_chunked(
const int32_t * n_rep_buf = replay ? (const int32_t *) aux_buf : NULL;
const uint32_t ids_type = ids ? ids->type : 0;
- uint8_t * vtcm_scales_1 = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, L.off_scales_1);
- hmx_init_column_scales(vtcm_scales_1, Q6_V_vsplat_R(0x3c00));
-
- hmx_queue_t hmx_q = octx->ctx->hmx_queue;
- work_queue_t wp = octx->ctx->hvx_queue;
-
- struct htp_gdn_head_ptrs heads[8];
-
- struct htp_gdn_batch_context bctx;
- bctx.heads = heads;
- bctx.vtcm_g_raw[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L.off_g_raw[0]);
- bctx.vtcm_g_raw[1] = VTCM_LAYOUT_PTR(float, vtcm_base, L.off_g_raw[1]);
- bctx.vtcm_b_raw[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L.off_b_raw[0]);
- bctx.vtcm_b_raw[1] = VTCM_LAYOUT_PTR(float, vtcm_base, L.off_b_raw[1]);
- bctx.hmx_scales = vtcm_scales_1;
- bctx.n_rep_data = n_rep_buf;
- bctx.n_pend = n_pend;
- bctx.replay = replay;
- bctx.keep = keep;
- bctx.S_v = S_v;
- bctx.scale = scale;
- bctx.octx = octx;
- bctx.kparams = kparams;
-
- uint32_t n_batch = 1;
- for (uint32_t r = row_start; r < row_start + nrows; r += n_batch) {
- const uint32_t head_in_seq = fastmodulo(r, H, &kparams->div_H);
- const uint32_t iv3 = fastdiv(r, &kparams->div_H);
- const uint32_t is3 = gdn_read_seq_idx(ids_buf, ids_type, iv3);
- const uint32_t heads_left_in_seq = H - head_in_seq;
- const uint32_t heads_left_in_range = (row_start + nrows) - r;
- n_batch = hex_smin((uint32_t) kparams->n_heads_batch, hex_smin(heads_left_in_seq, heads_left_in_range));
-
- for (uint32_t h = 0; h < n_batch; ++h) {
- gdn_init_head_ptrs(&heads[h], &L, vtcm_base, h, head_in_seq, iv3, is3,
- q, k, v, state, dst, dst_cache, kparams, S_v, H, n_tokens, chunk_size);
+ const size_t aux_spad_off = (aux_buf && aux_buf == octx->ctx->ddr_spad_base)
+ ? hex_round_up(htp_tensor_nbytes(aux_tensor), 128)
+ : 0;
+ const size_t l_l2_bytes = hex_round_up((size_t) n_workers * 2176u * sizeof(uint16_t), 128);
+ const size_t barrier_bytes = hex_round_up(sizeof(gdn_barrier_t), 128);
+ const size_t heads_bytes = hex_round_up((size_t) n_heads_group * sizeof(struct htp_gdn_head_ptrs), 128);
+ const size_t spad_bytes = l_l2_bytes + barrier_bytes + heads_bytes * (n_groups >= 2u ? 2u : 1u);
+ uint8_t * spad_mem = NULL;
+ bool spad_heap = false;
+ if (aux_spad_off + spad_bytes <= octx->ctx->ddr_spad_size) {
+ spad_mem = (uint8_t *) octx->ctx->ddr_spad_base + aux_spad_off;
+ } else {
+ spad_mem = (uint8_t *) memalign(128, spad_bytes);
+ if (!spad_mem) {
+ if (aux_buf && aux_buf != octx->ctx->ddr_spad_base) {
+ free(aux_buf);
+ }
+ return HTP_STATUS_INTERNAL_ERR;
}
+ spad_heap = true;
+ }
- bctx.head_in_seq = head_in_seq;
- bctx.iv3 = iv3;
- bctx.n_batch = n_batch;
-
- work_queue_run(wp, gdn_hvx_prologue_worker, &bctx, n_batch);
-
- // Prologue: prepare chunk 0 (phase1a, KK, phase1b, QK + KS in-flight)
- bctx.c = 0u;
- bctx.curr_buf = 0u;
- work_queue_run(wp, gdn_hvx_phase1a_worker, &bctx, n_batch);
- hmx_queue_push(hmx_q, hmx_queue_make_desc(gdn_hmx_kk_worker, &bctx));
-
- work_queue_run(wp, gdn_hvx_phase1b_worker, &bctx, n_batch);
- hmx_queue_pop(hmx_q);
- hmx_queue_push(hmx_q, hmx_queue_make_desc(gdn_hmx_qk_worker, &bctx));
- hmx_queue_push(hmx_q, hmx_queue_make_desc(gdn_hmx_ks_worker, &bctx));
-
- // Pipelined chunk loop: overlap chunk c tail with chunk c+1 prep
- for (uint32_t c = 0u; c < n_chunks; ++c) {
- const uint32_t curr_buf = c & 1u;
- const uint32_t next_buf = (c + 1u) & 1u;
- const bool has_next = (c + 1u < n_chunks);
-
- // Step 1: HMX QK(c) + KS(c) || HVX phase2(c)
- bctx.c = c;
- bctx.curr_buf = curr_buf;
- work_queue_run(wp, gdn_hvx_phase2_worker, &bctx, n_batch);
- hmx_queue_push(hmx_q, hmx_queue_make_desc(gdn_hmx_inv10_worker, &bctx));
- hmx_queue_push(hmx_q, hmx_queue_make_desc(gdn_hmx_qs_worker, &bctx));
- hmx_queue_pop(hmx_q); // QK(c)
- hmx_queue_pop(hmx_q); // KS(c)
-
- // Step 2: HMX INV_10(c) + QS(c) || HVX phase3(c) + DMA pop(c+1)/push(c+2)
- work_queue_run(wp, gdn_hvx_phase3_worker, &bctx, n_batch);
- hmx_queue_pop(hmx_q); // INV_10(c)
- hmx_queue_push(hmx_q, hmx_queue_make_desc(gdn_hmx_delta_worker, &bctx));
-
- // Step 3: HMX QS(c) tail + DELTA(c) || HVX phase1a(c+1)
- if (has_next) {
- bctx.c = c + 1u;
- bctx.curr_buf = next_buf;
- work_queue_run(wp, gdn_hvx_phase1a_worker, &bctx, n_batch);
- }
- hmx_queue_pop(hmx_q); // QS(c)
- hmx_queue_pop(hmx_q); // DELTA(c)
- if (has_next) {
- hmx_queue_push(hmx_q, hmx_queue_make_desc(gdn_hmx_kk_worker, &bctx));
- }
+ uint16_t * l_l2_spad = (uint16_t *) spad_mem;
+ gdn_barrier_t * barrier = (gdn_barrier_t *) (spad_mem + l_l2_bytes);
+ struct htp_gdn_head_ptrs * heads_a = (struct htp_gdn_head_ptrs *) (spad_mem + l_l2_bytes + barrier_bytes);
+ struct htp_gdn_head_ptrs * heads_b = (n_groups >= 2u)
+ ? (struct htp_gdn_head_ptrs *) (spad_mem + l_l2_bytes + barrier_bytes + heads_bytes)
+ : NULL;
- // Step 4: HMX KK(c+1) || HVX phase4(c)
- bctx.c = c;
- bctx.curr_buf = curr_buf;
- bctx.s_update_buf = curr_buf;
- work_queue_run(wp, gdn_hvx_phase4_worker, &bctx, n_batch);
- hmx_queue_push(hmx_q, hmx_queue_make_desc(gdn_hmx_o_intra_worker, &bctx));
- hmx_queue_push(hmx_q, hmx_queue_make_desc(gdn_hmx_s_update_worker, &bctx));
-
- // Step 5: HMX O_INTRA(c) + S_UPDATE(c) || HVX phase1b(c+1)
- if (has_next) {
- bctx.c = c + 1u;
- bctx.curr_buf = next_buf;
- work_queue_run(wp, gdn_hvx_phase1b_worker, &bctx, n_batch);
- hmx_queue_push(hmx_q, hmx_queue_make_desc(gdn_hmx_qk_worker, &bctx));
- hmx_queue_pop(hmx_q); // KK(c+1)
- }
- hmx_queue_pop(hmx_q); // O_INTRA(c)
-
- // Step 6: HMX S_UPDATE(c) tail + QK(c+1) || HVX phase5(c) + DMA store O(c)
- bctx.c = c;
- bctx.curr_buf = curr_buf;
- work_queue_run(wp, gdn_hvx_phase5_worker, &bctx, n_batch);
- hmx_queue_pop(hmx_q); // S_UPDATE(c)
-
- // Step 7: HMX QK(c+1) tail || HVX phase6(c)
- work_queue_run(wp, gdn_hvx_phase6_worker, &bctx, n_batch);
- if (has_next) {
- hmx_queue_push(hmx_q, hmx_queue_make_desc(gdn_hmx_ks_worker, &bctx));
- }
- }
+ uint8_t * vtcm_scales_1 = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, L.off_scales_1);
+ hmx_init_column_scales(vtcm_scales_1, Q6_V_vsplat_R(0x3c00));
- work_queue_run(wp, gdn_hvx_epilogue_worker, &bctx, n_batch);
+ gdn_barrier_init(barrier);
+
+ struct htp_gdn_hmx_batch_task hmx_tasks[2];
+
+ struct htp_gdn_persistent_ctx pctx;
+ pctx.L = &L;
+ pctx.vtcm_base = vtcm_base;
+ pctx.ddr_spad = l_l2_spad;
+ pctx.vtcm_g_raw[0][0] = VTCM_LAYOUT_PTR(float, vtcm_base, L.off_g_raw[0][0]);
+ pctx.vtcm_g_raw[0][1] = VTCM_LAYOUT_PTR(float, vtcm_base, L.off_g_raw[0][1]);
+ pctx.vtcm_b_raw[0][0] = VTCM_LAYOUT_PTR(float, vtcm_base, L.off_b_raw[0][0]);
+ pctx.vtcm_b_raw[0][1] = VTCM_LAYOUT_PTR(float, vtcm_base, L.off_b_raw[0][1]);
+ pctx.vtcm_g_raw[1][0] = VTCM_LAYOUT_PTR_OPTIONAL(float, vtcm_base, L.off_g_raw[1][0], n_groups >= 2u);
+ pctx.vtcm_g_raw[1][1] = VTCM_LAYOUT_PTR_OPTIONAL(float, vtcm_base, L.off_g_raw[1][1], n_groups >= 2u);
+ pctx.vtcm_b_raw[1][0] = VTCM_LAYOUT_PTR_OPTIONAL(float, vtcm_base, L.off_b_raw[1][0], n_groups >= 2u);
+ pctx.vtcm_b_raw[1][1] = VTCM_LAYOUT_PTR_OPTIONAL(float, vtcm_base, L.off_b_raw[1][1], n_groups >= 2u);
+ pctx.vtcm_scales_1 = vtcm_scales_1;
+ pctx.hmx_q = octx->ctx->hmx_queue;
+ pctx.barrier = barrier;
+ pctx.heads_a = heads_a;
+ pctx.heads_b = heads_b;
+ pctx.hmx_tasks = hmx_tasks;
+ pctx.octx = octx;
+ pctx.kparams = kparams;
+ pctx.ids_buf = ids_buf;
+ pctx.n_rep_buf = n_rep_buf;
+ pctx.ids_type = ids_type;
+ pctx.n_pend = n_pend;
+ pctx.replay = replay;
+ pctx.keep = keep;
+ pctx.q_is_f16 = q_is_f16;
+ pctx.k_is_f16 = k_is_f16;
+ pctx.v_is_f16 = v_is_f16;
+ pctx.dst_is_f16 = dst_is_f16;
+ pctx.row_start = row_start;
+ pctx.nrows = nrows;
+ pctx.n_heads_group = n_heads_group;
+ pctx.n_groups = n_groups;
+ pctx.n_sv_tiles = S_v >> 5;
+ pctx.row_out_bytes = S_v * (uint32_t) o_elem_sz;
+ pctx.replay_cap = (k_is_f16 || v_is_f16) ? (chunk_size >> 1) : chunk_size;
+
+ work_queue_run(octx->ctx->hvx_queue, gdn_hvx_persistent_worker, &pctx, n_workers);
+
+ if (spad_heap) {
+ free(spad_mem);
}
-
if (aux_buf && aux_buf != octx->ctx->ddr_spad_base) {
free(aux_buf);
}
diff --git a/ggml/src/ggml-hexagon/htp/gated-delta-net-ops.h b/ggml/src/ggml-hexagon/htp/gated-delta-net-ops.h
index 930881c17..3fa663e5d 100644
--- a/ggml/src/ggml-hexagon/htp/gated-delta-net-ops.h
+++ b/ggml/src/ggml-hexagon/htp/gated-delta-net-ops.h
@@ -23,12 +23,16 @@ enum htp_gdn_kernel_type {
HTP_GDN_KERNEL_HMX_CHUNKED = 1,
};
+#define HTP_GDN_HMX_FLAG_PAD_BANKS (1u << 0)
+#define HTP_GDN_HMX_FLAG_SEPARATE_ALIAS (1u << 1)
+
struct htp_gdn_kernel_params {
uint8_t kernel_type;
- uint8_t reserved0;
+ uint8_t hmx_flags;
uint16_t chunk_size;
uint16_t n_chunks;
- uint16_t n_heads_batch;
+ uint8_t n_heads_group;
+ uint8_t n_groups;
uint32_t n_hvx;
uint32_t S_v;
@@ -96,186 +100,214 @@ struct htp_gdn_hmx_vtcm_layout {
size_t off_s_col_tiles;
size_t off_s_update_tiles;
- size_t off_q_f32[2];
- size_t off_k_f32[2];
- size_t off_v_f32[2];
- size_t off_g_f32[2];
- size_t off_b_f32[2];
- size_t off_g_raw[2];
- size_t off_b_raw[2];
- size_t off_o_f32[2];
-
+ size_t off_q_buf[2];
+ size_t off_k_buf[2];
+ size_t off_v_buf[2];
+ size_t off_o_buf[2];
size_t off_k_f16;
+ size_t off_g_f32;
+ size_t off_b_f32;
+ size_t off_g_raw[2][2];
+ size_t off_b_raw[2][2];
- size_t off_q_row_tiles;
- size_t off_q_prime_row_tiles;
size_t off_k_row_tiles;
- size_t off_k_col_tiles;
size_t off_k_prime_row_tiles;
+ size_t off_k_col_tiles;
size_t off_k_col_tiles_64x128[2];
- size_t off_kk_tiles;
- size_t off_qk_tiles;
+ size_t off_q_row_tiles;
+ size_t off_q_prime_row_tiles;
size_t off_v_inter_tiles;
size_t off_o_inter_tiles;
- size_t off_inv_row_tiles;
- size_t off_a_row_tiles;
- size_t off_v_prime_col_tiles;
- size_t off_delta_tiles;
size_t off_o_intra_tiles;
size_t off_d_row_tiles;
+ size_t off_kk_tiles;
+ size_t off_qk_tiles;
+ size_t off_inv_row_tiles;
+ size_t off_a_row_tiles;
+
size_t off_gamma;
size_t off_lambda_init[2];
- size_t off_decay_m;
size_t off_decay_a;
- size_t off_rows_kk;
- size_t off_rows_qk;
- size_t off_rows_inv;
- size_t off_rows_a;
size_t off_hvx_scratch;
size_t off_scales_1;
size_t state_f32_bytes;
size_t state_tiles_bytes;
- size_t dma_chunk_bytes;
+ size_t q_buf_bytes;
+ size_t k_buf_bytes;
+ size_t v_buf_bytes;
+ size_t o_buf_bytes;
size_t act_f16_bytes;
+ size_t dma_scalar_bytes;
size_t tile_64xSv_bytes;
size_t tile_64x64_bytes;
-
- uint32_t n_heads_batch;
- uint32_t n_hvx;
- size_t total_bytes;
+ size_t decay_bytes;
+ size_t hvx_scratch_bytes;
+ size_t hmx_group_pad;
+ size_t total_bytes;
+ bool need_k_f16;
+ bool separate_aliases;
};
static inline void htp_gdn_hmx_vtcm_layout_build(
struct htp_gdn_hmx_vtcm_layout * L,
uint32_t S_v,
uint32_t chunk_size,
- uint32_t n_heads_batch,
- uint32_t n_hvx
+ uint32_t n_group_heads,
+ uint32_t n_groups,
+ size_t q_elem_sz,
+ size_t k_elem_sz,
+ size_t v_elem_sz,
+ size_t o_elem_sz,
+ bool pad_hmx_banks,
+ bool separate_aliases
) {
memset(L, 0, sizeof(*L));
- L->n_heads_batch = n_heads_batch;
- L->n_hvx = n_hvx;
-
- const size_t bh = (size_t) n_heads_batch;
- const size_t nth = (size_t) (n_hvx > 0 ? n_hvx : 1);
-
- const size_t state_f32_sz = hex_round_up(S_v * S_v * sizeof(float), 2048);
- const size_t n_sv_tiles = S_v / 32;
- const size_t state_tiles_sz = n_sv_tiles * n_sv_tiles * 2048;
-
- const size_t dma_chunk_sz = hex_round_up(chunk_size * S_v * sizeof(float), 2048);
- const size_t dma_scalar_sz = hex_round_up(chunk_size * sizeof(float), 128);
-
- const size_t act_f16_sz = hex_round_up(chunk_size * S_v * sizeof(__fp16), 2048);
- const size_t tile_64xSv_sz = 2 * n_sv_tiles * 2048;
- const size_t tile_64x64_sz = 4 * 2048;
-
- const size_t decay_sz = 64 * 64 * sizeof(__fp16);
- const size_t row_vecs_sz = 64 * 128;
+ L->need_k_f16 = (k_elem_sz == sizeof(float));
+ L->separate_aliases = separate_aliases;
+
+ const size_t total_h = (size_t) n_group_heads * n_groups;
+ const size_t n_sv_tiles = S_v >> 5;
+ // Odd vector strides for HVX buffers and odd tile strides for HMX buffers eliminate VTCM bank aliasing
+ const size_t hmx_pad = pad_hmx_banks ? (size_t) HMX_FP16_TILE_SIZE : 0u;
+ const size_t hmx_group_pad = (pad_hmx_banks && n_groups >= 2u) ? (size_t) HMX_FP16_TILE_SIZE : 0u;
+ const size_t state_f32_sz = hex_round_up(S_v * S_v * sizeof(float), 128) + 128u;
+ const size_t state_tiles_sz = n_sv_tiles * n_sv_tiles * HMX_FP16_TILE_SIZE + hmx_pad;
+ const size_t q_buf_sz = hex_round_up(chunk_size * S_v * q_elem_sz, 128) + 128u;
+ const size_t k_buf_sz = hex_round_up(chunk_size * S_v * k_elem_sz, 128) + 128u;
+ const size_t v_buf_sz = hex_round_up(chunk_size * S_v * v_elem_sz, 128) + 128u;
+ const size_t o_buf_sz = hex_round_up(chunk_size * S_v * o_elem_sz, 128) + 128u;
+ const size_t act_f16_sz = hex_round_up(chunk_size * S_v * sizeof(uint16_t), 128) + 128u;
+ const size_t dma_scalar_sz = hex_round_up(chunk_size * sizeof(float), 128) + 128u;
+ const size_t tile_64xSv_sz = 2u * n_sv_tiles * HMX_FP16_TILE_SIZE + hmx_pad;
+ const size_t tile_64x64_sz = 4u * HMX_FP16_TILE_SIZE + hmx_pad;
+ const size_t decay_sz = 64u * 64u * sizeof(uint16_t) + 128u;
+ const size_t hvx_scratch_sz = 64u * 128u + hmx_pad;
+ const size_t raw_gb_sz = hex_round_up((size_t) (n_group_heads + 1u) * chunk_size * sizeof(float), 128);
L->state_f32_bytes = state_f32_sz;
L->state_tiles_bytes = state_tiles_sz;
- L->dma_chunk_bytes = dma_chunk_sz;
+ L->q_buf_bytes = q_buf_sz;
+ L->k_buf_bytes = k_buf_sz;
+ L->v_buf_bytes = v_buf_sz;
+ L->o_buf_bytes = o_buf_sz;
L->act_f16_bytes = act_f16_sz;
+ L->dma_scalar_bytes = dma_scalar_sz;
L->tile_64xSv_bytes = tile_64xSv_sz;
L->tile_64x64_bytes = tile_64x64_sz;
+ L->decay_bytes = decay_sz;
+ L->hvx_scratch_bytes = hvx_scratch_sz;
+ L->hmx_group_pad = hmx_group_pad;
size_t off = 0;
- VTCM_LAYOUT_ALLOC(off, off_s_state, bh * state_f32_sz);
+ VTCM_LAYOUT_ALLOC(off, off_s_state, total_h * state_f32_sz);
+ off = hex_align_up(off, HMX_FP16_TILE_SIZE);
+ VTCM_LAYOUT_ALLOC(off, off_s_col_tiles, total_h * state_tiles_sz + hmx_group_pad);
off = hex_align_up(off, HMX_FP16_TILE_SIZE);
- VTCM_LAYOUT_ALLOC(off, off_s_col_tiles, bh * state_tiles_sz);
- VTCM_LAYOUT_ALLOC(off, off_s_update_tiles, bh * state_tiles_sz);
-
- VTCM_LAYOUT_ALLOC(off, off_q_f32[0], bh * dma_chunk_sz);
- VTCM_LAYOUT_ALLOC(off, off_q_f32[1], bh * dma_chunk_sz);
- VTCM_LAYOUT_ALLOC(off, off_k_f32[0], bh * dma_chunk_sz);
- VTCM_LAYOUT_ALLOC(off, off_k_f32[1], bh * dma_chunk_sz);
- VTCM_LAYOUT_ALLOC(off, off_v_f32[0], bh * dma_chunk_sz);
- VTCM_LAYOUT_ALLOC(off, off_v_f32[1], bh * dma_chunk_sz);
- VTCM_LAYOUT_ALLOC(off, off_g_f32[0], bh * dma_scalar_sz);
- VTCM_LAYOUT_ALLOC(off, off_g_f32[1], bh * dma_scalar_sz);
- VTCM_LAYOUT_ALLOC(off, off_b_f32[0], bh * dma_scalar_sz);
- VTCM_LAYOUT_ALLOC(off, off_b_f32[1], bh * dma_scalar_sz);
- const size_t raw_gb_sz = hex_round_up((bh + 1) * chunk_size * sizeof(float), 128);
- VTCM_LAYOUT_ALLOC(off, off_g_raw[0], raw_gb_sz);
- VTCM_LAYOUT_ALLOC(off, off_g_raw[1], raw_gb_sz);
- VTCM_LAYOUT_ALLOC(off, off_b_raw[0], raw_gb_sz);
- VTCM_LAYOUT_ALLOC(off, off_b_raw[1], raw_gb_sz);
- VTCM_LAYOUT_ALLOC(off, off_o_f32[0], bh * dma_chunk_sz);
- VTCM_LAYOUT_ALLOC(off, off_o_f32[1], bh * dma_chunk_sz);
-
- VTCM_LAYOUT_ALLOC(off, off_k_f16, bh * act_f16_sz);
+ VTCM_LAYOUT_ALLOC(off, off_s_update_tiles, total_h * state_tiles_sz + hmx_group_pad);
+
+ VTCM_LAYOUT_ALLOC(off, off_q_buf[0], total_h * q_buf_sz);
+ VTCM_LAYOUT_ALLOC(off, off_q_buf[1], total_h * q_buf_sz);
+ VTCM_LAYOUT_ALLOC(off, off_k_buf[0], total_h * k_buf_sz);
+ VTCM_LAYOUT_ALLOC(off, off_k_buf[1], total_h * k_buf_sz);
+ VTCM_LAYOUT_ALLOC(off, off_v_buf[0], total_h * v_buf_sz);
+ VTCM_LAYOUT_ALLOC(off, off_v_buf[1], total_h * v_buf_sz);
+ VTCM_LAYOUT_ALLOC(off, off_o_buf[0], total_h * o_buf_sz);
+ VTCM_LAYOUT_ALLOC(off, off_o_buf[1], total_h * o_buf_sz);
+ VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_k_f16, total_h * act_f16_sz, L->need_k_f16);
+ VTCM_LAYOUT_ALLOC(off, off_g_f32, total_h * dma_scalar_sz);
+ VTCM_LAYOUT_ALLOC(off, off_b_f32, total_h * dma_scalar_sz);
+
+ for (uint32_t g = 0u; g < 2u; ++g) {
+ const bool alloc_g = (g < n_groups);
+ VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_g_raw[g][0], raw_gb_sz, alloc_g);
+ VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_g_raw[g][1], raw_gb_sz, alloc_g);
+ VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_b_raw[g][0], raw_gb_sz, alloc_g);
+ VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_b_raw[g][1], raw_gb_sz, alloc_g);
+ }
off = hex_align_up(off, HMX_FP16_TILE_SIZE);
- VTCM_LAYOUT_ALLOC(off, off_q_row_tiles, bh * tile_64xSv_sz);
- VTCM_LAYOUT_ALLOC(off, off_q_prime_row_tiles, bh * tile_64xSv_sz);
- VTCM_LAYOUT_ALLOC(off, off_k_row_tiles, bh * tile_64xSv_sz);
- VTCM_LAYOUT_ALLOC(off, off_k_col_tiles, bh * tile_64xSv_sz);
- VTCM_LAYOUT_ALLOC(off, off_k_prime_row_tiles, bh * tile_64xSv_sz);
- VTCM_LAYOUT_ALLOC(off, off_k_col_tiles_64x128[0], bh * tile_64xSv_sz);
- VTCM_LAYOUT_ALLOC(off, off_k_col_tiles_64x128[1], bh * tile_64xSv_sz);
- VTCM_LAYOUT_ALLOC(off, off_kk_tiles, bh * tile_64x64_sz);
- VTCM_LAYOUT_ALLOC(off, off_qk_tiles, bh * tile_64x64_sz);
- VTCM_LAYOUT_ALLOC(off, off_v_inter_tiles, bh * tile_64xSv_sz);
- VTCM_LAYOUT_ALLOC(off, off_o_inter_tiles, bh * tile_64xSv_sz);
- VTCM_LAYOUT_ALLOC(off, off_inv_row_tiles, bh * tile_64x64_sz);
- VTCM_LAYOUT_ALLOC(off, off_a_row_tiles, bh * tile_64x64_sz);
- VTCM_LAYOUT_ALLOC(off, off_v_prime_col_tiles, bh * tile_64xSv_sz);
- VTCM_LAYOUT_ALLOC(off, off_delta_tiles, bh * tile_64xSv_sz);
- VTCM_LAYOUT_ALLOC(off, off_o_intra_tiles, bh * tile_64xSv_sz);
- VTCM_LAYOUT_ALLOC(off, off_d_row_tiles, bh * tile_64xSv_sz);
-
- VTCM_LAYOUT_ALLOC(off, off_gamma, bh * hex_round_up(chunk_size * sizeof(float), 128));
- VTCM_LAYOUT_ALLOC(off, off_lambda_init[0], bh * hex_round_up(chunk_size * sizeof(float), 128));
- VTCM_LAYOUT_ALLOC(off, off_lambda_init[1], bh * hex_round_up(chunk_size * sizeof(float), 128));
- VTCM_LAYOUT_ALLOC(off, off_decay_m, bh * decay_sz);
- VTCM_LAYOUT_ALLOC(off, off_decay_a, bh * decay_sz);
- VTCM_LAYOUT_ALLOC(off, off_rows_kk, bh * row_vecs_sz);
- VTCM_LAYOUT_ALLOC(off, off_rows_qk, bh * row_vecs_sz);
- VTCM_LAYOUT_ALLOC(off, off_rows_inv, bh * row_vecs_sz);
- VTCM_LAYOUT_ALLOC(off, off_rows_a, bh * row_vecs_sz);
-
- const size_t hvx_scratch_sz = 64 * 128;
+ VTCM_LAYOUT_ALLOC(off, off_k_row_tiles, total_h * tile_64xSv_sz + hmx_group_pad);
+ VTCM_LAYOUT_ALLOC(off, off_k_prime_row_tiles, total_h * tile_64xSv_sz + hmx_group_pad);
+ VTCM_LAYOUT_ALLOC(off, off_k_col_tiles, total_h * tile_64xSv_sz + hmx_group_pad);
+ VTCM_LAYOUT_ALLOC(off, off_k_col_tiles_64x128[0], total_h * tile_64xSv_sz + hmx_group_pad);
+ VTCM_LAYOUT_ALLOC(off, off_k_col_tiles_64x128[1], total_h * tile_64xSv_sz + hmx_group_pad);
+ VTCM_LAYOUT_ALLOC(off, off_q_row_tiles, total_h * tile_64xSv_sz + hmx_group_pad);
+ VTCM_LAYOUT_ALLOC(off, off_q_prime_row_tiles, total_h * tile_64xSv_sz + hmx_group_pad);
+ VTCM_LAYOUT_ALLOC(off, off_v_inter_tiles, total_h * tile_64xSv_sz + hmx_group_pad);
+ VTCM_LAYOUT_ALLOC(off, off_o_inter_tiles, total_h * tile_64xSv_sz + hmx_group_pad);
+ VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_o_intra_tiles, total_h * tile_64xSv_sz + hmx_group_pad, separate_aliases);
+ VTCM_LAYOUT_ALLOC(off, off_d_row_tiles, total_h * tile_64xSv_sz + hmx_group_pad);
+
+ VTCM_LAYOUT_ALLOC(off, off_kk_tiles, total_h * tile_64x64_sz + hmx_group_pad);
+ VTCM_LAYOUT_ALLOC(off, off_qk_tiles, total_h * tile_64x64_sz + hmx_group_pad);
+ VTCM_LAYOUT_ALLOC(off, off_inv_row_tiles, total_h * tile_64x64_sz + hmx_group_pad);
+ VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_a_row_tiles, total_h * tile_64x64_sz + hmx_group_pad, separate_aliases);
+
+ VTCM_LAYOUT_ALLOC(off, off_gamma, total_h * dma_scalar_sz);
+ VTCM_LAYOUT_ALLOC(off, off_lambda_init[0], total_h * dma_scalar_sz);
+ VTCM_LAYOUT_ALLOC(off, off_lambda_init[1], total_h * dma_scalar_sz);
+ VTCM_LAYOUT_ALLOC(off, off_decay_a, total_h * decay_sz);
+
off = hex_align_up(off, HMX_FP16_TILE_SIZE);
- VTCM_LAYOUT_ALLOC(off, off_hvx_scratch, nth * hvx_scratch_sz);
+ VTCM_LAYOUT_ALLOC(off, off_hvx_scratch, total_h * hvx_scratch_sz + hmx_group_pad);
off = hex_align_up(off, HMX_FP16_TILE_SIZE);
- VTCM_LAYOUT_ALLOC(off, off_scales_1, HMX_FP16_TILE_SIZE);
+ VTCM_LAYOUT_ALLOC(off, off_scales_1, HMX_FP16_TILE_SIZE);
L->total_bytes = off;
}
static inline bool htp_gdn_hmx_solve_layout(
struct htp_gdn_hmx_vtcm_layout * layout_out,
- uint32_t S_v,
- uint32_t chunk_size,
- uint32_t total_rows,
- size_t vtcm_budget,
- uint32_t n_hvx,
- uint32_t * n_heads_batch_out
+ uint32_t S_v,
+ uint32_t chunk_size,
+ uint32_t max_heads_in_batch,
+ size_t q_elem_sz,
+ size_t k_elem_sz,
+ size_t v_elem_sz,
+ size_t o_elem_sz,
+ size_t vtcm_budget,
+ uint32_t n_hvx,
+ uint32_t * group_size_out,
+ uint32_t * n_groups_out,
+ uint8_t * hmx_flags_out
) {
- uint32_t max_batch = 8;
- if (max_batch > total_rows) {
- max_batch = total_rows;
- }
- if (max_batch > n_hvx) {
- max_batch = n_hvx;
- }
- static const uint32_t candidates[] = { 8, 6, 4, 2, 1 };
- for (size_t i = 0; i < sizeof(candidates) / sizeof(candidates[0]); ++i) {
- uint32_t bh = candidates[i];
- if (bh > max_batch) {
- continue;
+ static const uint32_t candidates[] = { 8u, 6u, 4u, 2u, 1u };
+
+ // Prefer 2-group ping-pong when at least 2 heads exist, with HMX bank padding when it fits
+ for (uint32_t ng = (max_heads_in_batch >= 2u) ? 2u : 1u; ng >= 1u; --ng) {
+ uint32_t max_g = 8u;
+ if (max_g > n_hvx) {
+ max_g = n_hvx;
+ }
+ if (max_g > max_heads_in_batch / ng) {
+ max_g = max_heads_in_batch / ng;
}
- struct htp_gdn_hmx_vtcm_layout L;
- htp_gdn_hmx_vtcm_layout_build(&L, S_v, chunk_size, bh, n_hvx);
- if (L.total_bytes <= vtcm_budget) {
- *layout_out = L;
- *n_heads_batch_out = bh;
- return true;
+ for (size_t i = 0; i < sizeof(candidates) / sizeof(candidates[0]); ++i) {
+ const uint32_t gs = candidates[i];
+ if (gs > max_g) {
+ continue;
+ }
+ const bool want_sep = (n_hvx >= 2u * gs);
+ for (int sep = want_sep ? 1 : 0; sep >= 0; --sep) {
+ for (int pad = 1; pad >= 0; --pad) {
+ struct htp_gdn_hmx_vtcm_layout L;
+ htp_gdn_hmx_vtcm_layout_build(&L, S_v, chunk_size, gs, ng,
+ q_elem_sz, k_elem_sz, v_elem_sz, o_elem_sz,
+ pad != 0, sep != 0);
+ if (L.total_bytes <= vtcm_budget) {
+ *layout_out = L;
+ *group_size_out = gs;
+ *n_groups_out = ng;
+ *hmx_flags_out = (uint8_t) ((pad ? HTP_GDN_HMX_FLAG_PAD_BANKS : 0u) |
+ (sep ? HTP_GDN_HMX_FLAG_SEPARATE_ALIAS : 0u));
+ return true;
+ }
+ }
+ }
}
}
return false;
diff --git a/ggml/src/ggml-hexagon/htp/glu-ops.c b/ggml/src/ggml-hexagon/htp/glu-ops.c
index cecc0e538..36b3d7882 100644
--- a/ggml/src/ggml-hexagon/htp/glu-ops.c
+++ b/ggml/src/ggml-hexagon/htp/glu-ops.c
@@ -544,8 +544,8 @@ static __attribute__((noinline)) void hvx_swiglu_f16_vec(HVX_Vector * restrict v
HVX_Vector x0 = vsrc0[i + 0];
HVX_Vector x1 = vsrc0[i + 1];
HVX_VectorPair sigp = hvx_vec_fast_sigmoid_f16_pair(x0, x1);
- vdst[i + 0] = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(x0, Q6_V_lo_W(sigp)), vsrc1[i + 0]);
- vdst[i + 1] = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(x1, Q6_V_hi_W(sigp)), vsrc1[i + 1]);
+ vdst[i + 0] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(x0, Q6_V_lo_W(sigp)), vsrc1[i + 0]));
+ vdst[i + 1] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(x1, Q6_V_hi_W(sigp)), vsrc1[i + 1]));
}
}
@@ -555,7 +555,7 @@ static __attribute__((noinline)) void hvx_swiglu_f16_tail(HVX_Vector * restrict
uint32_t nloe) {
HVX_Vector x = vsrc0[0];
HVX_Vector sig = hvx_vec_fast_sigmoid_f16(x);
- HVX_Vector res = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(x, sig), vsrc1[0]);
+ HVX_Vector res = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(x, sig), vsrc1[0]));
hvx_vec_store_a((void *) vdst, nloe * sizeof(_Float16), res);
}
diff --git a/ggml/src/ggml-hexagon/htp/htp-ops.h b/ggml/src/ggml-hexagon/htp/htp-ops.h
index 7f3885a23..59391449a 100644
--- a/ggml/src/ggml-hexagon/htp/htp-ops.h
+++ b/ggml/src/ggml-hexagon/htp/htp-ops.h
@@ -57,6 +57,7 @@ enum htp_op_code {
HTP_OP_RMS_NORM,
HTP_OP_RMS_NORM_MUL,
HTP_OP_RMS_NORM_MUL_ADD,
+ HTP_OP_RMS_NORM_MUL_GLU,
HTP_OP_UNARY_SILU,
HTP_OP_UNARY_GELU,
HTP_OP_UNARY_SIGMOID,
diff --git a/ggml/src/ggml-hexagon/htp/hvx-glu.h b/ggml/src/ggml-hexagon/htp/hvx-glu.h
new file mode 100644
index 000000000..7549b9280
--- /dev/null
+++ b/ggml/src/ggml-hexagon/htp/hvx-glu.h
@@ -0,0 +1,33 @@
+#ifndef HVX_GLU_H
+#define HVX_GLU_H
+
+#include "htp-ops.h"
+#include "hvx-sigmoid.h"
+
+// Shared GLU activation helpers for matmul-ops.c and unary-ops.c.
+
+static inline HVX_Vector hvx_glu_geglu_arg_f16(HVX_Vector x) {
+ // 0.5 * x * (1 + tanh(inner)) = x * sigmoid(2 * inner)
+ const HVX_Vector v_c0 = hvx_vec_splat_f16(2.0f * 0.7978845608028654f * 0.044715f);
+ const HVX_Vector v_c1 = hvx_vec_splat_f16(2.0f * 0.7978845608028654f);
+ const HVX_Vector x2 = hvx_vec_mul_f16_f16(x, x);
+ const HVX_Vector poly = hvx_vec_add_f16_f16(hvx_vec_mul_f16_f16(x2, v_c0), v_c1);
+ return hvx_vec_mul_f16_f16(x, poly);
+}
+
+static inline HVX_Vector hvx_glu_geglu_arg_f32(HVX_Vector x) {
+ const HVX_Vector v_c0 = hvx_vec_splat_f32(2.0f * 0.7978845608028654f * 0.044715f);
+ const HVX_Vector v_c1 = hvx_vec_splat_f32(2.0f * 0.7978845608028654f);
+ const HVX_Vector x2 = hvx_vec_mul_f32_f32(x, x);
+ const HVX_Vector poly = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x2, v_c0), v_c1);
+ return hvx_vec_mul_f32_f32(x, poly);
+}
+
+#define HVX_GLU_ARG_SWIGLU_F16(x) (x)
+#define HVX_GLU_ARG_SWIGLU_F32(x) (x)
+#define HVX_GLU_ARG_GEGLU_F16(x) hvx_glu_geglu_arg_f16(x)
+#define HVX_GLU_ARG_GEGLU_F32(x) hvx_glu_geglu_arg_f32(x)
+#define HVX_GLU_ARG_GEGLU_QUICK_F16(x) hvx_vec_mul_f16_f16((x), hvx_vec_splat_f16(1.702f))
+#define HVX_GLU_ARG_GEGLU_QUICK_F32(x) hvx_vec_mul_f32_f32((x), hvx_vec_splat_f32(1.702f))
+
+#endif /* HVX_GLU_H */
diff --git a/ggml/src/ggml-hexagon/htp/main.c b/ggml/src/ggml-hexagon/htp/main.c
index 5fa3b186a..ddeb64e6d 100644
--- a/ggml/src/ggml-hexagon/htp/main.c
+++ b/ggml/src/ggml-hexagon/htp/main.c
@@ -973,6 +973,7 @@ static int execute_op(struct htp_ops_context * octx) {
case HTP_OP_RMS_NORM:
case HTP_OP_RMS_NORM_MUL:
case HTP_OP_RMS_NORM_MUL_ADD:
+ case HTP_OP_RMS_NORM_MUL_GLU:
case HTP_OP_SCALE:
case HTP_OP_CLAMP:
case HTP_OP_LEAKY_RELU:
diff --git a/ggml/src/ggml-hexagon/htp/matmul-ops.c b/ggml/src/ggml-hexagon/htp/matmul-ops.c
index ec64637c9..a35218aaf 100644
--- a/ggml/src/ggml-hexagon/htp/matmul-ops.c
+++ b/ggml/src/ggml-hexagon/htp/matmul-ops.c
@@ -2,6 +2,7 @@
#include "matmul-hmx-w8a8.h"
#include "matmul-hmx-f16.h"
#include "matmul-hvx.h"
+#include "hvx-glu.h"
// --- Shared LUTs ---
@@ -188,30 +189,6 @@ __attribute__((noinline)) void hvx_rms_norm_mul_rows_f16(
// --- Shared HMX GLU Store & Dispatch Helpers ---
-static inline HVX_Vector hmx_glu_geglu_arg_f16(HVX_Vector x) {
- // 0.5 * x * (1 + tanh(inner)) = x * sigmoid(2 * inner)
- const HVX_Vector v_c0 = hvx_vec_splat_f16(2.0f * 0.7978845608028654f * 0.044715f);
- const HVX_Vector v_c1 = hvx_vec_splat_f16(2.0f * 0.7978845608028654f);
- const HVX_Vector x2 = hvx_vec_mul_f16_f16(x, x);
- const HVX_Vector poly = hvx_vec_add_f16_f16(hvx_vec_mul_f16_f16(x2, v_c0), v_c1);
- return hvx_vec_mul_f16_f16(x, poly);
-}
-
-static inline HVX_Vector hmx_glu_geglu_arg_f32(HVX_Vector x) {
- const HVX_Vector v_c0 = hvx_vec_splat_f32(2.0f * 0.7978845608028654f * 0.044715f);
- const HVX_Vector v_c1 = hvx_vec_splat_f32(2.0f * 0.7978845608028654f);
- const HVX_Vector x2 = hvx_vec_mul_f32_f32(x, x);
- const HVX_Vector poly = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(x2, v_c0), v_c1);
- return hvx_vec_mul_f32_f32(x, poly);
-}
-
-#define HMX_GLU_ARG_SWIGLU_F16(x) (x)
-#define HMX_GLU_ARG_SWIGLU_F32(x) (x)
-#define HMX_GLU_ARG_GEGLU_F16(x) hmx_glu_geglu_arg_f16(x)
-#define HMX_GLU_ARG_GEGLU_F32(x) hmx_glu_geglu_arg_f32(x)
-#define HMX_GLU_ARG_GEGLU_QUICK_F16(x) hvx_vec_mul_f16_f16((x), hvx_vec_splat_f16(1.702f))
-#define HMX_GLU_ARG_GEGLU_QUICK_F32(x) hvx_vec_mul_f32_f32((x), hvx_vec_splat_f32(1.702f))
-
// Sigmoid-based GLU stores: out = g * sigmoid(ARG(g)) * u (vtcm_res_out is always 128B aligned).
#define DEFINE_HMX_SIG_GLU_STORE(NAME, ARG_F16, ARG_F32) \
static __attribute__((noinline)) void hmx_glu_##NAME##_store_tail_f16( \
@@ -279,9 +256,9 @@ static __attribute__((noinline)) void hmx_glu_##NAME##_store_quad_f32(
hmx_glu_##NAME##_store_tail_f32(dst, dst_stride, g_q, u_q, (valid_rows << 16) | 128u); \
}
-DEFINE_HMX_SIG_GLU_STORE(swiglu, HMX_GLU_ARG_SWIGLU_F16, HMX_GLU_ARG_SWIGLU_F32)
-DEFINE_HMX_SIG_GLU_STORE(geglu, HMX_GLU_ARG_GEGLU_F16, HMX_GLU_ARG_GEGLU_F32)
-DEFINE_HMX_SIG_GLU_STORE(geglu_quick, HMX_GLU_ARG_GEGLU_QUICK_F16, HMX_GLU_ARG_GEGLU_QUICK_F32)
+DEFINE_HMX_SIG_GLU_STORE(swiglu, HVX_GLU_ARG_SWIGLU_F16, HVX_GLU_ARG_SWIGLU_F32)
+DEFINE_HMX_SIG_GLU_STORE(geglu, HVX_GLU_ARG_GEGLU_F16, HVX_GLU_ARG_GEGLU_F32)
+DEFINE_HMX_SIG_GLU_STORE(geglu_quick, HVX_GLU_ARG_GEGLU_QUICK_F16, HVX_GLU_ARG_GEGLU_QUICK_F32)
static __attribute__((noinline)) void hmx_glu_sig_apply_strip_f16(
void * restrict dst_ptr,
@@ -335,8 +312,8 @@ static __attribute__((noinline)) void hmx_glu_##NAME##_store_strip_f16(
hmx_glu_sig_apply_strip_f16(dst_ptr, dst_stride, scratch, g_f16, u_f16, n_rows); \
}
-DEFINE_HMX_SIG_GLU_STRIP_F16(geglu, HMX_GLU_ARG_GEGLU_F16)
-DEFINE_HMX_SIG_GLU_STRIP_F16(geglu_quick, HMX_GLU_ARG_GEGLU_QUICK_F16)
+DEFINE_HMX_SIG_GLU_STRIP_F16(geglu, HVX_GLU_ARG_GEGLU_F16)
+DEFINE_HMX_SIG_GLU_STRIP_F16(geglu_quick, HVX_GLU_ARG_GEGLU_QUICK_F16)
static __attribute__((noinline)) void hmx_glu_reglu_store_tail_f16(
void * restrict dst_ptr,
diff --git a/ggml/src/ggml-hexagon/htp/unary-ops.c b/ggml/src/ggml-hexagon/htp/unary-ops.c
index 223035fb6..b4a23f671 100644
--- a/ggml/src/ggml-hexagon/htp/unary-ops.c
+++ b/ggml/src/ggml-hexagon/htp/unary-ops.c
@@ -8,6 +8,7 @@
#include "dma-pipeline.h"
#include "hex-fastdiv.h"
#include "hvx-exp.h"
+#include "hvx-glu.h"
#include "hvx-sigmoid.h"
#include "hvx-utils.h"
#include "unary-ops.h"
@@ -159,44 +160,44 @@ static inline uint32_t unary_block_size(uint32_t ir,
return limit;
}
-static inline void unary_push_src0_dma(dma_queue * dma_q,
- void * dst_vtcm,
- dma_addr_t data_src,
- uint32_t ir,
- uint32_t block_size,
- bool src0_contig,
- bool src0_slice_dma,
- uint32_t ne01,
- uint32_t ne02,
- const struct fastdiv_values * div_ne01,
- const struct fastdiv_values * div_ne02,
- const struct fastdiv_values * div_ne012,
- size_t nb01,
- size_t nb02,
- size_t nb03,
- size_t src0_row_size_aligned,
- size_t src0_data_row_size) {
- if (src0_contig) {
+static inline void unary_push_src_dma(dma_queue * dma_q,
+ void * dst_vtcm,
+ dma_addr_t data_src,
+ uint32_t ir,
+ uint32_t block_size,
+ bool src_contig,
+ bool src_slice_dma,
+ uint32_t ne01,
+ uint32_t ne02,
+ const struct fastdiv_values * div_ne01,
+ const struct fastdiv_values * div_ne02,
+ const struct fastdiv_values * div_ne012,
+ size_t nb1,
+ size_t nb2,
+ size_t nb3,
+ size_t row_size_aligned,
+ size_t data_row_size) {
+ if (src_contig) {
dma_queue_push(dma_q,
- dma_make_data(dst_vtcm, data_src + (size_t) ir * nb01),
- src0_row_size_aligned, nb01, src0_data_row_size, block_size);
+ dma_make_data(dst_vtcm, data_src + (size_t) ir * nb1),
+ row_size_aligned, nb1, data_row_size, block_size);
return;
}
- if (src0_slice_dma) {
+ if (src_slice_dma) {
const uint32_t slice_idx = fastdiv(ir, div_ne01);
if (ir == slice_idx * ne01 && block_size >= ne01) {
- const size_t slice_bytes = (size_t) ne01 * src0_data_row_size;
+ const size_t slice_bytes = (size_t) ne01 * data_row_size;
const uint32_t n_slices = fastdiv(block_size, div_ne01);
dma_queue_push(dma_q,
- dma_make_data(dst_vtcm, data_src + (size_t) slice_idx * nb02),
- slice_bytes, nb02, slice_bytes, n_slices);
+ dma_make_data(dst_vtcm, data_src + (size_t) slice_idx * nb2),
+ slice_bytes, nb2, slice_bytes, n_slices);
return;
}
}
- const size_t src0_off = unary_row_offset(ir, ne01, ne02, div_ne01, div_ne02, div_ne012, nb01, nb02, nb03);
+ const size_t src_off = unary_row_offset(ir, ne01, ne02, div_ne01, div_ne02, div_ne012, nb1, nb2, nb3);
dma_queue_push(dma_q,
- dma_make_data(dst_vtcm, data_src + src0_off),
- src0_row_size_aligned, nb01, src0_data_row_size, block_size);
+ dma_make_data(dst_vtcm, data_src + src_off),
+ row_size_aligned, nb1, data_row_size, block_size);
}
#define htp_unary_preamble \
@@ -1530,6 +1531,311 @@ static const unary_compute_fn_t rms_norm_rows_table[2][2] = {
{ rms_norm_rows_f16_f32, rms_norm_rows_f16_f16 },
};
+static inline HVX_Vector rms_norm_128_row_sum_sq_f16(const uint8_t * restrict sp) {
+ HVX_Vector a = *(const HVX_Vector *) (sp + 0);
+ HVX_Vector b = *(const HVX_Vector *) (sp + VLEN);
+ HVX_VectorPair acc = hvx_vec_mpyacc_f32_f16(hvx_vec_mpy_f32_f16(a, a), b, b);
+ return hvx_vec_add_f32_f32(Q6_V_lo_W(acc), Q6_V_hi_W(acc));
+}
+
+static inline HVX_Vector rms_norm_256_row_sum_sq_f16(const uint8_t * restrict sp) {
+ HVX_Vector a = *(const HVX_Vector *) (sp + 0);
+ HVX_Vector b = *(const HVX_Vector *) (sp + VLEN);
+ HVX_Vector c = *(const HVX_Vector *) (sp + 2 * VLEN);
+ HVX_Vector d = *(const HVX_Vector *) (sp + 3 * VLEN);
+ HVX_VectorPair acc = hvx_vec_mpy_f32_f16(a, a);
+ acc = hvx_vec_mpyacc_f32_f16(acc, b, b);
+ acc = hvx_vec_mpyacc_f32_f16(acc, c, c);
+ acc = hvx_vec_mpyacc_f32_f16(acc, d, d);
+ return hvx_vec_add_f32_f32(Q6_V_lo_W(acc), Q6_V_hi_W(acc));
+}
+
+#define DEFINE_RMS_NORM_128_8ROWS_F16(NAME, HAS_MUL) \
+static __attribute__((noinline)) void NAME(const uint8_t * restrict src, \
+ size_t src_stride, \
+ uint8_t * restrict dst, \
+ size_t dst_stride, \
+ uint32_t num_rows_8, \
+ HVX_Vector eps_v, \
+ HVX_Vector scale_param_v, \
+ HVX_Vector wh0, \
+ HVX_Vector wh1) { \
+ (void) wh0; \
+ (void) wh1; \
+ const HVX_Vector inv_ne0_v = hvx_vec_splat_f32(1.0f / 128.0f); \
+ const HVX_Vector f16_max = hvx_vec_splat_f32(65504.0f); \
+ const HVX_Vector f16_min = hvx_vec_splat_f32(-65504.0f); \
+ const uint8_t * restrict s = src; \
+ uint8_t * restrict d = dst; \
+ _Pragma("clang loop unroll(disable) pipeline(disable)") \
+ for (uint32_t ir = 0; ir < num_rows_8; ir += 8) { \
+ const uint8_t * restrict sp = s; \
+ HVX_Vector sum0 = rms_norm_128_row_sum_sq_f16(sp); sp += src_stride; \
+ HVX_Vector sum1 = rms_norm_128_row_sum_sq_f16(sp); sp += src_stride; \
+ HVX_Vector sum2 = rms_norm_128_row_sum_sq_f16(sp); sp += src_stride; \
+ HVX_Vector sum3 = rms_norm_128_row_sum_sq_f16(sp); sp += src_stride; \
+ HVX_Vector_x4 sums_a = { .v = { sum0, sum1, sum2, sum3 } }; \
+ HVX_Vector sum4_a = hvx_vec_reduce_sum_f32x4(sums_a); \
+ \
+ HVX_Vector sum4 = rms_norm_128_row_sum_sq_f16(sp); sp += src_stride; \
+ HVX_Vector sum5 = rms_norm_128_row_sum_sq_f16(sp); sp += src_stride; \
+ HVX_Vector sum6 = rms_norm_128_row_sum_sq_f16(sp); sp += src_stride; \
+ HVX_Vector sum7 = rms_norm_128_row_sum_sq_f16(sp); \
+ HVX_Vector_x4 sums_b = { .v = { sum4, sum5, sum6, sum7 } }; \
+ HVX_Vector sum4_b = hvx_vec_reduce_sum_f32x4(sums_b); \
+ \
+ HVX_Vector mean_eps_a = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(sum4_a, inv_ne0_v), eps_v); \
+ HVX_Vector mean_eps_b = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(sum4_b, inv_ne0_v), eps_v); \
+ HVX_Vector scale4_a = hvx_vec_mul_f32_f32(hvx_vec_rsqrt_f32(mean_eps_a), scale_param_v); \
+ HVX_Vector scale4_b = hvx_vec_mul_f32_f32(hvx_vec_rsqrt_f32(mean_eps_b), scale_param_v); \
+ \
+ scale4_a = Q6_Vsf_vmax_VsfVsf(Q6_Vsf_vmin_VsfVsf(scale4_a, f16_max), f16_min); \
+ scale4_b = Q6_Vsf_vmax_VsfVsf(Q6_Vsf_vmin_VsfVsf(scale4_b, f16_max), f16_min); \
+ \
+ HVX_Vector vh = hvx_vec_f32_to_f16_shuff(scale4_a, scale4_b); \
+ HVX_VectorPair p1 = Q6_W_vshuff_VVR(vh, vh, 2); \
+ HVX_VectorPair pa1 = Q6_W_vshuff_VVR(Q6_V_lo_W(p1), Q6_V_lo_W(p1), 4); \
+ HVX_VectorPair pa02 = Q6_W_vshuff_VVR(Q6_V_lo_W(pa1), Q6_V_lo_W(pa1), 8); \
+ HVX_VectorPair pa13 = Q6_W_vshuff_VVR(Q6_V_hi_W(pa1), Q6_V_hi_W(pa1), 8); \
+ HVX_Vector sh_a[4] = { Q6_V_lo_W(pa02), Q6_V_lo_W(pa13), Q6_V_hi_W(pa02), Q6_V_hi_W(pa13) }; \
+ \
+ /* Reload from VTCM rather than CSE-spilling 16 input vectors across rsqrt */ \
+ const uint8_t * restrict sa = s; \
+ uint8_t * restrict da = d; \
+ asm("" : "+r"(sa)); \
+ \
+ _Pragma("unroll(4)") \
+ for (uint32_t r = 0; r < 4; r++) { \
+ const HVX_Vector * restrict vr_src = (const HVX_Vector *) sa; \
+ HVX_Vector * restrict vr_dst = (HVX_Vector *) da; \
+ if (HAS_MUL) { \
+ vr_dst[0] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(vr_src[0], sh_a[r]), wh0));\
+ vr_dst[1] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(vr_src[1], sh_a[r]), wh1));\
+ } else { \
+ vr_dst[0] = hvx_vec_mul_f16_f16(vr_src[0], sh_a[r]); \
+ vr_dst[1] = hvx_vec_mul_f16_f16(vr_src[1], sh_a[r]); \
+ } \
+ sa += src_stride; \
+ da += dst_stride; \
+ } \
+ \
+ HVX_VectorPair pb1 = Q6_W_vshuff_VVR(Q6_V_hi_W(p1), Q6_V_hi_W(p1), 4); \
+ HVX_VectorPair pb46 = Q6_W_vshuff_VVR(Q6_V_lo_W(pb1), Q6_V_lo_W(pb1), 8); \
+ HVX_VectorPair pb57 = Q6_W_vshuff_VVR(Q6_V_hi_W(pb1), Q6_V_hi_W(pb1), 8); \
+ HVX_Vector sh_b[4] = { Q6_V_lo_W(pb46), Q6_V_lo_W(pb57), Q6_V_hi_W(pb46), Q6_V_hi_W(pb57) }; \
+ \
+ _Pragma("unroll(4)") \
+ for (uint32_t r = 0; r < 4; r++) { \
+ const HVX_Vector * restrict vr_src = (const HVX_Vector *) sa; \
+ HVX_Vector * restrict vr_dst = (HVX_Vector *) da; \
+ if (HAS_MUL) { \
+ vr_dst[0] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(vr_src[0], sh_b[r]), wh0));\
+ vr_dst[1] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(vr_src[1], sh_b[r]), wh1));\
+ } else { \
+ vr_dst[0] = hvx_vec_mul_f16_f16(vr_src[0], sh_b[r]); \
+ vr_dst[1] = hvx_vec_mul_f16_f16(vr_src[1], sh_b[r]); \
+ } \
+ sa += src_stride; \
+ da += dst_stride; \
+ } \
+ \
+ s += 8 * src_stride; \
+ d += 8 * dst_stride; \
+ } \
+}
+
+DEFINE_RMS_NORM_128_8ROWS_F16(rms_norm_128_8rows_f16_nomul, false)
+DEFINE_RMS_NORM_128_8ROWS_F16(rms_norm_128_8rows_f16_mul, true)
+#undef DEFINE_RMS_NORM_128_8ROWS_F16
+
+#define DEFINE_RMS_NORM_256_4ROWS_F16(NAME, HAS_MUL) \
+static __attribute__((noinline)) void NAME(const uint8_t * restrict src, \
+ size_t src_stride, \
+ uint8_t * restrict dst, \
+ size_t dst_stride, \
+ uint32_t num_rows_4, \
+ HVX_Vector eps_v, \
+ HVX_Vector scale_param_v, \
+ HVX_Vector wh0, \
+ HVX_Vector wh1, \
+ HVX_Vector wh2, \
+ HVX_Vector wh3) { \
+ (void) wh0; \
+ (void) wh1; \
+ (void) wh2; \
+ (void) wh3; \
+ const HVX_Vector inv_ne0_v = hvx_vec_splat_f32(1.0f / 256.0f); \
+ const HVX_Vector f16_max = hvx_vec_splat_f32(65504.0f); \
+ const HVX_Vector f16_min = hvx_vec_splat_f32(-65504.0f); \
+ const uint8_t * restrict s = src; \
+ uint8_t * restrict d = dst; \
+ _Pragma("clang loop unroll(disable) pipeline(disable)") \
+ for (uint32_t ir = 0; ir < num_rows_4; ir += 4) { \
+ const uint8_t * restrict sp = s; \
+ HVX_Vector sum0 = rms_norm_256_row_sum_sq_f16(sp); sp += src_stride; \
+ HVX_Vector sum1 = rms_norm_256_row_sum_sq_f16(sp); sp += src_stride; \
+ HVX_Vector sum2 = rms_norm_256_row_sum_sq_f16(sp); sp += src_stride; \
+ HVX_Vector sum3 = rms_norm_256_row_sum_sq_f16(sp); \
+ HVX_Vector_x4 sums = { .v = { sum0, sum1, sum2, sum3 } }; \
+ HVX_Vector sum4 = hvx_vec_reduce_sum_f32x4(sums); \
+ \
+ HVX_Vector mean_eps = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(sum4, inv_ne0_v), eps_v); \
+ HVX_Vector scale4 = hvx_vec_mul_f32_f32(hvx_vec_rsqrt_f32(mean_eps), scale_param_v); \
+ scale4 = Q6_Vsf_vmax_VsfVsf(Q6_Vsf_vmin_VsfVsf(scale4, f16_max), f16_min); \
+ \
+ HVX_Vector vh = hvx_vec_f32_to_f16_shuff(scale4, scale4); \
+ HVX_VectorPair pa1 = Q6_W_vshuff_VVR(vh, vh, 4); \
+ HVX_VectorPair pa02 = Q6_W_vshuff_VVR(Q6_V_lo_W(pa1), Q6_V_lo_W(pa1), 8); \
+ HVX_VectorPair pa13 = Q6_W_vshuff_VVR(Q6_V_hi_W(pa1), Q6_V_hi_W(pa1), 8); \
+ HVX_Vector sh[4] = { Q6_V_lo_W(pa02), Q6_V_lo_W(pa13), Q6_V_hi_W(pa02), Q6_V_hi_W(pa13) }; \
+ \
+ /* Reload from VTCM rather than CSE-spilling 16 input vectors across rsqrt */ \
+ const uint8_t * restrict sa = s; \
+ uint8_t * restrict da = d; \
+ asm("" : "+r"(sa)); \
+ \
+ _Pragma("unroll(4)") \
+ for (uint32_t r = 0; r < 4; r++) { \
+ const HVX_Vector * restrict vr_src = (const HVX_Vector *) sa; \
+ HVX_Vector * restrict vr_dst = (HVX_Vector *) da; \
+ if (HAS_MUL) { \
+ vr_dst[0] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(vr_src[0], sh[r]), wh0)); \
+ vr_dst[1] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(vr_src[1], sh[r]), wh1)); \
+ vr_dst[2] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(vr_src[2], sh[r]), wh2)); \
+ vr_dst[3] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(vr_src[3], sh[r]), wh3)); \
+ } else { \
+ vr_dst[0] = hvx_vec_mul_f16_f16(vr_src[0], sh[r]); \
+ vr_dst[1] = hvx_vec_mul_f16_f16(vr_src[1], sh[r]); \
+ vr_dst[2] = hvx_vec_mul_f16_f16(vr_src[2], sh[r]); \
+ vr_dst[3] = hvx_vec_mul_f16_f16(vr_src[3], sh[r]); \
+ } \
+ sa += src_stride; \
+ da += dst_stride; \
+ } \
+ \
+ s += 4 * src_stride; \
+ d += 4 * dst_stride; \
+ } \
+}
+
+DEFINE_RMS_NORM_256_4ROWS_F16(rms_norm_256_4rows_f16_nomul, false)
+DEFINE_RMS_NORM_256_4ROWS_F16(rms_norm_256_4rows_f16_mul, true)
+#undef DEFINE_RMS_NORM_256_4ROWS_F16
+
+static __attribute__((noinline)) void rms_norm_rows_f16_f16_ne128(const void * restrict src,
+ void * restrict dst,
+ const uint32_t num_rows,
+ const uint32_t ne0,
+ const struct htp_unary_context * uctx) {
+ htp_unary_op_preamble;
+ float epsilon = 0.f;
+ float scale = 0.f;
+ memcpy(&epsilon, &op_params[0], sizeof(float));
+ memcpy(&scale, &op_params[1], sizeof(float));
+
+ const uint32_t num_rows_8 = num_rows & ~7u;
+ if (num_rows_8 > 0) {
+ const HVX_Vector eps_v = hvx_vec_splat_f32(epsilon);
+ const HVX_Vector scale_param_v = hvx_vec_splat_f32((scale != 0.0f) ? scale : 1.0f);
+ rms_norm_128_8rows_f16_nomul((const uint8_t *) src, src0_row_size_aligned,
+ (uint8_t *) dst, dst_row_size_aligned,
+ num_rows_8, eps_v, scale_param_v,
+ Q6_V_vzero(), Q6_V_vzero());
+ }
+ if (num_rows > num_rows_8) {
+ rms_norm_rows_f16_f16((const uint8_t *) src + (size_t) num_rows_8 * src0_row_size_aligned,
+ (uint8_t *) dst + (size_t) num_rows_8 * dst_row_size_aligned,
+ num_rows - num_rows_8, ne0, uctx);
+ }
+}
+
+static __attribute__((noinline)) void rms_norm_rows_f16_f16_ne256(const void * restrict src,
+ void * restrict dst,
+ const uint32_t num_rows,
+ const uint32_t ne0,
+ const struct htp_unary_context * uctx) {
+ htp_unary_op_preamble;
+ float epsilon = 0.f;
+ float scale = 0.f;
+ memcpy(&epsilon, &op_params[0], sizeof(float));
+ memcpy(&scale, &op_params[1], sizeof(float));
+
+ const uint32_t num_rows_4 = num_rows & ~3u;
+ if (num_rows_4 > 0) {
+ const HVX_Vector eps_v = hvx_vec_splat_f32(epsilon);
+ const HVX_Vector scale_param_v = hvx_vec_splat_f32((scale != 0.0f) ? scale : 1.0f);
+ rms_norm_256_4rows_f16_nomul((const uint8_t *) src, src0_row_size_aligned,
+ (uint8_t *) dst, dst_row_size_aligned,
+ num_rows_4, eps_v, scale_param_v,
+ Q6_V_vzero(), Q6_V_vzero(), Q6_V_vzero(), Q6_V_vzero());
+ }
+ if (num_rows > num_rows_4) {
+ rms_norm_rows_f16_f16((const uint8_t *) src + (size_t) num_rows_4 * src0_row_size_aligned,
+ (uint8_t *) dst + (size_t) num_rows_4 * dst_row_size_aligned,
+ num_rows - num_rows_4, ne0, uctx);
+ }
+}
+
+static __attribute__((noinline)) void rms_norm_mul_rows_f16_f16_f16_ne128_bc(const void * restrict src,
+ const void * restrict mul,
+ void * restrict dst,
+ const uint32_t num_rows,
+ const uint32_t src1_row_start,
+ const struct htp_unary_context * uctx) {
+ htp_unary_op_preamble;
+ float epsilon = 0.f;
+ float scale = 0.f;
+ memcpy(&epsilon, &op_params[0], sizeof(float));
+ memcpy(&scale, &op_params[1], sizeof(float));
+
+ const uint32_t num_rows_8 = num_rows & ~7u;
+ if (num_rows_8 > 0) {
+ const HVX_Vector * restrict v_w = (const HVX_Vector *) mul;
+ const HVX_Vector eps_v = hvx_vec_splat_f32(epsilon);
+ const HVX_Vector scale_param_v = hvx_vec_splat_f32((scale != 0.0f) ? scale : 1.0f);
+ rms_norm_128_8rows_f16_mul((const uint8_t *) src, src0_row_size_aligned,
+ (uint8_t *) dst, dst_row_size_aligned,
+ num_rows_8, eps_v, scale_param_v,
+ v_w[0], v_w[1]);
+ }
+ if (num_rows > num_rows_8) {
+ rms_norm_mul_rows_f16_f16_f16((const uint8_t *) src + (size_t) num_rows_8 * src0_row_size_aligned,
+ mul,
+ (uint8_t *) dst + (size_t) num_rows_8 * dst_row_size_aligned,
+ num_rows - num_rows_8, src1_row_start, uctx);
+ }
+}
+
+static __attribute__((noinline)) void rms_norm_mul_rows_f16_f16_f16_ne256_bc(const void * restrict src,
+ const void * restrict mul,
+ void * restrict dst,
+ const uint32_t num_rows,
+ const uint32_t src1_row_start,
+ const struct htp_unary_context * uctx) {
+ htp_unary_op_preamble;
+ float epsilon = 0.f;
+ float scale = 0.f;
+ memcpy(&epsilon, &op_params[0], sizeof(float));
+ memcpy(&scale, &op_params[1], sizeof(float));
+
+ const uint32_t num_rows_4 = num_rows & ~3u;
+ if (num_rows_4 > 0) {
+ const HVX_Vector * restrict v_w = (const HVX_Vector *) mul;
+ const HVX_Vector eps_v = hvx_vec_splat_f32(epsilon);
+ const HVX_Vector scale_param_v = hvx_vec_splat_f32((scale != 0.0f) ? scale : 1.0f);
+ rms_norm_256_4rows_f16_mul((const uint8_t *) src, src0_row_size_aligned,
+ (uint8_t *) dst, dst_row_size_aligned,
+ num_rows_4, eps_v, scale_param_v,
+ v_w[0], v_w[1], v_w[2], v_w[3]);
+ }
+ if (num_rows > num_rows_4) {
+ rms_norm_mul_rows_f16_f16_f16((const uint8_t *) src + (size_t) num_rows_4 * src0_row_size_aligned,
+ mul,
+ (uint8_t *) dst + (size_t) num_rows_4 * dst_row_size_aligned,
+ num_rows - num_rows_4, src1_row_start, uctx);
+ }
+}
+
static void rms_norm_mul_rows_f16_f32_f16_ne128_bc(const void * restrict src,
const void * restrict mul,
void * restrict dst,
@@ -1542,8 +1848,26 @@ static void rms_norm_mul_rows_f16_f32_f16_ne128_bc(const void * restrict src,
float scale = 0.f;
memcpy(&epsilon, &op_params[0], sizeof(float));
memcpy(&scale, &op_params[1], sizeof(float));
+ uint32_t rem = num_rows;
+ const uint32_t num_rows_8 = num_rows & ~7u;
+ if (num_rows_8 > 0) {
+ const HVX_Vector * restrict v_w = (const HVX_Vector *) mul;
+ const HVX_Vector wh0 = hvx_vec_f32_to_f16_clamped(v_w[0], v_w[1]);
+ const HVX_Vector wh1 = hvx_vec_f32_to_f16_clamped(v_w[2], v_w[3]);
+ const HVX_Vector eps_v = hvx_vec_splat_f32(epsilon);
+ const HVX_Vector scale_param_v = hvx_vec_splat_f32((scale != 0.0f) ? scale : 1.0f);
+ rms_norm_128_8rows_f16_mul((const uint8_t *) src, uctx->src0_row_size_aligned,
+ (uint8_t *) dst, uctx->dst_row_size_aligned,
+ num_rows_8, eps_v, scale_param_v, wh0, wh1);
+ rem -= num_rows_8;
+ if (rem == 0) {
+ return;
+ }
+ src = (const uint8_t *) src + (size_t) num_rows_8 * uctx->src0_row_size_aligned;
+ dst = (uint8_t *) dst + (size_t) num_rows_8 * uctx->dst_row_size_aligned;
+ }
hvx_rms_norm_mul_f16_w32_ne128_bcast_rows((const uint8_t *) src, (const uint8_t *) mul, (uint8_t *) dst,
- num_rows, uctx->src0_row_size_aligned, uctx->dst_row_size_aligned,
+ rem, uctx->src0_row_size_aligned, uctx->dst_row_size_aligned,
epsilon, scale);
}
@@ -1559,8 +1883,28 @@ static void rms_norm_mul_rows_f16_f32_f16_ne256_bc(const void * restrict src,
float scale = 0.f;
memcpy(&epsilon, &op_params[0], sizeof(float));
memcpy(&scale, &op_params[1], sizeof(float));
+ uint32_t rem = num_rows;
+ const uint32_t num_rows_4 = num_rows & ~3u;
+ if (num_rows_4 > 0) {
+ const HVX_Vector * restrict v_w = (const HVX_Vector *) mul;
+ const HVX_Vector wh0 = hvx_vec_f32_to_f16_clamped(v_w[0], v_w[1]);
+ const HVX_Vector wh1 = hvx_vec_f32_to_f16_clamped(v_w[2], v_w[3]);
+ const HVX_Vector wh2 = hvx_vec_f32_to_f16_clamped(v_w[4], v_w[5]);
+ const HVX_Vector wh3 = hvx_vec_f32_to_f16_clamped(v_w[6], v_w[7]);
+ const HVX_Vector eps_v = hvx_vec_splat_f32(epsilon);
+ const HVX_Vector scale_param_v = hvx_vec_splat_f32((scale != 0.0f) ? scale : 1.0f);
+ rms_norm_256_4rows_f16_mul((const uint8_t *) src, uctx->src0_row_size_aligned,
+ (uint8_t *) dst, uctx->dst_row_size_aligned,
+ num_rows_4, eps_v, scale_param_v, wh0, wh1, wh2, wh3);
+ rem -= num_rows_4;
+ if (rem == 0) {
+ return;
+ }
+ src = (const uint8_t *) src + (size_t) num_rows_4 * uctx->src0_row_size_aligned;
+ dst = (uint8_t *) dst + (size_t) num_rows_4 * uctx->dst_row_size_aligned;
+ }
hvx_rms_norm_mul_f16_w32_ne256_bcast_rows((const uint8_t *) src, (const uint8_t *) mul, (uint8_t *) dst,
- num_rows, uctx->src0_row_size_aligned, uctx->dst_row_size_aligned,
+ rem, uctx->src0_row_size_aligned, uctx->dst_row_size_aligned,
epsilon, scale);
}
@@ -1598,6 +1942,254 @@ static const unary_rms_norm_mul_add_compute_fn_t rms_norm_mul_add_rows_table[2][
},
};
+#define DEFINE_HVX_SIG_GLU_F16_INPLACE(NAME, ARG_F16, MUL_X) \
+static __attribute__((noinline)) void hvx_##NAME##_f16_inplace_vec(HVX_Vector * restrict vdst, \
+ const HVX_Vector * restrict vz, \
+ uint32_t nvec) { \
+ _Pragma("clang loop pipeline(disable) unroll(disable)") \
+ for (uint32_t i = 0; i + 1 < nvec; i += 2) { \
+ HVX_Vector x0 = vz[i + 0]; \
+ HVX_Vector x1 = vz[i + 1]; \
+ HVX_VectorPair sigp = hvx_vec_fast_sigmoid_f16_pair(ARG_F16(x0), ARG_F16(x1)); \
+ if (MUL_X) { \
+ vdst[i + 0] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(x0, Q6_V_lo_W(sigp)), \
+ vdst[i + 0])); \
+ vdst[i + 1] = Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(x1, Q6_V_hi_W(sigp)), \
+ vdst[i + 1])); \
+ } else { \
+ vdst[i + 0] = hvx_vec_mul_f16_f16(Q6_V_lo_W(sigp), vdst[i + 0]); \
+ vdst[i + 1] = hvx_vec_mul_f16_f16(Q6_V_hi_W(sigp), vdst[i + 1]); \
+ } \
+ } \
+} \
+ \
+static __attribute__((noinline)) void hvx_##NAME##_f16_inplace_tail(HVX_Vector * restrict vdst, \
+ const HVX_Vector * restrict vz, \
+ uint32_t nloe) { \
+ HVX_Vector x = vz[0]; \
+ HVX_Vector sig = hvx_vec_fast_sigmoid_f16(ARG_F16(x)); \
+ HVX_Vector res = MUL_X \
+ ? Q6_Vhf_equals_Vqf16(Q6_Vqf16_vmpy_Vqf16Vhf(Q6_Vqf16_vmpy_VhfVhf(x, sig), vdst[0])) \
+ : hvx_vec_mul_f16_f16(sig, vdst[0]); \
+ hvx_vec_store_a((void *) vdst, nloe * sizeof(_Float16), res); \
+}
+
+DEFINE_HVX_SIG_GLU_F16_INPLACE(swiglu, HVX_GLU_ARG_SWIGLU_F16, true)
+DEFINE_HVX_SIG_GLU_F16_INPLACE(sigglu, HVX_GLU_ARG_SWIGLU_F16, false)
+DEFINE_HVX_SIG_GLU_F16_INPLACE(geglu, HVX_GLU_ARG_GEGLU_F16, true)
+DEFINE_HVX_SIG_GLU_F16_INPLACE(geglu_quick, HVX_GLU_ARG_GEGLU_QUICK_F16, true)
+#undef DEFINE_HVX_SIG_GLU_F16_INPLACE
+
+static __attribute__((noinline)) void hvx_reglu_f16_inplace_vec(HVX_Vector * restrict vdst,
+ const HVX_Vector * restrict vz,
+ uint32_t nvec) {
+ const HVX_Vector v_zero = Q6_V_vzero();
+ for (uint32_t i = 0; i < nvec; i++) {
+ vdst[i] = hvx_vec_mul_f16_f16(Q6_Vhf_vmax_VhfVhf(vz[i], v_zero), vdst[i]);
+ }
+}
+
+static __attribute__((noinline)) void hvx_reglu_f16_inplace_tail(HVX_Vector * restrict vdst,
+ const HVX_Vector * restrict vz,
+ uint32_t nloe) {
+ HVX_Vector res = hvx_vec_mul_f16_f16(Q6_Vhf_vmax_VhfVhf(vz[0], Q6_V_vzero()), vdst[0]);
+ hvx_vec_store_a((void *) vdst, nloe * sizeof(_Float16), res);
+}
+
+static inline void hvx_glu_f16_inplace_aa(uint8_t * restrict dst, const uint8_t * restrict z, uint32_t n, uint32_t glu_op) {
+ HVX_Vector * restrict vdst = (HVX_Vector *) dst;
+ const HVX_Vector * restrict vz = (const HVX_Vector *) z;
+
+ const uint32_t epv = 128 / sizeof(_Float16);
+ const uint32_t nvec = n / epv;
+ const uint32_t nloe = n % epv;
+
+ if (glu_op == HTP_OP_GLU_REGLU) {
+ if (nvec > 0) {
+ hvx_reglu_f16_inplace_vec(vdst, vz, nvec);
+ }
+ if (nloe) {
+ hvx_reglu_f16_inplace_tail(vdst + nvec, vz + nvec, nloe);
+ }
+ return;
+ }
+
+ void (*vec_fn)(HVX_Vector * restrict, const HVX_Vector * restrict, uint32_t) = hvx_swiglu_f16_inplace_vec;
+ void (*tail_fn)(HVX_Vector * restrict, const HVX_Vector * restrict, uint32_t) = hvx_swiglu_f16_inplace_tail;
+ switch (glu_op) {
+ case HTP_OP_GLU_SIGMOID:
+ vec_fn = hvx_sigglu_f16_inplace_vec;
+ tail_fn = hvx_sigglu_f16_inplace_tail;
+ break;
+ case HTP_OP_GLU_GEGLU:
+ vec_fn = hvx_geglu_f16_inplace_vec;
+ tail_fn = hvx_geglu_f16_inplace_tail;
+ break;
+ case HTP_OP_GLU_GEGLU_QUICK:
+ vec_fn = hvx_geglu_quick_f16_inplace_vec;
+ tail_fn = hvx_geglu_quick_f16_inplace_tail;
+ break;
+ default:
+ break;
+ }
+
+ uint32_t i = 0;
+ if (nvec >= 2) {
+ const uint32_t nvec2 = nvec & ~1u;
+ vec_fn(vdst, vz, nvec2);
+ i = nvec2;
+ }
+ if (i < nvec) {
+ tail_fn(vdst + i, vz + i, epv);
+ }
+ if (nloe) {
+ tail_fn(vdst + nvec, vz + nvec, nloe);
+ }
+}
+
+static inline HVX_Vector hvx_glu_act_f32(HVX_Vector z, uint32_t glu_op,
+ HVX_Vector v_one, HVX_Vector v_max_exp, HVX_Vector v_min_exp) {
+ if (glu_op == HTP_OP_GLU_REGLU) {
+ return Q6_Vsf_vmax_VsfVsf(z, Q6_V_vzero());
+ }
+ HVX_Vector arg = z;
+ if (glu_op == HTP_OP_GLU_GEGLU) {
+ arg = HVX_GLU_ARG_GEGLU_F32(z);
+ } else if (glu_op == HTP_OP_GLU_GEGLU_QUICK) {
+ arg = HVX_GLU_ARG_GEGLU_QUICK_F32(z);
+ }
+ HVX_Vector sig = hvx_vec_fast_sigmoid_f32_guard(arg, v_one, v_max_exp, v_min_exp);
+ return (glu_op == HTP_OP_GLU_SIGMOID) ? sig : hvx_vec_mul_f32_f32(z, sig);
+}
+
+static __attribute__((noinline)) void hvx_glu_f32_inplace_row(uint8_t * restrict dst, const uint8_t * restrict z, uint32_t n, uint32_t glu_op) {
+ HVX_Vector * restrict vdst = (HVX_Vector *) dst;
+ const HVX_Vector * restrict vz = (const HVX_Vector *) z;
+ const HVX_Vector v_one = hvx_vec_splat_f32(1.0f);
+ const HVX_Vector v_max_exp = hvx_vec_splat_f32(87.0f);
+ const HVX_Vector v_min_exp = hvx_vec_splat_f32(-87.0f);
+ const uint32_t nvec = n / 32;
+ const uint32_t nloe = n % 32;
+
+ _Pragma("clang loop unroll(disable)")
+ for (uint32_t i = 0; i < nvec; i++) {
+ HVX_Vector act = hvx_glu_act_f32(vz[i], glu_op, v_one, v_max_exp, v_min_exp);
+ vdst[i] = hvx_vec_mul_f32_f32(act, vdst[i]);
+ }
+ if (nloe) {
+ HVX_Vector act = hvx_glu_act_f32(vz[nvec], glu_op, v_one, v_max_exp, v_min_exp);
+ HVX_Vector res = hvx_vec_mul_f32_f32(act, vdst[nvec]);
+ hvx_vec_store_a((void *) (vdst + nvec), nloe * sizeof(float), res);
+ }
+}
+
+static __attribute__((noinline)) void hvx_glu_f16z_f32d_inplace_row(uint8_t * restrict dst, const uint8_t * restrict z, uint32_t n, uint32_t glu_op) {
+ HVX_Vector * restrict vdst = (HVX_Vector *) dst;
+ const HVX_Vector * restrict vz = (const HVX_Vector *) z;
+ const HVX_Vector v_one = hvx_vec_splat_f32(1.0f);
+ const HVX_Vector v_max_exp = hvx_vec_splat_f32(87.0f);
+ const HVX_Vector v_min_exp = hvx_vec_splat_f32(-87.0f);
+ const uint32_t nvec = n / 64;
+ const uint32_t rem = n % 64;
+
+ _Pragma("clang loop unroll(disable)")
+ for (uint32_t i = 0; i < nvec; i++) {
+ HVX_VectorPair zp = hvx_vec_f16_to_f32(vz[i]);
+ HVX_Vector a0 = hvx_glu_act_f32(Q6_V_lo_W(zp), glu_op, v_one, v_max_exp, v_min_exp);
+ HVX_Vector a1 = hvx_glu_act_f32(Q6_V_hi_W(zp), glu_op, v_one, v_max_exp, v_min_exp);
+ vdst[2 * i + 0] = hvx_vec_mul_f32_f32(a0, vdst[2 * i + 0]);
+ vdst[2 * i + 1] = hvx_vec_mul_f32_f32(a1, vdst[2 * i + 1]);
+ }
+ if (rem) {
+ HVX_VectorPair zp = hvx_vec_f16_to_f32(vz[nvec]);
+ HVX_Vector a0 = hvx_glu_act_f32(Q6_V_lo_W(zp), glu_op, v_one, v_max_exp, v_min_exp);
+ HVX_Vector r0 = hvx_vec_mul_f32_f32(a0, vdst[2 * nvec + 0]);
+ hvx_vec_store_a((void *) (vdst + 2 * nvec + 0), MIN(rem, 32u) * sizeof(float), r0);
+ if (rem > 32u) {
+ HVX_Vector a1 = hvx_glu_act_f32(Q6_V_hi_W(zp), glu_op, v_one, v_max_exp, v_min_exp);
+ HVX_Vector r1 = hvx_vec_mul_f32_f32(a1, vdst[2 * nvec + 1]);
+ hvx_vec_store_a((void *) (vdst + 2 * nvec + 1), (rem - 32u) * sizeof(float), r1);
+ }
+ }
+}
+
+static __attribute__((noinline)) void hvx_glu_f32z_f16d_inplace_row(uint8_t * restrict dst, const uint8_t * restrict z, uint32_t n, uint32_t glu_op) {
+ HVX_Vector * restrict vdst = (HVX_Vector *) dst;
+ const HVX_Vector * restrict vz = (const HVX_Vector *) z;
+ const HVX_Vector v_one = hvx_vec_splat_f32(1.0f);
+ const HVX_Vector v_max_exp = hvx_vec_splat_f32(87.0f);
+ const HVX_Vector v_min_exp = hvx_vec_splat_f32(-87.0f);
+ const uint32_t nvec = n / 64;
+ const uint32_t rem = n % 64;
+
+ _Pragma("clang loop unroll(disable)")
+ for (uint32_t i = 0; i < nvec; i++) {
+ HVX_Vector a0 = hvx_glu_act_f32(vz[2 * i + 0], glu_op, v_one, v_max_exp, v_min_exp);
+ HVX_Vector a1 = hvx_glu_act_f32(vz[2 * i + 1], glu_op, v_one, v_max_exp, v_min_exp);
+ vdst[i] = hvx_vec_mul_f16_f16(hvx_vec_f32_to_f16_clamped(a0, a1), vdst[i]);
+ }
+ if (rem) {
+ HVX_Vector z1 = (rem > 32u) ? vz[2 * nvec + 1] : Q6_V_vzero();
+ HVX_Vector a0 = hvx_glu_act_f32(vz[2 * nvec + 0], glu_op, v_one, v_max_exp, v_min_exp);
+ HVX_Vector a1 = hvx_glu_act_f32(z1, glu_op, v_one, v_max_exp, v_min_exp);
+ HVX_Vector res = hvx_vec_mul_f16_f16(hvx_vec_f32_to_f16_clamped(a0, a1), vdst[nvec]);
+ hvx_vec_store_a((void *) (vdst + nvec), rem * sizeof(_Float16), res);
+ }
+}
+
+static void rms_norm_mul_glu_rows(const void * restrict src,
+ const void * restrict mul,
+ const void * restrict z,
+ void * restrict dst,
+ const uint32_t num_rows,
+ const uint32_t src1_row_start,
+ const struct htp_unary_context * uctx) {
+ const uint32_t nc = uctx->nc;
+ const uint32_t glu_op = uctx->kparams->glu_op;
+ unary_rms_norm_mul_compute_fn_t rms_fn = rms_norm_mul_rows_table[uctx->src0_f16][uctx->src1_f16][uctx->dst_f16];
+ if (uctx->broadcast_src1 && uctx->src1_nrows == 1 && uctx->src0_f16 && uctx->dst_f16) {
+ if (!uctx->src1_f16) {
+ if (nc == 128) {
+ rms_fn = rms_norm_mul_rows_f16_f32_f16_ne128_bc;
+ } else if (nc == 256) {
+ rms_fn = rms_norm_mul_rows_f16_f32_f16_ne256_bc;
+ }
+ } else {
+ if (nc == 128) {
+ rms_fn = rms_norm_mul_rows_f16_f16_f16_ne128_bc;
+ } else if (nc == 256) {
+ rms_fn = rms_norm_mul_rows_f16_f16_f16_ne256_bc;
+ }
+ }
+ }
+
+ rms_fn(src, mul, dst, num_rows, src1_row_start, uctx);
+
+ if (uctx->src2_f16 && uctx->dst_f16) {
+ if ((size_t) nc * sizeof(_Float16) == uctx->dst_row_size_aligned) {
+ hvx_glu_f16_inplace_aa((uint8_t *) dst, (const uint8_t *) z, num_rows * nc, glu_op);
+ return;
+ }
+ for (uint32_t ib = 0; ib < num_rows; ib++) {
+ hvx_glu_f16_inplace_aa((uint8_t *) dst + ib * uctx->dst_row_size_aligned,
+ (const uint8_t *) z + ib * uctx->src2_row_size_aligned, nc, glu_op);
+ }
+ return;
+ }
+
+ for (uint32_t ib = 0; ib < num_rows; ib++) {
+ uint8_t * dst_ptr = (uint8_t *) dst + ib * uctx->dst_row_size_aligned;
+ const uint8_t * z_ptr = (const uint8_t *) z + ib * uctx->src2_row_size_aligned;
+ if (!uctx->src2_f16 && !uctx->dst_f16) {
+ hvx_glu_f32_inplace_row(dst_ptr, z_ptr, nc, glu_op);
+ } else if (uctx->src2_f16) {
+ hvx_glu_f16z_f32d_inplace_row(dst_ptr, z_ptr, nc, glu_op);
+ } else {
+ hvx_glu_f32z_f16d_inplace_row(dst_ptr, z_ptr, nc, glu_op);
+ }
+ }
+}
+
static __attribute__((noinline)) void rms_norm_apply_tile(const uint8_t * restrict src,
const uint8_t * restrict mul,
const uint8_t * restrict add,
@@ -3120,8 +3712,11 @@ struct unary_row_pipe_state {
uint32_t block;
bool src0_contig;
bool src1_contig;
+ bool src2_contig;
bool block_contig;
bool src0_slice_dma;
+ bool src2_slice_dma;
+ bool block_slice_dma;
uint32_t ir;
uint32_t ir_end;
};
@@ -3131,7 +3726,7 @@ static bool unary_row_next_task(void * ctx, struct dma_pipe_task * task) {
if (s->ir >= s->ir_end) {
return false;
}
- const uint32_t block_size = unary_block_size(s->ir, s->ir_end, s->block, s->block_contig, s->src0_slice_dma,
+ const uint32_t block_size = unary_block_size(s->ir, s->ir_end, s->block, s->block_contig, s->block_slice_dma,
s->ne01, &s->uctx->kparams->div_ne01);
task->row = s->ir;
task->off = 0;
@@ -3144,10 +3739,10 @@ static bool unary_row_next_task(void * ctx, struct dma_pipe_task * task) {
static void unary_row_push_in(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in_spad) {
struct unary_row_pipe_state * s = (struct unary_row_pipe_state *) ctx;
const struct htp_unary_context * uctx = s->uctx;
- unary_push_src0_dma(dma_q, (uint8_t *) in_spad, s->data_src0,
- task->row, task->len, s->src0_contig, s->src0_slice_dma, s->ne01, s->ne02,
- &uctx->kparams->div_ne01, &uctx->kparams->div_ne02, &uctx->kparams->div_ne012,
- s->nb01, s->nb02, s->nb03, uctx->src0_row_size_aligned, uctx->src0_data_row_size);
+ unary_push_src_dma(dma_q, (uint8_t *) in_spad, s->data_src0,
+ task->row, task->len, s->src0_contig, s->src0_slice_dma, s->ne01, s->ne02,
+ &uctx->kparams->div_ne01, &uctx->kparams->div_ne02, &uctx->kparams->div_ne012,
+ s->nb01, s->nb02, s->nb03, uctx->src0_row_size_aligned, uctx->src0_data_row_size);
}
static void unary_row_push_out(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * out_spad) {
@@ -3203,15 +3798,14 @@ static void unary_rms_norm_mul_add_push_in(dma_queue * dma_q, void * ctx, const
const struct fastdiv_values * div_ne02 = &uctx->kparams->div_ne02;
const struct fastdiv_values * div_ne012 = &uctx->kparams->div_ne012;
- const size_t src0_off = s->src0_contig ? ((size_t) task->row * s->nb01) :
- unary_row_offset(task->row, s->ne01, s->ne02, div_ne01, div_ne02, div_ne012, s->nb01, s->nb02, s->nb03);
- const size_t src2_off = s->src0_contig ? ((size_t) task->row * s->nb21) :
- unary_row_offset(task->row, s->ne01, s->ne02, div_ne01, div_ne02, div_ne012, s->nb21, s->nb22, s->nb23);
-
- dma_queue_push(dma_q, dma_make_data(in0_spad, s->data_src0 + src0_off),
- uctx->src0_row_size_aligned, s->nb01, uctx->src0_data_row_size, task->len);
- dma_queue_push(dma_q, dma_make_data(in2_spad, s->data_src2 + src2_off),
- uctx->src2_row_size_aligned, s->nb21, uctx->src2_data_row_size, task->len);
+ unary_push_src_dma(dma_q, (uint8_t *) in0_spad, s->data_src0,
+ task->row, task->len, s->src0_contig, s->src0_slice_dma, s->ne01, s->ne02,
+ div_ne01, div_ne02, div_ne012,
+ s->nb01, s->nb02, s->nb03, uctx->src0_row_size_aligned, uctx->src0_data_row_size);
+ unary_push_src_dma(dma_q, (uint8_t *) in2_spad, s->data_src2,
+ task->row, task->len, s->src2_contig, s->src2_slice_dma, s->ne01, s->ne02,
+ div_ne01, div_ne02, div_ne012,
+ s->nb21, s->nb22, s->nb23, uctx->src2_row_size_aligned, uctx->src2_data_row_size);
}
static void unary_rms_norm_mul_add_compute(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in0_spad, void * in2_spad) {
@@ -3250,11 +3844,12 @@ static void unary_thread_row_block(const htp_thread_t * th, unsigned int nth, un
.nb01 = nb01, .nb02 = nb02, .nb03 = nb03,
.nb1 = nb1,
.block = uctx->block,
- .src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
- .block_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
- .src0_slice_dma = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_SLICE_DMA) != 0,
- .ir = src0_start_row,
- .ir_end = src0_end_row,
+ .src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
+ .block_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
+ .src0_slice_dma = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_SLICE_DMA) != 0,
+ .block_slice_dma = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_SLICE_DMA) != 0,
+ .ir = src0_start_row,
+ .ir_end = src0_end_row,
};
dma_pipe_io_run(th->dma_q, th->trace, &s, in_buf, out_buf,
@@ -3297,11 +3892,12 @@ static void unary_thread_rms_norm_mul_bc(const htp_thread_t * th, unsigned int n
.nb01 = nb01, .nb02 = nb02, .nb03 = nb03,
.nb1 = nb1,
.block = uctx->block,
- .src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
- .block_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
- .src0_slice_dma = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_SLICE_DMA) != 0,
- .ir = src0_start_row,
- .ir_end = src0_end_row,
+ .src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
+ .block_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
+ .src0_slice_dma = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_SLICE_DMA) != 0,
+ .block_slice_dma = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_SLICE_DMA) != 0,
+ .ir = src0_start_row,
+ .ir_end = src0_end_row,
};
dma_pipe_io_run(dma_q, th->trace, &s, in_buf, out_buf,
@@ -3341,12 +3937,13 @@ static void unary_thread_rms_norm_mul(const htp_thread_t * th, unsigned int nth,
.nb11 = src1->nb[1], .nb12 = src1->nb[2], .nb13 = src1->nb[3],
.nb1 = nb1,
.block = uctx->block,
- .src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
- .src1_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC1_CONTIG) != 0,
- .block_contig = (uctx->kparams->flags & (HTP_UNARY_FLAG_SRC0_CONTIG | HTP_UNARY_FLAG_SRC1_CONTIG)) == (HTP_UNARY_FLAG_SRC0_CONTIG | HTP_UNARY_FLAG_SRC1_CONTIG),
- .src0_slice_dma = false,
- .ir = src0_start_row,
- .ir_end = src0_end_row,
+ .src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
+ .src1_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC1_CONTIG) != 0,
+ .block_contig = (uctx->kparams->flags & (HTP_UNARY_FLAG_SRC0_CONTIG | HTP_UNARY_FLAG_SRC1_CONTIG)) == (HTP_UNARY_FLAG_SRC0_CONTIG | HTP_UNARY_FLAG_SRC1_CONTIG),
+ .src0_slice_dma = false,
+ .block_slice_dma = false,
+ .ir = src0_start_row,
+ .ir_end = src0_end_row,
};
dma_pipe_io2_run(th->dma_q, th->trace, &s, in0_buf, in1_buf, out_buf,
@@ -3383,22 +3980,31 @@ static void unary_thread_rms_norm_mul_add(const htp_thread_t * th, unsigned int
uctx->src1_row_size_aligned, src1->nb[1], uctx->src1_data_row_size, uctx->src1_nrows);
dma_queue_flush(dma_q);
+ const bool src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0;
+ const bool src2_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC2_CONTIG) != 0;
+ const bool src0_slice_dma = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_SLICE_DMA) != 0;
+ const bool src2_slice_dma = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC2_SLICE_DMA) != 0;
+ const bool block_contig = src0_contig && src2_contig;
+
struct unary_row_pipe_state s = {
- .uctx = uctx,
- .data_src0 = uctx->data_src0,
- .data_src2 = uctx->data_src2,
- .data_dst = uctx->data_dst,
- .src1_vtcm_bc = src1_vtcm_data,
+ .uctx = uctx,
+ .data_src0 = uctx->data_src0,
+ .data_src2 = uctx->data_src2,
+ .data_dst = uctx->data_dst,
+ .src1_vtcm_bc = src1_vtcm_data,
.ne0 = ne0, .ne01 = ne01, .ne02 = ne02,
.nb01 = nb01, .nb02 = nb02, .nb03 = nb03,
.nb21 = src2->nb[1], .nb22 = src2->nb[2], .nb23 = src2->nb[3],
- .nb1 = nb1,
- .block = uctx->block,
- .src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
- .block_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0,
- .src0_slice_dma = false,
- .ir = src0_start_row,
- .ir_end = src0_end_row,
+ .nb1 = nb1,
+ .block = uctx->block,
+ .src0_contig = src0_contig,
+ .src2_contig = src2_contig,
+ .block_contig = block_contig,
+ .src0_slice_dma = src0_slice_dma,
+ .src2_slice_dma = src2_slice_dma,
+ .block_slice_dma = !block_contig && (src0_contig || src0_slice_dma) && (src2_contig || src2_slice_dma),
+ .ir = src0_start_row,
+ .ir_end = src0_end_row,
};
dma_pipe_io2_run(dma_q, th->trace, &s, in0_buf, in2_buf, out_buf,
@@ -3465,6 +4071,7 @@ static void unary_thread_tiled_rms_norm(const htp_thread_t * th, unsigned int nt
const uint32_t tiles_per_row = uctx->kparams->tiles_per_row;
const bool src0_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC0_CONTIG) != 0;
const bool src1_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC1_CONTIG) != 0;
+ const bool src2_contig = (uctx->kparams->flags & HTP_UNARY_FLAG_SRC2_CONTIG) != 0;
float epsilon = 0.f;
float scale = 0.f;
@@ -3484,7 +4091,7 @@ static void unary_thread_tiled_rms_norm(const htp_thread_t * th, unsigned int nt
(src1_contig ? ((size_t) row * nb11) :
unary_row_offset(row, ne01, ne02, div_ne01, div_ne02, div_ne012, nb11, nb12, nb13)));
const size_t src2_row_off = !has_src2_add ? 0 :
- (src0_contig ? ((size_t) row * nb21) :
+ (src2_contig ? ((size_t) row * nb21) :
unary_row_offset(row, ne01, ne02, div_ne01, div_ne02, div_ne012, nb21, nb22, nb23));
const size_t dst_row_off = (size_t) row * nb1;
@@ -3620,11 +4227,12 @@ static void unary_thread_tri_f32(const htp_thread_t * th, unsigned int nth, unsi
.nb01 = nb01, .nb02 = nb02, .nb03 = nb03,
.nb1 = nb1,
.block = uctx->block,
- .src0_contig = true,
- .block_contig = true,
- .src0_slice_dma = false,
- .ir = src0_start_row,
- .ir_end = src0_end_row,
+ .src0_contig = true,
+ .block_contig = true,
+ .src0_slice_dma = false,
+ .block_slice_dma = false,
+ .ir = src0_start_row,
+ .ir_end = src0_end_row,
};
dma_pipe_io_run(th->dma_q, th->trace, &s, in_buf, out_buf,
@@ -3805,6 +4413,7 @@ static int execute_op_unary(struct htp_ops_context * octx) {
case HTP_OP_RMS_NORM: op_type = is_f16 ? "rmsnorm-f16" : "rmsnorm-f32"; break;
case HTP_OP_RMS_NORM_MUL: op_type = is_f16 ? "rmsnorm-mul-f16" : "rmsnorm-mul-f32"; break;
case HTP_OP_RMS_NORM_MUL_ADD: op_type = is_f16 ? "rmsnorm-mul-add-f16" : "rmsnorm-mul-add-f32"; break;
+ case HTP_OP_RMS_NORM_MUL_GLU: op_type = is_f16 ? "rmsnorm-mul-glu-f16" : "rmsnorm-mul-glu-f32"; break;
case HTP_OP_SCALE: op_type = is_f16 ? "scale-f16" : "scale-f32"; break;
case HTP_OP_CLAMP: op_type = is_f16 ? "clamp-f16" : "clamp-f32"; break;
case HTP_OP_LEAKY_RELU: op_type = is_f16 ? "leaky-relu-f16" : "leaky-relu-f32"; break;
@@ -3839,22 +4448,22 @@ static int execute_op_unary(struct htp_ops_context * octx) {
return HTP_STATUS_INVAL_PARAMS;
}
- const bool has_src1_mul = (octx->op == HTP_OP_RMS_NORM_MUL || octx->op == HTP_OP_RMS_NORM_MUL_ADD);
- const bool has_src2_add = (octx->op == HTP_OP_RMS_NORM_MUL_ADD);
+ const bool has_src1_mul = (octx->op == HTP_OP_RMS_NORM_MUL || octx->op == HTP_OP_RMS_NORM_MUL_ADD || octx->op == HTP_OP_RMS_NORM_MUL_GLU);
+ const bool has_src2 = (octx->op == HTP_OP_RMS_NORM_MUL_ADD || octx->op == HTP_OP_RMS_NORM_MUL_GLU);
const struct htp_tensor * src1 = has_src1_mul ? octx->src[1] : NULL;
- const struct htp_tensor * src2 = has_src2_add ? octx->src[2] : NULL;
+ const struct htp_tensor * src2 = has_src2 ? octx->src[2] : NULL;
const size_t src0_elem_size = is_f16 ? sizeof(_Float16) : sizeof(float);
const size_t dst_elem_size = (dst->type == HTP_TYPE_F16) ? sizeof(_Float16) : sizeof(float);
const size_t src1_elem_size = (has_src1_mul && src1->type == HTP_TYPE_F16) ? sizeof(_Float16) : sizeof(float);
- const size_t src2_elem_size = (has_src2_add && src2->type == HTP_TYPE_F16) ? sizeof(_Float16) : sizeof(float);
+ const size_t src2_elem_size = (has_src2 && src2->type == HTP_TYPE_F16) ? sizeof(_Float16) : sizeof(float);
const uint32_t src0_nrows = src0->ne[1] * src0->ne[2] * src0->ne[3];
const size_t src0_data_row_size = src0->ne[0] * src0_elem_size;
const size_t dst_data_row_size = dst->ne[0] * dst_elem_size;
const size_t src1_data_row_size = has_src1_mul ? (src1->ne[0] * src1_elem_size) : 0;
- const size_t src2_data_row_size = has_src2_add ? (src2->ne[0] * src2_elem_size) : 0;
+ const size_t src2_data_row_size = has_src2 ? (src2->ne[0] * src2_elem_size) : 0;
uint32_t row_start = 0;
uint32_t nrows = src0_nrows;
@@ -3901,7 +4510,7 @@ static int execute_op_unary(struct htp_ops_context * octx) {
.data_src0 = src0->data,
.data_src1 = has_src1_mul ? src1->data : 0,
- .data_src2 = has_src2_add ? src2->data : 0,
+ .data_src2 = has_src2 ? src2->data : 0,
.data_dst = dst->data,
.src0_data_row_size = src0_data_row_size,
@@ -3927,7 +4536,7 @@ static int execute_op_unary(struct htp_ops_context * octx) {
.broadcast_src1 = broadcast_src1,
.src0_f16 = is_f16,
.src1_f16 = has_src1_mul && (src1->type == HTP_TYPE_F16),
- .src2_f16 = has_src2_add && (src2->type == HTP_TYPE_F16),
+ .src2_f16 = has_src2 && (src2->type == HTP_TYPE_F16),
.dst_f16 = (dst->type == HTP_TYPE_F16),
.vtcm_src0 = VTCM_LAYOUT_PTR(uint8_t, base, L.off_src0),
@@ -3950,17 +4559,32 @@ static int execute_op_unary(struct htp_ops_context * octx) {
case HTP_OP_RMS_NORM:
task_func = col_tile ? unary_thread_tiled_rms_norm : unary_thread_row_block;
compute_func = (void *) rms_norm_rows_table[is_f16][uctx.dst_f16];
+ if (!col_tile && is_f16 && uctx.dst_f16) {
+ if (src0->ne[0] == 128) {
+ compute_func = (void *) rms_norm_rows_f16_f16_ne128;
+ } else if (src0->ne[0] == 256) {
+ compute_func = (void *) rms_norm_rows_f16_f16_ne256;
+ }
+ }
uctx.rms_tile_sum_sq_fn = is_f16 ? rms_norm_tile_sum_sq_f16 : rms_norm_tile_sum_sq_f32;
uctx.rms_apply_row_fn = rms_norm_apply_row_table[is_f16][uctx.dst_f16];
break;
case HTP_OP_RMS_NORM_MUL:
task_func = col_tile ? unary_thread_tiled_rms_norm : (broadcast_src1 ? unary_thread_rms_norm_mul_bc : unary_thread_rms_norm_mul);
compute_func = (void *) rms_norm_mul_rows_table[is_f16][uctx.src1_f16][uctx.dst_f16];
- if (!col_tile && broadcast_src1 && uctx.src1_nrows == 1 && is_f16 && !uctx.src1_f16 && uctx.dst_f16) {
- if (src0->ne[0] == 128) {
- compute_func = (void *) rms_norm_mul_rows_f16_f32_f16_ne128_bc;
- } else if (src0->ne[0] == 256) {
- compute_func = (void *) rms_norm_mul_rows_f16_f32_f16_ne256_bc;
+ if (!col_tile && broadcast_src1 && uctx.src1_nrows == 1 && is_f16 && uctx.dst_f16) {
+ if (!uctx.src1_f16) {
+ if (src0->ne[0] == 128) {
+ compute_func = (void *) rms_norm_mul_rows_f16_f32_f16_ne128_bc;
+ } else if (src0->ne[0] == 256) {
+ compute_func = (void *) rms_norm_mul_rows_f16_f32_f16_ne256_bc;
+ }
+ } else {
+ if (src0->ne[0] == 128) {
+ compute_func = (void *) rms_norm_mul_rows_f16_f16_f16_ne128_bc;
+ } else if (src0->ne[0] == 256) {
+ compute_func = (void *) rms_norm_mul_rows_f16_f16_f16_ne256_bc;
+ }
}
}
uctx.rms_tile_sum_sq_fn = is_f16 ? rms_norm_tile_sum_sq_f16 : rms_norm_tile_sum_sq_f32;
@@ -3972,6 +4596,15 @@ static int execute_op_unary(struct htp_ops_context * octx) {
uctx.rms_tile_sum_sq_fn = is_f16 ? rms_norm_tile_sum_sq_f16 : rms_norm_tile_sum_sq_f32;
uctx.rms_apply_row_fn = rms_norm_mul_add_apply_row_table[is_f16][uctx.src1_f16][uctx.src2_f16][uctx.dst_f16];
break;
+ case HTP_OP_RMS_NORM_MUL_GLU:
+ if (kparams->glu_op != HTP_OP_GLU_SWIGLU && kparams->glu_op != HTP_OP_GLU_SIGMOID &&
+ kparams->glu_op != HTP_OP_GLU_GEGLU && kparams->glu_op != HTP_OP_GLU_GEGLU_QUICK &&
+ kparams->glu_op != HTP_OP_GLU_REGLU) {
+ return HTP_STATUS_NO_SUPPORT;
+ }
+ task_func = unary_thread_rms_norm_mul_add;
+ compute_func = (void *) rms_norm_mul_glu_rows;
+ break;
default:
break;
}
diff --git a/ggml/src/ggml-hexagon/htp/unary-ops.h b/ggml/src/ggml-hexagon/htp/unary-ops.h
index 0dda17b0d..a83ef25af 100644
--- a/ggml/src/ggml-hexagon/htp/unary-ops.h
+++ b/ggml/src/ggml-hexagon/htp/unary-ops.h
@@ -12,6 +12,8 @@ enum htp_unary_flags {
HTP_UNARY_FLAG_SRC1_CONTIG = (1u << 2),
HTP_UNARY_FLAG_SRC0_SLICE_DMA = (1u << 3),
HTP_UNARY_FLAG_SCALE_BIAS = (1u << 4),
+ HTP_UNARY_FLAG_SRC2_CONTIG = (1u << 5),
+ HTP_UNARY_FLAG_SRC2_SLICE_DMA = (1u << 6),
};
// Op-specific struct for precomputed unary params
@@ -23,6 +25,7 @@ struct htp_unary_kernel_params {
uint32_t vtcm_size;
uint32_t tiles_per_row;
+ uint32_t glu_op; // HTP_OP_GLU_* for HTP_OP_RMS_NORM_MUL_GLU (0 if none)
float in_scale;
float in_bias;
@@ -49,6 +52,7 @@ static inline bool htp_op_is_unary(uint32_t opcode) {
case HTP_OP_RMS_NORM:
case HTP_OP_RMS_NORM_MUL:
case HTP_OP_RMS_NORM_MUL_ADD:
+ case HTP_OP_RMS_NORM_MUL_GLU:
case HTP_OP_SCALE:
case HTP_OP_SQR:
case HTP_OP_SQRT:
@@ -85,6 +89,7 @@ static inline bool htp_unary_is_elementwise(uint32_t opcode) {
case HTP_OP_RMS_NORM:
case HTP_OP_RMS_NORM_MUL:
case HTP_OP_RMS_NORM_MUL_ADD:
+ case HTP_OP_RMS_NORM_MUL_GLU:
case HTP_OP_L2_NORM:
case HTP_OP_HADAMARD:
case HTP_OP_TRI:
@@ -129,22 +134,22 @@ static inline void htp_unary_vtcm_layout_build(
uint32_t src1_nrows,
uint32_t n_hvx
) {
- const bool has_src1_mul = (op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_RMS_NORM_MUL_ADD);
- const bool has_src2_add = (op == HTP_OP_RMS_NORM_MUL_ADD);
+ const bool has_src1_mul = (op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_RMS_NORM_MUL_ADD || op == HTP_OP_RMS_NORM_MUL_GLU);
+ const bool has_src2 = (op == HTP_OP_RMS_NORM_MUL_ADD || op == HTP_OP_RMS_NORM_MUL_GLU);
L->src0_row_size_aligned = hex_round_up(src0_data_row_size, 128);
L->dst_row_size_aligned = hex_round_up(dst_data_row_size, 128);
L->src1_row_size_aligned = has_src1_mul ? hex_round_up(src1_data_row_size, 128) : 0;
- L->src2_row_size_aligned = has_src2_add ? hex_round_up(src2_data_row_size, 128) : 0;
+ L->src2_row_size_aligned = has_src2 ? hex_round_up(src2_data_row_size, 128) : 0;
if (col_tile > 0) {
L->src0_bytes = (size_t) col_tile * src0_elem_size * 2;
L->dst_bytes = (size_t) col_tile * dst_elem_size * 2;
L->src1_bytes = has_src1_mul ? ((size_t) col_tile * src1_elem_size * 2) : 0;
- L->src2_bytes = has_src2_add ? ((size_t) col_tile * src2_elem_size * 2) : 0;
+ L->src2_bytes = has_src2 ? ((size_t) col_tile * src2_elem_size * 2) : 0;
} else {
L->src0_bytes = L->src0_row_size_aligned * block * 2;
- L->src2_bytes = has_src2_add ? (L->src2_row_size_aligned * block * 2) : 0;
+ L->src2_bytes = has_src2 ? (L->src2_row_size_aligned * block * 2) : 0;
L->dst_bytes = L->dst_row_size_aligned * block * 2;
if (has_src1_mul) {
L->src1_bytes = broadcast_src1 ? (L->src1_row_size_aligned * src1_nrows) : (L->src1_row_size_aligned * block * 2);
@@ -179,24 +184,24 @@ static inline void htp_unary_solve_layout(
uint32_t * out_col_tile,
uint32_t * out_vtcm_row_per_hvx
) {
- const bool has_src1_mul = (op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_RMS_NORM_MUL_ADD);
- const bool has_src2_add = (op == HTP_OP_RMS_NORM_MUL_ADD);
+ const bool has_src1_mul = (op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_RMS_NORM_MUL_ADD || op == HTP_OP_RMS_NORM_MUL_GLU);
+ const bool has_src2 = (op == HTP_OP_RMS_NORM_MUL_ADD || op == HTP_OP_RMS_NORM_MUL_GLU);
const size_t eff_dst_elem_size = (op == HTP_OP_HADAMARD) ? sizeof(float) : dst_elem_size;
const size_t src0_data_row_size = ne00 * src0_elem_size;
const size_t dst_data_row_size = ne10 * eff_dst_elem_size;
const size_t src1_data_row_size = has_src1_mul ? (ne11 * src1_elem_size) : 0;
- const size_t src2_data_row_size = has_src2_add ? (ne00 * src2_elem_size) : 0;
+ const size_t src2_data_row_size = has_src2 ? (ne00 * src2_elem_size) : 0;
const size_t src0_row_size_aligned = hex_round_up(src0_data_row_size, 128);
const size_t dst_row_size_aligned = hex_round_up(dst_data_row_size, 128);
const size_t src1_row_size_aligned = has_src1_mul ? hex_round_up(src1_data_row_size, 128) : 0;
- const size_t src2_row_size_aligned = has_src2_add ? hex_round_up(src2_data_row_size, 128) : 0;
+ const size_t src2_row_size_aligned = has_src2 ? hex_round_up(src2_data_row_size, 128) : 0;
size_t vtcm_size_per_row = 0;
size_t vtcm_row_per_hvx = 0;
- if (op == HTP_OP_RMS_NORM_MUL_ADD) {
+ if (has_src2) {
size_t src1_vtcm_total = n_hvx * src1_row_size_aligned * src1_nrows;
size_t available_vtcm = (vtcm_size > src1_vtcm_total) ? (vtcm_size - src1_vtcm_total) : 0;
vtcm_size_per_row = 2 * (src0_row_size_aligned + src2_row_size_aligned + dst_row_size_aligned);
@@ -225,7 +230,7 @@ static inline void htp_unary_solve_layout(
vtcm_row_per_hvx = max_block;
}
- const bool is_rms_norm = (op == HTP_OP_RMS_NORM || op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_RMS_NORM_MUL_ADD);
+ const bool is_rms_norm = (op == HTP_OP_RMS_NORM || op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_RMS_NORM_MUL_ADD || op == HTP_OP_RMS_NORM_MUL_GLU);
const bool is_reduction = (op == HTP_OP_NORM || is_rms_norm || op == HTP_OP_L2_NORM || op == HTP_OP_HADAMARD);
uint32_t col_tile = 0;
@@ -233,7 +238,7 @@ static inline void htp_unary_solve_layout(
const size_t per_hvx_budget = vtcm_size / n_hvx;
const size_t tile_elem_bytes = 2 * (src0_elem_size + eff_dst_elem_size +
(has_src1_mul ? src1_elem_size : 0) +
- (has_src2_add ? src2_elem_size : 0));
+ (has_src2 ? src2_elem_size : 0));
col_tile = (uint32_t) hex_align_down(per_hvx_budget / tile_elem_bytes, 64);
}