Compare commits

..
2 Commits
Author SHA1 Message Date
Mark TodorovichandMax Krasnyansky 72a780e6a6 hexagon: CONV_2D/3D Cin chunking and cost-based tile solver (#32989)
* hexagon: CONV_2D/3D Cin chunking and cost-based tile solver

- stage input channels in chunks
- stage per (channel, kd) channel stride > 24-bit 2D DMA stride limit
- split weight packing into work items
- replace first-fit tile selection with a solver
- use fastdiv for the dilation division in the per-channel stage path

* hexagon: conv-2d/3d rebase and fixes (guards for corner cases, etc)

* hexagon: add mixed f16/f32 inputs, improve spad allocation and cleanup kparams

* hexagon: conv-2d remove replace all divs and further cleanup kparams

---------

Co-authored-by: Max Krasnyansky <maxk@qti.qualcomm.com>
2026-12-03 21:54:31 -08:00
Mark TodorovichandMax Krasnyansky 59b716523f hexagon: move conv2d_dw kernels to a separate file (#32982)
* hexagon: move conv2d-dw kernels to conv2d-dw-ops.c

* hexagon: split conv2d-dw headers as well

* hexagon: add support for f16/f32 mixed types in conv2d

---------

Co-authored-by: Max Krasnyansky <maxk@qti.qualcomm.com>
2026-12-03 13:43:35 -08:00
10 changed files with 2096 additions and 1600 deletions
+191 -138
View File
@@ -4833,16 +4833,18 @@ static bool htp_kparams_conv2d_win(
const uint32_t OH = dst->ne[1];
const uint32_t N = dst->ne[3];
const size_t es = ggml_type_size(src0->type);
const size_t x_es = ggml_type_size(src1->type);
const size_t y_es = ggml_type_size(dst->type);
if (s0 != 1 || s1 != 1 || p0 < 0 || p1 < 0 || dst->nb[0] != sizeof(float)) {
if (s0 != 1 || s1 != 1 || p0 < 0 || p1 < 0 || dst->nb[0] != y_es) {
return false;
}
if (dst->nb[1] < OW * sizeof(float) || (Cout > 1 && dst->nb[2] < OW * sizeof(float))) {
if (dst->nb[1] < OW * y_es || (Cout > 1 && dst->nb[2] < OW * y_es)) {
return false;
}
const bool whcn = src1->nb[0] == sizeof(float) && src1->nb[1] >= IW * sizeof(float) && (Cin == 1 || src1->nb[2] >= IH * src1->nb[1]);
const bool cwhn = !whcn && src1->nb[2] == sizeof(float) && src1->nb[0] == Cin * sizeof(float) && src1->nb[1] >= IW * src1->nb[0];
const bool whcn = src1->nb[0] == x_es && src1->nb[1] >= IW * x_es && (Cin == 1 || src1->nb[2] >= IH * src1->nb[1]);
const bool cwhn = !whcn && src1->nb[2] == x_es && src1->nb[0] == Cin * x_es && src1->nb[1] >= IW * src1->nb[0];
if (!whcn && !cwhn) {
return false;
}
@@ -4865,11 +4867,11 @@ static bool htp_kparams_conv2d_win(
const uint32_t IHS = IH + 2 * p1;
const uint32_t n_wt = fold ? KH : T;
const uint32_t moff = (KH - 1) * d1 * SR + (fold ? 0 : (KW - 1) * d0);
const uint32_t halo = moff / 32 + 1;
const uint32_t n_rt = (Cout + 31) / 32;
const uint64_t n_q = (uint64_t) (N * IHS - (IHS - OH) - 1) * SR + OW;
const uint32_t nth = kp->n_hvx;
const size_t tile = 2048;
const uint32_t halo = moff / 32 + 1;
const uint32_t n_rt = (Cout + 31) / 32;
const uint64_t n_q = (uint64_t) (N * IHS - (IHS - OH) - 1) * SR + OW;
const uint32_t max_hvx = kp->n_hvx;
const size_t tile = 2048;
if ((uint64_t) N * IHS * SR + moff + 64 >= (1ull << 31)) {
return false;
@@ -4916,7 +4918,7 @@ static bool htp_kparams_conv2d_win(
rt_block = (rt_block + 1) / 2;
}
size_t stage = 0, thr = 0, act = 0, out = 0, w_size = 0, total = 0;
size_t stage = 0, spad_per_hvx = 0, act = 0, out = 0, w_size = 0, total = 0;
uint32_t n_bc = 0;
while (true) {
n_bc = ct + halo;
@@ -4926,12 +4928,14 @@ static bool htp_kparams_conv2d_win(
stage = (size_t) Cin * ((seg_b * 32 + OW - 1) / OW + 1) * IW * sizeof(float) + 256;
}
stage = hex_round_up(stage, 128);
thr = hex_round_up((std::max)(2 * stage + (size_t) HTP_CONV2D_WIN_OBUF * 32 * seg_t * 32 * sizeof(float), wscr), tile);
struct htp_conv2d_win_spad_layout sl;
htp_conv2d_win_spad_layout_build(&sl, stage, seg_t, wscr);
spad_per_hvx = sl.total_bytes;
w_size = (size_t) rt_block * n_wt * n_cc * tile;
act = (size_t) n_cc * n_bc * tile;
out = (size_t) rt_block * ct * tile;
struct htp_conv2d_vtcm_layout L;
htp_conv2d_vtcm_layout_build(&L, rt_block, w_size, act, out, thr, nth);
htp_conv2d_vtcm_layout_build(&L, rt_block, w_size, act, out, spad_per_hvx, max_hvx);
total = L.total_bytes;
if (total <= htp_session_vtcm_size(sess)) {
break;
@@ -4950,9 +4954,9 @@ static bool htp_kparams_conv2d_win(
}
}
uint32_t n_hvx = nth;
uint32_t n_hvx = max_hvx;
// Keep the fold schedule, where output traffic can dominate input packing.
if (!fold && rt_block < n_rt && ct > 0 && nth > 0) {
if (!fold && rt_block < n_rt) {
const uint32_t old_rt = rt_block;
const uint32_t ct_max = (std::min)((std::min)((std::max)(4 * halo, (n_tiles + 3) / 4), 512u), n_tiles);
const size_t budget = htp_session_vtcm_size(sess);
@@ -4960,33 +4964,32 @@ static bool htp_kparams_conv2d_win(
const size_t c_chunk = 8;
const uint32_t tail = n_tiles % ct;
const size_t work =
(size_t) (n_tiles / ct) * ((ct + halo + nth - 1) / nth) + (tail ? (tail + halo + nth - 1) / nth : 0);
(size_t) (n_tiles / ct) * ((ct + halo + max_hvx - 1) / max_hvx) + (tail ? (tail + halo + max_hvx - 1) / max_hvx : 0);
size_t best_cost =
(size_t) ((n_rt + rt_block - 1) / rt_block) * (n_cc * work + c_chunk * (n_tiles / ct + (tail != 0)));
bool found = false;
// Keep output DMA segments. Try larger kernel blocks to avoid rebuilding input croutons.
for (uint32_t rb = n_rt; rb > old_rt && !found; rb = (rb + 1) / 2) {
for (uint32_t nt = nth; nt > 0; nt /= 2) {
for (uint32_t nh = max_hvx; nh > 0; nh /= 2) {
for (uint32_t sb = 8; sb > 0; sb /= 2) {
const size_t input_stage = hex_round_up(cwhn ? (size_t) sb * 32 * Cin * sizeof(float) + 256 : (size_t) n_ch * sb * 32 * sizeof(float), 128);
const size_t scratch = hex_round_up(
(std::max)(2 * input_stage + (size_t) HTP_CONV2D_WIN_OBUF * 32 * seg_t * 32 * sizeof(float),
wscr),
tile);
if (n_cc == 0 || n_wt > budget / tile / n_cc / rb || halo > budget / tile / n_cc ||
scratch > budget / nt) {
struct htp_conv2d_win_spad_layout sl;
htp_conv2d_win_spad_layout_build(&sl, input_stage, seg_t, wscr);
const size_t spad = sl.total_bytes;
if (n_wt > budget / tile / n_cc / rb || halo > budget / tile / n_cc ||
spad > budget / nh) {
continue;
}
const size_t weights = (size_t) rb * n_wt * n_cc * tile;
htp_conv2d_vtcm_layout fixed;
htp_conv2d_vtcm_layout_build(&fixed, rb, weights, (size_t) n_cc * halo * tile, 0, scratch, nt);
htp_conv2d_vtcm_layout_build(&fixed, rb, weights, (size_t) n_cc * halo * tile, 0, spad, nh);
if (fixed.total_bytes >= budget) {
continue;
}
const size_t per_ct = 2 * ((size_t) n_cc + rb) * tile;
const uint32_t cap = (uint32_t) (std::min)((size_t) ct_max, (budget - fixed.total_bytes) / per_ct);
const uint32_t aligned = (cap + halo) / nt * nt;
const uint32_t aligned = (cap + halo) / nh * nh;
const uint32_t candidates[2] = { cap, aligned > halo ? aligned - halo : 0 };
for (uint32_t i = 0; i < 2; i++) {
const uint32_t candidate = candidates[i];
@@ -4997,30 +5000,30 @@ static bool htp_kparams_conv2d_win(
continue;
}
const uint32_t rem = n_tiles % candidate;
const size_t input_work = (size_t) (n_tiles / candidate) * ((candidate + halo + nt - 1) / nt) + (rem ? (rem + halo + nt - 1) / nt : 0);
const size_t input_work = (size_t) (n_tiles / candidate) * ((candidate + halo + nh - 1) / nh) + (rem ? (rem + halo + nh - 1) / nh : 0);
const size_t chunks = n_tiles / candidate + (rem != 0);
const size_t cost = (size_t) ((n_rt + rb - 1) / rb) * (n_cc * input_work + c_chunk * chunks);
if (cost < best_cost) {
const size_t input = (size_t) n_cc * (candidate + halo) * tile;
const size_t output = (size_t) rb * candidate * tile;
htp_conv2d_vtcm_layout L;
htp_conv2d_vtcm_layout_build(&L, rb, weights, input, output, scratch, nt);
htp_conv2d_vtcm_layout_build(&L, rb, weights, input, output, spad, nh);
if (L.total_bytes > budget) {
continue;
}
found = true;
best_cost = cost;
rt_block = rb;
ct = candidate;
seg_b = sb;
n_hvx = nt;
stage = input_stage;
thr = scratch;
act = input;
out = output;
w_size = weights;
total = L.total_bytes;
n_bc = candidate + halo;
found = true;
best_cost = cost;
rt_block = rb;
ct = candidate;
seg_b = sb;
n_hvx = nh;
stage = input_stage;
spad_per_hvx = spad;
act = input;
out = output;
w_size = weights;
total = L.total_bytes;
n_bc = candidate + halo;
}
}
}
@@ -5044,7 +5047,7 @@ static bool htp_kparams_conv2d_win(
kp->ct_chunk = ct;
kp->rt_block = rt_block;
kp->stage_size = stage;
kp->thr_size = thr;
kp->spad_per_hvx = spad_per_hvx;
kp->vtcm_size = total;
kp->window.fold = fold;
kp->window.n_cc = n_cc;
@@ -5053,9 +5056,10 @@ static bool htp_kparams_conv2d_win(
kp->window.ihs = IHS;
kp->window.max_off = moff;
kp->window.n_bc = n_bc;
kp->window.seg_b = seg_b;
kp->window.seg_t = seg_t;
kp->window.w_cs = w_cs;
kp->window.seg_b = (uint16_t) seg_b;
kp->window.seg_t = (uint16_t) seg_t;
kp->window.w_cs = (uint16_t) w_cs;
kp->window.w_n_seg = (uint16_t) (w_rows ? n_cs : n_tg);
kp->window.div_sr = init_fastdiv_values(SR);
kp->window.div_ihs = init_fastdiv_values(IHS);
kp->window.div_kw = init_fastdiv_values(KW);
@@ -5089,7 +5093,7 @@ static bool htp_conv_normalize_geom(const struct ggml_tensor * dst, struct htp_c
if (kh_w < 1 || kh_w > 255 || k_w < 1 || k_w > (1 << 24) || ct_w < 1 || ct_w > UINT32_MAX) {
return false;
}
if (s0 <= 0 || d0 <= 0 || dst->nb[0] != sizeof(float)) {
if (s0 <= 0 || d0 <= 0 || dst->nb[0] != ggml_type_size(dst->type)) {
return false;
}
@@ -5129,8 +5133,9 @@ static bool htp_kparams_conv2d_tiled(
// kernel k order must match the staged input: (cin, kh, kw) for WHCN input, (kh, kw, cin) for CWHN input.
// Kernel is either k-contiguous rows per cout (WHCN only) or cout-contiguous with a uniform k stride.
const bool whcn = g.is3d || src1->nb[0] == sizeof(float);
const bool cwhn = !whcn && src1->nb[2] == sizeof(float) && src1->nb[0] == g.Cin * sizeof(float);
const size_t x_es = ggml_type_size(src1->type);
const bool whcn = g.is3d || src1->nb[0] == x_es;
const bool cwhn = !whcn && src1->nb[2] == x_es && src1->nb[0] == g.Cin * x_es;
const bool co_c = g.Cout == 1 || src0->nb[3] == g.es;
const bool w_rows = g.is3d || (whcn && src0->nb[0] == g.es && (g.KH == 1 || src0->nb[1] == g.KW * g.es) && (g.Cin == 1 || src0->nb[2] == g.KW * g.KH * g.es));
const size_t ks = whcn ? src0->nb[0] : src0->nb[2];
@@ -5144,6 +5149,9 @@ static bool htp_kparams_conv2d_tiled(
return false;
}
kmajor = kmajor && !w_rows;
if (kmajor && ks > UINT32_MAX) {
return false;
}
const uint32_t K = g.KW * g.KH * g.Cin;
const uint32_t kg = !cwhn ? K : g.d0 == 1 ? g.KW * g.Cin : g.Cin;
@@ -5154,77 +5162,140 @@ static bool htp_kparams_conv2d_tiled(
const uint32_t n_rt = (g.Cout + 31) / 32;
const uint32_t n_owt = (g.OW + 31) / 32;
const uint32_t n_ct = g.N * g.OH * n_owt;
const uint32_t wk = (std::min)(k_pad, 512u);
const uint32_t KW = g.KW, KH = g.KH, Cin = g.Cin, s0 = g.s0, d0 = g.d0;
// channel stride over the 24-bit 2D DMA limit: stage one DMA per (channel, kd) instead of the sync per-row fallback
const uint32_t KD = g.is3d ? src0->ne[2] : 1;
const size_t x_nb_c = g.is3d ? src1->nb[3] : src1->nb[2];
const bool stage_pc = whcn && x_nb_c > 0x00FFFFFFu;
const uint32_t cc_max = stage_pc ? (std::max)(1u, 255 / KD) : 32768;
const uint32_t max_hvx = htp_session_n_hvx(sess);
const size_t tile = 2048;
const size_t tile = 2048;
const size_t budget = htp_session_vtcm_size(sess);
// cost model constants in 0.1 ns, fitted to v81 measurements: pack per k tile, DMA per descriptor and per row, per chunk, unpack
const uint64_t c_pack = 710;
const uint64_t c_desc = 885;
const uint64_t c_row = 113;
const uint64_t c_chunk = 75850;
const uint64_t c_unp = 2500;
uint32_t rt_block = n_rt;
uint32_t ct = 0;
uint32_t seg = 0;
uint32_t nth = 0;
uint32_t rt_block = 0, ct = 0, seg = 0, cc = Cin, n_hvx = 0, wk = 0;
uint32_t n_stage = 1;
uint32_t sr = 0;
size_t stage = 0, thr_size = 0;
size_t best_cost = SIZE_MAX;
const size_t wpack = kmajor ? 2 * (size_t) wk * 64 * sizeof(float) + 96 * 128 : 2 * (size_t) 32 * wk * (sizeof(float) + sizeof(uint16_t));
const size_t budget = htp_session_vtcm_size(sess);
// Approximate chunk overhead in patch K-tile work units.
const size_t c_chunk = 8;
size_t stage = 0, spad_per_hvx = 0;
uint64_t best_cost = UINT64_MAX;
// Keep the kernel resident to avoid regenerating the input for each rt block.
// Balance patch work per thread and chunk overhead before reducing rt.
for (uint32_t rb = n_rt; rb > 0 && ct == 0; rb = rb > 1 ? (rb + 1) / 2 : 0) {
for (uint32_t nt = max_hvx; nt > 0; nt /= 2) {
// pick the config with the lowest estimated time among all that fit VTCM
for (uint32_t rb = n_rt; rb > 0; rb = rb > 1 ? (rb + 1) / 2 : 0) {
const uint64_t n_rp = (n_rt + rb - 1) / rb;
for (uint32_t nh = max_hvx; nh > 0; nh /= 2) {
for (uint32_t sg = (std::min)(8u, n_owt); sg > 0; --sg) {
// Keep the unused final stride lane for full-vector loads during packing.
const uint64_t wb = (uint64_t) 32 * sg * g.s0 + (uint64_t) (g.KW - 1) * g.d0;
const uint64_t wb = (uint64_t) 32 * sg * s0 + (uint64_t) (KW - 1) * d0;
if (wb > budget / sizeof(float) || wb > INT32_MAX) {
continue;
}
const uint64_t width = cwhn ? wb * g.Cin : wb;
const uint64_t width = cwhn ? wb * Cin : wb;
if (width > budget / sizeof(float) || width > UINT32_MAX - 63) {
continue;
}
const uint32_t row = hex_round_up((uint32_t) width, cwhn ? 64 : 32);
const uint64_t rows = cwhn ? g.KH : (uint64_t) g.Cin * g.KH;
if (rows > budget / ((uint64_t) row * sizeof(float))) {
continue;
}
const size_t input_stage = cwhn ? (size_t) g.KH * row * sizeof(float) : (size_t) g.Cin * g.KH * row * sizeof(float);
const size_t output_stage = 2 * 32 * (size_t) sg * 32 * sizeof(float);
const size_t transpose = cwhn ? input_stage / 2 + 256 + 64 * 128 : 0;
const size_t scratch = hex_round_up((std::max)(input_stage + output_stage + transpose, wpack), tile);
htp_conv2d_vtcm_layout fixed;
htp_conv2d_vtcm_layout_build(&fixed, rb, (size_t) rb * n_kt * tile, 0, 0, scratch, nt);
if (fixed.total_bytes >= budget) {
const uint32_t row = hex_round_up((uint32_t) width, cwhn ? 64 : 32);
const size_t per = (size_t) KH * row * sizeof(float);
if (per > budget) {
continue;
}
// Each spatial tile adds two activation tiles and two output tiles per rt.
const size_t per_ct = 2 * ((size_t) n_kt + rb) * tile;
const uint32_t nc = (uint32_t) (std::min)((size_t) (std::min)(n_ct, 64u), (budget - fixed.total_bytes) / per_ct);
const uint32_t candidates[2] = { nc, nc / nt * nt };
for (uint32_t i = 0; i < 2; i++) {
const uint32_t candidate = candidates[i];
if (candidate == 0 || (i > 0 && candidate == candidates[0])) {
// channel chunk: all channels or the most that fit HTP_CONV2D_STAGE_MAX, step k range even unless whole K
uint32_t cc_cap = (uint32_t) (std::max)((size_t) 1, (std::min)({ (size_t) Cin, HTP_CONV2D_STAGE_MAX / per, (size_t) cc_max }));
if (cc_cap < Cin && (cc_cap * KW * KH) % 2) {
if (cc_cap > 1) {
cc_cap--;
} else if (cc_max >= 2) {
cc_cap = 2;
}
}
const uint32_t ccs[2] = { Cin, cwhn ? Cin : cc_cap };
for (uint32_t j = 0; j < 2; j++) {
const uint32_t c = ccs[j];
if ((j > 0 && c == ccs[0]) || (!cwhn && c > cc_max) || (!cwhn && c < Cin && ((c * KW * KH) & 1))) {
continue;
}
const uint32_t per_hvx = (candidate + nt - 1) / nt;
if (sg > per_hvx) {
if (!cwhn && c > budget / per) {
continue;
}
const size_t work = (size_t) (n_ct / candidate) * per_hvx + (n_ct % candidate + nt - 1) / nt;
const size_t chunks = n_ct / candidate + (n_ct % candidate != 0);
const size_t cost = work * n_kt + c_chunk * chunks;
if (cost < best_cost) {
best_cost = cost;
rt_block = rb;
ct = candidate;
seg = sg;
nth = nt;
sr = row;
stage = input_stage;
thr_size = scratch;
const uint32_t n_cc = (Cin + c - 1) / c;
const uint32_t n_dma = stage_pc ? c * KD : KH;
const size_t rows = cwhn ? KH : (size_t) c * KH;
const size_t input_stage = cwhn ? per : (size_t) c * per;
// second stage buffer overlaps next DMA with this pack; skip it for large stages
for (uint32_t ns = 2; ns > 0; ns--) {
if (ns == 2 && (input_stage > HTP_CONV2D_STAGE_MAX || 2 * n_dma > 255)) {
continue;
}
// weight pack scratch shares the per-HVX spad region, take the largest k step that does not grow it
uint32_t w_k = (std::min)(k_pad, 512u);
struct htp_conv2d_tiled_spad_layout sl;
while (true) {
htp_conv2d_tiled_spad_layout_build(&sl, input_stage, ns, sg, cwhn, kmajor, w_k);
if (w_k <= 64 || sl.wpack_bytes <= sl.stages_bytes) {
break;
}
w_k = (std::max)(64u, w_k / 2 / 64 * 64);
}
const size_t spad = sl.total_bytes;
if (spad > budget / nh) {
continue;
}
htp_conv2d_vtcm_layout fixed;
htp_conv2d_vtcm_layout_build(&fixed, rb, (size_t) rb * n_kt * tile, 0, 0, spad, nh);
if (fixed.total_bytes >= budget) {
continue;
}
// Each col tile adds two activation tiles and two output tiles per rt
const size_t per_ct = 2 * ((size_t) n_kt + rb) * tile;
const uint32_t nc = (uint32_t) (std::min)((size_t) (std::min)(n_ct, 64u), (budget - fixed.total_bytes) / per_ct);
const uint32_t candidates[2] = { nc, nc / nh * nh };
for (uint32_t i = 0; i < 2; i++) {
const uint32_t cand = candidates[i];
if (cand == 0 || (i > 0 && cand == candidates[0])) {
continue;
}
if (sg > (cand + nh - 1) / nh) {
continue;
}
// chunk of m col tiles: HVX lanes take tpt each, idle lanes split the channel chunks
const auto chunk_cost = [&](uint32_t m) -> uint64_t {
const uint32_t tpt = (m + nh - 1) / nh;
const uint32_t n_tt = (m + tpt - 1) / tpt;
const uint32_t n_cs = (std::min)(n_cc, nh / n_tt);
const uint32_t cpt = (n_cc + n_cs - 1) / n_cs;
const uint64_t steps = (uint64_t) ((tpt + sg - 1) / sg) * cpt;
const uint64_t pack = (c_pack * tpt * cpt * n_kt + n_cc - 1) / n_cc;
const uint64_t dma = c_desc * n_dma + c_row * rows;
const uint64_t pstep = (pack + steps - 1) / steps;
const uint64_t in = steps * dma + pack;
const uint64_t full = dma + (steps - 1) * (std::max)(dma, pstep) + pstep;
const uint64_t unp = c_unp * rb * tpt;
return (ns == 2 ? (in + full) / 2 : in) + unp + c_chunk;
};
const uint64_t wwork = c_pack * (((uint64_t) rb * n_kt + nh - 1) / nh);
const uint64_t cost = n_rp * (wwork + (uint64_t) (n_ct / cand) * chunk_cost(cand) + (n_ct % cand ? chunk_cost(n_ct % cand) : 0));
if (cost < best_cost) {
best_cost = cost;
rt_block = rb;
ct = cand;
seg = sg;
n_hvx = nh;
cc = c;
wk = w_k;
n_stage = ns;
sr = row;
stage = input_stage;
spad_per_hvx = spad;
}
}
}
}
}
@@ -5237,33 +5308,20 @@ static bool htp_kparams_conv2d_tiled(
const size_t w_size = (size_t) rt_block * n_kt * tile;
const size_t act_size = (size_t) ct * n_kt * tile;
const size_t out_size = (size_t) rt_block * ct * tile;
const size_t ostage = 2 * 32 * (size_t) seg * 32 * sizeof(float);
const size_t prow = cwhn ? stage / 2 + 256 + 64 * 128 : 0;
struct htp_conv2d_vtcm_layout L;
htp_conv2d_vtcm_layout_build(&L, rt_block, w_size, act_size, out_size, thr_size, nth);
size_t total = L.total_bytes;
// Second stage buffer: DMA of the next segment overlaps HVX pack of this one.
// Skip large stages. They already fill VTCM, and the extra DMA slows HMX.
{
const size_t thr2 = hex_round_up((std::max)(2 * stage + ostage + prow, wpack), tile);
struct htp_conv2d_vtcm_layout L2;
htp_conv2d_vtcm_layout_build(&L2, rt_block, w_size, act_size, out_size, thr2, nth);
if (stage <= 128 * 1024 && 2 * g.KH <= 255 && L2.total_bytes <= htp_session_vtcm_size(sess)) {
n_stage = 2;
thr_size = thr2;
total = L2.total_bytes;
}
}
htp_conv2d_vtcm_layout_build(&L, rt_block, w_size, act_size, out_size, spad_per_hvx, n_hvx);
const size_t total = L.total_bytes;
kp->kernel_type = (uint8_t) HTP_CONV2D_KERNEL_HMX_TILED;
kp->flags = (cwhn ? HTP_CONV2D_FLAG_CWHN : 0) | (kmajor ? HTP_CONV2D_FLAG_W_KMAJOR : 0);
kp->n_hvx = (uint8_t) nth;
kp->flags = (cwhn ? HTP_CONV2D_FLAG_CWHN : 0) |
(kmajor ? HTP_CONV2D_FLAG_W_KMAJOR : 0) |
(stage_pc ? HTP_CONV2D_FLAG_STAGE_PC : 0);
kp->n_hvx = (uint8_t) n_hvx;
kp->n_row_tiles = n_rt;
kp->ct_chunk = ct;
kp->rt_block = rt_block;
kp->stage_size = stage;
kp->thr_size = thr_size;
kp->spad_per_hvx = spad_per_hvx;
kp->vtcm_size = total;
kp->tiled.w_kstride = ks;
kp->tiled.k_size = K;
@@ -5272,14 +5330,18 @@ static bool htp_kparams_conv2d_tiled(
kp->tiled.n_groups = n_grp;
kp->tiled.n_k_tiles = n_kt;
kp->tiled.n_ow_tiles = n_owt;
kp->tiled.seg_tiles = seg;
kp->tiled.seg_tiles = (uint16_t) seg;
kp->tiled.stage_row = sr;
kp->tiled.n_stage = n_stage;
kp->tiled.n_stage = (uint16_t) n_stage;
kp->tiled.cin_chunk = cc;
kp->tiled.n_cin_chunks = (Cin + cc - 1) / cc;
kp->tiled.kd = KD;
kp->tiled.wk_chunk = wk;
kp->tiled.div_ow_tiles = init_fastdiv_values(n_owt);
kp->tiled.div_oh = init_fastdiv_values(g.OH);
kp->tiled.div_khr = init_fastdiv_values(src0->ne[1]);
kp->tiled.div_ohr = init_fastdiv_values(dst->ne[1]);
kp->tiled.div_d1 = init_fastdiv_values(ggml_get_op_params_i32(dst, g.is3d ? 7 : 5));
return true;
}
@@ -5313,7 +5375,8 @@ static bool htp_kparams_solve_conv2d(
const struct ggml_tensor * dst = op;
if ((src0->type != GGML_TYPE_F16 && src0->type != GGML_TYPE_F32) ||
src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
(src1->type != GGML_TYPE_F16 && src1->type != GGML_TYPE_F32) ||
(dst->type != GGML_TYPE_F16 && dst->type != GGML_TYPE_F32)) {
return false;
}
@@ -5390,7 +5453,8 @@ static bool htp_kparams_solve_conv2d_dw(
}
if ((src0->type != GGML_TYPE_F16 && src0->type != GGML_TYPE_F32) ||
src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
(src1->type != GGML_TYPE_F16 && src1->type != GGML_TYPE_F32) ||
(dst->type != GGML_TYPE_F16 && dst->type != GGML_TYPE_F32)) {
return false;
}
@@ -5437,12 +5501,8 @@ static bool htp_kparams_solve_conv2d_dw(
const uint32_t ow_u = (uint32_t) ow;
const uint32_t oh_u = (uint32_t) oh;
const uint32_t row_src_bytes = cwhn
? (w * c * (uint32_t) sizeof(float))
: (w * (uint32_t) sizeof(float));
const uint32_t row_dst_bytes = cwhn
? (ow_u * c * (uint32_t) sizeof(float))
: (ow_u * (uint32_t) sizeof(float));
const uint32_t row_src_bytes = (cwhn ? w * c : w) * (uint32_t) ggml_type_size(src1->type);
const uint32_t row_dst_bytes = (cwhn ? ow_u * c : ow_u) * (uint32_t) ggml_type_size(dst->type);
uint8_t kernel_type = HTP_CONV2D_DW_KERNEL_UNSUPPORTED;
if (cwhn) {
@@ -5464,7 +5524,6 @@ static bool htp_kparams_solve_conv2d_dw(
}
const bool fast_k3x3 = kernel_type >= HTP_CONV2D_DW_KERNEL_WHCN_S1_K3X3;
const uint32_t pad_rows = fast_k3x3 ? 4 : 0;
const uint32_t pad_top_stride = fast_k3x3 ? w : 0;
const size_t vtcm_budget = htp_session_vtcm_size(sess);
@@ -5478,11 +5537,7 @@ static bool htp_kparams_solve_conv2d_dw(
struct htp_conv2d_dw_vtcm_layout layout;
while (true) {
const uint32_t max_ih_chunk = (std::min)(h, (oh_chunk - 1) * (uint32_t) s1 + (kh - 1) * (uint32_t) d1 + 1);
const uint32_t max_src_bytes = (max_ih_chunk + pad_rows) * row_src_bytes + 128;
const uint32_t max_dst_bytes = oh_chunk * row_dst_bytes + 128;
htp_conv2d_dw_vtcm_layout_build(&layout, max_src_bytes, max_dst_bytes, w_size, n_hvx);
htp_conv2d_dw_vtcm_layout_build(&layout, w, h, c, kh, ow_u, oh_chunk, s1, d1, cwhn, fast_k3x3, w_size, n_hvx);
if (layout.total_bytes <= vtcm_budget && layout.total_bytes > 0) {
break;
@@ -5504,10 +5559,7 @@ static bool htp_kparams_solve_conv2d_dw(
n_hvx = 1;
}
const uint32_t max_ih_chunk = (std::min)(h, (oh_chunk - 1) * (uint32_t) s1 + (kh - 1) * (uint32_t) d1 + 1);
const uint32_t max_src_bytes = (max_ih_chunk + pad_rows) * row_src_bytes + 128;
const uint32_t max_dst_bytes = oh_chunk * row_dst_bytes + 128;
htp_conv2d_dw_vtcm_layout_build(&layout, max_src_bytes, max_dst_bytes, w_size, n_hvx);
htp_conv2d_dw_vtcm_layout_build(&layout, w, h, c, kh, ow_u, oh_chunk, s1, d1, cwhn, fast_k3x3, w_size, n_hvx);
kparams->kernel_type = kernel_type;
kparams->flags = (cwhn ? HTP_CONV2D_DW_FLAG_CWHN : 0) | (src0->type == GGML_TYPE_F16 ? HTP_CONV2D_DW_FLAG_F16 : 0);
@@ -5550,7 +5602,8 @@ static bool htp_kparams_solve_conv3d(
const struct ggml_tensor * dst = op;
if ((src0->type != GGML_TYPE_F16 && src0->type != GGML_TYPE_F32) ||
src1->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) {
(src1->type != GGML_TYPE_F16 && src1->type != GGML_TYPE_F32) ||
(dst->type != GGML_TYPE_F16 && dst->type != GGML_TYPE_F32)) {
return false;
}
+1
View File
@@ -9,6 +9,7 @@
#include "htp/binary-ops.h"
#include "htp/concat-ops.h"
#include "htp/conv2d-ops.h"
#include "htp/conv2d-dw-ops.h"
#include "htp/cpy-ops.h"
#include "htp/cumsum-ops.h"
#include "htp/diag-ops.h"
+1
View File
@@ -4,6 +4,7 @@
#include "htp/argsort-ops.h"
#include "htp/binary-ops.h"
#include "htp/conv2d-ops.h"
#include "htp/conv2d-dw-ops.h"
#include "htp/cumsum-ops.h"
#include "htp/diag-ops.h"
#include "htp/dsv4-hc-ops.h"
+1 -1
View File
@@ -52,7 +52,7 @@ add_library(${HTP_LIB} SHARED
argsort-ops.c
im2col-ops.c
conv2d-ops.c
conv2d-dw5-ops.c
conv2d-dw-ops.c
roll-ops.c
pool-ops.c
upscale-ops.c
File diff suppressed because it is too large Load Diff
+144
View File
@@ -0,0 +1,144 @@
#ifndef HTP_CONV2D_DW_OPS_H
#define HTP_CONV2D_DW_OPS_H
#include <stddef.h>
#include <stdint.h>
#include "hex-common.h"
#include "hex-fastdiv.h"
#include "htp-ops.h"
#include "htp-vtcm.h"
enum htp_conv2d_dw_kernel_type {
HTP_CONV2D_DW_KERNEL_UNSUPPORTED = 0,
HTP_CONV2D_DW_KERNEL_CWHN, // CWHN channel-contiguous layout
HTP_CONV2D_DW_KERNEL_WHCN_S1, // WHCN general stride-1
HTP_CONV2D_DW_KERNEL_WHCN_S2, // WHCN general stride-2
HTP_CONV2D_DW_KERNEL_WHCN_S1_K3X3, // WHCN aligned w%32==0, 3x3 s=1 p=1 d=1
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,
};
enum htp_conv2d_dw_flags {
HTP_CONV2D_DW_FLAG_CWHN = (1u << 0),
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
uint8_t n_hvx;
uint32_t total_tasks;
uint32_t vtcm_size;
uint32_t w;
uint32_t h;
uint32_t c;
uint32_t kw;
uint32_t kh;
uint32_t ow;
uint32_t oh;
int32_t s0;
int32_t s1;
int32_t p0;
int32_t p1;
int32_t d0;
int32_t d1;
uint32_t oh_chunk;
uint32_t n_chunks;
uint32_t row_src_bytes;
uint32_t row_dst_bytes;
uint32_t pad_top_stride;
struct fastdiv_values div_c;
struct fastdiv_values div_n_chunks;
};
#if defined(__cplusplus)
static_assert(sizeof(struct htp_conv2d_dw_kernel_params) <= 128, "htp_conv2d_dw_kernel_params is too large for kernel_params blob");
#else
_Static_assert(sizeof(struct htp_conv2d_dw_kernel_params) <= 128, "htp_conv2d_dw_kernel_params is too large for kernel_params blob");
#endif
struct htp_conv2d_dw_vtcm_layout {
size_t total_bytes;
size_t off_w;
size_t off_src;
size_t off_dst;
size_t src_spad_half_size;
size_t dst_spad_half_size;
size_t src_bytes_per_hvx;
size_t dst_bytes_per_hvx;
};
static inline void htp_conv2d_dw_vtcm_layout_build(
struct htp_conv2d_dw_vtcm_layout * L,
uint32_t w,
uint32_t h,
uint32_t c,
uint32_t kh,
uint32_t ow,
uint32_t oh_chunk,
int32_t s1,
int32_t d1,
bool cwhn,
bool has_pad_rows,
uint32_t w_size,
uint32_t n_hvx
) {
const uint32_t pad_rows = has_pad_rows ? 4 : 0;
const uint32_t row_src_f32 = (cwhn ? w * c : w) * (uint32_t) sizeof(float);
const uint32_t row_dst_f32 = (cwhn ? ow * c : ow) * (uint32_t) sizeof(float);
const uint32_t max_ih_chunk = MIN(h, (oh_chunk - 1) * (uint32_t) s1 + (kh - 1) * (uint32_t) d1 + 1);
const uint32_t max_src_bytes = (max_ih_chunk + pad_rows) * row_src_f32 + 128;
const uint32_t max_dst_bytes = oh_chunk * row_dst_f32 + 128;
L->src_spad_half_size = hex_round_up(max_src_bytes + 256, 128);
L->dst_spad_half_size = hex_round_up(max_dst_bytes + 128, 128);
L->src_bytes_per_hvx = 2 * L->src_spad_half_size;
L->dst_bytes_per_hvx = 2 * L->dst_spad_half_size;
size_t off = 0;
VTCM_LAYOUT_ALLOC(off, off_w, hex_round_up(w_size, 128));
VTCM_LAYOUT_ALLOC(off, off_src, L->src_bytes_per_hvx * n_hvx);
VTCM_LAYOUT_ALLOC(off, off_dst, L->dst_bytes_per_hvx * n_hvx);
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;
}
#endif // HTP_CONV2D_DW_OPS_H
-295
View File
@@ -1,295 +0,0 @@
// 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;
}
File diff suppressed because it is too large Load Diff
+78 -127
View File
@@ -15,12 +15,15 @@ enum htp_conv2d_kernel_type {
HTP_CONV2D_KERNEL_HMX_TILED, // tiled implicit-GEMM convolution on HMX
};
// HMX_WINDOW output staging buffers per thread
// HMX_WINDOW output staging buffers per HVX
#define HTP_CONV2D_WIN_OBUF 2
// HMX scales of one row tile at the start of VTCM: 32 (scale, bias) fp16 pairs and 128 zero bytes
#define HTP_CONV2D_HMX_SCALES_SIZE 256
// Max bytes of one staged input buffer (the DMA descriptor row/stride limits are far below VTCM size)
#define HTP_CONV2D_STAGE_MAX ((size_t) 128 * 1024)
struct htp_conv2d_tiled_params {
uint32_t w_kstride;
uint32_t k_size;
@@ -29,14 +32,18 @@ struct htp_conv2d_tiled_params {
uint32_t n_groups;
uint32_t n_k_tiles;
uint32_t n_ow_tiles;
uint32_t seg_tiles;
uint32_t stage_row;
uint32_t n_stage;
uint16_t seg_tiles;
uint16_t n_stage;
uint32_t cin_chunk;
uint32_t wk_chunk;
uint32_t n_cin_chunks;
uint32_t kd;
struct fastdiv_values div_ow_tiles;
struct fastdiv_values div_oh;
struct fastdiv_values div_khr; // kernel ne[1], unfolds conv_3d kh = kd * KHr + kh'
struct fastdiv_values div_ohr; // dst ne[1], unfolds conv_3d oh = od * OHr + oh'
struct fastdiv_values div_d1; // dilation along H
};
struct htp_conv2d_window_params {
@@ -47,9 +54,10 @@ struct htp_conv2d_window_params {
uint32_t ihs;
uint32_t max_off;
uint32_t n_bc;
uint32_t seg_b;
uint32_t seg_t;
uint32_t w_cs;
uint16_t seg_b;
uint16_t seg_t;
uint16_t w_cs;
uint16_t w_n_seg; // n_tg (kmajor) or n_cs (w_rows)
struct fastdiv_values div_sr;
struct fastdiv_values div_ihs;
struct fastdiv_values div_kw;
@@ -61,6 +69,7 @@ struct htp_conv2d_window_params {
enum htp_conv2d_flags {
HTP_CONV2D_FLAG_CWHN = (1u << 0),
HTP_CONV2D_FLAG_W_KMAJOR = (1u << 1),
HTP_CONV2D_FLAG_STAGE_PC = (1u << 2),
};
struct htp_conv2d_kernel_params {
@@ -73,7 +82,7 @@ struct htp_conv2d_kernel_params {
uint32_t ct_chunk;
uint32_t rt_block;
uint32_t stage_size;
uint32_t thr_size;
uint32_t spad_per_hvx;
uint32_t vtcm_size;
union {
@@ -88,130 +97,13 @@ static_assert(sizeof(struct htp_conv2d_kernel_params) <= 128, "htp_conv2d_kernel
_Static_assert(sizeof(struct htp_conv2d_kernel_params) <= 128, "htp_conv2d_kernel_params is too large for kernel_params blob");
#endif
enum htp_conv2d_dw_kernel_type {
HTP_CONV2D_DW_KERNEL_UNSUPPORTED = 0,
HTP_CONV2D_DW_KERNEL_CWHN, // CWHN channel-contiguous layout
HTP_CONV2D_DW_KERNEL_WHCN_S1, // WHCN general stride-1
HTP_CONV2D_DW_KERNEL_WHCN_S2, // WHCN general stride-2
HTP_CONV2D_DW_KERNEL_WHCN_S1_K3X3, // WHCN aligned w%32==0, 3x3 s=1 p=1 d=1
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,
};
enum htp_conv2d_dw_flags {
HTP_CONV2D_DW_FLAG_CWHN = (1u << 0),
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
uint8_t n_hvx;
uint32_t total_tasks;
uint32_t vtcm_size;
uint32_t w;
uint32_t h;
uint32_t c;
uint32_t kw;
uint32_t kh;
uint32_t ow;
uint32_t oh;
int32_t s0;
int32_t s1;
int32_t p0;
int32_t p1;
int32_t d0;
int32_t d1;
uint32_t oh_chunk;
uint32_t n_chunks;
uint32_t row_src_bytes;
uint32_t row_dst_bytes;
uint32_t pad_top_stride;
struct fastdiv_values div_c;
struct fastdiv_values div_n_chunks;
};
#if defined(__cplusplus)
static_assert(sizeof(struct htp_conv2d_dw_kernel_params) <= 128, "htp_conv2d_dw_kernel_params is too large for kernel_params blob");
#else
_Static_assert(sizeof(struct htp_conv2d_dw_kernel_params) <= 128, "htp_conv2d_dw_kernel_params is too large for kernel_params blob");
#endif
struct htp_conv2d_dw_vtcm_layout {
size_t total_bytes;
size_t off_w;
size_t off_src;
size_t off_dst;
size_t src_spad_half_size;
size_t dst_spad_half_size;
size_t src_bytes_per_hvx;
size_t dst_bytes_per_hvx;
};
static inline void htp_conv2d_dw_vtcm_layout_build(
struct htp_conv2d_dw_vtcm_layout * L,
uint32_t max_src_bytes,
uint32_t max_dst_bytes,
uint32_t w_size,
uint32_t n_hvx
) {
L->src_spad_half_size = hex_round_up(max_src_bytes + 256, 128);
L->dst_spad_half_size = hex_round_up(max_dst_bytes + 128, 128);
L->src_bytes_per_hvx = 2 * L->src_spad_half_size;
L->dst_bytes_per_hvx = 2 * L->dst_spad_half_size;
size_t off = 0;
VTCM_LAYOUT_ALLOC(off, off_w, hex_round_up(w_size, 128));
VTCM_LAYOUT_ALLOC(off, off_src, L->src_bytes_per_hvx * n_hvx);
VTCM_LAYOUT_ALLOC(off, off_dst, L->dst_bytes_per_hvx * n_hvx);
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;
size_t off_w;
size_t off_act[2];
size_t off_out[2];
size_t off_thr;
size_t off_spad;
size_t act_size;
};
@@ -222,7 +114,7 @@ static inline void htp_conv2d_vtcm_layout_build(
size_t w_size,
size_t act_size,
size_t out_size,
size_t thr_size,
size_t spad_per_hvx,
uint32_t n_hvx
) {
const size_t sc = hex_round_up((size_t) rt_block * HTP_CONV2D_HMX_SCALES_SIZE, 2048);
@@ -233,9 +125,68 @@ static inline void htp_conv2d_vtcm_layout_build(
VTCM_LAYOUT_ALLOC(off, off_act[1], act_size);
VTCM_LAYOUT_ALLOC(off, off_out[0], out_size);
VTCM_LAYOUT_ALLOC(off, off_out[1], out_size);
VTCM_LAYOUT_ALLOC(off, off_thr, (size_t) n_hvx * thr_size);
VTCM_LAYOUT_ALLOC(off, off_spad, (size_t) n_hvx * spad_per_hvx);
L->act_size = act_size;
L->total_bytes = off;
}
struct htp_conv2d_tiled_spad_layout {
size_t total_bytes;
size_t off_stage[2];
size_t off_ostage[2];
size_t off_stage16;
size_t off_vin;
size_t off_scratch;
size_t stages_bytes;
size_t wpack_bytes;
};
static inline void htp_conv2d_tiled_spad_layout_build(
struct htp_conv2d_tiled_spad_layout * L,
size_t stage_size,
uint32_t n_stage,
uint32_t seg_tiles,
bool cwhn,
bool kmajor,
uint32_t wk_chunk
) {
const size_t ostage_half = (size_t) 32 * seg_tiles * 32 * sizeof(float);
size_t off = 0;
VTCM_LAYOUT_ALLOC(off, off_stage[0], stage_size);
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_stage[1], stage_size, n_stage > 1);
VTCM_LAYOUT_ALLOC(off, off_ostage[0], ostage_half);
VTCM_LAYOUT_ALLOC(off, off_ostage[1], ostage_half);
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_stage16, stage_size / 2 + 256, cwhn);
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_vin, 32 * 128, cwhn);
VTCM_LAYOUT_ALLOC_OPTIONAL(off, off_scratch, 32 * 128, cwhn);
L->stages_bytes = off;
L->wpack_bytes = kmajor
? 2 * (size_t) wk_chunk * 64 * sizeof(float) + 96 * 128
: 2 * (size_t) 32 * wk_chunk * (sizeof(float) + sizeof(uint16_t));
L->total_bytes = hex_round_up(MAX(L->stages_bytes, L->wpack_bytes), 2048);
}
struct htp_conv2d_win_spad_layout {
size_t total_bytes;
size_t off_stage[2];
size_t off_obuf;
size_t stages_bytes;
};
static inline void htp_conv2d_win_spad_layout_build(
struct htp_conv2d_win_spad_layout * L,
size_t stage_size,
uint32_t seg_t,
size_t wscr_bytes
) {
const size_t obuf_bytes = (size_t) HTP_CONV2D_WIN_OBUF * 32 * seg_t * 32 * sizeof(float);
size_t off = 0;
VTCM_LAYOUT_ALLOC(off, off_stage[0], stage_size);
VTCM_LAYOUT_ALLOC(off, off_stage[1], stage_size);
VTCM_LAYOUT_ALLOC(off, off_obuf, obuf_bytes);
L->stages_bytes = off;
L->total_bytes = hex_round_up(MAX(L->stages_bytes, wscr_bytes), 2048);
}
#endif // HTP_CONV2D_OPS_H
-1
View File
@@ -185,7 +185,6 @@ 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);