GELU activation kernels with SIMD (SSE/AVX/AVX512) More...
#include <math.h>#include <stddef.h>#include <pthread.h>#include <dlfcn.h>#include <stdio.h>#include <stdlib.h>#include "bf16_utils.h"#include "ckernel_quant.h"Go to the source code of this file.
Macros | |
| #define | _GNU_SOURCE |
Typedefs | |
| typedef void(* | ck_gelu_ggml_cpu_init_fn) (void) |
| typedef float(* | ck_gelu_ggml_fp16_to_fp32_fn) (ck_half) |
| typedef ck_half(* | ck_gelu_ggml_fp32_to_fp16_fn) (float) |
| typedef float(* | ck_gelu_math_f32_fn) (float) |
| typedef double(* | ck_gelu_math_f64_fn) (double) |
Functions | |
| static void | ck_gelu_ggml_runtime_init (void) |
| static void | ck_gelu_ggml_table_init (void) |
| static void | ck_gelu_reference_math_init (void) |
| static ck_gelu_math_f64_fn | ck_gelu_system_erf (void) |
| static ck_gelu_math_f32_fn | ck_gelu_system_tanhf (void) |
| static float | ck_gelu_tanh_f32 (float x) |
| static float | ck_gelu_tanh_ggml_reference_f32 (float x) |
| static void | ck_gelu_try_bind_runtime (void *handle) |
| void | gelu_backward_exact (const float *input, const float *d_output, float *d_input, size_t n) |
| void | gelu_backward_fast (const float *input, const float *d_output, float *d_input, size_t n) |
| void | gelu_backward_scalar (const float *input, const float *d_output, float *d_input, size_t n) |
| void | gelu_erf_bf16_storage (float *data, size_t n) |
| void | gelu_erf_fp64_f32_inplace (float *data, size_t n) |
| void | gelu_exact_inplace (float *data, size_t n) |
| void | gelu_fast_inplace (float *data, size_t n) |
| void | gelu_ggml_inplace (float *data, size_t n) |
| void | gelu_ggml_native_inplace (float *data, size_t n) |
| void | gelu_pytorch_erf_f32_inplace (float *data, size_t n) |
| void | gelu_pytorch_erf_sleef_bf16_storage (float *data, size_t n) |
| void | gelu_pytorch_tanh_bf16_storage (float *data, size_t n) |
Variables | |
| static pthread_once_t | ck_gelu_ggml_runtime_once = PTHREAD_ONCE_INIT |
| static ck_half | ck_gelu_ggml_table_f16 [1u<< 16] |
| static pthread_once_t | ck_gelu_ggml_table_once = PTHREAD_ONCE_INIT |
| static ck_gelu_math_f64_fn | ck_gelu_reference_erf = NULL |
| static pthread_once_t | ck_gelu_reference_math_once = PTHREAD_ONCE_INIT |
| static ck_gelu_math_f32_fn | ck_gelu_reference_tanhf = NULL |
| static ck_gelu_ggml_fp16_to_fp32_fn | ck_gelu_runtime_fp16_to_fp32 = NULL |
| static ck_gelu_ggml_fp32_to_fp16_fn | ck_gelu_runtime_fp32_to_fp16 = NULL |
| static void * | ck_gelu_runtime_handle = NULL |
| static int | ck_gelu_runtime_ready = 0 |
| static const ck_half * | ck_gelu_runtime_table_f16 = NULL |
GELU activation kernels with SIMD (SSE/AVX/AVX512)
After changes: make test && make llamacpp-parity-full
GELU: y = x * 0.5 * (1 + erf(x / sqrt(2))) Fast approx: y = x * sigmoid(1.702 * x)
Definition in file gelu_kernels.c.
| #define _GNU_SOURCE |
Definition at line 19 of file gelu_kernels.c.
| typedef void(* ck_gelu_ggml_cpu_init_fn) (void) |
Definition at line 48 of file gelu_kernels.c.
| typedef float(* ck_gelu_ggml_fp16_to_fp32_fn) (ck_half) |
Definition at line 50 of file gelu_kernels.c.
| typedef ck_half(* ck_gelu_ggml_fp32_to_fp16_fn) (float) |
Definition at line 49 of file gelu_kernels.c.
| typedef float(* ck_gelu_math_f32_fn) (float) |
Definition at line 51 of file gelu_kernels.c.
| typedef double(* ck_gelu_math_f64_fn) (double) |
Definition at line 52 of file gelu_kernels.c.
|
static |
Definition at line 119 of file gelu_kernels.c.
References ck_gelu_runtime_handle, ck_gelu_runtime_ready, ck_gelu_try_bind_runtime(), and RTLD_DEFAULT.
Referenced by gelu_ggml_inplace().
|
static |
Definition at line 131 of file gelu_kernels.c.
References ck_gelu_ggml_table_f16, ck_gelu_tanh_ggml_reference_f32(), ggml_fp16_to_fp32, and ggml_fp32_to_fp16.
Referenced by gelu_ggml_inplace(), and gelu_ggml_native_inplace().
|
static |
Definition at line 63 of file gelu_kernels.c.
References ck_gelu_reference_erf, and ck_gelu_reference_tanhf.
Referenced by ck_gelu_system_erf(), and ck_gelu_system_tanhf().
|
static |
Definition at line 78 of file gelu_kernels.c.
References ck_gelu_reference_erf, ck_gelu_reference_math_init(), and ck_gelu_reference_math_once.
Referenced by gelu_erf_bf16_storage(), and gelu_erf_fp64_f32_inplace().
|
static |
Definition at line 73 of file gelu_kernels.c.
References ck_gelu_reference_math_init(), ck_gelu_reference_math_once, and ck_gelu_reference_tanhf.
Referenced by ck_gelu_tanh_ggml_reference_f32().
|
inlinestatic |
Definition at line 36 of file gelu_kernels.c.
Referenced by gelu_exact_inplace().
|
static |
Definition at line 86 of file gelu_kernels.c.
References ck_gelu_system_tanhf().
Referenced by ck_gelu_ggml_table_init().
|
static |
Definition at line 98 of file gelu_kernels.c.
References ck_gelu_runtime_fp16_to_fp32, ck_gelu_runtime_fp32_to_fp16, ck_gelu_runtime_ready, and ck_gelu_runtime_table_f16.
Referenced by ck_gelu_ggml_runtime_init().
| void gelu_backward_exact | ( | const float * | input, |
| const float * | d_output, | ||
| float * | d_input, | ||
| size_t | n | ||
| ) |
Definition at line 368 of file gelu_kernels.c.
References __attribute__().
| void gelu_backward_fast | ( | const float * | input, |
| const float * | d_output, | ||
| float * | d_input, | ||
| size_t | n | ||
| ) |
Definition at line 818 of file gelu_kernels.c.
References __attribute__().
Referenced by gelu_backward_fast_bf16().
| void gelu_backward_scalar | ( | const float * | input, |
| const float * | d_output, | ||
| float * | d_input, | ||
| size_t | n | ||
| ) |
Definition at line 794 of file gelu_kernels.c.
Referenced by gelu_backward_exact_bf16().
| void gelu_erf_bf16_storage | ( | float * | data, |
| size_t | n | ||
| ) |
Definition at line 609 of file gelu_kernels.c.
References bf16_to_float(), ck_gelu_system_erf(), and float_to_bf16().
| void gelu_erf_fp64_f32_inplace | ( | float * | data, |
| size_t | n | ||
| ) |
Definition at line 566 of file gelu_kernels.c.
References ck_gelu_system_erf().
Referenced by gelu_pytorch_erf_f32_inplace().
| void gelu_exact_inplace | ( | float * | data, |
| size_t | n | ||
| ) |
Definition at line 557 of file gelu_kernels.c.
References ck_gelu_tanh_f32().
Referenced by gelu_fast_inplace_bf16(), and mlp_token_parallel_exact().
| void gelu_fast_inplace | ( | float * | data, |
| size_t | n | ||
| ) |
GELU activation forward (fast approximation, in-place)
test_gelu.py::TestGELUForward::test_gelu_fast_inplace
test_gelu.py::TestGELUForward::test_gelu_vs_exact
test_parity.py::test_gelu_parity
Fast GELU approximation: 0.5 * x * (1 + tanh(sqrt(2/pi) * (x + 0.044715 * x^3))) In-place on contiguous buffer.
After changes: make test && make llamacpp-parity-full
Definition at line 243 of file gelu_kernels.c.
References __attribute__().
Referenced by geglu_forward_fp32(), and mlp_token_parallel().
| void gelu_ggml_inplace | ( | float * | data, |
| size_t | n | ||
| ) |
Definition at line 753 of file gelu_kernels.c.
References ck_gelu_ggml_runtime_init(), ck_gelu_ggml_runtime_once, ck_gelu_ggml_table_f16, ck_gelu_ggml_table_init(), ck_gelu_ggml_table_once, ck_gelu_runtime_fp16_to_fp32, ck_gelu_runtime_fp32_to_fp16, ck_gelu_runtime_ready, ck_gelu_runtime_table_f16, ggml_fp16_to_fp32, and ggml_fp32_to_fp16.
| void gelu_ggml_native_inplace | ( | float * | data, |
| size_t | n | ||
| ) |
Definition at line 741 of file gelu_kernels.c.
References ck_gelu_ggml_table_f16, ck_gelu_ggml_table_init(), ck_gelu_ggml_table_once, ggml_fp16_to_fp32, and ggml_fp32_to_fp16.
Referenced by geglu_forward_ggml_native().
| void gelu_pytorch_erf_f32_inplace | ( | float * | data, |
| size_t | n | ||
| ) |
Definition at line 580 of file gelu_kernels.c.
References gelu_erf_fp64_f32_inplace().
| void gelu_pytorch_erf_sleef_bf16_storage | ( | float * | data, |
| size_t | n | ||
| ) |
Definition at line 682 of file gelu_kernels.c.
References bf16_to_float(), and float_to_bf16().
| void gelu_pytorch_tanh_bf16_storage | ( | float * | data, |
| size_t | n | ||
| ) |
Definition at line 590 of file gelu_kernels.c.
References bf16_to_float(), and float_to_bf16().
|
static |
Definition at line 46 of file gelu_kernels.c.
Referenced by gelu_ggml_inplace().
|
static |
Definition at line 44 of file gelu_kernels.c.
Referenced by ck_gelu_ggml_table_init(), gelu_ggml_inplace(), and gelu_ggml_native_inplace().
|
static |
Definition at line 45 of file gelu_kernels.c.
Referenced by gelu_ggml_inplace(), and gelu_ggml_native_inplace().
|
static |
Definition at line 60 of file gelu_kernels.c.
Referenced by ck_gelu_reference_math_init(), and ck_gelu_system_erf().
|
static |
Definition at line 61 of file gelu_kernels.c.
Referenced by ck_gelu_system_erf(), and ck_gelu_system_tanhf().
|
static |
Definition at line 59 of file gelu_kernels.c.
Referenced by ck_gelu_reference_math_init(), and ck_gelu_system_tanhf().
|
static |
Definition at line 56 of file gelu_kernels.c.
Referenced by ck_gelu_try_bind_runtime(), and gelu_ggml_inplace().
|
static |
Definition at line 55 of file gelu_kernels.c.
Referenced by ck_gelu_try_bind_runtime(), and gelu_ggml_inplace().
|
static |
Definition at line 57 of file gelu_kernels.c.
Referenced by ck_gelu_ggml_runtime_init().
|
static |
Definition at line 58 of file gelu_kernels.c.
Referenced by ck_gelu_ggml_runtime_init(), ck_gelu_try_bind_runtime(), and gelu_ggml_inplace().
|
static |
Definition at line 54 of file gelu_kernels.c.
Referenced by ck_gelu_try_bind_runtime(), and gelu_ggml_inplace().