19 if (!x || !out || tokens <= 0 || dim <= 0)
return;
21 for (
int t = 0; t < tokens; ++t) {
22 const float *row = x + (size_t)t * (
size_t)dim * 2;
23 float *dst = out + (size_t)t * dim;
24 for (
int d = 0; d < dim; ++d) {
25 const float gate = row[d], up = row[dim + d];
28 dst[d] = gate <= -10.0f ? 0.0f : value * up;
35 const float sqrt_2_over_pi = 0.7978845608f;
36 const float coeff = 0.044715f;
37 const float x3 = x * x * x;
38 const float inner = sqrt_2_over_pi * (x + coeff * x3);
39 return 0.5f * x * (1.0f + tanhf(inner));
44 const int inner_dim = dim * 2;
45 for (
int t = 0; t < tokens; ++t) {
46 const float *x_ptr = x + (size_t)t * (
size_t)inner_dim;
47 float *out_ptr = out + (size_t)t * (
size_t)dim;
49 for (
int d = 0; d < dim; ++d) {
57 if (!x || !out || tokens <= 0 || dim <= 0) {
66 const int inner_dim = dim * 2;
67 for (
int t = 0; t < tokens; ++t) {
68 const float *x_ptr = x + (size_t)t * (
size_t)inner_dim;
69 float *out_ptr = out + (size_t)t * (
size_t)dim;
71 for (
int d = 0; d < dim; ++d) {
72 out_ptr[d] = x_ptr[d];
77 for (
int d = 0; d < dim; ++d) {
78 out_ptr[d] *= x_ptr[dim + d];
85 if (!x || !out || !scratch || tokens <= 0 || dim <= 0) {
89 const size_t fp32_size = (size_t)tokens * (
size_t)dim;
90 const size_t input_size = fp32_size * 2;
91 float *fp32_input = scratch;
92 float *fp32_output = scratch + input_size;
105 if (!x || !d_out || !d_x || tokens <= 0 || dim <= 0) {
109 const float sqrt_2_over_pi = 0.7978845608f;
110 const float coeff = 0.044715f;
111 const int inner_dim = dim * 2;
113 for (
int t = 0; t < tokens; ++t) {
114 const float *x_ptr = x + (size_t)t * (
size_t)inner_dim;
115 const float *d_out_ptr = d_out + (size_t)t * (
size_t)dim;
116 float *d_x_ptr = d_x + (size_t)t * (
size_t)inner_dim;
118 for (
int d = 0; d < dim; ++d) {
120 float b = x_ptr[dim + d];
121 float dout = d_out_ptr[d];
125 float g = sqrt_2_over_pi * (a + coeff * a3);
126 float tanh_g = tanhf(g);
127 float sech2_g = 1.0f - tanh_g * tanh_g;
128 float g_prime = sqrt_2_over_pi * (1.0f + 3.0f * coeff * a2);
130 float d_gelu = 0.5f * (1.0f + tanh_g) + 0.5f * a * sech2_g * g_prime;
131 d_x_ptr[d] = dout * d_gelu * b;
133 float gelu_a = 0.5f * a * (1.0f + tanh_g);
134 d_x_ptr[dim + d] = dout * gelu_a;
140 const uint16_t *d_out,
145 if (!x || !d_out || !d_x || tokens <= 0 || dim <= 0) {
149 const float sqrt_2_over_pi = 0.7978845608f;
150 const float coeff = 0.044715f;
151 const int inner_dim = dim * 2;
153 for (
int t = 0; t < tokens; ++t) {
154 const uint16_t *x_ptr = x + (size_t)t * (
size_t)inner_dim;
155 const uint16_t *d_out_ptr = d_out + (size_t)t * (
size_t)dim;
156 float *d_x_ptr = d_x + (size_t)t * (
size_t)inner_dim;
158 for (
int d = 0; d < dim; ++d) {
163 const float a2 = a * a;
164 const float a3 = a2 * a;
165 const float g = sqrt_2_over_pi * (a + coeff * a3);
166 const float tanh_g = tanhf(g);
167 const float sech2_g = 1.0f - tanh_g * tanh_g;
168 const float g_prime = sqrt_2_over_pi * (1.0f + 3.0f * coeff * a2);
170 const float d_gelu = 0.5f * (1.0f + tanh_g) + 0.5f * a * sech2_g * g_prime;
171 d_x_ptr[d] = dout * d_gelu * b;
173 const float gelu_a = 0.5f * a * (1.0f + tanh_g);
174 d_x_ptr[dim + d] = dout * gelu_a;
static void float_tensor_to_bf16(const float *src, uint16_t *dst, size_t count)
static float bf16_to_float(uint16_t v)
static void bf16_tensor_to_float(const uint16_t *src, float *dst, size_t count)
int ck_strict_parity_enabled(void)
static float ck_gelu_tanh_parity_f32(float x)
void geglu_backward_bf16_mixed(const uint16_t *x, const uint16_t *d_out, float *d_x, int tokens, int dim)
void geglu_forward_exact(const float *x, float *out, int tokens, int dim)
void geglu_backward_fp32(const float *x, const float *d_out, float *d_x, int tokens, int dim)
void geglu_forward_fp32(const float *x, float *out, int tokens, int dim)
void geglu_forward_ggml_native(const float *x, float *out, int tokens, int dim)
void gelu_ggml_native_inplace(float *data, size_t n)
void gelu_fast_inplace(float *data, size_t n)
void geglu_forward_bf16(const uint16_t *x, uint16_t *out, int tokens, int dim, float *scratch)