mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2026-12-04 00:27:38 -06:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
72a780e6a6 |
@@ -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;
|
||||
}
|
||||
|
||||
@@ -5539,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;
|
||||
}
|
||||
|
||||
|
||||
@@ -171,6 +171,15 @@ static void conv_geom_init(struct htp_conv_geom * g, const struct htp_ops_contex
|
||||
}
|
||||
}
|
||||
|
||||
struct htp_conv2d_chunk_plan {
|
||||
uint32_t tpt;
|
||||
uint32_t n_tt;
|
||||
uint32_t n_cs;
|
||||
uint32_t cc_q;
|
||||
uint32_t cc_r;
|
||||
struct fastdiv_values div_n_tt;
|
||||
};
|
||||
|
||||
struct htp_conv2d_hmx_context {
|
||||
struct htp_ops_context * octx;
|
||||
const struct htp_conv2d_kernel_params * kp;
|
||||
@@ -180,15 +189,21 @@ struct htp_conv2d_hmx_context {
|
||||
__fp16 * w_tiles;
|
||||
__fp16 * act[2];
|
||||
__fp16 * out[2];
|
||||
uint8_t * thr_base;
|
||||
uint8_t * spad_base;
|
||||
uint32_t t_base;
|
||||
uint32_t t_end;
|
||||
uint32_t rt0;
|
||||
uint32_t n_rt;
|
||||
int32_t pack_chunk;
|
||||
int32_t unpack_chunk;
|
||||
uint32_t pack_tpt;
|
||||
int32_t last_chunk;
|
||||
uint32_t unpack_tpt;
|
||||
uint32_t wpack_n_kp;
|
||||
uint32_t wpack_ks_q;
|
||||
uint32_t wpack_ks_r;
|
||||
struct fastdiv_values wpack_div_n_kp;
|
||||
struct htp_conv2d_chunk_plan plan[2]; // 0: full chunk, 1: tail chunk
|
||||
const struct htp_conv2d_chunk_plan * pack_plan;
|
||||
};
|
||||
|
||||
// col tiles [t0, t1) of chunk c for thread ith, returns first col tile of the chunk
|
||||
@@ -215,12 +230,12 @@ static void conv2d_hmx_wpack_thread(const htp_thread_t * th, unsigned int nth, u
|
||||
const uint32_t es = is_f16 ? sizeof(__fp16) : sizeof(float);
|
||||
|
||||
dma_queue * dmaq = th->dma_q;
|
||||
uint8_t * thr = hc->thr_base + (size_t) ith * kp->thr_size;
|
||||
uint8_t * spad = hc->spad_base + (size_t) ith * kp->spad_per_hvx;
|
||||
const size_t w_buf_size = (size_t) WK * 32 * sizeof(__fp16);
|
||||
const size_t w_raw_size = (size_t) WK * 32 * sizeof(float);
|
||||
const size_t w_step = w_buf_size + w_raw_size;
|
||||
|
||||
__fp16 * wbuf[2] = { (__fp16 *) thr, (__fp16 *) (thr + w_step) };
|
||||
__fp16 * wbuf[2] = { (__fp16 *) spad, (__fp16 *) (spad + w_step) };
|
||||
float * wraw[2] = { (float *) ((uint8_t *) wbuf[0] + w_buf_size),
|
||||
(float *) ((uint8_t *) wbuf[1] + w_buf_size) };
|
||||
uint8_t * wdma[2] = { is_f16 ? (uint8_t *) wbuf[0] : (uint8_t *) wraw[0],
|
||||
@@ -230,19 +245,30 @@ static void conv2d_hmx_wpack_thread(const htp_thread_t * th, unsigned int nth, u
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
|
||||
for (uint32_t rt = ith; rt < hc->n_rt; rt += nth) {
|
||||
const uint32_t co0 = (hc->rt0 + rt) * 32;
|
||||
const uint32_t nco = MIN(32, Cout - co0);
|
||||
// work items are (rt, k range) so deep K keeps all threads busy when rt_block is small
|
||||
const uint32_t n_kp = hc->wpack_n_kp;
|
||||
const uint32_t ks_q = hc->wpack_ks_q;
|
||||
const uint32_t ks_r = hc->wpack_ks_r;
|
||||
|
||||
for (uint32_t it = ith; it < hc->n_rt * n_kp; it += nth) {
|
||||
const uint32_t rt = fastdiv(it, &hc->wpack_div_n_kp);
|
||||
const uint32_t kpi = it - rt * n_kp;
|
||||
const uint32_t co0 = (hc->rt0 + rt) * 32;
|
||||
const uint32_t nco = MIN(32, Cout - co0);
|
||||
const uint32_t ks0 = kpi * ks_q + MIN(kpi, ks_r);
|
||||
const uint32_t ks1 = ks0 + ks_q + (kpi < ks_r ? 1 : 0);
|
||||
const uint32_t kbeg = ks0 * WK;
|
||||
const uint32_t kend = MIN(ks1 * WK, k_pad);
|
||||
|
||||
uint32_t bi = 0;
|
||||
for (uint32_t i = 0; i < 2 && i * WK < k_pad; i++) {
|
||||
const uint32_t kc = i * WK;
|
||||
for (uint32_t i = 0; i < 2 && kbeg + i * WK < kend; i++) {
|
||||
const uint32_t kc = kbeg + i * WK;
|
||||
const uint32_t kv = MIN(WK, K - kc);
|
||||
dma_queue_push(dmaq, dma_make_data(wdma[i], kernel->data + (size_t) co0 * hc->g->w_nb_co + (size_t) kc * es),
|
||||
WK * es, hc->g->w_nb_co, kv * es, nco);
|
||||
}
|
||||
|
||||
for (uint32_t kc = 0; kc < k_pad; kc += WK) {
|
||||
for (uint32_t kc = kbeg; kc < kend; kc += WK) {
|
||||
const uint32_t kv = MIN(WK, K - kc);
|
||||
const uint32_t hd = MIN(WK, k_pad - kc);
|
||||
|
||||
@@ -270,7 +296,7 @@ static void conv2d_hmx_wpack_thread(const htp_thread_t * th, unsigned int nth, u
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_W_PREP, (uint16_t) kc);
|
||||
|
||||
const uint32_t next_kc = kc + 2 * WK;
|
||||
if (next_kc < k_pad) {
|
||||
if (next_kc < kend) {
|
||||
const uint32_t next_kv = MIN(WK, K - next_kc);
|
||||
dma_queue_push(dmaq, dma_make_data(wdma[bi], kernel->data + (size_t) co0 * hc->g->w_nb_co + (size_t) next_kc * es),
|
||||
WK * es, hc->g->w_nb_co, next_kv * es, nco);
|
||||
@@ -280,26 +306,76 @@ static void conv2d_hmx_wpack_thread(const htp_thread_t * th, unsigned int nth, u
|
||||
}
|
||||
}
|
||||
|
||||
// Backwards in-place f16 -> f32 expansion in VTCM (buf has f32 capacity).
|
||||
static __attribute__((noinline)) void conv2d_hmx_expand_f16_to_f32_inplace(float * buf, uint32_t n_elems) {
|
||||
const uint32_t full = n_elems & ~63u;
|
||||
const uint32_t rem = n_elems - full;
|
||||
if (rem > 0) {
|
||||
const HVX_Vector v = hvx_vmemu((const __fp16 *) buf + full);
|
||||
const HVX_VectorPair p = hvx_vec_f16_to_f32(v);
|
||||
if (rem <= VLEN_FP32) {
|
||||
hvx_vec_store_u(buf + full, rem * sizeof(float), Q6_V_lo_W(p));
|
||||
} else {
|
||||
hvx_vec_store_u(buf + full, VLEN, Q6_V_lo_W(p));
|
||||
hvx_vec_store_u(buf + full + VLEN_FP32, (rem - VLEN_FP32) * sizeof(float), Q6_V_hi_W(p));
|
||||
}
|
||||
}
|
||||
for (int32_t i = (int32_t) full - 64; i >= 0; i -= 64) {
|
||||
const HVX_Vector v = hvx_vmemu((const __fp16 *) buf + i);
|
||||
const HVX_VectorPair p = hvx_vec_f16_to_f32(v);
|
||||
hvx_vec_store_u(buf + i + VLEN_FP32, VLEN, Q6_V_hi_W(p));
|
||||
hvx_vec_store_u(buf + i, VLEN, Q6_V_lo_W(p));
|
||||
}
|
||||
}
|
||||
|
||||
static __attribute__((noinline)) void conv2d_hmx_expand_rows_f16_to_f32(float * stage, uint32_t row_stride, uint32_t row_elems, uint32_t n_rows) {
|
||||
for (uint32_t r = 0; r < n_rows; r++) {
|
||||
conv2d_hmx_expand_f16_to_f32_inplace(stage + (size_t) r * row_stride, row_elems);
|
||||
}
|
||||
}
|
||||
|
||||
// Pack n_rows of row_elems f32 values at f32 row stride row_stride into f16 in-place (preserving 128B row alignment).
|
||||
static __attribute__((noinline)) void conv2d_hmx_pack_rows_f32_to_f16(float * ob, uint32_t row_stride, uint32_t row_elems, uint32_t n_rows) {
|
||||
for (uint32_t r = 0; r < n_rows; r++) {
|
||||
const float * src = ob + (size_t) r * row_stride;
|
||||
__fp16 * dst = (__fp16 *) (ob + (size_t) r * row_stride);
|
||||
uint32_t i = 0;
|
||||
for (; i + 64 <= row_elems; i += 64) {
|
||||
const HVX_Vector v0 = hvx_vmem(src + i);
|
||||
const HVX_Vector v1 = hvx_vmem(src + i + VLEN_FP32);
|
||||
hvx_vmem(dst + i) = conv2d_f32_to_f16(v0, v1);
|
||||
}
|
||||
const uint32_t rem = row_elems - i;
|
||||
if (rem > 0) {
|
||||
const HVX_Vector v0 = hvx_vmem(src + i);
|
||||
const HVX_Vector v1 = (rem > VLEN_FP32) ? hvx_vmem(src + i + VLEN_FP32) : Q6_V_vzero();
|
||||
hvx_vec_store_a(dst + i, rem * sizeof(__fp16), conv2d_f32_to_f16(v0, v1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Stage Cin*KH input rows for G col tiles: stage[(cin*KH + kh)*SR + j] = x[n][cin][ih(kh)][ow0*s0 - p0 + j], zero outside
|
||||
// For conv_3d, kh = kd * KHr + kh' and oh = od * OHr + oh' are unfolded here
|
||||
static void conv2d_hmx_stage(const struct htp_ops_context * octx, const struct htp_conv2d_kernel_params * kp,
|
||||
const struct htp_conv_geom * g, dma_queue * dmaq, float * stage, uint32_t n, uint32_t oh,
|
||||
uint32_t ow0, uint32_t G) {
|
||||
uint32_t ow0, uint32_t G, uint32_t c0, uint32_t cc) {
|
||||
const struct htp_tensor * kernel = octx->src[0];
|
||||
const struct htp_tensor * input = octx->src[1];
|
||||
|
||||
const uint32_t KW = kernel->ne[0];
|
||||
const uint32_t KH = g->KH;
|
||||
const uint32_t Cin = g->Cin;
|
||||
const int32_t IW = input->ne[0];
|
||||
const int32_t IH = input->ne[1];
|
||||
const int32_t s0 = g->s0;
|
||||
const int32_t s1 = g->s1;
|
||||
const int32_t p0 = g->p0;
|
||||
const int32_t p1 = g->p1;
|
||||
const int32_t d0 = g->d0;
|
||||
const int32_t d1 = g->d1;
|
||||
const uint32_t SR = kp->tiled.stage_row;
|
||||
const uint32_t KW = kernel->ne[0];
|
||||
const uint32_t KH = g->KH;
|
||||
const uint32_t Cin = cc;
|
||||
const int32_t IW = input->ne[0];
|
||||
const int32_t IH = input->ne[1];
|
||||
const int32_t s0 = g->s0;
|
||||
const int32_t s1 = g->s1;
|
||||
const int32_t p0 = g->p0;
|
||||
const int32_t p1 = g->p1;
|
||||
const int32_t d0 = g->d0;
|
||||
const int32_t d1 = g->d1;
|
||||
const uint32_t SR = kp->tiled.stage_row;
|
||||
const uint32_t x_es = input->type == HTP_TYPE_F16 ? sizeof(__fp16) : sizeof(float);
|
||||
const HVX_Vector zero = Q6_V_vzero();
|
||||
|
||||
const int32_t Wb = 32 * G * s0 + (KW - 1) * d0;
|
||||
const int32_t iw0 = (int32_t) ow0 * s0 - p0;
|
||||
@@ -317,24 +393,93 @@ static void conv2d_hmx_stage(const struct htp_ops_context * octx, const struct h
|
||||
|
||||
if (id < 0 || id >= (int32_t) g->ID || ih < 0 || ih >= IH || hi <= lo) {
|
||||
for (uint32_t c = 0; c < Cin; c++) {
|
||||
hvx_splat_f32_u(rows + (size_t) c * KH * SR, 0.0f, Wb);
|
||||
hvx_splat_u(rows + (size_t) c * KH * SR, zero, Wb, x_es);
|
||||
}
|
||||
dma_queue_push(dmaq, dma_make_data(rows, 0), 0, 0, 0, 0);
|
||||
continue;
|
||||
}
|
||||
|
||||
const dma_addr_t src = input->data + (size_t) n * g->x_nb_n + (size_t) id * g->x_nb_d + (size_t) ih * input->nb[1] + (size_t) (iw0 + lo) * sizeof(float);
|
||||
dma_queue_push(dmaq, dma_make_data(rows + lo, src), (size_t) KH * SR * sizeof(float), g->x_nb_c,
|
||||
(size_t) (hi - lo) * sizeof(float), Cin);
|
||||
const dma_addr_t src = input->data + (size_t) n * g->x_nb_n + (size_t) id * g->x_nb_d + (size_t) ih * input->nb[1] + (size_t) (iw0 + lo) * x_es + (size_t) c0 * g->x_nb_c;
|
||||
dma_queue_push(dmaq, dma_make_data((uint8_t *) rows + (size_t) lo * x_es, src), (size_t) KH * SR * sizeof(float), g->x_nb_c,
|
||||
(size_t) (hi - lo) * x_es, Cin);
|
||||
|
||||
if (lo > 0 || hi < Wb) {
|
||||
for (uint32_t c = 0; c < Cin; c++) {
|
||||
float * row = rows + (size_t) c * KH * SR;
|
||||
uint8_t * row = (uint8_t *) (rows + (size_t) c * KH * SR);
|
||||
if (lo > 0) {
|
||||
hvx_splat_f32_u(row, 0.0f, lo);
|
||||
hvx_splat_u(row, zero, lo, x_es);
|
||||
}
|
||||
if (hi < Wb) {
|
||||
hvx_splat_f32_u(row + hi, 0.0f, Wb - hi);
|
||||
hvx_splat_u(row + (size_t) hi * x_es, zero, Wb - hi, x_es);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Same layout as conv2d_hmx_stage, but one DMA per (c, kd) for channel strides over the 2D DMA limit
|
||||
static void conv2d_hmx_stage_pc(const struct htp_ops_context * octx, const struct htp_conv2d_kernel_params * kp,
|
||||
const struct htp_conv_geom * g, dma_queue * dmaq, float * stage, uint32_t n, uint32_t oh,
|
||||
uint32_t ow0, uint32_t G, uint32_t c0, uint32_t cc) {
|
||||
const struct htp_tensor * kernel = octx->src[0];
|
||||
const struct htp_tensor * input = octx->src[1];
|
||||
|
||||
const uint32_t KW = kernel->ne[0];
|
||||
const uint32_t KH = g->KH;
|
||||
const uint32_t KHr = g->KHr;
|
||||
const uint32_t KD = kp->tiled.kd;
|
||||
const int32_t IW = input->ne[0];
|
||||
const int32_t IH = input->ne[1];
|
||||
const int32_t s0 = g->s0;
|
||||
const int32_t s1 = g->s1;
|
||||
const int32_t d0 = g->d0;
|
||||
const int32_t d1 = g->d1;
|
||||
const uint32_t SR = kp->tiled.stage_row;
|
||||
const uint32_t x_es = input->type == HTP_TYPE_F16 ? sizeof(__fp16) : sizeof(float);
|
||||
const HVX_Vector zero = Q6_V_vzero();
|
||||
|
||||
const int32_t Wb = 32 * G * s0 + (KW - 1) * d0;
|
||||
const int32_t iw0 = (int32_t) ow0 * s0 - g->p0;
|
||||
const int32_t lo = MAX(0, -iw0);
|
||||
const int32_t hi = MIN(Wb, IW - iw0);
|
||||
|
||||
const uint32_t od = fastdiv(oh, &kp->tiled.div_ohr);
|
||||
oh -= od * g->OHr;
|
||||
|
||||
// kh rows [kb, ke) have ih = b + kh * d1 in [0, IH)
|
||||
const int32_t b = (int32_t) oh * s1 - g->p1;
|
||||
const int32_t kb = MIN((int32_t) KHr, b >= 0 ? 0 : (int32_t) fastdiv(-b + d1 - 1, &kp->tiled.div_d1));
|
||||
const int32_t ke = MAX(kb, MIN((int32_t) KHr, IH - b <= 0 ? 0 : (int32_t) fastdiv(IH - b + d1 - 1, &kp->tiled.div_d1)));
|
||||
|
||||
for (uint32_t c = 0; c < cc; c++) {
|
||||
for (uint32_t kd = 0; kd < KD; kd++) {
|
||||
const int32_t id = (int32_t) od * g->s2 + (int32_t) kd * g->d2 - g->p2;
|
||||
float * rows = stage + ((size_t) c * KH + kd * KHr) * SR;
|
||||
|
||||
if (id < 0 || id >= (int32_t) g->ID || kb >= ke || hi <= lo) {
|
||||
for (uint32_t kh = 0; kh < KHr; kh++) {
|
||||
hvx_splat_u(rows + (size_t) kh * SR, zero, Wb, x_es);
|
||||
}
|
||||
dma_queue_push(dmaq, dma_make_data(rows, 0), 0, 0, 0, 0);
|
||||
continue;
|
||||
}
|
||||
|
||||
const dma_addr_t src = input->data + (size_t) n * g->x_nb_n + (size_t) (c0 + c) * g->x_nb_c + (size_t) id * g->x_nb_d +
|
||||
(size_t) (b + kb * d1) * input->nb[1] + (size_t) (iw0 + lo) * x_es;
|
||||
dma_queue_push(dmaq, dma_make_data((uint8_t *) (rows + (size_t) kb * SR) + (size_t) lo * x_es, src),
|
||||
(size_t) SR * sizeof(float), (size_t) d1 * input->nb[1], (size_t) (hi - lo) * x_es, ke - kb);
|
||||
|
||||
for (int32_t kh = 0; kh < (int32_t) KHr; kh++) {
|
||||
uint8_t * row = (uint8_t *) (rows + (size_t) kh * SR);
|
||||
if (kh < kb || kh >= ke) {
|
||||
hvx_splat_u(row, zero, Wb, x_es);
|
||||
continue;
|
||||
}
|
||||
if (lo > 0) {
|
||||
hvx_splat_u(row, zero, lo, x_es);
|
||||
}
|
||||
if (hi < Wb) {
|
||||
hvx_splat_u(row + (size_t) hi * x_es, zero, Wb - hi, x_es);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -409,43 +554,50 @@ static void conv2d_hmx_stage_cwhn(const struct htp_ops_context * octx, const str
|
||||
const int32_t d1 = octx->op_params[5];
|
||||
const uint32_t SR = kp->tiled.stage_row;
|
||||
|
||||
const int32_t Wb = 32 * G * s0 + (KW - 1) * d0;
|
||||
const int32_t iw0 = (int32_t) ow0 * s0 - p0;
|
||||
const int32_t lo = MAX(0, -iw0);
|
||||
const int32_t hi = MIN(Wb, IW - iw0);
|
||||
const int32_t Wb = 32 * G * s0 + (KW - 1) * d0;
|
||||
const int32_t iw0 = (int32_t) ow0 * s0 - p0;
|
||||
const int32_t lo = MAX(0, -iw0);
|
||||
const int32_t hi = MIN(Wb, IW - iw0);
|
||||
const uint32_t x_es = input->type == HTP_TYPE_F16 ? sizeof(__fp16) : sizeof(float);
|
||||
const HVX_Vector zero = Q6_V_vzero();
|
||||
|
||||
for (uint32_t kh = 0; kh < KH; kh++) {
|
||||
const int32_t ih = (int32_t) oh * s1 + (int32_t) kh * d1 - p1;
|
||||
float * row = stage + (size_t) kh * SR;
|
||||
uint8_t * row = (uint8_t *) (stage + (size_t) kh * SR);
|
||||
|
||||
if (ih < 0 || ih >= IH || hi <= lo) {
|
||||
hvx_splat_f32_u(row, 0.0f, Wb * Cin);
|
||||
hvx_splat_u(row, zero, Wb * Cin, x_es);
|
||||
dma_queue_push(dmaq, dma_make_data(row, 0), 0, 0, 0, 0);
|
||||
continue;
|
||||
}
|
||||
|
||||
const dma_addr_t src = input->data + (size_t) n * input->nb[3] + (size_t) ih * input->nb[1] + (size_t) (iw0 + lo) * input->nb[0];
|
||||
const size_t size = (size_t) (hi - lo) * Cin * sizeof(float);
|
||||
dma_queue_push(dmaq, dma_make_data(row + lo * Cin, src), size, size, size, 1);
|
||||
const size_t size = (size_t) (hi - lo) * Cin * x_es;
|
||||
dma_queue_push(dmaq, dma_make_data(row + (size_t) lo * Cin * x_es, src), size, size, size, 1);
|
||||
|
||||
if (lo > 0) {
|
||||
hvx_splat_f32_u(row, 0.0f, lo * Cin);
|
||||
hvx_splat_u(row, zero, lo * Cin, x_es);
|
||||
}
|
||||
if (hi < Wb) {
|
||||
hvx_splat_f32_u(row + hi * Cin, 0.0f, (Wb - hi) * Cin);
|
||||
hvx_splat_u(row + (size_t) hi * Cin * x_es, zero, (Wb - hi) * Cin, x_es);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void conv2d_hmx_cwhn_f16(__fp16 * restrict dst, const float * restrict src, uint32_t KH, uint32_t SR, uint32_t n) {
|
||||
static void conv2d_hmx_cwhn_f16(__fp16 * restrict dst, const float * restrict src, uint32_t KH, uint32_t SR, uint32_t n, bool src_f16) {
|
||||
for (uint32_t kh = 0; kh < KH; kh++) {
|
||||
hvx_copy_f16_f32_aa((uint8_t *) (dst + (size_t) kh * SR), (const uint8_t *) (src + (size_t) kh * SR), n);
|
||||
if (src_f16) {
|
||||
hvx_copy_f16_aa((uint8_t *) (dst + (size_t) kh * SR), (const uint8_t *) (src + (size_t) kh * SR), n);
|
||||
} else {
|
||||
hvx_copy_f16_f32_aa((uint8_t *) (dst + (size_t) kh * SR), (const uint8_t *) (src + (size_t) kh * SR), n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Stage the segment of col tiles starting at t, returns its number of col tiles
|
||||
static uint32_t conv2d_hmx_issue_seg(const struct htp_ops_context * octx, const struct htp_conv2d_kernel_params * kp,
|
||||
const struct htp_conv_geom * g, dma_queue * dmaq, float * stage, uint32_t t, uint32_t t1) {
|
||||
const struct htp_conv_geom * g, dma_queue * dmaq, float * stage, uint32_t t, uint32_t t1,
|
||||
uint32_t c0, uint32_t cc) {
|
||||
const uint32_t OH = g->OH;
|
||||
const uint32_t n_owt = kp->tiled.n_ow_tiles;
|
||||
const uint32_t row = fastdiv(t, &kp->tiled.div_ow_tiles);
|
||||
@@ -456,8 +608,10 @@ static uint32_t conv2d_hmx_issue_seg(const struct htp_ops_context * octx, const
|
||||
|
||||
if (kp->flags & HTP_CONV2D_FLAG_CWHN) {
|
||||
conv2d_hmx_stage_cwhn(octx, kp, dmaq, stage, n, oh, owt * 32, G);
|
||||
} else if (kp->flags & HTP_CONV2D_FLAG_STAGE_PC) {
|
||||
conv2d_hmx_stage_pc(octx, kp, g, dmaq, stage, n, oh, owt * 32, G, c0, cc);
|
||||
} else {
|
||||
conv2d_hmx_stage(octx, kp, g, dmaq, stage, n, oh, owt * 32, G);
|
||||
conv2d_hmx_stage(octx, kp, g, dmaq, stage, n, oh, owt * 32, G, c0, cc);
|
||||
}
|
||||
return G;
|
||||
}
|
||||
@@ -719,8 +873,8 @@ static void conv2d_hmx_wpack_kmajor_thread(const htp_thread_t * th, unsigned int
|
||||
|
||||
dma_queue * dmaq = th->dma_q;
|
||||
const size_t w_raw_size = (size_t) WK * 64 * sizeof(float);
|
||||
uint8_t * wraws[2] = { hc->thr_base + (size_t) ith * kp->thr_size,
|
||||
hc->thr_base + (size_t) ith * kp->thr_size + w_raw_size };
|
||||
uint8_t * wraws[2] = { hc->spad_base + (size_t) ith * kp->spad_per_hvx,
|
||||
hc->spad_base + (size_t) ith * kp->spad_per_hvx + w_raw_size };
|
||||
HVX_Vector * vin = (HVX_Vector *) (wraws[1] + w_raw_size);
|
||||
HVX_Vector * scratch = vin + 32;
|
||||
HVX_Vector * sink = scratch + 32;
|
||||
@@ -786,78 +940,125 @@ static void conv2d_hmx_pack(const struct htp_conv2d_hmx_context * hc, const htp_
|
||||
const uint32_t d0 = (uint32_t) hc->g->d0;
|
||||
const uint32_t n_vec = kp->tiled.n_k_tiles * (HMX_FP16_TILE_N_ELMS / 64);
|
||||
|
||||
dma_queue * dmaq = th->dma_q;
|
||||
uint8_t * thr = hc->thr_base + (size_t) ith * kp->thr_size;
|
||||
const bool cwhn = (kp->flags & HTP_CONV2D_FLAG_CWHN) != 0;
|
||||
const bool kmajor = (kp->flags & HTP_CONV2D_FLAG_W_KMAJOR) != 0;
|
||||
const bool src_f16 = octx->src[1]->type == HTP_TYPE_F16;
|
||||
const uint32_t n_stage = kp->tiled.n_stage;
|
||||
|
||||
struct htp_conv2d_tiled_spad_layout sl;
|
||||
htp_conv2d_tiled_spad_layout_build(&sl, kp->stage_size, n_stage, kp->tiled.seg_tiles, cwhn, kmajor, kp->tiled.wk_chunk);
|
||||
|
||||
dma_queue * dmaq = th->dma_q;
|
||||
uint8_t * spad = hc->spad_base + (size_t) ith * kp->spad_per_hvx;
|
||||
float * stages[2] = {
|
||||
(float *) thr,
|
||||
(float *) (thr + kp->stage_size),
|
||||
VTCM_LAYOUT_PTR(float, spad, sl.off_stage[0]),
|
||||
VTCM_LAYOUT_PTR(float, spad, sl.off_stage[1]),
|
||||
};
|
||||
__fp16 * act = hc->act[hc->pack_chunk & 1];
|
||||
__fp16 * act = hc->act[hc->pack_chunk & 1];
|
||||
|
||||
// CWHN: fp16 stage (+256 bytes of over-read slack), then 32 + 32 vectors for the transpose
|
||||
const uint32_t n_stage = kp->tiled.n_stage > 1 ? 2 : 1;
|
||||
__fp16 * stage16 = (__fp16 *) (thr + (size_t) n_stage * kp->stage_size + (size_t) 2 * 32 * kp->tiled.seg_tiles * 32 * sizeof(float));
|
||||
HVX_Vector * vin = (HVX_Vector *) ((uint8_t *) stage16 + kp->stage_size / 2 + 256);
|
||||
HVX_Vector * scratch = vin + 32;
|
||||
__fp16 * stage16 = VTCM_LAYOUT_PTR_OPTIONAL(__fp16, spad, sl.off_stage16, cwhn);
|
||||
HVX_Vector * vin = VTCM_LAYOUT_PTR_OPTIONAL(HVX_Vector, spad, sl.off_vin, cwhn);
|
||||
HVX_Vector * scratch = VTCM_LAYOUT_PTR_OPTIONAL(HVX_Vector, spad, sl.off_scratch, cwhn);
|
||||
const uint32_t gpr = kp->tiled.n_groups == KH ? 1 : KW;
|
||||
|
||||
// WHCN stages CC input channels per step. Chunk k range is [c0 * kc, (c0 + cc) * kc), even except for the last chunk.
|
||||
const uint32_t CC = kp->tiled.cin_chunk;
|
||||
const uint32_t kc = KH * KW;
|
||||
|
||||
// threads left idle by a small col tile chunk split the channel chunks of each col tile
|
||||
const struct htp_conv2d_chunk_plan * pp = hc->pack_plan;
|
||||
const uint32_t n_tt = pp->n_tt;
|
||||
const uint32_t n_cs = pp->n_cs;
|
||||
const uint32_t ci = fastdiv(ith, &pp->div_n_tt);
|
||||
const uint32_t ti = ith - ci * n_tt;
|
||||
const uint32_t cc0 = ci * pp->cc_q + MIN(ci, pp->cc_r);
|
||||
const uint32_t cc1 = cc0 + pp->cc_q + (ci < pp->cc_r ? 1 : 0);
|
||||
const uint32_t cbeg = MIN(Cin, cc0 * CC);
|
||||
const uint32_t cend = MIN(Cin, cc1 * CC);
|
||||
|
||||
uint32_t t0, t1;
|
||||
const uint32_t cs = conv2d_hmx_chunk_range(hc, hc->pack_chunk, hc->pack_tpt, ith, &t0, &t1);
|
||||
const uint32_t cs = conv2d_hmx_chunk_range(hc, hc->pack_chunk, pp->tpt, ti, &t0, &t1);
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
|
||||
if (t0 >= t1) {
|
||||
if (ith >= n_tt * n_cs) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Reuse output staging after unpack has drained its DMA.
|
||||
HVX_Vector * gather_tmp = (HVX_Vector *) (thr + (size_t) n_stage * kp->stage_size);
|
||||
const HVX_Vector offsets = (kp->flags & HTP_CONV2D_FLAG_CWHN) || s0 <= 2 ? Q6_V_vzero() : hvx_vec_gather_offsets_w(s0 * sizeof(float));
|
||||
HVX_Vector * gather_tmp = VTCM_LAYOUT_PTR(HVX_Vector, spad, sl.off_ostage[0]);
|
||||
const HVX_Vector offsets = cwhn || s0 <= 2 ? Q6_V_vzero() : hvx_vec_gather_offsets_w(s0 * sizeof(float));
|
||||
|
||||
// A step stages channels [c, c + CC) of one segment: all channel chunks of a segment, then the next segment
|
||||
uint32_t ng[2] = { 0, 0 };
|
||||
uint32_t nc[2] = { 0, 0 };
|
||||
uint32_t tq = t0;
|
||||
uint32_t cq = cbeg;
|
||||
for (uint32_t i = 0; i < n_stage && tq < t1; i++) {
|
||||
ng[i] = conv2d_hmx_issue_seg(octx, kp, hc->g, dmaq, stages[i], tq, t1);
|
||||
tq += ng[i];
|
||||
ng[i] = conv2d_hmx_issue_seg(octx, kp, hc->g, dmaq, stages[i], tq, t1, cq, MIN(CC, cend - cq));
|
||||
nc[i] = cq;
|
||||
cq += CC;
|
||||
if (cq >= cend) {
|
||||
cq = cbeg;
|
||||
tq += ng[i];
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t bi = 0;
|
||||
for (uint32_t t = t0; t < t1;) {
|
||||
const uint32_t G = ng[bi];
|
||||
const uint32_t c0 = nc[bi];
|
||||
const uint32_t cc = MIN(CC, cend - c0);
|
||||
float * stage = stages[bi];
|
||||
|
||||
for (uint32_t p = 0; p < KH; p++) {
|
||||
const uint32_t n_dma = (kp->flags & HTP_CONV2D_FLAG_STAGE_PC) ? cc * kp->tiled.kd : KH;
|
||||
for (uint32_t p = 0; p < n_dma; p++) {
|
||||
dma_queue_pop(dmaq);
|
||||
}
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) t);
|
||||
if (kp->flags & HTP_CONV2D_FLAG_CWHN) {
|
||||
if (cwhn) {
|
||||
const uint32_t Wb = 32 * G * s0 + (KW - 1) * d0;
|
||||
conv2d_hmx_cwhn_f16(stage16, stage, KH, kp->tiled.stage_row, Wb * Cin);
|
||||
conv2d_hmx_cwhn_f16(stage16, stage, KH, kp->tiled.stage_row, Wb * Cin, src_f16);
|
||||
for (uint32_t g = 0; g < G; g++) {
|
||||
__fp16 * tile = act + (size_t) (t - cs + g) * n_vec * 64;
|
||||
conv2d_hmx_pack_tile_cwhn(tile, stage16 + (size_t) g * 32 * Cin * s0, vin, scratch, kp, gpr, Cin, s0, d0);
|
||||
}
|
||||
} else {
|
||||
if (src_f16) {
|
||||
const uint32_t Wb = 32 * G * s0 + (KW - 1) * d0;
|
||||
conv2d_hmx_expand_rows_f16_to_f32(stage, kp->tiled.stage_row, Wb, cc * KH);
|
||||
}
|
||||
const uint32_t k0 = c0 * kc;
|
||||
const uint32_t nk = cc * kc;
|
||||
const uint32_t nv = c0 + cc == Cin ? n_vec - k0 / 2 : nk / 2;
|
||||
for (uint32_t g = 0; g < G; g++) {
|
||||
__fp16 * tile = act + (size_t) (t - cs + g) * n_vec * 64;
|
||||
__fp16 * tile = act + (size_t) (t - cs + g) * n_vec * 64 + (size_t) (k0 / 2) * 64;
|
||||
const float * pixels = stage + (size_t) g * 32 * s0;
|
||||
if (s0 == 1) {
|
||||
conv2d_hmx_pack_tile(tile, pixels, kp->tiled.k_size, KW, 1, d0, kp->tiled.stage_row, n_vec, offsets, gather_tmp);
|
||||
conv2d_hmx_pack_tile(tile, pixels, nk, KW, 1, d0, kp->tiled.stage_row, nv, offsets, gather_tmp);
|
||||
} else if (s0 == 2) {
|
||||
conv2d_hmx_pack_tile(tile, pixels, kp->tiled.k_size, KW, 2, d0, kp->tiled.stage_row, n_vec, offsets, gather_tmp);
|
||||
conv2d_hmx_pack_tile(tile, pixels, nk, KW, 2, d0, kp->tiled.stage_row, nv, offsets, gather_tmp);
|
||||
} else {
|
||||
conv2d_hmx_pack_tile(tile, pixels, kp->tiled.k_size, KW, s0, d0, kp->tiled.stage_row, n_vec, offsets, gather_tmp);
|
||||
conv2d_hmx_pack_tile(tile, pixels, nk, KW, s0, d0, kp->tiled.stage_row, nv, offsets, gather_tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) t);
|
||||
|
||||
if (tq < t1) {
|
||||
ng[bi] = conv2d_hmx_issue_seg(octx, kp, hc->g, dmaq, stage, tq, t1);
|
||||
tq += ng[bi];
|
||||
ng[bi] = conv2d_hmx_issue_seg(octx, kp, hc->g, dmaq, stage, tq, t1, cq, MIN(CC, cend - cq));
|
||||
nc[bi] = cq;
|
||||
cq += CC;
|
||||
if (cq >= cend) {
|
||||
cq = cbeg;
|
||||
tq += ng[bi];
|
||||
}
|
||||
}
|
||||
if (c0 + cc >= cend) {
|
||||
t += G;
|
||||
}
|
||||
t += G;
|
||||
bi = bi + 1 == n_stage ? 0 : bi + 1;
|
||||
}
|
||||
}
|
||||
@@ -873,11 +1074,20 @@ static void conv2d_hmx_unpack(const struct htp_conv2d_hmx_context * hc, const ht
|
||||
const uint32_t n_owt = kp->tiled.n_ow_tiles;
|
||||
const uint32_t OS = kp->tiled.seg_tiles * 32;
|
||||
|
||||
const bool cwhn = (kp->flags & HTP_CONV2D_FLAG_CWHN) != 0;
|
||||
const bool kmajor = (kp->flags & HTP_CONV2D_FLAG_W_KMAJOR) != 0;
|
||||
const bool dst_f16 = dst->type == HTP_TYPE_F16;
|
||||
const size_t y_es = dst_f16 ? sizeof(__fp16) : sizeof(float);
|
||||
const uint32_t n_stage = kp->tiled.n_stage;
|
||||
|
||||
struct htp_conv2d_tiled_spad_layout sl;
|
||||
htp_conv2d_tiled_spad_layout_build(&sl, kp->stage_size, n_stage, kp->tiled.seg_tiles, cwhn, kmajor, kp->tiled.wk_chunk);
|
||||
|
||||
dma_queue * dmaq = th->dma_q;
|
||||
uint8_t * thr = hc->thr_base + (size_t) ith * kp->thr_size + (size_t) (kp->tiled.n_stage > 1 ? 2 : 1) * kp->stage_size;
|
||||
uint8_t * spad = hc->spad_base + (size_t) ith * kp->spad_per_hvx;
|
||||
float * ostage[2] = {
|
||||
(float *) thr,
|
||||
(float *) (thr + (size_t) 32 * OS * sizeof(float)),
|
||||
VTCM_LAYOUT_PTR(float, spad, sl.off_ostage[0]),
|
||||
VTCM_LAYOUT_PTR(float, spad, sl.off_ostage[1]),
|
||||
};
|
||||
const __fp16 * out = hc->out[hc->unpack_chunk & 1];
|
||||
|
||||
@@ -922,10 +1132,13 @@ static void conv2d_hmx_unpack(const struct htp_conv2d_hmx_context * hc, const ht
|
||||
*(HVX_Vector *) (ob + (size_t) (2 * r + 1) * OS + g * 32) = hvx_vec_mul_f32_f32(Q6_V_hi_W(pr), restore);
|
||||
}
|
||||
}
|
||||
if (dst_f16) {
|
||||
conv2d_hmx_pack_rows_f32_to_f16(ob, OS, nv, nco);
|
||||
}
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_O_PROC, (uint16_t) t);
|
||||
|
||||
const dma_addr_t ddr = dst->data + (size_t) n * hc->g->y_nb_n + (size_t) oh * dst->nb[1] + (size_t) co0 * hc->g->y_nb_c + (size_t) ow0 * sizeof(float);
|
||||
dma_queue_push(dmaq, dma_make_data(ddr, ob), hc->g->y_nb_c, (size_t) OS * sizeof(float), (size_t) nv * sizeof(float), nco);
|
||||
const dma_addr_t ddr = dst->data + (size_t) n * hc->g->y_nb_n + (size_t) oh * dst->nb[1] + (size_t) co0 * hc->g->y_nb_c + (size_t) ow0 * y_es;
|
||||
dma_queue_push(dmaq, dma_make_data(ddr, ob), hc->g->y_nb_c, (size_t) OS * sizeof(float), (size_t) nv * y_es, nco);
|
||||
n_pushed++;
|
||||
b ^= 1;
|
||||
}
|
||||
@@ -960,20 +1173,20 @@ static void conv2d_hmx_phase_thread(const htp_thread_t * th, unsigned int nth, u
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint32_t conv2d_hmx_tpt(const struct htp_conv2d_hmx_context * hc, int32_t c, uint32_t nth) {
|
||||
if (c < 0) {
|
||||
return 0;
|
||||
}
|
||||
const uint32_t cs = hc->t_base + (uint32_t) c * hc->kp->ct_chunk;
|
||||
const uint32_t nct = MIN(hc->kp->ct_chunk, hc->t_end - cs);
|
||||
return (nct + nth - 1) / nth;
|
||||
static void conv2d_hmx_chunk_plan_init(struct htp_conv2d_chunk_plan * p, uint32_t nct, uint32_t n_cc, uint32_t nth) {
|
||||
p->tpt = (nct + nth - 1) / nth;
|
||||
p->n_tt = (nct + p->tpt - 1) / p->tpt;
|
||||
p->n_cs = MIN(n_cc, nth / p->n_tt);
|
||||
p->cc_q = n_cc / p->n_cs;
|
||||
p->cc_r = n_cc - p->cc_q * p->n_cs;
|
||||
p->div_n_tt = init_fastdiv_values(p->n_tt);
|
||||
}
|
||||
|
||||
static void conv2d_hmx_run_phase(struct htp_conv2d_hmx_context * hc, int32_t pack, int32_t unpack, uint32_t nth) {
|
||||
hc->pack_chunk = pack;
|
||||
hc->unpack_chunk = unpack;
|
||||
hc->pack_tpt = conv2d_hmx_tpt(hc, pack, nth);
|
||||
hc->unpack_tpt = conv2d_hmx_tpt(hc, unpack, nth);
|
||||
hc->pack_plan = pack >= 0 ? &hc->plan[pack == hc->last_chunk] : NULL;
|
||||
hc->unpack_tpt = unpack >= 0 ? hc->plan[unpack == hc->last_chunk].tpt : 0;
|
||||
work_queue_run(hc->octx->ctx->hvx_queue, conv2d_hmx_phase_thread, hc, nth);
|
||||
}
|
||||
|
||||
@@ -993,7 +1206,7 @@ static int conv2d_hmx_tiled(struct htp_ops_context * octx, const struct htp_conv
|
||||
const size_t out_size = (size_t) kp->rt_block * kp->ct_chunk * tile;
|
||||
|
||||
struct htp_conv2d_vtcm_layout layout;
|
||||
htp_conv2d_vtcm_layout_build(&layout, kp->rt_block, w_size, act_size, out_size, kp->thr_size, kp->n_hvx);
|
||||
htp_conv2d_vtcm_layout_build(&layout, kp->rt_block, w_size, act_size, out_size, kp->spad_per_hvx, kp->n_hvx);
|
||||
|
||||
uint8_t * base = (uint8_t *) octx->ctx->vtcm_base;
|
||||
|
||||
@@ -1006,19 +1219,30 @@ static int conv2d_hmx_tiled(struct htp_ops_context * octx, const struct htp_conv
|
||||
.w_tiles = VTCM_LAYOUT_PTR(__fp16, base, layout.off_w),
|
||||
.act = { VTCM_LAYOUT_PTR(__fp16, base, layout.off_act[0]), VTCM_LAYOUT_PTR(__fp16, base, layout.off_act[1]) },
|
||||
.out = { VTCM_LAYOUT_PTR(__fp16, base, layout.off_out[0]), VTCM_LAYOUT_PTR(__fp16, base, layout.off_out[1]) },
|
||||
.thr_base = VTCM_LAYOUT_PTR(uint8_t, base, layout.off_thr),
|
||||
.spad_base = VTCM_LAYOUT_PTR(uint8_t, base, layout.off_spad),
|
||||
.t_base = row_base * kp->tiled.n_ow_tiles,
|
||||
.t_end = (row_base + n_rows) * kp->tiled.n_ow_tiles,
|
||||
};
|
||||
|
||||
const uint32_t nth = kp->n_hvx;
|
||||
const int32_t n_chunks = (int32_t) ((hc.t_end - hc.t_base + kp->ct_chunk - 1) / kp->ct_chunk);
|
||||
const uint32_t nth = kp->n_hvx;
|
||||
const uint32_t total_tiles = hc.t_end - hc.t_base;
|
||||
const int32_t n_chunks = (int32_t) ((total_tiles + kp->ct_chunk - 1) / kp->ct_chunk);
|
||||
const uint32_t tail_nct = total_tiles - (uint32_t) (n_chunks - 1) * kp->ct_chunk;
|
||||
hc.last_chunk = n_chunks - 1;
|
||||
conv2d_hmx_chunk_plan_init(&hc.plan[0], kp->ct_chunk, kp->tiled.n_cin_chunks, nth);
|
||||
conv2d_hmx_chunk_plan_init(&hc.plan[1], tail_nct, kp->tiled.n_cin_chunks, nth);
|
||||
|
||||
struct htp_conv2d_hmx_task task;
|
||||
|
||||
const uint32_t wpack_n_ks = (kp->tiled.n_k_tiles * HMX_FP16_TILE_N_COLS + kp->tiled.wk_chunk - 1) / kp->tiled.wk_chunk;
|
||||
|
||||
for (uint32_t rt0 = 0; rt0 < kp->n_row_tiles; rt0 += kp->rt_block) {
|
||||
hc.rt0 = rt0;
|
||||
hc.n_rt = MIN(kp->rt_block, kp->n_row_tiles - rt0);
|
||||
hc.rt0 = rt0;
|
||||
hc.n_rt = MIN(kp->rt_block, kp->n_row_tiles - rt0);
|
||||
hc.wpack_n_kp = MIN(wpack_n_ks, MAX(1u, nth / hc.n_rt));
|
||||
hc.wpack_ks_q = wpack_n_ks / hc.wpack_n_kp;
|
||||
hc.wpack_ks_r = wpack_n_ks - hc.wpack_ks_q * hc.wpack_n_kp;
|
||||
hc.wpack_div_n_kp = init_fastdiv_values(hc.wpack_n_kp);
|
||||
work_queue_run(octx->ctx->hvx_queue, (kp->flags & HTP_CONV2D_FLAG_W_KMAJOR) ? conv2d_hmx_wpack_kmajor_thread : conv2d_hmx_wpack_thread, &hc, nth);
|
||||
|
||||
conv2d_hmx_run_phase(&hc, 0, -1, nth);
|
||||
@@ -1071,7 +1295,7 @@ struct htp_conv2d_win_context {
|
||||
__fp16 * w_tiles; // [rt][tap][cc] weight tiles
|
||||
__fp16 * act[2]; // [cc][b] croutons of a chunk
|
||||
__fp16 * out[2]; // [rt][tile] output tiles of a chunk
|
||||
uint8_t * thr_base;
|
||||
uint8_t * spad_base;
|
||||
uint32_t t_base; // device col tile range
|
||||
uint32_t t_end;
|
||||
uint32_t vr0; // device virtual output row range
|
||||
@@ -1082,6 +1306,7 @@ struct htp_conv2d_win_context {
|
||||
int32_t unpack_chunk;
|
||||
uint32_t n_scat; // small Cin: scatters per crouton into zeroed croutons, else 0
|
||||
uint8_t * live[2]; // per chunk: tiles with an output row to write
|
||||
struct fastdiv_values div_nth;
|
||||
};
|
||||
|
||||
struct htp_conv2d_win_task {
|
||||
@@ -1340,13 +1565,13 @@ static void conv2d_hmx_win_wpack_kmajor(const struct htp_conv2d_win_context * wc
|
||||
|
||||
dma_queue * dmaq = th->dma_q;
|
||||
const size_t w_raw_size = (size_t) TG * 32 * VLEN;
|
||||
uint8_t * raws[2] = { wc->thr_base + (size_t) ith * kp->thr_size,
|
||||
wc->thr_base + (size_t) ith * kp->thr_size + w_raw_size };
|
||||
uint8_t * raws[2] = { wc->spad_base + (size_t) ith * kp->spad_per_hvx,
|
||||
wc->spad_base + (size_t) ith * kp->spad_per_hvx + w_raw_size };
|
||||
const HVX_Vector zero = Q6_V_vzero();
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
|
||||
const uint32_t n_tg = (T + TG - 1) / TG;
|
||||
const uint32_t n_tg = kp->window.w_n_seg;
|
||||
const uint32_t n_units = wc->n_rt * n_cc * n_tg;
|
||||
|
||||
struct conv2d_win_wpack_kmajor_task tasks[2];
|
||||
@@ -1517,12 +1742,12 @@ static void conv2d_hmx_win_wpack_rows(const struct htp_conv2d_win_context * wc,
|
||||
|
||||
const size_t wraw = (size_t) 32 * ((CS * T + kpw - 1) / kpw) * VLEN;
|
||||
dma_queue * dmaq = th->dma_q;
|
||||
uint8_t * raws[2] = { wc->thr_base + (size_t) ith * kp->thr_size, wc->thr_base + (size_t) ith * kp->thr_size + wraw };
|
||||
uint8_t * raws[2] = { wc->spad_base + (size_t) ith * kp->spad_per_hvx, wc->spad_base + (size_t) ith * kp->spad_per_hvx + wraw };
|
||||
uint8_t * xr = raws[1] + wraw;
|
||||
HVX_Vector * vin = (HVX_Vector *) (xr + wraw);
|
||||
HVX_Vector * scratch = vin + 32;
|
||||
|
||||
const uint32_t n_cs = (32 + CS - 1) / CS;
|
||||
const uint32_t n_cs = kp->window.w_n_seg;
|
||||
const uint32_t n_units = wc->n_rt * kp->window.n_cc * n_cs;
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
@@ -1571,8 +1796,10 @@ static uint32_t conv2d_hmx_win_stage(const struct htp_conv2d_win_context * wc, d
|
||||
const uint32_t IHS = kp->window.ihs;
|
||||
const uint32_t p0 = octx->op_params[2];
|
||||
const uint32_t p1 = octx->op_params[3];
|
||||
const uint32_t span = kp->window.seg_b * 32;
|
||||
const bool cwhn = (kp->flags & HTP_CONV2D_FLAG_CWHN) != 0;
|
||||
const uint32_t span = kp->window.seg_b * 32;
|
||||
const bool cwhn = (kp->flags & HTP_CONV2D_FLAG_CWHN) != 0;
|
||||
const uint32_t x_es = input->type == HTP_TYPE_F16 ? sizeof(__fp16) : sizeof(float);
|
||||
const HVX_Vector zero = Q6_V_vzero();
|
||||
|
||||
// rows of one image are contiguous when there is no column padding
|
||||
const bool rows_contig = p0 == 0 && input->nb[1] == (size_t) IW * input->nb[0];
|
||||
@@ -1590,10 +1817,10 @@ static uint32_t conv2d_hmx_win_stage(const struct htp_conv2d_win_context * wc, d
|
||||
if (n >= N || ih < 0 || ih >= (int32_t) IH) {
|
||||
const uint32_t len = end - u;
|
||||
if (cwhn) {
|
||||
hvx_splat_f32_u(stage + (size_t) (u - ua) * Cin, 0.0f, len * Cin);
|
||||
hvx_splat_u((uint8_t *) stage + (size_t) (u - ua) * Cin * x_es, zero, len * Cin, x_es);
|
||||
} else {
|
||||
for (uint32_t c = 0; c < Cin; c++) {
|
||||
hvx_splat_f32_u(stage + (size_t) c * span + (u - ua), 0.0f, len);
|
||||
hvx_splat_u((uint8_t *) (stage + (size_t) c * span) + (size_t) (u - ua) * x_es, zero, len, x_es);
|
||||
}
|
||||
}
|
||||
u = end;
|
||||
@@ -1615,10 +1842,10 @@ static uint32_t conv2d_hmx_win_stage(const struct htp_conv2d_win_context * wc, d
|
||||
if (a > col) {
|
||||
const uint32_t z = MIN(a, col + len) - col;
|
||||
if (cwhn) {
|
||||
hvx_splat_f32_u(stage + (size_t) (u - ua) * Cin, 0.0f, z * Cin);
|
||||
hvx_splat_u((uint8_t *) stage + (size_t) (u - ua) * Cin * x_es, zero, z * Cin, x_es);
|
||||
} else {
|
||||
for (uint32_t c = 0; c < Cin; c++) {
|
||||
hvx_splat_f32_u(stage + (size_t) c * span + (u - ua), 0.0f, z);
|
||||
hvx_splat_u((uint8_t *) (stage + (size_t) c * span) + (size_t) (u - ua) * x_es, zero, z, x_es);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1626,10 +1853,10 @@ static uint32_t conv2d_hmx_win_stage(const struct htp_conv2d_win_context * wc, d
|
||||
const uint32_t z0 = MAX(a, b);
|
||||
const uint32_t z = col + len - z0;
|
||||
if (cwhn) {
|
||||
hvx_splat_f32_u(stage + (size_t) (u - ua + z0 - col) * Cin, 0.0f, z * Cin);
|
||||
hvx_splat_u((uint8_t *) stage + (size_t) (u - ua + z0 - col) * Cin * x_es, zero, z * Cin, x_es);
|
||||
} else {
|
||||
for (uint32_t c = 0; c < Cin; c++) {
|
||||
hvx_splat_f32_u(stage + (size_t) c * span + (u - ua + z0 - col), 0.0f, z);
|
||||
hvx_splat_u((uint8_t *) (stage + (size_t) c * span) + (size_t) (u - ua + z0 - col) * x_es, zero, z, x_es);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1637,11 +1864,11 @@ static uint32_t conv2d_hmx_win_stage(const struct htp_conv2d_win_context * wc, d
|
||||
const dma_addr_t src = input->data + (size_t) n * input->nb[3] + (size_t) ih * input->nb[1] + (size_t) (a - p0) * input->nb[0];
|
||||
const uint32_t off = u - ua + (a - col);
|
||||
if (cwhn) {
|
||||
const size_t sz = (size_t) (b - a) * Cin * sizeof(float);
|
||||
dma_queue_push(dmaq, dma_make_data(stage + (size_t) off * Cin, src), sz, sz, sz, 1);
|
||||
const size_t sz = (size_t) (b - a) * Cin * x_es;
|
||||
dma_queue_push(dmaq, dma_make_data((uint8_t *) stage + (size_t) off * Cin * x_es, src), sz, sz, sz, 1);
|
||||
} else {
|
||||
dma_queue_push(dmaq, dma_make_data(stage + off, src), (size_t) span * sizeof(float), input->nb[2],
|
||||
(size_t) (b - a) * sizeof(float), Cin);
|
||||
dma_queue_push(dmaq, dma_make_data((uint8_t *) stage + (size_t) off * x_es, src), (size_t) span * sizeof(float), input->nb[2],
|
||||
(size_t) (b - a) * x_es, Cin);
|
||||
}
|
||||
nd++;
|
||||
}
|
||||
@@ -1659,7 +1886,7 @@ static inline float * conv2d_hmx_win_fold_raw(float * stage) {
|
||||
|
||||
static inline uint32_t conv2d_hmx_win_fold_rplane(const struct htp_conv2d_win_context * wc) {
|
||||
const uint32_t IW = wc->octx->src[1]->ne[0];
|
||||
return ((wc->kp->window.seg_b * 32 + wc->kp->window.sr - 1) / wc->kp->window.sr + 1) * IW;
|
||||
return (fastdiv(wc->kp->window.seg_b * 32 + wc->kp->window.sr - 1, &wc->kp->window.div_sr) + 1) * IW;
|
||||
}
|
||||
|
||||
// DMA the raw input rows of [ua, ub), returns the number of DMAs pushed
|
||||
@@ -1690,15 +1917,16 @@ static uint32_t conv2d_hmx_win_stage_fold(const struct htp_conv2d_win_context *
|
||||
continue;
|
||||
}
|
||||
// rows of one image are contiguous in DDR
|
||||
const uint32_t nr = cont ? MIN(vr_b - vr, (uint32_t) (IH - ih)) : 1;
|
||||
const dma_addr_t src = input->data + (size_t) n * input->nb[3] + (size_t) ih * input->nb[1];
|
||||
const uint32_t nr = cont ? MIN(vr_b - vr, (uint32_t) (IH - ih)) : 1;
|
||||
const size_t x_es = input->type == HTP_TYPE_F16 ? sizeof(__fp16) : sizeof(float);
|
||||
const dma_addr_t src = input->data + (size_t) n * input->nb[3] + (size_t) ih * input->nb[1];
|
||||
if (kp->flags & HTP_CONV2D_FLAG_CWHN) {
|
||||
// raw[vr - vr_a][iw][c]
|
||||
const size_t sz = (size_t) nr * IW * Cin * sizeof(float);
|
||||
dma_queue_push(dmaq, dma_make_data(raw + (size_t) (vr - vr_a) * IW * Cin, src), sz, sz, sz, 1);
|
||||
const size_t sz = (size_t) nr * IW * Cin * x_es;
|
||||
dma_queue_push(dmaq, dma_make_data((uint8_t *) raw + (size_t) (vr - vr_a) * IW * Cin * x_es, src), sz, sz, sz, 1);
|
||||
} else {
|
||||
dma_queue_push(dmaq, dma_make_data(raw + (size_t) (vr - vr_a) * IW, src), (size_t) RP * sizeof(float), input->nb[2],
|
||||
(size_t) nr * IW * sizeof(float), Cin);
|
||||
dma_queue_push(dmaq, dma_make_data((uint8_t *) raw + (size_t) (vr - vr_a) * IW * x_es, src), (size_t) RP * sizeof(float), input->nb[2],
|
||||
(size_t) nr * IW * x_es, Cin);
|
||||
}
|
||||
nd++;
|
||||
vr += nr;
|
||||
@@ -2003,13 +2231,19 @@ static void conv2d_hmx_win_pack(const struct htp_conv2d_win_context * wc, const
|
||||
uint32_t ts;
|
||||
const uint32_t nt = conv2d_hmx_win_chunk_tiles(wc, wc->pack_chunk, &ts);
|
||||
const uint32_t ncb = MIN(kp->window.n_bc, nt + (kp->window.max_off >> 5) + 1);
|
||||
const uint32_t bpt = (ncb + nth - 1) / nth;
|
||||
const uint32_t bpt = fastdiv(ncb + nth - 1, &wc->div_nth);
|
||||
const uint32_t b0 = MIN(ith * bpt, ncb);
|
||||
const uint32_t b1 = MIN(b0 + bpt, ncb);
|
||||
|
||||
struct htp_conv2d_win_spad_layout sl;
|
||||
htp_conv2d_win_spad_layout_build(&sl, kp->stage_size, kp->window.seg_t, 0);
|
||||
|
||||
dma_queue * dmaq = th->dma_q;
|
||||
uint8_t * thr = wc->thr_base + (size_t) ith * kp->thr_size;
|
||||
float * stages[2] = { (float *) thr, (float *) (thr + kp->stage_size) };
|
||||
uint8_t * spad = wc->spad_base + (size_t) ith * kp->spad_per_hvx;
|
||||
float * stages[2] = {
|
||||
VTCM_LAYOUT_PTR(float, spad, sl.off_stage[0]),
|
||||
VTCM_LAYOUT_PTR(float, spad, sl.off_stage[1]),
|
||||
};
|
||||
__fp16 * act = wc->act[wc->pack_chunk & 1];
|
||||
|
||||
uint32_t (*stage_fn)(const struct htp_conv2d_win_context *, dma_queue *, float *, uint32_t, uint32_t) =
|
||||
@@ -2039,6 +2273,27 @@ static void conv2d_hmx_win_pack(const struct htp_conv2d_win_context * wc, const
|
||||
}
|
||||
|
||||
htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_A_PREP, (uint16_t) b);
|
||||
if (wc->octx->src[1]->type == HTP_TYPE_F16) {
|
||||
const uint32_t Cin = wc->octx->src[1]->ne[2];
|
||||
const bool cwhn = (kp->flags & HTP_CONV2D_FLAG_CWHN) != 0;
|
||||
if (kp->window.fold) {
|
||||
const uint32_t ua = (ts + b) * 32;
|
||||
const uint32_t ub = (ts + b + nb) * 32;
|
||||
const uint32_t vr_a = fastdiv(ua, &kp->window.div_sr);
|
||||
const uint32_t vr_b = fastdiv(ub - 1, &kp->window.div_sr) + 1;
|
||||
const uint32_t IW = wc->octx->src[1]->ne[0];
|
||||
float * raw = conv2d_hmx_win_fold_raw(stages[si]);
|
||||
if (cwhn) {
|
||||
conv2d_hmx_expand_f16_to_f32_inplace(raw, (vr_b - vr_a) * IW * Cin);
|
||||
} else {
|
||||
conv2d_hmx_expand_rows_f16_to_f32(raw, conv2d_hmx_win_fold_rplane(wc), (vr_b - vr_a) * IW, Cin);
|
||||
}
|
||||
} else if (cwhn) {
|
||||
conv2d_hmx_expand_f16_to_f32_inplace(stages[si], nb * 32 * Cin);
|
||||
} else {
|
||||
conv2d_hmx_expand_rows_f16_to_f32(stages[si], kp->window.seg_b * 32, nb * 32, Cin);
|
||||
}
|
||||
}
|
||||
if (kp->window.fold && (kp->flags & HTP_CONV2D_FLAG_CWHN)) {
|
||||
conv2d_hmx_win_croutons_fold_cwhn(wc, stages[si], act, b, nb, (ts + b) * 32);
|
||||
} else if (kp->window.fold) {
|
||||
@@ -2069,13 +2324,16 @@ static void conv2d_hmx_win_unpack(const struct htp_conv2d_win_context * wc, cons
|
||||
|
||||
uint32_t ts;
|
||||
const uint32_t nt = conv2d_hmx_win_chunk_tiles(wc, wc->unpack_chunk, &ts);
|
||||
const uint32_t tpt = (nt + nth - 1) / nth;
|
||||
const uint32_t tpt = fastdiv(nt + nth - 1, &wc->div_nth);
|
||||
const uint32_t i0 = MIN(ith * tpt, nt);
|
||||
const uint32_t i1 = MIN(i0 + tpt, nt);
|
||||
|
||||
struct htp_conv2d_win_spad_layout sl;
|
||||
htp_conv2d_win_spad_layout_build(&sl, kp->stage_size, kp->window.seg_t, 0);
|
||||
|
||||
dma_queue * dmaq = th->dma_q;
|
||||
uint8_t * thr = wc->thr_base + (size_t) ith * kp->thr_size + 2 * (size_t) kp->stage_size;
|
||||
float * obuf = (float *) thr;
|
||||
uint8_t * spad = wc->spad_base + (size_t) ith * kp->spad_per_hvx;
|
||||
float * obuf = VTCM_LAYOUT_PTR(float, spad, sl.off_obuf);
|
||||
__fp16 * out = wc->out[wc->unpack_chunk & 1];
|
||||
|
||||
struct htp_thread_trace * tr = th->trace;
|
||||
@@ -2084,7 +2342,9 @@ static void conv2d_hmx_win_unpack(const struct htp_conv2d_win_context * wc, cons
|
||||
uint32_t nd[HTP_CONV2D_WIN_OBUF] = { 0 };
|
||||
uint32_t bi = 0;
|
||||
|
||||
const bool rows_contig = SR == OW && dst->nb[1] == OW * sizeof(float);
|
||||
const bool dst_f16 = dst->type == HTP_TYPE_F16;
|
||||
const size_t y_es = dst_f16 ? sizeof(__fp16) : sizeof(float);
|
||||
const bool rows_contig = SR == OW && dst->nb[1] == OW * y_es;
|
||||
|
||||
for (uint32_t i = i0; i < i1; i += ST) {
|
||||
const uint32_t ni = MIN(ST, i1 - i);
|
||||
@@ -2123,6 +2383,9 @@ static void conv2d_hmx_win_unpack(const struct htp_conv2d_win_context * wc, cons
|
||||
o[(2 * k + 1) * (OS / 32)] = hvx_vec_mul_f32_f32(Q6_V_hi_W(pr), restore);
|
||||
}
|
||||
}
|
||||
if (dst_f16) {
|
||||
conv2d_hmx_pack_rows_f32_to_f16(ob, OS, ni * 32, nco);
|
||||
}
|
||||
htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_O_PROC, (uint16_t) i);
|
||||
|
||||
// write the valid part of each virtual row in [q_lo, q_hi)
|
||||
@@ -2140,7 +2403,7 @@ static void conv2d_hmx_win_unpack(const struct htp_conv2d_win_context * wc, cons
|
||||
if (b <= a) {
|
||||
continue;
|
||||
}
|
||||
const dma_addr_t ddr = dst->data + (size_t) n * dst->nb[3] + (size_t) oh * dst->nb[1] + (size_t) co0 * dst->nb[2] + (size_t) a * sizeof(float);
|
||||
const dma_addr_t ddr = dst->data + (size_t) n * dst->nb[3] + (size_t) oh * dst->nb[1] + (size_t) co0 * dst->nb[2] + (size_t) a * y_es;
|
||||
|
||||
// no gap between rows: extend over the following rows of this image
|
||||
if (rows_contig) {
|
||||
@@ -2151,8 +2414,8 @@ static void conv2d_hmx_win_unpack(const struct htp_conv2d_win_context * wc, cons
|
||||
b = MIN(q_hi - base, (r - 1) * SR + OW);
|
||||
vr += r - 1;
|
||||
}
|
||||
dma_queue_push(dmaq, dma_make_data(ddr, ob + (base + a - q_lo)), dst->nb[2], (size_t) OS * sizeof(float),
|
||||
(size_t) (b - a) * sizeof(float), nco);
|
||||
dma_queue_push(dmaq, dma_make_data(ddr, (uint8_t *) ob + (size_t) (base + a - q_lo) * y_es), dst->nb[2], (size_t) OS * sizeof(float),
|
||||
(size_t) (b - a) * y_es, nco);
|
||||
nd[bi]++;
|
||||
}
|
||||
bi = bi + 1 == HTP_CONV2D_WIN_OBUF ? 0 : bi + 1;
|
||||
@@ -2192,7 +2455,7 @@ static void conv2d_hmx_win_zero_thread(const htp_thread_t * th, unsigned int nth
|
||||
const struct htp_conv2d_win_context * wc = (const struct htp_conv2d_win_context *) data;
|
||||
|
||||
const size_t n = 2 * wc->layout.act_size / sizeof(float);
|
||||
const size_t per = hex_round_up((n + nth - 1) / nth, VLEN_FP32);
|
||||
const size_t per = hex_round_up(fastdiv((uint32_t) (n + nth - 1), &wc->div_nth), VLEN_FP32);
|
||||
const size_t i0 = MIN((size_t) ith * per, n);
|
||||
const size_t i1 = MIN(i0 + per, n);
|
||||
|
||||
@@ -2265,7 +2528,7 @@ static int conv2d_hmx_win(struct htp_ops_context * octx, const struct htp_conv2d
|
||||
const size_t out_size = (size_t) kp->rt_block * kp->ct_chunk * tile;
|
||||
|
||||
struct htp_conv2d_vtcm_layout layout;
|
||||
htp_conv2d_vtcm_layout_build(&layout, kp->rt_block, w_size, act_size, out_size, kp->thr_size, kp->n_hvx);
|
||||
htp_conv2d_vtcm_layout_build(&layout, kp->rt_block, w_size, act_size, out_size, kp->spad_per_hvx, kp->n_hvx);
|
||||
|
||||
uint8_t * base = (uint8_t *) octx->ctx->vtcm_base;
|
||||
|
||||
@@ -2277,11 +2540,12 @@ static int conv2d_hmx_win(struct htp_ops_context * octx, const struct htp_conv2d
|
||||
.w_tiles = VTCM_LAYOUT_PTR(__fp16, base, layout.off_w),
|
||||
.act = { VTCM_LAYOUT_PTR(__fp16, base, layout.off_act[0]), VTCM_LAYOUT_PTR(__fp16, base, layout.off_act[1]) },
|
||||
.out = { VTCM_LAYOUT_PTR(__fp16, base, layout.off_out[0]), VTCM_LAYOUT_PTR(__fp16, base, layout.off_out[1]) },
|
||||
.thr_base = VTCM_LAYOUT_PTR(uint8_t, base, layout.off_thr),
|
||||
.spad_base = VTCM_LAYOUT_PTR(uint8_t, base, layout.off_spad),
|
||||
.t_base = (vr0 * SR) / 32,
|
||||
.t_end = ((vr1 - 1) * SR + OW + 31) / 32,
|
||||
.vr0 = vr0,
|
||||
.vr1 = vr1,
|
||||
.div_nth = init_fastdiv_values(kp->n_hvx),
|
||||
};
|
||||
|
||||
// small Cin: croutons are built by scattering the staged values into zeroed croutons
|
||||
|
||||
@@ -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 {
|
||||
@@ -94,7 +103,7 @@ struct htp_conv2d_vtcm_layout {
|
||||
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;
|
||||
};
|
||||
@@ -105,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);
|
||||
@@ -116,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
|
||||
|
||||
Reference in New Issue
Block a user