ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-hexagon / htp / dsv4-hc-ops.c
| 1 | #pragma clang diagnostic ignored "-Wunused-variable" |
| 2 | #pragma clang diagnostic ignored "-Wunused-function" |
| 3 | |
| 4 | #include <HAP_farf.h> |
| 5 | |
| 6 | #include <stdint.h> |
| 7 | #include <string.h> |
| 8 | #include <math.h> |
| 9 | |
| 10 | #include "dma-queue.h" |
| 11 | #include "hex-common.h" |
| 12 | #include "hex-profile.h" |
| 13 | #include "htp-ctx.h" |
| 14 | #include "htp-tensor.h" |
| 15 | #include "hvx-utils.h" |
| 16 | #include "dsv4-hc-ops.h" |
| 17 | |
| 18 | // --------------------------------------------------------------------------- |
| 19 | // DSV4_HC_PRE |
| 20 | // --------------------------------------------------------------------------- |
| 21 | |
| 22 | // hvx_vec_fast_sigmoid_f32_guard_2it with fewer live constants and predicates |
| 23 | static inline HVX_Vector dsv4_hc_sigmoid_f32(HVX_Vector v, HVX_Vector max_exp, HVX_Vector min_exp) { |
| 24 | v = Q6_Vsf_vmin_VsfVsf(Q6_Vsf_vmax_VsfVsf(v, min_exp), max_exp); |
| 25 | v = Q6_Vqf32_vmpy_VsfVsf(v, hvx_vec_splat_f32(0.5f * 1.442695022f)); |
| 26 | |
| 27 | HVX_Vector in_int = Q6_Vw_equals_Vsf(Q6_Vsf_equals_Vqf32(v)); |
| 28 | HVX_Vector x = Q6_Vqf32_vsub_Vqf32Vsf(v, Q6_Vsf_equals_Vw(in_int)); |
| 29 | HVX_Vector xx = Q6_Vqf32_vmpy_Vqf32Vqf32(x, x); |
| 30 | |
| 31 | HVX_Vector v1 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(xx), Q6_V_vsplat_R(FAST_SIGMOID_C2)); |
| 32 | v1 = Q6_Vqf32_vadd_Vqf32Vsf(v1, Q6_V_vsplat_R(FAST_SIGMOID_LOG2F)); |
| 33 | |
| 34 | HVX_Vector v2 = Q6_Vqf32_vmpy_VsfVsf(Q6_Vsf_equals_Vqf32(x), Q6_V_vsplat_R(FAST_SIGMOID_C1)); |
| 35 | v2 = Q6_Vqf32_vmpy_Vqf32Vqf32(v2, xx); |
| 36 | v2 = Q6_Vqf32_vadd_Vqf32Vqf32(v2, x); |
| 37 | |
| 38 | HVX_Vector v3 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vqf32(v2, v1)); |
| 39 | v3 = Q6_Vw_vaslacc_VwVwR(v3, in_int, 24); |
| 40 | |
| 41 | HVX_Vector v4 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vqf32(v2, v1)); |
| 42 | HVX_Vector v5 = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_VsfVsf(v3, v4)); |
| 43 | |
| 44 | // Newton-Raphson with 2 iterations |
| 45 | HVX_Vector r = Q6_Vw_vsub_VwVw(Q6_V_vsplat_R(0x7EEEEBB3), v5); |
| 46 | for (int i = 0; i < 2; ++i) { |
| 47 | const HVX_Vector rv = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(r, v5)); |
| 48 | r = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vsub_Vqf32Vqf32(Q6_Vqf32_vadd_VsfVsf(r, r), Q6_Vqf32_vmpy_VsfVsf(r, rv))); |
| 49 | } |
| 50 | |
| 51 | return Q6_Vsf_equals_Vqf32(Q6_Vqf32_vmpy_VsfVsf(v3, r)); |
| 52 | } |
| 53 | |
| 54 | struct dsv4_hc_pre_context { |
| 55 | struct htp_ops_context * octx; |
| 56 | const struct htp_dsv4_hc_pre_kernel_params * kparams; |
| 57 | uint32_t token_start; |
| 58 | uint32_t n_chunks; |
| 59 | uint32_t total_jobs; |
| 60 | uint32_t jobs_per_thread; |
| 61 | float scale; |
| 62 | uint8_t * vtcm; |
| 63 | uint32_t dma_block_chunks; |
| 64 | uint32_t dma_row_stride; |
| 65 | uint32_t dma_buffer_size; |
| 66 | }; |
| 67 | |
| 68 | struct dsv4_hc_block { |
| 69 | uint32_t it; |
| 70 | uint32_t chunk; |
| 71 | uint32_t count; |
| 72 | }; |
| 73 | |
| 74 | static inline struct dsv4_hc_block dsv4_hc_pre_block_make( |
| 75 | const struct dsv4_hc_pre_context * dctx, |
| 76 | uint32_t job, |
| 77 | uint32_t end) { |
| 78 | const uint32_t local_token = fastdiv(job, &dctx->kparams->div_n_chunks); |
| 79 | const uint32_t chunk = job - local_token*dctx->n_chunks; |
| 80 | const uint32_t count = hex_smin(dctx->dma_block_chunks, hex_smin(dctx->n_chunks - chunk, end - job)); |
| 81 | return (struct dsv4_hc_block) { |
| 82 | .it = dctx->token_start + local_token, |
| 83 | .chunk = chunk, |
| 84 | .count = count, |
| 85 | }; |
| 86 | } |
| 87 | |
| 88 | static inline void dsv4_hc_pre_push_plain( |
| 89 | dma_queue * dma_q, |
| 90 | const struct dsv4_hc_pre_context * dctx, |
| 91 | const struct dsv4_hc_block * block, |
| 92 | uint8_t * spad) { |
| 93 | const struct htp_tensor * x = dctx->octx->src[0]; |
| 94 | const struct htp_tensor * weights = dctx->octx->src[1]; |
| 95 | const uint32_t row_stride = dctx->dma_row_stride; |
| 96 | const uint32_t row_size = block->count * VLEN; |
| 97 | |
| 98 | dma_queue_push(dma_q, dma_make_data(spad, x->data + (block->it*x->nb[2] + block->chunk*VLEN)), row_stride, x->nb[1], row_size, 4); |
| 99 | dma_queue_push(dma_q, dma_make_data(spad + 5*row_stride, weights->data + (block->it*weights->nb[1])), 16, 16, 16, 1); |
| 100 | } |
| 101 | |
| 102 | static inline void dsv4_hc_pre_push_gated( |
| 103 | dma_queue * dma_q, |
| 104 | const struct dsv4_hc_pre_context * dctx, |
| 105 | const struct dsv4_hc_block * block, |
| 106 | uint8_t * spad) { |
| 107 | const struct htp_tensor * x = dctx->octx->src[0]; |
| 108 | const struct htp_tensor * g = dctx->octx->src[1]; |
| 109 | const uint32_t row_stride = dctx->dma_row_stride; |
| 110 | const uint32_t row_size = block->count * VLEN; |
| 111 | |
| 112 | dma_queue_push(dma_q, dma_make_data(spad, x->data + (block->it*x->nb[2] + block->chunk*VLEN)), row_stride, x->nb[1], row_size, 4); |
| 113 | dma_queue_push(dma_q, dma_make_data(spad + 4*row_stride, g->data + (block->it*g->nb[2] + block->chunk*VLEN)), row_stride, g->nb[1], row_size, 4); |
| 114 | } |
| 115 | |
| 116 | static __attribute__((noinline)) void dsv4_hc_pre_plain_block_f32( |
| 117 | const struct dsv4_hc_pre_context * dctx, |
| 118 | const struct dsv4_hc_block * block, |
| 119 | const uint8_t * x_spad, |
| 120 | uint8_t * d_spad, |
| 121 | const float * wb) { |
| 122 | const uint32_t row_stride = dctx->dma_row_stride; |
| 123 | const HVX_Vector scale_v = hvx_vec_splat_f32(dctx->scale); |
| 124 | const HVX_Vector w0 = hvx_vec_splat_f32(wb[0]); |
| 125 | const HVX_Vector w1 = hvx_vec_splat_f32(wb[1]); |
| 126 | const HVX_Vector w2 = hvx_vec_splat_f32(wb[2]); |
| 127 | const HVX_Vector w3 = hvx_vec_splat_f32(wb[3]); |
| 128 | |
| 129 | for (uint32_t c = 0; c < block->count; ++c) { |
| 130 | const uint8_t * xb = x_spad + c*VLEN; |
| 131 | |
| 132 | HVX_Vector x0 = *(const HVX_Vector *) xb; |
| 133 | HVX_Vector x1 = *(const HVX_Vector *) (xb + row_stride); |
| 134 | HVX_Vector x2 = *(const HVX_Vector *) (xb + 2*row_stride); |
| 135 | HVX_Vector x3 = *(const HVX_Vector *) (xb + 3*row_stride); |
| 136 | |
| 137 | HVX_Vector acc = hvx_vec_mul_f32_f32(x0, w0); |
| 138 | acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(x1, w1)); |
| 139 | acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(x2, w2)); |
| 140 | acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(x3, w3)); |
| 141 | |
| 142 | *(HVX_Vector *) (d_spad + c*VLEN) = hvx_vec_mul_f32_f32(acc, scale_v); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static __attribute__((noinline)) void dsv4_hc_pre_gated_block_f32( |
| 147 | const struct dsv4_hc_pre_context * dctx, |
| 148 | const struct dsv4_hc_block * block, |
| 149 | const uint8_t * x_spad, |
| 150 | const uint8_t * g_spad, |
| 151 | uint8_t * d_spad) { |
| 152 | const uint32_t row_stride = dctx->dma_row_stride; |
| 153 | const HVX_Vector scale_v = hvx_vec_splat_f32(dctx->scale); |
| 154 | const HVX_Vector max_exp = hvx_vec_splat_f32(87.0f); |
| 155 | const HVX_Vector min_exp = hvx_vec_splat_f32(-87.0f); |
| 156 | |
| 157 | for (uint32_t c = 0; c < block->count; ++c) { |
| 158 | const uint8_t * xb = x_spad + c*VLEN; |
| 159 | const uint8_t * gb = g_spad + c*VLEN; |
| 160 | |
| 161 | HVX_Vector xv = *(const HVX_Vector *) xb; |
| 162 | HVX_Vector gv = dsv4_hc_sigmoid_f32(*(const HVX_Vector *) gb, max_exp, min_exp); |
| 163 | HVX_Vector acc = hvx_vec_mul_f32_f32(xv, gv); |
| 164 | |
| 165 | xv = *(const HVX_Vector *) (xb + row_stride); |
| 166 | gv = dsv4_hc_sigmoid_f32(*(const HVX_Vector *) (gb + row_stride), max_exp, min_exp); |
| 167 | acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(xv, gv)); |
| 168 | |
| 169 | xv = *(const HVX_Vector *) (xb + 2*row_stride); |
| 170 | gv = dsv4_hc_sigmoid_f32(*(const HVX_Vector *) (gb + 2*row_stride), max_exp, min_exp); |
| 171 | acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(xv, gv)); |
| 172 | |
| 173 | xv = *(const HVX_Vector *) (xb + 3*row_stride); |
| 174 | gv = dsv4_hc_sigmoid_f32(*(const HVX_Vector *) (gb + 3*row_stride), max_exp, min_exp); |
| 175 | acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(xv, gv)); |
| 176 | |
| 177 | *(HVX_Vector *) (d_spad + c*VLEN) = hvx_vec_mul_f32_f32(acc, scale_v); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static void dsv4_hc_pre_thread_plain_f32(unsigned int nth, unsigned int ith, void * data) { |
| 182 | struct dsv4_hc_pre_context * dctx = (struct dsv4_hc_pre_context *) data; |
| 183 | struct htp_ops_context * octx = dctx->octx; |
| 184 | |
| 185 | const struct htp_tensor * dst = octx->dst; |
| 186 | const uint64_t d_data = dst->data; |
| 187 | |
| 188 | const uint32_t row_stride = dctx->dma_row_stride; |
| 189 | const uint32_t buffer_size = dctx->dma_buffer_size; |
| 190 | const uint32_t start = dctx->jobs_per_thread * ith; |
| 191 | const uint32_t end = hex_smin(start + dctx->jobs_per_thread, dctx->total_jobs); |
| 192 | if (start >= end) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | uint8_t * spad[2] = { |
| 197 | dctx->vtcm + 2*buffer_size*ith, |
| 198 | dctx->vtcm + 2*buffer_size*ith + buffer_size, |
| 199 | }; |
| 200 | |
| 201 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 202 | uint32_t prefetch_job = start; |
| 203 | for (uint32_t i = 0; i < 2 && prefetch_job < end; ++i) { |
| 204 | const struct dsv4_hc_block block = dsv4_hc_pre_block_make(dctx, prefetch_job, end); |
| 205 | // empty dst writeback keeps pop order dst, x, weights in loop |
| 206 | dma_queue_push(dma_q, dma_make_data(d_data, spad[i] + 4*row_stride), block.count*VLEN, block.count*VLEN, 0, 0); |
| 207 | dsv4_hc_pre_push_plain(dma_q, dctx, &block, spad[i]); |
| 208 | prefetch_job += block.count; |
| 209 | } |
| 210 | |
| 211 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 212 | |
| 213 | uint32_t job = start; |
| 214 | uint32_t curr = 0; |
| 215 | while (job < end) { |
| 216 | const struct dsv4_hc_block block = dsv4_hc_pre_block_make(dctx, job, end); |
| 217 | |
| 218 | dma_queue_pop(dma_q); |
| 219 | dma_queue_pop(dma_q); |
| 220 | dma_queue_pop(dma_q); |
| 221 | |
| 222 | const float * wb = (const float *) (spad[curr] + 5*row_stride); |
| 223 | uint8_t * d_spad = spad[curr] + 4*row_stride; |
| 224 | |
| 225 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it); |
| 226 | dsv4_hc_pre_plain_block_f32(dctx, &block, spad[curr], d_spad, wb); |
| 227 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it); |
| 228 | |
| 229 | const uint64_t d_dst = d_data + (block.it*dst->nb[1] + block.chunk*VLEN); |
| 230 | dma_queue_push(dma_q, dma_make_data(d_dst, d_spad), block.count*VLEN, block.count*VLEN, block.count*VLEN, 1); |
| 231 | |
| 232 | if (prefetch_job < end) { |
| 233 | const struct dsv4_hc_block next = dsv4_hc_pre_block_make(dctx, prefetch_job, end); |
| 234 | dsv4_hc_pre_push_plain(dma_q, dctx, &next, spad[curr]); |
| 235 | prefetch_job += next.count; |
| 236 | } |
| 237 | |
| 238 | job += block.count; |
| 239 | curr ^= 1; |
| 240 | } |
| 241 | |
| 242 | dma_queue_flush(dma_q); |
| 243 | |
| 244 | (void) nth; |
| 245 | } |
| 246 | |
| 247 | static void dsv4_hc_pre_thread_gated_f32(unsigned int nth, unsigned int ith, void * data) { |
| 248 | struct dsv4_hc_pre_context * dctx = (struct dsv4_hc_pre_context *) data; |
| 249 | struct htp_ops_context * octx = dctx->octx; |
| 250 | |
| 251 | const struct htp_tensor * dst = octx->dst; |
| 252 | const uint64_t d_data = dst->data; |
| 253 | |
| 254 | const uint32_t row_stride = dctx->dma_row_stride; |
| 255 | const uint32_t buffer_size = dctx->dma_buffer_size; |
| 256 | const uint32_t start = dctx->jobs_per_thread * ith; |
| 257 | const uint32_t end = hex_smin(start + dctx->jobs_per_thread, dctx->total_jobs); |
| 258 | if (start >= end) { |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | uint8_t * spad[2] = { |
| 263 | dctx->vtcm + 2*buffer_size*ith, |
| 264 | dctx->vtcm + 2*buffer_size*ith + buffer_size, |
| 265 | }; |
| 266 | |
| 267 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 268 | uint32_t prefetch_job = start; |
| 269 | for (uint32_t i = 0; i < 2 && prefetch_job < end; ++i) { |
| 270 | const struct dsv4_hc_block block = dsv4_hc_pre_block_make(dctx, prefetch_job, end); |
| 271 | // empty dst writeback keeps pop order dst, x, g in loop |
| 272 | dma_queue_push(dma_q, dma_make_data(d_data, spad[i] + 8*row_stride), block.count*VLEN, block.count*VLEN, 0, 0); |
| 273 | dsv4_hc_pre_push_gated(dma_q, dctx, &block, spad[i]); |
| 274 | prefetch_job += block.count; |
| 275 | } |
| 276 | |
| 277 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 278 | |
| 279 | uint32_t job = start; |
| 280 | uint32_t curr = 0; |
| 281 | while (job < end) { |
| 282 | const struct dsv4_hc_block block = dsv4_hc_pre_block_make(dctx, job, end); |
| 283 | |
| 284 | dma_queue_pop(dma_q); |
| 285 | dma_queue_pop(dma_q); |
| 286 | dma_queue_pop(dma_q); |
| 287 | |
| 288 | uint8_t * d_spad = spad[curr] + 8*row_stride; |
| 289 | |
| 290 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it); |
| 291 | dsv4_hc_pre_gated_block_f32(dctx, &block, spad[curr], spad[curr] + 4*row_stride, d_spad); |
| 292 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it); |
| 293 | |
| 294 | const uint64_t d_dst = d_data + (block.it*dst->nb[1] + block.chunk*VLEN); |
| 295 | dma_queue_push(dma_q, dma_make_data(d_dst, d_spad), block.count*VLEN, block.count*VLEN, block.count*VLEN, 1); |
| 296 | |
| 297 | if (prefetch_job < end) { |
| 298 | const struct dsv4_hc_block next = dsv4_hc_pre_block_make(dctx, prefetch_job, end); |
| 299 | dsv4_hc_pre_push_gated(dma_q, dctx, &next, spad[curr]); |
| 300 | prefetch_job += next.count; |
| 301 | } |
| 302 | |
| 303 | job += block.count; |
| 304 | curr ^= 1; |
| 305 | } |
| 306 | |
| 307 | dma_queue_flush(dma_q); |
| 308 | |
| 309 | (void) nth; |
| 310 | } |
| 311 | |
| 312 | int op_dsv4_hc_pre(struct htp_ops_context * octx) { |
| 313 | const struct htp_tensor * x = octx->src[0]; |
| 314 | const struct htp_tensor * weights = octx->src[1]; |
| 315 | const struct htp_tensor * dst = octx->dst; |
| 316 | |
| 317 | const struct htp_dsv4_hc_pre_kernel_params * kparams = |
| 318 | (const struct htp_dsv4_hc_pre_kernel_params *) octx->kernel_params; |
| 319 | |
| 320 | const uint32_t n_threads = kparams->n_threads; |
| 321 | if (!htp_ops_context_set_n_threads_div(octx, n_threads, kparams->div_n_threads)) { |
| 322 | return HTP_STATUS_INVAL_PARAMS; |
| 323 | } |
| 324 | |
| 325 | uint32_t token_start = 0; |
| 326 | uint32_t n_tokens = x->ne[2]; |
| 327 | uint32_t total_jobs = kparams->total_jobs; |
| 328 | uint32_t jobs_per_thread = kparams->jobs_per_thread; |
| 329 | if (octx->ctx->mdev.count > 1) { |
| 330 | const bool can_split = htp_tensor_mdev_data_aligned(dst) && ((dst->nb[1] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0); |
| 331 | const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(n_tokens, can_split ? 1 : 0, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); |
| 332 | token_start = range.start; |
| 333 | n_tokens = range.count; |
| 334 | total_jobs = n_tokens * kparams->n_chunks; |
| 335 | jobs_per_thread = fastdiv(total_jobs + n_threads - 1, &kparams->div_n_threads); |
| 336 | } |
| 337 | |
| 338 | if (n_tokens == 0) { |
| 339 | return HTP_STATUS_OK; |
| 340 | } |
| 341 | |
| 342 | float scale; |
| 343 | memcpy(&scale, octx->op_params, sizeof(scale)); |
| 344 | |
| 345 | struct dsv4_hc_pre_context dctx = { |
| 346 | .octx = octx, |
| 347 | .kparams = kparams, |
| 348 | .token_start = token_start, |
| 349 | .n_chunks = kparams->n_chunks, |
| 350 | .total_jobs = total_jobs, |
| 351 | .jobs_per_thread = jobs_per_thread, |
| 352 | .scale = scale, |
| 353 | .vtcm = (uint8_t *) octx->ctx->vtcm_base, |
| 354 | .dma_block_chunks = kparams->dma_block_chunks, |
| 355 | .dma_row_stride = kparams->dma_row_stride, |
| 356 | .dma_buffer_size = kparams->dma_buffer_size, |
| 357 | }; |
| 358 | |
| 359 | work_queue_func_t worker = NULL; |
| 360 | switch (kparams->kernel_type) { |
| 361 | case HTP_DSV4_HC_PRE_KERNEL_PLAIN: worker = dsv4_hc_pre_thread_plain_f32; break; |
| 362 | case HTP_DSV4_HC_PRE_KERNEL_GATED: worker = dsv4_hc_pre_thread_gated_f32; break; |
| 363 | default: return HTP_STATUS_INVAL_PARAMS; |
| 364 | } |
| 365 | |
| 366 | FARF(HIGH, "dsv4-hc-pre: (%ux%ux%u) -> (%ux%u) : ktype %u tokens %u:%u nth %u\n", |
| 367 | x->ne[0], x->ne[1], x->ne[2], dst->ne[0], dst->ne[1], kparams->kernel_type, |
| 368 | token_start, token_start + n_tokens, n_threads); |
| 369 | |
| 370 | work_queue_run(octx->ctx->work_queue, worker, &dctx, n_threads); |
| 371 | return HTP_STATUS_OK; |
| 372 | } |
| 373 | |
| 374 | // --------------------------------------------------------------------------- |
| 375 | // DSV4_HC_POST |
| 376 | // --------------------------------------------------------------------------- |
| 377 | |
| 378 | struct dsv4_hc_post_context { |
| 379 | struct htp_ops_context * octx; |
| 380 | const struct htp_dsv4_hc_post_kernel_params * kparams; |
| 381 | uint32_t token_start; |
| 382 | uint32_t total_jobs; |
| 383 | uint32_t jobs_per_thread; |
| 384 | uint8_t * vtcm; |
| 385 | }; |
| 386 | |
| 387 | static inline struct dsv4_hc_block dsv4_hc_post_block_make( |
| 388 | const struct dsv4_hc_post_context * dctx, |
| 389 | uint32_t job, |
| 390 | uint32_t end, |
| 391 | uint32_t block_chunks) { |
| 392 | const uint32_t local_token = fastdiv(job, &dctx->kparams->div_n_chunks); |
| 393 | const uint32_t chunk = job - local_token*dctx->kparams->n_chunks; |
| 394 | const uint32_t count = hex_smin(block_chunks, hex_smin(dctx->kparams->n_chunks - chunk, end - job)); |
| 395 | return (struct dsv4_hc_block) { |
| 396 | .it = dctx->token_start + local_token, |
| 397 | .chunk = chunk, |
| 398 | .count = count, |
| 399 | }; |
| 400 | } |
| 401 | |
| 402 | static __attribute__((noinline)) void dsv4_hc_post_identity_block_f32( |
| 403 | const uint8_t * xb, |
| 404 | const uint8_t * rb, |
| 405 | size_t r_stride, |
| 406 | uint8_t * db, |
| 407 | size_t d_stride, |
| 408 | uint32_t count, |
| 409 | const float * p) { |
| 410 | for (uint32_t id = 0; id < 4; ++id) { |
| 411 | const HVX_Vector pv = hvx_vec_splat_f32(p[id]); |
| 412 | const uint8_t * rr = rb + id*r_stride; |
| 413 | uint8_t * dd = db + id*d_stride; |
| 414 | |
| 415 | for (uint32_t i = 0; i < count; ++i) { |
| 416 | const HVX_Vector xv = *(const HVX_Vector *) (xb + i*VLEN); |
| 417 | const HVX_Vector rv = *(const HVX_Vector *) (rr + i*VLEN); |
| 418 | *(HVX_Vector *) (dd + i*VLEN) = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(xv, pv), rv); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | static __attribute__((noinline)) void dsv4_hc_post_comb_block_f32( |
| 424 | const uint8_t * xb, |
| 425 | const uint8_t * rb, |
| 426 | size_t r_stride, |
| 427 | uint8_t * db, |
| 428 | size_t d_stride, |
| 429 | uint32_t count, |
| 430 | const float * p, |
| 431 | const float * c) { |
| 432 | for (uint32_t id = 0; id < 4; ++id) { |
| 433 | const HVX_Vector pv = hvx_vec_splat_f32(p[id]); |
| 434 | const HVX_Vector c0 = hvx_vec_splat_f32(c[id]); |
| 435 | const HVX_Vector c1 = hvx_vec_splat_f32(c[4 + id]); |
| 436 | const HVX_Vector c2 = hvx_vec_splat_f32(c[8 + id]); |
| 437 | const HVX_Vector c3 = hvx_vec_splat_f32(c[12 + id]); |
| 438 | uint8_t * dd = db + id*d_stride; |
| 439 | |
| 440 | for (uint32_t i = 0; i < count; ++i) { |
| 441 | const uint8_t * r = rb + i*VLEN; |
| 442 | HVX_Vector acc = hvx_vec_mul_f32_f32(*(const HVX_Vector *) (xb + i*VLEN), pv); |
| 443 | acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(*(const HVX_Vector *) r, c0)); |
| 444 | acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(*(const HVX_Vector *) (r + r_stride), c1)); |
| 445 | acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(*(const HVX_Vector *) (r + 2*r_stride), c2)); |
| 446 | acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(*(const HVX_Vector *) (r + 3*r_stride), c3)); |
| 447 | *(HVX_Vector *) (dd + i*VLEN) = acc; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | static inline void dsv4_hc_post_push_identity( |
| 453 | dma_queue * dma_q, |
| 454 | const struct dsv4_hc_post_context * dctx, |
| 455 | const struct dsv4_hc_block * block, |
| 456 | uint8_t * spad) { |
| 457 | const struct htp_tensor * x = dctx->octx->src[0]; |
| 458 | const struct htp_tensor * residual = dctx->octx->src[1]; |
| 459 | const struct htp_tensor * post = dctx->octx->src[2]; |
| 460 | const uint32_t row_stride = dctx->kparams->dma_row_stride; |
| 461 | const uint32_t row_size = block->count * VLEN; |
| 462 | |
| 463 | dma_queue_push(dma_q, dma_make_data(spad, residual->data + (block->it*residual->nb[2] + block->chunk*VLEN)), |
| 464 | row_stride, residual->nb[1], row_size, 4); |
| 465 | dma_queue_push(dma_q, dma_make_data(spad + 4*row_stride, x->data + (block->it*x->nb[1] + block->chunk*VLEN)), |
| 466 | row_stride, row_size, row_size, 1); |
| 467 | dma_queue_push(dma_q, dma_make_data(spad + 9*row_stride, post->data + (block->it*post->nb[1])), |
| 468 | 16, 16, 16, 1); |
| 469 | } |
| 470 | |
| 471 | static inline void dsv4_hc_post_push_comb( |
| 472 | dma_queue * dma_q, |
| 473 | const struct dsv4_hc_post_context * dctx, |
| 474 | const struct dsv4_hc_block * block, |
| 475 | uint8_t * spad) { |
| 476 | const struct htp_tensor * comb = dctx->octx->src[3]; |
| 477 | const uint32_t row_stride = dctx->kparams->dma_row_stride; |
| 478 | |
| 479 | dsv4_hc_post_push_identity(dma_q, dctx, block, spad); |
| 480 | dma_queue_push(dma_q, dma_make_data(spad + 9*row_stride + 64, comb->data + (block->it*comb->nb[2])), |
| 481 | 16, comb->nb[1], 16, 4); |
| 482 | } |
| 483 | |
| 484 | static void dsv4_hc_post_thread_identity_f32(unsigned int nth, unsigned int ith, void * data) { |
| 485 | struct dsv4_hc_post_context * dctx = (struct dsv4_hc_post_context *) data; |
| 486 | struct htp_ops_context * octx = dctx->octx; |
| 487 | |
| 488 | const struct htp_tensor * dst = octx->dst; |
| 489 | const uint64_t d_data = dst->data; |
| 490 | |
| 491 | const uint32_t block_chunks = dctx->kparams->dma_block_chunks; |
| 492 | const uint32_t row_stride = dctx->kparams->dma_row_stride; |
| 493 | const uint32_t buffer_size = dctx->kparams->dma_buffer_size; |
| 494 | const uint32_t start = dctx->jobs_per_thread * ith; |
| 495 | const uint32_t end = hex_smin(start + dctx->jobs_per_thread, dctx->total_jobs); |
| 496 | if (start >= end) { |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | uint8_t * spad[2] = { |
| 501 | dctx->vtcm + 2*buffer_size*ith, |
| 502 | dctx->vtcm + 2*buffer_size*ith + buffer_size, |
| 503 | }; |
| 504 | |
| 505 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 506 | uint32_t prefetch_job = start; |
| 507 | for (uint32_t i = 0; i < 2 && prefetch_job < end; ++i) { |
| 508 | const struct dsv4_hc_block block = dsv4_hc_post_block_make(dctx, prefetch_job, end, block_chunks); |
| 509 | // empty dst writeback keeps pop order dst, residual, x, post in loop |
| 510 | dma_queue_push(dma_q, dma_make_data(d_data, spad[i] + 5*row_stride), dst->nb[1], row_stride, 0, 0); |
| 511 | dsv4_hc_post_push_identity(dma_q, dctx, &block, spad[i]); |
| 512 | prefetch_job += block.count; |
| 513 | } |
| 514 | |
| 515 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 516 | |
| 517 | uint32_t job = start; |
| 518 | uint32_t curr = 0; |
| 519 | while (job < end) { |
| 520 | const struct dsv4_hc_block block = dsv4_hc_post_block_make(dctx, job, end, block_chunks); |
| 521 | |
| 522 | dma_queue_pop(dma_q); |
| 523 | dma_queue_pop(dma_q); |
| 524 | dma_queue_pop(dma_q); |
| 525 | dma_queue_pop(dma_q); |
| 526 | |
| 527 | const float * p = (const float *) (spad[curr] + 9*row_stride); |
| 528 | uint8_t * d_spad = spad[curr] + 5*row_stride; |
| 529 | |
| 530 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it); |
| 531 | dsv4_hc_post_identity_block_f32(spad[curr] + 4*row_stride, spad[curr], row_stride, d_spad, row_stride, block.count, p); |
| 532 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it); |
| 533 | |
| 534 | const uint64_t d_dst = d_data + (block.it*dst->nb[2] + block.chunk*VLEN); |
| 535 | dma_queue_push(dma_q, dma_make_data(d_dst, d_spad), dst->nb[1], row_stride, block.count*VLEN, 4); |
| 536 | |
| 537 | if (prefetch_job < end) { |
| 538 | const struct dsv4_hc_block next = dsv4_hc_post_block_make(dctx, prefetch_job, end, block_chunks); |
| 539 | dsv4_hc_post_push_identity(dma_q, dctx, &next, spad[curr]); |
| 540 | prefetch_job += next.count; |
| 541 | } |
| 542 | |
| 543 | job += block.count; |
| 544 | curr ^= 1; |
| 545 | } |
| 546 | |
| 547 | dma_queue_flush(dma_q); |
| 548 | |
| 549 | (void) nth; |
| 550 | } |
| 551 | |
| 552 | static void dsv4_hc_post_thread_comb_f32(unsigned int nth, unsigned int ith, void * data) { |
| 553 | struct dsv4_hc_post_context * dctx = (struct dsv4_hc_post_context *) data; |
| 554 | struct htp_ops_context * octx = dctx->octx; |
| 555 | |
| 556 | const struct htp_tensor * dst = octx->dst; |
| 557 | const uint64_t d_data = dst->data; |
| 558 | |
| 559 | const uint32_t block_chunks = dctx->kparams->dma_block_chunks; |
| 560 | const uint32_t row_stride = dctx->kparams->dma_row_stride; |
| 561 | const uint32_t buffer_size = dctx->kparams->dma_buffer_size; |
| 562 | const uint32_t start = dctx->jobs_per_thread * ith; |
| 563 | const uint32_t end = hex_smin(start + dctx->jobs_per_thread, dctx->total_jobs); |
| 564 | if (start >= end) { |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | uint8_t * spad[2] = { |
| 569 | dctx->vtcm + 2*buffer_size*ith, |
| 570 | dctx->vtcm + 2*buffer_size*ith + buffer_size, |
| 571 | }; |
| 572 | |
| 573 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 574 | uint32_t prefetch_job = start; |
| 575 | for (uint32_t i = 0; i < 2 && prefetch_job < end; ++i) { |
| 576 | const struct dsv4_hc_block block = dsv4_hc_post_block_make(dctx, prefetch_job, end, block_chunks); |
| 577 | // empty dst writeback keeps pop order dst, residual, x, post, comb in loop |
| 578 | dma_queue_push(dma_q, dma_make_data(d_data, spad[i] + 5*row_stride), dst->nb[1], row_stride, 0, 0); |
| 579 | dsv4_hc_post_push_comb(dma_q, dctx, &block, spad[i]); |
| 580 | prefetch_job += block.count; |
| 581 | } |
| 582 | |
| 583 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 584 | |
| 585 | uint32_t job = start; |
| 586 | uint32_t curr = 0; |
| 587 | while (job < end) { |
| 588 | const struct dsv4_hc_block block = dsv4_hc_post_block_make(dctx, job, end, block_chunks); |
| 589 | |
| 590 | dma_queue_pop(dma_q); |
| 591 | dma_queue_pop(dma_q); |
| 592 | dma_queue_pop(dma_q); |
| 593 | dma_queue_pop(dma_q); |
| 594 | dma_queue_pop(dma_q); |
| 595 | |
| 596 | const float * p = (const float *) (spad[curr] + 9*row_stride); |
| 597 | const float * c = (const float *) (spad[curr] + 9*row_stride + 64); |
| 598 | uint8_t * d_spad = spad[curr] + 5*row_stride; |
| 599 | |
| 600 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it); |
| 601 | dsv4_hc_post_comb_block_f32(spad[curr] + 4*row_stride, spad[curr], row_stride, d_spad, row_stride, block.count, p, c); |
| 602 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) block.it); |
| 603 | |
| 604 | const uint64_t d_dst = d_data + (block.it*dst->nb[2] + block.chunk*VLEN); |
| 605 | dma_queue_push(dma_q, dma_make_data(d_dst, d_spad), dst->nb[1], row_stride, block.count*VLEN, 4); |
| 606 | |
| 607 | if (prefetch_job < end) { |
| 608 | const struct dsv4_hc_block next = dsv4_hc_post_block_make(dctx, prefetch_job, end, block_chunks); |
| 609 | dsv4_hc_post_push_comb(dma_q, dctx, &next, spad[curr]); |
| 610 | prefetch_job += next.count; |
| 611 | } |
| 612 | |
| 613 | job += block.count; |
| 614 | curr ^= 1; |
| 615 | } |
| 616 | |
| 617 | dma_queue_flush(dma_q); |
| 618 | |
| 619 | (void) nth; |
| 620 | } |
| 621 | |
| 622 | int op_dsv4_hc_post(struct htp_ops_context * octx) { |
| 623 | const struct htp_tensor * x = octx->src[0]; |
| 624 | const struct htp_tensor * residual = octx->src[1]; |
| 625 | const struct htp_tensor * dst = octx->dst; |
| 626 | |
| 627 | const struct htp_dsv4_hc_post_kernel_params * kparams = |
| 628 | (const struct htp_dsv4_hc_post_kernel_params *) octx->kernel_params; |
| 629 | |
| 630 | const uint32_t n_threads = kparams->n_threads; |
| 631 | if (!htp_ops_context_set_n_threads_div(octx, n_threads, kparams->div_n_threads)) { |
| 632 | return HTP_STATUS_INVAL_PARAMS; |
| 633 | } |
| 634 | |
| 635 | uint32_t token_start = 0; |
| 636 | uint32_t n_tokens = x->ne[1]; |
| 637 | uint32_t total_jobs = kparams->total_jobs; |
| 638 | uint32_t jobs_per_thread = kparams->jobs_per_thread; |
| 639 | if (octx->ctx->mdev.count > 1) { |
| 640 | const bool can_split = htp_tensor_mdev_data_aligned(dst) && ((dst->nb[2] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0); |
| 641 | const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition(n_tokens, can_split ? 1 : 0, octx->ctx->mdev.idx, octx->ctx->mdev.count, &octx->ctx->mdev.count_div); |
| 642 | token_start = range.start; |
| 643 | n_tokens = range.count; |
| 644 | total_jobs = n_tokens * kparams->n_chunks; |
| 645 | jobs_per_thread = fastdiv(total_jobs + n_threads - 1, &kparams->div_n_threads); |
| 646 | } |
| 647 | |
| 648 | if (n_tokens == 0) { |
| 649 | return HTP_STATUS_OK; |
| 650 | } |
| 651 | |
| 652 | struct dsv4_hc_post_context dctx = { |
| 653 | .octx = octx, |
| 654 | .kparams = kparams, |
| 655 | .token_start = token_start, |
| 656 | .total_jobs = total_jobs, |
| 657 | .jobs_per_thread = jobs_per_thread, |
| 658 | .vtcm = (uint8_t *) octx->ctx->vtcm_base, |
| 659 | }; |
| 660 | |
| 661 | work_queue_func_t worker = NULL; |
| 662 | switch (kparams->kernel_type) { |
| 663 | case HTP_DSV4_HC_POST_KERNEL_IDENTITY: worker = dsv4_hc_post_thread_identity_f32; break; |
| 664 | case HTP_DSV4_HC_POST_KERNEL_COMB: worker = dsv4_hc_post_thread_comb_f32; break; |
| 665 | default: return HTP_STATUS_INVAL_PARAMS; |
| 666 | } |
| 667 | |
| 668 | FARF(HIGH, "dsv4-hc-post: (%ux%u) (%ux%ux%u) -> (%ux%ux%u) : ktype %u tokens %u:%u nth %u\n", |
| 669 | x->ne[0], x->ne[1], residual->ne[0], residual->ne[1], residual->ne[2], |
| 670 | dst->ne[0], dst->ne[1], dst->ne[2], kparams->kernel_type, |
| 671 | token_start, token_start + n_tokens, n_threads); |
| 672 | |
| 673 | work_queue_run(octx->ctx->work_queue, worker, &dctx, n_threads); |
| 674 | return HTP_STATUS_OK; |
| 675 | } |