ax2 = GGML_F32_VEC_LOAD(x + i + 1*ggml_f32_epr);
ay2 = GGML_F32_VEC_LOAD(y + i + 1*ggml_f32_epr);
ay2 = GGML_F32_VEC_FMA(ay2, ax2, vx);
GGML_F32_VEC_STORE(y + i + 1*ggml_f32_epr, ay2);
ax3 = GGML_F32_VEC_LOAD(x + i + 2*ggml_f32_epr);
ay3 = GGML_F32_VEC_LOAD(y + i + 2*ggml_f32_epr);
ay3 = GGML_F32_VEC_FMA(ay3, ax3, vx);
GGML_F32_VEC_STORE(y + i + 2*ggml_f32_epr, ay3);
ax4 = GGML_F32_VEC_LOAD(x + i + 3*ggml_f32_epr);
ay4 = GGML_F32_VEC_LOAD(y + i + 3*ggml_f32_epr);
ay4 = GGML_F32_VEC_FMA(ay4, ax4, vx);
GGML_F32_VEC_STORE(y + i + 3*ggml_f32_epr, ay4);
ax5 = GGML_F32_VEC_LOAD(x + i + 4*ggml_f32_epr);
ay5 = GGML_F32_VEC_LOAD(y + i + 4*ggml_f32_epr);
ay5 = GGML_F32_VEC_FMA(ay5, ax5, vx);
GGML_F32_VEC_STORE(y + i + 4*ggml_f32_epr, ay5);
ax6 = GGML_F32_VEC_LOAD(x + i + 5*ggml_f32_epr);
ay6 = GGML_F32_VEC_LOAD(y + i + 5*ggml_f32_epr);
ay6 = GGML_F32_VEC_FMA(ay6, ax6, vx);
GGML_F32_VEC_STORE(y + i + 5*ggml_f32_epr, ay6);
ax7 = GGML_F32_VEC_LOAD(x + i + 6*ggml_f32_epr);
ay7 = GGML_F32_VEC_LOAD(y + i + 6*ggml_f32_epr);
ay7 = GGML_F32_VEC_FMA(ay7, ax7, vx);
GGML_F32_VEC_STORE(y + i + 6*ggml_f32_epr, ay7);
ax8 = GGML_F32_VEC_LOAD(x + i + 7*ggml_f32_epr);
ay8 = GGML_F32_VEC_LOAD(y + i + 7*ggml_f32_epr);
ay8 = GGML_F32_VEC_FMA(ay8, ax8, vx);
GGML_F32_VEC_STORE(y + i + 7*ggml_f32_epr, ay8);
} // leftovers // Since 8 unrolls are done in above loop, leftovers lie in range [0, ggml_f32_step] which is handled in below loop constint np2 = (n & ~(ggml_f32_epr - 1)); for (int i = np; i < np2; i += ggml_f32_epr) {
ax1 = GGML_F32_VEC_LOAD(x + i);
ay1 = GGML_F32_VEC_LOAD(y + i);
ay1 = GGML_F32_VEC_FMA(ay1, ax1, vx);
GGML_F32_VEC_STORE(y + i, ay1);
} // maximum number of leftover elements will be less that ggml_f32_epr. Apply predicated svmad on available elements only if (np2 < n) {
svbool_t pg =svwhilelt_b32(np2, n);
ax1 = svld1_f32(pg, x + np2);
ay1 = svld1_f32(pg, y + np2);
ay1 = svmad_f32_m(pg, ax1, vx, ay1);
for (int i = 0; i < np; i += GGML_F16_STEP) { for (int j = 0; j < GGML_F16_ARR; j++) {
ax[j] = GGML_F16_VEC_LOAD(x + i + j*GGML_F16_EPR, j);
ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
ay[j] = GGML_F16_VEC_FMA(ay[j], ax[j], vx);
GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
}
}
// leftovers for (int i = np; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i]) + GGML_CPU_FP16_TO_FP32(x[i])*v);
} #else // scalar for (int i = 0; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i]) + GGML_CPU_FP16_TO_FP32(x[i])*v);
} #endif
}
// xs and vs are byte strides of x and v inlinestaticvoid ggml_vec_mad_f32_unroll(constint n, constint xs, constint vs, float * GGML_RESTRICT y, constfloat * GGML_RESTRICT xv, constfloat * GGML_RESTRICT vv) {
for (int i = 0; i < np; i += GGML_F32_STEP) { for (int j = 0; j < GGML_F32_ARR; j++) {
ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
ax[k][j] = GGML_F32_VEC_LOAD(x[k] + i + j*GGML_F32_EPR);
ay[j] = GGML_F32_VEC_FMA(ay[j], ax[k][j], vx[k]);
}
GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
}
}
// leftovers for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) { for (int i = np; i < n; ++i) {
y[i] += x[k][i]*v[k][0];
}
} #endif #else // scalar for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) { for (int i = 0; i < n; ++i) {
y[i] += x[k][i]*v[k][0];
}
} #endif
}
inlinestaticvoid ggml_vec_mad1_f32(constint n, float * y, constfloat * x, constfloat s, constfloat b) { #ifdefined(GGML_USE_ACCELERATE)
vDSP_vsmsa(x, 1, &s, &b, y, 1, n); #elifdefined(GGML_SIMD) #ifdefined(__ARM_FEATURE_SVE) // scalar ; TODO: Write SVE code for (int i = 0; i < n; ++i) {
y[i] = x[i]*s + b;
} #else constint np = (n & ~(GGML_F32_STEP - 1));
GGML_F32_VEC vs = GGML_F32_VEC_SET1(s);
GGML_F32_VEC vb = GGML_F32_VEC_SET1(b);
GGML_F32_VEC ay[GGML_F32_ARR];
for (int i = 0; i < np; i += GGML_F32_STEP) { for (int j = 0; j < GGML_F32_ARR; j++) {
ay[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
ay[j] = GGML_F32_VEC_FMA(ay[j], vs, vb);
GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
}
}
// leftovers for (int i = np; i < n; ++i) {
y[i] = x[i]*s + b;
} #endif #else // scalar for (int i = 0; i < n; ++i) {
y[i] = x[i]*s + b;
} #endif
}
//inline static void ggml_vec_scale_f32(const int n, float * y, const float v) { for (int i = 0; i < n; ++i) y[i] *= v; } inlinestaticvoid ggml_vec_scale_f32(constint n, float * y, constfloat v) { #ifdefined(GGML_USE_ACCELERATE)
vDSP_vsmul(y, 1, &v, y, 1, n); #elifdefined(GGML_SIMD) #ifdefined(__ARM_FEATURE_SVE) constint sve_register_length = ggml_cpu_get_sve_cnt() * 8; constint ggml_f32_epr = sve_register_length / 32;//8;//svcntw(); // SVE128:4, SVE256:8, SVE512:16 constint ggml_f32_step = 2 * ggml_f32_epr;
GGML_F32_VEC vx = GGML_F32_VEC_SET1(v); constint np = (n & ~(ggml_f32_step - 1));
svfloat32_t ay1;
svfloat32_t ay2; for (int i = 0; i < np; i += ggml_f32_step) {
ay1 = GGML_F32_VEC_LOAD(y + i);
ay1 = GGML_F32_VEC_MUL(ay1, vx);
GGML_F32_VEC_STORE(y + i, ay1);
ay2 = GGML_F32_VEC_LOAD(y + i + 1*ggml_f32_epr);
ay2 = GGML_F32_VEC_MUL(ay2, vx);
GGML_F32_VEC_STORE(y + i + 1*ggml_f32_epr, ay2);
} // leftovers // maximum number of leftover elements will be less that ggml_f32_epr. Apply predicated svmad on available elements only if (np < n) {
svbool_t pg = svwhilelt_b32(np, n);
ay1 = svld1_f32(pg, y + np);
ay1 = svmul_f32_m(pg, ay1, vx);
svst1_f32(pg, y + np, ay1);
} #else constint np = (n & ~(GGML_F32_STEP - 1));
GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
GGML_F32_VEC ay[GGML_F32_ARR];
for (int i = 0; i < np; i += GGML_F32_STEP) { for (int j = 0; j < GGML_F32_ARR; j++) {
ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
ay[j] = GGML_F32_VEC_MUL(ay[j], vx);
GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
}
}
// leftovers for (int i = np; i < n; ++i) {
y[i] *= v;
} #endif #else // scalar for (int i = 0; i < n; ++i) {
y[i] *= v;
} #endif
}
inlinestaticvoid ggml_vec_scale_f16(constint n, ggml_fp16_t * y, constfloat v) { #ifdefined(GGML_SIMD) constint np = (n & ~(GGML_F16_STEP - 1));
GGML_F16_VEC vx = GGML_F16_VEC_SET1(v);
GGML_F16_VEC ay[GGML_F16_ARR];
for (int i = 0; i < np; i += GGML_F16_STEP) { for (int j = 0; j < GGML_F16_ARR; j++) {
ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
ay[j] = GGML_F16_VEC_MUL(ay[j], vx);
GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
}
}
// leftovers for (int i = np; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i])*v);
} #else // scalar for (int i = 0; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(y[i])*v);
} #endif
}
inlinestaticvoid ggml_vec_norm_f32 (constint n, float * s, constfloat * x) { ggml_vec_dot_f32(n, s, 0, x, 0, x, 0, 1); *s = sqrtf(*s); } inlinestaticvoid ggml_vec_sqr_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = x[i]*x[i]; } inlinestaticvoid ggml_vec_sqr_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) { float v = GGML_CPU_FP16_TO_FP32(x[i]);
y[i] = GGML_CPU_FP32_TO_FP16(v*v);
}
} inlinestaticvoid ggml_vec_sqrt_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = sqrtf(x[i]); } inlinestaticvoid ggml_vec_sqrt_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(sqrtf(GGML_CPU_FP16_TO_FP32(x[i])));
}
} inlinestaticvoid ggml_vec_log_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = logf(x[i]); } inlinestaticvoid ggml_vec_log_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(logf(GGML_CPU_FP16_TO_FP32(x[i])));
}
} inlinestaticvoid ggml_vec_sin_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = sinf(x[i]); } inlinestaticvoid ggml_vec_sin_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(sinf(GGML_CPU_FP16_TO_FP32(x[i])));
}
} inlinestaticvoid ggml_vec_cos_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = cosf(x[i]); } inlinestaticvoid ggml_vec_cos_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(cosf(GGML_CPU_FP16_TO_FP32(x[i])));
}
} inlinestaticvoid ggml_vec_abs_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = fabsf(x[i]); } inlinestaticvoid ggml_vec_abs_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(fabsf(GGML_CPU_FP16_TO_FP32(x[i])));
}
} inlinestaticvoid ggml_vec_sgn_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? 1.f : ((x[i] < 0.f) ? -1.f : 0.f); } inlinestaticvoid ggml_vec_sgn_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) { float v = GGML_CPU_FP16_TO_FP32(x[i]);
y[i] = GGML_CPU_FP32_TO_FP16((v > 0.f) ? 1.f : ((v < 0.f) ? -1.f : 0.f));
}
} inlinestaticvoid ggml_vec_step_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? 1.f : 0.f; } inlinestaticvoid ggml_vec_step_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16((GGML_CPU_FP16_TO_FP32(x[i]) > 0.f) ? 1.f : 0.f);
}
} inlinestaticvoid ggml_vec_tanh_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = tanhf(x[i]); } inlinestaticvoid ggml_vec_tanh_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(tanhf(GGML_CPU_FP16_TO_FP32(x[i])));
}
} inlinestaticvoid ggml_vec_elu_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? x[i] : expm1f(x[i]); } inlinestaticvoid ggml_vec_elu_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(expm1f(GGML_CPU_FP16_TO_FP32(x[i])));
}
} inlinestaticvoid ggml_vec_relu_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? x[i] : 0.f; } inlinestaticvoid ggml_vec_relu_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) { float v = GGML_CPU_FP16_TO_FP32(x[i]);
y[i] = GGML_CPU_FP32_TO_FP16((v > 0.f) ? v : 0.f);
}
} inlinestaticvoid ggml_vec_leaky_relu_f32 (constint n, float * y, constfloat * x, constfloat ns) { for (int i = 0; i < n; ++i) y[i] = ((x[i] > 0.f) ? x[i] : 0.f) + ns * ((x[i] < 0.0f) ? x[i] : 0.f); } inlinestaticvoid ggml_vec_leaky_relu_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x, constfloat ns) { for (int i = 0; i < n; ++i) { float v = GGML_CPU_FP16_TO_FP32(x[i]);
y[i] = GGML_CPU_FP32_TO_FP16(((v > 0.f) ? v : 0.f) + ns * ((v < 0.0f) ? v : 0.f));
}
} inlinestaticvoid ggml_vec_sigmoid_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = 1.f / (1.f + expf(-x[i])); } inlinestaticvoid ggml_vec_sigmoid_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(1.f / (1.f + expf(-GGML_CPU_FP16_TO_FP32(x[i]))));
}
} // TODO: optimize performance inlinestaticvoid ggml_vec_hardswish_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f)); } inlinestaticvoid ggml_vec_hardswish_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) { float v = GGML_CPU_FP16_TO_FP32(x[i]);
y[i] = GGML_CPU_FP32_TO_FP16(v * fminf(1.0f, fmaxf(0.0f, (v + 3.0f) / 6.0f)));
}
} inlinestaticvoid ggml_vec_hardsigmoid_f32 (constint n, float * y, constfloat * x) { for (inti = 0; i < n; ++i) y[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f)); } inlinestaticvoid ggml_vec_hardsigmoid_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(fminf(1.0f, fmaxf(0.0f, (GGML_CPU_FP16_TO_FP32(x[i]) + 3.0f) / 6.0f)));
}
} inlinestaticvoid ggml_vec_exp_f32 (constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) y[i] = expf(x[i]); } inlinestaticvoid ggml_vec_exp_f16 (constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) {
y[i] = GGML_CPU_FP32_TO_FP16(expf(GGML_CPU_FP16_TO_FP32(x[i])));
}
}
//inline static void ggml_vec_gelu_quick_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) { // const uint16_t * i16 = (const uint16_t *) x; // for (int i = 0; i < n; ++i) { // y[i] = ggml_table_gelu_quick_f16[i16[i]]; // } //}
#ifdef GGML_GELU_QUICK_FP16 inlinestaticvoid ggml_vec_gelu_quick_f32(constint n, float * y, constfloat * x) {
uint16_t t; for (int i = 0; i < n; ++i) {
ggml_fp16_t fp16 = GGML_CPU_FP32_TO_FP16(x[i]);
memcpy(&t, &fp16, sizeof(uint16_t));
y[i] = GGML_CPU_FP16_TO_FP32(ggml_table_gelu_quick_f16[t]);
}
} #else inlinestaticvoid ggml_vec_gelu_quick_f32(constint n, float * y, constfloat * x) { for (int i = 0; i < n; ++i) {
y[i] = ggml_gelu_quick_f32(x[i]);
}
} #endif
inlinestaticvoid ggml_vec_gelu_quick_f16(constint n, ggml_fp16_t * y, const ggml_fp16_t * x) { for (int i = 0; i < n; ++i) { float v = GGML_CPU_FP16_TO_FP32(x[i]);
y[i] = GGML_CPU_FP32_TO_FP16(v*(1.0f/(1.0f+expf(GELU_QUICK_COEF*v))));
}
}
// Sigmoid Linear Unit (SiLU) function inlinestaticfloat ggml_silu_f32(float x) { return x/(1.0f + expf(-x));
} inlinestatic ggml_fp16_t ggml_silu_f16(ggml_fp16_t x) { float v = GGML_CPU_FP16_TO_FP32(x); return GGML_CPU_FP32_TO_FP16(v/(1.0f + expf(-v)));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.