← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
linear_rows_q8_0_checked.c
Go to the documentation of this file.
2#include "checked_parallel.h"
5
6#include <stdatomic.h>
7#include <string.h>
8
9struct q8_projection {
10 const unsigned char *weights, *packed;
11 unsigned char *staged;
12 size_t width, channels, row_bytes;
13 atomic_int failed;
14};
15
16static void q8_projection_dot(size_t begin, size_t end, void *opaque)
17{
18 struct q8_projection *task = opaque;
19 while (begin < end) {
20 const size_t row = begin / task->channels;
21 const size_t channel = begin % task->channels;
22 const size_t remaining = task->channels - channel;
23 const size_t count = end - begin < remaining ? end - begin : remaining;
24 const unsigned char *activation = task->packed + row * task->row_bytes;
25 const unsigned char *weight = task->weights + channel * task->row_bytes;
26 unsigned char *output = task->staged + begin * sizeof(float);
27 for (size_t c = 0; c < count; ++c) {
28 const float dot = ck_q8_activation_dot(weight + c * task->row_bytes, activation, task->width);
29 if (!isfinite(dot)) {
30 atomic_store_explicit(&task->failed, 1, memory_order_relaxed);
31 return;
32 }
33 memcpy(output + c * sizeof(dot), &dot, sizeof(dot));
34 }
35 begin += count;
36 }
37}
38
39static int linear_rows_q8_0_checked(const float *input, size_t input_elements,
40 const void *weight, size_t weight_bytes,
41 float *output, size_t output_elements,
42 size_t rows, size_t width, size_t channels,
43 unsigned char *activation, size_t activation_bytes)
44{
45 size_t in, out;
46 if (!width || width % QK8_0 || width / QK8_0 > SIZE_MAX / sizeof(block_q8_0) ||
47 !ck_checked_span(rows, width, width, &in) ||
48 !ck_checked_span(rows, channels, channels, &out) ||
49 input_elements < in || output_elements < out) return -2;
50 const size_t row_bytes = width / QK8_0 * sizeof(block_q8_0);
51 if (channels > SIZE_MAX / row_bytes || weight_bytes != channels * row_bytes) return -2;
52 if (!weight || (uintptr_t)weight > UINTPTR_MAX - weight_bytes ||
53 !ck_checked_region(input, in) || !ck_checked_region(output, out) ||
54 ck_checked_overlap(input, in, output, out)) return -1;
55 const uintptr_t a = (uintptr_t)output, b = (uintptr_t)weight;
56 if (a < b + weight_bytes && b < a + out * sizeof(float)) return -1;
57 size_t scratch_bytes = row_bytes;
58 int stage = 0;
59 if (activation) {
60 if (rows <= (SIZE_MAX - out * sizeof(float)) / row_bytes) {
61 const size_t staged_bytes = rows * row_bytes + out * sizeof(float);
62 if (activation_bytes >= staged_bytes) {
63 scratch_bytes = staged_bytes;
64 stage = 1;
65 }
66 }
67 const uintptr_t scratch = (uintptr_t)activation, src = (uintptr_t)input;
68 if (activation_bytes < scratch_bytes || scratch > UINTPTR_MAX - scratch_bytes) return -2;
69 if ((scratch < src + in * sizeof(float) && src < scratch + scratch_bytes) ||
70 (scratch < a + out * sizeof(float) && a < scratch + scratch_bytes) ||
71 (scratch < b + weight_bytes && b < scratch + scratch_bytes)) return -1;
72 }
73 if (!ck_checked_finite(input, in)) return -3;
74
75 const unsigned char *packed = weight;
76 if (stage) {
77 unsigned char *staged = activation + rows * row_bytes;
78 for (size_t r = 0; r < rows; ++r) {
79 if (ck_pack_q8_activation_rne(input + r * width, activation + r * row_bytes, width))
80 return -3;
81 }
82 struct q8_projection task = {
83 .weights = packed, .packed = activation, .staged = staged,
84 .width = width, .channels = channels, .row_bytes = row_bytes,
85 };
86 atomic_init(&task.failed, 0);
87 if (out >= 8192) ck_checked_parallel_for(out, 128, q8_projection_dot, &task);
88 else q8_projection_dot(0, out, &task);
89 if (atomic_load_explicit(&task.failed, memory_order_relaxed)) return -3;
90 memcpy(output, staged, out * sizeof(float));
91 return 0;
92 }
93 /* Preflight all rows before publishing output. Scratch may change on error. */
94 for (int publish = 0; publish < 2; ++publish) {
95 for (size_t r = 0; r < rows; ++r) {
96 const float *x = input + r * width;
97 if (activation && ck_pack_q8_activation_rne(x, activation, width)) return -3;
98 for (size_t c = 0; c < channels; ++c) {
99 const unsigned char *w = packed + c * row_bytes;
100 const float dot = activation ? ck_q8_activation_dot(w, activation, width)
101 : ck_packed_q8_dot_ordered(x, w, width);
102 if (!isfinite(dot)) return -3;
103 if (publish) output[r * channels + c] = dot;
104 }
105 }
106 }
107 return 0;
108}
109
110int linear_rows_q8_0_checked_f32(const float *input, size_t input_elements,
111 const void *weight, size_t weight_bytes,
112 float *output, size_t output_elements,
113 size_t rows, size_t width, size_t channels)
114{
115 return linear_rows_q8_0_checked(input, input_elements, weight, weight_bytes, output,
116 output_elements, rows, width, channels, NULL, 0);
117}
118
119int linear_rows_q8_0_checked_q8_activation(const float *input, size_t input_elements,
120 const void *weight, size_t weight_bytes,
121 float *output, size_t output_elements,
122 size_t rows, size_t width, size_t channels,
123 unsigned char *activation, size_t activation_bytes)
124{
125 if (!activation) return -1;
126 return linear_rows_q8_0_checked(input, input_elements, weight, weight_bytes, output,
127 output_elements, rows, width, channels, activation, activation_bytes);
128}
static double dot(const float *a, const float *b, size_t n)
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 void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
#define QK8_0
static int publish(const int32_t *indices, float *dl, float *dr, float *scratch, size_t groups, size_t rows, size_t channels, size_t nl, size_t nr)
int linear_rows_q8_0_checked_f32(const float *input, size_t input_elements, const void *weight, size_t weight_bytes, float *output, size_t output_elements, size_t rows, size_t width, size_t channels)
static int linear_rows_q8_0_checked(const float *input, size_t input_elements, const void *weight, size_t weight_bytes, float *output, size_t output_elements, size_t rows, size_t width, size_t channels, unsigned char *activation, size_t activation_bytes)
static void q8_projection_dot(size_t begin, size_t end, void *opaque)
int linear_rows_q8_0_checked_q8_activation(const float *input, size_t input_elements, const void *weight, size_t weight_bytes, float *output, size_t output_elements, size_t rows, size_t width, size_t channels, unsigned char *activation, size_t activation_bytes)
static float ck_q8_activation_dot(const unsigned char *weight, const unsigned char *input, size_t width)
static int ck_pack_q8_activation_rne(const float *input, unsigned char *packed, size_t width)
static float ck_packed_q8_dot_ordered(const float *input, const unsigned char *weight, size_t width)
const uint8_t size_t count
Definition true_bpe.h:211
uint32_t end
Definition utf8.c:215