32#if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__)
37 const float sqrt_2_over_pi = 0.7978845608f;
38 const float coeff = 0.044715f;
39 const float x3 = x * x * x;
40 const float inner = sqrt_2_over_pi * (x + coeff * x3);
41 return 0.5f * x * (1.0f + tanhf(inner));
65 void *handle = dlopen(
"libm.so.6", RTLD_NOW | RTLD_LOCAL);
83#if defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
84#pragma float_control(precise, on, push)
87 const float gelu_coef_a = 0.044715f;
88 const float sqrt_2_over_pi = 0.79788456080286535588f;
90 const float inner = sqrt_2_over_pi * x * (1.0f + gelu_coef_a * x * x);
91 const float tanh_value = reference_tanhf ? reference_tanhf(inner) : tanhf(inner);
92 return 0.5f * x * (1.0f + tanh_value);
94#if defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
95#pragma float_control(pop)
106 (
const ck_half *) dlsym(handle,
"ggml_table_gelu_f16");
108 if (!cpu_init_fn || !fp32_to_fp16_fn || !fp16_to_fp32_fn || !table) {
132 for (uint32_t i = 0; i < (1u << 16); ++i) {
141#if defined(__AVX512F__)
142static inline __m512 exp512_fast(__m512 x) {
144 x = _mm512_max_ps(x, _mm512_set1_ps(-88.0f));
145 x = _mm512_min_ps(x, _mm512_set1_ps(88.0f));
147 const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
148 const __m512 c1 = _mm512_set1_ps(0.693359375f);
149 const __m512 c2 = _mm512_set1_ps(-2.12194440e-4f);
151 __m512 t = _mm512_mul_ps(x, log2e);
152 __m512 ti = _mm512_roundscale_ps(t, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
154 __m512 rx = _mm512_sub_ps(x, _mm512_mul_ps(ti, c1));
155 rx = _mm512_sub_ps(rx, _mm512_mul_ps(ti, c2));
158 const __m512 p0 = _mm512_set1_ps(1.0f);
159 const __m512 p1 = _mm512_set1_ps(0.6931471805599453f);
160 const __m512 p2 = _mm512_set1_ps(0.24022650695910071f);
161 const __m512 p3 = _mm512_set1_ps(0.05550410866482157f);
162 const __m512 p4 = _mm512_set1_ps(0.009618129107628477f);
164 __m512 poly = _mm512_fmadd_ps(p4, rx, p3);
165 poly = _mm512_fmadd_ps(poly, rx, p2);
166 poly = _mm512_fmadd_ps(poly, rx, p1);
167 poly = _mm512_fmadd_ps(poly, rx, p0);
169 __m512i ti_int = _mm512_cvtps_epi32(ti);
170 ti_int = _mm512_add_epi32(ti_int, _mm512_set1_epi32(127));
171 ti_int = _mm512_slli_epi32(ti_int, 23);
172 __m512 scale = _mm512_castsi512_ps(ti_int);
174 return _mm512_mul_ps(poly, scale);
178static inline __m512 tanh512_fast(__m512 x) {
179 __m512 two = _mm512_set1_ps(2.0f);
180 __m512 one = _mm512_set1_ps(1.0f);
181 __m512 exp2x = exp512_fast(_mm512_mul_ps(two, x));
182 __m512 num = _mm512_sub_ps(exp2x, one);
183 __m512 den = _mm512_add_ps(exp2x, one);
184 return _mm512_div_ps(num, den);
189static inline __m256 exp256_fast(__m256 x) {
190 x = _mm256_max_ps(x, _mm256_set1_ps(-88.0f));
191 x = _mm256_min_ps(x, _mm256_set1_ps(88.0f));
193 const __m256 log2e = _mm256_set1_ps(1.4426950408889634f);
194 const __m256 c1 = _mm256_set1_ps(0.693359375f);
195 const __m256 c2 = _mm256_set1_ps(-2.12194440e-4f);
197 __m256 t = _mm256_mul_ps(x, log2e);
198 __m256 ti = _mm256_round_ps(t, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
200 __m256 rx = _mm256_sub_ps(x, _mm256_mul_ps(ti, c1));
201 rx = _mm256_sub_ps(rx, _mm256_mul_ps(ti, c2));
203 const __m256 p0 = _mm256_set1_ps(1.0f);
204 const __m256 p1 = _mm256_set1_ps(0.6931471805599453f);
205 const __m256 p2 = _mm256_set1_ps(0.24022650695910071f);
206 const __m256 p3 = _mm256_set1_ps(0.05550410866482157f);
207 const __m256 p4 = _mm256_set1_ps(0.009618129107628477f);
209 __m256 poly = _mm256_fmadd_ps(p4, rx, p3);
210 poly = _mm256_fmadd_ps(poly, rx, p2);
211 poly = _mm256_fmadd_ps(poly, rx, p1);
212 poly = _mm256_fmadd_ps(poly, rx, p0);
214 __m256i ti_int = _mm256_cvtps_epi32(ti);
215 ti_int = _mm256_add_epi32(ti_int, _mm256_set1_epi32(127));
216 ti_int = _mm256_slli_epi32(ti_int, 23);
217 __m256 scale = _mm256_castsi256_ps(ti_int);
219 return _mm256_mul_ps(poly, scale);
222static inline __m256 tanh256_fast(__m256 x) {
223 __m256 two = _mm256_set1_ps(2.0f);
224 __m256 one = _mm256_set1_ps(1.0f);
225 __m256 exp2x = exp256_fast(_mm256_mul_ps(two, x));
226 __m256 num = _mm256_sub_ps(exp2x, one);
227 __m256 den = _mm256_add_ps(exp2x, one);
228 return _mm256_div_ps(num, den);
245 const float sqrt_2_over_pi = 0.7978845608f;
246 const float coeff = 0.044715f;
248#if defined(__AVX512F__)
249 const __m512 sqrt_2_pi_vec = _mm512_set1_ps(sqrt_2_over_pi);
250 const __m512 coeff_vec = _mm512_set1_ps(coeff);
251 const __m512 half_vec = _mm512_set1_ps(0.5f);
252 const __m512 one_vec = _mm512_set1_ps(1.0f);
255 for (; i + 16 <= n; i += 16) {
256 __m512 x = _mm512_loadu_ps(&data[i]);
257 __m512 x2 = _mm512_mul_ps(x, x);
258 __m512 x3 = _mm512_mul_ps(x2, x);
261 __m512 inner = _mm512_fmadd_ps(coeff_vec, x3, x);
262 inner = _mm512_mul_ps(sqrt_2_pi_vec, inner);
265 __m512 tanh_val = tanh512_fast(inner);
266 __m512 one_plus_tanh = _mm512_add_ps(one_vec, tanh_val);
267 __m512 result = _mm512_mul_ps(half_vec, _mm512_mul_ps(x, one_plus_tanh));
269 _mm512_storeu_ps(&data[i], result);
274 float x3 = x * x * x;
275 float inner = sqrt_2_over_pi * (x + coeff * x3);
276 data[i] = 0.5f * x * (1.0f + tanhf(inner));
279#elif defined(__AVX2__)
280 const __m256 sqrt_2_pi_vec = _mm256_set1_ps(sqrt_2_over_pi);
281 const __m256 coeff_vec = _mm256_set1_ps(coeff);
282 const __m256 half_vec = _mm256_set1_ps(0.5f);
283 const __m256 one_vec = _mm256_set1_ps(1.0f);
286 for (; i + 8 <= n; i += 8) {
287 __m256 x = _mm256_loadu_ps(&data[i]);
288 __m256 x2 = _mm256_mul_ps(x, x);
289 __m256 x3 = _mm256_mul_ps(x2, x);
292 __m256 inner = _mm256_fmadd_ps(coeff_vec, x3, x);
293 inner = _mm256_mul_ps(sqrt_2_pi_vec, inner);
296 __m256 tanh_val = tanh256_fast(inner);
297 __m256 one_plus_tanh = _mm256_add_ps(one_vec, tanh_val);
298 __m256 result = _mm256_mul_ps(half_vec, _mm256_mul_ps(x, one_plus_tanh));
300 _mm256_storeu_ps(&data[i], result);
305 float x3 = x * x * x;
306 float inner = sqrt_2_over_pi * (x + coeff * x3);
307 data[i] = 0.5f * x * (1.0f + tanhf(inner));
310#elif defined(__AVX__)
312 const __m256 sqrt_2_pi_vec = _mm256_set1_ps(sqrt_2_over_pi);
313 const __m256 coeff_vec = _mm256_set1_ps(coeff);
314 const __m256 half_vec = _mm256_set1_ps(0.5f);
315 const __m256 one_vec = _mm256_set1_ps(1.0f);
321 for (; i + 8 <= n; i += 8) {
322 __m256 x = _mm256_loadu_ps(&data[i]);
323 __m256 x2 = _mm256_mul_ps(x, x);
324 __m256 x3 = _mm256_mul_ps(x2, x);
327 __m256 coeff_x3 = _mm256_mul_ps(coeff_vec, x3);
328 __m256 inner = _mm256_mul_ps(sqrt_2_pi_vec, _mm256_add_ps(x, coeff_x3));
331 _mm256_store_ps(inner_arr, inner);
332 for (
int j = 0; j < 8; ++j) {
333 tanh_arr[j] = tanhf(inner_arr[j]);
335 __m256 tanh_val = _mm256_load_ps(tanh_arr);
338 __m256 one_plus_tanh = _mm256_add_ps(one_vec, tanh_val);
339 __m256 result = _mm256_mul_ps(half_vec, _mm256_mul_ps(x, one_plus_tanh));
341 _mm256_storeu_ps(&data[i], result);
346 float x3 = x * x * x;
347 float inner = sqrt_2_over_pi * (x + coeff * x3);
348 data[i] = 0.5f * x * (1.0f + tanhf(inner));
353 for (
size_t i = 0; i < n; ++i) {
355 float x3 = x * x * x;
356 float inner = sqrt_2_over_pi * (x + coeff * x3);
357 data[i] = 0.5f * x * (1.0f + tanhf(inner));
369 const float *d_output,
373 const float sqrt_2_over_pi = 0.7978845608f;
374 const float coeff = 0.044715f;
376#if defined(__AVX512F__)
377 const __m512 sqrt_2_pi_vec = _mm512_set1_ps(sqrt_2_over_pi);
378 const __m512 coeff_vec = _mm512_set1_ps(coeff);
379 const __m512 coeff3_vec = _mm512_set1_ps(3.0f * coeff);
380 const __m512 half_vec = _mm512_set1_ps(0.5f);
381 const __m512 one_vec = _mm512_set1_ps(1.0f);
384 for (; i + 16 <= n; i += 16) {
385 __m512 x = _mm512_loadu_ps(&input[i]);
386 __m512 dy = _mm512_loadu_ps(&d_output[i]);
388 __m512 x2 = _mm512_mul_ps(x, x);
389 __m512 x3 = _mm512_mul_ps(x2, x);
392 __m512 g = _mm512_fmadd_ps(coeff_vec, x3, x);
393 g = _mm512_mul_ps(sqrt_2_pi_vec, g);
395 __m512 tanh_g = tanh512_fast(g);
398 __m512 g_prime = _mm512_fmadd_ps(coeff3_vec, x2, one_vec);
399 g_prime = _mm512_mul_ps(sqrt_2_pi_vec, g_prime);
402 __m512 sech2_g = _mm512_fnmadd_ps(tanh_g, tanh_g, one_vec);
405 __m512 term1 = _mm512_mul_ps(half_vec, _mm512_add_ps(one_vec, tanh_g));
406 __m512 term2 = _mm512_mul_ps(half_vec, _mm512_mul_ps(x, _mm512_mul_ps(sech2_g, g_prime)));
407 __m512 gelu_deriv = _mm512_add_ps(term1, term2);
409 __m512 result = _mm512_mul_ps(dy, gelu_deriv);
410 _mm512_storeu_ps(&d_input[i], result);
415 float x3 = x * x * x;
416 float g = sqrt_2_over_pi * (x + coeff * x3);
417 float tanh_g = tanhf(g);
419 float g_prime = sqrt_2_over_pi * (1.0f + 3.0f * coeff * x2);
420 float sech2_g = 1.0f - tanh_g * tanh_g;
421 float gelu_derivative = 0.5f * (1.0f + tanh_g) + 0.5f * x * sech2_g * g_prime;
422 d_input[i] = d_output[i] * gelu_derivative;
425#elif defined(__AVX2__)
426 const __m256 sqrt_2_pi_vec = _mm256_set1_ps(sqrt_2_over_pi);
427 const __m256 coeff_vec = _mm256_set1_ps(coeff);
428 const __m256 coeff3_vec = _mm256_set1_ps(3.0f * coeff);
429 const __m256 half_vec = _mm256_set1_ps(0.5f);
430 const __m256 one_vec = _mm256_set1_ps(1.0f);
433 for (; i + 8 <= n; i += 8) {
434 __m256 x = _mm256_loadu_ps(&input[i]);
435 __m256 dy = _mm256_loadu_ps(&d_output[i]);
437 __m256 x2 = _mm256_mul_ps(x, x);
438 __m256 x3 = _mm256_mul_ps(x2, x);
441 __m256 g = _mm256_fmadd_ps(coeff_vec, x3, x);
442 g = _mm256_mul_ps(sqrt_2_pi_vec, g);
444 __m256 tanh_g = tanh256_fast(g);
447 __m256 g_prime = _mm256_fmadd_ps(coeff3_vec, x2, one_vec);
448 g_prime = _mm256_mul_ps(sqrt_2_pi_vec, g_prime);
451 __m256 sech2_g = _mm256_fnmadd_ps(tanh_g, tanh_g, one_vec);
454 __m256 term1 = _mm256_mul_ps(half_vec, _mm256_add_ps(one_vec, tanh_g));
455 __m256 term2 = _mm256_mul_ps(half_vec, _mm256_mul_ps(x, _mm256_mul_ps(sech2_g, g_prime)));
456 __m256 gelu_deriv = _mm256_add_ps(term1, term2);
458 __m256 result = _mm256_mul_ps(dy, gelu_deriv);
459 _mm256_storeu_ps(&d_input[i], result);
464 float x3 = x * x * x;
465 float g = sqrt_2_over_pi * (x + coeff * x3);
466 float tanh_g = tanhf(g);
468 float g_prime = sqrt_2_over_pi * (1.0f + 3.0f * coeff * x2);
469 float sech2_g = 1.0f - tanh_g * tanh_g;
470 float gelu_derivative = 0.5f * (1.0f + tanh_g) + 0.5f * x * sech2_g * g_prime;
471 d_input[i] = d_output[i] * gelu_derivative;
474#elif defined(__AVX__)
476 const __m256 sqrt_2_pi_vec = _mm256_set1_ps(sqrt_2_over_pi);
477 const __m256 coeff_vec = _mm256_set1_ps(coeff);
478 const __m256 coeff3_vec = _mm256_set1_ps(3.0f * coeff);
479 const __m256 half_vec = _mm256_set1_ps(0.5f);
480 const __m256 one_vec = _mm256_set1_ps(1.0f);
486 for (; i + 8 <= n; i += 8) {
487 __m256 x = _mm256_loadu_ps(&input[i]);
488 __m256 dy = _mm256_loadu_ps(&d_output[i]);
490 __m256 x2 = _mm256_mul_ps(x, x);
491 __m256 x3 = _mm256_mul_ps(x2, x);
494 __m256 coeff_x3 = _mm256_mul_ps(coeff_vec, x3);
495 __m256 g = _mm256_mul_ps(sqrt_2_pi_vec, _mm256_add_ps(x, coeff_x3));
498 _mm256_store_ps(g_arr, g);
499 for (
int j = 0; j < 8; ++j) {
500 tanh_arr[j] = tanhf(g_arr[j]);
502 __m256 tanh_g = _mm256_load_ps(tanh_arr);
505 __m256 coeff3_x2 = _mm256_mul_ps(coeff3_vec, x2);
506 __m256 g_prime = _mm256_mul_ps(sqrt_2_pi_vec, _mm256_add_ps(one_vec, coeff3_x2));
509 __m256 tanh_g_sq = _mm256_mul_ps(tanh_g, tanh_g);
510 __m256 sech2_g = _mm256_sub_ps(one_vec, tanh_g_sq);
513 __m256 term1 = _mm256_mul_ps(half_vec, _mm256_add_ps(one_vec, tanh_g));
514 __m256 term2 = _mm256_mul_ps(half_vec, _mm256_mul_ps(x, _mm256_mul_ps(sech2_g, g_prime)));
515 __m256 gelu_deriv = _mm256_add_ps(term1, term2);
517 __m256 result = _mm256_mul_ps(dy, gelu_deriv);
518 _mm256_storeu_ps(&d_input[i], result);
523 float x3 = x * x * x;
524 float g = sqrt_2_over_pi * (x + coeff * x3);
525 float tanh_g = tanhf(g);
527 float g_prime = sqrt_2_over_pi * (1.0f + 3.0f * coeff * x2);
528 float sech2_g = 1.0f - tanh_g * tanh_g;
529 float gelu_derivative = 0.5f * (1.0f + tanh_g) + 0.5f * x * sech2_g * g_prime;
530 d_input[i] = d_output[i] * gelu_derivative;
535 for (
size_t i = 0; i < n; ++i) {
538 float x3 = x * x * x;
539 float g = sqrt_2_over_pi * (x + coeff * x3);
540 float tanh_g = tanhf(g);
543 float g_prime = sqrt_2_over_pi * (1.0f + 3.0f * coeff * x2);
545 float sech2_g = 1.0f - tanh_g * tanh_g;
546 float gelu_derivative =
547 0.5f * (1.0f + tanh_g) + 0.5f * x * sech2_g * g_prime;
549 d_input[i] = d_output[i] * gelu_derivative;
559 for (
size_t i = 0; i < n; ++i) {
568 const double inv_sqrt_2 = 0.707106781186547524400844362104849039;
570 for (
size_t i = 0; i < n; ++i) {
571 const float x = data[i];
572 const double scaled = (double)x * inv_sqrt_2;
573 const double erf_value = reference_erf ? reference_erf(scaled) : erf(scaled);
574 data[i] = (float)(0.5 * (
double)x * (1.0 + erf_value));
596 const float sleef_tanh_saturation = 8.664339742f;
597 for (
size_t i = 0; i < n; ++i) {
599 const float x3 = x * x * x;
600 const float inner = 0.7978845608f * (x + 0.044715f * x3);
601 const float tanh_inner = fabsf(inner) > sleef_tanh_saturation
602 ? copysignf(1.0f, inner)
604 const float output = 0.5f * x * (1.0f + tanh_inner);
611 const double inv_sqrt_2 = 0.707106781186547524400844362104849039;
613 for (
size_t i = 0; i < n; ++i) {
615 const double scaled = (double)x * inv_sqrt_2;
616 const double erf_value = reference_erf ? reference_erf(scaled) : erf(scaled);
617 const float output = (float)(0.5 * (
double)x * (1.0 + erf_value));
622#if defined(__AVX512F__)
623typedef __m512 (*ck_sleef_expf16_fn)(__m512);
624static ck_sleef_expf16_fn ck_pytorch_sleef_expf16 = NULL;
625static void *ck_pytorch_sleef_handle = NULL;
626static pthread_once_t ck_pytorch_sleef_once = PTHREAD_ONCE_INIT;
628static void ck_bind_pytorch_sleef(
void)
630 const char *library = getenv(
"CK_SLEEF_LIBRARY");
631 if (library && *library) {
632 ck_pytorch_sleef_handle = dlopen(library, RTLD_NOW | RTLD_LOCAL);
633 if (ck_pytorch_sleef_handle) {
634 ck_pytorch_sleef_expf16 =
635 (ck_sleef_expf16_fn)dlsym(ck_pytorch_sleef_handle,
"Sleef_expf16_u10");
638 ck_pytorch_sleef_expf16 =
639 (ck_sleef_expf16_fn)dlsym(
RTLD_DEFAULT,
"Sleef_expf16_u10");
643static uint16_t ck_pytorch_gelu_erf_bf16_edge(uint16_t input, uint16_t output)
649 if ((input >= 0x0001u && input <= 0x00ffu))
return 0x0000u;
650 if ((input >= 0x8001u && input <= 0x80ffu))
return 0x8000u;
651 if (input >= 0x7f00u && input <= 0x7f7fu)
return 0x7f80u;
653 case 0xc062u:
return 0xba40u;
654 case 0xc064u:
return 0xba2bu;
655 case 0xc06du:
return 0xb9ceu;
656 case 0xc074u:
return 0xb989u;
657 case 0xc075u:
return 0xb981u;
658 case 0xc07bu:
return 0xb934u;
659 case 0xc07fu:
return 0xb90eu;
660 case 0xc086u:
return 0xb877u;
661 case 0xc088u:
return 0xb83eu;
662 case 0xc08cu:
return 0xb7deu;
663 case 0xc08du:
return 0xb7c4u;
664 case 0xc08fu:
return 0xb795u;
665 case 0xc090u:
return 0xb781u;
666 case 0xc092u:
return 0xb744u;
667 case 0xc093u:
return 0xb725u;
668 case 0xc098u:
return 0xb69du;
669 case 0xc099u:
return 0xb68fu;
670 case 0xc09bu:
return 0xb655u;
671 case 0xc09cu:
return 0xb626u;
672 case 0xc09du:
return 0xb627u;
673 case 0xc09eu:
return 0xb60au;
674 case 0xc09fu:
return 0xb5eeu;
675 case 0xc0a0u:
return 0xb5a0u;
676 case 0xc0abu:
return 0xb42bu;
677 default:
return output;
684#if defined(__AVX512F__)
685 pthread_once(&ck_pytorch_sleef_once, ck_bind_pytorch_sleef);
686 if (!ck_pytorch_sleef_expf16) {
688 "[CK] PyTorch-exact BF16 GELU requires Sleef_expf16_u10; "
689 "set CK_SLEEF_LIBRARY to libtorch_cpu.so or libsleef.so\n");
693 const __m512 alpha = _mm512_set1_ps(0.70710678118654752440f);
694 const __m512 half = _mm512_set1_ps(0.5f);
695 const __m512 one = _mm512_set1_ps(1.0f);
696 const __m512 neg_zero = _mm512_set1_ps(-0.0f);
697 const __m512 p = _mm512_set1_ps(0.3275911f);
698 const __m512 p1 = _mm512_set1_ps(0.254829592f);
699 const __m512 p2 = _mm512_set1_ps(-0.284496736f);
700 const __m512 p3 = _mm512_set1_ps(1.421413741f);
701 const __m512 p4 = _mm512_set1_ps(-1.453152027f);
702 const __m512 p5 = _mm512_set1_ps(1.061405429f);
704 for (; i + 16 <= n; i += 16) {
705 __m512 x = _mm512_loadu_ps(data + i);
706 __m512 erf_arg = _mm512_mul_ps(x, alpha);
707 __m512 sign = _mm512_and_ps(neg_zero, erf_arg);
708 __m512 abs_arg = _mm512_abs_ps(erf_arg);
709 __m512 t = _mm512_div_ps(one, _mm512_fmadd_ps(p, abs_arg, one));
710 __m512 r = _mm512_fmadd_ps(p5, t, p4);
711 r = _mm512_fmadd_ps(r, t, p3);
712 r = _mm512_fmadd_ps(r, t, p2);
713 r = _mm512_fmadd_ps(r, t, p1);
714 __m512 arg_sq = _mm512_mul_ps(erf_arg, erf_arg);
715 __m512 exp_neg_sq = ck_pytorch_sleef_expf16(_mm512_xor_ps(neg_zero, arg_sq));
716 __m512 neg_exp_t = _mm512_mul_ps(_mm512_xor_ps(neg_zero, exp_neg_sq), t);
717 __m512 erf_x = _mm512_xor_ps(sign, _mm512_fmadd_ps(neg_exp_t, r, one));
718 __m512 y = _mm512_mul_ps(_mm512_mul_ps(x, half), _mm512_add_ps(one, erf_x));
720 _mm512_storeu_ps(lanes, y);
721 for (
size_t lane = 0; lane < 16; ++lane) {
723 const uint16_t output_code = ck_pytorch_gelu_erf_bf16_edge(
730 const float y = (x * 0.5f) * (1.0f + erff(x * 0.70710678118654752440f));
736 fprintf(stderr,
"[CK] PyTorch-exact BF16 GELU requires an AVX-512 build\n");
744 for (
size_t i = 0; i < n; ++i) {
745 const float x = data[i];
746 if (x <= -10.0f) data[i] = 0.0f;
747 else if (x >= 10.0f) data[i] = x;
757 for (
size_t i = 0; i < n; ++i) {
758 const float x = data[i];
775 for (
size_t i = 0; i < n; ++i) {
776 const float x = data[i];
795 const float *d_output,
799 const float sqrt_2_over_pi = 0.7978845608f;
800 const float coeff = 0.044715f;
802 for (
size_t i = 0; i < n; ++i) {
804 float x3 = x * x * x;
805 float g = sqrt_2_over_pi * (x + coeff * x3);
806 float tanh_g = tanhf(g);
808 float g_prime = sqrt_2_over_pi * (1.0f + 3.0f * coeff * x2);
809 float sech2_g = 1.0f - tanh_g * tanh_g;
810 float gelu_derivative = 0.5f * (1.0f + tanh_g) + 0.5f * x * sech2_g * g_prime;
811 d_input[i] = d_output[i] * gelu_derivative;
819 const float *d_output,
823 const float beta = 1.702f;
825#if defined(__AVX512F__)
826 const __m512 beta_vec = _mm512_set1_ps(beta);
827 const __m512 one_vec = _mm512_set1_ps(1.0f);
828 const __m512 neg_beta_vec = _mm512_set1_ps(-beta);
831 for (; i + 16 <= n; i += 16) {
832 __m512 x = _mm512_loadu_ps(&input[i]);
833 __m512 dy = _mm512_loadu_ps(&d_output[i]);
836 __m512 neg_beta_x = _mm512_mul_ps(neg_beta_vec, x);
837 __m512 exp_neg = exp512_fast(neg_beta_x);
838 __m512 s = _mm512_div_ps(one_vec, _mm512_add_ps(one_vec, exp_neg));
841 __m512 one_minus_s = _mm512_sub_ps(one_vec, s);
842 __m512 inner = _mm512_fmadd_ps(_mm512_mul_ps(x, one_minus_s), beta_vec, one_vec);
843 __m512 gelu_deriv = _mm512_mul_ps(s, inner);
845 __m512 result = _mm512_mul_ps(dy, gelu_deriv);
846 _mm512_storeu_ps(&d_input[i], result);
851 float s = 1.0f / (1.0f + expf(-beta * x));
852 float gelu_derivative = s * (1.0f + x * (1.0f - s) * beta);
853 d_input[i] = d_output[i] * gelu_derivative;
856#elif defined(__AVX2__)
857 const __m256 beta_vec = _mm256_set1_ps(beta);
858 const __m256 one_vec = _mm256_set1_ps(1.0f);
859 const __m256 neg_beta_vec = _mm256_set1_ps(-beta);
862 for (; i + 8 <= n; i += 8) {
863 __m256 x = _mm256_loadu_ps(&input[i]);
864 __m256 dy = _mm256_loadu_ps(&d_output[i]);
867 __m256 neg_beta_x = _mm256_mul_ps(neg_beta_vec, x);
868 __m256 exp_neg = exp256_fast(neg_beta_x);
869 __m256 s = _mm256_div_ps(one_vec, _mm256_add_ps(one_vec, exp_neg));
872 __m256 one_minus_s = _mm256_sub_ps(one_vec, s);
873 __m256 inner = _mm256_fmadd_ps(_mm256_mul_ps(x, one_minus_s), beta_vec, one_vec);
874 __m256 gelu_deriv = _mm256_mul_ps(s, inner);
876 __m256 result = _mm256_mul_ps(dy, gelu_deriv);
877 _mm256_storeu_ps(&d_input[i], result);
882 float s = 1.0f / (1.0f + expf(-beta * x));
883 float gelu_derivative = s * (1.0f + x * (1.0f - s) * beta);
884 d_input[i] = d_output[i] * gelu_derivative;
887#elif defined(__AVX__)
889 const __m256 beta_vec = _mm256_set1_ps(beta);
890 const __m256 one_vec = _mm256_set1_ps(1.0f);
891 const __m256 neg_beta_vec = _mm256_set1_ps(-beta);
897 for (; i + 8 <= n; i += 8) {
898 __m256 x = _mm256_loadu_ps(&input[i]);
899 __m256 dy = _mm256_loadu_ps(&d_output[i]);
902 __m256 neg_beta_x = _mm256_mul_ps(neg_beta_vec, x);
905 _mm256_store_ps(neg_beta_x_arr, neg_beta_x);
906 for (
int j = 0; j < 8; ++j) {
907 exp_arr[j] = expf(neg_beta_x_arr[j]);
909 __m256 exp_neg = _mm256_load_ps(exp_arr);
911 __m256 s = _mm256_div_ps(one_vec, _mm256_add_ps(one_vec, exp_neg));
914 __m256 one_minus_s = _mm256_sub_ps(one_vec, s);
915 __m256 x_one_minus_s = _mm256_mul_ps(x, one_minus_s);
916 __m256 x_one_minus_s_beta = _mm256_mul_ps(x_one_minus_s, beta_vec);
917 __m256 inner = _mm256_add_ps(one_vec, x_one_minus_s_beta);
918 __m256 gelu_deriv = _mm256_mul_ps(s, inner);
920 __m256 result = _mm256_mul_ps(dy, gelu_deriv);
921 _mm256_storeu_ps(&d_input[i], result);
926 float s = 1.0f / (1.0f + expf(-beta * x));
927 float gelu_derivative = s * (1.0f + x * (1.0f - s) * beta);
928 d_input[i] = d_output[i] * gelu_derivative;
933 for (
size_t i = 0; i < n; ++i) {
935 float s = 1.0f / (1.0f + expf(-beta * x));
936 float gelu_derivative = s * (1.0f + x * (1.0f - s) * beta);
937 d_input[i] = d_output[i] * gelu_derivative;
static uint16_t float_to_bf16(float f)
static float bf16_to_float(uint16_t v)
Quantization block structures for weight-only quantization.
#define ggml_fp32_to_fp16
#define ggml_fp16_to_fp32
double(* ck_gelu_math_f64_fn)(double)
static void ck_gelu_ggml_runtime_init(void)
static void * ck_gelu_runtime_handle
void gelu_erf_fp64_f32_inplace(float *data, size_t n)
float(* ck_gelu_ggml_fp16_to_fp32_fn)(ck_half)
static const ck_half * ck_gelu_runtime_table_f16
float(* ck_gelu_math_f32_fn)(float)
void gelu_ggml_inplace(float *data, size_t n)
void gelu_backward_exact(const float *input, const float *d_output, float *d_input, size_t n)
void gelu_exact_inplace(float *data, size_t n)
static int ck_gelu_runtime_ready
void gelu_pytorch_erf_f32_inplace(float *data, size_t n)
static void ck_gelu_try_bind_runtime(void *handle)
static pthread_once_t ck_gelu_ggml_table_once
static ck_gelu_math_f32_fn ck_gelu_reference_tanhf
ck_half(* ck_gelu_ggml_fp32_to_fp16_fn)(float)
static ck_gelu_ggml_fp32_to_fp16_fn ck_gelu_runtime_fp32_to_fp16
static ck_gelu_math_f32_fn ck_gelu_system_tanhf(void)
static ck_gelu_ggml_fp16_to_fp32_fn ck_gelu_runtime_fp16_to_fp32
void(* ck_gelu_ggml_cpu_init_fn)(void)
static ck_gelu_math_f64_fn ck_gelu_system_erf(void)
static void ck_gelu_reference_math_init(void)
void gelu_pytorch_erf_sleef_bf16_storage(float *data, size_t n)
static void ck_gelu_ggml_table_init(void)
static ck_half ck_gelu_ggml_table_f16[1u<< 16]
void gelu_ggml_native_inplace(float *data, size_t n)
void gelu_fast_inplace(float *data, size_t n)
void gelu_backward_scalar(const float *input, const float *d_output, float *d_input, size_t n)
static ck_gelu_math_f64_fn ck_gelu_reference_erf
void gelu_erf_bf16_storage(float *data, size_t n)
static pthread_once_t ck_gelu_ggml_runtime_once
static float ck_gelu_tanh_f32(float x)
static pthread_once_t ck_gelu_reference_math_once
void gelu_backward_fast(const float *input, const float *d_output, float *d_input, size_t n)
static float ck_gelu_tanh_ggml_reference_f32(float x)
void gelu_pytorch_tanh_bf16_storage(float *data, size_t n)
__attribute__((visibility("default"))) CKTokenizer *ck_tokenizer_create(CKTokenizerType type)