Skip to content

Commit 21a2b7f

Browse files
committed
Use float in vector dot products
SIMD implementation is limited to floats anyway and the return value is also a float, a float-double-float roundtrip is useless.
1 parent 3afbe43 commit 21a2b7f

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

ggml.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,7 @@ inline static void ggml_vec_mul_f32 (const int n, float * z, const float * x, co
13361336
inline static void ggml_vec_div_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i]/y[i]; }
13371337

13381338
inline static void ggml_vec_dot_f32(const int n, float * restrict s, const float * restrict x, const float * restrict y) {
1339-
ggml_float sumf = 0.0;
1339+
float sumf = 0.0f;
13401340

13411341
#ifdef GGML_SIMD
13421342
const int np = (n & ~(GGML_F32_STEP - 1));
@@ -1360,12 +1360,12 @@ inline static void ggml_vec_dot_f32(const int n, float * restrict s, const float
13601360

13611361
// leftovers
13621362
for (int i = np; i < n; ++i) {
1363-
sumf += (ggml_float)(x[i]*y[i]);
1363+
sumf += x[i]*y[i];
13641364
}
13651365
#else
13661366
// scalar
13671367
for (int i = 0; i < n; ++i) {
1368-
sumf += (ggml_float)(x[i]*y[i]);
1368+
sumf += x[i]*y[i];
13691369
}
13701370
#endif
13711371

@@ -1414,7 +1414,7 @@ static inline __m512 dot_q4_0_oneblock_avx512(
14141414
#endif
14151415

14161416
inline static void ggml_vec_dot_f16(const int n, float * restrict s, ggml_fp16_t * restrict x, ggml_fp16_t * restrict y) {
1417-
ggml_float sumf = 0.0;
1417+
float sumf = 0.0f;
14181418

14191419
#if defined(GGML_SIMD)
14201420
const int np = (n & ~(GGML_F16_STEP - 1));
@@ -1438,11 +1438,11 @@ inline static void ggml_vec_dot_f16(const int n, float * restrict s, ggml_fp16_t
14381438

14391439
// leftovers
14401440
for (int i = np; i < n; ++i) {
1441-
sumf += (ggml_float)(GGML_FP16_TO_FP32(x[i])*GGML_FP16_TO_FP32(y[i]));
1441+
sumf += GGML_FP16_TO_FP32(x[i])*GGML_FP16_TO_FP32(y[i]);
14421442
}
14431443
#else
14441444
for (int i = 0; i < n; ++i) {
1445-
sumf += (ggml_float)(GGML_FP16_TO_FP32(x[i])*GGML_FP16_TO_FP32(y[i]));
1445+
sumf += GGML_FP16_TO_FP32(x[i])*GGML_FP16_TO_FP32(y[i]);
14461446
}
14471447
#endif
14481448

@@ -1899,7 +1899,7 @@ inline static void ggml_vec_dot_q4_1(const int n, float * restrict s, const void
18991899
// compute GGML_VEC_DOT_UNROLL dot products at once
19001900
// xs - x row stride in bytes
19011901
inline static void ggml_vec_dot_f16_unroll(const int n, const int xs, float * restrict s, void * restrict xv, ggml_fp16_t * restrict y) {
1902-
ggml_float sumf[GGML_VEC_DOT_UNROLL] = { 0.0 };
1902+
float sumf[GGML_VEC_DOT_UNROLL] = { 0.0f };
19031903

19041904
ggml_fp16_t * restrict x[GGML_VEC_DOT_UNROLL];
19051905

@@ -1935,13 +1935,13 @@ inline static void ggml_vec_dot_f16_unroll(const int n, const int xs, float * re
19351935
// leftovers
19361936
for (int i = np; i < n; ++i) {
19371937
for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
1938-
sumf[j] += (ggml_float)(GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]));
1938+
sumf[j] += GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]);
19391939
}
19401940
}
19411941
#else
19421942
for (int i = 0; i < n; ++i) {
19431943
for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
1944-
sumf[j] += (ggml_float)(GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]));
1944+
sumf[j] += GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]);
19451945
}
19461946
}
19471947
#endif
@@ -2663,7 +2663,7 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
26632663
const float f = table_f32_f16[i] = GGML_COMPUTE_FP16_TO_FP32(ii);
26642664
table_gelu_f16[i] = GGML_FP32_TO_FP16(ggml_gelu_f32(f));
26652665
table_silu_f16[i] = GGML_FP32_TO_FP16(ggml_silu_f32(f));
2666-
table_exp_f16[i] = GGML_FP32_TO_FP16(exp(f));
2666+
table_exp_f16[i] = GGML_FP32_TO_FP16(expf(f));
26672667
}
26682668

26692669
const uint64_t t_end = ggml_time_us(); UNUSED(t_end);

0 commit comments

Comments
 (0)