Commit 9a811bab3 for llama.cpp
commit 9a811bab3c4242226acfcdff4b75e64d590a1e0f
Author: Mark Todorovich <mtodorov@qti.qualcomm.com>
Date: Thu Dec 3 08:40:21 2026 -0800
hexagon: conv2d_dw 5x5 performance improvement (#32979)
* hexagon: conv2d-dw 5x5 kernel dbl buf DMA
Co-authored-by: Todor Boinovski <todorb@qti.qualcomm.com>
* hexagon: update conv2d to support f16/f32 mixed types, proper f16 lowering, use generic dma_pipeline
---------
Co-authored-by: Todor Boinovski <todorb@qti.qualcomm.com>
Co-authored-by: Max Krasnyansky <maxk@qti.qualcomm.com>
diff --git a/ggml/src/ggml-hexagon/htp-kparams.cpp b/ggml/src/ggml-hexagon/htp-kparams.cpp
index 85c359520..7762b9276 100644
--- a/ggml/src/ggml-hexagon/htp-kparams.cpp
+++ b/ggml/src/ggml-hexagon/htp-kparams.cpp
@@ -5352,6 +5352,43 @@ static bool htp_kparams_solve_conv2d_dw(
const struct ggml_tensor * src1 = op->src[1];
const struct ggml_tensor * dst = op;
+ // 5x5 s=1 p=2 d=1 depthwise: DMA/HVX kernel (supports f16/f32 weights, inputs, and outputs).
+ if ((src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_F32) &&
+ (src1->type == GGML_TYPE_F16 || src1->type == GGML_TYPE_F32) &&
+ (dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32) &&
+ src0->ne[0] == 5 && src0->ne[1] == 5 && src0->ne[2] == 1 && src0->ne[3] == src1->ne[2] &&
+ ggml_are_same_shape(dst, src1) &&
+ ggml_get_op_params_i32(op, 0) == 1 && ggml_get_op_params_i32(op, 1) == 1 &&
+ ggml_get_op_params_i32(op, 2) == 2 && ggml_get_op_params_i32(op, 3) == 2 &&
+ ggml_get_op_params_i32(op, 4) == 1 && ggml_get_op_params_i32(op, 5) == 1 &&
+ ggml_is_contiguous(src0) && ggml_is_contiguous(src1) && ggml_is_contiguous(dst)) {
+ const uint32_t n_threads = htp_session_n_hvx(sess);
+ const size_t vtcm_sz = htp_session_vtcm_size(sess);
+ const size_t src_es = ggml_type_size(src1->type);
+ const size_t dst_es = ggml_type_size(dst->type);
+ const uint64_t planes = (uint64_t) dst->ne[2] * dst->ne[3];
+ if (planes <= UINT32_MAX && src1->ne[0] <= UINT32_MAX - 4 && src1->ne[1] <= UINT32_MAX - 4 &&
+ (uint64_t) (src1->ne[0] + 4) * src_es <= vtcm_sz &&
+ (uint64_t) (src1->ne[1] + 4) * (uint64_t) (src1->ne[0] + 4) * src_es <= vtcm_sz) {
+ uint32_t nth = (uint32_t) (std::min)(planes, (uint64_t) (std::min)(n_threads, 255u));
+ for (; nth > 0; --nth) {
+ struct htp_conv2d_dw5_vtcm_layout layout;
+ htp_conv2d_dw5_vtcm_layout_build(&layout, (uint32_t) src1->ne[0], (uint32_t) src1->ne[1],
+ src_es, dst_es, nth);
+ if (layout.total_bytes <= UINT32_MAX && layout.total_bytes <= vtcm_sz) {
+ kparams->kernel_type = HTP_CONV2D_DW_KERNEL_WHCN_5X5_DMA;
+ kparams->flags = (src0->type == GGML_TYPE_F16 ? HTP_CONV2D_DW_FLAG_F16 : 0);
+ kparams->n_hvx = (uint8_t) nth;
+ kparams->total_tasks = (uint32_t) planes;
+ kparams->vtcm_size = (uint32_t) layout.total_bytes;
+ kparams->w = (uint32_t) src1->ne[0];
+ kparams->h = (uint32_t) src1->ne[1];
+ return true;
+ }
+ }
+ }
+ }
+
if ((src0->type != GGML_TYPE_F16 && src0->type != GGML_TYPE_F32) ||
src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
return false;
diff --git a/ggml/src/ggml-hexagon/htp/CMakeLists.txt b/ggml/src/ggml-hexagon/htp/CMakeLists.txt
index 2da061973..5d85e4f98 100644
--- a/ggml/src/ggml-hexagon/htp/CMakeLists.txt
+++ b/ggml/src/ggml-hexagon/htp/CMakeLists.txt
@@ -52,6 +52,7 @@ add_library(${HTP_LIB} SHARED
argsort-ops.c
im2col-ops.c
conv2d-ops.c
+ conv2d-dw5-ops.c
roll-ops.c
pool-ops.c
upscale-ops.c
diff --git a/ggml/src/ggml-hexagon/htp/conv2d-dw5-ops.c b/ggml/src/ggml-hexagon/htp/conv2d-dw5-ops.c
new file mode 100644
index 000000000..d5af8eb0b
--- /dev/null
+++ b/ggml/src/ggml-hexagon/htp/conv2d-dw5-ops.c
@@ -0,0 +1,295 @@
+// HVX + DMA kernel for 5x5 stride-1 pad-2 dilation-1 depthwise conv (f16/f32 weights, inputs, and outputs).
+
+#include "conv2d-ops.h"
+#include "dma-pipeline.h"
+#include "hex-common.h"
+#include "htp-ctx.h"
+#include "htp-ops.h"
+#include "htp-tensor.h"
+#include "hvx-base.h"
+#include "hvx-copy.h"
+
+#include <HAP_farf.h>
+
+typedef void (*conv_2d_dw5_prep_weights_fn)(const void * restrict, HVX_Vector * restrict);
+typedef void (*conv_2d_dw5_plane_fn)(const uint8_t * restrict, const HVX_Vector * restrict, void * restrict, uint32_t, uint32_t, uint32_t);
+
+struct conv_2d_dw5_hvx_context {
+ struct htp_ops_context * octx;
+ struct htp_conv2d_dw5_vtcm_layout layout;
+ conv_2d_dw5_prep_weights_fn prep_weights_fn;
+ conv_2d_dw5_plane_fn plane_fn;
+ uint32_t weight_bytes;
+ uint32_t halo_offset;
+ uint32_t plane_start;
+ uint32_t plane_count;
+ uint32_t planes_per_thread;
+};
+
+// Zero both source slots once per thread; DMA only writes the interior, so the 2-pixel halo stays zero.
+static __attribute__((noinline)) void conv_2d_dw5_clear(uint8_t * restrict data, uint32_t bytes) {
+ const HVX_Vector zero = Q6_V_vzero();
+ for (uint32_t offset = 0; offset < bytes; offset += VLEN) {
+ *(HVX_Vector *) (data + offset) = zero;
+ }
+}
+
+static inline __attribute__((always_inline)) void conv_2d_dw5_splat_weights(HVX_Vector w, HVX_Vector * restrict wsplat) {
+ for (uint32_t t = 0; t < 25; ++t) {
+ wsplat[t] = hvx_vec_repl4(Q6_V_vror_VR(w, t * sizeof(float)));
+ }
+}
+
+static __attribute__((noinline)) void conv_2d_dw5_prep_weights_f16(const void * restrict wsrc, HVX_Vector * restrict wsplat) {
+ conv_2d_dw5_splat_weights(Q6_V_lo_W(hvx_vec_f16_to_f32(*(const HVX_Vector *) wsrc)), wsplat);
+}
+
+static __attribute__((noinline)) void conv_2d_dw5_prep_weights_f32(const void * restrict wsrc, HVX_Vector * restrict wsplat) {
+ conv_2d_dw5_splat_weights(*(const HVX_Vector *) wsrc, wsplat);
+}
+
+// One output plane. src rows are 128B aligned with the 2-pixel left halo at the row start, so the five kx taps
+// of a 32-wide output vector are valigns of two aligned loads.
+static inline __attribute__((always_inline)) void conv_2d_dw5_plane_impl(const uint8_t * restrict src,
+ const HVX_Vector * restrict wsplat,
+ void * restrict dst,
+ uint32_t src_row_stride,
+ uint32_t ow,
+ uint32_t oh,
+ const bool src_f16,
+ const bool dst_f16) {
+ for (uint32_t oy = 0; oy < oh; ++oy) {
+ for (uint32_t ox = 0; ox < ow; ox += 2 * VLEN_FP32) {
+ HVX_Vector acc0 = Q6_V_vzero();
+ HVX_Vector acc1 = Q6_V_vzero();
+
+ for (uint32_t ky = 0; ky < 5; ++ky) {
+ const HVX_Vector * w = wsplat + ky * 5;
+ HVX_Vector v0, v1, v2;
+ if (src_f16) {
+ const HVX_Vector * row = (const HVX_Vector *) (src + (oy + ky) * src_row_stride + ox * sizeof(__fp16));
+ const HVX_VectorPair p0 = hvx_vec_f16_to_f32(row[0]);
+ v0 = Q6_V_lo_W(p0);
+ v1 = Q6_V_hi_W(p0);
+ v2 = Q6_V_lo_W(hvx_vec_f16_to_f32(row[1]));
+ } else {
+ const HVX_Vector * row = (const HVX_Vector *) (src + (oy + ky) * src_row_stride + ox * sizeof(float));
+ v0 = row[0];
+ v1 = row[1];
+ v2 = row[2];
+ }
+
+ const HVX_Vector a01 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(v0, w[0]),
+ Q6_Vqf32_vmpy_VsfVsf(Q6_V_valign_VVR(v1, v0, 4), w[1]));
+ const HVX_Vector a23 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_valign_VVR(v1, v0, 8), w[2]),
+ Q6_Vqf32_vmpy_VsfVsf(Q6_V_valign_VVR(v1, v0, 12), w[3]));
+ const HVX_Vector a4 = Q6_Vqf32_vadd_Vqf32Vqf32(acc0, Q6_Vqf32_vmpy_VsfVsf(Q6_V_valign_VVR(v1, v0, 16), w[4]));
+ acc0 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(a01, a23), a4);
+
+ const HVX_Vector b01 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(v1, w[0]),
+ Q6_Vqf32_vmpy_VsfVsf(Q6_V_valign_VVR(v2, v1, 4), w[1]));
+ const HVX_Vector b23 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vmpy_VsfVsf(Q6_V_valign_VVR(v2, v1, 8), w[2]),
+ Q6_Vqf32_vmpy_VsfVsf(Q6_V_valign_VVR(v2, v1, 12), w[3]));
+ const HVX_Vector b4 = Q6_Vqf32_vadd_Vqf32Vqf32(acc1, Q6_Vqf32_vmpy_VsfVsf(Q6_V_valign_VVR(v2, v1, 16), w[4]));
+ acc1 = Q6_Vqf32_vadd_Vqf32Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(b01, b23), b4);
+ }
+
+ const uint32_t rem = ow - ox;
+ if (dst_f16) {
+ __fp16 * out = (__fp16 *) dst + oy * ow + ox;
+ const HVX_Vector out_f16 = Q6_Vh_vdeal_Vh(Q6_Vhf_equals_Wqf32(Q6_W_vcombine_VV(acc1, acc0)));
+ hvx_vec_store_u(out, MIN(rem, VLEN_FP16) * sizeof(__fp16), out_f16);
+ } else {
+ float * out = (float *) dst + oy * ow + ox;
+ hvx_vec_store_u(out, MIN(rem, VLEN_FP32) * sizeof(float), Q6_Vsf_equals_Vqf32(acc0));
+ if (rem > VLEN_FP32) {
+ hvx_vec_store_u(out + VLEN_FP32, MIN(rem - VLEN_FP32, VLEN_FP32) * sizeof(float),
+ Q6_Vsf_equals_Vqf32(acc1));
+ }
+ }
+ }
+ }
+}
+
+static __attribute__((noinline)) void conv_2d_dw5_plane_f32_f32(const uint8_t * restrict src, const HVX_Vector * restrict wsplat, void * restrict dst, uint32_t src_row_stride, uint32_t ow, uint32_t oh) {
+ conv_2d_dw5_plane_impl(src, wsplat, dst, src_row_stride, ow, oh, false, false);
+}
+
+static __attribute__((noinline)) void conv_2d_dw5_plane_f32_f16(const uint8_t * restrict src, const HVX_Vector * restrict wsplat, void * restrict dst, uint32_t src_row_stride, uint32_t ow, uint32_t oh) {
+ conv_2d_dw5_plane_impl(src, wsplat, dst, src_row_stride, ow, oh, false, true);
+}
+
+static __attribute__((noinline)) void conv_2d_dw5_plane_f16_f32(const uint8_t * restrict src, const HVX_Vector * restrict wsplat, void * restrict dst, uint32_t src_row_stride, uint32_t ow, uint32_t oh) {
+ conv_2d_dw5_plane_impl(src, wsplat, dst, src_row_stride, ow, oh, true, false);
+}
+
+static __attribute__((noinline)) void conv_2d_dw5_plane_f16_f16(const uint8_t * restrict src, const HVX_Vector * restrict wsplat, void * restrict dst, uint32_t src_row_stride, uint32_t ow, uint32_t oh) {
+ conv_2d_dw5_plane_impl(src, wsplat, dst, src_row_stride, ow, oh, true, true);
+}
+
+struct conv_2d_dw5_pipe_state {
+ dma_addr_t w_data;
+ dma_addr_t in_data;
+ dma_addr_t out_data;
+ HVX_Vector * wsplat;
+ conv_2d_dw5_prep_weights_fn prep_weights_fn;
+ conv_2d_dw5_plane_fn plane_fn;
+ uint32_t plane;
+ uint32_t last;
+ uint32_t c_next;
+ uint32_t channels;
+ uint32_t ih;
+ uint32_t ow;
+ uint32_t oh;
+ uint32_t stride;
+ uint32_t in_row;
+ uint32_t in_plane;
+ uint32_t out_row;
+ uint32_t out_plane;
+ uint32_t w_plane;
+ uint32_t weight_bytes;
+ uint32_t halo_offset;
+};
+
+static bool conv_2d_dw5_next_task(void * ctx, struct dma_pipe_task * task) {
+ struct conv_2d_dw5_pipe_state * s = (struct conv_2d_dw5_pipe_state *) ctx;
+ if (s->plane >= s->last) {
+ return false;
+ }
+ task->row = s->plane;
+ task->aux = s->c_next;
+ s->plane++;
+ s->c_next = (s->c_next + 1 == s->channels) ? 0 : s->c_next + 1;
+ return true;
+}
+
+static void conv_2d_dw5_push_in(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * in0_spad, void * in1_spad) {
+ const struct conv_2d_dw5_pipe_state * s = (const struct conv_2d_dw5_pipe_state *) ctx;
+ dma_queue_push(dma_q, dma_make_data(in0_spad, s->w_data + (dma_addr_t) task->aux * s->w_plane),
+ VLEN, s->w_plane, s->weight_bytes, 1);
+ dma_queue_push(dma_q, dma_make_data(in1_spad, s->in_data + (dma_addr_t) task->row * s->in_plane),
+ s->stride, s->in_row, s->in_row, s->ih);
+}
+
+static void conv_2d_dw5_compute(void * ctx, const struct dma_pipe_task * task, void * out_spad, void * in0_spad, void * in1_spad) {
+ (void) task;
+ const struct conv_2d_dw5_pipe_state * s = (const struct conv_2d_dw5_pipe_state *) ctx;
+ s->prep_weights_fn(in0_spad, s->wsplat);
+ s->plane_fn((const uint8_t *) in1_spad - s->halo_offset, s->wsplat, out_spad, s->stride, s->ow, s->oh);
+}
+
+static void conv_2d_dw5_push_out(dma_queue * dma_q, void * ctx, const struct dma_pipe_task * task, void * out_spad) {
+ const struct conv_2d_dw5_pipe_state * s = (const struct conv_2d_dw5_pipe_state *) ctx;
+ dma_queue_push(dma_q, dma_make_data(s->out_data + (dma_addr_t) task->row * s->out_plane, out_spad),
+ s->out_row, s->out_row, s->out_row, s->oh);
+}
+
+// Planes are processed in memory order (plane = n * C + c). Each plane is three DMAs: weights in, src in, dst out.
+// Two buffers per stream: plane i+1 is in flight while plane i is computed.
+static void conv_2d_dw5_hvx_thread(const htp_thread_t * th, unsigned int nth, unsigned int ith, void * data) {
+ const struct conv_2d_dw5_hvx_context * ctx = (const struct conv_2d_dw5_hvx_context *) data;
+ const struct htp_ops_context * octx = ctx->octx;
+ const struct htp_conv2d_dw5_vtcm_layout * L = &ctx->layout;
+ const struct htp_tensor * weight = octx->src[0];
+ const struct htp_tensor * input = octx->src[1];
+ const struct htp_tensor * output = octx->dst;
+
+ const uint32_t first = ctx->plane_start + ith * ctx->planes_per_thread;
+ const uint32_t last = MIN(first + ctx->planes_per_thread, ctx->plane_start + ctx->plane_count);
+ if (first >= last) {
+ return;
+ }
+
+ uint8_t * vtcm = octx->ctx->vtcm_base;
+ uint8_t * src_base = VTCM_LAYOUT_PTR(uint8_t, vtcm, L->off_src) + ith * 2 * L->src_slot_bytes;
+ uint8_t * weight_base = VTCM_LAYOUT_PTR(uint8_t, vtcm, L->off_weight) + ith * HTP_CONV2D_DW5_W_BYTES;
+ uint8_t * dst_base = VTCM_LAYOUT_PTR(uint8_t, vtcm, L->off_dst) + ith * 2 * L->dst_slot_bytes;
+ const uint32_t halo_offset = ctx->halo_offset;
+
+ void * in0_buf[2] = { weight_base, weight_base + VLEN };
+ void * in1_buf[2] = { src_base + halo_offset, src_base + L->src_slot_bytes + halo_offset };
+ void * out_buf[2] = { dst_base, dst_base + L->dst_slot_bytes };
+
+ conv_2d_dw5_clear(src_base, 2 * L->src_slot_bytes);
+
+ struct conv_2d_dw5_pipe_state s = {
+ .w_data = weight->data,
+ .in_data = input->data,
+ .out_data = output->data,
+ .wsplat = (HVX_Vector *) (weight_base + 2 * VLEN),
+ .prep_weights_fn = ctx->prep_weights_fn,
+ .plane_fn = ctx->plane_fn,
+ .plane = first,
+ .last = last,
+ .c_next = first % input->ne[2],
+ .channels = input->ne[2],
+ .ih = input->ne[1],
+ .ow = output->ne[0],
+ .oh = output->ne[1],
+ .stride = L->src_row_stride,
+ .in_row = input->nb[1],
+ .in_plane = input->nb[2],
+ .out_row = output->nb[1],
+ .out_plane = output->nb[2],
+ .w_plane = weight->nb[3],
+ .weight_bytes = ctx->weight_bytes,
+ .halo_offset = halo_offset,
+ };
+
+ dma_pipe_io2_run(th->dma_q, th->trace, &s, in0_buf, in1_buf, out_buf,
+ conv_2d_dw5_next_task, conv_2d_dw5_push_in, conv_2d_dw5_compute, conv_2d_dw5_push_out);
+
+ FARF(HIGH, "conv2d-dw-hvx-dma-5x5 %d/%d: %ux%ux%ux%u -> %ux%ux%ux%u (%u:%u)\n", ith, nth, input->ne[0],
+ input->ne[1], input->ne[2], input->ne[3], output->ne[0], output->ne[1], output->ne[2], output->ne[3], first,
+ last);
+}
+
+int op_conv_2d_dw5(struct htp_ops_context * octx) {
+ // Tensor shapes, types and VTCM sizing were validated by the host (htp_kparams_solve_conv2d_dw).
+ const struct htp_conv2d_dw_kernel_params * kp = (const struct htp_conv2d_dw_kernel_params *) octx->kernel_params;
+
+ uint32_t plane_start = 0;
+ uint32_t plane_count = kp->total_tasks;
+
+ if (octx->ctx->mdev.count > 1) {
+ const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(
+ plane_count, 1, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div);
+ plane_start = range.start;
+ plane_count = range.count;
+ }
+ if (plane_count == 0) {
+ return HTP_STATUS_OK;
+ }
+
+ static const conv_2d_dw5_plane_fn plane_fns[2][2] = {
+ { conv_2d_dw5_plane_f32_f32, conv_2d_dw5_plane_f32_f16 },
+ { conv_2d_dw5_plane_f16_f32, conv_2d_dw5_plane_f16_f16 },
+ };
+
+ const bool w_f16 = (kp->flags & HTP_CONV2D_DW_FLAG_F16) != 0;
+ const bool src_f16 = (octx->src[1]->type == HTP_TYPE_F16);
+ const bool dst_f16 = (octx->dst->type == HTP_TYPE_F16);
+ const size_t src_es = src_f16 ? sizeof(__fp16) : sizeof(float);
+ const size_t dst_es = dst_f16 ? sizeof(__fp16) : sizeof(float);
+ const uint32_t weight_bytes = 25 * (w_f16 ? sizeof(__fp16) : sizeof(float));
+
+ struct htp_conv2d_dw5_vtcm_layout layout;
+ htp_conv2d_dw5_vtcm_layout_build(&layout, kp->w, kp->h, src_es, dst_es, kp->n_hvx);
+
+ const uint32_t n_threads = MIN(kp->n_hvx, plane_count);
+ const uint32_t planes_per_thread = (plane_count + n_threads - 1) / n_threads;
+ struct conv_2d_dw5_hvx_context ctx = {
+ .octx = octx,
+ .layout = layout,
+ .prep_weights_fn = w_f16 ? conv_2d_dw5_prep_weights_f16 : conv_2d_dw5_prep_weights_f32,
+ .plane_fn = plane_fns[src_f16][dst_f16],
+ .weight_bytes = weight_bytes,
+ .halo_offset = 2 * layout.src_row_stride + 2 * src_es,
+ .plane_start = plane_start,
+ .plane_count = plane_count,
+ .planes_per_thread = planes_per_thread,
+ };
+
+ work_queue_run(octx->ctx->hvx_queue, conv_2d_dw5_hvx_thread, &ctx, n_threads);
+ return HTP_STATUS_OK;
+}
diff --git a/ggml/src/ggml-hexagon/htp/conv2d-ops.c b/ggml/src/ggml-hexagon/htp/conv2d-ops.c
index 7de19582f..b2b5a6c2e 100644
--- a/ggml/src/ggml-hexagon/htp/conv2d-ops.c
+++ b/ggml/src/ggml-hexagon/htp/conv2d-ops.c
@@ -3217,6 +3217,10 @@ int op_conv_2d_dw(struct htp_ops_context * octx) {
return HTP_STATUS_NO_SUPPORT;
}
+ if (kp->kernel_type == HTP_CONV2D_DW_KERNEL_WHCN_5X5_DMA) {
+ return op_conv_2d_dw5(octx);
+ }
+
const uint32_t pad_rows = (kp->pad_top_stride > 0) ? 4 : 0;
const uint32_t max_ih_chunk = MIN(kp->h, (kp->oh_chunk - 1) * (uint32_t) kp->s1 + (kp->kh - 1) * (uint32_t) kp->d1 + 1);
const uint32_t max_src_bytes = (max_ih_chunk + pad_rows) * kp->row_src_bytes + 128;
diff --git a/ggml/src/ggml-hexagon/htp/conv2d-ops.h b/ggml/src/ggml-hexagon/htp/conv2d-ops.h
index 054a9bd9f..473d0fe6d 100644
--- a/ggml/src/ggml-hexagon/htp/conv2d-ops.h
+++ b/ggml/src/ggml-hexagon/htp/conv2d-ops.h
@@ -97,6 +97,7 @@ enum htp_conv2d_dw_kernel_type {
HTP_CONV2D_DW_KERNEL_WHCN_S2_K3X3_W32, // WHCN w=32->ow=16, 3x3 s=2 p=1 d=1 (2 rows per vector)
HTP_CONV2D_DW_KERNEL_WHCN_S2_K3X3_W64, // WHCN w=64->ow=32, 3x3 s=2 p=1 d=1 (1 vector rolling kh)
HTP_CONV2D_DW_KERNEL_WHCN_S2_K3X3, // WHCN aligned w%32==0 (w>=96), 3x3 s=2 p=1 d=1
+ HTP_CONV2D_DW_KERNEL_WHCN_5X5_DMA, // WHCN 5x5 s=1 p=2 d=1, DMA row halo
HTP_CONV2D_DW_KERNEL_COUNT,
};
@@ -105,6 +106,11 @@ enum htp_conv2d_dw_flags {
HTP_CONV2D_DW_FLAG_F16 = (1u << 1),
};
+// WHCN_5X5_DMA VTCM layout shared by host and NPU: per-thread weight area (2 f16 slots + 25 f32 splats, rounded
+// up) and the source slot tail for the valign over-read past the last row.
+#define HTP_CONV2D_DW5_W_BYTES (32 * 128)
+#define HTP_CONV2D_DW5_SRC_TAIL (3 * 128)
+
struct htp_conv2d_dw_kernel_params {
uint8_t kernel_type;
uint8_t flags; // bitmask of enum htp_conv2d_dw_flags
@@ -170,6 +176,35 @@ static inline void htp_conv2d_dw_vtcm_layout_build(
L->total_bytes = off;
}
+struct htp_conv2d_dw5_vtcm_layout {
+ size_t total_bytes;
+ size_t off_src;
+ size_t off_weight;
+ size_t off_dst;
+ size_t src_row_stride;
+ size_t src_slot_bytes;
+ size_t dst_slot_bytes;
+};
+
+static inline void htp_conv2d_dw5_vtcm_layout_build(
+ struct htp_conv2d_dw5_vtcm_layout * L,
+ uint32_t w,
+ uint32_t h,
+ size_t src_es,
+ size_t dst_es,
+ uint32_t n_hvx
+) {
+ L->src_row_stride = hex_align_up((size_t) (w + 4) * src_es, 128);
+ L->src_slot_bytes = hex_align_up((size_t) (h + 4) * L->src_row_stride + HTP_CONV2D_DW5_SRC_TAIL, 128);
+ L->dst_slot_bytes = hex_align_up((size_t) w * h * dst_es, 128);
+
+ size_t off = 0;
+ VTCM_LAYOUT_ALLOC(off, off_src, (size_t) 2 * n_hvx * L->src_slot_bytes);
+ VTCM_LAYOUT_ALLOC(off, off_weight, (size_t) n_hvx * HTP_CONV2D_DW5_W_BYTES);
+ VTCM_LAYOUT_ALLOC(off, off_dst, (size_t) 2 * n_hvx * L->dst_slot_bytes);
+ L->total_bytes = off;
+}
+
struct htp_conv2d_vtcm_layout {
size_t total_bytes;
size_t off_scales;
diff --git a/ggml/src/ggml-hexagon/htp/htp-ctx.h b/ggml/src/ggml-hexagon/htp/htp-ctx.h
index b514f633e..7ceb77a72 100644
--- a/ggml/src/ggml-hexagon/htp/htp-ctx.h
+++ b/ggml/src/ggml-hexagon/htp/htp-ctx.h
@@ -185,6 +185,7 @@ int op_im2col(struct htp_ops_context * octx);
int op_im2col_3d(struct htp_ops_context * octx);
int op_conv_2d(struct htp_ops_context * octx);
int op_conv_2d_dw(struct htp_ops_context * octx);
+int op_conv_2d_dw5(struct htp_ops_context * octx);
int op_pool_2d(struct htp_ops_context * octx);
int op_pool_1d(struct htp_ops_context * octx);
int op_allreduce(struct htp_ops_context * octx);
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 86701eb98..520f31470 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -12973,6 +12973,13 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_conv_2d_dw({17, 34, 9, 1}, {3, 3, 1, 9}, GGML_TYPE_F16, 1, 0, 1, false));
test_cases.emplace_back(new test_conv_2d_dw({17, 34, 9, 1}, {3, 3, 1, 9}, GGML_TYPE_F16, 1, 0, 1, true));
+ // 5x5 s=1 p=2 d=1 (dedicated HTP kernel)
+ test_cases.emplace_back(new test_conv_2d_dw({32, 24, 16, 1}, {5, 5, 1, 16}, GGML_TYPE_F16, 1, 2, 1, false));
+ test_cases.emplace_back(new test_conv_2d_dw({33, 17, 9, 2}, {5, 5, 1, 9}, GGML_TYPE_F16, 1, 2, 1, false));
+ // batched, fewer channels than HVX threads
+ test_cases.emplace_back(new test_conv_2d_dw({16, 16, 2, 4}, {5, 5, 1, 2}, GGML_TYPE_F16, 1, 2, 1, false));
+ test_cases.emplace_back(new test_conv_2d_dw({16, 16, 1, 4}, {5, 5, 1, 1}, GGML_TYPE_F16, 1, 2, 1, false));
+ test_cases.emplace_back(new test_conv_2d_dw({104, 80, 32, 1}, {5, 5, 1, 32}, GGML_TYPE_F16, 1, 2, 1, false));
test_cases.emplace_back(new test_conv_2d_dw({32, 8, 64, 1}, {3, 3, 1, 64}, GGML_TYPE_F16, 2, 1, 1, false));
test_cases.emplace_back(new test_conv_2d_dw({32, 8, 64, 1}, {3, 3, 1, 64}, GGML_TYPE_F16, 2, 1, 1, true));
@@ -16496,6 +16503,8 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
test_cases.emplace_back(new test_conv_2d_dw({112, 112, 32, 1}, {3, 3, 1, 32}, GGML_TYPE_F32, 1, 1, 1, true));
test_cases.emplace_back(new test_conv_2d_dw({56, 56, 128, 1}, {5, 5, 1, 128}, GGML_TYPE_F32, 2, 2, 1, false));
test_cases.emplace_back(new test_conv_2d_dw({56, 56, 128, 1}, {5, 5, 1, 128}, GGML_TYPE_F32, 2, 2, 1, true));
+ test_cases.emplace_back(new test_conv_2d_dw({56, 56, 128, 1}, {5, 5, 1, 128}, GGML_TYPE_F16, 1, 2, 1, false));
+ test_cases.emplace_back(new test_conv_2d_dw({112, 112, 64, 1}, {5, 5, 1, 64}, GGML_TYPE_F16, 1, 2, 1, false));
for (ggml_type kernel_type : {GGML_TYPE_F32, GGML_TYPE_F16}) {
test_cases.emplace_back(new test_conv_transpose_2d({256, 256, 256, 1}, {3, 3, 16, 256}, 1, kernel_type));