MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / ggml_vec_dot_f16_unroll

Function ggml_vec_dot_f16_unroll

ggml.c:1226–1277  ·  view source on GitHub ↗

compute GGML_VEC_DOT_UNROLL dot products at once xs - x row stride in bytes

Source from the content-addressed store, hash-verified

1224// compute GGML_VEC_DOT_UNROLL dot products at once
1225// xs - x row stride in bytes
1226inline static void ggml_vec_dot_f16_unroll(const int n, const int xs, float * restrict s, void * restrict xv, ggml_fp16_t * restrict y) {
1227 ggml_float sumf[GGML_VEC_DOT_UNROLL] = { 0.0 };
1228
1229 ggml_fp16_t * restrict x[GGML_VEC_DOT_UNROLL];
1230
1231 for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
1232 x[i] = (ggml_fp16_t *) ((char *) xv + i*xs);
1233 }
1234
1235#if defined(GGML_SIMD)
1236 const int np = (n & ~(GGML_F16_STEP - 1));
1237
1238 GGML_F16_VEC sum[GGML_VEC_DOT_UNROLL][GGML_F16_ARR] = { { GGML_F16_VEC_ZERO } };
1239
1240 GGML_F16_VEC ax[GGML_F16_ARR];
1241 GGML_F16_VEC ay[GGML_F16_ARR];
1242
1243 for (int i = 0; i < np; i += GGML_F16_STEP) {
1244 for (int j = 0; j < GGML_F16_ARR; j++) {
1245 ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
1246
1247 for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
1248 ax[j] = GGML_F16_VEC_LOAD(x[k] + i + j*GGML_F16_EPR, j);
1249
1250 sum[k][j] = GGML_F16_VEC_FMA(sum[k][j], ax[j], ay[j]);
1251 }
1252 }
1253 }
1254
1255 // reduce sum0..sum3 to sum0
1256 for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
1257 GGML_F16_VEC_REDUCE(sumf[k], sum[k]);
1258 }
1259
1260 // leftovers
1261 for (int i = np; i < n; ++i) {
1262 for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
1263 sumf[j] += (ggml_float)(GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]));
1264 }
1265 }
1266#else
1267 for (int i = 0; i < n; ++i) {
1268 for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
1269 sumf[j] += (ggml_float)(GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]));
1270 }
1271 }
1272#endif
1273
1274 for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
1275 s[i] = sumf[i];
1276 }
1277}
1278
1279inline static void ggml_vec_mad_f32(const int n, float * restrict y, const float * restrict x, const float v) {
1280#if defined(GGML_SIMD)

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected