← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
packed_text_io_checked.c File Reference
#include "checked_float_buffer.h"
#include "ckernel_quant.h"
#include "ckernel_engine.h"
#include <string.h>

Go to the source code of this file.

Functions

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)
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_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)
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_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)
int softcap_logits_checked_f32 (const float *input, size_t input_elements, float *output, size_t output_elements, size_t elements, float cap)

Function Documentation

◆ copy_last_row_checked_f32()

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 )

Definition at line 106 of file packed_text_io_checked.c.

108{
109 size_t extent;
110 if (!ck_checked_span(rows, width, width, &extent) ||
111 input_elements < extent || output_elements < width) return -2;
112 if (!ck_checked_region(input, extent) || !ck_checked_region(output, width) ||
113 ck_checked_overlap(input, extent, output, width)) return -1;
114 const float *row = input + extent - width;
115 if (!ck_checked_finite(row, width)) return -3;
116 memcpy(output, row, width * sizeof(float));
117 return 0;
118}
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 int extent(size_t rows, size_t cols, size_t stride, size_t size)

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), ck_checked_span(), and extent().

◆ embedding_lookup_packed_checked_f32()

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 )
static

Definition at line 7 of file packed_text_io_checked.c.

11{
12 const size_t block_width = ck_dtype_block_size(type);
13 const size_t block_bytes = ck_dtype_block_bytes(type);
14 size_t extent;
15 if (!width || width % block_width || width / block_width > SIZE_MAX / block_bytes ||
16 !ck_checked_span(tokens, width, width, &extent) ||
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)
20 return -2;
21 if (!ids || (uintptr_t)ids % _Alignof(int32_t) ||
22 (uintptr_t)ids > UINTPTR_MAX - tokens * sizeof(int32_t) ||
23 !table || (uintptr_t)table > UINTPTR_MAX - table_bytes || !ck_checked_region(output, extent)) return -1;
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;
36 if (type != CK_DT_Q8_0) {
37 const uint16_t min_bits = (uint16_t)(p[2] | ((uint16_t)p[3] << 8));
38 if ((min_bits & 0x7c00u) == 0x7c00u) return -3;
39 }
40 }
41 }
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) {
45 if (type == CK_DT_Q5_K) {
46 dequant_q5_k_row(p, output + t * width + c, QK_K);
47 continue;
48 }
49 if (type == CK_DT_Q4_K) {
50 block_q4_K block;
51 memcpy(&block, p, sizeof(block));
52 dequant_q4_k_row(&block, output + t * width + c, QK_K);
53 continue;
54 }
55 const float scale = ck_fp16_to_fp32_soft((uint16_t)(p[0] | ((uint16_t)p[1] << 8)));
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;
59 }
60 }
61 }
62 return 0;
63}
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.
@ CK_DT_Q4_K
@ CK_DT_Q8_0
@ CK_DT_Q5_K
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).
static float ck_fp16_to_fp32_soft(ck_half h)
Convert FP16 (ck_half) to FP32 — software implementation.
#define QK8_0
#define QK_K
const int32_t * ids
Definition tokenizer.h:447

References ck_checked_region(), ck_checked_span(), CK_DT_Q4_K, CK_DT_Q5_K, CK_DT_Q8_0, ck_dtype_block_bytes(), ck_dtype_block_size(), ck_fp16_to_fp32_soft(), dequant_q4_k_row(), dequant_q5_k_row(), extent(), ids, QK8_0, and QK_K.

Referenced by embedding_lookup_q4_k_checked_f32(), embedding_lookup_q5_k_checked_f32(), and embedding_lookup_q8_0_checked_f32().

◆ embedding_lookup_q4_k_checked_f32()

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 )

Definition at line 75 of file packed_text_io_checked.c.

79{
80 return embedding_lookup_packed_checked_f32(ids, ids_elements, table, table_bytes, output,
81 output_elements, tokens, vocabulary, width, CK_DT_Q4_K);
82}
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)

References CK_DT_Q4_K, embedding_lookup_packed_checked_f32(), and ids.

◆ embedding_lookup_q5_k_checked_f32()

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 )

Definition at line 84 of file packed_text_io_checked.c.

88{
89 return embedding_lookup_packed_checked_f32(ids, ids_elements, table, table_bytes, output,
90 output_elements, tokens, vocabulary, width, CK_DT_Q5_K);
91}

References CK_DT_Q5_K, embedding_lookup_packed_checked_f32(), and ids.

◆ embedding_lookup_q8_0_checked_f32()

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 )

Definition at line 66 of file packed_text_io_checked.c.

70{
71 return embedding_lookup_packed_checked_f32(ids, ids_elements, table, table_bytes, output,
72 output_elements, tokens, vocabulary, width, CK_DT_Q8_0);
73}

References CK_DT_Q8_0, embedding_lookup_packed_checked_f32(), and ids.

◆ softcap_logits_checked_f32()

int softcap_logits_checked_f32 ( const float * input,
size_t input_elements,
float * output,
size_t output_elements,
size_t elements,
float cap )

Definition at line 93 of file packed_text_io_checked.c.

95{
96 if (!elements || input_elements < elements || output_elements < elements ||
97 !isfinite(cap) || cap <= 0.0f) return -2;
98 if (!ck_checked_region(input, elements) || !ck_checked_region(output, elements) ||
99 ck_checked_overlap(input, elements, output, elements)) return -1;
100 if (!ck_checked_finite(input, elements)) return -3;
101 for (size_t i = 0; i < elements; ++i)
102 output[i] = (float)((double)cap * tanh((double)input[i] / (double)cap));
103 return 0;
104}

References ck_checked_finite(), ck_checked_overlap(), and ck_checked_region().