8 const void *table,
size_t table_bytes,
float *output,
9 size_t output_elements,
size_t tokens,
size_t vocabulary,
15 if (!width || width % block_width || width / block_width > SIZE_MAX / block_bytes ||
17 tokens > ids_elements ||
extent > output_elements || tokens > SIZE_MAX /
sizeof(int32_t))
return -2;
18 const size_t row_bytes = width / block_width * block_bytes;
19 if (!vocabulary || vocabulary > SIZE_MAX / row_bytes || table_bytes != vocabulary * row_bytes)
21 if (!
ids || (uintptr_t)
ids % _Alignof(int32_t) ||
22 (uintptr_t)
ids > UINTPTR_MAX - tokens *
sizeof(int32_t) ||
24 const uintptr_t dst = (uintptr_t)output;
25 const uintptr_t stop = dst +
extent *
sizeof(float);
26 if ((dst < (uintptr_t)
ids + tokens *
sizeof(int32_t) && (uintptr_t)
ids < stop) ||
27 (dst < (uintptr_t)table + table_bytes && (uintptr_t)table < stop))
return -1;
28 const unsigned char *packed = table;
29 for (
size_t t = 0; t < tokens; ++t) {
30 if (
ids[t] < 0 || (
size_t)
ids[t] >= vocabulary)
return -3;
31 const unsigned char *row = packed + (size_t)
ids[t] * row_bytes;
32 for (
size_t c = 0; c < row_bytes; c += block_bytes) {
33 const unsigned char *p = row + c;
34 const uint16_t bits = (uint16_t)(p[0] | ((uint16_t)p[1] << 8));
35 if ((bits & 0x7c00u) == 0x7c00u)
return -3;
37 const uint16_t min_bits = (uint16_t)(p[2] | ((uint16_t)p[3] << 8));
38 if ((min_bits & 0x7c00u) == 0x7c00u)
return -3;
42 for (
size_t t = 0; t < tokens; ++t) {
43 const unsigned char *p = packed + (size_t)
ids[t] * row_bytes;
44 for (
size_t c = 0; c < width; c += block_width, p += block_bytes) {
51 memcpy(&block, p,
sizeof(block));
56 for (
size_t j = 0; j <
QK8_0; ++j) {
57 const int code = p[2 + j];
58 output[t * width + c + j] = (float)(code < 128 ? code : code - 256) * scale;
67 const void *table,
size_t table_bytes,
float *output,
68 size_t output_elements,
size_t tokens,
size_t vocabulary,
72 output_elements, tokens, vocabulary, width,
CK_DT_Q8_0);
76 const void *table,
size_t table_bytes,
float *output,
77 size_t output_elements,
size_t tokens,
size_t vocabulary,
81 output_elements, tokens, vocabulary, width,
CK_DT_Q4_K);
85 const void *table,
size_t table_bytes,
float *output,
86 size_t output_elements,
size_t tokens,
size_t vocabulary,
90 output_elements, tokens, vocabulary, width,
CK_DT_Q5_K);
94 size_t output_elements,
size_t elements,
float cap)
96 if (!elements || input_elements < elements || output_elements < elements ||
97 !isfinite(cap) || cap <= 0.0f)
return -2;
101 for (
size_t i = 0; i < elements; ++i)
102 output[i] = (
float)((double)cap * tanh((
double)input[i] / (
double)cap));
107 size_t output_elements,
size_t rows,
size_t width)
111 input_elements <
extent || output_elements < width)
return -2;
114 const float *row = input +
extent - width;
116 memcpy(output, row, width *
sizeof(
float));
static int ck_checked_finite(const float *ptr, size_t n)
static int ck_checked_span(size_t rows, size_t width, size_t stride, size_t *span)
static int ck_checked_region(const float *ptr, size_t n)
static int ck_checked_overlap(const float *a, size_t na, const float *b, size_t nb)
static size_t ck_dtype_block_size(CKDataType dt)
Get the number of elements per quantization block.
static size_t ck_dtype_block_bytes(CKDataType dt)
Get bytes per block for quantized types.
CKDataType
Supported data types in C-Kernel-Engine.
void dequant_q5_k_row(const void *src, float *dst, size_t n_elements)
void dequant_q4_k_row(const void *src, float *dst, size_t n_elements)
Dequantize Q4_K row (multiple blocks).
Quantization block structures for weight-only quantization.
static float ck_fp16_to_fp32_soft(ck_half h)
Convert FP16 (ck_half) to FP32 — software implementation.
static int extent(size_t rows, size_t cols, size_t stride, size_t size)
int softcap_logits_checked_f32(const float *input, size_t input_elements, float *output, size_t output_elements, size_t elements, float cap)
int copy_last_row_checked_f32(const float *input, size_t input_elements, float *output, size_t output_elements, size_t rows, size_t width)
int embedding_lookup_q5_k_checked_f32(const int32_t *ids, size_t ids_elements, const void *table, size_t table_bytes, float *output, size_t output_elements, size_t tokens, size_t vocabulary, size_t width)
int embedding_lookup_q4_k_checked_f32(const int32_t *ids, size_t ids_elements, const void *table, size_t table_bytes, float *output, size_t output_elements, size_t tokens, size_t vocabulary, size_t width)
static int embedding_lookup_packed_checked_f32(const int32_t *ids, size_t ids_elements, const void *table, size_t table_bytes, float *output, size_t output_elements, size_t tokens, size_t vocabulary, size_t width, CKDataType type)
int embedding_lookup_q8_0_checked_f32(const int32_t *ids, size_t ids_elements, const void *table, size_t table_bytes, float *output, size_t output_elements, size_t tokens, size_t vocabulary, size_t width)