← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
recurrent_gate_kernels.c File Reference
#include "bf16_utils.h"
#include "ckernel_engine.h"
#include <dlfcn.h>
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Typedefs

typedef float(* ck_recurrent_libm_f32_fn) (float)
 

Functions

static void ck_bind_recurrent_llama_libm (void)
 
void recurrent_dt_gate_backward (const float *d_gate, const float *alpha, const float *dt_bias, const float *a, float *d_alpha, float *d_dt_bias, float *d_a, int rows, int dim)
 
void recurrent_dt_gate_expanded_forward (const float *alpha, const float *dt_bias, const float *a, float *gate, int rows, int num_heads, int state_dim)
 
void recurrent_dt_gate_forward (const float *alpha, const float *dt_bias, const float *a, float *gate, int rows, int num_heads, int state_dim)
 
void recurrent_dt_gate_forward_pytorch_fp32 (const float *alpha, const float *dt_bias, const float *a, float *gate, int rows, int num_heads, int state_dim)
 
static float recurrent_sigmoid (float x)
 
void recurrent_sigmoid_forward_ggml (const float *x, float *out, int rows, int dim)
 
void recurrent_sigmoid_forward_pytorch_bf16_input_fp32_output (const float *x, float *out, int rows, int dim)
 
void recurrent_silu_backward (const float *d_out, const float *x, float *d_x, int rows, int dim)
 
void recurrent_silu_forward (const float *x, float *out, int rows, int dim)
 
void recurrent_silu_forward_ggml (const float *x, float *out, int rows, int dim)
 
void recurrent_silu_forward_pytorch_bf16_input_fp32_output (const float *x, float *out, int rows, int dim)
 
void recurrent_silu_forward_pytorch_bf16_storage (const float *x, float *out, int rows, int dim)
 
static float recurrent_softplus (float x)
 

Variables

static void * ck_recurrent_libm_handle = NULL
 
static pthread_once_t ck_recurrent_libm_once = PTHREAD_ONCE_INIT
 
static ck_recurrent_libm_f32_fn ck_recurrent_llama_expf = NULL
 
static ck_recurrent_libm_f32_fn ck_recurrent_llama_logf = NULL
 
static ck_recurrent_libm_f32_fn ck_recurrent_pytorch_log1pf = NULL
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 2 of file recurrent_gate_kernels.c.

Typedef Documentation

◆ ck_recurrent_libm_f32_fn

typedef float(* ck_recurrent_libm_f32_fn) (float)

Definition at line 17 of file recurrent_gate_kernels.c.

Function Documentation

◆ ck_bind_recurrent_llama_libm()

static void ck_bind_recurrent_llama_libm ( void  )
static

Definition at line 24 of file recurrent_gate_kernels.c.

25{
26 ck_recurrent_libm_handle = dlopen("libm.so.6", RTLD_NOW | RTLD_LOCAL);
34 }
36 fprintf(stderr,
37 "HARD KERNEL CONTRACT FAULT: llama.cpp recurrent softplus "
38 "requires expf/logf from libm.so.6\n");
39 abort();
40 }
41}
float(* ck_recurrent_libm_f32_fn)(float)
static ck_recurrent_libm_f32_fn ck_recurrent_llama_logf
static void * ck_recurrent_libm_handle
static ck_recurrent_libm_f32_fn ck_recurrent_pytorch_log1pf
static ck_recurrent_libm_f32_fn ck_recurrent_llama_expf

References ck_recurrent_libm_handle, ck_recurrent_llama_expf, ck_recurrent_llama_logf, and ck_recurrent_pytorch_log1pf.

Referenced by recurrent_dt_gate_forward_pytorch_fp32(), and recurrent_softplus().

◆ recurrent_dt_gate_backward()

void recurrent_dt_gate_backward ( const float *  d_gate,
const float *  alpha,
const float *  dt_bias,
const float *  a,
float *  d_alpha,
float *  d_dt_bias,
float *  d_a,
int  rows,
int  dim 
)

Definition at line 104 of file recurrent_gate_kernels.c.

112 {
113 for (int col = 0; col < dim; ++col) {
114 d_dt_bias[col] = 0.0f;
115 d_a[col] = 0.0f;
116 }
117
118 for (int row = 0; row < rows; ++row) {
119 const float *d_gate_row = d_gate + (size_t) row * (size_t) dim;
120 const float *alpha_row = alpha + (size_t) row * (size_t) dim;
121 float *d_alpha_row = d_alpha + (size_t) row * (size_t) dim;
122 for (int col = 0; col < dim; ++col) {
123 const float x = alpha_row[col] + dt_bias[col];
124 const float sp = recurrent_softplus(x);
125 const float sig = recurrent_sigmoid(x);
126 const float d_out = d_gate_row[col];
127 d_a[col] += d_out * sp;
128 {
129 const float d_x = d_out * a[col] * sig;
130 d_alpha_row[col] = d_x;
131 d_dt_bias[col] += d_x;
132 }
133 }
134 }
135}
static float recurrent_softplus(float x)
static float recurrent_sigmoid(float x)

References recurrent_sigmoid(), and recurrent_softplus().

◆ recurrent_dt_gate_expanded_forward()

void recurrent_dt_gate_expanded_forward ( const float *  alpha,
const float *  dt_bias,
const float *  a,
float *  gate,
int  rows,
int  num_heads,
int  state_dim 
)

Definition at line 83 of file recurrent_gate_kernels.c.

89 {
90 for (int row = 0; row < rows; ++row) {
91 const float *alpha_row = alpha + (size_t) row * (size_t) num_heads;
92 float *gate_row = gate + (size_t) row * (size_t) num_heads * (size_t) state_dim;
93 for (int h = 0; h < num_heads; ++h) {
94 const float sp = recurrent_softplus(alpha_row[h] + dt_bias[h]);
95 const float *a_head = a + (size_t) h * (size_t) state_dim;
96 float *gate_head = gate_row + (size_t) h * (size_t) state_dim;
97 for (int col = 0; col < state_dim; ++col) {
98 gate_head[col] = sp * a_head[col];
99 }
100 }
101 }
102}

References recurrent_softplus().

◆ recurrent_dt_gate_forward()

void recurrent_dt_gate_forward ( const float *  alpha,
const float *  dt_bias,
const float *  a,
float *  gate,
int  rows,
int  num_heads,
int  state_dim 
)

Definition at line 65 of file recurrent_gate_kernels.c.

71 {
72 const int dim = num_heads * state_dim;
73 for (int row = 0; row < rows; ++row) {
74 const float *alpha_row = alpha + (size_t) row * (size_t) dim;
75 float *gate_row = gate + (size_t) row * (size_t) dim;
76 for (int col = 0; col < dim; ++col) {
77 const float x = alpha_row[col] + dt_bias[col];
78 gate_row[col] = recurrent_softplus(x) * a[col];
79 }
80 }
81}

References recurrent_softplus().

Referenced by ck_test_recurrent_dt_gate().

◆ recurrent_dt_gate_forward_pytorch_fp32()

void recurrent_dt_gate_forward_pytorch_fp32 ( const float *  alpha,
const float *  dt_bias,
const float *  a,
float *  gate,
int  rows,
int  num_heads,
int  state_dim 
)

Definition at line 179 of file recurrent_gate_kernels.c.

186{
187 if (!alpha || !dt_bias || !a || !gate || rows < 0 || num_heads < 0 || state_dim != 1) {
188 fprintf(stderr,
189 "HARD KERNEL CONTRACT FAULT: invalid PyTorch FP32 recurrent dt-gate arguments\n");
190 abort();
191 }
192#if defined(__AVX512F__)
193 pthread_once(&ck_recurrent_sleef_once, ck_bind_recurrent_pytorch_sleef);
194 if (!ck_recurrent_pytorch_expf16 || !ck_recurrent_pytorch_log1pf16) {
195 fprintf(stderr,
196 "HARD KERNEL CONTRACT FAULT: PyTorch FP32 recurrent dt gate requires "
197 "SLEEF Sleef_expf16_u10 and Sleef_log1pf16_u10; set CK_SLEEF_LIBRARY\n");
198 abort();
199 }
200 if ((num_heads & 15) != 0) {
201 fprintf(stderr,
202 "HARD KERNEL CONTRACT FAULT: PyTorch AVX-512 recurrent dt gate requires "
203 "a head count divisible by 16 (got %d)\n",
204 num_heads);
205 abort();
206 }
209 fprintf(stderr,
210 "HARD KERNEL CONTRACT FAULT: PyTorch FP32 recurrent dt gate requires "
211 "log1pf from libm.so.6\n");
212 abort();
213 }
214 const __m512 threshold = _mm512_set1_ps(20.0f);
215 const int count = rows * num_heads;
216 int index = 0;
217 for (; index + 32 <= count; index += 32) {
218 for (int half = 0; half < 2; ++half) {
219 float x_lanes[16] __attribute__((aligned(64)));
220 float a_lanes[16] __attribute__((aligned(64)));
221 const int base = index + half * 16;
222 for (int lane = 0; lane < 16; ++lane) {
223 const int head = (base + lane) % num_heads;
224 x_lanes[lane] = alpha[base + lane] + dt_bias[head];
225 a_lanes[lane] = a[head];
226 }
227 const __m512 x = _mm512_load_ps(x_lanes);
228 const __m512 softplus = _mm512_mask_blend_ps(
229 _mm512_cmp_ps_mask(x, threshold, _CMP_GT_OQ),
230 ck_recurrent_pytorch_log1pf16(ck_recurrent_pytorch_expf16(x)),
231 x);
232 _mm512_storeu_ps(
233 gate + base,
234 _mm512_mul_ps(softplus, _mm512_load_ps(a_lanes)));
235 }
236 }
237 for (; index < count; ++index) {
238 const int head = index % num_heads;
239 const float x = alpha[index] + dt_bias[head];
240 const float softplus = x > 20.0f
241 ? x
243 gate[index] = softplus * a[head];
244 }
245#else
246 fprintf(stderr,
247 "HARD KERNEL CONTRACT FAULT: PyTorch FP32 recurrent dt gate requires AVX-512\n");
248 abort();
249#endif
250}
static void ck_bind_recurrent_llama_libm(void)
static pthread_once_t ck_recurrent_libm_once
__attribute__((visibility("default"))) CKTokenizer *ck_tokenizer_create(CKTokenizerType type)

References __attribute__(), ck_bind_recurrent_llama_libm(), ck_recurrent_libm_once, ck_recurrent_llama_expf, and ck_recurrent_pytorch_log1pf.

◆ recurrent_sigmoid()

static float recurrent_sigmoid ( float  x)
inlinestatic

Definition at line 54 of file recurrent_gate_kernels.c.

54 {
55 if (x >= 0.0f) {
56 float z = expf(-x);
57 return 1.0f / (1.0f + z);
58 }
59 {
60 float z = expf(x);
61 return z / (1.0f + z);
62 }
63}

Referenced by recurrent_dt_gate_backward(), recurrent_silu_backward(), and recurrent_silu_forward().

◆ recurrent_sigmoid_forward_ggml()

void recurrent_sigmoid_forward_ggml ( const float *  x,
float *  out,
int  rows,
int  dim 
)

Definition at line 501 of file recurrent_gate_kernels.c.

504 {
505 float (*volatile llama_expf)(float) = expf;
506 for (int row = 0; row < rows; ++row) {
507 const float *x_row = x + (size_t) row * (size_t) dim;
508 float *out_row = out + (size_t) row * (size_t) dim;
509 for (int col = 0; col < dim; ++col) {
510 out_row[col] = 1.0f / (1.0f + llama_expf(-x_row[col]));
511 }
512 }
513}

Referenced by hyper_connection_mix_quantized(), and recurrent_norm_sigmoid_gate_llama_avx2_forward().

◆ recurrent_sigmoid_forward_pytorch_bf16_input_fp32_output()

void recurrent_sigmoid_forward_pytorch_bf16_input_fp32_output ( const float *  x,
float *  out,
int  rows,
int  dim 
)

Definition at line 339 of file recurrent_gate_kernels.c.

344{
345 if (!x || !out || rows < 0 || dim < 0) {
346 fprintf(stderr, "HARD KERNEL CONTRACT FAULT: invalid BF16-input FP32-output sigmoid arguments\n");
347 abort();
348 }
349#if defined(__AVX512F__)
350 pthread_once(&ck_recurrent_sleef_once, ck_bind_recurrent_pytorch_sleef);
351 if (!ck_recurrent_pytorch_expf16) {
352 fprintf(stderr,
353 "HARD KERNEL CONTRACT FAULT: PyTorch BF16-input sigmoid requires "
354 "SLEEF Sleef_expf16_u10; set CK_SLEEF_LIBRARY\n");
355 abort();
356 }
357#endif
358 const int count = rows * dim;
359 int i = 0;
360#if defined(__AVX512F__)
361 for (; i + 16 <= count; i += 16) {
362 float lanes[16] __attribute__((aligned(64)));
363 for (int lane = 0; lane < 16; ++lane) {
364 lanes[lane] = bf16_to_float(float_to_bf16(x[i + lane]));
365 }
366 const __m512 values = _mm512_load_ps(lanes);
367 const __m512 denominator = _mm512_add_ps(
368 _mm512_set1_ps(1.0f),
369 ck_recurrent_pytorch_expf16(
370 _mm512_sub_ps(_mm512_setzero_ps(), values)));
371 _mm512_storeu_ps(out + i,
372 _mm512_div_ps(_mm512_set1_ps(1.0f), denominator));
373 }
374#endif
375 for (; i < count; ++i) {
376 const float value = bf16_to_float(float_to_bf16(x[i]));
377 out[i] = 1.0f / (1.0f + expf(-value));
378 }
379}
static uint16_t float_to_bf16(float f)
Definition bf16_utils.h:90
static float bf16_to_float(uint16_t v)
Definition bf16_utils.h:38

References __attribute__(), bf16_to_float(), and float_to_bf16().

Referenced by attn_gate_sigmoid_mul_pytorch_bf16_storage(), and recurrent_norm_sigmoid_gate_pytorch_bf16_storage().

◆ recurrent_silu_backward()

void recurrent_silu_backward ( const float *  d_out,
const float *  x,
float *  d_x,
int  rows,
int  dim 
)

Definition at line 515 of file recurrent_gate_kernels.c.

519 {
520 for (int row = 0; row < rows; ++row) {
521 const float *d_out_row = d_out + (size_t) row * (size_t) dim;
522 const float *x_row = x + (size_t) row * (size_t) dim;
523 float *d_x_row = d_x + (size_t) row * (size_t) dim;
524 for (int col = 0; col < dim; ++col) {
525 const float xv = x_row[col];
526 const float sig = recurrent_sigmoid(xv);
527 d_x_row[col] = d_out_row[col] * (sig + xv * sig * (1.0f - sig));
528 }
529 }
530}

References recurrent_sigmoid().

◆ recurrent_silu_forward()

void recurrent_silu_forward ( const float *  x,
float *  out,
int  rows,
int  dim 
)

Definition at line 137 of file recurrent_gate_kernels.c.

140 {
141 for (int row = 0; row < rows; ++row) {
142 const float *x_row = x + (size_t) row * (size_t) dim;
143 float *out_row = out + (size_t) row * (size_t) dim;
144 for (int col = 0; col < dim; ++col) {
145 const float xv = x_row[col];
146 out_row[col] = xv * recurrent_sigmoid(xv);
147 }
148 }
149}

References recurrent_sigmoid().

Referenced by ck_test_recurrent_silu().

◆ recurrent_silu_forward_ggml()

void recurrent_silu_forward_ggml ( const float *  x,
float *  out,
int  rows,
int  dim 
)

Definition at line 468 of file recurrent_gate_kernels.c.

471 {
472 for (int row = 0; row < rows; ++row) {
473 const float *x_row = x + (size_t) row * (size_t) dim;
474 float *out_row = out + (size_t) row * (size_t) dim;
475 int col = 0;
476#if defined(__AVX512F__) && defined(__AVX512DQ__)
477 for (; col + 16 <= dim; col += 16) {
478 const __m512 xv = _mm512_loadu_ps(x_row + col);
479 const __m512 neg = _mm512_sub_ps(_mm512_setzero_ps(), xv);
480 const __m512 denom = _mm512_add_ps(
481 _mm512_set1_ps(1.0f), recurrent_ggml_expf_avx512(neg));
482 _mm512_storeu_ps(out_row + col, _mm512_div_ps(xv, denom));
483 }
484#elif defined(__AVX2__) && defined(__FMA__)
485 for (; col + 8 <= dim; col += 8) {
486 const __m256 xv = _mm256_loadu_ps(x_row + col);
487 const __m256 neg = _mm256_sub_ps(_mm256_setzero_ps(), xv);
488 const __m256 denom = _mm256_add_ps(
489 _mm256_set1_ps(1.0f), recurrent_ggml_expf_avx2(neg));
490 _mm256_storeu_ps(out_row + col, _mm256_div_ps(xv, denom));
491 }
492#endif
493 float (*volatile llama_expf)(float) = expf;
494 for (; col < dim; ++col) {
495 const float xv = x_row[col];
496 out_row[col] = xv / (1.0f + llama_expf(-xv));
497 }
498 }
499}

Referenced by hyper_connection_mix_quantized(), qwen4_ple_gate_conv_inject_impl(), and recurrent_norm_gate_llama_avx2_forward().

◆ recurrent_silu_forward_pytorch_bf16_input_fp32_output()

void recurrent_silu_forward_pytorch_bf16_input_fp32_output ( const float *  x,
float *  out,
int  rows,
int  dim 
)

Definition at line 299 of file recurrent_gate_kernels.c.

303{
304 if (!x || !out || rows < 0 || dim < 0) {
305 fprintf(stderr, "HARD KERNEL CONTRACT FAULT: invalid BF16-input FP32-output SiLU arguments\n");
306 abort();
307 }
308#if defined(__AVX512F__)
309 pthread_once(&ck_recurrent_sleef_once, ck_bind_recurrent_pytorch_sleef);
310 if (!ck_recurrent_pytorch_expf16) {
311 fprintf(stderr,
312 "HARD KERNEL CONTRACT FAULT: PyTorch BF16-input SiLU requires "
313 "SLEEF Sleef_expf16_u10; set CK_SLEEF_LIBRARY\n");
314 abort();
315 }
316#endif
317 const int count = rows * dim;
318 int i = 0;
319#if defined(__AVX512F__)
320 for (; i + 16 <= count; i += 16) {
321 float lanes[16] __attribute__((aligned(64)));
322 for (int lane = 0; lane < 16; ++lane) {
323 lanes[lane] = bf16_to_float(float_to_bf16(x[i + lane]));
324 }
325 const __m512 values = _mm512_load_ps(lanes);
326 const __m512 denominator = _mm512_add_ps(
327 _mm512_set1_ps(1.0f),
328 ck_recurrent_pytorch_expf16(
329 _mm512_sub_ps(_mm512_setzero_ps(), values)));
330 _mm512_storeu_ps(out + i, _mm512_div_ps(values, denominator));
331 }
332#endif
333 for (; i < count; ++i) {
334 const float value = bf16_to_float(float_to_bf16(x[i]));
335 out[i] = value / (1.0f + expf(-value));
336 }
337}

References __attribute__(), bf16_to_float(), and float_to_bf16().

Referenced by recurrent_norm_gate_pytorch_bf16_storage().

◆ recurrent_silu_forward_pytorch_bf16_storage()

void recurrent_silu_forward_pytorch_bf16_storage ( const float *  x,
float *  out,
int  rows,
int  dim 
)

Definition at line 252 of file recurrent_gate_kernels.c.

256{
257 if (!x || !out || rows < 0 || dim < 0) {
258 fprintf(stderr, "HARD KERNEL CONTRACT FAULT: invalid PyTorch BF16 recurrent SiLU arguments\n");
259 abort();
260 }
261#if defined(__AVX512F__)
262 pthread_once(&ck_recurrent_sleef_once, ck_bind_recurrent_pytorch_sleef);
263 if (!ck_recurrent_pytorch_expf16) {
264 fprintf(stderr,
265 "HARD KERNEL CONTRACT FAULT: PyTorch BF16 recurrent SiLU requires "
266 "SLEEF Sleef_expf16_u10; set CK_SLEEF_LIBRARY\n");
267 abort();
268 }
269#endif
270 for (int row = 0; row < rows; ++row) {
271 const float *src = x + (size_t)row * (size_t)dim;
272 float *dst = out + (size_t)row * (size_t)dim;
273 int col = 0;
274#if defined(__AVX512F__)
275 for (; col + 16 <= dim; col += 16) {
276 float lanes[16] __attribute__((aligned(64)));
277 for (int lane = 0; lane < 16; ++lane) {
278 lanes[lane] = bf16_to_float(float_to_bf16(src[col + lane]));
279 }
280 const __m512 values = _mm512_load_ps(lanes);
281 const __m512 denominator = _mm512_add_ps(
282 _mm512_set1_ps(1.0f),
283 ck_recurrent_pytorch_expf16(
284 _mm512_sub_ps(_mm512_setzero_ps(), values)));
285 _mm512_store_ps(lanes, _mm512_div_ps(values, denominator));
286 for (int lane = 0; lane < 16; ++lane) {
287 dst[col + lane] = bf16_to_float(float_to_bf16(lanes[lane]));
288 }
289 }
290#endif
291 for (; col < dim; ++col) {
292 const float value = bf16_to_float(float_to_bf16(src[col]));
293 const float silu = value / (1.0f + expf(-value));
294 dst[col] = bf16_to_float(float_to_bf16(silu));
295 }
296 }
297}
static void silu(float *x, int n)

References __attribute__(), bf16_to_float(), float_to_bf16(), and silu().

◆ recurrent_softplus()

static float recurrent_softplus ( float  x)
inlinestatic

Variable Documentation

◆ ck_recurrent_libm_handle

void* ck_recurrent_libm_handle = NULL
static

Definition at line 21 of file recurrent_gate_kernels.c.

Referenced by ck_bind_recurrent_llama_libm().

◆ ck_recurrent_libm_once

pthread_once_t ck_recurrent_libm_once = PTHREAD_ONCE_INIT
static

◆ ck_recurrent_llama_expf

ck_recurrent_libm_f32_fn ck_recurrent_llama_expf = NULL
static

◆ ck_recurrent_llama_logf

ck_recurrent_libm_f32_fn ck_recurrent_llama_logf = NULL
static

Definition at line 19 of file recurrent_gate_kernels.c.

Referenced by ck_bind_recurrent_llama_libm(), and recurrent_softplus().

◆ ck_recurrent_pytorch_log1pf

ck_recurrent_libm_f32_fn ck_recurrent_pytorch_log1pf = NULL
static