14#if defined(__AVX2__) || defined(__AVX512F__)
38 "HARD KERNEL CONTRACT FAULT: llama.cpp recurrent softplus "
39 "requires expf/logf from libm.so.6\n");
58 return 1.0f / (1.0f + z);
62 return z / (1.0f + z);
73 const int dim = num_heads * state_dim;
74 for (
int row = 0; row < rows; ++row) {
75 const float *alpha_row = alpha + (size_t) row * (
size_t) dim;
76 float *gate_row = gate + (size_t) row * (
size_t) dim;
77 for (
int col = 0; col < dim; ++col) {
78 const float x = alpha_row[col] + dt_bias[col];
91 for (
int row = 0; row < rows; ++row) {
92 const float *alpha_row = alpha + (size_t) row * (
size_t) num_heads;
93 float *gate_row = gate + (size_t) row * (
size_t) num_heads * (size_t) state_dim;
94 for (
int h = 0; h < num_heads; ++h) {
96 const float *a_head = a + (size_t) h * (
size_t) state_dim;
97 float *gate_head = gate_row + (size_t) h * (
size_t) state_dim;
98 for (
int col = 0; col < state_dim; ++col) {
99 gate_head[col] = sp * a_head[col];
107 const float *dt_bias,
114 for (
int col = 0; col < dim; ++col) {
115 d_dt_bias[col] = 0.0f;
119 for (
int row = 0; row < rows; ++row) {
120 const float *d_gate_row = d_gate + (size_t) row * (
size_t) dim;
121 const float *alpha_row = alpha + (size_t) row * (
size_t) dim;
122 float *d_alpha_row = d_alpha + (size_t) row * (
size_t) dim;
123 for (
int col = 0; col < dim; ++col) {
124 const float x = alpha_row[col] + dt_bias[col];
127 const float d_out = d_gate_row[col];
128 d_a[col] += d_out * sp;
130 const float d_x = d_out * a[col] * sig;
131 d_alpha_row[col] = d_x;
132 d_dt_bias[col] += d_x;
142 for (
int row = 0; row < rows; ++row) {
143 const float *x_row = x + (size_t) row * (
size_t) dim;
144 float *out_row = out + (size_t) row * (
size_t) dim;
145 for (
int col = 0; col < dim; ++col) {
146 const float xv = x_row[col];
152#if defined(__AVX512F__)
153typedef __m512 (*ck_recurrent_sleef_expf16_fn)(__m512);
154typedef __m512 (*ck_recurrent_sleef_log1pf16_fn)(__m512);
155static ck_recurrent_sleef_expf16_fn ck_recurrent_pytorch_expf16 = NULL;
156static ck_recurrent_sleef_log1pf16_fn ck_recurrent_pytorch_log1pf16 = NULL;
157static void *ck_recurrent_sleef_handle = NULL;
158static pthread_once_t ck_recurrent_sleef_once = PTHREAD_ONCE_INIT;
160static void ck_bind_recurrent_pytorch_sleef(
void)
162 const char *library = getenv(
"CK_SLEEF_LIBRARY");
163 if (library && *library) {
164 ck_recurrent_sleef_handle = dlopen(library, RTLD_NOW | RTLD_LOCAL);
165 if (ck_recurrent_sleef_handle) {
166 ck_recurrent_pytorch_expf16 = (ck_recurrent_sleef_expf16_fn)dlsym(
167 ck_recurrent_sleef_handle,
"Sleef_expf16_u10");
168 ck_recurrent_pytorch_log1pf16 = (ck_recurrent_sleef_log1pf16_fn)dlsym(
169 ck_recurrent_sleef_handle,
"Sleef_log1pf16_u10");
172 ck_recurrent_pytorch_expf16 =
173 (ck_recurrent_sleef_expf16_fn)dlsym(
RTLD_DEFAULT,
"Sleef_expf16_u10");
174 ck_recurrent_pytorch_log1pf16 =
175 (ck_recurrent_sleef_log1pf16_fn)dlsym(
RTLD_DEFAULT,
"Sleef_log1pf16_u10");
181 const float *dt_bias,
188 if (!alpha || !dt_bias || !a || !gate || rows < 0 || num_heads < 0 || state_dim != 1) {
190 "HARD KERNEL CONTRACT FAULT: invalid PyTorch FP32 recurrent dt-gate arguments\n");
193#if defined(__AVX512F__)
194 pthread_once(&ck_recurrent_sleef_once, ck_bind_recurrent_pytorch_sleef);
195 if (!ck_recurrent_pytorch_expf16 || !ck_recurrent_pytorch_log1pf16) {
197 "HARD KERNEL CONTRACT FAULT: PyTorch FP32 recurrent dt gate requires "
198 "SLEEF Sleef_expf16_u10 and Sleef_log1pf16_u10; set CK_SLEEF_LIBRARY\n");
201 if ((num_heads & 15) != 0) {
203 "HARD KERNEL CONTRACT FAULT: PyTorch AVX-512 recurrent dt gate requires "
204 "a head count divisible by 16 (got %d)\n",
211 "HARD KERNEL CONTRACT FAULT: PyTorch FP32 recurrent dt gate requires "
212 "log1pf from libm.so.6\n");
215 const __m512 threshold = _mm512_set1_ps(20.0f);
216 const int count = rows * num_heads;
218 for (; index + 32 <=
count; index += 32) {
219 for (
int half = 0; half < 2; ++half) {
222 const int base = index + half * 16;
223 for (
int lane = 0; lane < 16; ++lane) {
224 const int head = (base + lane) % num_heads;
225 x_lanes[lane] = alpha[base + lane] + dt_bias[head];
226 a_lanes[lane] = a[head];
228 const __m512 x = _mm512_load_ps(x_lanes);
229 const __m512 softplus = _mm512_mask_blend_ps(
230 _mm512_cmp_ps_mask(x, threshold, _CMP_GT_OQ),
231 ck_recurrent_pytorch_log1pf16(ck_recurrent_pytorch_expf16(x)),
235 _mm512_mul_ps(softplus, _mm512_load_ps(a_lanes)));
238 for (; index <
count; ++index) {
239 const int head = index % num_heads;
240 const float x = alpha[index] + dt_bias[head];
241 const float softplus = x > 20.0f
244 gate[index] = softplus * a[head];
248 "HARD KERNEL CONTRACT FAULT: PyTorch FP32 recurrent dt gate requires AVX-512\n");
258 if (!x || !out || rows < 0 || dim < 0) {
259 fprintf(stderr,
"HARD KERNEL CONTRACT FAULT: invalid PyTorch BF16 recurrent SiLU arguments\n");
262#if defined(__AVX512F__)
263 pthread_once(&ck_recurrent_sleef_once, ck_bind_recurrent_pytorch_sleef);
264 if (!ck_recurrent_pytorch_expf16) {
266 "HARD KERNEL CONTRACT FAULT: PyTorch BF16 recurrent SiLU requires "
267 "SLEEF Sleef_expf16_u10; set CK_SLEEF_LIBRARY\n");
271 for (
int row = 0; row < rows; ++row) {
272 const float *src = x + (size_t)row * (
size_t)dim;
273 float *dst = out + (size_t)row * (
size_t)dim;
275#if defined(__AVX512F__)
276 for (; col + 16 <= dim; col += 16) {
278 for (
int lane = 0; lane < 16; ++lane) {
281 const __m512 values = _mm512_load_ps(lanes);
282 const __m512 denominator = _mm512_add_ps(
283 _mm512_set1_ps(1.0f),
284 ck_recurrent_pytorch_expf16(
285 _mm512_sub_ps(_mm512_setzero_ps(), values)));
286 _mm512_store_ps(lanes, _mm512_div_ps(values, denominator));
287 for (
int lane = 0; lane < 16; ++lane) {
292 for (; col < dim; ++col) {
294 const float silu = value / (1.0f + expf(-value));
305 if (!x || !out || rows < 0 || dim < 0) {
306 fprintf(stderr,
"HARD KERNEL CONTRACT FAULT: invalid BF16-input FP32-output SiLU arguments\n");
309#if defined(__AVX512F__)
310 pthread_once(&ck_recurrent_sleef_once, ck_bind_recurrent_pytorch_sleef);
311 if (!ck_recurrent_pytorch_expf16) {
313 "HARD KERNEL CONTRACT FAULT: PyTorch BF16-input SiLU requires "
314 "SLEEF Sleef_expf16_u10; set CK_SLEEF_LIBRARY\n");
318 const int count = rows * dim;
320#if defined(__AVX512F__)
321 for (; i + 16 <=
count; i += 16) {
323 for (
int lane = 0; lane < 16; ++lane) {
326 const __m512 values = _mm512_load_ps(lanes);
327 const __m512 denominator = _mm512_add_ps(
328 _mm512_set1_ps(1.0f),
329 ck_recurrent_pytorch_expf16(
330 _mm512_sub_ps(_mm512_setzero_ps(), values)));
331 _mm512_storeu_ps(out + i, _mm512_div_ps(values, denominator));
334 for (; i <
count; ++i) {
336 out[i] = value / (1.0f + expf(-value));
346 if (!x || !out || rows < 0 || dim < 0) {
347 fprintf(stderr,
"HARD KERNEL CONTRACT FAULT: invalid BF16-input FP32-output sigmoid arguments\n");
350#if defined(__AVX512F__)
351 pthread_once(&ck_recurrent_sleef_once, ck_bind_recurrent_pytorch_sleef);
352 if (!ck_recurrent_pytorch_expf16) {
354 "HARD KERNEL CONTRACT FAULT: PyTorch BF16-input sigmoid requires "
355 "SLEEF Sleef_expf16_u10; set CK_SLEEF_LIBRARY\n");
359 const int count = rows * dim;
361#if defined(__AVX512F__)
362 for (; i + 16 <=
count; i += 16) {
364 for (
int lane = 0; lane < 16; ++lane) {
367 const __m512 values = _mm512_load_ps(lanes);
368 const __m512 denominator = _mm512_add_ps(
369 _mm512_set1_ps(1.0f),
370 ck_recurrent_pytorch_expf16(
371 _mm512_sub_ps(_mm512_setzero_ps(), values)));
372 _mm512_storeu_ps(out + i,
373 _mm512_div_ps(_mm512_set1_ps(1.0f), denominator));
376 for (; i <
count; ++i) {
378 out[i] = 1.0f / (1.0f + expf(-value));
382#if defined(__AVX512F__) && defined(__AVX512DQ__)
384static inline __m512 recurrent_ggml_expf_avx512(__m512 x) {
385 const __m512 r = _mm512_set1_ps(0x1.8p23f);
386 const __m512 z = _mm512_fmadd_ps(x, _mm512_set1_ps(0x1.715476p+0f), r);
387 const __m512 n = _mm512_sub_ps(z, r);
388 const __m512 b = _mm512_fnmadd_ps(
389 n, _mm512_set1_ps(0x1.7f7d1cp-20f),
390 _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.62e4p-1f), x));
391 const __mmask16 d = _mm512_cmp_ps_mask(
392 _mm512_abs_ps(n), _mm512_set1_ps(192.0f), _CMP_GT_OQ);
393 const __m512 u = _mm512_mul_ps(b, b);
394 const __m512 j = _mm512_fmadd_ps(
397 _mm512_set1_ps(0x1.0e4020p-7f), b,
398 _mm512_set1_ps(0x1.573e2ep-5f)),
401 _mm512_set1_ps(0x1.555e66p-3f), b,
402 _mm512_set1_ps(0x1.fffdb6p-2f))),
405 _mm512_set1_ps(0x1.ffffecp-1f), b,
406 _mm512_set1_ps(1.0f)));
407 const __m512 res = _mm512_scalef_ps(j, n);
408 if (_mm512_kortestz(d, d)) {
411 const __m512 zero = _mm512_setzero_ps();
412 const __m512 alt = _mm512_mask_blend_ps(
413 _mm512_cmp_ps_mask(n, zero, _CMP_LE_OQ),
414 _mm512_set1_ps(INFINITY),
416 return _mm512_mask_blend_ps(d, res, alt);
420#if defined(__AVX2__) && defined(__FMA__)
422static inline __m256 recurrent_ggml_expf_avx2(__m256 x) {
423 const __m256 r = _mm256_set1_ps(0x1.8p23f);
424 const __m256 z = _mm256_fmadd_ps(x, _mm256_set1_ps(0x1.715476p+0f), r);
425 const __m256 n = _mm256_sub_ps(z, r);
426 const __m256 b = _mm256_fnmadd_ps(
427 n, _mm256_set1_ps(0x1.7f7d1cp-20f),
428 _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.62e4p-1f), x));
429 const __m256i e = _mm256_slli_epi32(_mm256_castps_si256(z), 23);
430 const __m256 k = _mm256_castsi256_ps(
431 _mm256_add_epi32(e, _mm256_castps_si256(_mm256_set1_ps(1.0f))));
432 const __m256i c = _mm256_castps_si256(
433 _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.0f), n),
434 _mm256_set1_ps(126.0f), _CMP_GT_OQ));
435 const __m256 u = _mm256_mul_ps(b, b);
436 const __m256 j = _mm256_fmadd_ps(
438 _mm256_fmadd_ps(_mm256_set1_ps(0x1.0e4020p-7f), b,
439 _mm256_set1_ps(0x1.573e2ep-5f)),
441 _mm256_fmadd_ps(_mm256_set1_ps(0x1.555e66p-3f), b,
442 _mm256_set1_ps(0x1.fffdb6p-2f))),
443 u, _mm256_mul_ps(_mm256_set1_ps(0x1.ffffecp-1f), b));
444 if (!_mm256_movemask_ps(_mm256_castsi256_ps(c))) {
445 return _mm256_fmadd_ps(j, k, k);
447 const __m256i g = _mm256_and_si256(
448 _mm256_castps_si256(_mm256_cmp_ps(n, _mm256_setzero_ps(), _CMP_LE_OQ)),
449 _mm256_set1_epi32((
int) 0x82000000u));
450 const __m256 s1 = _mm256_castsi256_ps(
451 _mm256_add_epi32(g, _mm256_set1_epi32(0x7f000000)));
452 const __m256 s2 = _mm256_castsi256_ps(_mm256_sub_epi32(e, g));
453 const __m256i d = _mm256_castps_si256(
454 _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.0f), n),
455 _mm256_set1_ps(192.0f), _CMP_GT_OQ));
457 _mm256_and_ps(_mm256_castsi256_ps(d), _mm256_mul_ps(s1, s1)),
459 _mm256_castsi256_ps(d),
462 _mm256_castsi256_ps(c),
463 _mm256_mul_ps(_mm256_fmadd_ps(s2, j, s2), s1)),
465 _mm256_castsi256_ps(c), _mm256_fmadd_ps(k, j, k)))));
473} ck_recurrent_silu_ggml_args_t;
478 const ck_recurrent_silu_ggml_args_t *args =
479 (
const ck_recurrent_silu_ggml_args_t *)opaque;
480 const int dim = args->dim;
481 for (
int row = begin; row <
end; ++row) {
482 const float *x_row = args->x + (size_t)row * (
size_t)dim;
483 float *out_row = args->out + (size_t)row * (
size_t)dim;
485#if defined(__AVX512F__) && defined(__AVX512DQ__)
486 for (; col + 16 <= dim; col += 16) {
487 const __m512 xv = _mm512_loadu_ps(x_row + col);
488 const __m512 neg = _mm512_sub_ps(_mm512_setzero_ps(), xv);
489 const __m512 denom = _mm512_add_ps(
490 _mm512_set1_ps(1.0f), recurrent_ggml_expf_avx512(neg));
491 _mm512_storeu_ps(out_row + col, _mm512_div_ps(xv, denom));
493#elif defined(__AVX2__) && defined(__FMA__)
494 for (; col + 8 <= dim; col += 8) {
495 const __m256 xv = _mm256_loadu_ps(x_row + col);
496 const __m256 neg = _mm256_sub_ps(_mm256_setzero_ps(), xv);
497 const __m256 denom = _mm256_add_ps(
498 _mm256_set1_ps(1.0f), recurrent_ggml_expf_avx2(neg));
499 _mm256_storeu_ps(out_row + col, _mm256_div_ps(xv, denom));
502 float (*
volatile llama_expf)(float) = expf;
503 for (; col < dim; ++col) {
504 const float xv = x_row[col];
505 out_row[col] = xv / (1.0f + llama_expf(-xv));
514 if (!x || !out || rows <= 0 || dim <= 0) {
518 ck_recurrent_silu_ggml_args_t args = {x, out, dim};
519 const size_t elements = (size_t)rows * (
size_t)dim;
520 const size_t minimum_elements_per_thread = 65536u;
521 if (rows < 2 || elements < 2u * minimum_elements_per_thread) {
528 size_t useful_threads =
529 (elements + minimum_elements_per_thread - 1u) /
530 minimum_elements_per_thread;
531 if ((
size_t)active > useful_threads) active = (
int)useful_threads;
532 if (active > rows) active = rows;
536 pool, active, 0, rows, 1,
547 float (*
volatile llama_expf)(float) = expf;
548 for (
int row = 0; row < rows; ++row) {
549 const float *x_row = x + (size_t) row * (
size_t) dim;
550 float *out_row = out + (size_t) row * (
size_t) dim;
551 for (
int col = 0; col < dim; ++col) {
552 out_row[col] = 1.0f / (1.0f + llama_expf(-x_row[col]));
562 for (
int row = 0; row < rows; ++row) {
563 const float *d_out_row = d_out + (size_t) row * (
size_t) dim;
564 const float *x_row = x + (size_t) row * (
size_t) dim;
565 float *d_x_row = d_x + (size_t) row * (
size_t) dim;
566 for (
int col = 0; col < dim; ++col) {
567 const float xv = x_row[col];
569 d_x_row[col] = d_out_row[col] * (sig + xv * sig * (1.0f - sig));
static uint16_t float_to_bf16(float f)
static float bf16_to_float(uint16_t v)
Persistent pthread thread pool for CK-Engine inference.
void ck_threadpool_parallel_for_n(ck_threadpool_t *pool, int active_threads, int begin, int end, int grain_size, ck_range_fn_t fn, void *args)
ck_threadpool_t * ck_threadpool_global(void)
int ck_threadpool_thread_id(const ck_threadpool_t *pool)
int ck_threadpool_n_threads(const ck_threadpool_t *pool)
float(* ck_recurrent_libm_f32_fn)(float)
void recurrent_sigmoid_forward_ggml(const float *x, float *out, int rows, int dim)
static ck_recurrent_libm_f32_fn ck_recurrent_llama_logf
void recurrent_silu_forward_pytorch_bf16_input_fp32_output(const float *x, float *out, 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)
static float recurrent_softplus(float x)
void recurrent_sigmoid_forward_pytorch_bf16_input_fp32_output(const float *x, float *out, int rows, int dim)
void recurrent_silu_forward_ggml(const float *x, float *out, int rows, int dim)
static void * ck_recurrent_libm_handle
static void recurrent_silu_forward_ggml_rows(int begin, int end, void *opaque)
void recurrent_silu_forward(const float *x, float *out, int rows, int dim)
static ck_recurrent_libm_f32_fn ck_recurrent_pytorch_log1pf
void recurrent_silu_forward_pytorch_bf16_storage(const float *x, float *out, int rows, int dim)
static void ck_bind_recurrent_llama_libm(void)
static float recurrent_sigmoid(float x)
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 ck_recurrent_libm_f32_fn ck_recurrent_llama_expf
void recurrent_silu_backward(const float *d_out, const float *x, float *d_x, int rows, int dim)
static pthread_once_t ck_recurrent_libm_once
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_forward(const float *alpha, const float *dt_bias, const float *a, float *gate, int rows, int num_heads, int state_dim)
__attribute__((visibility("default"))) CKTokenizer *ck_tokenizer_create(CKTokenizerType type)
const uint8_t size_t count
static void silu(float *x, int n)