Commit 845648f8a for llama.cpp
commit 845648f8a0a74c018feeb2209df046b16cb7522a
Author: Max Krasnyansky <maxk@qti.qualcomm.com>
Date: Sun Dec 6 06:53:35 2026 -0800
hexagon: support for mixed quant types in fused MUL_MAT_NX (#33861)
diff --git a/ggml/src/ggml-hexagon/htp-fusion.cpp b/ggml/src/ggml-hexagon/htp-fusion.cpp
index c828f1319..15fb8f42c 100644
--- a/ggml/src/ggml-hexagon/htp-fusion.cpp
+++ b/ggml/src/ggml-hexagon/htp-fusion.cpp
@@ -283,6 +283,26 @@ static bool fuse_mul_mat_add(const ggml_hexagon_session * sess, htp_opnode & pre
return true;
}
+static bool htp_mm_nx_wtypes_compatible(
+ const struct htp_mm_kernel_params * orig_kparams,
+ ggml_type w0_type,
+ ggml_type w_in_type,
+ bool is_id) {
+ if (w_in_type == w0_type) {
+ return true;
+ }
+ if (is_id || htp_mm_hmx_lut_bytes(w0_type) != htp_mm_hmx_lut_bytes(w_in_type)) {
+ return false;
+ }
+ if (orig_kparams->n_hmx > 0) {
+ return orig_kparams->kernel_type == HTP_MM_KERNEL_HMX_W8A8_2D ||
+ orig_kparams->kernel_type == HTP_MM_KERNEL_HMX_F16_2D;
+ }
+ return (orig_kparams->kernel_type == HTP_MM_KERNEL_HVX_QUANT_BLOCK ||
+ orig_kparams->kernel_type == HTP_MM_KERNEL_HVX_QUANT_ROW) &&
+ htp_mm_get_hvx_act_format(w0_type) == htp_mm_get_hvx_act_format(w_in_type);
+}
+
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;
@@ -296,6 +316,7 @@ static bool fuse_mm_nx(const ggml_hexagon_session * sess, htp_opnode & prev, con
const auto * cur_kparams = cur.kparams<htp_mm_kernel_params>();
const bool prev_has_scale = (orig_kparams->scale_mask != 0);
+ const bool w0_has_scale = ((orig_kparams->scale_mask & 1u) != 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);
@@ -308,8 +329,9 @@ static bool fuse_mm_nx(const ggml_hexagon_session * sess, htp_opnode & prev, con
const ggml_tensor * x_in = cur.src1();
const ggml_tensor * d_in = cur.dst();
- 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) ||
+ if (x_in != x || !htp_mm_nx_wtypes_compatible(orig_kparams, w0->type, w_in->type, is_id) ||
+ w_in->ne[0] != w0->ne[0] ||
+ (w_in->type == w0->type && ggml_needs_scale(w0->type) && w0_has_scale != cur_has_scale) ||
ggml_hexagon_tensor_is_tiled(w_in) != ggml_hexagon_tensor_is_tiled(w0) ||
orig_kparams->n_hmx != cur_kparams->n_hmx) {
return false;
@@ -357,9 +379,11 @@ static bool fuse_mm_nx(const ggml_hexagon_session * sess, htp_opnode & prev, con
}
}
- const ggml_tensor * prev_w = prev.inputs[0];
- ggml_type prev_dst_type = prev.outputs[0]->type;
+ const ggml_tensor * prev_w = prev.inputs[0];
+ ggml_type prev_dst_type = prev.outputs[0]->type;
+ bool has_mixed_wtypes = (w_in->type != w0->type);
for (uint32_t i = 1; i < curr_n; ++i) {
+ if (prev.inputs[i]->type != w0->type) has_mixed_wtypes = true;
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;
@@ -389,7 +413,7 @@ static bool fuse_mm_nx(const ggml_hexagon_session * sess, htp_opnode & prev, con
target.fused.push_back(cur.node);
};
- if (prev.opcode == nx_op && w_for_solve == prev_w &&
+ if (!has_mixed_wtypes && 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;
@@ -816,12 +840,17 @@ bool htp_fusion_can_stack(const struct ggml_tensor * a, const struct ggml_tensor
}
const ggml_tensor * w0 = a->src[0];
const ggml_tensor * w1 = b->src[0];
- if (w0->type != w1->type || w0->ne[0] != w1->ne[0] || w0->ne[2] != w1->ne[2]) {
+ if (w0->ne[0] != w1->ne[0] || w0->ne[2] != w1->ne[2]) {
return false;
}
+ if (w0->type != w1->type) {
+ if (a->op != GGML_OP_MUL_MAT || htp_mm_hmx_lut_bytes(w0->type) != htp_mm_hmx_lut_bytes(w1->type)) {
+ return false;
+ }
+ }
const bool a_has_scale = ggml_get_src_scale(a, 0) != nullptr;
const bool b_has_scale = ggml_get_src_scale(b, 0) != nullptr;
- if (ggml_needs_scale(w0->type) && a_has_scale != b_has_scale) {
+ if (w0->type == w1->type && ggml_needs_scale(w0->type) && a_has_scale != b_has_scale) {
return false;
}
return ggml_hexagon_tensor_is_tiled(w0) == ggml_hexagon_tensor_is_tiled(w1);
diff --git a/ggml/src/ggml-hexagon/htp-kparams.cpp b/ggml/src/ggml-hexagon/htp-kparams.cpp
index e299c8a0e..8443bb7cc 100644
--- a/ggml/src/ggml-hexagon/htp-kparams.cpp
+++ b/ggml/src/ggml-hexagon/htp-kparams.cpp
@@ -933,6 +933,7 @@ static bool htp_kparams_solve_hmx_mm(
kparams->vtcm_bias_size = (int32_t) bias_size;
kparams->vtcm_scale_size = (int32_t) scale_size;
kparams->kernel_type = kernel_type;
+ kparams->vtcm_wtype = (uint8_t) wtype;
return true;
}
@@ -961,8 +962,9 @@ static void htp_kparams_solve_hvx_mm(
}
const uint32_t n_hvx = htp_session_n_hvx(sess);
- kparams->n_hmx = 0;
- kparams->n_hvx = n_hvx;
+ kparams->n_hmx = 0;
+ kparams->n_hvx = n_hvx;
+ kparams->vtcm_wtype = (uint8_t) wtype;
GGML_UNUSED(ne02);
GGML_UNUSED(ne03);
@@ -1393,6 +1395,7 @@ static bool htp_kparams_solve_mmnx(
kparams->vtcm_size = L.total_bytes;
kparams->n_prefetch = best_n_prefetch;
kparams->n_weights = n_weights;
+ kparams->vtcm_wtype = (uint8_t) wtype;
} else {
kparams->kernel_type = HTP_MM_KERNEL_UNSUPPORTED;
return false;
@@ -6215,36 +6218,114 @@ static bool htp_kparams_solve_mm_node(
const ggml_tensor * act = tmp.inputs[n_w];
const ggml_tensor * w_for_solve = tmp.inputs[0];
const ggml_tensor * dst_for_solve = tmp.outputs[0];
- ggml_type max_dst_type = tmp.outputs[0]->type;
- size_t hmx_scale_sz = 0;
- size_t hvx_scale_sz = 0;
+ ggml_type max_dst_type = tmp.outputs[0]->type;
+ const bool is_tiled = ggml_hexagon_tensor_will_tile(tmp.inputs[0]);
+ const int w0_eff_type = is_tiled ? htp_mm_tiled_weight_type(tmp.inputs[0]->type) : (int) tmp.inputs[0]->type;
+ const auto w0_hvx_act_fmt = htp_mm_get_hvx_act_format(tmp.inputs[0]->type);
+ const size_t w0_lut_bytes = htp_mm_hmx_lut_bytes(tmp.inputs[0]->type);
+ bool has_mixed_wtypes = false;
+ bool same_hvx_act_fmt = true;
+ bool same_lut_bytes = true;
+ ggml_type max_raw_wtype = (ggml_type) w0_eff_type;
+ size_t max_raw_bytes = is_tiled ? htp_mm_tiled_raw_bytes_per_col_tile(tmp.inputs[0]->type, (uint32_t) act->ne[0])
+ : ggml_row_size(tmp.inputs[0]->type, act->ne[0]);
+ size_t max_nb1 = tmp.inputs[0]->nb[1];
+ int64_t fwht_cols = 0;
+ int64_t no_fwht_cols = 0;
+ size_t hmx_scale_sz = 0;
+ size_t hvx_scale_sz = 0;
for (size_t i = 0; i < n_w; ++i) {
- if (!htp_matmul_check_types(tmp.inputs[i], act, tmp.outputs[i])) {
+ const ggml_tensor * wi = tmp.inputs[i];
+ if (!htp_matmul_check_types(wi, act, tmp.outputs[i]) ||
+ ggml_hexagon_tensor_will_tile(wi) != is_tiled) {
return false;
}
- if (tmp.inputs[i]->ne[1] > w_for_solve->ne[1]) {
- w_for_solve = tmp.inputs[i];
+ const int wi_eff_type = is_tiled ? htp_mm_tiled_weight_type(wi->type) : (int) wi->type;
+ if (wi_eff_type != w0_eff_type) {
+ has_mixed_wtypes = true;
+ }
+ if (htp_mm_get_hvx_act_format(wi->type) != w0_hvx_act_fmt) {
+ same_hvx_act_fmt = false;
+ }
+ if (htp_mm_hmx_lut_bytes(wi->type) != w0_lut_bytes) {
+ same_lut_bytes = false;
+ }
+ if (wi->ne[1] > w_for_solve->ne[1]) {
+ w_for_solve = wi;
+ }
+ const size_t raw_bytes = is_tiled ? htp_mm_tiled_raw_bytes_per_col_tile(wi->type, (uint32_t) act->ne[0])
+ : ggml_row_size(wi->type, act->ne[0]);
+ if (raw_bytes > max_raw_bytes) {
+ max_raw_bytes = raw_bytes;
+ max_raw_wtype = (ggml_type) wi_eff_type;
+ }
+ if (wi->nb[1] > max_nb1) {
+ max_nb1 = wi->nb[1];
+ }
+ if (htp_mm_use_fwht(wi->type, act)) {
+ fwht_cols += wi->ne[1];
+ } else {
+ no_fwht_cols += wi->ne[1];
}
if (ggml_type_size(tmp.outputs[i]->type) > ggml_type_size(max_dst_type)) {
max_dst_type = tmp.outputs[i]->type;
}
if ((scale_mask >> i) & 1u) {
const uint32_t sm = htp_mm_scale_mode_get(scale_mode, (uint32_t) i);
- const size_t sb = htp_mm_fused_scale_vtcm_bytes((uint32_t) tmp.inputs[i]->ne[1], (uint32_t) tmp.inputs[i]->ne[2], sm, true);
+ const size_t sb = htp_mm_fused_scale_vtcm_bytes((uint32_t) wi->ne[1], (uint32_t) wi->ne[2], sm, true);
if (sb > hmx_scale_sz) {
hmx_scale_sz = sb;
}
- hvx_scale_sz += htp_mm_fused_scale_vtcm_bytes((uint32_t) tmp.inputs[i]->ne[1], (uint32_t) tmp.inputs[i]->ne[2], sm, false);
+ hvx_scale_sz += htp_mm_fused_scale_vtcm_bytes((uint32_t) wi->ne[1], (uint32_t) wi->ne[2], sm, false);
}
}
+ if (has_mixed_wtypes && (is_id || !same_lut_bytes)) {
+ return false;
+ }
+ ggml_tensor w_solve = *w_for_solve;
+ w_solve.type = max_raw_wtype;
+ w_solve.nb[0] = ggml_type_size(max_raw_wtype);
+ w_solve.nb[1] = is_tiled ? ggml_row_size(max_raw_wtype, w_solve.ne[0]) : max_nb1;
+ w_solve.nb[2] = w_solve.nb[1] * w_solve.ne[1];
+ w_solve.nb[3] = w_solve.nb[2] * w_solve.ne[2];
ggml_tensor d_for_solve = *dst_for_solve;
d_for_solve.type = max_dst_type;
const bool solved = is_id
- ? htp_kparams_solve_mmidnx(sess, w_for_solve, act, &d_for_solve, (int32_t) n_w, kp, hmx_scale_sz, hvx_scale_sz)
- : htp_kparams_solve_mmnx(sess, w_for_solve, act, &d_for_solve, (int32_t) n_w, kp, hmx_scale_sz, hvx_scale_sz, has_norm);
+ ? htp_kparams_solve_mmidnx(sess, &w_solve, act, &d_for_solve, (int32_t) n_w, kp, hmx_scale_sz, hvx_scale_sz)
+ : htp_kparams_solve_mmnx(sess, &w_solve, act, &d_for_solve, (int32_t) n_w, kp, hmx_scale_sz, hvx_scale_sz, has_norm);
if (!solved) {
return false;
}
+ if (kp->n_hmx > 0 && !htp_mm_kernel_is_hmx_w8a8(kp->kernel_type)) {
+ for (size_t i = 0; i < n_w; ++i) {
+ if (ggml_needs_scale(tmp.inputs[i]->type) && !((scale_mask >> i) & 1u)) {
+ return false;
+ }
+ }
+ }
+ if (has_mixed_wtypes) {
+ if (kp->n_hmx > 0) {
+ if (htp_mm_kernel_is_hmx_w8a8(kp->kernel_type)) {
+ const int k = (int) act->ne[0];
+ for (size_t i = 0; i < n_w; ++i) {
+ const int wi_eff = htp_mm_tiled_weight_type(tmp.inputs[i]->type);
+ const uint32_t min_k = htp_hmx_w8a8_min_k(wi_eff);
+ if (!htp_is_hmx_w8a8_weight_type(wi_eff) || k < (int) min_k || (k % (int) min_k) != 0) {
+ return false;
+ }
+ }
+ }
+ } else if (!same_hvx_act_fmt) {
+ return false;
+ }
+ }
+ if (!is_id && htp_mm_kernel_is_hmx_w8a8(kp->kernel_type)) {
+ if (fwht_cols > no_fwht_cols) {
+ kp->flags |= HTP_MM_FLAG_FWHT;
+ } else {
+ kp->flags &= ~HTP_MM_FLAG_FWHT;
+ }
+ }
kp->scale_mask = scale_mask;
kp->scale_mode = scale_mode;
if (!is_id) {
@@ -6252,7 +6333,7 @@ static bool htp_kparams_solve_mm_node(
kp->norm_eps = norm_eps;
}
return kp->n_hmx ? htp_mm_kernel_is_hmx_2d(kp->kernel_type)
- : (ggml_hexagon_tensor_is_tiled(tmp.inputs[0]) &&
+ : (is_tiled &&
(kp->kernel_type == HTP_MM_KERNEL_HVX_QUANT_ROW || kp->kernel_type == HTP_MM_KERNEL_HVX_QUANT_BLOCK));
}
diff --git a/ggml/src/ggml-hexagon/htp/matmul-hmx-f16.c b/ggml/src/ggml-hexagon/htp/matmul-hmx-f16.c
index 46f846557..4edb0c5e3 100644
--- a/ggml/src/ggml-hexagon/htp/matmul-hmx-f16.c
+++ b/ggml/src/ggml-hexagon/htp/matmul-hmx-f16.c
@@ -518,8 +518,9 @@ static void hmx_f16_dequant_weight_chunk(
state.w_scale_ct_stride = p->w_scale_ct_stride;
const struct htp_mm_kernel_params * kparams = (const struct htp_mm_kernel_params *) octx->kernel_params;
- state.tile_size = kparams->tile_size;
- state.aligned_tile_size = kparams->aligned_tile_size;
+ const bool is_tiled = (kparams->tile_size > 0);
+ state.tile_size = is_tiled ? (int) htp_tensor_get_tile_size(p->weight_type) : 0;
+ state.aligned_tile_size = is_tiled ? (int) htp_tensor_get_tile_padded_size(p->weight_type) : 0;
work_queue_run(octx->ctx->hvx_queue, hmx_f16_dequant_weight_worker, &state, (int) state.n_tasks);
}
@@ -2175,15 +2176,11 @@ typedef struct {
size_t mr_end;
uint32_t n_weights;
uint32_t act_elem_size;
- uint32_t dma_dst_stride;
- uint32_t dma_width_bytes;
+ int k;
int act_stride;
int m_chunk_n_rows;
int n_chunk_n_cols;
int n_k_tiles;
- int n_weight_tiles;
- int tile_size;
- int weight_type;
bool is_tiled;
} hmx_f16_mm_nx_2d_rt_t;
@@ -2191,32 +2188,15 @@ static __attribute__((noinline)) void hmx_f16_mm_nx_2d_pipelined(hmx_f16_mm_nx_2
struct htp_ops_context * octx = rt->octx;
struct htp_context * ctx = octx->ctx;
const struct htp_mm_kernel_params * kparams = rt->kparams;
- tiled_dequant_task_fn_t dequant_worker_fn = rt->deq_p.dequant_worker_fn;
const uint32_t n_weights = rt->n_weights;
const size_t mr_end = rt->mr_end;
+ const int k = rt->k;
const int m_chunk_n_rows = rt->m_chunk_n_rows;
const int n_chunk_n_cols = rt->n_chunk_n_cols;
const int n_k_tiles = rt->n_k_tiles;
- const int n_weight_tiles = rt->n_weight_tiles;
- const int tile_size = rt->tile_size;
- const int weight_type = rt->weight_type;
const bool is_tiled = rt->is_tiled;
- const uint32_t dma_dst_stride = rt->dma_dst_stride;
- const uint32_t dma_width_bytes = rt->dma_width_bytes;
hmx_f16_matmul_task_t task_slots[2];
- void * vtcm_raw_bufs[4] = {
- rt->vtcm_weight_raw[0],
- rt->vtcm_weight_raw[1],
- dequant_worker_fn ? (void *) rt->vtcm_weight_raw[0] : rt->vtcm_scratch0,
- dequant_worker_fn ? (void *) rt->vtcm_weight_raw[1] : rt->vtcm_scratch1,
- };
- void * vtcm_weight_bufs[4] = {
- dequant_worker_fn ? rt->vtcm_scratch0 : (void *) rt->vtcm_weight_raw[0],
- dequant_worker_fn ? rt->vtcm_scratch1 : (void *) rt->vtcm_weight_raw[1],
- rt->vtcm_scratch0,
- rt->vtcm_scratch1,
- };
__fp16 * vtcm_output_bufs[2] = { rt->vtcm_output, (__fp16 *) rt->vtcm_scratch2 };
for (size_t mr = rt->m_start; mr < mr_end; mr += m_chunk_n_rows) {
@@ -2238,6 +2218,27 @@ static __attribute__((noinline)) void hmx_f16_mm_nx_2d_pipelined(hmx_f16_mm_nx_2
const dma_addr_t dst_dma_addr = dst->data;
const size_t n = src_w->ne[1];
if (n == 0) continue;
+
+ const int weight_type = (int) src_w->type;
+ const size_t row_stride = htp_tensor_get_row_size(weight_type, k);
+ const int tile_size = is_tiled ? (int) htp_tensor_get_tile_size(weight_type) : 0;
+ const int aligned_tile_size = is_tiled ? (int) htp_tensor_get_tile_padded_size(weight_type) : 0;
+ const int n_weight_tiles = is_tiled ? ((weight_type == HTP_TYPE_Q4_0) ? n_k_tiles : (k / (int) htp_tensor_get_tile_k(weight_type))) : 0;
+ const tiled_dequant_task_fn_t dequant_worker_fn = hmx_f16_get_dequant_worker_fn(weight_type, is_tiled);
+
+ void * vtcm_raw_bufs[4] = {
+ rt->vtcm_weight_raw[0],
+ rt->vtcm_weight_raw[1],
+ dequant_worker_fn ? (void *) rt->vtcm_weight_raw[0] : rt->vtcm_scratch0,
+ dequant_worker_fn ? (void *) rt->vtcm_weight_raw[1] : rt->vtcm_scratch1,
+ };
+ void * vtcm_weight_bufs[4] = {
+ dequant_worker_fn ? rt->vtcm_scratch0 : (void *) rt->vtcm_weight_raw[0],
+ dequant_worker_fn ? rt->vtcm_scratch1 : (void *) rt->vtcm_weight_raw[1],
+ rt->vtcm_scratch0,
+ rt->vtcm_scratch1,
+ };
+
const size_t weight_stride = src_w->nb[1];
const size_t dst_elem_size = (dst->type == HTP_TYPE_F16) ? sizeof(__fp16) : sizeof(float);
const size_t dst_stride = dst->nb[1] / dst_elem_size;
@@ -2246,7 +2247,9 @@ static __attribute__((noinline)) void hmx_f16_mm_nx_2d_pipelined(hmx_f16_mm_nx_2
const int res_out_stride_elems = (int) (res_out_stride_bytes / dst_elem_size);
const int dst_cols = (int) dst->ne[0];
const uint32_t n_chunk_cnt = (uint32_t) hmx_ceil_div(n, n_chunk_n_cols);
- const uint32_t dma_src_stride = is_tiled ? tile_size : weight_stride;
+ const uint32_t dma_dst_stride = is_tiled ? (uint32_t) aligned_tile_size : (uint32_t) row_stride;
+ const uint32_t dma_src_stride = is_tiled ? (uint32_t) tile_size : (uint32_t) weight_stride;
+ const uint32_t dma_width_bytes = is_tiled ? (uint32_t) tile_size : (uint32_t) row_stride;
const struct htp_tensor * p_scale = (((kparams->scale_mask >> p) & 1u) != 0) ? octx->src[n_weights + 1 + p] : NULL;
const bool p_per_ch = p_scale && htp_mm_scale_is_per_channel(htp_mm_scale_mode_get(kparams->scale_mode, p));
@@ -2259,6 +2262,9 @@ static __attribute__((noinline)) void hmx_f16_mm_nx_2d_pipelined(hmx_f16_mm_nx_2
const bool p_scale_in_deq = p_scale && hmx_f16_scales_in_dequant(weight_type, rt->vtcm_scale, p_per_ch ? n : 1u);
const bool p_scale_in_ep = p_scale && !p_scale_in_deq;
+ rt->deq_p.row_stride = row_stride;
+ rt->deq_p.weight_type = weight_type;
+ rt->deq_p.dequant_worker_fn = dequant_worker_fn;
rt->deq_p.w_scale_ct_stride = p_ct_stride;
rt->out_p.dst_stride = res_out_stride_elems;
rt->out_p.dst_elem_size = dst_elem_size;
@@ -2380,22 +2386,15 @@ static __attribute__((noinline)) void hmx_f16_mm_nx_2d_seq(hmx_f16_mm_nx_2d_rt_t
struct htp_ops_context * octx = rt->octx;
struct htp_context * ctx = octx->ctx;
const struct htp_mm_kernel_params * kparams = rt->kparams;
- tiled_dequant_task_fn_t dequant_worker_fn = rt->deq_p.dequant_worker_fn;
const uint32_t n_weights = rt->n_weights;
const size_t mr_end = rt->mr_end;
+ const int k = rt->k;
const int m_chunk_n_rows = rt->m_chunk_n_rows;
const int n_chunk_n_cols = rt->n_chunk_n_cols;
const int n_k_tiles = rt->n_k_tiles;
- const int n_weight_tiles = rt->n_weight_tiles;
- const int tile_size = rt->tile_size;
- const int weight_type = rt->weight_type;
const bool is_tiled = rt->is_tiled;
- const uint32_t dma_dst_stride = rt->dma_dst_stride;
- const uint32_t dma_width_bytes = rt->dma_width_bytes;
hmx_f16_matmul_task_t task;
- void * vtcm_raw_bufs[2] = { rt->vtcm_weight_raw[0], dequant_worker_fn ? (void *) rt->vtcm_weight_raw[0] : rt->vtcm_scratch0 };
- void * vtcm_weight_bufs[2] = { dequant_worker_fn ? rt->vtcm_scratch0 : (void *) rt->vtcm_weight_raw[0], rt->vtcm_scratch0 };
rt->out_p.dst = rt->vtcm_res_out[0];
rt->out_p.vtcm_src = rt->vtcm_output;
@@ -2419,6 +2418,17 @@ static __attribute__((noinline)) void hmx_f16_mm_nx_2d_seq(hmx_f16_mm_nx_2d_rt_t
const dma_addr_t dst_dma_addr = dst->data;
const size_t n = src_w->ne[1];
if (n == 0) continue;
+
+ const int weight_type = (int) src_w->type;
+ const size_t row_stride = htp_tensor_get_row_size(weight_type, k);
+ const int tile_size = is_tiled ? (int) htp_tensor_get_tile_size(weight_type) : 0;
+ const int aligned_tile_size = is_tiled ? (int) htp_tensor_get_tile_padded_size(weight_type) : 0;
+ const int n_weight_tiles = is_tiled ? ((weight_type == HTP_TYPE_Q4_0) ? n_k_tiles : (k / (int) htp_tensor_get_tile_k(weight_type))) : 0;
+ const tiled_dequant_task_fn_t dequant_worker_fn = hmx_f16_get_dequant_worker_fn(weight_type, is_tiled);
+
+ void * vtcm_raw_bufs[2] = { rt->vtcm_weight_raw[0], dequant_worker_fn ? (void *) rt->vtcm_weight_raw[0] : rt->vtcm_scratch0 };
+ void * vtcm_weight_bufs[2] = { dequant_worker_fn ? rt->vtcm_scratch0 : (void *) rt->vtcm_weight_raw[0], rt->vtcm_scratch0 };
+
const size_t weight_stride = src_w->nb[1];
const size_t dst_elem_size = (dst->type == HTP_TYPE_F16) ? sizeof(__fp16) : sizeof(float);
const size_t dst_stride = dst->nb[1] / dst_elem_size;
@@ -2427,7 +2437,9 @@ static __attribute__((noinline)) void hmx_f16_mm_nx_2d_seq(hmx_f16_mm_nx_2d_rt_t
const int res_out_stride_elems = (int) (res_out_stride_bytes / dst_elem_size);
const int dst_cols = (int) dst->ne[0];
const uint32_t n_chunk_cnt = (uint32_t) hmx_ceil_div(n, n_chunk_n_cols);
- const uint32_t dma_src_stride = is_tiled ? tile_size : weight_stride;
+ const uint32_t dma_dst_stride = is_tiled ? (uint32_t) aligned_tile_size : (uint32_t) row_stride;
+ const uint32_t dma_src_stride = is_tiled ? (uint32_t) tile_size : (uint32_t) weight_stride;
+ const uint32_t dma_width_bytes = is_tiled ? (uint32_t) tile_size : (uint32_t) row_stride;
const struct htp_tensor * p_scale = (((kparams->scale_mask >> p) & 1u) != 0) ? octx->src[n_weights + 1 + p] : NULL;
const bool p_per_ch = p_scale && htp_mm_scale_is_per_channel(htp_mm_scale_mode_get(kparams->scale_mode, p));
@@ -2440,6 +2452,9 @@ static __attribute__((noinline)) void hmx_f16_mm_nx_2d_seq(hmx_f16_mm_nx_2d_rt_t
const bool p_scale_in_deq = p_scale && hmx_f16_scales_in_dequant(weight_type, rt->vtcm_scale, p_per_ch ? n : 1u);
const bool p_scale_in_ep = p_scale && !p_scale_in_deq;
+ rt->deq_p.row_stride = row_stride;
+ rt->deq_p.weight_type = weight_type;
+ rt->deq_p.dequant_worker_fn = dequant_worker_fn;
rt->deq_p.w_scale_ct_stride = p_ct_stride;
rt->out_p.dst_stride = res_out_stride_elems;
rt->out_p.dst_elem_size = dst_elem_size;
@@ -2509,25 +2524,20 @@ int hmx_f16_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_p
const uint32_t n_weights = kparams->n_weights;
- const struct htp_tensor * restrict src0 = octx->src[0];
const struct htp_tensor * restrict act = octx->src[n_weights];
- const int weight_type = (int) src0->type;
const int k = (int) act->ne[0];
const int k_valid = (int) act->ne[0];
const int m = (int) (act->ne[1] * act->ne[2] * act->ne[3]);
const uint32_t act_elem_size = (act->type == HTP_TYPE_F16) ? sizeof(__fp16) : sizeof(float);
const int act_stride = (int) (act->nb[1] / act_elem_size);
const dma_addr_t act_dma_addr = act->data;
- const size_t row_stride = htp_tensor_get_row_size(weight_type, k);
const int n_k_tiles = k >> 5;
- const int n_weight_tiles = (weight_type == HTP_TYPE_Q4_0) ? n_k_tiles : (k / htp_tensor_get_tile_k(weight_type));
const struct fastdiv_values n_k_tiles_div = kparams->div_n_k_tiles;
- const int tile_size = kparams->tile_size;
- const int aligned_tile_size = kparams->aligned_tile_size;
- const bool is_tiled = (tile_size > 0);
- tiled_dequant_task_fn_t dequant_worker_fn = hmx_f16_get_dequant_worker_fn(weight_type, is_tiled);
+ const bool is_tiled = (kparams->tile_size > 0);
+ const int vtcm_wtype = (int) kparams->vtcm_wtype;
+ const int aligned_tile_size = (int) kparams->aligned_tile_size;
const size_t vtcm_budget = ctx->vtcm_size;
const int m_chunk_n_rows = kparams->m_chunk;
@@ -2537,9 +2547,6 @@ int hmx_f16_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_p
const struct fastdiv_values * act_hvx_div = &kparams->div_n_act_hvx;
const struct fastdiv_values * k_div = &kparams->div_ne00_padded;
- const uint32_t dma_dst_stride = is_tiled ? aligned_tile_size : row_stride;
- const uint32_t dma_width_bytes = is_tiled ? tile_size : row_stride;
-
size_t max_dst_elem_size = sizeof(__fp16);
for (uint32_t p = 0; p < n_weights; ++p) {
if (octx->dsts[p]) {
@@ -2552,7 +2559,7 @@ int hmx_f16_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_p
const size_t vtcm_norm_bytes = norm_w ? htp_mm_fused_norm_vtcm_bytes((uint32_t) k) : 0;
struct htp_mm_hmx_vtcm_layout L;
- htp_mm_hmx_vtcm_layout_build_ex_scaled(&L, HTP_MM_KERNEL_HMX_F16_2D, weight_type, k,
+ htp_mm_hmx_vtcm_layout_build_ex_scaled(&L, HTP_MM_KERNEL_HMX_F16_2D, vtcm_wtype, k,
m_chunk_n_rows, n_chunk_n_cols, 1, pipeline, act_hvx, aligned_tile_size, 0,
(size_t) kparams->vtcm_scale_size, 0, max_dst_elem_size, vtcm_norm_bytes);
@@ -2602,7 +2609,7 @@ int hmx_f16_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_p
}
FARF(HIGH, "hmx-f16-mm-nx-2d: n_weights %u m %d (%d..%d) k %d wtype %d mc %d nc %d vtcm %zu/%zu",
- n_weights, m, m_start, m_start + m_rows, k, weight_type, m_chunk_n_rows, n_chunk_n_cols, L.total_bytes, vtcm_budget);
+ n_weights, m, m_start, m_start + m_rows, k, vtcm_wtype, m_chunk_n_rows, n_chunk_n_cols, L.total_bytes, vtcm_budget);
htp_trace_event_stop(tr, HTP_TRACE_EVT_INIT, 0);
@@ -2625,12 +2632,9 @@ int hmx_f16_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_p
.norm_eps = kparams->norm_eps,
},
.deq_p = {
- .k_block = k,
- .row_stride = row_stride,
- .weight_type = weight_type,
- .n_k_tiles = n_k_tiles,
- .n_k_tiles_div = n_k_tiles_div,
- .dequant_worker_fn = dequant_worker_fn,
+ .k_block = k,
+ .n_k_tiles = n_k_tiles,
+ .n_k_tiles_div = n_k_tiles_div,
},
.out_p = {
.bias_elem_size = sizeof(float),
@@ -2649,15 +2653,11 @@ int hmx_f16_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_p
.mr_end = (size_t) (m_start + m_rows),
.n_weights = n_weights,
.act_elem_size = act_elem_size,
- .dma_dst_stride = dma_dst_stride,
- .dma_width_bytes = dma_width_bytes,
+ .k = k,
.act_stride = act_stride,
.m_chunk_n_rows = m_chunk_n_rows,
.n_chunk_n_cols = n_chunk_n_cols,
.n_k_tiles = n_k_tiles,
- .n_weight_tiles = n_weight_tiles,
- .tile_size = tile_size,
- .weight_type = weight_type,
.is_tiled = is_tiled,
};
diff --git a/ggml/src/ggml-hexagon/htp/matmul-hmx-w8a8.c b/ggml/src/ggml-hexagon/htp/matmul-hmx-w8a8.c
index a8a7f78a6..ae967d582 100644
--- a/ggml/src/ggml-hexagon/htp/matmul-hmx-w8a8.c
+++ b/ggml/src/ggml-hexagon/htp/matmul-hmx-w8a8.c
@@ -1754,9 +1754,6 @@ typedef struct {
int m_chunk_n_rows;
int n_chunk_n_cols;
int n_k_tiles;
- int n_weight_tiles;
- int tile_size;
- int aligned_tile_size;
int n_groups;
} hmx_w8a8_mm_nx_2d_rt_t;
@@ -1770,15 +1767,18 @@ static __attribute__((noinline)) void hmx_w8a8_mm_nx_2d_pipelined(hmx_w8a8_mm_nx
const int m_chunk_n_rows = rt->m_chunk_n_rows;
const int n_chunk_n_cols = rt->n_chunk_n_cols;
const int n_k_tiles = rt->n_k_tiles;
- const int n_weight_tiles = rt->n_weight_tiles;
- const int tile_size = rt->tile_size;
- const int aligned_tile_size = rt->aligned_tile_size;
+ const int k = n_k_tiles << 5;
const int n_groups = rt->n_groups;
hmx_w8a8_matmul_task_t task_slots[2];
const bool preissue_p0 = L->can_preissue_weight && n_weights > 0
&& octx->src[0] && octx->dsts[0] && octx->src[0]->ne[1] > 0;
+ const int p0_wtype = preissue_p0 ? (int) octx->src[0]->type : 0;
+ const int p0_tile_size = preissue_p0 ? (int) htp_tensor_get_tile_size(p0_wtype) : 0;
+ const int p0_aligned_tile_size = preissue_p0 ? (int) htp_tensor_get_tile_padded_size(p0_wtype) : 0;
+ const int p0_n_weight_tiles = preissue_p0
+ ? ((p0_wtype == HTP_TYPE_Q4_0) ? n_k_tiles : (k / (int) htp_tensor_get_tile_k(p0_wtype))) : 0;
for (size_t mr = rt->m_start; mr < mr_end; mr += m_chunk_n_rows) {
const size_t n_rows = hex_smin(mr_end - mr, m_chunk_n_rows);
@@ -1787,9 +1787,9 @@ static __attribute__((noinline)) void hmx_w8a8_mm_nx_2d_pipelined(hmx_w8a8_mm_nx
const struct htp_tensor * src_w0 = octx->src[0];
const size_t n0 = src_w0->ne[1];
const size_t n_cols_A0 = hex_smin(n0, (size_t) n_chunk_n_cols);
- const uint32_t height_A0 = hmx_ceil_div(n_cols_A0, 32) * n_weight_tiles;
+ const uint32_t height_A0 = hmx_ceil_div(n_cols_A0, 32) * p0_n_weight_tiles;
dma_proxy_push(ctx->dma_proxy, dma_make_data(rt->vtcm_weight_raw[0], src_w0->data),
- aligned_tile_size, tile_size, tile_size, height_A0);
+ p0_aligned_tile_size, p0_tile_size, p0_tile_size, height_A0);
}
rt->act_p.act_dma_addr = rt->act_dma_addr + (size_t) mr * rt->act_stride * rt->act_elem_size;
@@ -1803,6 +1803,11 @@ static __attribute__((noinline)) void hmx_w8a8_mm_nx_2d_pipelined(hmx_w8a8_mm_nx
const struct htp_tensor * restrict dst = octx->dsts[p];
if (!src_w || !dst) continue;
+ const int p_wtype = (int) src_w->type;
+ const int tile_size = (int) htp_tensor_get_tile_size(p_wtype);
+ const int aligned_tile_size = (int) htp_tensor_get_tile_padded_size(p_wtype);
+ const int n_weight_tiles = (p_wtype == HTP_TYPE_Q4_0) ? n_k_tiles : (k / (int) htp_tensor_get_tile_k(p_wtype));
+
const dma_addr_t weight = src_w->data;
dma_queue * weight_dma = octx->ctx->dma[0];
const dma_addr_t dst_dma_addr = dst->data;
@@ -1826,6 +1831,8 @@ static __attribute__((noinline)) void hmx_w8a8_mm_nx_2d_pipelined(hmx_w8a8_mm_nx
htp_mm_load_scale(weight_dma, rt->vtcm_scale, p_scale->data, p_sbytes, !p_per_ch);
}
+ rt->rescale_p.weight_type = p_wtype;
+ rt->rescale_p.aligned_tile_size = (uint32_t) aligned_tile_size;
rt->rescale_p.w_scale_ct_stride = p_ct_stride;
rt->out_p.dst_stride = res_out_stride_elems;
rt->out_p.dst_elem_size = dst_elem_size;
@@ -1961,9 +1968,7 @@ static __attribute__((noinline)) void hmx_w8a8_mm_nx_2d_seq(hmx_w8a8_mm_nx_2d_rt
const int m_chunk_n_rows = rt->m_chunk_n_rows;
const int n_chunk_n_cols = rt->n_chunk_n_cols;
const int n_k_tiles = rt->n_k_tiles;
- const int n_weight_tiles = rt->n_weight_tiles;
- const int tile_size = rt->tile_size;
- const int aligned_tile_size = rt->aligned_tile_size;
+ const int k = n_k_tiles << 5;
const int n_groups = rt->n_groups;
hmx_w8a8_matmul_task_t task;
@@ -1987,6 +1992,11 @@ static __attribute__((noinline)) void hmx_w8a8_mm_nx_2d_seq(hmx_w8a8_mm_nx_2d_rt
const struct htp_tensor * restrict dst = octx->dsts[p];
if (!src_w || !dst) continue;
+ const int p_wtype = (int) src_w->type;
+ const int tile_size = (int) htp_tensor_get_tile_size(p_wtype);
+ const int aligned_tile_size = (int) htp_tensor_get_tile_padded_size(p_wtype);
+ const int n_weight_tiles = (p_wtype == HTP_TYPE_Q4_0) ? n_k_tiles : (k / (int) htp_tensor_get_tile_k(p_wtype));
+
const dma_addr_t weight = src_w->data;
dma_queue * weight_dma = octx->ctx->dma[0];
const dma_addr_t dst_dma_addr = dst->data;
@@ -2009,6 +2019,8 @@ static __attribute__((noinline)) void hmx_w8a8_mm_nx_2d_seq(hmx_w8a8_mm_nx_2d_rt
htp_mm_load_scale(weight_dma, rt->vtcm_scale, p_scale->data, p_sbytes, !p_per_ch);
}
+ rt->rescale_p.weight_type = p_wtype;
+ rt->rescale_p.aligned_tile_size = (uint32_t) aligned_tile_size;
rt->rescale_p.w_scale_ct_stride = p_ct_stride;
rt->out_p.dst_stride = res_out_stride_elems;
rt->out_p.dst_elem_size = dst_elem_size;
@@ -2075,10 +2087,8 @@ int hmx_w8a8_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_
const uint32_t n_weights = kparams->n_weights;
- const struct htp_tensor * restrict src0 = octx->src[0];
const struct htp_tensor * restrict act = octx->src[n_weights];
- const int weight_type = (int) src0->type;
const int k = (int) act->ne[0];
const int k_valid = (int) act->ne[0];
const int m = (int) (act->ne[1] * act->ne[2] * act->ne[3]);
@@ -2091,9 +2101,8 @@ int hmx_w8a8_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_
const struct fastdiv_values n_groups_div = kparams->div_hmx_n_groups;
const int n_k_tiles = k >> 5;
- const int n_weight_tiles = (weight_type == HTP_TYPE_Q4_0) ? n_k_tiles : (k / htp_tensor_get_tile_k(weight_type));
- const int tile_size = kparams->tile_size;
- const int aligned_tile_size = kparams->aligned_tile_size;
+ const int vtcm_wtype = (int) kparams->vtcm_wtype;
+ const int aligned_tile_size = (int) kparams->aligned_tile_size;
const size_t vtcm_budget = ctx->vtcm_size;
const int m_chunk_n_rows = kparams->m_chunk;
@@ -2114,7 +2123,7 @@ int hmx_w8a8_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_
const size_t vtcm_norm_bytes = norm_w ? htp_mm_fused_norm_vtcm_bytes((uint32_t) k) : 0;
struct htp_mm_hmx_vtcm_layout L;
- htp_mm_hmx_vtcm_layout_build_ex_scaled(&L, kernel_type, weight_type, k,
+ htp_mm_hmx_vtcm_layout_build_ex_scaled(&L, kernel_type, vtcm_wtype, k,
m_chunk_n_rows, n_chunk_n_cols, 1, pipeline, act_hvx, aligned_tile_size, 0,
(size_t) kparams->vtcm_scale_size, 0, max_dst_elem_size, vtcm_norm_bytes);
@@ -2167,7 +2176,7 @@ int hmx_w8a8_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_
}
FARF(HIGH, "hmx-w8a8-mm-nx-2d: ktype %d n_weights %u m %d (%d..%d) k %d wtype %d mc %d nc %d vtcm %zu/%zu",
- (int) kernel_type, n_weights, m, m_start, m_start + m_rows, k, weight_type, m_chunk_n_rows, n_chunk_n_cols, L.total_bytes, vtcm_budget);
+ (int) kernel_type, n_weights, m, m_start, m_start + m_rows, k, vtcm_wtype, m_chunk_n_rows, n_chunk_n_cols, L.total_bytes, vtcm_budget);
htp_trace_event_stop(tr, HTP_TRACE_EVT_INIT, 0);
@@ -2197,8 +2206,6 @@ int hmx_w8a8_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_
.k_block = (uint32_t) k,
.n_groups = (uint32_t) n_groups,
.n_groups_div = &n_groups_div,
- .aligned_tile_size = (uint32_t) aligned_tile_size,
- .weight_type = weight_type,
},
.out_p = {
.vtcm_act_scales = vtcm_act_scales,
@@ -2220,9 +2227,6 @@ int hmx_w8a8_mm_nx_2d(struct htp_ops_context * octx, const struct htp_mm_kernel_
.m_chunk_n_rows = m_chunk_n_rows,
.n_chunk_n_cols = n_chunk_n_cols,
.n_k_tiles = n_k_tiles,
- .n_weight_tiles = n_weight_tiles,
- .tile_size = tile_size,
- .aligned_tile_size = aligned_tile_size,
.n_groups = n_groups,
};
diff --git a/ggml/src/ggml-hexagon/htp/matmul-hvx.c b/ggml/src/ggml-hexagon/htp/matmul-hvx.c
index 603a11d52..a66437b87 100644
--- a/ggml/src/ggml-hexagon/htp/matmul-hvx.c
+++ b/ggml/src/ggml-hexagon/htp/matmul-hvx.c
@@ -40,6 +40,16 @@ struct htp_mm_context {
const void * restrict vy0, const void * restrict vy1, uint32_t valid_rows,
const void * restrict sz0, const void * restrict sz1, hvx_tiled_store_row_fn_t store_row);
+ void (*vec_dot_32x1_nx[HTP_OP_MAX_OUTPUTS])(const uint32_t n, void * restrict s,
+ const void * restrict vx,
+ const void * restrict vy, uint32_t valid_rows,
+ const void * restrict sz, hvx_tiled_store_row_fn_t store_row);
+
+ void (*vec_dot_32x2_nx[HTP_OP_MAX_OUTPUTS])(const uint32_t n, void * restrict s0, void * restrict s1,
+ const void * restrict vx,
+ const void * restrict vy0, const void * restrict vy1, uint32_t valid_rows,
+ const void * restrict sz0, const void * restrict sz1, hvx_tiled_store_row_fn_t store_row);
+
hvx_tiled_store_row_fn_t store_row;
hvx_flat_store_1x1_fn_t flat_store_1x1;
hvx_flat_store_2x1_fn_t flat_store_2x1;
@@ -307,8 +317,8 @@ static void hvx_mm_nx_2d_tiled(const htp_thread_t * th, unsigned int nth, unsign
if (!src_w || !dst) continue;
dma_queue * dma_q = th->dma_q;
- const uint32_t tile_size = kparams->tile_size;
- const uint32_t aligned_tile_size = kparams->aligned_tile_size;
+ const uint32_t tile_size = htp_tensor_get_tile_size(src_w->type);
+ const uint32_t aligned_tile_size = htp_tensor_get_tile_padded_size(src_w->type);
const uint32_t ne00 = src_w->ne[0];
const uint32_t ne01 = src_w->ne[1];
const size_t dst_row_size = dst->nb[1];
@@ -359,7 +369,7 @@ static void hvx_mm_nx_2d_tiled(const htp_thread_t * th, unsigned int nth, unsign
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, ct);
void * dst_ptr = vtcm_dst_ptr + (size_t) (ct - ct_start) * 32 * dst_elem_size;
- mmctx->vec_dot_32x1(ne10, dst_ptr, w_tile, act_data, valid_rows, w_scale_ptr, store_row);
+ mmctx->vec_dot_32x1_nx[widx](ne10, dst_ptr, w_tile, act_data, valid_rows, w_scale_ptr, store_row);
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, ct);
if (push_ct < ct_end) {
@@ -391,12 +401,12 @@ static void hvx_mm_nx_2d_tiled(const htp_thread_t * th, unsigned int nth, unsign
const uint8_t * restrict act_col1 = (const uint8_t *) (act_data + (m0 + r + 1) * act_stride);
void * dst_ptr0 = vtcm_dst_ptr + (size_t) (r + 0) * 128;
void * dst_ptr1 = vtcm_dst_ptr + (size_t) (r + 1) * 128;
- mmctx->vec_dot_32x2(ne10, dst_ptr0, dst_ptr1, w_tile, act_col0, act_col1, valid_rows, w_scale_ptr, w_scale_ptr, store_row);
+ mmctx->vec_dot_32x2_nx[widx](ne10, dst_ptr0, dst_ptr1, w_tile, act_col0, act_col1, valid_rows, w_scale_ptr, w_scale_ptr, store_row);
}
for (; r < m_cnt; ++r) {
const uint8_t * restrict act_col = (const uint8_t *) (act_data + (m0 + r) * act_stride);
void * dst_ptr = vtcm_dst_ptr + (size_t) r * 128;
- mmctx->vec_dot_32x1(ne10, dst_ptr, w_tile, act_col, valid_rows, w_scale_ptr, store_row);
+ mmctx->vec_dot_32x1_nx[widx](ne10, dst_ptr, w_tile, act_col, valid_rows, w_scale_ptr, store_row);
}
const dma_addr_t dst_addr = dst->data + (dma_addr_t) m0 * dst_row_size + ct_dst_off;
dma_queue_push(dma_q, dma_make_data(dst_addr, vtcm_dst_ptr), dst_row_size, 128, row_bytes, m_cnt);
@@ -733,31 +743,24 @@ static void quantize_f16_q8_1_s16_tiled_block(const htp_thread_t * th, unsigned
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_A_QUANT, mmctx->quant_ib_first[ith]);
}
-// q8_1 for weight types with offsets (q8_1_s16 for Q2_K), f16_tiled for F16/F8_E4M3, otherwise q8_0
static inline work_queue_func_t htp_mm_act_quant_row_func(int weight_type, int act_type) {
const bool act_f16 = (act_type == HTP_TYPE_F16);
- if (weight_type == HTP_TYPE_F16 || weight_type == HTP_TYPE_F8_E4M3) {
- return act_f16 ? quantize_f16_f16_tiled : quantize_f32_f16_tiled;
- }
- if (weight_type == HTP_TYPE_Q2_K) {
- return act_f16 ? quantize_f16_q8_1_s16_tiled : quantize_f32_q8_1_s16_tiled;
- }
- if (htp_mm_weight_has_offset(weight_type)) {
- return act_f16 ? quantize_f16_q8_1_tiled : quantize_f32_q8_1_tiled;
+ switch (htp_mm_get_hvx_act_format(weight_type)) {
+ case HTP_MM_HVX_ACT_F16: return act_f16 ? quantize_f16_f16_tiled : quantize_f32_f16_tiled;
+ case HTP_MM_HVX_ACT_Q8_1_S16: return act_f16 ? quantize_f16_q8_1_s16_tiled : quantize_f32_q8_1_s16_tiled;
+ case HTP_MM_HVX_ACT_Q8_1: return act_f16 ? quantize_f16_q8_1_tiled : quantize_f32_q8_1_tiled;
+ case HTP_MM_HVX_ACT_Q8_0: return act_f16 ? quantize_f16_q8_0_tiled : quantize_f32_q8_0_tiled;
}
return act_f16 ? quantize_f16_q8_0_tiled : quantize_f32_q8_0_tiled;
}
static inline work_queue_func_t htp_mm_act_quant_block_func(int weight_type, int act_type) {
const bool act_f16 = (act_type == HTP_TYPE_F16);
- if (weight_type == HTP_TYPE_F16 || weight_type == HTP_TYPE_F8_E4M3) {
- return act_f16 ? quantize_f16_f16_tiled_block : quantize_f32_f16_tiled_block;
- }
- if (weight_type == HTP_TYPE_Q2_K) {
- return act_f16 ? quantize_f16_q8_1_s16_tiled_block : quantize_f32_q8_1_s16_tiled_block;
- }
- if (htp_mm_weight_has_offset(weight_type)) {
- return act_f16 ? quantize_f16_q8_1_tiled_block : quantize_f32_q8_1_tiled_block;
+ switch (htp_mm_get_hvx_act_format(weight_type)) {
+ case HTP_MM_HVX_ACT_F16: return act_f16 ? quantize_f16_f16_tiled_block : quantize_f32_f16_tiled_block;
+ case HTP_MM_HVX_ACT_Q8_1_S16: return act_f16 ? quantize_f16_q8_1_s16_tiled_block : quantize_f32_q8_1_s16_tiled_block;
+ case HTP_MM_HVX_ACT_Q8_1: return act_f16 ? quantize_f16_q8_1_tiled_block : quantize_f32_q8_1_tiled_block;
+ case HTP_MM_HVX_ACT_Q8_0: return act_f16 ? quantize_f16_q8_0_tiled_block : quantize_f32_q8_0_tiled_block;
}
return act_f16 ? quantize_f16_q8_0_tiled_block : quantize_f32_q8_0_tiled_block;
}
@@ -2338,9 +2341,14 @@ int hvx_mm_matmul_nx(struct htp_ops_context * octx) {
mmctx->cur_m_rows = act_nrows;
const size_t src0_row_size = src0->nb[1];
-
- if (hvx_mm_init_tiled_vec_dot(mmctx, src0->type) != 0) {
- return HTP_STATUS_NO_SUPPORT;
+ for (uint32_t p = 0; p < n_weights; ++p) {
+ const struct htp_tensor * sw = octx->src[p];
+ if (!sw) continue;
+ if (hvx_mm_init_tiled_vec_dot(mmctx, sw->type) != 0) {
+ return HTP_STATUS_NO_SUPPORT;
+ }
+ mmctx->vec_dot_32x1_nx[p] = mmctx->vec_dot_32x1;
+ mmctx->vec_dot_32x2_nx[p] = mmctx->vec_dot_32x2;
}
const uint32_t qk = HTP_MM_ACT_BLOCK_K;
@@ -2376,7 +2384,7 @@ int hvx_mm_matmul_nx(struct htp_ops_context * octx) {
}
}
struct htp_mm_hvx_vtcm_layout L;
- htp_mm_hvx_vtcm_layout_build_ex(&L, kparams->kernel_type, src0->type, act->ne[0], act_nrows, octx->n_hvx,
+ htp_mm_hvx_vtcm_layout_build_ex(&L, kparams->kernel_type, (int) kparams->vtcm_wtype, act->ne[0], act_nrows, octx->n_hvx,
max_dst_row_size, src0_row_size, act_elem_size, 0,
(size_t) kparams->vtcm_scale_size, kparams->n_prefetch, false, true);
diff --git a/ggml/src/ggml-hexagon/htp/matmul-ops.h b/ggml/src/ggml-hexagon/htp/matmul-ops.h
index fde8ebb48..7166e8552 100644
--- a/ggml/src/ggml-hexagon/htp/matmul-ops.h
+++ b/ggml/src/ggml-hexagon/htp/matmul-ops.h
@@ -190,6 +190,7 @@ struct htp_mm_kernel_params {
// Fused RMS_NORM_MUL -> HMX activation prep
float norm_eps; // RMSNorm epsilon
+ uint8_t vtcm_wtype; // Worst-case weight type used for VTCM sizing (enum htp_data_type)
};
#if defined(__cplusplus)
@@ -296,6 +297,31 @@ static inline bool htp_mm_weight_has_offset(int weight_type) {
weight_type == HTP_TYPE_Q2_K || weight_type == HTP_TYPE_Q2_0 || weight_type == HTP_TYPE_Q5_1;
}
+enum htp_mm_hvx_act_format {
+ HTP_MM_HVX_ACT_F16 = 0,
+ HTP_MM_HVX_ACT_Q8_1_S16 = 1,
+ HTP_MM_HVX_ACT_Q8_1 = 2,
+ HTP_MM_HVX_ACT_Q8_0 = 3,
+};
+
+static inline int htp_mm_tiled_weight_type(int wtype) {
+ return (wtype == HTP_TYPE_F32 || wtype == HTP_TYPE_BF16) ? HTP_TYPE_F16 : wtype;
+}
+
+static inline enum htp_mm_hvx_act_format htp_mm_get_hvx_act_format(int weight_type) {
+ weight_type = htp_mm_tiled_weight_type(weight_type);
+ if (weight_type == HTP_TYPE_F16 || weight_type == HTP_TYPE_F8_E4M3) return HTP_MM_HVX_ACT_F16;
+ if (weight_type == HTP_TYPE_Q2_K) return HTP_MM_HVX_ACT_Q8_1_S16;
+ if (htp_mm_weight_has_offset(weight_type)) return HTP_MM_HVX_ACT_Q8_1;
+ return HTP_MM_HVX_ACT_Q8_0;
+}
+
+static inline size_t htp_mm_tiled_raw_bytes_per_col_tile(int wtype, uint32_t k) {
+ wtype = htp_mm_tiled_weight_type(wtype);
+ const uint32_t tk = htp_tensor_get_tile_k(wtype);
+ return tk ? ((size_t) ((k + tk - 1u) / tk) * htp_tensor_get_tile_padded_size(wtype)) : 0;
+}
+
// --- Activation/Row Size Helpers ---
static inline size_t htp_mm_q8_0_tiled_row_size(uint32_t ne) {
const uint32_t ne_padded = hex_round_up(ne, HTP_MM_ACT_BLOCK_K);
@@ -316,10 +342,13 @@ static inline size_t htp_mm_f16_tiled_row_size(uint32_t ne) {
}
static inline size_t htp_mm_tiled_act_row_size(int weight_type, uint32_t ne) {
- if (weight_type == HTP_TYPE_F16 || weight_type == HTP_TYPE_F8_E4M3) {
- return htp_mm_f16_tiled_row_size(ne);
+ switch (htp_mm_get_hvx_act_format(weight_type)) {
+ case HTP_MM_HVX_ACT_F16: return htp_mm_f16_tiled_row_size(ne);
+ case HTP_MM_HVX_ACT_Q8_1_S16:
+ case HTP_MM_HVX_ACT_Q8_1: return htp_mm_q8_1_tiled_row_size(ne);
+ case HTP_MM_HVX_ACT_Q8_0: return htp_mm_q8_0_tiled_row_size(ne);
}
- return htp_mm_weight_has_offset(weight_type) ? htp_mm_q8_1_tiled_row_size(ne) : htp_mm_q8_0_tiled_row_size(ne);
+ return htp_mm_q8_0_tiled_row_size(ne);
}
static inline size_t htp_mm_act_raw_row_size(uint32_t ne, size_t act_elem_size) {