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)
return;
58 const size_t inner_dim = (size_t)dim * 2u;
60 for (
int t = 0; t < tokens; ++t) {
61 const float *row = x + (size_t)t * inner_dim;
62 float *dst = out + (size_t)t * (
size_t)dim;
63 for (
int d = 0; d < dim; ++d) {
64 const float gate = row[d];
65 dst[d] = (gate * (1.0f / (1.0f + expf(-1.702f * gate)))) * row[dim + d];
72 if (!x || !out || tokens <= 0 || dim <= 0) {
81 const int inner_dim = dim * 2;
82 for (
int t = 0; t < tokens; ++t) {
83 const float *x_ptr = x + (size_t)t * (
size_t)inner_dim;
84 float *out_ptr = out + (size_t)t * (
size_t)dim;
86 for (
int d = 0; d < dim; ++d) {
87 out_ptr[d] = x_ptr[d];
92 for (
int d = 0; d < dim; ++d) {
93 out_ptr[d] *= x_ptr[dim + d];
100 if (!x || !out || !scratch || tokens <= 0 || dim <= 0) {
104 const size_t fp32_size = (size_t)tokens * (
size_t)dim;
105 const size_t input_size = fp32_size * 2;
106 float *fp32_input = scratch;
107 float *fp32_output = scratch + input_size;
120 if (!x || !d_out || !d_x || tokens <= 0 || dim <= 0) {
124 const float sqrt_2_over_pi = 0.7978845608f;
125 const float coeff = 0.044715f;
126 const int inner_dim = dim * 2;
128 for (
int t = 0; t < tokens; ++t) {
129 const float *x_ptr = x + (size_t)t * (
size_t)inner_dim;
130 const float *d_out_ptr = d_out + (size_t)t * (
size_t)dim;
131 float *d_x_ptr = d_x + (size_t)t * (
size_t)inner_dim;
133 for (
int d = 0; d < dim; ++d) {
135 float b = x_ptr[dim + d];
136 float dout = d_out_ptr[d];
140 float g = sqrt_2_over_pi * (a + coeff * a3);
141 float tanh_g = tanhf(g);
142 float sech2_g = 1.0f - tanh_g * tanh_g;
143 float g_prime = sqrt_2_over_pi * (1.0f + 3.0f * coeff * a2);
145 float d_gelu = 0.5f * (1.0f + tanh_g) + 0.5f * a * sech2_g * g_prime;
146 d_x_ptr[d] = dout * d_gelu * b;
148 float gelu_a = 0.5f * a * (1.0f + tanh_g);
149 d_x_ptr[dim + d] = dout * gelu_a;
155 const uint16_t *d_out,
160 if (!x || !d_out || !d_x || tokens <= 0 || dim <= 0) {
164 const float sqrt_2_over_pi = 0.7978845608f;
165 const float coeff = 0.044715f;
166 const int inner_dim = dim * 2;
168 for (
int t = 0; t < tokens; ++t) {
169 const uint16_t *x_ptr = x + (size_t)t * (
size_t)inner_dim;
170 const uint16_t *d_out_ptr = d_out + (size_t)t * (
size_t)dim;
171 float *d_x_ptr = d_x + (size_t)t * (
size_t)inner_dim;
173 for (
int d = 0; d < dim; ++d) {
178 const float a2 = a * a;
179 const float a3 = a2 * a;
180 const float g = sqrt_2_over_pi * (a + coeff * a3);
181 const float tanh_g = tanhf(g);
182 const float sech2_g = 1.0f - tanh_g * tanh_g;
183 const float g_prime = sqrt_2_over_pi * (1.0f + 3.0f * coeff * a2);
185 const float d_gelu = 0.5f * (1.0f + tanh_g) + 0.5f * a * sech2_g * g_prime;
186 d_x_ptr[d] = dout * d_gelu * b;
188 const float gelu_a = 0.5f * a * (1.0f + tanh_g);
189 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)
void geglu_forward_quick(const float *x, float *out, int tokens, int dim)