Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 111 additions & 5 deletions ggml/src/ggml-cpu/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,101 @@ inline static void ggml_vec_add_f16 (const int n, ggml_fp16_t * z, const ggml_fp
z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) + GGML_CPU_FP16_TO_FP32(y[i]));
}
}
inline static void ggml_vec_add1_f32(const int n, float * z, const float * x, const float v) { for (int i = 0; i < n; ++i) z[i] = x[i] + v; }
inline static void ggml_vec_acc_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] += x[i]; }
inline static void ggml_vec_acc1_f32(const int n, float * y, const float v) { for (int i = 0; i < n; ++i) y[i] += v; }
inline static void ggml_vec_add1_f32(const int n, float * z, const float * x, const float v) {
#if defined(GGML_SIMD)
const int np = (n & ~(GGML_F32_STEP - 1));

GGML_F32_VEC vv = GGML_F32_VEC_SET1(v);

for (int i = 0; i < np; i += GGML_F32_STEP) {
for (int j = 0; j < GGML_F32_ARR; ++j) {
GGML_F32_VEC ax = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
GGML_F32_VEC az = GGML_F32_VEC_ADD(ax, vv);
GGML_F32_VEC_STORE(z + i + j*GGML_F32_EPR, az);
}
}

for (int i = np; i < n; ++i) {
z[i] = x[i] + v;
}
#else
for (int i = 0; i < n; ++i) {
z[i] = x[i] + v;
}
#endif
}
Comment on lines 80 to 98
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make the code consistent about how it handles the leftovers. Here we duplicate the scalar code, while in ggml_vec_add_f32 above we use a common loop iterator. I think we should do the same as in ggml_vec_add_f32.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure thing. latest brings simd/scalar functions inline with each other.

inline static void ggml_vec_acc_f32 (const int n, float * y, const float * x) {
#if defined(GGML_SIMD)
const int np = (n & ~(GGML_F32_STEP - 1));

for (int i = 0; i < np; i += GGML_F32_STEP) {
for (int j = 0; j < GGML_F32_ARR; ++j) {
GGML_F32_VEC ay = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
GGML_F32_VEC ax = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
ay = GGML_F32_VEC_ADD(ay, ax);
GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay);
}
}

for (int i = np; i < n; ++i) {
y[i] += x[i];
}
#else
for (int i = 0; i < n; ++i) {
y[i] += x[i];
}
#endif
}
inline static void ggml_vec_acc1_f32(const int n, float * y, const float v) {
#if defined(GGML_SIMD)
const int np = (n & ~(GGML_F32_STEP - 1));

GGML_F32_VEC vv = GGML_F32_VEC_SET1(v);

for (int i = 0; i < np; i += GGML_F32_STEP) {
for (int j = 0; j < GGML_F32_ARR; ++j) {
GGML_F32_VEC ay = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
ay = GGML_F32_VEC_ADD(ay, vv);
GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay);
}
}

for (int i = np; i < n; ++i) {
y[i] += v;
}
#else
for (int i = 0; i < n; ++i) {
y[i] += v;
}
#endif
}
inline static void ggml_vec_sub_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]; }
inline static void ggml_vec_sub_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) {
for (int i = 0; i < n; ++i) {
z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) - GGML_CPU_FP16_TO_FP32(y[i]));
}
}
inline static void ggml_vec_set_f32 (const int n, float * x, const float v) { for (int i = 0; i < n; ++i) x[i] = v; }
inline static void ggml_vec_set_f32 (const int n, float * x, const float v) {
#if defined(GGML_SIMD)
const int np = (n & ~(GGML_F32_STEP - 1));

GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);

for (int i = 0; i < np; i += GGML_F32_STEP) {
for (int j = 0; j < GGML_F32_ARR; ++j) {
GGML_F32_VEC_STORE(x + i + j*GGML_F32_EPR, vx);
}
}

for (int i = np; i < n; ++i) {
x[i] = v;
}
#else
for (int i = 0; i < n; ++i) {
x[i] = v;
}
#endif
}
inline static void ggml_vec_cpy_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = x[i]; }
inline static void ggml_vec_neg_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = -x[i]; }
inline static void ggml_vec_neg_f16 (const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
Expand All @@ -95,7 +180,28 @@ inline static void ggml_vec_neg_f16 (const int n, ggml_fp16_t * y, const ggml_fp
}
}

inline static void ggml_vec_mul_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]; }
inline static void ggml_vec_mul_f32 (const int n, float * z, const float * x, const float * y) {
#if defined(GGML_SIMD)
const int np = (n & ~(GGML_F32_STEP - 1));

for (int i = 0; i < np; i += GGML_F32_STEP) {
for (int j = 0; j < GGML_F32_ARR; ++j) {
GGML_F32_VEC ax = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
GGML_F32_VEC ay = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
GGML_F32_VEC az = GGML_F32_VEC_MUL(ax, ay);
GGML_F32_VEC_STORE(z + i + j*GGML_F32_EPR, az);
}
}

for (int i = np; i < n; ++i) {
z[i] = x[i]*y[i];
}
#else
for (int i = 0; i < n; ++i) {
z[i] = x[i]*y[i];
}
#endif
}
inline static void ggml_vec_mul_f16 (const int n, ggml_fp16_t * z, const ggml_fp16_t * x, const ggml_fp16_t * y) {
for (int i = 0; i < n; ++i) {
z[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(x[i]) * GGML_CPU_FP16_TO_FP32(y[i]));
Expand Down