← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
linear_rows_packed_k_checked.c
Go to the documentation of this file.
2#include "checked_parallel.h"
4#include "packed_iq2_xs_dot.h"
6
7#include <stdatomic.h>
8
9#define LINEAR_ARGS \
10 const float *input, size_t input_elements, const void *weight, size_t weight_bytes, \
11 float *output, size_t output_elements, size_t rows, size_t width, size_t channels
12
13struct packed_linear_work {
14 const float *input;
15 const unsigned char *weights;
16 float *output;
17 size_t width, channels, row_bytes;
18 float (*dot)(const float *, const unsigned char *, size_t);
19 atomic_int failed;
20 int publish;
21};
22
23static void packed_linear_outputs(size_t begin, size_t end, void *opaque)
24{
25 struct packed_linear_work *work = opaque;
26 size_t row = begin / work->channels;
27 size_t channel = begin % work->channels;
28 for (size_t i = begin; i < end; ++i) {
29 const unsigned char *packed = work->weights + channel * work->row_bytes;
30 const float value = work->dot(work->input + row * work->width, packed, work->width);
31 if (!isfinite(value)) {
32 atomic_store_explicit(&work->failed, 1, memory_order_relaxed);
33 return;
34 }
35 if (work->publish) work->output[i] = value;
36 if (++channel == work->channels) {
37 channel = 0;
38 ++row;
39 }
40 }
41}
42
44 LINEAR_ARGS, size_t block_width, size_t block_bytes,
45 float (*dot)(const float *, const unsigned char *, size_t))
46{
47 size_t in, out;
48 if (!width || width % block_width || width / block_width > SIZE_MAX / block_bytes ||
49 !ck_checked_span(rows, width, width, &in) ||
50 !ck_checked_span(rows, channels, channels, &out) ||
51 input_elements < in || output_elements < out) return -2;
52 const size_t row_bytes = width / block_width * block_bytes;
53 if (channels > SIZE_MAX / row_bytes || weight_bytes != channels * row_bytes) return -2;
54 if (!weight || (uintptr_t)weight > UINTPTR_MAX - weight_bytes ||
55 !ck_checked_region(input, in) || !ck_checked_region(output, out) ||
56 ck_checked_overlap(input, in, output, out)) return -1;
57 const uintptr_t dst = (uintptr_t)output, weights = (uintptr_t)weight;
58 if (dst < weights + weight_bytes && weights < dst + out * sizeof(float)) return -1;
59 if (!ck_checked_finite(input, in)) return -3;
60 struct packed_linear_work work = {
61 .input = input, .weights = weight, .output = output, .width = width,
62 .channels = channels, .row_bytes = row_bytes, .dot = dot,
63 };
64 atomic_init(&work.failed, 0);
65 for (work.publish = 0; work.publish < 2; ++work.publish) {
67 if (atomic_load_explicit(&work.failed, memory_order_relaxed)) return -3;
68 }
69 return 0;
70}
71
73{
74 return linear_rows_packed_checked(input, input_elements, weight, weight_bytes, output,
75 output_elements, rows, width, channels, QK_K, sizeof(block_q3_K),
77}
78
80{
81 return linear_rows_packed_checked(input, input_elements, weight, weight_bytes, output,
82 output_elements, rows, width, channels, QK_K, sizeof(block_q4_K),
84}
85
87{
88 return linear_rows_packed_checked(input, input_elements, weight, weight_bytes, output,
89 output_elements, rows, width, channels, QK5_1, sizeof(block_q5_1),
91}
92
94{
95 return linear_rows_packed_checked(input, input_elements, weight, weight_bytes, output,
96 output_elements, rows, width, channels, QK_K, sizeof(block_q6_K),
98}
99
101{
102 return linear_rows_packed_checked(input, input_elements, weight, weight_bytes, output,
103 output_elements, rows, width, channels, QK_K, sizeof(block_q5_K),
105}
106
107void gemv_q6_k_q8_k_checked_cpu(float *, const void *, const void *, int, int);
108void gemv_q5_k_q8_k(float *, const void *, const void *, int, int);
109
110typedef void (*packed_matvec_fn)(float *, const void *, const void *, int, int);
111struct q8_k_linear_work {
112 const unsigned char *weights;
113 const block_q8_K *activations;
114 float *staged;
115 size_t width, channels, weight_row_bytes;
116 packed_matvec_fn matvec;
117};
118
119static void packed_k_matvec_ranges(size_t begin, size_t end, void *opaque)
120{
121 const struct q8_k_linear_work *work = opaque;
122 while (begin < end) {
123 const size_t row = begin / work->channels, channel = begin % work->channels;
124 const size_t left = work->channels - channel, remaining = end - begin;
125 const size_t count = left < remaining ? left : remaining;
126 work->matvec(work->staged + begin, work->weights + channel * work->weight_row_bytes,
127 work->activations + row * (work->width / QK_K),
128 (int)count, (int)work->width);
129 begin += count;
130 }
131}
132
134 LINEAR_ARGS, float *work, size_t work_elements, size_t block_bytes, size_t weight_alignment,
135 packed_matvec_fn matvec)
136{
137 size_t in, out;
138 if (width > INT_MAX || channels > INT_MAX || !width || width % QK_K ||
139 !ck_checked_span(rows, width, width, &in) ||
140 !ck_checked_span(rows, channels, channels, &out) ||
141 input_elements < in || output_elements < out) return -2;
142 const size_t blocks = width / QK_K;
143 const size_t row_bytes = blocks * block_bytes;
144 if (channels > SIZE_MAX / row_bytes || weight_bytes != channels * row_bytes ||
145 rows > SIZE_MAX / sizeof(block_q8_K) / blocks) return -2;
146 const size_t packed_bytes = rows * blocks * sizeof(block_q8_K);
147 const size_t packed_elements = packed_bytes / sizeof(float);
148 if (packed_elements > work_elements || out > work_elements - packed_elements) return -2;
149 const size_t needed = packed_elements + out;
150 if (!weight || (uintptr_t)weight % weight_alignment ||
151 (uintptr_t)weight > UINTPTR_MAX - weight_bytes || !ck_checked_region(input, in) ||
152 !ck_checked_region(output, out) || !ck_checked_region(work, needed) ||
153 ck_checked_overlap(input, in, output, out) || ck_checked_overlap(work, needed, input, in) ||
154 ck_checked_overlap(work, needed, output, out)) return -1;
155 const uintptr_t dst = (uintptr_t)output, weights = (uintptr_t)weight, scratch = (uintptr_t)work;
156 if ((dst < weights + weight_bytes && weights < dst + out * sizeof(float)) ||
157 (scratch < weights + weight_bytes && weights < scratch + needed * sizeof(float))) return -1;
158 if (!ck_checked_finite(input, in)) return -3;
159 unsigned char *packed = (unsigned char *)work;
160 for (size_t row = 0; row < rows; ++row) {
161 if (ck_pack_q4_input_q8_k(input + row * width, packed + row * blocks * sizeof(block_q8_K), width))
162 return -3;
163 }
164 struct q8_k_linear_work task = {
165 .weights = weight, .activations = (const block_q8_K *)work, .staged = work + packed_elements,
166 .width = width, .channels = channels, .weight_row_bytes = row_bytes, .matvec = matvec,
167 };
169 if (!ck_checked_finite(task.staged, out)) return -3;
170 memcpy(output, task.staged, out * sizeof(float));
171 return 0;
172}
173
174int linear_rows_q4_k_q8_k_checked_f32(LINEAR_ARGS, float *work, size_t work_elements)
175{
176 return linear_rows_packed_k_q8_k_checked(input, input_elements, weight, weight_bytes, output,
177 output_elements, rows, width, channels, work, work_elements, sizeof(block_q4_K),
179}
180
181int linear_rows_q6_k_q8_k_checked_f32(LINEAR_ARGS, float *work, size_t work_elements)
182{
183 return linear_rows_packed_k_q8_k_checked(input, input_elements, weight, weight_bytes, output,
184 output_elements, rows, width, channels, work, work_elements, sizeof(block_q6_K),
186}
187
188static void iq2_xs_matvec(float *out, const void *weight, const void *activation, int rows, int width)
189{
190 const ck_block_iq2_xs *packed = weight;
191 const size_t blocks = (size_t)width / QK_K;
192 for (int row = 0; row < rows; ++row)
193 out[row] = ck_dot_iq2_xs_q8_k(blocks, packed + (size_t)row * blocks, activation);
194}
195
196int linear_rows_iq2_xs_q8_k_checked_f32(LINEAR_ARGS, float *work, size_t work_elements)
197{
198 return linear_rows_packed_k_q8_k_checked(input, input_elements, weight, weight_bytes, output,
199 output_elements, rows, width, channels, work, work_elements, sizeof(ck_block_iq2_xs),
200 _Alignof(ck_block_iq2_xs), iq2_xs_matvec);
201}
202
203int linear_rows_q5_k_q8_k_checked_f32(LINEAR_ARGS, float *work, size_t work_elements)
204{
205 return linear_rows_packed_k_q8_k_checked(input, input_elements, weight, weight_bytes, output,
206 output_elements, rows, width, channels, work, work_elements, ck_dtype_block_bytes(CK_DT_Q5_K),
207 _Alignof(uint16_t), gemv_q5_k_q8_k);
208}
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)
void gemv_q4_k_q8_k_avx2(float *, const void *, const void *, int, int)
static int ck_pack_q4_input_q8_k(const float *input, unsigned char *packed, size_t width)
static size_t ck_dtype_block_bytes(CKDataType dt)
Get bytes per block for quantized types.
@ CK_DT_Q5_K
void gemv_q5_k_q8_k(float *y, const void *W, const void *x_q8, int M, int K)
#define QK5_1
#define QK_K
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)
void gemv_q6_k_q8_k_checked_cpu(float *out, const void *weights, const void *input, int rows, int width)
void gemv_q5_k_q8_k(float *, const void *, const void *, int, int)
#define LINEAR_ARGS
static int linear_rows_packed_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, size_t block_width, size_t block_bytes, float(*dot)(const float *, const unsigned char *, size_t))
void gemv_q6_k_q8_k_checked_cpu(float *, const void *, const void *, int, int)
int linear_rows_iq2_xs_q8_k_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, float *work, size_t work_elements)
int linear_rows_q6_k_q8_k_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, float *work, size_t work_elements)
void(* packed_matvec_fn)(float *, const void *, const void *, int, int)
static void iq2_xs_matvec(float *out, const void *weight, const void *activation, int rows, int width)
int linear_rows_q6_k_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)
int linear_rows_q4_k_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)
int linear_rows_q5_1_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)
int linear_rows_q5_k_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 void packed_k_matvec_ranges(size_t begin, size_t end, void *opaque)
int linear_rows_q5_k_q8_k_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, float *work, size_t work_elements)
static int linear_rows_packed_k_q8_k_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, float *work, size_t work_elements, size_t block_bytes, size_t weight_alignment, packed_matvec_fn matvec)
int linear_rows_q4_k_q8_k_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, float *work, size_t work_elements)
int linear_rows_q3_k_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 void packed_linear_outputs(size_t begin, size_t end, void *opaque)
static float ck_dot_iq2_xs_q8_k(size_t blocks, const ck_block_iq2_xs *weights, const block_q8_K *input)
static float ck_packed_q4_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q5_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q5_1_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q3_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)
static float ck_packed_q6_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)
const char * left
Definition true_bpe.h:140
const uint8_t size_t count
Definition true_bpe.h:211
uint32_t end
Definition utf8.c:215