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 / gated-delta-net-ops.c
| 1 | #include <stdint.h> |
| 2 | #include <stdbool.h> |
| 3 | #include <string.h> |
| 4 | #include <math.h> |
| 5 | #include <HAP_farf.h> |
| 6 | |
| 7 | #include "hvx-base.h" |
| 8 | #include "hvx-copy.h" |
| 9 | #include "hvx-reduce.h" |
| 10 | #include "hvx-exp.h" |
| 11 | #include "dma-queue.h" |
| 12 | #include "ggml-common.h" |
| 13 | #include "htp-ctx.h" |
| 14 | #include "htp-tensor.h" |
| 15 | #include "htp-vtcm.h" |
| 16 | #include "hmx-utils.h" |
| 17 | #include "hmx-fa-kernels.h" |
| 18 | #include "hmx-queue.h" |
| 19 | #include "gated-delta-net-ops.h" |
| 20 | |
| 21 | #ifndef MIN |
| 22 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 23 | #endif |
| 24 | |
| 25 | struct htp_gdn_context { |
| 26 | struct htp_ops_context * octx; |
| 27 | const struct htp_gdn_kernel_params * kparams; |
| 28 | struct htp_gdn_vtcm_layout layout; |
| 29 | uint8_t * vtcm_base; |
| 30 | uint32_t row_start; |
| 31 | uint32_t nrows; |
| 32 | }; |
| 33 | |
| 34 | static inline HVX_Vector gdn_mul_dot_f32(float * restrict dst, const HVX_Vector * restrict mul, const HVX_Vector * restrict dot, uint32_t n) { |
| 35 | HVX_Vector acc = Q6_V_vzero(); |
| 36 | const uint32_t epv = 128 / sizeof(float); |
| 37 | const uint32_t nvec = n / epv; |
| 38 | const uint32_t nloe = n % epv; |
| 39 | for (uint32_t i = 0; i < nvec; ++i) { |
| 40 | HVX_Vector vd = hvx_vmemu(dst + i * epv); |
| 41 | HVX_Vector vm = mul[i]; |
| 42 | HVX_Vector vdot = dot[i]; |
| 43 | HVX_Vector out = hvx_vec_mul_f32_f32(vd, vm); |
| 44 | hvx_vmemu(dst + i * epv) = out; |
| 45 | acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(out, vdot)); |
| 46 | } |
| 47 | |
| 48 | if (nloe) { |
| 49 | const uint32_t off = nvec * epv; |
| 50 | HVX_Vector vm = mul[nvec]; |
| 51 | HVX_Vector vdot = dot[nvec]; |
| 52 | HVX_VectorPred mask = Q6_Q_vsetq2_R(nloe * sizeof(float)); |
| 53 | HVX_Vector zero = Q6_V_vzero(); |
| 54 | |
| 55 | HVX_Vector out = hvx_vec_mul_f32_f32(hvx_vmemu(dst + off), vm); |
| 56 | hvx_vec_store_u(dst + off, nloe * sizeof(float), out); |
| 57 | acc = hvx_vec_add_f32_f32(acc, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out, vdot), zero)); |
| 58 | } |
| 59 | |
| 60 | return hvx_vec_reduce_sum_f32(acc); |
| 61 | } |
| 62 | |
| 63 | static inline HVX_Vector gdn_mul_scalar_dot_f32(float * restrict dst, HVX_Vector vmul, const HVX_Vector * restrict dot, uint32_t n) { |
| 64 | HVX_Vector acc = Q6_V_vzero(); |
| 65 | const uint32_t epv = 128 / sizeof(float); |
| 66 | const uint32_t nvec = n / epv; |
| 67 | const uint32_t nloe = n % epv; |
| 68 | for (uint32_t i = 0; i < nvec; ++i) { |
| 69 | HVX_Vector vd = hvx_vmemu(dst + i * epv); |
| 70 | HVX_Vector vdot = dot[i]; |
| 71 | HVX_Vector out = hvx_vec_mul_f32_f32(vd, vmul); |
| 72 | hvx_vmemu(dst + i * epv) = out; |
| 73 | acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(out, vdot)); |
| 74 | } |
| 75 | |
| 76 | if (nloe) { |
| 77 | const uint32_t off = nvec * epv; |
| 78 | HVX_Vector vdot = dot[nvec]; |
| 79 | HVX_VectorPred mask = Q6_Q_vsetq2_R(nloe * sizeof(float)); |
| 80 | HVX_Vector zero = Q6_V_vzero(); |
| 81 | |
| 82 | HVX_Vector out = hvx_vec_mul_f32_f32(hvx_vmemu(dst + off), vmul); |
| 83 | hvx_vec_store_u(dst + off, nloe * sizeof(float), out); |
| 84 | acc = hvx_vec_add_f32_f32(acc, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out, vdot), zero)); |
| 85 | } |
| 86 | |
| 87 | return hvx_vec_reduce_sum_f32(acc); |
| 88 | } |
| 89 | |
| 90 | static inline HVX_Vector gdn_add_scaled_dot_f32(float * restrict dst, const HVX_Vector * restrict src, |
| 91 | HVX_Vector vscale, const HVX_Vector * restrict dot, uint32_t n) { |
| 92 | HVX_Vector acc = Q6_V_vzero(); |
| 93 | const uint32_t epv = 128 / sizeof(float); |
| 94 | const uint32_t nvec = n / epv; |
| 95 | const uint32_t nloe = n % epv; |
| 96 | for (uint32_t i = 0; i < nvec; ++i) { |
| 97 | HVX_Vector vd = hvx_vmemu(dst + i * epv); |
| 98 | HVX_Vector vs = src[i]; |
| 99 | HVX_Vector vdot = dot[i]; |
| 100 | HVX_Vector out = hvx_vec_add_f32_f32(vd, hvx_vec_mul_f32_f32(vs, vscale)); |
| 101 | hvx_vmemu(dst + i * epv) = out; |
| 102 | acc = hvx_vec_add_f32_f32(acc, hvx_vec_mul_f32_f32(out, vdot)); |
| 103 | } |
| 104 | |
| 105 | if (nloe) { |
| 106 | const uint32_t off = nvec * epv; |
| 107 | HVX_Vector vs = src[nvec]; |
| 108 | HVX_Vector vdot = dot[nvec]; |
| 109 | HVX_VectorPred mask = Q6_Q_vsetq2_R(nloe * sizeof(float)); |
| 110 | HVX_Vector zero = Q6_V_vzero(); |
| 111 | |
| 112 | HVX_Vector out = hvx_vec_add_f32_f32(hvx_vmemu(dst + off), hvx_vec_mul_f32_f32(vs, vscale)); |
| 113 | hvx_vec_store_u(dst + off, nloe * sizeof(float), out); |
| 114 | acc = hvx_vec_add_f32_f32(acc, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out, vdot), zero)); |
| 115 | } |
| 116 | |
| 117 | return hvx_vec_reduce_sum_f32(acc); |
| 118 | } |
| 119 | |
| 120 | static inline HVX_Vector gdn_mul_dot4_f32(float * restrict dst0, float * restrict dst1, |
| 121 | float * restrict dst2, float * restrict dst3, |
| 122 | const HVX_Vector * restrict mul, const HVX_Vector * restrict dot, uint32_t n) { |
| 123 | HVX_Vector acc0 = Q6_V_vzero(); |
| 124 | HVX_Vector acc1 = Q6_V_vzero(); |
| 125 | HVX_Vector acc2 = Q6_V_vzero(); |
| 126 | HVX_Vector acc3 = Q6_V_vzero(); |
| 127 | |
| 128 | const uint32_t epv = 128 / sizeof(float); |
| 129 | const uint32_t nvec = n / epv; |
| 130 | const uint32_t nloe = n % epv; |
| 131 | for (uint32_t i = 0; i < nvec; ++i) { |
| 132 | HVX_Vector vm = mul[i]; |
| 133 | HVX_Vector vdot = dot[i]; |
| 134 | |
| 135 | HVX_Vector out0 = hvx_vec_mul_f32_f32(hvx_vmemu(dst0 + i * epv), vm); |
| 136 | HVX_Vector out1 = hvx_vec_mul_f32_f32(hvx_vmemu(dst1 + i * epv), vm); |
| 137 | HVX_Vector out2 = hvx_vec_mul_f32_f32(hvx_vmemu(dst2 + i * epv), vm); |
| 138 | HVX_Vector out3 = hvx_vec_mul_f32_f32(hvx_vmemu(dst3 + i * epv), vm); |
| 139 | |
| 140 | hvx_vmemu(dst0 + i * epv) = out0; |
| 141 | hvx_vmemu(dst1 + i * epv) = out1; |
| 142 | hvx_vmemu(dst2 + i * epv) = out2; |
| 143 | hvx_vmemu(dst3 + i * epv) = out3; |
| 144 | |
| 145 | acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(out0, vdot)); |
| 146 | acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(out1, vdot)); |
| 147 | acc2 = hvx_vec_add_f32_f32(acc2, hvx_vec_mul_f32_f32(out2, vdot)); |
| 148 | acc3 = hvx_vec_add_f32_f32(acc3, hvx_vec_mul_f32_f32(out3, vdot)); |
| 149 | } |
| 150 | |
| 151 | if (nloe) { |
| 152 | const uint32_t off = nvec * epv; |
| 153 | HVX_Vector vm = mul[nvec]; |
| 154 | HVX_Vector vdot = dot[nvec]; |
| 155 | HVX_VectorPred mask = Q6_Q_vsetq2_R(nloe * sizeof(float)); |
| 156 | HVX_Vector zero = Q6_V_vzero(); |
| 157 | |
| 158 | HVX_Vector out0 = hvx_vec_mul_f32_f32(hvx_vmemu(dst0 + off), vm); |
| 159 | HVX_Vector out1 = hvx_vec_mul_f32_f32(hvx_vmemu(dst1 + off), vm); |
| 160 | HVX_Vector out2 = hvx_vec_mul_f32_f32(hvx_vmemu(dst2 + off), vm); |
| 161 | HVX_Vector out3 = hvx_vec_mul_f32_f32(hvx_vmemu(dst3 + off), vm); |
| 162 | |
| 163 | hvx_vec_store_u(dst0 + off, nloe * sizeof(float), out0); |
| 164 | hvx_vec_store_u(dst1 + off, nloe * sizeof(float), out1); |
| 165 | hvx_vec_store_u(dst2 + off, nloe * sizeof(float), out2); |
| 166 | hvx_vec_store_u(dst3 + off, nloe * sizeof(float), out3); |
| 167 | |
| 168 | acc0 = hvx_vec_add_f32_f32(acc0, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out0, vdot), zero)); |
| 169 | acc1 = hvx_vec_add_f32_f32(acc1, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out1, vdot), zero)); |
| 170 | acc2 = hvx_vec_add_f32_f32(acc2, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out2, vdot), zero)); |
| 171 | acc3 = hvx_vec_add_f32_f32(acc3, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out3, vdot), zero)); |
| 172 | } |
| 173 | |
| 174 | HVX_Vector_x4 acc = { .v = { acc0, acc1, acc2, acc3 } }; |
| 175 | return hvx_vec_reduce_sum_f32x4(acc); |
| 176 | } |
| 177 | |
| 178 | static inline HVX_Vector gdn_mul_scalar_dot4_f32(float * restrict dst0, float * restrict dst1, |
| 179 | float * restrict dst2, float * restrict dst3, |
| 180 | HVX_Vector vmul, const HVX_Vector * restrict dot, uint32_t n) { |
| 181 | HVX_Vector acc0 = Q6_V_vzero(); |
| 182 | HVX_Vector acc1 = Q6_V_vzero(); |
| 183 | HVX_Vector acc2 = Q6_V_vzero(); |
| 184 | HVX_Vector acc3 = Q6_V_vzero(); |
| 185 | |
| 186 | const uint32_t epv = 128 / sizeof(float); |
| 187 | const uint32_t nvec = n / epv; |
| 188 | const uint32_t nloe = n % epv; |
| 189 | for (uint32_t i = 0; i < nvec; ++i) { |
| 190 | HVX_Vector vdot = dot[i]; |
| 191 | |
| 192 | HVX_Vector out0 = hvx_vec_mul_f32_f32(hvx_vmemu(dst0 + i * epv), vmul); |
| 193 | HVX_Vector out1 = hvx_vec_mul_f32_f32(hvx_vmemu(dst1 + i * epv), vmul); |
| 194 | HVX_Vector out2 = hvx_vec_mul_f32_f32(hvx_vmemu(dst2 + i * epv), vmul); |
| 195 | HVX_Vector out3 = hvx_vec_mul_f32_f32(hvx_vmemu(dst3 + i * epv), vmul); |
| 196 | |
| 197 | hvx_vmemu(dst0 + i * epv) = out0; |
| 198 | hvx_vmemu(dst1 + i * epv) = out1; |
| 199 | hvx_vmemu(dst2 + i * epv) = out2; |
| 200 | hvx_vmemu(dst3 + i * epv) = out3; |
| 201 | |
| 202 | acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(out0, vdot)); |
| 203 | acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(out1, vdot)); |
| 204 | acc2 = hvx_vec_add_f32_f32(acc2, hvx_vec_mul_f32_f32(out2, vdot)); |
| 205 | acc3 = hvx_vec_add_f32_f32(acc3, hvx_vec_mul_f32_f32(out3, vdot)); |
| 206 | } |
| 207 | |
| 208 | if (nloe) { |
| 209 | const uint32_t off = nvec * epv; |
| 210 | HVX_Vector vdot = dot[nvec]; |
| 211 | HVX_VectorPred mask = Q6_Q_vsetq2_R(nloe * sizeof(float)); |
| 212 | HVX_Vector zero = Q6_V_vzero(); |
| 213 | |
| 214 | HVX_Vector out0 = hvx_vec_mul_f32_f32(hvx_vmemu(dst0 + off), vmul); |
| 215 | HVX_Vector out1 = hvx_vec_mul_f32_f32(hvx_vmemu(dst1 + off), vmul); |
| 216 | HVX_Vector out2 = hvx_vec_mul_f32_f32(hvx_vmemu(dst2 + off), vmul); |
| 217 | HVX_Vector out3 = hvx_vec_mul_f32_f32(hvx_vmemu(dst3 + off), vmul); |
| 218 | |
| 219 | hvx_vec_store_u(dst0 + off, nloe * sizeof(float), out0); |
| 220 | hvx_vec_store_u(dst1 + off, nloe * sizeof(float), out1); |
| 221 | hvx_vec_store_u(dst2 + off, nloe * sizeof(float), out2); |
| 222 | hvx_vec_store_u(dst3 + off, nloe * sizeof(float), out3); |
| 223 | |
| 224 | acc0 = hvx_vec_add_f32_f32(acc0, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out0, vdot), zero)); |
| 225 | acc1 = hvx_vec_add_f32_f32(acc1, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out1, vdot), zero)); |
| 226 | acc2 = hvx_vec_add_f32_f32(acc2, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out2, vdot), zero)); |
| 227 | acc3 = hvx_vec_add_f32_f32(acc3, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out3, vdot), zero)); |
| 228 | } |
| 229 | |
| 230 | HVX_Vector_x4 acc = { .v = { acc0, acc1, acc2, acc3 } }; |
| 231 | return hvx_vec_reduce_sum_f32x4(acc); |
| 232 | } |
| 233 | |
| 234 | static inline HVX_Vector gdn_add_scaled_dot4_f32(float * restrict dst0, float * restrict dst1, |
| 235 | float * restrict dst2, float * restrict dst3, |
| 236 | const HVX_Vector * restrict src, const float * restrict scale, |
| 237 | const HVX_Vector * restrict dot, uint32_t n) { |
| 238 | HVX_Vector acc0 = Q6_V_vzero(); |
| 239 | HVX_Vector acc1 = Q6_V_vzero(); |
| 240 | HVX_Vector acc2 = Q6_V_vzero(); |
| 241 | HVX_Vector acc3 = Q6_V_vzero(); |
| 242 | const HVX_Vector scale0 = hvx_vec_splat_f32(scale[0]); |
| 243 | const HVX_Vector scale1 = hvx_vec_splat_f32(scale[1]); |
| 244 | const HVX_Vector scale2 = hvx_vec_splat_f32(scale[2]); |
| 245 | const HVX_Vector scale3 = hvx_vec_splat_f32(scale[3]); |
| 246 | |
| 247 | const uint32_t epv = 128 / sizeof(float); |
| 248 | const uint32_t nvec = n / epv; |
| 249 | const uint32_t nloe = n % epv; |
| 250 | for (uint32_t i = 0; i < nvec; ++i) { |
| 251 | HVX_Vector vs = src[i]; |
| 252 | HVX_Vector vdot = dot[i]; |
| 253 | |
| 254 | HVX_Vector out0 = hvx_vec_add_f32_f32(hvx_vmemu(dst0 + i * epv), hvx_vec_mul_f32_f32(vs, scale0)); |
| 255 | HVX_Vector out1 = hvx_vec_add_f32_f32(hvx_vmemu(dst1 + i * epv), hvx_vec_mul_f32_f32(vs, scale1)); |
| 256 | HVX_Vector out2 = hvx_vec_add_f32_f32(hvx_vmemu(dst2 + i * epv), hvx_vec_mul_f32_f32(vs, scale2)); |
| 257 | HVX_Vector out3 = hvx_vec_add_f32_f32(hvx_vmemu(dst3 + i * epv), hvx_vec_mul_f32_f32(vs, scale3)); |
| 258 | |
| 259 | hvx_vmemu(dst0 + i * epv) = out0; |
| 260 | hvx_vmemu(dst1 + i * epv) = out1; |
| 261 | hvx_vmemu(dst2 + i * epv) = out2; |
| 262 | hvx_vmemu(dst3 + i * epv) = out3; |
| 263 | |
| 264 | acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(out0, vdot)); |
| 265 | acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(out1, vdot)); |
| 266 | acc2 = hvx_vec_add_f32_f32(acc2, hvx_vec_mul_f32_f32(out2, vdot)); |
| 267 | acc3 = hvx_vec_add_f32_f32(acc3, hvx_vec_mul_f32_f32(out3, vdot)); |
| 268 | } |
| 269 | |
| 270 | if (nloe) { |
| 271 | const uint32_t off = nvec * epv; |
| 272 | HVX_Vector vs = src[nvec]; |
| 273 | HVX_Vector vdot = dot[nvec]; |
| 274 | HVX_VectorPred mask = Q6_Q_vsetq2_R(nloe * sizeof(float)); |
| 275 | HVX_Vector zero = Q6_V_vzero(); |
| 276 | |
| 277 | HVX_Vector out0 = hvx_vec_add_f32_f32(hvx_vmemu(dst0 + off), hvx_vec_mul_f32_f32(vs, scale0)); |
| 278 | HVX_Vector out1 = hvx_vec_add_f32_f32(hvx_vmemu(dst1 + off), hvx_vec_mul_f32_f32(vs, scale1)); |
| 279 | HVX_Vector out2 = hvx_vec_add_f32_f32(hvx_vmemu(dst2 + off), hvx_vec_mul_f32_f32(vs, scale2)); |
| 280 | HVX_Vector out3 = hvx_vec_add_f32_f32(hvx_vmemu(dst3 + off), hvx_vec_mul_f32_f32(vs, scale3)); |
| 281 | |
| 282 | hvx_vec_store_u(dst0 + off, nloe * sizeof(float), out0); |
| 283 | hvx_vec_store_u(dst1 + off, nloe * sizeof(float), out1); |
| 284 | hvx_vec_store_u(dst2 + off, nloe * sizeof(float), out2); |
| 285 | hvx_vec_store_u(dst3 + off, nloe * sizeof(float), out3); |
| 286 | |
| 287 | acc0 = hvx_vec_add_f32_f32(acc0, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out0, vdot), zero)); |
| 288 | acc1 = hvx_vec_add_f32_f32(acc1, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out1, vdot), zero)); |
| 289 | acc2 = hvx_vec_add_f32_f32(acc2, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out2, vdot), zero)); |
| 290 | acc3 = hvx_vec_add_f32_f32(acc3, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out3, vdot), zero)); |
| 291 | } |
| 292 | |
| 293 | HVX_Vector_x4 acc = { .v = { acc0, acc1, acc2, acc3 } }; |
| 294 | return hvx_vec_reduce_sum_f32x4(acc); |
| 295 | } |
| 296 | |
| 297 | static inline HVX_Vector gdn_mul_dot8_f32(float * restrict dst0, float * restrict dst1, |
| 298 | float * restrict dst2, float * restrict dst3, float * restrict dst4, |
| 299 | float * restrict dst5, float * restrict dst6, float * restrict dst7, |
| 300 | const HVX_Vector * restrict mul, const HVX_Vector * restrict dot, uint32_t n) { |
| 301 | HVX_Vector acc0 = Q6_V_vzero(); |
| 302 | HVX_Vector acc1 = Q6_V_vzero(); |
| 303 | HVX_Vector acc2 = Q6_V_vzero(); |
| 304 | HVX_Vector acc3 = Q6_V_vzero(); |
| 305 | HVX_Vector acc4 = Q6_V_vzero(); |
| 306 | HVX_Vector acc5 = Q6_V_vzero(); |
| 307 | HVX_Vector acc6 = Q6_V_vzero(); |
| 308 | HVX_Vector acc7 = Q6_V_vzero(); |
| 309 | |
| 310 | const uint32_t epv = 128 / sizeof(float); |
| 311 | const uint32_t nvec = n / epv; |
| 312 | const uint32_t nloe = n % epv; |
| 313 | for (uint32_t i = 0; i < nvec; ++i) { |
| 314 | HVX_Vector vm = mul[i]; |
| 315 | HVX_Vector vdot = dot[i]; |
| 316 | |
| 317 | HVX_Vector out0 = hvx_vec_mul_f32_f32(hvx_vmemu(dst0 + i * epv), vm); |
| 318 | HVX_Vector out1 = hvx_vec_mul_f32_f32(hvx_vmemu(dst1 + i * epv), vm); |
| 319 | HVX_Vector out2 = hvx_vec_mul_f32_f32(hvx_vmemu(dst2 + i * epv), vm); |
| 320 | HVX_Vector out3 = hvx_vec_mul_f32_f32(hvx_vmemu(dst3 + i * epv), vm); |
| 321 | HVX_Vector out4 = hvx_vec_mul_f32_f32(hvx_vmemu(dst4 + i * epv), vm); |
| 322 | HVX_Vector out5 = hvx_vec_mul_f32_f32(hvx_vmemu(dst5 + i * epv), vm); |
| 323 | HVX_Vector out6 = hvx_vec_mul_f32_f32(hvx_vmemu(dst6 + i * epv), vm); |
| 324 | HVX_Vector out7 = hvx_vec_mul_f32_f32(hvx_vmemu(dst7 + i * epv), vm); |
| 325 | |
| 326 | hvx_vmemu(dst0 + i * epv) = out0; |
| 327 | hvx_vmemu(dst1 + i * epv) = out1; |
| 328 | hvx_vmemu(dst2 + i * epv) = out2; |
| 329 | hvx_vmemu(dst3 + i * epv) = out3; |
| 330 | hvx_vmemu(dst4 + i * epv) = out4; |
| 331 | hvx_vmemu(dst5 + i * epv) = out5; |
| 332 | hvx_vmemu(dst6 + i * epv) = out6; |
| 333 | hvx_vmemu(dst7 + i * epv) = out7; |
| 334 | |
| 335 | acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(out0, vdot)); |
| 336 | acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(out1, vdot)); |
| 337 | acc2 = hvx_vec_add_f32_f32(acc2, hvx_vec_mul_f32_f32(out2, vdot)); |
| 338 | acc3 = hvx_vec_add_f32_f32(acc3, hvx_vec_mul_f32_f32(out3, vdot)); |
| 339 | acc4 = hvx_vec_add_f32_f32(acc4, hvx_vec_mul_f32_f32(out4, vdot)); |
| 340 | acc5 = hvx_vec_add_f32_f32(acc5, hvx_vec_mul_f32_f32(out5, vdot)); |
| 341 | acc6 = hvx_vec_add_f32_f32(acc6, hvx_vec_mul_f32_f32(out6, vdot)); |
| 342 | acc7 = hvx_vec_add_f32_f32(acc7, hvx_vec_mul_f32_f32(out7, vdot)); |
| 343 | } |
| 344 | |
| 345 | if (nloe) { |
| 346 | const uint32_t off = nvec * epv; |
| 347 | HVX_Vector vm = mul[nvec]; |
| 348 | HVX_Vector vdot = dot[nvec]; |
| 349 | HVX_VectorPred mask = Q6_Q_vsetq2_R(nloe * sizeof(float)); |
| 350 | HVX_Vector zero = Q6_V_vzero(); |
| 351 | |
| 352 | HVX_Vector out0 = hvx_vec_mul_f32_f32(hvx_vmemu(dst0 + off), vm); |
| 353 | HVX_Vector out1 = hvx_vec_mul_f32_f32(hvx_vmemu(dst1 + off), vm); |
| 354 | HVX_Vector out2 = hvx_vec_mul_f32_f32(hvx_vmemu(dst2 + off), vm); |
| 355 | HVX_Vector out3 = hvx_vec_mul_f32_f32(hvx_vmemu(dst3 + off), vm); |
| 356 | HVX_Vector out4 = hvx_vec_mul_f32_f32(hvx_vmemu(dst4 + off), vm); |
| 357 | HVX_Vector out5 = hvx_vec_mul_f32_f32(hvx_vmemu(dst5 + off), vm); |
| 358 | HVX_Vector out6 = hvx_vec_mul_f32_f32(hvx_vmemu(dst6 + off), vm); |
| 359 | HVX_Vector out7 = hvx_vec_mul_f32_f32(hvx_vmemu(dst7 + off), vm); |
| 360 | |
| 361 | hvx_vec_store_u(dst0 + off, nloe * sizeof(float), out0); |
| 362 | hvx_vec_store_u(dst1 + off, nloe * sizeof(float), out1); |
| 363 | hvx_vec_store_u(dst2 + off, nloe * sizeof(float), out2); |
| 364 | hvx_vec_store_u(dst3 + off, nloe * sizeof(float), out3); |
| 365 | hvx_vec_store_u(dst4 + off, nloe * sizeof(float), out4); |
| 366 | hvx_vec_store_u(dst5 + off, nloe * sizeof(float), out5); |
| 367 | hvx_vec_store_u(dst6 + off, nloe * sizeof(float), out6); |
| 368 | hvx_vec_store_u(dst7 + off, nloe * sizeof(float), out7); |
| 369 | |
| 370 | acc0 = hvx_vec_add_f32_f32(acc0, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out0, vdot), zero)); |
| 371 | acc1 = hvx_vec_add_f32_f32(acc1, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out1, vdot), zero)); |
| 372 | acc2 = hvx_vec_add_f32_f32(acc2, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out2, vdot), zero)); |
| 373 | acc3 = hvx_vec_add_f32_f32(acc3, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out3, vdot), zero)); |
| 374 | acc4 = hvx_vec_add_f32_f32(acc4, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out4, vdot), zero)); |
| 375 | acc5 = hvx_vec_add_f32_f32(acc5, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out5, vdot), zero)); |
| 376 | acc6 = hvx_vec_add_f32_f32(acc6, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out6, vdot), zero)); |
| 377 | acc7 = hvx_vec_add_f32_f32(acc7, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out7, vdot), zero)); |
| 378 | } |
| 379 | |
| 380 | HVX_Vector_x4 accA = { .v = { acc0, acc1, acc2, acc3 } }; |
| 381 | HVX_Vector_x4 accB = { .v = { acc4, acc5, acc6, acc7 } }; |
| 382 | HVX_Vector rA = hvx_vec_reduce_sum_f32x4(accA); |
| 383 | HVX_Vector rB = hvx_vec_reduce_sum_f32x4(accB); |
| 384 | HVX_VectorPred q16 = Q6_Q_vsetq2_R(16); |
| 385 | return Q6_V_vmux_QVV(q16, rA, Q6_V_vror_VR(rB, 128 - 16)); |
| 386 | } |
| 387 | |
| 388 | static inline HVX_Vector gdn_mul_scalar_dot8_f32(float * restrict dst0, float * restrict dst1, |
| 389 | float * restrict dst2, float * restrict dst3, float * restrict dst4, |
| 390 | float * restrict dst5, float * restrict dst6, float * restrict dst7, |
| 391 | HVX_Vector vmul, const HVX_Vector * restrict dot, uint32_t n) { |
| 392 | HVX_Vector acc0 = Q6_V_vzero(); |
| 393 | HVX_Vector acc1 = Q6_V_vzero(); |
| 394 | HVX_Vector acc2 = Q6_V_vzero(); |
| 395 | HVX_Vector acc3 = Q6_V_vzero(); |
| 396 | HVX_Vector acc4 = Q6_V_vzero(); |
| 397 | HVX_Vector acc5 = Q6_V_vzero(); |
| 398 | HVX_Vector acc6 = Q6_V_vzero(); |
| 399 | HVX_Vector acc7 = Q6_V_vzero(); |
| 400 | |
| 401 | const uint32_t epv = 128 / sizeof(float); |
| 402 | const uint32_t nvec = n / epv; |
| 403 | const uint32_t nloe = n % epv; |
| 404 | for (uint32_t i = 0; i < nvec; ++i) { |
| 405 | HVX_Vector vdot = dot[i]; |
| 406 | |
| 407 | HVX_Vector out0 = hvx_vec_mul_f32_f32(hvx_vmemu(dst0 + i * epv), vmul); |
| 408 | HVX_Vector out1 = hvx_vec_mul_f32_f32(hvx_vmemu(dst1 + i * epv), vmul); |
| 409 | HVX_Vector out2 = hvx_vec_mul_f32_f32(hvx_vmemu(dst2 + i * epv), vmul); |
| 410 | HVX_Vector out3 = hvx_vec_mul_f32_f32(hvx_vmemu(dst3 + i * epv), vmul); |
| 411 | HVX_Vector out4 = hvx_vec_mul_f32_f32(hvx_vmemu(dst4 + i * epv), vmul); |
| 412 | HVX_Vector out5 = hvx_vec_mul_f32_f32(hvx_vmemu(dst5 + i * epv), vmul); |
| 413 | HVX_Vector out6 = hvx_vec_mul_f32_f32(hvx_vmemu(dst6 + i * epv), vmul); |
| 414 | HVX_Vector out7 = hvx_vec_mul_f32_f32(hvx_vmemu(dst7 + i * epv), vmul); |
| 415 | |
| 416 | hvx_vmemu(dst0 + i * epv) = out0; |
| 417 | hvx_vmemu(dst1 + i * epv) = out1; |
| 418 | hvx_vmemu(dst2 + i * epv) = out2; |
| 419 | hvx_vmemu(dst3 + i * epv) = out3; |
| 420 | hvx_vmemu(dst4 + i * epv) = out4; |
| 421 | hvx_vmemu(dst5 + i * epv) = out5; |
| 422 | hvx_vmemu(dst6 + i * epv) = out6; |
| 423 | hvx_vmemu(dst7 + i * epv) = out7; |
| 424 | |
| 425 | acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(out0, vdot)); |
| 426 | acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(out1, vdot)); |
| 427 | acc2 = hvx_vec_add_f32_f32(acc2, hvx_vec_mul_f32_f32(out2, vdot)); |
| 428 | acc3 = hvx_vec_add_f32_f32(acc3, hvx_vec_mul_f32_f32(out3, vdot)); |
| 429 | acc4 = hvx_vec_add_f32_f32(acc4, hvx_vec_mul_f32_f32(out4, vdot)); |
| 430 | acc5 = hvx_vec_add_f32_f32(acc5, hvx_vec_mul_f32_f32(out5, vdot)); |
| 431 | acc6 = hvx_vec_add_f32_f32(acc6, hvx_vec_mul_f32_f32(out6, vdot)); |
| 432 | acc7 = hvx_vec_add_f32_f32(acc7, hvx_vec_mul_f32_f32(out7, vdot)); |
| 433 | } |
| 434 | |
| 435 | if (nloe) { |
| 436 | const uint32_t off = nvec * epv; |
| 437 | HVX_Vector vdot = dot[nvec]; |
| 438 | HVX_VectorPred mask = Q6_Q_vsetq2_R(nloe * sizeof(float)); |
| 439 | HVX_Vector zero = Q6_V_vzero(); |
| 440 | |
| 441 | HVX_Vector out0 = hvx_vec_mul_f32_f32(hvx_vmemu(dst0 + off), vmul); |
| 442 | HVX_Vector out1 = hvx_vec_mul_f32_f32(hvx_vmemu(dst1 + off), vmul); |
| 443 | HVX_Vector out2 = hvx_vec_mul_f32_f32(hvx_vmemu(dst2 + off), vmul); |
| 444 | HVX_Vector out3 = hvx_vec_mul_f32_f32(hvx_vmemu(dst3 + off), vmul); |
| 445 | HVX_Vector out4 = hvx_vec_mul_f32_f32(hvx_vmemu(dst4 + off), vmul); |
| 446 | HVX_Vector out5 = hvx_vec_mul_f32_f32(hvx_vmemu(dst5 + off), vmul); |
| 447 | HVX_Vector out6 = hvx_vec_mul_f32_f32(hvx_vmemu(dst6 + off), vmul); |
| 448 | HVX_Vector out7 = hvx_vec_mul_f32_f32(hvx_vmemu(dst7 + off), vmul); |
| 449 | |
| 450 | hvx_vec_store_u(dst0 + off, nloe * sizeof(float), out0); |
| 451 | hvx_vec_store_u(dst1 + off, nloe * sizeof(float), out1); |
| 452 | hvx_vec_store_u(dst2 + off, nloe * sizeof(float), out2); |
| 453 | hvx_vec_store_u(dst3 + off, nloe * sizeof(float), out3); |
| 454 | hvx_vec_store_u(dst4 + off, nloe * sizeof(float), out4); |
| 455 | hvx_vec_store_u(dst5 + off, nloe * sizeof(float), out5); |
| 456 | hvx_vec_store_u(dst6 + off, nloe * sizeof(float), out6); |
| 457 | hvx_vec_store_u(dst7 + off, nloe * sizeof(float), out7); |
| 458 | |
| 459 | acc0 = hvx_vec_add_f32_f32(acc0, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out0, vdot), zero)); |
| 460 | acc1 = hvx_vec_add_f32_f32(acc1, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out1, vdot), zero)); |
| 461 | acc2 = hvx_vec_add_f32_f32(acc2, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out2, vdot), zero)); |
| 462 | acc3 = hvx_vec_add_f32_f32(acc3, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out3, vdot), zero)); |
| 463 | acc4 = hvx_vec_add_f32_f32(acc4, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out4, vdot), zero)); |
| 464 | acc5 = hvx_vec_add_f32_f32(acc5, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out5, vdot), zero)); |
| 465 | acc6 = hvx_vec_add_f32_f32(acc6, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out6, vdot), zero)); |
| 466 | acc7 = hvx_vec_add_f32_f32(acc7, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out7, vdot), zero)); |
| 467 | } |
| 468 | |
| 469 | HVX_Vector_x4 accA = { .v = { acc0, acc1, acc2, acc3 } }; |
| 470 | HVX_Vector_x4 accB = { .v = { acc4, acc5, acc6, acc7 } }; |
| 471 | HVX_Vector rA = hvx_vec_reduce_sum_f32x4(accA); |
| 472 | HVX_Vector rB = hvx_vec_reduce_sum_f32x4(accB); |
| 473 | HVX_VectorPred q16 = Q6_Q_vsetq2_R(16); |
| 474 | return Q6_V_vmux_QVV(q16, rA, Q6_V_vror_VR(rB, 128 - 16)); |
| 475 | } |
| 476 | |
| 477 | static inline HVX_Vector gdn_add_scaled_dot8_f32(float * restrict dst0, float * restrict dst1, |
| 478 | float * restrict dst2, float * restrict dst3, float * restrict dst4, |
| 479 | float * restrict dst5, float * restrict dst6, float * restrict dst7, |
| 480 | const HVX_Vector * restrict src, const float * restrict scale, |
| 481 | const HVX_Vector * restrict dot, uint32_t n) { |
| 482 | HVX_Vector acc0 = Q6_V_vzero(); |
| 483 | HVX_Vector acc1 = Q6_V_vzero(); |
| 484 | HVX_Vector acc2 = Q6_V_vzero(); |
| 485 | HVX_Vector acc3 = Q6_V_vzero(); |
| 486 | HVX_Vector acc4 = Q6_V_vzero(); |
| 487 | HVX_Vector acc5 = Q6_V_vzero(); |
| 488 | HVX_Vector acc6 = Q6_V_vzero(); |
| 489 | HVX_Vector acc7 = Q6_V_vzero(); |
| 490 | const HVX_Vector scale0 = hvx_vec_splat_f32(scale[0]); |
| 491 | const HVX_Vector scale1 = hvx_vec_splat_f32(scale[1]); |
| 492 | const HVX_Vector scale2 = hvx_vec_splat_f32(scale[2]); |
| 493 | const HVX_Vector scale3 = hvx_vec_splat_f32(scale[3]); |
| 494 | const HVX_Vector scale4 = hvx_vec_splat_f32(scale[4]); |
| 495 | const HVX_Vector scale5 = hvx_vec_splat_f32(scale[5]); |
| 496 | const HVX_Vector scale6 = hvx_vec_splat_f32(scale[6]); |
| 497 | const HVX_Vector scale7 = hvx_vec_splat_f32(scale[7]); |
| 498 | |
| 499 | const uint32_t epv = 128 / sizeof(float); |
| 500 | const uint32_t nvec = n / epv; |
| 501 | const uint32_t nloe = n % epv; |
| 502 | for (uint32_t i = 0; i < nvec; ++i) { |
| 503 | HVX_Vector vs = src[i]; |
| 504 | HVX_Vector vdot = dot[i]; |
| 505 | |
| 506 | HVX_Vector out0 = hvx_vec_add_f32_f32(hvx_vmemu(dst0 + i * epv), hvx_vec_mul_f32_f32(vs, scale0)); |
| 507 | HVX_Vector out1 = hvx_vec_add_f32_f32(hvx_vmemu(dst1 + i * epv), hvx_vec_mul_f32_f32(vs, scale1)); |
| 508 | HVX_Vector out2 = hvx_vec_add_f32_f32(hvx_vmemu(dst2 + i * epv), hvx_vec_mul_f32_f32(vs, scale2)); |
| 509 | HVX_Vector out3 = hvx_vec_add_f32_f32(hvx_vmemu(dst3 + i * epv), hvx_vec_mul_f32_f32(vs, scale3)); |
| 510 | HVX_Vector out4 = hvx_vec_add_f32_f32(hvx_vmemu(dst4 + i * epv), hvx_vec_mul_f32_f32(vs, scale4)); |
| 511 | HVX_Vector out5 = hvx_vec_add_f32_f32(hvx_vmemu(dst5 + i * epv), hvx_vec_mul_f32_f32(vs, scale5)); |
| 512 | HVX_Vector out6 = hvx_vec_add_f32_f32(hvx_vmemu(dst6 + i * epv), hvx_vec_mul_f32_f32(vs, scale6)); |
| 513 | HVX_Vector out7 = hvx_vec_add_f32_f32(hvx_vmemu(dst7 + i * epv), hvx_vec_mul_f32_f32(vs, scale7)); |
| 514 | |
| 515 | hvx_vmemu(dst0 + i * epv) = out0; |
| 516 | hvx_vmemu(dst1 + i * epv) = out1; |
| 517 | hvx_vmemu(dst2 + i * epv) = out2; |
| 518 | hvx_vmemu(dst3 + i * epv) = out3; |
| 519 | hvx_vmemu(dst4 + i * epv) = out4; |
| 520 | hvx_vmemu(dst5 + i * epv) = out5; |
| 521 | hvx_vmemu(dst6 + i * epv) = out6; |
| 522 | hvx_vmemu(dst7 + i * epv) = out7; |
| 523 | |
| 524 | acc0 = hvx_vec_add_f32_f32(acc0, hvx_vec_mul_f32_f32(out0, vdot)); |
| 525 | acc1 = hvx_vec_add_f32_f32(acc1, hvx_vec_mul_f32_f32(out1, vdot)); |
| 526 | acc2 = hvx_vec_add_f32_f32(acc2, hvx_vec_mul_f32_f32(out2, vdot)); |
| 527 | acc3 = hvx_vec_add_f32_f32(acc3, hvx_vec_mul_f32_f32(out3, vdot)); |
| 528 | acc4 = hvx_vec_add_f32_f32(acc4, hvx_vec_mul_f32_f32(out4, vdot)); |
| 529 | acc5 = hvx_vec_add_f32_f32(acc5, hvx_vec_mul_f32_f32(out5, vdot)); |
| 530 | acc6 = hvx_vec_add_f32_f32(acc6, hvx_vec_mul_f32_f32(out6, vdot)); |
| 531 | acc7 = hvx_vec_add_f32_f32(acc7, hvx_vec_mul_f32_f32(out7, vdot)); |
| 532 | } |
| 533 | |
| 534 | if (nloe) { |
| 535 | const uint32_t off = nvec * epv; |
| 536 | HVX_Vector vs = src[nvec]; |
| 537 | HVX_Vector vdot = dot[nvec]; |
| 538 | HVX_VectorPred mask = Q6_Q_vsetq2_R(nloe * sizeof(float)); |
| 539 | HVX_Vector zero = Q6_V_vzero(); |
| 540 | |
| 541 | HVX_Vector out0 = hvx_vec_add_f32_f32(hvx_vmemu(dst0 + off), hvx_vec_mul_f32_f32(vs, scale0)); |
| 542 | HVX_Vector out1 = hvx_vec_add_f32_f32(hvx_vmemu(dst1 + off), hvx_vec_mul_f32_f32(vs, scale1)); |
| 543 | HVX_Vector out2 = hvx_vec_add_f32_f32(hvx_vmemu(dst2 + off), hvx_vec_mul_f32_f32(vs, scale2)); |
| 544 | HVX_Vector out3 = hvx_vec_add_f32_f32(hvx_vmemu(dst3 + off), hvx_vec_mul_f32_f32(vs, scale3)); |
| 545 | HVX_Vector out4 = hvx_vec_add_f32_f32(hvx_vmemu(dst4 + off), hvx_vec_mul_f32_f32(vs, scale4)); |
| 546 | HVX_Vector out5 = hvx_vec_add_f32_f32(hvx_vmemu(dst5 + off), hvx_vec_mul_f32_f32(vs, scale5)); |
| 547 | HVX_Vector out6 = hvx_vec_add_f32_f32(hvx_vmemu(dst6 + off), hvx_vec_mul_f32_f32(vs, scale6)); |
| 548 | HVX_Vector out7 = hvx_vec_add_f32_f32(hvx_vmemu(dst7 + off), hvx_vec_mul_f32_f32(vs, scale7)); |
| 549 | |
| 550 | hvx_vec_store_u(dst0 + off, nloe * sizeof(float), out0); |
| 551 | hvx_vec_store_u(dst1 + off, nloe * sizeof(float), out1); |
| 552 | hvx_vec_store_u(dst2 + off, nloe * sizeof(float), out2); |
| 553 | hvx_vec_store_u(dst3 + off, nloe * sizeof(float), out3); |
| 554 | hvx_vec_store_u(dst4 + off, nloe * sizeof(float), out4); |
| 555 | hvx_vec_store_u(dst5 + off, nloe * sizeof(float), out5); |
| 556 | hvx_vec_store_u(dst6 + off, nloe * sizeof(float), out6); |
| 557 | hvx_vec_store_u(dst7 + off, nloe * sizeof(float), out7); |
| 558 | |
| 559 | acc0 = hvx_vec_add_f32_f32(acc0, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out0, vdot), zero)); |
| 560 | acc1 = hvx_vec_add_f32_f32(acc1, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out1, vdot), zero)); |
| 561 | acc2 = hvx_vec_add_f32_f32(acc2, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out2, vdot), zero)); |
| 562 | acc3 = hvx_vec_add_f32_f32(acc3, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out3, vdot), zero)); |
| 563 | acc4 = hvx_vec_add_f32_f32(acc4, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out4, vdot), zero)); |
| 564 | acc5 = hvx_vec_add_f32_f32(acc5, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out5, vdot), zero)); |
| 565 | acc6 = hvx_vec_add_f32_f32(acc6, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out6, vdot), zero)); |
| 566 | acc7 = hvx_vec_add_f32_f32(acc7, Q6_V_vmux_QVV(mask, hvx_vec_mul_f32_f32(out7, vdot), zero)); |
| 567 | } |
| 568 | |
| 569 | HVX_Vector_x4 accA = { .v = { acc0, acc1, acc2, acc3 } }; |
| 570 | HVX_Vector_x4 accB = { .v = { acc4, acc5, acc6, acc7 } }; |
| 571 | HVX_Vector rA = hvx_vec_reduce_sum_f32x4(accA); |
| 572 | HVX_Vector rB = hvx_vec_reduce_sum_f32x4(accB); |
| 573 | HVX_VectorPred q16 = Q6_Q_vsetq2_R(16); |
| 574 | return Q6_V_vmux_QVV(q16, rA, Q6_V_vror_VR(rB, 128 - 16)); |
| 575 | } |
| 576 | |
| 577 | static inline void gdn_step_kda_f32( |
| 578 | float * restrict s_work, |
| 579 | float * restrict attn_out, |
| 580 | const float * restrict q_t, |
| 581 | const float * restrict k_t, |
| 582 | const float * restrict v_t, |
| 583 | const float * restrict g_t, |
| 584 | float beta_val, |
| 585 | float scale, |
| 586 | uint32_t S_v |
| 587 | ) { |
| 588 | const uint32_t epv = 128 / sizeof(float); |
| 589 | const uint32_t nvec = S_v / epv; |
| 590 | const uint32_t nloe = S_v % epv; |
| 591 | |
| 592 | HVX_Vector vq[4]; |
| 593 | HVX_Vector vk[4]; |
| 594 | HVX_Vector vg[4]; |
| 595 | |
| 596 | for (uint32_t i = 0; i < nvec; ++i) { |
| 597 | vq[i] = hvx_vmemu(q_t + i * epv); |
| 598 | vk[i] = hvx_vmemu(k_t + i * epv); |
| 599 | vg[i] = hvx_vec_exp_f32(hvx_vmemu(g_t + i * epv)); |
| 600 | } |
| 601 | if (nloe) { |
| 602 | vq[nvec] = hvx_vmemu(q_t + nvec * epv); |
| 603 | vk[nvec] = hvx_vmemu(k_t + nvec * epv); |
| 604 | vg[nvec] = hvx_vec_exp_f32(hvx_vmemu(g_t + nvec * epv)); |
| 605 | } |
| 606 | |
| 607 | const HVX_Vector vbeta = hvx_vec_splat_f32(beta_val); |
| 608 | const HVX_Vector vscale = hvx_vec_splat_f32(scale); |
| 609 | |
| 610 | float delta[8] __attribute__((aligned(128))); |
| 611 | |
| 612 | uint32_t j = 0; |
| 613 | for (; j + 8 <= S_v; j += 8) { |
| 614 | float * row0 = s_work + (uint64_t) (j + 0) * S_v; |
| 615 | float * row1 = s_work + (uint64_t) (j + 1) * S_v; |
| 616 | float * row2 = s_work + (uint64_t) (j + 2) * S_v; |
| 617 | float * row3 = s_work + (uint64_t) (j + 3) * S_v; |
| 618 | float * row4 = s_work + (uint64_t) (j + 4) * S_v; |
| 619 | float * row5 = s_work + (uint64_t) (j + 5) * S_v; |
| 620 | float * row6 = s_work + (uint64_t) (j + 6) * S_v; |
| 621 | float * row7 = s_work + (uint64_t) (j + 7) * S_v; |
| 622 | |
| 623 | HVX_Vector vsums = gdn_mul_dot8_f32(row0, row1, row2, row3, row4, row5, row6, row7, |
| 624 | vg, vk, S_v); |
| 625 | |
| 626 | HVX_Vector vv_t = hvx_vmemu(v_t + j); |
| 627 | HVX_Vector diff = hvx_vec_sub_f32_f32(vv_t, vsums); |
| 628 | HVX_Vector vdelta = hvx_vec_mul_f32_f32(diff, vbeta); |
| 629 | hvx_vec_store_u(delta, 8 * sizeof(float), vdelta); |
| 630 | |
| 631 | HVX_Vector vattn = gdn_add_scaled_dot8_f32(row0, row1, row2, row3, row4, row5, row6, row7, |
| 632 | vk, delta, vq, S_v); |
| 633 | |
| 634 | HVX_Vector res_attn = hvx_vec_mul_f32_f32(vattn, vscale); |
| 635 | hvx_vec_store_u(attn_out + j, 8 * sizeof(float), res_attn); |
| 636 | } |
| 637 | for (; j + 4 <= S_v; j += 4) { |
| 638 | float * row0 = s_work + (uint64_t) (j + 0) * S_v; |
| 639 | float * row1 = s_work + (uint64_t) (j + 1) * S_v; |
| 640 | float * row2 = s_work + (uint64_t) (j + 2) * S_v; |
| 641 | float * row3 = s_work + (uint64_t) (j + 3) * S_v; |
| 642 | |
| 643 | HVX_Vector vsums = gdn_mul_dot4_f32(row0, row1, row2, row3, vg, vk, S_v); |
| 644 | |
| 645 | HVX_Vector vv_t = hvx_vmemu(v_t + j); |
| 646 | HVX_Vector diff = hvx_vec_sub_f32_f32(vv_t, vsums); |
| 647 | HVX_Vector vdelta = hvx_vec_mul_f32_f32(diff, vbeta); |
| 648 | hvx_vec_store_u(delta, 4 * sizeof(float), vdelta); |
| 649 | |
| 650 | HVX_Vector vattn = gdn_add_scaled_dot4_f32(row0, row1, row2, row3, vk, delta, vq, S_v); |
| 651 | |
| 652 | HVX_Vector res_attn = hvx_vec_mul_f32_f32(vattn, vscale); |
| 653 | hvx_vec_store_u(attn_out + j, 4 * sizeof(float), res_attn); |
| 654 | } |
| 655 | for (; j < S_v; ++j) { |
| 656 | float * row = s_work + (uint64_t) j * S_v; |
| 657 | HVX_Vector vsum = gdn_mul_dot_f32(row, vg, vk, S_v); |
| 658 | HVX_Vector vv_t = hvx_vec_splat_f32(v_t[j]); |
| 659 | HVX_Vector vdj = hvx_vec_mul_f32_f32(hvx_vec_sub_f32_f32(vv_t, vsum), vbeta); |
| 660 | HVX_Vector vres = gdn_add_scaled_dot_f32(row, vk, vdj, vq, S_v); |
| 661 | attn_out[j] = hvx_vec_get_f32(hvx_vec_mul_f32_f32(vres, vscale)); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | static inline void gdn_step_scalar_f32( |
| 666 | float * restrict s_work, |
| 667 | float * restrict attn_out, |
| 668 | const float * restrict q_t, |
| 669 | const float * restrict k_t, |
| 670 | const float * restrict v_t, |
| 671 | const float * restrict g_t, |
| 672 | float beta_val, |
| 673 | float scale, |
| 674 | uint32_t S_v |
| 675 | ) { |
| 676 | const uint32_t epv = 128 / sizeof(float); |
| 677 | const uint32_t nvec = S_v / epv; |
| 678 | const uint32_t nloe = S_v % epv; |
| 679 | |
| 680 | HVX_Vector vq[4]; |
| 681 | HVX_Vector vk[4]; |
| 682 | |
| 683 | for (uint32_t i = 0; i < nvec; ++i) { |
| 684 | vq[i] = hvx_vmemu(q_t + i * epv); |
| 685 | vk[i] = hvx_vmemu(k_t + i * epv); |
| 686 | } |
| 687 | if (nloe) { |
| 688 | vq[nvec] = hvx_vmemu(q_t + nvec * epv); |
| 689 | vk[nvec] = hvx_vmemu(k_t + nvec * epv); |
| 690 | } |
| 691 | |
| 692 | const HVX_Vector vgate = hvx_vec_exp_f32(hvx_vec_splat_f32(g_t[0])); |
| 693 | const HVX_Vector vbeta = hvx_vec_splat_f32(beta_val); |
| 694 | const HVX_Vector vscale = hvx_vec_splat_f32(scale); |
| 695 | |
| 696 | float delta[8] __attribute__((aligned(128))); |
| 697 | |
| 698 | uint32_t j = 0; |
| 699 | for (; j + 8 <= S_v; j += 8) { |
| 700 | float * row0 = s_work + (uint64_t) (j + 0) * S_v; |
| 701 | float * row1 = s_work + (uint64_t) (j + 1) * S_v; |
| 702 | float * row2 = s_work + (uint64_t) (j + 2) * S_v; |
| 703 | float * row3 = s_work + (uint64_t) (j + 3) * S_v; |
| 704 | float * row4 = s_work + (uint64_t) (j + 4) * S_v; |
| 705 | float * row5 = s_work + (uint64_t) (j + 5) * S_v; |
| 706 | float * row6 = s_work + (uint64_t) (j + 6) * S_v; |
| 707 | float * row7 = s_work + (uint64_t) (j + 7) * S_v; |
| 708 | |
| 709 | HVX_Vector vsums = gdn_mul_scalar_dot8_f32(row0, row1, row2, row3, row4, row5, row6, row7, |
| 710 | vgate, vk, S_v); |
| 711 | |
| 712 | HVX_Vector vv_t = hvx_vmemu(v_t + j); |
| 713 | HVX_Vector diff = hvx_vec_sub_f32_f32(vv_t, vsums); |
| 714 | HVX_Vector vdelta = hvx_vec_mul_f32_f32(diff, vbeta); |
| 715 | hvx_vec_store_u(delta, 8 * sizeof(float), vdelta); |
| 716 | |
| 717 | HVX_Vector vattn = gdn_add_scaled_dot8_f32(row0, row1, row2, row3, row4, row5, row6, row7, |
| 718 | vk, delta, vq, S_v); |
| 719 | |
| 720 | HVX_Vector res_attn = hvx_vec_mul_f32_f32(vattn, vscale); |
| 721 | hvx_vec_store_u(attn_out + j, 8 * sizeof(float), res_attn); |
| 722 | } |
| 723 | for (; j + 4 <= S_v; j += 4) { |
| 724 | float * row0 = s_work + (uint64_t) (j + 0) * S_v; |
| 725 | float * row1 = s_work + (uint64_t) (j + 1) * S_v; |
| 726 | float * row2 = s_work + (uint64_t) (j + 2) * S_v; |
| 727 | float * row3 = s_work + (uint64_t) (j + 3) * S_v; |
| 728 | |
| 729 | HVX_Vector vsums = gdn_mul_scalar_dot4_f32(row0, row1, row2, row3, vgate, vk, S_v); |
| 730 | |
| 731 | HVX_Vector vv_t = hvx_vmemu(v_t + j); |
| 732 | HVX_Vector diff = hvx_vec_sub_f32_f32(vv_t, vsums); |
| 733 | HVX_Vector vdelta = hvx_vec_mul_f32_f32(diff, vbeta); |
| 734 | hvx_vec_store_u(delta, 4 * sizeof(float), vdelta); |
| 735 | |
| 736 | HVX_Vector vattn = gdn_add_scaled_dot4_f32(row0, row1, row2, row3, vk, delta, vq, S_v); |
| 737 | |
| 738 | HVX_Vector res_attn = hvx_vec_mul_f32_f32(vattn, vscale); |
| 739 | hvx_vec_store_u(attn_out + j, 4 * sizeof(float), res_attn); |
| 740 | } |
| 741 | for (; j < S_v; ++j) { |
| 742 | float * row = s_work + (uint64_t) j * S_v; |
| 743 | HVX_Vector vsum = gdn_mul_scalar_dot_f32(row, vgate, vk, S_v); |
| 744 | HVX_Vector vv_t = hvx_vec_splat_f32(v_t[j]); |
| 745 | HVX_Vector vdj = hvx_vec_mul_f32_f32(hvx_vec_sub_f32_f32(vv_t, vsum), vbeta); |
| 746 | HVX_Vector vres = gdn_add_scaled_dot_f32(row, vk, vdj, vq, S_v); |
| 747 | attn_out[j] = hvx_vec_get_f32(hvx_vec_mul_f32_f32(vres, vscale)); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | static void gated_delta_net_f32_pp_thread(unsigned int nth, unsigned int ith, void * data) { |
| 752 | struct htp_gdn_context * gctx = (struct htp_gdn_context *) data; |
| 753 | struct htp_ops_context * octx = gctx->octx; |
| 754 | const struct htp_gdn_kernel_params * kparams = gctx->kparams; |
| 755 | |
| 756 | const struct htp_tensor * q = octx->src[0]; |
| 757 | const struct htp_tensor * k = octx->src[1]; |
| 758 | const struct htp_tensor * v = octx->src[2]; |
| 759 | const struct htp_tensor * g = octx->src[3]; |
| 760 | const struct htp_tensor * beta = octx->src[4]; |
| 761 | const struct htp_tensor * state = octx->src[5]; |
| 762 | const struct htp_tensor * dst = octx->dst; |
| 763 | |
| 764 | const uint32_t S_v = kparams->S_v; |
| 765 | const uint32_t H = kparams->H; |
| 766 | const uint32_t n_tokens = kparams->n_tokens; |
| 767 | const uint32_t n_seqs = kparams->n_seqs; |
| 768 | const uint32_t K = kparams->K; |
| 769 | const uint32_t row_end = gctx->row_start + gctx->nrows; |
| 770 | |
| 771 | if (ith >= gctx->nrows) { |
| 772 | return; |
| 773 | } |
| 774 | |
| 775 | const struct htp_tensor * dst_cache = octx->dsts[1]; |
| 776 | const float scale = kparams->scale; |
| 777 | float * dst_base = (float *) (uintptr_t) dst->data; |
| 778 | float * state_out_base = dst_cache ? (float *) (uintptr_t) dst_cache->data : (dst_base + S_v * H * n_tokens * n_seqs); |
| 779 | |
| 780 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 781 | const struct htp_gdn_vtcm_layout * layout = &gctx->layout; |
| 782 | float * s_work[2]; |
| 783 | s_work[0] = (float *) (gctx->vtcm_base + layout->bytes_per_thread * ith); |
| 784 | s_work[1] = s_work[0] + layout->state_aligned / sizeof(float); |
| 785 | |
| 786 | const struct fastdiv_values * fd_H = &kparams->div_H; |
| 787 | const struct fastdiv_values * fd_q1 = &kparams->div_q1; |
| 788 | const struct fastdiv_values * fd_k1 = &kparams->div_k1; |
| 789 | const struct fastdiv_values * fd_rq3 = &kparams->div_rq3; |
| 790 | const struct fastdiv_values * fd_rk3 = &kparams->div_rk3; |
| 791 | |
| 792 | const uint32_t state_seq_stride = kparams->state_seq_stride; |
| 793 | const uint64_t state_size_per_snap = (uint64_t) kparams->state_size_per_snap; |
| 794 | const dma_addr_t state_out_dma_base = dst_cache ? dst_cache->data : (dst->data + S_v * H * n_tokens * n_seqs * sizeof(float)); |
| 795 | |
| 796 | uint32_t ir_prefetch = gctx->row_start + ith; |
| 797 | int spad_idx = 0; |
| 798 | |
| 799 | // Prefetch preamble (up to 2 steps) |
| 800 | for (int step = 0; step < 2 && ir_prefetch < row_end; step++) { |
| 801 | const uint32_t piv1 = fastmodulo(ir_prefetch, H, fd_H); |
| 802 | const uint32_t piv3 = fastdiv(ir_prefetch, fd_H); |
| 803 | dma_addr_t ps_in = state->data + ((uint64_t) piv3 * state_seq_stride + (uint64_t) piv1 * S_v * S_v) * sizeof(float); |
| 804 | dma_addr_t ps_out = state_out_dma_base + ((uint64_t) piv3 * H + piv1) * S_v * S_v * sizeof(float); |
| 805 | |
| 806 | // Push dummy write-back |
| 807 | dma_queue_push(dma_q, dma_make_data(ps_out, s_work[spad_idx]), |
| 808 | S_v * sizeof(float), S_v * sizeof(float), |
| 809 | S_v * sizeof(float), 0); |
| 810 | |
| 811 | // Push fetch |
| 812 | dma_queue_push(dma_q, dma_make_data(s_work[spad_idx], ps_in), |
| 813 | S_v * sizeof(float), S_v * sizeof(float), |
| 814 | S_v * sizeof(float), S_v); |
| 815 | |
| 816 | ir_prefetch += nth; |
| 817 | spad_idx ^= 1; |
| 818 | } |
| 819 | |
| 820 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 821 | |
| 822 | int curr_spad_idx = 0; |
| 823 | for (uint32_t ir = gctx->row_start + ith; ir < row_end; ir += nth) { |
| 824 | dma_queue_pop(dma_q); |
| 825 | dma_queue_pop(dma_q); |
| 826 | |
| 827 | float * s_work_curr = s_work[curr_spad_idx]; |
| 828 | |
| 829 | const uint32_t iv1 = fastmodulo(ir, H, fd_H); |
| 830 | const uint32_t iv3 = fastdiv(ir, fd_H); |
| 831 | |
| 832 | const uint32_t iq1 = fastmodulo(iv1, q->ne[1], fd_q1); |
| 833 | const uint32_t ik1 = fastmodulo(iv1, k->ne[1], fd_k1); |
| 834 | const uint32_t iq3 = fastdiv(iv3, fd_rq3); |
| 835 | const uint32_t ik3 = fastdiv(iv3, fd_rk3); |
| 836 | |
| 837 | dma_addr_t s_out = state_out_dma_base + ((uint64_t) iv3 * H + iv1) * S_v * S_v * sizeof(float); |
| 838 | float * attn_data = dst_base + ((uint64_t) iv3 * n_tokens * H + iv1) * S_v; |
| 839 | |
| 840 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 841 | for (uint32_t t = 0; t < n_tokens; ++t) { |
| 842 | const float * q_t = (const float *) ((const uint8_t *) (uintptr_t) q->data + |
| 843 | (uint64_t) iq3 * q->nb[3] + (uint64_t) t * q->nb[2] + (uint64_t) iq1 * q->nb[1]); |
| 844 | const float * k_t = (const float *) ((const uint8_t *) (uintptr_t) k->data + |
| 845 | (uint64_t) ik3 * k->nb[3] + (uint64_t) t * k->nb[2] + (uint64_t) ik1 * k->nb[1]); |
| 846 | const float * v_t = (const float *) ((const uint8_t *) (uintptr_t) v->data + |
| 847 | (uint64_t) iv3 * v->nb[3] + (uint64_t) t * v->nb[2] + (uint64_t) iv1 * v->nb[1]); |
| 848 | const float * g_t = (const float *) ((const uint8_t *) (uintptr_t) g->data + |
| 849 | (uint64_t) iv3 * g->nb[3] + (uint64_t) t * g->nb[2] + (uint64_t) iv1 * g->nb[1]); |
| 850 | const float beta_val = *(const float *) ((const uint8_t *) (uintptr_t) beta->data + |
| 851 | (uint64_t) iv3 * beta->nb[3] + (uint64_t) t * beta->nb[2] + (uint64_t) iv1 * beta->nb[1]); |
| 852 | |
| 853 | if (kparams->kda) { |
| 854 | gdn_step_kda_f32(s_work_curr, attn_data, q_t, k_t, v_t, g_t, beta_val, scale, S_v); |
| 855 | } else { |
| 856 | gdn_step_scalar_f32(s_work_curr, attn_data, q_t, k_t, v_t, g_t, beta_val, scale, S_v); |
| 857 | } |
| 858 | |
| 859 | if (K > 1) { |
| 860 | const int64_t target_slot = (int64_t) n_tokens - 1 - (int64_t) t; |
| 861 | if (target_slot > 0 && target_slot < (int64_t) K) { |
| 862 | float * curr_state_o = state_out_base + (uint64_t) target_slot * state_size_per_snap + ((uint64_t) iv3 * H + iv1) * S_v * S_v; |
| 863 | hvx_copy_f32_uu((uint8_t *) curr_state_o, (const uint8_t *) s_work_curr, S_v * S_v); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | attn_data += (uint64_t) S_v * H; |
| 868 | } |
| 869 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 870 | |
| 871 | // Push real write-back |
| 872 | dma_queue_push(dma_q, dma_make_data(s_out, s_work_curr), |
| 873 | S_v * sizeof(float), S_v * sizeof(float), |
| 874 | S_v * sizeof(float), S_v); |
| 875 | |
| 876 | // Prefetch next block (if any) |
| 877 | if (ir_prefetch < row_end) { |
| 878 | const uint32_t piv1 = fastmodulo(ir_prefetch, H, fd_H); |
| 879 | const uint32_t piv3 = fastdiv(ir_prefetch, fd_H); |
| 880 | dma_addr_t ps_in = state->data + ((uint64_t) piv3 * state_seq_stride + (uint64_t) piv1 * S_v * S_v) * sizeof(float); |
| 881 | |
| 882 | dma_queue_push(dma_q, dma_make_data(s_work[spad_idx], ps_in), |
| 883 | S_v * sizeof(float), S_v * sizeof(float), |
| 884 | S_v * sizeof(float), S_v); |
| 885 | |
| 886 | ir_prefetch += nth; |
| 887 | spad_idx ^= 1; |
| 888 | } |
| 889 | |
| 890 | curr_spad_idx ^= 1; |
| 891 | } |
| 892 | dma_queue_flush(dma_q); |
| 893 | } |
| 894 | |
| 895 | static void gated_delta_net_f32_tg_thread(unsigned int nth, unsigned int ith, void * data) { |
| 896 | struct htp_gdn_context * gctx = (struct htp_gdn_context *) data; |
| 897 | struct htp_ops_context * octx = gctx->octx; |
| 898 | const struct htp_gdn_kernel_params * kparams = gctx->kparams; |
| 899 | |
| 900 | const struct htp_tensor * q = octx->src[0]; |
| 901 | const struct htp_tensor * k = octx->src[1]; |
| 902 | const struct htp_tensor * v = octx->src[2]; |
| 903 | const struct htp_tensor * g = octx->src[3]; |
| 904 | const struct htp_tensor * beta = octx->src[4]; |
| 905 | const struct htp_tensor * state = octx->src[5]; |
| 906 | const struct htp_tensor * dst = octx->dst; |
| 907 | |
| 908 | const uint32_t S_v = kparams->S_v; |
| 909 | const uint32_t H = kparams->H; |
| 910 | const uint32_t n_seqs = kparams->n_seqs; |
| 911 | const uint32_t row_end = gctx->row_start + gctx->nrows; |
| 912 | |
| 913 | if (ith >= gctx->nrows) { |
| 914 | return; |
| 915 | } |
| 916 | |
| 917 | const struct htp_tensor * dst_cache = octx->dsts[1]; |
| 918 | const float scale = kparams->scale; |
| 919 | float * dst_base = (float *) (uintptr_t) dst->data; |
| 920 | |
| 921 | dma_queue * dma_q = octx->ctx->dma[ith]; |
| 922 | const struct htp_gdn_vtcm_layout * layout = &gctx->layout; |
| 923 | float * s_work[2]; |
| 924 | s_work[0] = (float *) (gctx->vtcm_base + layout->bytes_per_thread * ith); |
| 925 | s_work[1] = s_work[0] + layout->state_aligned / sizeof(float); |
| 926 | |
| 927 | const struct fastdiv_values * fd_H = &kparams->div_H; |
| 928 | const struct fastdiv_values * fd_q1 = &kparams->div_q1; |
| 929 | const struct fastdiv_values * fd_k1 = &kparams->div_k1; |
| 930 | const struct fastdiv_values * fd_rq3 = &kparams->div_rq3; |
| 931 | const struct fastdiv_values * fd_rk3 = &kparams->div_rk3; |
| 932 | |
| 933 | const uint32_t state_seq_stride = kparams->state_seq_stride; |
| 934 | const dma_addr_t state_out_dma_base = dst_cache ? dst_cache->data : (dst->data + S_v * H * n_seqs * sizeof(float)); |
| 935 | |
| 936 | uint32_t ir_prefetch = gctx->row_start + ith; |
| 937 | int spad_idx = 0; |
| 938 | |
| 939 | // Prefetch preamble (up to 2 steps) |
| 940 | for (int step = 0; step < 2 && ir_prefetch < row_end; step++) { |
| 941 | const uint32_t piv1 = fastmodulo(ir_prefetch, H, fd_H); |
| 942 | const uint32_t piv3 = fastdiv(ir_prefetch, fd_H); |
| 943 | dma_addr_t ps_in = state->data + ((uint64_t) piv3 * state_seq_stride + (uint64_t) piv1 * S_v * S_v) * sizeof(float); |
| 944 | dma_addr_t ps_out = state_out_dma_base + ((uint64_t) piv3 * H + piv1) * S_v * S_v * sizeof(float); |
| 945 | |
| 946 | // Push dummy write-back |
| 947 | dma_queue_push(dma_q, dma_make_data(ps_out, s_work[spad_idx]), |
| 948 | S_v * sizeof(float), S_v * sizeof(float), |
| 949 | S_v * sizeof(float), 0); |
| 950 | |
| 951 | // Push fetch |
| 952 | dma_queue_push(dma_q, dma_make_data(s_work[spad_idx], ps_in), |
| 953 | S_v * sizeof(float), S_v * sizeof(float), |
| 954 | S_v * sizeof(float), S_v); |
| 955 | |
| 956 | ir_prefetch += nth; |
| 957 | spad_idx ^= 1; |
| 958 | } |
| 959 | |
| 960 | struct htp_thread_trace * tr = &octx->ctx->trace[ith]; |
| 961 | |
| 962 | int curr_spad_idx = 0; |
| 963 | for (uint32_t ir = gctx->row_start + ith; ir < row_end; ir += nth) { |
| 964 | dma_queue_pop(dma_q); |
| 965 | dma_queue_pop(dma_q); |
| 966 | |
| 967 | float * s_work_curr = s_work[curr_spad_idx]; |
| 968 | |
| 969 | const uint32_t iv1 = fastmodulo(ir, H, fd_H); |
| 970 | const uint32_t iv3 = fastdiv(ir, fd_H); |
| 971 | |
| 972 | const uint32_t iq1 = fastmodulo(iv1, q->ne[1], fd_q1); |
| 973 | const uint32_t ik1 = fastmodulo(iv1, k->ne[1], fd_k1); |
| 974 | const uint32_t iq3 = fastdiv(iv3, fd_rq3); |
| 975 | const uint32_t ik3 = fastdiv(iv3, fd_rk3); |
| 976 | |
| 977 | dma_addr_t s_out = state_out_dma_base + ((uint64_t) iv3 * H + iv1) * S_v * S_v * sizeof(float); |
| 978 | float * attn_data = dst_base + ((uint64_t) iv3 * H + iv1) * S_v; |
| 979 | |
| 980 | const float * q_t = (const float *) ((const uint8_t *) (uintptr_t) q->data + |
| 981 | (uint64_t) iq3 * q->nb[3] + (uint64_t) iq1 * q->nb[1]); |
| 982 | const float * k_t = (const float *) ((const uint8_t *) (uintptr_t) k->data + |
| 983 | (uint64_t) ik3 * k->nb[3] + (uint64_t) ik1 * k->nb[1]); |
| 984 | const float * v_t = (const float *) ((const uint8_t *) (uintptr_t) v->data + |
| 985 | (uint64_t) iv3 * v->nb[3] + (uint64_t) iv1 * v->nb[1]); |
| 986 | const float * g_t = (const float *) ((const uint8_t *) (uintptr_t) g->data + |
| 987 | (uint64_t) iv3 * g->nb[3] + (uint64_t) iv1 * g->nb[1]); |
| 988 | const float beta_val = *(const float *) ((const uint8_t *) (uintptr_t) beta->data + |
| 989 | (uint64_t) iv3 * beta->nb[3] + (uint64_t) iv1 * beta->nb[1]); |
| 990 | |
| 991 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 992 | if (kparams->kda) { |
| 993 | gdn_step_kda_f32(s_work_curr, attn_data, q_t, k_t, v_t, g_t, beta_val, scale, S_v); |
| 994 | } else { |
| 995 | gdn_step_scalar_f32(s_work_curr, attn_data, q_t, k_t, v_t, g_t, beta_val, scale, S_v); |
| 996 | } |
| 997 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, (uint16_t) ir); |
| 998 | |
| 999 | // Push real write-back |
| 1000 | dma_queue_push(dma_q, dma_make_data(s_out, s_work_curr), |
| 1001 | S_v * sizeof(float), S_v * sizeof(float), |
| 1002 | S_v * sizeof(float), S_v); |
| 1003 | |
| 1004 | // Prefetch next block (if any) |
| 1005 | if (ir_prefetch < row_end) { |
| 1006 | const uint32_t piv1 = fastmodulo(ir_prefetch, H, fd_H); |
| 1007 | const uint32_t piv3 = fastdiv(ir_prefetch, fd_H); |
| 1008 | dma_addr_t ps_in = state->data + ((uint64_t) piv3 * state_seq_stride + (uint64_t) piv1 * S_v * S_v) * sizeof(float); |
| 1009 | |
| 1010 | dma_queue_push(dma_q, dma_make_data(s_work[spad_idx], ps_in), |
| 1011 | S_v * sizeof(float), S_v * sizeof(float), |
| 1012 | S_v * sizeof(float), S_v); |
| 1013 | |
| 1014 | ir_prefetch += nth; |
| 1015 | spad_idx ^= 1; |
| 1016 | } |
| 1017 | |
| 1018 | curr_spad_idx ^= 1; |
| 1019 | } |
| 1020 | dma_queue_flush(dma_q); |
| 1021 | } |
| 1022 | |
| 1023 | struct htp_gdn_hmx_gemm_task { |
| 1024 | const __fp16 * row_tiles; |
| 1025 | const __fp16 * col_tiles; |
| 1026 | __fp16 * out_tiles; |
| 1027 | uint32_t n_row_tiles; |
| 1028 | uint32_t n_col_tiles; |
| 1029 | uint32_t n_dot_tiles; |
| 1030 | uint32_t dot_stride; |
| 1031 | uint8_t * hmx_scales; |
| 1032 | }; |
| 1033 | |
| 1034 | static void htp_gdn_hmx_gemm_worker(void * data) { |
| 1035 | struct htp_gdn_hmx_gemm_task * task = (struct htp_gdn_hmx_gemm_task *) data; |
| 1036 | asm volatile(HMX_SET_BIAS("%0") :: "r"((unsigned int)task->hmx_scales)); |
| 1037 | |
| 1038 | const size_t dot_stride = task->dot_stride; |
| 1039 | for (uint32_t r = 0; r < task->n_row_tiles; ++r) { |
| 1040 | const __fp16 * r_tiles = task->row_tiles + r * dot_stride; |
| 1041 | const __fp16 * c_tiles = task->col_tiles; |
| 1042 | __fp16 * o_tile = task->out_tiles + r * task->n_col_tiles * HMX_FP16_TILE_N_ELMS; |
| 1043 | |
| 1044 | for (uint32_t c = 0; c < task->n_col_tiles; ++c) { |
| 1045 | hmx_fa_qk_dot_tile(r_tiles, c_tiles, o_tile, task->n_dot_tiles); |
| 1046 | c_tiles += dot_stride; |
| 1047 | o_tile += HMX_FP16_TILE_N_ELMS; |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | static inline void htp_gdn_push_hmx_gemm_task( |
| 1053 | hmx_queue_t q, |
| 1054 | struct htp_gdn_hmx_gemm_task * task, |
| 1055 | const __fp16 * row_tiles, |
| 1056 | const __fp16 * col_tiles, |
| 1057 | __fp16 * out_tiles, |
| 1058 | uint32_t n_row_tiles, |
| 1059 | uint32_t n_col_tiles, |
| 1060 | uint32_t n_dot_tiles, |
| 1061 | uint8_t * scales |
| 1062 | ) { |
| 1063 | task->row_tiles = row_tiles; |
| 1064 | task->col_tiles = col_tiles; |
| 1065 | task->out_tiles = out_tiles; |
| 1066 | task->n_row_tiles = n_row_tiles; |
| 1067 | task->n_col_tiles = n_col_tiles; |
| 1068 | task->n_dot_tiles = n_dot_tiles; |
| 1069 | task->dot_stride = n_dot_tiles * HMX_FP16_TILE_N_ELMS; |
| 1070 | task->hmx_scales = scales; |
| 1071 | |
| 1072 | hmx_queue_push(q, hmx_queue_make_desc(htp_gdn_hmx_gemm_worker, task)); |
| 1073 | } |
| 1074 | |
| 1075 | static inline void gdn_unpack_64x64_tiles_to_vectors( |
| 1076 | HVX_Vector * restrict rows, |
| 1077 | const __fp16 * restrict tiles |
| 1078 | ) { |
| 1079 | const HVX_Vector * t00 = (const HVX_Vector *) (tiles + 0 * HMX_FP16_TILE_N_ELMS); |
| 1080 | const HVX_Vector * t01 = (const HVX_Vector *) (tiles + 1 * HMX_FP16_TILE_N_ELMS); |
| 1081 | const HVX_Vector * t10 = (const HVX_Vector *) (tiles + 2 * HMX_FP16_TILE_N_ELMS); |
| 1082 | const HVX_Vector * t11 = (const HVX_Vector *) (tiles + 3 * HMX_FP16_TILE_N_ELMS); |
| 1083 | |
| 1084 | for (uint32_t r = 0; r < 16; ++r) { |
| 1085 | HVX_VectorPair vp0 = Q6_W_vdeal_VVR(t01[r], t00[r], -2); |
| 1086 | rows[2 * r + 0] = Q6_V_lo_W(vp0); |
| 1087 | rows[2 * r + 1] = Q6_V_hi_W(vp0); |
| 1088 | |
| 1089 | HVX_VectorPair vp1 = Q6_W_vdeal_VVR(t11[r], t10[r], -2); |
| 1090 | rows[32 + 2 * r + 0] = Q6_V_lo_W(vp1); |
| 1091 | rows[32 + 2 * r + 1] = Q6_V_hi_W(vp1); |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | static inline void gdn_pack_64x64_vectors_to_tiles( |
| 1096 | __fp16 * restrict tiles, |
| 1097 | const HVX_Vector * restrict rows |
| 1098 | ) { |
| 1099 | HVX_Vector * t00 = (HVX_Vector *) (tiles + 0 * HMX_FP16_TILE_N_ELMS); |
| 1100 | HVX_Vector * t01 = (HVX_Vector *) (tiles + 1 * HMX_FP16_TILE_N_ELMS); |
| 1101 | HVX_Vector * t10 = (HVX_Vector *) (tiles + 2 * HMX_FP16_TILE_N_ELMS); |
| 1102 | HVX_Vector * t11 = (HVX_Vector *) (tiles + 3 * HMX_FP16_TILE_N_ELMS); |
| 1103 | |
| 1104 | for (uint32_t r = 0; r < 16; ++r) { |
| 1105 | HVX_VectorPair vp0 = Q6_W_vshuff_VVR(rows[2 * r + 1], rows[2 * r + 0], -2); |
| 1106 | t00[r] = Q6_V_lo_W(vp0); |
| 1107 | t01[r] = Q6_V_hi_W(vp0); |
| 1108 | |
| 1109 | HVX_VectorPair vp1 = Q6_W_vshuff_VVR(rows[32 + 2 * r + 1], rows[32 + 2 * r + 0], -2); |
| 1110 | t10[r] = Q6_V_lo_W(vp1); |
| 1111 | t11[r] = Q6_V_hi_W(vp1); |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | static inline void gdn_unpack_64xS_tiles_to_f32( |
| 1116 | float * restrict dst_f32, |
| 1117 | const __fp16 * restrict tiles, |
| 1118 | uint32_t S_v |
| 1119 | ) { |
| 1120 | const uint32_t n_col_tiles = S_v / 32; |
| 1121 | for (uint32_t r0 = 0; r0 < 2; ++r0) { |
| 1122 | for (uint32_t d = 0; d < S_v / 64; ++d) { |
| 1123 | const HVX_Vector * t0 = (const HVX_Vector *) (tiles + (r0 * n_col_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS); |
| 1124 | const HVX_Vector * t1 = (const HVX_Vector *) (tiles + (r0 * n_col_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS); |
| 1125 | |
| 1126 | for (uint32_t r = 0; r < 16; ++r) { |
| 1127 | HVX_VectorPair vp01 = Q6_W_vdeal_VVR(t1[r], t0[r], -2); |
| 1128 | HVX_VectorPair p0 = hvx_vec_f16_to_f32(Q6_V_lo_W(vp01)); |
| 1129 | HVX_VectorPair p1 = hvx_vec_f16_to_f32(Q6_V_hi_W(vp01)); |
| 1130 | |
| 1131 | float * out0 = dst_f32 + (r0 * 32 + 2 * r + 0) * S_v + d * 64; |
| 1132 | float * out1 = dst_f32 + (r0 * 32 + 2 * r + 1) * S_v + d * 64; |
| 1133 | |
| 1134 | hvx_vmem(out0 + 0) = Q6_V_lo_W(p0); |
| 1135 | hvx_vmem(out0 + 32) = Q6_V_hi_W(p0); |
| 1136 | hvx_vmem(out1 + 0) = Q6_V_lo_W(p1); |
| 1137 | hvx_vmem(out1 + 32) = Q6_V_hi_W(p1); |
| 1138 | } |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | static inline void gdn_unpack_64xS_tiles_to_f16( |
| 1144 | __fp16 * restrict dst_f16, |
| 1145 | const __fp16 * restrict tiles, |
| 1146 | uint32_t S_v |
| 1147 | ) { |
| 1148 | const uint32_t n_col_tiles = S_v / 32; |
| 1149 | for (uint32_t r0 = 0; r0 < 2; ++r0) { |
| 1150 | for (uint32_t d = 0; d < S_v / 64; ++d) { |
| 1151 | const HVX_Vector * t0 = (const HVX_Vector *) (tiles + (r0 * n_col_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS); |
| 1152 | const HVX_Vector * t1 = (const HVX_Vector *) (tiles + (r0 * n_col_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS); |
| 1153 | |
| 1154 | for (uint32_t r = 0; r < 16; ++r) { |
| 1155 | HVX_VectorPair vp01 = Q6_W_vdeal_VVR(t1[r], t0[r], -2); |
| 1156 | __fp16 * out0 = dst_f16 + (r0 * 32 + 2 * r + 0) * S_v + d * 64; |
| 1157 | __fp16 * out1 = dst_f16 + (r0 * 32 + 2 * r + 1) * S_v + d * 64; |
| 1158 | |
| 1159 | hvx_vmem(out0) = Q6_V_lo_W(vp01); |
| 1160 | hvx_vmem(out1) = Q6_V_hi_W(vp01); |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | static inline void gdn_unpack_SxS_tiles_to_f32( |
| 1167 | float * restrict dst_f32, |
| 1168 | const __fp16 * restrict tiles, |
| 1169 | uint32_t S_v |
| 1170 | ) { |
| 1171 | const uint32_t n_tiles = S_v / 32; |
| 1172 | for (uint32_t r0 = 0; r0 < n_tiles; ++r0) { |
| 1173 | for (uint32_t d = 0; d < S_v / 64; ++d) { |
| 1174 | const HVX_Vector * t0 = (const HVX_Vector *) (tiles + (r0 * n_tiles + 2 * d + 0) * HMX_FP16_TILE_N_ELMS); |
| 1175 | const HVX_Vector * t1 = (const HVX_Vector *) (tiles + (r0 * n_tiles + 2 * d + 1) * HMX_FP16_TILE_N_ELMS); |
| 1176 | |
| 1177 | for (uint32_t r = 0; r < 16; ++r) { |
| 1178 | HVX_VectorPair vp01 = Q6_W_vdeal_VVR(t1[r], t0[r], -2); |
| 1179 | HVX_VectorPair p0 = hvx_vec_f16_to_f32(Q6_V_lo_W(vp01)); |
| 1180 | HVX_VectorPair p1 = hvx_vec_f16_to_f32(Q6_V_hi_W(vp01)); |
| 1181 | |
| 1182 | float * out0 = dst_f32 + (r0 * 32 + 2 * r + 0) * S_v + d * 64; |
| 1183 | float * out1 = dst_f32 + (r0 * 32 + 2 * r + 1) * S_v + d * 64; |
| 1184 | |
| 1185 | hvx_vmem(out0 + 0) = Q6_V_lo_W(p0); |
| 1186 | hvx_vmem(out0 + 32) = Q6_V_hi_W(p0); |
| 1187 | hvx_vmem(out1 + 0) = Q6_V_lo_W(p1); |
| 1188 | hvx_vmem(out1 + 32) = Q6_V_hi_W(p1); |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | static inline void gdn_f32_to_hmx_row_tiles_and_f16( |
| 1195 | __fp16 * restrict dst_tiles, |
| 1196 | __fp16 * restrict dst_prime_tiles, |
| 1197 | __fp16 * restrict dst_f16, |
| 1198 | const float * restrict src, |
| 1199 | const __fp16 * restrict scale_per_row, |
| 1200 | uint32_t n_rows, |
| 1201 | uint32_t n_cols |
| 1202 | ) { |
| 1203 | const uint32_t n_col_tiles = n_cols / 32; |
| 1204 | const uint32_t * scale_pairs = (const uint32_t *) scale_per_row; |
| 1205 | |
| 1206 | for (uint32_t r = 0; r < n_rows; r += 2) { |
| 1207 | uint32_t r0 = r / 32; |
| 1208 | uint32_t r1 = (r % 32) / 2; |
| 1209 | const float * p0 = src + (r + 0) * n_cols; |
| 1210 | const float * p1 = src + (r + 1) * n_cols; |
| 1211 | |
| 1212 | HVX_Vector v_scale; |
| 1213 | if (dst_prime_tiles) { |
| 1214 | uint32_t scale_pair = scale_pairs ? scale_pairs[r / 2] : 0x3c003c00; |
| 1215 | v_scale = Q6_V_vsplat_R(scale_pair); |
| 1216 | } |
| 1217 | |
| 1218 | for (uint32_t c = 0; c < n_col_tiles; c += 2) { |
| 1219 | HVX_Vector v0_0 = hvx_vmem(p0 + (c + 0) * 32); |
| 1220 | HVX_Vector v1_0 = hvx_vmem(p1 + (c + 0) * 32); |
| 1221 | HVX_Vector v0_1 = hvx_vmem(p0 + (c + 1) * 32); |
| 1222 | HVX_Vector v1_1 = hvx_vmem(p1 + (c + 1) * 32); |
| 1223 | |
| 1224 | HVX_Vector vh0 = hvx_vec_f32_to_f16_shuff(v0_0, v1_0); |
| 1225 | HVX_Vector vh1 = hvx_vec_f32_to_f16_shuff(v0_1, v1_1); |
| 1226 | __fp16 * tile0 = dst_tiles + (r0 * n_col_tiles + c + 0) * HMX_FP16_TILE_N_ELMS; |
| 1227 | __fp16 * tile1 = dst_tiles + (r0 * n_col_tiles + c + 1) * HMX_FP16_TILE_N_ELMS; |
| 1228 | ((HVX_Vector *) tile0)[r1] = vh0; |
| 1229 | ((HVX_Vector *) tile1)[r1] = vh1; |
| 1230 | |
| 1231 | if (dst_prime_tiles) { |
| 1232 | HVX_Vector vh0_s = hvx_vec_mul_f16_f16(vh0, v_scale); |
| 1233 | HVX_Vector vh1_s = hvx_vec_mul_f16_f16(vh1, v_scale); |
| 1234 | __fp16 * tile0_s = dst_prime_tiles + (r0 * n_col_tiles + c + 0) * HMX_FP16_TILE_N_ELMS; |
| 1235 | __fp16 * tile1_s = dst_prime_tiles + (r0 * n_col_tiles + c + 1) * HMX_FP16_TILE_N_ELMS; |
| 1236 | ((HVX_Vector *) tile0_s)[r1] = vh0_s; |
| 1237 | ((HVX_Vector *) tile1_s)[r1] = vh1_s; |
| 1238 | } |
| 1239 | |
| 1240 | if (dst_f16) { |
| 1241 | HVX_VectorPair vp01 = Q6_W_vdeal_VVR(vh1, vh0, -2); |
| 1242 | hvx_vmem(dst_f16 + (r + 0) * n_cols + c * 32) = Q6_V_lo_W(vp01); |
| 1243 | hvx_vmem(dst_f16 + (r + 1) * n_cols + c * 32) = Q6_V_hi_W(vp01); |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | static inline void hvx_transpose_32x32_words(HVX_Vector * restrict m, HVX_Vector * restrict tmp) { |
| 1250 | for (int i = 0; i < 16; ++i) { |
| 1251 | HVX_VectorPair p = Q6_W_vshuff_VVR(m[2*i + 1], m[2*i], -4); |
| 1252 | tmp[2*i + 0] = Q6_V_lo_W(p); |
| 1253 | tmp[2*i + 1] = Q6_V_hi_W(p); |
| 1254 | } |
| 1255 | |
| 1256 | for (int b = 0; b < 32; b += 4) { |
| 1257 | HVX_VectorPair p0 = Q6_W_vshuff_VVR(tmp[b + 2], tmp[b + 0], -8); |
| 1258 | HVX_VectorPair p1 = Q6_W_vshuff_VVR(tmp[b + 3], tmp[b + 1], -8); |
| 1259 | m[b + 0] = Q6_V_lo_W(p0); m[b + 1] = Q6_V_hi_W(p0); |
| 1260 | m[b + 2] = Q6_V_lo_W(p1); m[b + 3] = Q6_V_hi_W(p1); |
| 1261 | } |
| 1262 | |
| 1263 | for (int b = 0; b < 32; b += 8) { |
| 1264 | for (int i = 0; i < 4; ++i) { |
| 1265 | HVX_VectorPair p = Q6_W_vshuff_VVR(m[b + i + 4], m[b + i], -16); |
| 1266 | tmp[b + 2*i + 0] = Q6_V_lo_W(p); |
| 1267 | tmp[b + 2*i + 1] = Q6_V_hi_W(p); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | for (int b = 0; b < 32; b += 16) { |
| 1272 | for (int i = 0; i < 8; ++i) { |
| 1273 | HVX_VectorPair p = Q6_W_vshuff_VVR(tmp[b + i + 8], tmp[b + i], -32); |
| 1274 | m[b + 2*i + 0] = Q6_V_lo_W(p); |
| 1275 | m[b + 2*i + 1] = Q6_V_hi_W(p); |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | for (int i = 0; i < 16; ++i) { |
| 1280 | HVX_VectorPair p = Q6_W_vshuff_VVR(m[i + 16], m[i], -64); |
| 1281 | tmp[2 * i + 0] = Q6_V_lo_W(p); |
| 1282 | tmp[2 * i + 1] = Q6_V_hi_W(p); |
| 1283 | } |
| 1284 | |
| 1285 | for (int i = 0; i < 32; ++i) { |
| 1286 | m[i] = tmp[i]; |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | static inline void gdn_pack_d_t_row_tiles( |
| 1291 | __fp16 * restrict dst_tiles, |
| 1292 | const __fp16 * restrict src_d, |
| 1293 | uint32_t S_v, |
| 1294 | HVX_Vector * restrict m, |
| 1295 | HVX_Vector * restrict tmp |
| 1296 | ) { |
| 1297 | for (uint32_t col_half = 0; col_half < S_v / 64; ++col_half) { |
| 1298 | uint32_t r0_base = col_half * 2; |
| 1299 | for (uint32_t c0 = 0; c0 < 2; ++c0) { |
| 1300 | for (uint32_t s_local = 0; s_local < 32; ++s_local) { |
| 1301 | uint32_t s = c0 * 32 + s_local; |
| 1302 | m[s_local] = hvx_vmem(src_d + s * S_v + col_half * 64); |
| 1303 | } |
| 1304 | |
| 1305 | hvx_transpose_32x32_words(m, tmp); |
| 1306 | |
| 1307 | uint32_t tile0_idx = (r0_base + 0) * 2 + c0; |
| 1308 | uint32_t tile1_idx = (r0_base + 1) * 2 + c0; |
| 1309 | HVX_Vector * t0 = (HVX_Vector *)(dst_tiles + tile0_idx * HMX_FP16_TILE_N_ELMS); |
| 1310 | HVX_Vector * t1 = (HVX_Vector *)(dst_tiles + tile1_idx * HMX_FP16_TILE_N_ELMS); |
| 1311 | |
| 1312 | for (uint32_t r = 0; r < 16; ++r) { |
| 1313 | t0[r] = m[r]; |
| 1314 | t1[r] = m[16 + r]; |
| 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | static __attribute__((noinline)) void gdn_build_inv_l_blocks( |
| 1321 | __fp16 * restrict inv_row_tiles, |
| 1322 | const HVX_Vector * restrict rows_kk, |
| 1323 | const __fp16 * restrict decay_m, |
| 1324 | const float * restrict beta, |
| 1325 | __fp16 * restrict l10_tile, |
| 1326 | __fp16 * restrict neg_a11_tile |
| 1327 | ) { |
| 1328 | const HVX_Vector v_one_f16 = hvx_vec_splat_f16(1.0f); |
| 1329 | const HVX_VectorPred q_mask64 = Q6_Q_vsetq2_R(64); |
| 1330 | |
| 1331 | uint16_t beta_u16[64] __attribute__((aligned(128))); |
| 1332 | uint16_t l00[32][32] __attribute__((aligned(128))); |
| 1333 | uint16_t l11[32][32] __attribute__((aligned(128))); |
| 1334 | |
| 1335 | HVX_Vector * restrict p_l00 = (HVX_Vector *) l00; |
| 1336 | HVX_Vector * restrict p_l11 = (HVX_Vector *) l11; |
| 1337 | HVX_Vector * restrict p_l10_tile = (HVX_Vector *) l10_tile; |
| 1338 | |
| 1339 | HVX_Vector * restrict tile00 = (HVX_Vector *) (inv_row_tiles + 0 * HMX_FP16_TILE_N_ELMS); |
| 1340 | HVX_Vector * restrict tile01 = (HVX_Vector *) (inv_row_tiles + 1 * HMX_FP16_TILE_N_ELMS); |
| 1341 | HVX_Vector * restrict tile11 = (HVX_Vector *) (inv_row_tiles + 3 * HMX_FP16_TILE_N_ELMS); |
| 1342 | HVX_Vector * restrict p_neg_a11 = (HVX_Vector *) neg_a11_tile; |
| 1343 | |
| 1344 | hvx_vmem(beta_u16) = hvx_vec_f32_to_f16(hvx_vmem(beta + 0), hvx_vmem(beta + 32)); |
| 1345 | |
| 1346 | for (uint32_t r = 0; r < 16; ++r) { |
| 1347 | tile01[r] = Q6_V_vzero(); |
| 1348 | } |
| 1349 | |
| 1350 | for (uint32_t r = 0; r < 16; ++r) { |
| 1351 | uint32_t t0 = 2 * r; |
| 1352 | uint32_t t1 = t0 + 1; |
| 1353 | |
| 1354 | HVX_Vector v_d0 = hvx_vmem(decay_m + t0 * 64); |
| 1355 | HVX_Vector v_d1 = hvx_vmem(decay_m + t1 * 64); |
| 1356 | HVX_Vector v_b0 = Q6_Vh_vsplat_R(beta_u16[t0]); |
| 1357 | HVX_Vector v_b1 = Q6_Vh_vsplat_R(beta_u16[t1]); |
| 1358 | |
| 1359 | HVX_Vector r0 = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(rows_kk[t0], v_d0), v_b0); |
| 1360 | HVX_Vector r1 = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(rows_kk[t1], v_d1), v_b1); |
| 1361 | |
| 1362 | p_l00[r] = Q6_V_vmux_QVV(q_mask64, r0, Q6_V_vror_VR(r1, 64)); |
| 1363 | } |
| 1364 | |
| 1365 | for (uint32_t r = 0; r < 16; ++r) { |
| 1366 | uint32_t t0 = 32 + 2 * r; |
| 1367 | uint32_t t1 = t0 + 1; |
| 1368 | |
| 1369 | HVX_Vector v_d0 = hvx_vmem(decay_m + t0 * 64); |
| 1370 | HVX_Vector v_d1 = hvx_vmem(decay_m + t1 * 64); |
| 1371 | HVX_Vector v_b0 = Q6_Vh_vsplat_R(beta_u16[t0]); |
| 1372 | HVX_Vector v_b1 = Q6_Vh_vsplat_R(beta_u16[t1]); |
| 1373 | |
| 1374 | HVX_Vector r0 = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(rows_kk[t0], v_d0), v_b0); |
| 1375 | HVX_Vector r1 = hvx_vec_mul_f16_f16(hvx_vec_mul_f16_f16(rows_kk[t1], v_d1), v_b1); |
| 1376 | |
| 1377 | HVX_VectorPair vp_l10 = Q6_W_vshuff_VVR(r1, r0, -2); |
| 1378 | p_l10_tile[r] = Q6_V_lo_W(vp_l10); |
| 1379 | p_l11[r] = Q6_V_vmux_QVV(q_mask64, Q6_V_vror_VR(r0, 64), r1); |
| 1380 | } |
| 1381 | |
| 1382 | HVX_Vector a_rows[32]; |
| 1383 | for (uint32_t t = 0; t < 32; ++t) { |
| 1384 | HVX_Vector v_inv = Q6_V_vzero(); |
| 1385 | for (uint32_t k = 0; k < t; ++k) { |
| 1386 | HVX_Vector v_lk = Q6_Vh_vsplat_R(l00[t][k]); |
| 1387 | v_inv = hvx_vec_sub_f16_f16(v_inv, hvx_vec_mul_f16_f16(v_lk, a_rows[k])); |
| 1388 | } |
| 1389 | HVX_VectorPred q_diag = (t == 0) ? Q6_Q_vsetq2_R(2) : Q6_Q_and_QQn(Q6_Q_vsetq2_R(2 * (t + 1)), Q6_Q_vsetq2_R(2 * t)); |
| 1390 | a_rows[t] = Q6_V_vand_QV(q_mask64, Q6_V_vmux_QVV(q_diag, v_one_f16, v_inv)); |
| 1391 | } |
| 1392 | |
| 1393 | for (uint32_t r = 0; r < 16; ++r) { |
| 1394 | HVX_VectorPair vp = Q6_W_vshuff_VVR(a_rows[2 * r + 1], a_rows[2 * r + 0], -2); |
| 1395 | tile00[r] = Q6_V_lo_W(vp); |
| 1396 | } |
| 1397 | |
| 1398 | for (uint32_t t = 0; t < 32; ++t) { |
| 1399 | HVX_Vector v_inv = Q6_V_vzero(); |
| 1400 | for (uint32_t k = 0; k < t; ++k) { |
| 1401 | HVX_Vector v_lk = Q6_Vh_vsplat_R(l11[t][k]); |
| 1402 | v_inv = hvx_vec_sub_f16_f16(v_inv, hvx_vec_mul_f16_f16(v_lk, a_rows[k])); |
| 1403 | } |
| 1404 | HVX_VectorPred q_diag = (t == 0) ? Q6_Q_vsetq2_R(2) : Q6_Q_and_QQn(Q6_Q_vsetq2_R(2 * (t + 1)), Q6_Q_vsetq2_R(2 * t)); |
| 1405 | a_rows[t] = Q6_V_vand_QV(q_mask64, Q6_V_vmux_QVV(q_diag, v_one_f16, v_inv)); |
| 1406 | } |
| 1407 | |
| 1408 | for (uint32_t r = 0; r < 16; ++r) { |
| 1409 | HVX_VectorPair vp = Q6_W_vshuff_VVR(a_rows[2 * r + 1], a_rows[2 * r + 0], -2); |
| 1410 | tile11[r] = Q6_V_lo_W(vp); |
| 1411 | |
| 1412 | HVX_Vector n0 = hvx_vec_sub_f16_f16(Q6_V_vzero(), a_rows[2 * r + 0]); |
| 1413 | HVX_Vector n1 = hvx_vec_sub_f16_f16(Q6_V_vzero(), a_rows[2 * r + 1]); |
| 1414 | HVX_VectorPair vp_neg = Q6_W_vshuff_VVR(n1, n0, -2); |
| 1415 | p_neg_a11[r] = Q6_V_lo_W(vp_neg); |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | static inline void gdn_dma_push_chunk_inputs( |
| 1421 | dma_queue * dma_q, |
| 1422 | float * vtcm_q, |
| 1423 | float * vtcm_k, |
| 1424 | float * vtcm_v, |
| 1425 | const struct htp_tensor * q, |
| 1426 | const struct htp_tensor * k, |
| 1427 | const struct htp_tensor * v, |
| 1428 | uint32_t iq3, uint32_t iq1, |
| 1429 | uint32_t ik3, uint32_t ik1, |
| 1430 | uint32_t iv3, uint32_t iv1, |
| 1431 | uint32_t t_chunk, |
| 1432 | uint32_t chunk_size, |
| 1433 | uint32_t S_v |
| 1434 | ) { |
| 1435 | const dma_addr_t q_dma = q->data + (uint64_t) iq3 * q->nb[3] + (uint64_t) t_chunk * q->nb[2] + (uint64_t) iq1 * q->nb[1]; |
| 1436 | const dma_addr_t k_dma = k->data + (uint64_t) ik3 * k->nb[3] + (uint64_t) t_chunk * k->nb[2] + (uint64_t) ik1 * k->nb[1]; |
| 1437 | const dma_addr_t v_dma = v->data + (uint64_t) iv3 * v->nb[3] + (uint64_t) t_chunk * v->nb[2] + (uint64_t) iv1 * v->nb[1]; |
| 1438 | |
| 1439 | dma_queue_push(dma_q, dma_make_data(vtcm_q, q_dma), S_v * sizeof(float), q->nb[2], S_v * sizeof(float), chunk_size); |
| 1440 | dma_queue_push(dma_q, dma_make_data(vtcm_k, k_dma), S_v * sizeof(float), k->nb[2], S_v * sizeof(float), chunk_size); |
| 1441 | dma_queue_push(dma_q, dma_make_data(vtcm_v, v_dma), S_v * sizeof(float), v->nb[2], S_v * sizeof(float), chunk_size); |
| 1442 | } |
| 1443 | |
| 1444 | static inline void gdn_dma_push_chunk_gb( |
| 1445 | dma_queue * dma_q, |
| 1446 | float * vtcm_g_raw, |
| 1447 | float * vtcm_b_raw, |
| 1448 | const struct htp_tensor * g, |
| 1449 | const struct htp_tensor * beta, |
| 1450 | uint32_t iv3, |
| 1451 | uint32_t iv1, |
| 1452 | uint32_t t_chunk, |
| 1453 | uint32_t chunk_size, |
| 1454 | uint32_t n_batch |
| 1455 | ) { |
| 1456 | const dma_addr_t g_dma = g->data + (uint64_t) iv3 * g->nb[3] + (uint64_t) t_chunk * g->nb[2] + (uint64_t) iv1 * g->nb[1]; |
| 1457 | const dma_addr_t beta_dma = beta->data + (uint64_t) iv3 * beta->nb[3] + (uint64_t) t_chunk * beta->nb[2] + (uint64_t) iv1 * beta->nb[1]; |
| 1458 | const uint32_t row_bytes = n_batch * sizeof(float); |
| 1459 | const uint32_t dst_stride = (n_batch > 1) ? row_bytes + sizeof(float) : row_bytes; |
| 1460 | |
| 1461 | dma_queue_push(dma_q, dma_make_data(vtcm_g_raw, g_dma), dst_stride, g->nb[2], row_bytes, chunk_size); |
| 1462 | dma_queue_push(dma_q, dma_make_data(vtcm_b_raw, beta_dma), dst_stride, beta->nb[2], row_bytes, chunk_size); |
| 1463 | } |
| 1464 | |
| 1465 | static inline void gdn_pack_s_col_tiles( |
| 1466 | __fp16 * restrict vtcm_s_col_tiles, |
| 1467 | __fp16 * restrict vtcm_s_f16, |
| 1468 | const float * restrict vtcm_s_state, |
| 1469 | uint32_t S_v |
| 1470 | ) { |
| 1471 | for (uint32_t j = 0; j < S_v; ++j) { |
| 1472 | for (uint32_t i = 0; i < S_v; i += 64) { |
| 1473 | HVX_Vector v0 = hvx_vmem(vtcm_s_state + j * S_v + i + 0); |
| 1474 | HVX_Vector v1 = (i + 32 < S_v) ? hvx_vmem(vtcm_s_state + j * S_v + i + 32) : Q6_V_vzero(); |
| 1475 | hvx_vmem(vtcm_s_f16 + j * S_v + i) = hvx_vec_f32_to_f16(v0, v1); |
| 1476 | } |
| 1477 | } |
| 1478 | hmx_interleave_rows_to_tiles(vtcm_s_col_tiles, vtcm_s_f16, S_v, S_v, S_v, 0, S_v); |
| 1479 | } |
| 1480 | |
| 1481 | struct htp_gdn_head_ptrs { |
| 1482 | float * s_state; |
| 1483 | __fp16 * s_f16; |
| 1484 | __fp16 * s_col_tiles; |
| 1485 | float * s_update_f32; |
| 1486 | __fp16 * s_update_tiles; |
| 1487 | |
| 1488 | float * q_f32[2]; |
| 1489 | float * k_f32[2]; |
| 1490 | float * v_f32[2]; |
| 1491 | float * g_f32[2]; |
| 1492 | float * b_f32[2]; |
| 1493 | float * o_f32[2]; |
| 1494 | |
| 1495 | float * v_inter_f32; |
| 1496 | float * o_inter_f32; |
| 1497 | float * o_intra_f32; |
| 1498 | |
| 1499 | __fp16 * k_f16; |
| 1500 | __fp16 * v_prime_f16; |
| 1501 | __fp16 * delta_f16; |
| 1502 | __fp16 * d_f16; |
| 1503 | |
| 1504 | __fp16 * q_row_tiles; |
| 1505 | __fp16 * q_prime_row_tiles; |
| 1506 | __fp16 * k_row_tiles; |
| 1507 | __fp16 * k_col_tiles; |
| 1508 | __fp16 * k_prime_row_tiles; |
| 1509 | __fp16 * k_col_tiles_64x128; |
| 1510 | __fp16 * kk_tiles; |
| 1511 | __fp16 * qk_tiles; |
| 1512 | __fp16 * v_inter_tiles; |
| 1513 | __fp16 * o_inter_tiles; |
| 1514 | __fp16 * inv_row_tiles; |
| 1515 | __fp16 * a_row_tiles; |
| 1516 | __fp16 * v_prime_col_tiles; |
| 1517 | __fp16 * delta_tiles; |
| 1518 | __fp16 * delta_col_tiles; |
| 1519 | __fp16 * o_intra_tiles; |
| 1520 | __fp16 * d_row_tiles; |
| 1521 | |
| 1522 | __fp16 * gamma; |
| 1523 | float * lambda_init; |
| 1524 | __fp16 * lambda_init_f16; |
| 1525 | __fp16 * decay_m; |
| 1526 | __fp16 * decay_a; |
| 1527 | |
| 1528 | HVX_Vector * rows_kk; |
| 1529 | HVX_Vector * rows_qk; |
| 1530 | HVX_Vector * rows_inv; |
| 1531 | HVX_Vector * rows_a; |
| 1532 | |
| 1533 | HVX_Vector * vtcm_m; |
| 1534 | HVX_Vector * vtcm_tmp; |
| 1535 | |
| 1536 | uint32_t iv1; |
| 1537 | uint32_t iv3; |
| 1538 | uint32_t iq1; |
| 1539 | uint32_t ik1; |
| 1540 | uint32_t iq3; |
| 1541 | uint32_t ik3; |
| 1542 | dma_addr_t state_in_dma; |
| 1543 | dma_addr_t state_out_dma; |
| 1544 | }; |
| 1545 | |
| 1546 | static inline void gdn_init_head_ptrs( |
| 1547 | struct htp_gdn_head_ptrs * head, |
| 1548 | const struct htp_gdn_hmx_vtcm_layout * L, |
| 1549 | uint8_t * vtcm_base, |
| 1550 | uint32_t h, |
| 1551 | uint32_t base_iv1, |
| 1552 | uint32_t iv3, |
| 1553 | const struct htp_tensor * q, |
| 1554 | const struct htp_tensor * k, |
| 1555 | const struct htp_tensor * v, |
| 1556 | const struct htp_tensor * state, |
| 1557 | const struct htp_tensor * dst, |
| 1558 | const struct htp_tensor * dst_cache, |
| 1559 | const struct htp_gdn_kernel_params * kparams, |
| 1560 | uint32_t S_v, |
| 1561 | uint32_t H, |
| 1562 | uint32_t n_tokens, |
| 1563 | uint32_t chunk_size |
| 1564 | ) { |
| 1565 | const size_t dma_scalar_sz = hex_round_up(chunk_size * sizeof(float), 128); |
| 1566 | const size_t decay_sz = 64 * 64 * sizeof(__fp16); |
| 1567 | const size_t row_vecs_sz = 64 * 128; |
| 1568 | |
| 1569 | head->s_state = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_s_state + h * L->state_f32_bytes); |
| 1570 | head->s_f16 = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_s_f16 + h * L->state_f16_bytes); |
| 1571 | head->s_col_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_s_col_tiles + h * L->state_tiles_bytes); |
| 1572 | head->s_update_f32 = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_s_update_f32 + h * L->state_f32_bytes); |
| 1573 | head->s_update_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_s_update_tiles + h * L->state_tiles_bytes); |
| 1574 | |
| 1575 | head->q_f32[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_q_f32[0] + h * L->dma_chunk_bytes); |
| 1576 | head->q_f32[1] = L->pipeline ? VTCM_LAYOUT_PTR(float, vtcm_base, L->off_q_f32[1] + h * L->dma_chunk_bytes) : head->q_f32[0]; |
| 1577 | head->k_f32[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_k_f32[0] + h * L->dma_chunk_bytes); |
| 1578 | head->k_f32[1] = L->pipeline ? VTCM_LAYOUT_PTR(float, vtcm_base, L->off_k_f32[1] + h * L->dma_chunk_bytes) : head->k_f32[0]; |
| 1579 | head->v_f32[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_v_f32[0] + h * L->dma_chunk_bytes); |
| 1580 | head->v_f32[1] = L->pipeline ? VTCM_LAYOUT_PTR(float, vtcm_base, L->off_v_f32[1] + h * L->dma_chunk_bytes) : head->v_f32[0]; |
| 1581 | head->g_f32[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_g_f32[0] + h * dma_scalar_sz); |
| 1582 | head->g_f32[1] = L->pipeline ? VTCM_LAYOUT_PTR(float, vtcm_base, L->off_g_f32[1] + h * dma_scalar_sz) : head->g_f32[0]; |
| 1583 | head->b_f32[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_b_f32[0] + h * dma_scalar_sz); |
| 1584 | head->b_f32[1] = L->pipeline ? VTCM_LAYOUT_PTR(float, vtcm_base, L->off_b_f32[1] + h * dma_scalar_sz) : head->b_f32[0]; |
| 1585 | head->o_f32[0] = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_o_f32[0] + h * L->dma_chunk_bytes); |
| 1586 | head->o_f32[1] = L->pipeline ? VTCM_LAYOUT_PTR(float, vtcm_base, L->off_o_f32[1] + h * L->dma_chunk_bytes) : head->o_f32[0]; |
| 1587 | |
| 1588 | head->v_inter_f32 = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_v_inter_f32 + h * L->dma_chunk_bytes); |
| 1589 | head->o_inter_f32 = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_o_inter_f32 + h * L->dma_chunk_bytes); |
| 1590 | head->o_intra_f32 = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_o_intra_f32 + h * L->dma_chunk_bytes); |
| 1591 | |
| 1592 | head->k_f16 = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_f16 + h * L->act_f16_bytes); |
| 1593 | head->v_prime_f16 = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_v_prime_f16 + h * L->act_f16_bytes); |
| 1594 | head->delta_f16 = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_delta_f16 + h * L->act_f16_bytes); |
| 1595 | head->d_f16 = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_d_f16 + h * L->act_f16_bytes); |
| 1596 | |
| 1597 | head->q_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_q_row_tiles + h * L->tile_64xSv_bytes); |
| 1598 | head->q_prime_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_q_prime_row_tiles + h * L->tile_64xSv_bytes); |
| 1599 | head->k_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_row_tiles + h * L->tile_64xSv_bytes); |
| 1600 | head->k_col_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_col_tiles + h * L->tile_64xSv_bytes); |
| 1601 | head->k_prime_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_prime_row_tiles + h * L->tile_64xSv_bytes); |
| 1602 | head->k_col_tiles_64x128 = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_k_col_tiles_64x128 + h * L->tile_64xSv_bytes); |
| 1603 | head->kk_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_kk_tiles + h * L->tile_64x64_bytes); |
| 1604 | head->qk_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_qk_tiles + h * L->tile_64x64_bytes); |
| 1605 | head->v_inter_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_v_inter_tiles + h * L->tile_64xSv_bytes); |
| 1606 | head->o_inter_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_o_inter_tiles + h * L->tile_64xSv_bytes); |
| 1607 | head->inv_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_inv_row_tiles + h * L->tile_64x64_bytes); |
| 1608 | head->a_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_a_row_tiles + h * L->tile_64x64_bytes); |
| 1609 | head->v_prime_col_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_v_prime_col_tiles + h * L->tile_64xSv_bytes); |
| 1610 | head->delta_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_delta_tiles + h * L->tile_64xSv_bytes); |
| 1611 | head->delta_col_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_delta_col_tiles + h * L->tile_64xSv_bytes); |
| 1612 | head->o_intra_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_o_intra_tiles + h * L->tile_64xSv_bytes); |
| 1613 | head->d_row_tiles = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_d_row_tiles + h * L->tile_64xSv_bytes); |
| 1614 | |
| 1615 | head->gamma = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_gamma + h * dma_scalar_sz); |
| 1616 | head->lambda_init_f16 = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_gamma + h * dma_scalar_sz + 128); |
| 1617 | head->lambda_init = VTCM_LAYOUT_PTR(float, vtcm_base, L->off_lambda_init + h * dma_scalar_sz); |
| 1618 | head->decay_m = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_decay_m + h * decay_sz); |
| 1619 | head->decay_a = VTCM_LAYOUT_PTR(__fp16, vtcm_base, L->off_decay_a + h * decay_sz); |
| 1620 | |
| 1621 | head->rows_kk = VTCM_LAYOUT_PTR(HVX_Vector, vtcm_base, L->off_rows_kk + h * row_vecs_sz); |
| 1622 | head->rows_qk = VTCM_LAYOUT_PTR(HVX_Vector, vtcm_base, L->off_rows_qk + h * row_vecs_sz); |
| 1623 | head->rows_inv = VTCM_LAYOUT_PTR(HVX_Vector, vtcm_base, L->off_rows_inv + h * row_vecs_sz); |
| 1624 | head->rows_a = VTCM_LAYOUT_PTR(HVX_Vector, vtcm_base, L->off_rows_a + h * row_vecs_sz); |
| 1625 | |
| 1626 | head->vtcm_m = VTCM_LAYOUT_PTR(HVX_Vector, vtcm_base, L->off_thread_scratch + h * (64 * 128)); |
| 1627 | head->vtcm_tmp = head->vtcm_m + 32; |
| 1628 | |
| 1629 | head->iv1 = base_iv1 + h; |
| 1630 | head->iv3 = iv3; |
| 1631 | head->iq1 = fastmodulo(head->iv1, q->ne[1], &kparams->div_q1); |
| 1632 | head->ik1 = fastmodulo(head->iv1, k->ne[1], &kparams->div_k1); |
| 1633 | head->iq3 = fastdiv(head->iv3, &kparams->div_rq3); |
| 1634 | head->ik3 = fastdiv(head->iv3, &kparams->div_rk3); |
| 1635 | |
| 1636 | head->state_in_dma = state->data + |
| 1637 | ((uint64_t) head->iv3 * kparams->state_seq_stride + (uint64_t) head->iv1 * S_v * S_v) * sizeof(float); |
| 1638 | |
| 1639 | head->state_out_dma = dst_cache ? |
| 1640 | (dst_cache->data + ((uint64_t) head->iv3 * H + head->iv1) * S_v * S_v * sizeof(float)) : |
| 1641 | (dst->data + ((uint64_t) S_v * H * n_tokens * kparams->n_seqs + (uint64_t) (head->iv3 * H + head->iv1) * S_v * S_v) * sizeof(float)); |
| 1642 | } |
| 1643 | |
| 1644 | struct htp_gdn_batch_context { |
| 1645 | struct htp_gdn_head_ptrs * heads; |
| 1646 | const float * vtcm_g_raw; |
| 1647 | const float * vtcm_b_raw; |
| 1648 | uint32_t curr_buf; |
| 1649 | uint32_t c; |
| 1650 | uint32_t n_batch; |
| 1651 | uint32_t S_v; |
| 1652 | float scale; |
| 1653 | struct htp_ops_context * octx; |
| 1654 | const struct htp_gdn_kernel_params * kparams; |
| 1655 | }; |
| 1656 | |
| 1657 | static void gdn_hvx_init_state_worker(unsigned int n, unsigned int i, void * data) { |
| 1658 | (void) n; |
| 1659 | struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data; |
| 1660 | struct htp_thread_trace * tr = &bctx->octx->ctx->trace[i]; |
| 1661 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_COMP, 0); |
| 1662 | |
| 1663 | struct htp_gdn_head_ptrs * head = &bctx->heads[i]; |
| 1664 | gdn_pack_s_col_tiles(head->s_col_tiles, head->s_f16, head->s_state, bctx->S_v); |
| 1665 | |
| 1666 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_COMP, 0); |
| 1667 | } |
| 1668 | |
| 1669 | static inline __attribute__((unused)) HVX_Vector hvx_clamp_neg20_0(HVX_Vector v, HVX_Vector v_zero, HVX_Vector v_neg20) { |
| 1670 | HVX_VectorPred p_gt = Q6_Q_vcmp_gt_VsfVsf(v, v_zero); |
| 1671 | v = Q6_V_vmux_QVV(p_gt, v_zero, v); |
| 1672 | HVX_VectorPred p_lt = Q6_Q_vcmp_gt_VsfVsf(v_neg20, v); |
| 1673 | return Q6_V_vmux_QVV(p_lt, v_neg20, v); |
| 1674 | } |
| 1675 | |
| 1676 | static inline HVX_Vector hvx_prefix_scan_f32(HVX_Vector v, HVX_Vector carry_in) { |
| 1677 | const HVX_Vector zero = Q6_V_vzero(); |
| 1678 | |
| 1679 | v = hvx_vec_add_f32_f32(v, Q6_V_vlalign_VVR(v, zero, 4)); |
| 1680 | v = hvx_vec_add_f32_f32(v, Q6_V_vlalign_VVR(v, zero, 8)); |
| 1681 | v = hvx_vec_add_f32_f32(v, Q6_V_vlalign_VVR(v, zero, 16)); |
| 1682 | v = hvx_vec_add_f32_f32(v, Q6_V_vlalign_VVR(v, zero, 32)); |
| 1683 | v = hvx_vec_add_f32_f32(v, Q6_V_vlalign_VVR(v, zero, 64)); |
| 1684 | v = hvx_vec_add_f32_f32(v, carry_in); |
| 1685 | |
| 1686 | return v; |
| 1687 | } |
| 1688 | |
| 1689 | static inline HVX_Vector hvx_splat_last_f32(HVX_Vector v) { |
| 1690 | return hvx_vec_repl4(Q6_V_vror_VR(v, 124)); |
| 1691 | } |
| 1692 | |
| 1693 | static void gdn_hvx_phase1a_worker(unsigned int n, unsigned int i, void * data) { |
| 1694 | (void) n; |
| 1695 | struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data; |
| 1696 | struct htp_thread_trace * tr = &bctx->octx->ctx->trace[i]; |
| 1697 | const uint16_t info = (uint16_t) bctx->c; |
| 1698 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, info); |
| 1699 | |
| 1700 | struct htp_gdn_head_ptrs * head = &bctx->heads[i]; |
| 1701 | const uint32_t curr_buf = bctx->curr_buf; |
| 1702 | const uint32_t S_v = bctx->S_v; |
| 1703 | const uint32_t n_batch = bctx->n_batch; |
| 1704 | |
| 1705 | if (n_batch == 1) { |
| 1706 | hvx_vmem(head->g_f32[curr_buf] + 0) = hvx_vmem(bctx->vtcm_g_raw + 0); |
| 1707 | hvx_vmem(head->g_f32[curr_buf] + 32) = hvx_vmem(bctx->vtcm_g_raw + 32); |
| 1708 | hvx_vmem(head->b_f32[curr_buf] + 0) = hvx_vmem(bctx->vtcm_b_raw + 0); |
| 1709 | hvx_vmem(head->b_f32[curr_buf] + 32) = hvx_vmem(bctx->vtcm_b_raw + 32); |
| 1710 | } else { |
| 1711 | const uint32_t vtcm_stride = (n_batch + 1) * sizeof(float); |
| 1712 | const HVX_Vector vv = hvx_vec_gather_offsets_w(vtcm_stride); |
| 1713 | const size_t rt_g = (size_t) ((const uint8_t *) bctx->vtcm_g_raw + i * sizeof(float)); |
| 1714 | const size_t rt_b = (size_t) ((const uint8_t *) bctx->vtcm_b_raw + i * sizeof(float)); |
| 1715 | const size_t mu = 64 * vtcm_stride; |
| 1716 | |
| 1717 | Q6_vgather_ARMVw((HVX_Vector *) (head->g_f32[curr_buf] + 0), rt_g, mu, vv); |
| 1718 | Q6_vgather_ARMVw((HVX_Vector *) (head->g_f32[curr_buf] + 32), rt_g + 32 * vtcm_stride, mu, vv); |
| 1719 | Q6_vgather_ARMVw((HVX_Vector *) (head->b_f32[curr_buf] + 0), rt_b, mu, vv); |
| 1720 | Q6_vgather_ARMVw((HVX_Vector *) (head->b_f32[curr_buf] + 32), rt_b + 32 * vtcm_stride, mu, vv); |
| 1721 | } |
| 1722 | |
| 1723 | const uint32_t t_chunk = bctx->c * 64; |
| 1724 | const uint32_t valid_tokens = hex_smin(64, bctx->kparams->n_tokens - t_chunk); |
| 1725 | if (valid_tokens < 64) { |
| 1726 | for (uint32_t t = valid_tokens; t < 64; ++t) { |
| 1727 | head->g_f32[curr_buf][t] = 0.0f; |
| 1728 | head->b_f32[curr_buf][t] = 0.0f; |
| 1729 | } |
| 1730 | const HVX_Vector vzero = Q6_V_vzero(); |
| 1731 | for (uint32_t t = valid_tokens; t < 64; ++t) { |
| 1732 | for (uint32_t j = 0; j < S_v; j += 32) { |
| 1733 | hvx_vmem(head->q_f32[curr_buf] + t * S_v + j) = vzero; |
| 1734 | hvx_vmem(head->k_f32[curr_buf] + t * S_v + j) = vzero; |
| 1735 | hvx_vmem(head->v_f32[curr_buf] + t * S_v + j) = vzero; |
| 1736 | } |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | const HVX_Vector v_g0 = hvx_vmem(head->g_f32[curr_buf] + 0); |
| 1741 | const HVX_Vector v_g1 = hvx_vmem(head->g_f32[curr_buf] + 32); |
| 1742 | |
| 1743 | HVX_Vector v_gamma0 = hvx_prefix_scan_f32(v_g0, Q6_V_vzero()); |
| 1744 | HVX_Vector v_carry = hvx_splat_last_f32(v_gamma0); |
| 1745 | HVX_Vector v_gamma1 = hvx_prefix_scan_f32(v_g1, v_carry); |
| 1746 | |
| 1747 | const HVX_Vector v_zero = Q6_V_vzero(); |
| 1748 | const HVX_Vector v_neg20 = hvx_vec_splat_f32(-20.0f); |
| 1749 | |
| 1750 | hvx_vmem(head->gamma) = hvx_vec_f32_to_f16(v_gamma0, v_gamma1); |
| 1751 | |
| 1752 | HVX_Vector v_l0 = hvx_vec_exp_f32(hvx_clamp_neg20_0(v_gamma0, v_zero, v_neg20)); |
| 1753 | HVX_Vector v_l1 = hvx_vec_exp_f32(hvx_clamp_neg20_0(v_gamma1, v_zero, v_neg20)); |
| 1754 | |
| 1755 | hvx_vmem(head->lambda_init + 0) = v_l0; |
| 1756 | hvx_vmem(head->lambda_init + 32) = v_l1; |
| 1757 | hvx_vmem(head->lambda_init_f16) = hvx_vec_f32_to_f16(v_l0, v_l1); |
| 1758 | |
| 1759 | gdn_f32_to_hmx_row_tiles_and_f16(head->k_row_tiles, head->k_prime_row_tiles, head->k_f16, |
| 1760 | head->k_f32[curr_buf], head->lambda_init_f16, 64, S_v); |
| 1761 | hmx_interleave_rows_to_tiles(head->k_col_tiles, head->k_f16, 64, S_v, S_v, 0, 64); |
| 1762 | |
| 1763 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, info); |
| 1764 | } |
| 1765 | |
| 1766 | static void gdn_hvx_phase1b_worker(unsigned int n, unsigned int i, void * data) { |
| 1767 | (void) n; |
| 1768 | struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data; |
| 1769 | struct htp_thread_trace * tr = &bctx->octx->ctx->trace[i]; |
| 1770 | const uint16_t info = (uint16_t) bctx->c; |
| 1771 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_PREP, info); |
| 1772 | |
| 1773 | struct htp_gdn_head_ptrs * head = &bctx->heads[i]; |
| 1774 | const uint32_t curr_buf = bctx->curr_buf; |
| 1775 | const uint32_t S_v = bctx->S_v; |
| 1776 | |
| 1777 | gdn_f32_to_hmx_row_tiles_and_f16(head->q_row_tiles, head->q_prime_row_tiles, NULL, |
| 1778 | head->q_f32[curr_buf], head->lambda_init_f16, 64, S_v); |
| 1779 | |
| 1780 | hmx_interleave_cols_to_tiles(head->k_col_tiles_64x128, head->k_f16, 64, S_v, S_v, 2, 0, 64); |
| 1781 | |
| 1782 | const uint16_t * gamma_u16 = (const uint16_t *) head->gamma; |
| 1783 | const HVX_Vector v_gamma = hvx_vmem(head->gamma); |
| 1784 | |
| 1785 | const HVX_Vector v_zero_f16 = Q6_V_vzero(); |
| 1786 | const HVX_Vector v_neg20_f16 = hvx_vec_splat_f16(-20.0f); |
| 1787 | const HVX_Vector v_log2e_f16 = hvx_vec_splat_f16(1.4426950408889634f); |
| 1788 | const HVX_Vector v_one_f16 = hvx_vec_splat_f16(1.0f); |
| 1789 | |
| 1790 | hvx_vmem(head->decay_m + 0) = Q6_V_vzero(); |
| 1791 | hvx_vmem(head->decay_a + 0) = Q6_V_vand_QV(Q6_Q_vsetq2_R(2), v_one_f16); |
| 1792 | |
| 1793 | for (uint32_t t = 1; t < 63; t += 2) { |
| 1794 | uint32_t t0 = t; |
| 1795 | uint32_t t1 = t + 1; |
| 1796 | |
| 1797 | HVX_Vector v_gamma_t0 = Q6_Vh_vsplat_R(gamma_u16[t0]); |
| 1798 | HVX_Vector v_gamma_t1 = Q6_Vh_vsplat_R(gamma_u16[t1]); |
| 1799 | |
| 1800 | HVX_Vector diff0 = hvx_vec_sub_f16_f16(v_gamma_t0, v_gamma); |
| 1801 | HVX_Vector diff1 = hvx_vec_sub_f16_f16(v_gamma_t1, v_gamma); |
| 1802 | |
| 1803 | HVX_VectorPred p_gt0 = Q6_Q_vcmp_gt_VhfVhf(diff0, v_zero_f16); |
| 1804 | HVX_VectorPred p_gt1 = Q6_Q_vcmp_gt_VhfVhf(diff1, v_zero_f16); |
| 1805 | |
| 1806 | diff0 = Q6_V_vmux_QVV(p_gt0, v_zero_f16, diff0); |
| 1807 | diff1 = Q6_V_vmux_QVV(p_gt1, v_zero_f16, diff1); |
| 1808 | |
| 1809 | diff0 = Q6_Vhf_vmax_VhfVhf(v_neg20_f16, diff0); |
| 1810 | diff1 = Q6_Vhf_vmax_VhfVhf(v_neg20_f16, diff1); |
| 1811 | |
| 1812 | HVX_Vector diff_log2e0 = hvx_vec_mul_f16_f16(diff0, v_log2e_f16); |
| 1813 | HVX_Vector diff_log2e1 = hvx_vec_mul_f16_f16(diff1, v_log2e_f16); |
| 1814 | |
| 1815 | HVX_Vector v_exp0 = hvx_vec_exp2_f16(diff_log2e0); |
| 1816 | HVX_Vector v_exp1 = hvx_vec_exp2_f16(diff_log2e1); |
| 1817 | |
| 1818 | HVX_VectorPred mask_lt0 = Q6_Q_vsetq2_R(2 * t0); |
| 1819 | HVX_VectorPred mask_lt1 = Q6_Q_vsetq2_R(2 * t1); |
| 1820 | |
| 1821 | HVX_Vector v_m0 = Q6_V_vand_QV(mask_lt0, v_exp0); |
| 1822 | HVX_Vector v_m1 = Q6_V_vand_QV(mask_lt1, v_exp1); |
| 1823 | |
| 1824 | HVX_VectorPred mask_le0 = Q6_Q_vsetq2_R(2 * (t0 + 1)); |
| 1825 | HVX_VectorPred mask_le1 = Q6_Q_vsetq2_R(2 * (t1 + 1)); |
| 1826 | |
| 1827 | HVX_VectorPred mask_diag0 = Q6_Q_and_QQn(mask_le0, mask_lt0); |
| 1828 | HVX_VectorPred mask_diag1 = Q6_Q_and_QQn(mask_le1, mask_lt1); |
| 1829 | |
| 1830 | HVX_Vector v_a0 = Q6_V_vmux_QVV(mask_diag0, v_one_f16, v_m0); |
| 1831 | HVX_Vector v_a1 = Q6_V_vmux_QVV(mask_diag1, v_one_f16, v_m1); |
| 1832 | |
| 1833 | hvx_vmem(head->decay_m + t0 * 64) = v_m0; |
| 1834 | hvx_vmem(head->decay_a + t0 * 64) = v_a0; |
| 1835 | hvx_vmem(head->decay_m + t1 * 64) = v_m1; |
| 1836 | hvx_vmem(head->decay_a + t1 * 64) = v_a1; |
| 1837 | } |
| 1838 | |
| 1839 | { |
| 1840 | HVX_Vector v_gamma_t = Q6_Vh_vsplat_R(gamma_u16[63]); |
| 1841 | HVX_Vector diff = hvx_vec_sub_f16_f16(v_gamma_t, v_gamma); |
| 1842 | HVX_VectorPred p_gt = Q6_Q_vcmp_gt_VhfVhf(diff, v_zero_f16); |
| 1843 | diff = Q6_V_vmux_QVV(p_gt, v_zero_f16, diff); |
| 1844 | diff = Q6_Vhf_vmax_VhfVhf(v_neg20_f16, diff); |
| 1845 | |
| 1846 | HVX_Vector diff_log2e = hvx_vec_mul_f16_f16(diff, v_log2e_f16); |
| 1847 | HVX_Vector v_exp = hvx_vec_exp2_f16(diff_log2e); |
| 1848 | |
| 1849 | HVX_VectorPred mask_lt_t = Q6_Q_vsetq2_R(2 * 63); |
| 1850 | HVX_Vector v_m = Q6_V_vand_QV(mask_lt_t, v_exp); |
| 1851 | |
| 1852 | HVX_VectorPred mask_le_t = Q6_Q_vcmp_eq_VhVh(v_zero_f16, v_zero_f16); |
| 1853 | HVX_VectorPred mask_diag = Q6_Q_and_QQn(mask_le_t, mask_lt_t); |
| 1854 | HVX_Vector v_a = Q6_V_vmux_QVV(mask_diag, v_one_f16, v_m); |
| 1855 | |
| 1856 | hvx_vmem(head->decay_m + 63 * 64) = v_m; |
| 1857 | hvx_vmem(head->decay_a + 63 * 64) = v_a; |
| 1858 | } |
| 1859 | |
| 1860 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_PREP, info); |
| 1861 | } |
| 1862 | |
| 1863 | static void gdn_hvx_phase2_worker(unsigned int n, unsigned int i, void * data) { |
| 1864 | (void) n; |
| 1865 | struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data; |
| 1866 | struct htp_thread_trace * tr = &bctx->octx->ctx->trace[i]; |
| 1867 | const uint16_t info = (uint16_t) bctx->c; |
| 1868 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_SOLVE, info); |
| 1869 | |
| 1870 | struct htp_gdn_head_ptrs * head = &bctx->heads[i]; |
| 1871 | const uint32_t curr_buf = bctx->curr_buf; |
| 1872 | |
| 1873 | gdn_unpack_64x64_tiles_to_vectors(head->rows_kk, head->kk_tiles); |
| 1874 | |
| 1875 | gdn_build_inv_l_blocks( |
| 1876 | head->inv_row_tiles, |
| 1877 | head->rows_kk, |
| 1878 | head->decay_m, |
| 1879 | head->b_f32[curr_buf], |
| 1880 | (__fp16 *) head->vtcm_m, |
| 1881 | (__fp16 *) head->vtcm_m + HMX_FP16_TILE_N_ELMS |
| 1882 | ); |
| 1883 | |
| 1884 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_SOLVE, info); |
| 1885 | } |
| 1886 | |
| 1887 | static void gdn_hvx_phase3_worker(unsigned int n, unsigned int i, void * data) { |
| 1888 | (void) n; |
| 1889 | struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data; |
| 1890 | struct htp_thread_trace * tr = &bctx->octx->ctx->trace[i]; |
| 1891 | const uint16_t info = (uint16_t) bctx->c; |
| 1892 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_V_PREP, info); |
| 1893 | |
| 1894 | struct htp_gdn_head_ptrs * head = &bctx->heads[i]; |
| 1895 | const uint32_t curr_buf = bctx->curr_buf; |
| 1896 | const uint32_t S_v = bctx->S_v; |
| 1897 | |
| 1898 | gdn_unpack_64xS_tiles_to_f32(head->v_inter_f32, head->v_inter_tiles, S_v); |
| 1899 | |
| 1900 | HVX_VectorAlias local_b[2]; |
| 1901 | local_b[0].v = hvx_vmem(head->b_f32[curr_buf] + 0); |
| 1902 | local_b[1].v = hvx_vmem(head->b_f32[curr_buf] + 32); |
| 1903 | |
| 1904 | for (uint32_t t = 0; t < 64; ++t) { |
| 1905 | HVX_Vector vb = hvx_vec_splat_f32(local_b[t / 32].fp32[t % 32]); |
| 1906 | for (uint32_t j = 0; j < S_v; j += 64) { |
| 1907 | HVX_Vector vv0 = hvx_vmem(head->v_f32[curr_buf] + t * S_v + j + 0); |
| 1908 | HVX_Vector vv1 = (j + 32 < S_v) ? hvx_vmem(head->v_f32[curr_buf] + t * S_v + j + 32) : Q6_V_vzero(); |
| 1909 | HVX_Vector vi0 = hvx_vmem(head->v_inter_f32 + t * S_v + j + 0); |
| 1910 | HVX_Vector vi1 = (j + 32 < S_v) ? hvx_vmem(head->v_inter_f32 + t * S_v + j + 32) : Q6_V_vzero(); |
| 1911 | |
| 1912 | HVX_Vector vp0 = hvx_vec_mul_f32_f32(hvx_vec_sub_f32_f32(vv0, vi0), vb); |
| 1913 | HVX_Vector vp1 = hvx_vec_mul_f32_f32(hvx_vec_sub_f32_f32(vv1, vi1), vb); |
| 1914 | |
| 1915 | hvx_vmem(head->v_prime_f16 + t * S_v + j) = hvx_vec_f32_to_f16(vp0, vp1); |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | hmx_interleave_cols_to_tiles(head->v_prime_col_tiles, head->v_prime_f16, 64, S_v, S_v, 2, 0, 64); |
| 1920 | |
| 1921 | gdn_unpack_64x64_tiles_to_vectors(head->rows_qk, head->qk_tiles); |
| 1922 | for (uint32_t t = 0; t < 64; ++t) { |
| 1923 | HVX_Vector v_decay_a = hvx_vmem(head->decay_a + t * 64); |
| 1924 | head->rows_a[t] = hvx_vec_mul_f16_f16(head->rows_qk[t], v_decay_a); |
| 1925 | } |
| 1926 | gdn_pack_64x64_vectors_to_tiles(head->a_row_tiles, head->rows_a); |
| 1927 | |
| 1928 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_V_PREP, info); |
| 1929 | } |
| 1930 | |
| 1931 | static void gdn_hvx_phase4_worker(unsigned int n, unsigned int i, void * data) { |
| 1932 | (void) n; |
| 1933 | struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data; |
| 1934 | struct htp_thread_trace * tr = &bctx->octx->ctx->trace[i]; |
| 1935 | const uint16_t info = (uint16_t) bctx->c; |
| 1936 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info); |
| 1937 | |
| 1938 | struct htp_gdn_head_ptrs * head = &bctx->heads[i]; |
| 1939 | const uint32_t S_v = bctx->S_v; |
| 1940 | |
| 1941 | gdn_unpack_64xS_tiles_to_f16(head->delta_f16, head->delta_tiles, S_v); |
| 1942 | hmx_interleave_cols_to_tiles(head->delta_col_tiles, head->delta_f16, 64, S_v, S_v, 2, 0, 64); |
| 1943 | |
| 1944 | const uint16_t * decay_last = (const uint16_t *) (head->decay_a + 63 * 64); |
| 1945 | const HVX_Vector vzero = Q6_V_vzero(); |
| 1946 | |
| 1947 | for (uint32_t s = 0; s < 64; ++s) { |
| 1948 | HVX_Vector vs = Q6_Vh_vsplat_R(decay_last[s]); |
| 1949 | HVX_VectorPred p_zero = Q6_Q_vcmp_eq_VhVh(vs, vzero); |
| 1950 | for (uint32_t j = 0; j < S_v; j += 64) { |
| 1951 | HVX_Vector vd = hvx_vmem(head->delta_f16 + s * S_v + j); |
| 1952 | HVX_Vector prod = hvx_vec_mul_f16_f16(vd, vs); |
| 1953 | hvx_vmem(head->d_f16 + s * S_v + j) = Q6_V_vmux_QVV(p_zero, vzero, prod); |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | gdn_pack_d_t_row_tiles(head->d_row_tiles, head->d_f16, S_v, head->vtcm_m, head->vtcm_tmp); |
| 1958 | |
| 1959 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_D_PREP, info); |
| 1960 | } |
| 1961 | |
| 1962 | static void gdn_hvx_phase5_worker(unsigned int n, unsigned int i, void * data) { |
| 1963 | (void) n; |
| 1964 | struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data; |
| 1965 | struct htp_thread_trace * tr = &bctx->octx->ctx->trace[i]; |
| 1966 | const uint16_t info = (uint16_t) bctx->c; |
| 1967 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_OUT, info); |
| 1968 | |
| 1969 | struct htp_gdn_head_ptrs * head = &bctx->heads[i]; |
| 1970 | const uint32_t curr_buf = bctx->curr_buf; |
| 1971 | const uint32_t S_v = bctx->S_v; |
| 1972 | const float scale = bctx->scale; |
| 1973 | |
| 1974 | gdn_unpack_64xS_tiles_to_f32(head->o_inter_f32, head->o_inter_tiles, S_v); |
| 1975 | gdn_unpack_64xS_tiles_to_f32(head->o_intra_f32, head->o_intra_tiles, S_v); |
| 1976 | |
| 1977 | HVX_Vector vscale = hvx_vec_splat_f32(scale); |
| 1978 | for (uint32_t j = 0; j < 64 * S_v / 32; ++j) { |
| 1979 | HVX_Vector vi = hvx_vmem(head->o_inter_f32 + j * 32); |
| 1980 | HVX_Vector va = hvx_vmem(head->o_intra_f32 + j * 32); |
| 1981 | hvx_vmem(head->o_f32[curr_buf] + j * 32) = hvx_vec_mul_f32_f32(hvx_vec_add_f32_f32(vi, va), vscale); |
| 1982 | } |
| 1983 | |
| 1984 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_OUT, info); |
| 1985 | } |
| 1986 | |
| 1987 | static void gdn_hvx_phase6_worker(unsigned int n, unsigned int i, void * data) { |
| 1988 | (void) n; |
| 1989 | struct htp_gdn_batch_context * bctx = (struct htp_gdn_batch_context *) data; |
| 1990 | struct htp_thread_trace * tr = &bctx->octx->ctx->trace[i]; |
| 1991 | const uint16_t info = (uint16_t) bctx->c; |
| 1992 | htp_trace_event_start(tr, HTP_TRACE_EVT_HVX_GDN_STATE, info); |
| 1993 | |
| 1994 | struct htp_gdn_head_ptrs * head = &bctx->heads[i]; |
| 1995 | const uint32_t S_v = bctx->S_v; |
| 1996 | const uint32_t c = bctx->c; |
| 1997 | const uint32_t n_chunks = bctx->kparams->n_chunks; |
| 1998 | |
| 1999 | gdn_unpack_SxS_tiles_to_f32(head->s_update_f32, head->s_update_tiles, S_v); |
| 2000 | |
| 2001 | HVX_VectorAlias last_lambda; |
| 2002 | last_lambda.v = hvx_vmem(head->lambda_init + 32); |
| 2003 | HVX_Vector v_l_final = hvx_vec_splat_f32(last_lambda.fp32[31]); |
| 2004 | |
| 2005 | for (uint32_t j = 0; j < S_v * S_v / 32; ++j) { |
| 2006 | HVX_Vector vs_old = hvx_vmem(head->s_state + j * 32); |
| 2007 | HVX_Vector vsu = hvx_vmem(head->s_update_f32 + j * 32); |
| 2008 | hvx_vmem(head->s_state + j * 32) = hvx_vec_add_f32_f32(hvx_vec_mul_f32_f32(vs_old, v_l_final), vsu); |
| 2009 | } |
| 2010 | |
| 2011 | if (c + 1 < n_chunks) { |
| 2012 | gdn_pack_s_col_tiles(head->s_col_tiles, head->s_f16, head->s_state, S_v); |
| 2013 | } |
| 2014 | |
| 2015 | htp_trace_event_stop(tr, HTP_TRACE_EVT_HVX_GDN_STATE, info); |
| 2016 | } |
| 2017 | |
| 2018 | |
| 2019 | static int gated_delta_net_f32_hmx_chunked( |
| 2020 | struct htp_ops_context * octx, |
| 2021 | const struct htp_gdn_kernel_params * kparams, |
| 2022 | uint32_t row_start, |
| 2023 | uint32_t nrows |
| 2024 | ) { |
| 2025 | const struct htp_tensor * q = octx->src[0]; |
| 2026 | const struct htp_tensor * k = octx->src[1]; |
| 2027 | const struct htp_tensor * v = octx->src[2]; |
| 2028 | const struct htp_tensor * g = octx->src[3]; |
| 2029 | const struct htp_tensor * beta = octx->src[4]; |
| 2030 | const struct htp_tensor * state = octx->src[5]; |
| 2031 | const struct htp_tensor * dst = octx->dst; |
| 2032 | const struct htp_tensor * dst_cache = octx->dsts[1]; |
| 2033 | |
| 2034 | const uint32_t S_v = kparams->S_v; |
| 2035 | const uint32_t H = kparams->H; |
| 2036 | const uint32_t n_tokens = kparams->n_tokens; |
| 2037 | const float scale = kparams->scale; |
| 2038 | const uint32_t chunk_size = kparams->chunk_size; |
| 2039 | const uint32_t n_chunks = kparams->n_chunks; |
| 2040 | const uint32_t n_sv_tiles = S_v / 32; |
| 2041 | |
| 2042 | struct htp_gdn_hmx_vtcm_layout L; |
| 2043 | htp_gdn_hmx_vtcm_layout_build(&L, S_v, chunk_size, kparams->n_heads_batch, kparams->n_threads, kparams->pipeline != 0); |
| 2044 | |
| 2045 | if (L.total_bytes > octx->ctx->vtcm_size) { |
| 2046 | return HTP_STATUS_VTCM_TOO_SMALL; |
| 2047 | } |
| 2048 | |
| 2049 | uint8_t * const vtcm_base = (uint8_t *) octx->ctx->vtcm_base; |
| 2050 | |
| 2051 | float * vtcm_g_raw[2] = { |
| 2052 | VTCM_LAYOUT_PTR(float, vtcm_base, L.off_g_raw[0]), |
| 2053 | L.pipeline ? VTCM_LAYOUT_PTR(float, vtcm_base, L.off_g_raw[1]) : VTCM_LAYOUT_PTR(float, vtcm_base, L.off_g_raw[0]) |
| 2054 | }; |
| 2055 | float * vtcm_b_raw[2] = { |
| 2056 | VTCM_LAYOUT_PTR(float, vtcm_base, L.off_b_raw[0]), |
| 2057 | L.pipeline ? VTCM_LAYOUT_PTR(float, vtcm_base, L.off_b_raw[1]) : VTCM_LAYOUT_PTR(float, vtcm_base, L.off_b_raw[0]) |
| 2058 | }; |
| 2059 | |
| 2060 | uint8_t * vtcm_scales_1 = VTCM_LAYOUT_PTR(uint8_t, vtcm_base, L.off_scales_1); |
| 2061 | hmx_init_column_scales(vtcm_scales_1, Q6_V_vsplat_R(0x3c00)); |
| 2062 | |
| 2063 | hmx_queue_t hmx_q = octx->ctx->hmx_queue; |
| 2064 | dma_queue * dma_q = octx->ctx->dma[0]; |
| 2065 | work_queue_t wp = octx->ctx->work_queue; |
| 2066 | |
| 2067 | struct htp_gdn_head_ptrs heads[8]; |
| 2068 | struct htp_gdn_hmx_gemm_task gemm_tasks[8][9]; |
| 2069 | |
| 2070 | uint32_t n_batch = 1; |
| 2071 | for (uint32_t r = row_start; r < row_start + nrows; r += n_batch) { |
| 2072 | const uint32_t head_in_seq = fastmodulo(r, H, &kparams->div_H); |
| 2073 | const uint32_t iv3 = fastdiv(r, &kparams->div_H); |
| 2074 | const uint32_t heads_left_in_seq = H - head_in_seq; |
| 2075 | const uint32_t heads_left_in_range = (row_start + nrows) - r; |
| 2076 | n_batch = hex_smin((uint32_t) kparams->n_heads_batch, hex_smin(heads_left_in_seq, heads_left_in_range)); |
| 2077 | |
| 2078 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2079 | gdn_init_head_ptrs(&heads[h], &L, vtcm_base, h, head_in_seq, iv3, |
| 2080 | q, k, v, state, dst, dst_cache, kparams, S_v, H, n_tokens, chunk_size); |
| 2081 | } |
| 2082 | |
| 2083 | struct htp_gdn_batch_context bctx; |
| 2084 | bctx.heads = heads; |
| 2085 | bctx.vtcm_g_raw = NULL; |
| 2086 | bctx.vtcm_b_raw = NULL; |
| 2087 | bctx.curr_buf = 0; |
| 2088 | bctx.c = 0; |
| 2089 | bctx.n_batch = n_batch; |
| 2090 | bctx.S_v = S_v; |
| 2091 | bctx.scale = scale; |
| 2092 | bctx.octx = octx; |
| 2093 | bctx.kparams = kparams; |
| 2094 | |
| 2095 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2096 | dma_queue_push(dma_q, dma_make_data(heads[h].s_state, heads[h].state_in_dma), |
| 2097 | S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v); |
| 2098 | } |
| 2099 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2100 | dma_queue_pop(dma_q); |
| 2101 | } |
| 2102 | |
| 2103 | if (n_chunks > 0) { |
| 2104 | work_queue_run(wp, gdn_hvx_init_state_worker, &bctx, n_batch); |
| 2105 | |
| 2106 | const uint32_t chunk0_tokens = hex_smin(chunk_size, n_tokens); |
| 2107 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2108 | gdn_dma_push_chunk_inputs(dma_q, heads[h].q_f32[0], heads[h].k_f32[0], heads[h].v_f32[0], |
| 2109 | q, k, v, heads[h].iq3, heads[h].iq1, heads[h].ik3, heads[h].ik1, |
| 2110 | heads[h].iv3, heads[h].iv1, 0, chunk0_tokens, S_v); |
| 2111 | } |
| 2112 | gdn_dma_push_chunk_gb(dma_q, vtcm_g_raw[0], vtcm_b_raw[0], g, beta, iv3, head_in_seq, 0, chunk0_tokens, n_batch); |
| 2113 | } |
| 2114 | |
| 2115 | for (uint32_t c = 0; c < n_chunks; ++c) { |
| 2116 | const uint32_t curr_buf = c & 1; |
| 2117 | const uint32_t next_buf = (c + 1) & 1; |
| 2118 | const uint32_t t_chunk = c * chunk_size; |
| 2119 | |
| 2120 | bctx.curr_buf = curr_buf; |
| 2121 | bctx.c = c; |
| 2122 | bctx.vtcm_g_raw = vtcm_g_raw[curr_buf]; |
| 2123 | bctx.vtcm_b_raw = vtcm_b_raw[curr_buf]; |
| 2124 | |
| 2125 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2126 | dma_queue_pop(dma_q); |
| 2127 | dma_queue_pop(dma_q); |
| 2128 | dma_queue_pop(dma_q); |
| 2129 | } |
| 2130 | dma_queue_pop(dma_q); |
| 2131 | dma_queue_pop(dma_q); |
| 2132 | |
| 2133 | if (c + 1 < n_chunks) { |
| 2134 | const uint32_t next_t_chunk = (c + 1) * chunk_size; |
| 2135 | const uint32_t next_tokens = hex_smin(chunk_size, n_tokens - next_t_chunk); |
| 2136 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2137 | gdn_dma_push_chunk_inputs(dma_q, heads[h].q_f32[next_buf], heads[h].k_f32[next_buf], heads[h].v_f32[next_buf], |
| 2138 | q, k, v, heads[h].iq3, heads[h].iq1, heads[h].ik3, heads[h].ik1, |
| 2139 | heads[h].iv3, heads[h].iv1, next_t_chunk, next_tokens, S_v); |
| 2140 | } |
| 2141 | gdn_dma_push_chunk_gb(dma_q, vtcm_g_raw[next_buf], vtcm_b_raw[next_buf], |
| 2142 | g, beta, iv3, head_in_seq, next_t_chunk, next_tokens, n_batch); |
| 2143 | } |
| 2144 | |
| 2145 | if (c > 0) { |
| 2146 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2147 | dma_queue_pop(dma_q); |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | work_queue_run(wp, gdn_hvx_phase1a_worker, &bctx, n_batch); |
| 2152 | |
| 2153 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2154 | htp_gdn_push_hmx_gemm_task(hmx_q, &gemm_tasks[h][0], heads[h].k_row_tiles, heads[h].k_col_tiles, heads[h].kk_tiles, 2, 2, n_sv_tiles, vtcm_scales_1); |
| 2155 | } |
| 2156 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2157 | htp_gdn_push_hmx_gemm_task(hmx_q, &gemm_tasks[h][2], heads[h].k_prime_row_tiles, heads[h].s_col_tiles, heads[h].v_inter_tiles, 2, n_sv_tiles, n_sv_tiles, vtcm_scales_1); |
| 2158 | } |
| 2159 | |
| 2160 | work_queue_run(wp, gdn_hvx_phase1b_worker, &bctx, n_batch); |
| 2161 | |
| 2162 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2163 | hmx_queue_pop(hmx_q); |
| 2164 | } |
| 2165 | |
| 2166 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2167 | htp_gdn_push_hmx_gemm_task(hmx_q, &gemm_tasks[h][1], heads[h].q_row_tiles, heads[h].k_col_tiles, heads[h].qk_tiles, 2, 2, n_sv_tiles, vtcm_scales_1); |
| 2168 | } |
| 2169 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2170 | htp_gdn_push_hmx_gemm_task(hmx_q, &gemm_tasks[h][3], heads[h].q_prime_row_tiles, heads[h].s_col_tiles, heads[h].o_inter_tiles, 2, n_sv_tiles, n_sv_tiles, vtcm_scales_1); |
| 2171 | } |
| 2172 | |
| 2173 | work_queue_run(wp, gdn_hvx_phase2_worker, &bctx, n_batch); |
| 2174 | |
| 2175 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2176 | hmx_queue_pop(hmx_q); |
| 2177 | } |
| 2178 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2179 | hmx_queue_pop(hmx_q); |
| 2180 | } |
| 2181 | |
| 2182 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2183 | htp_gdn_push_hmx_gemm_task( |
| 2184 | hmx_q, &gemm_tasks[h][7], |
| 2185 | (__fp16 *) heads[h].vtcm_m, |
| 2186 | heads[h].inv_row_tiles + 0 * HMX_FP16_TILE_N_ELMS, |
| 2187 | (__fp16 *) heads[h].vtcm_tmp, |
| 2188 | 1, 1, 1, vtcm_scales_1 |
| 2189 | ); |
| 2190 | } |
| 2191 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2192 | htp_gdn_push_hmx_gemm_task( |
| 2193 | hmx_q, &gemm_tasks[h][8], |
| 2194 | (__fp16 *) heads[h].vtcm_m + HMX_FP16_TILE_N_ELMS, |
| 2195 | (__fp16 *) heads[h].vtcm_tmp, |
| 2196 | heads[h].inv_row_tiles + 2 * HMX_FP16_TILE_N_ELMS, |
| 2197 | 1, 1, 1, vtcm_scales_1 |
| 2198 | ); |
| 2199 | } |
| 2200 | |
| 2201 | work_queue_run(wp, gdn_hvx_phase3_worker, &bctx, n_batch); |
| 2202 | |
| 2203 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2204 | hmx_queue_pop(hmx_q); |
| 2205 | } |
| 2206 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2207 | hmx_queue_pop(hmx_q); |
| 2208 | } |
| 2209 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2210 | hmx_queue_pop(hmx_q); |
| 2211 | } |
| 2212 | |
| 2213 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2214 | htp_gdn_push_hmx_gemm_task(hmx_q, &gemm_tasks[h][4], heads[h].inv_row_tiles, heads[h].v_prime_col_tiles, heads[h].delta_tiles, 2, n_sv_tiles, 2, vtcm_scales_1); |
| 2215 | } |
| 2216 | |
| 2217 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2218 | hmx_queue_pop(hmx_q); |
| 2219 | } |
| 2220 | |
| 2221 | work_queue_run(wp, gdn_hvx_phase4_worker, &bctx, n_batch); |
| 2222 | |
| 2223 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2224 | htp_gdn_push_hmx_gemm_task(hmx_q, &gemm_tasks[h][5], heads[h].a_row_tiles, heads[h].delta_col_tiles, heads[h].o_intra_tiles, 2, n_sv_tiles, 2, vtcm_scales_1); |
| 2225 | } |
| 2226 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2227 | htp_gdn_push_hmx_gemm_task(hmx_q, &gemm_tasks[h][6], heads[h].d_row_tiles, heads[h].k_col_tiles_64x128, heads[h].s_update_tiles, n_sv_tiles, n_sv_tiles, 2, vtcm_scales_1); |
| 2228 | } |
| 2229 | |
| 2230 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2231 | hmx_queue_pop(hmx_q); |
| 2232 | } |
| 2233 | |
| 2234 | work_queue_run(wp, gdn_hvx_phase5_worker, &bctx, n_batch); |
| 2235 | |
| 2236 | const uint32_t valid_tokens = hex_smin(chunk_size, n_tokens - t_chunk); |
| 2237 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2238 | const dma_addr_t attn_chunk_dma = dst->data + |
| 2239 | ((uint64_t) heads[h].iv3 * n_tokens * H + (uint64_t) t_chunk * H + heads[h].iv1) * S_v * sizeof(float); |
| 2240 | dma_queue_push(dma_q, dma_make_data(attn_chunk_dma, heads[h].o_f32[curr_buf]), |
| 2241 | dst->nb[1], S_v * sizeof(float), S_v * sizeof(float), valid_tokens); |
| 2242 | } |
| 2243 | |
| 2244 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2245 | hmx_queue_pop(hmx_q); |
| 2246 | } |
| 2247 | |
| 2248 | work_queue_run(wp, gdn_hvx_phase6_worker, &bctx, n_batch); |
| 2249 | } |
| 2250 | |
| 2251 | if (n_chunks > 0) { |
| 2252 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2253 | dma_queue_pop(dma_q); |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2258 | dma_queue_push(dma_q, dma_make_data(heads[h].state_out_dma, heads[h].s_state), |
| 2259 | S_v * sizeof(float), S_v * sizeof(float), S_v * sizeof(float), S_v); |
| 2260 | } |
| 2261 | for (uint32_t h = 0; h < n_batch; ++h) { |
| 2262 | dma_queue_pop(dma_q); |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | dma_queue_flush(dma_q); |
| 2267 | return HTP_STATUS_OK; |
| 2268 | } |
| 2269 | |
| 2270 | int op_gated_delta_net(struct htp_ops_context * octx) { |
| 2271 | const struct htp_tensor * dst = octx->dst; |
| 2272 | |
| 2273 | for (int i = 0; i < 6; i++) { |
| 2274 | if (htp_tensor_is_extended(octx->src[i])) { |
| 2275 | return HTP_STATUS_NO_SUPPORT; |
| 2276 | } |
| 2277 | } |
| 2278 | if (htp_tensor_is_extended(octx->dst)) { |
| 2279 | return HTP_STATUS_NO_SUPPORT; |
| 2280 | } |
| 2281 | if (octx->dsts[1]) { |
| 2282 | const struct htp_tensor * dst_cache = octx->dsts[1]; |
| 2283 | if (dst_cache->type != HTP_TYPE_F32 || htp_tensor_is_extended(dst_cache)) { |
| 2284 | return HTP_STATUS_NO_SUPPORT; |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | const struct htp_gdn_kernel_params * kparams = (const struct htp_gdn_kernel_params *) octx->kernel_params; |
| 2289 | if (!htp_ops_context_set_n_threads(octx, kparams->n_threads)) { |
| 2290 | return HTP_STATUS_INVAL_PARAMS; |
| 2291 | } |
| 2292 | |
| 2293 | const uint32_t S_v = kparams->S_v; |
| 2294 | const uint32_t n_tokens = kparams->n_tokens; |
| 2295 | const uint32_t total_rows = kparams->total_rows; |
| 2296 | uint32_t row_start = 0; |
| 2297 | uint32_t nrows = total_rows; |
| 2298 | |
| 2299 | if (octx->ctx->mdev.count > 1) { |
| 2300 | const bool can_split = htp_tensor_mdev_data_aligned(dst) && |
| 2301 | ((dst->nb[1] & (HTP_TENSOR_MDEV_LINE_SIZE - 1)) == 0); |
| 2302 | const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition( |
| 2303 | total_rows, |
| 2304 | can_split ? 1 : 0, |
| 2305 | octx->ctx->mdev.idx, |
| 2306 | octx->ctx->mdev.count, |
| 2307 | &octx->ctx->mdev.count_div |
| 2308 | ); |
| 2309 | row_start = range.start; |
| 2310 | nrows = range.count; |
| 2311 | } else if (octx->op_params[1] != 0) { |
| 2312 | row_start = octx->op_params[1]; |
| 2313 | nrows = octx->op_params[2]; |
| 2314 | } |
| 2315 | |
| 2316 | if (nrows == 0) { |
| 2317 | return HTP_STATUS_OK; |
| 2318 | } |
| 2319 | |
| 2320 | if (kparams->kernel_type == HTP_GDN_KERNEL_HMX_CHUNKED) { |
| 2321 | return gated_delta_net_f32_hmx_chunked(octx, kparams, row_start, nrows); |
| 2322 | } |
| 2323 | |
| 2324 | const uint32_t n_threads = (nrows < octx->n_threads) ? nrows : octx->n_threads; |
| 2325 | |
| 2326 | struct htp_gdn_context gctx; |
| 2327 | gctx.octx = octx; |
| 2328 | gctx.kparams = kparams; |
| 2329 | gctx.row_start = row_start; |
| 2330 | gctx.nrows = nrows; |
| 2331 | |
| 2332 | htp_gdn_vtcm_layout_build(&gctx.layout, S_v, n_threads); |
| 2333 | gctx.vtcm_base = VTCM_LAYOUT_PTR(uint8_t, octx->ctx->vtcm_base, gctx.layout.off_state); |
| 2334 | |
| 2335 | if (gctx.layout.total_bytes > octx->ctx->vtcm_size) { |
| 2336 | return HTP_STATUS_VTCM_TOO_SMALL; |
| 2337 | } |
| 2338 | |
| 2339 | FARF(HIGH, "gated-delta-net-f32: q(%ux%ux%ux%u) k(%ux%ux%ux%u) v(%ux%ux%ux%u) state(%ux%ux%ux%u) -> (%ux%ux%ux%u) : " |
| 2340 | "vtcm-size %zu n_threads %u\n", |
| 2341 | octx->src[0]->ne[0], octx->src[0]->ne[1], octx->src[0]->ne[2], octx->src[0]->ne[3], |
| 2342 | octx->src[1]->ne[0], octx->src[1]->ne[1], octx->src[1]->ne[2], octx->src[1]->ne[3], |
| 2343 | octx->src[2]->ne[0], octx->src[2]->ne[1], octx->src[2]->ne[2], octx->src[2]->ne[3], |
| 2344 | octx->src[5]->ne[0], octx->src[5]->ne[1], octx->src[5]->ne[2], octx->src[5]->ne[3], |
| 2345 | dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], |
| 2346 | gctx.layout.total_bytes, n_threads); |
| 2347 | |
| 2348 | if (n_tokens == 1) { |
| 2349 | work_queue_run(octx->ctx->work_queue, gated_delta_net_f32_tg_thread, &gctx, n_threads); |
| 2350 | } else { |
| 2351 | work_queue_run(octx->ctx->work_queue, gated_delta_net_f32_pp_thread, &gctx, n_threads); |
| 2352 | } |
| 2353 | |
| 2354 | return HTP_STATUS_OK; |
| 2355 | } |