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
13struct packed_linear_work {
15 const unsigned char *weights;
17 size_t width, channels, row_bytes;
18 float (*
dot)(
const float *,
const unsigned char *, size_t);
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);
35 if (work->publish) work->output[i] = value;
36 if (++channel == work->channels) {
44 LINEAR_ARGS,
size_t block_width,
size_t block_bytes,
45 float (*
dot)(
const float *,
const unsigned char *,
size_t))
48 if (!width || width % block_width || width / block_width > SIZE_MAX / block_bytes ||
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 ||
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;
60 struct packed_linear_work work = {
61 .input = input, .weights = weight, .output = output, .width = width,
62 .channels = channels, .row_bytes = row_bytes, .dot =
dot,
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;
108void gemv_q5_k_q8_k(
float *,
const void *,
const void *,
int,
int);
111struct q8_k_linear_work {
112 const unsigned char *weights;
115 size_t width, channels, weight_row_bytes;
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;
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);
134 LINEAR_ARGS,
float *work,
size_t work_elements,
size_t block_bytes,
size_t weight_alignment,
138 if (width > INT_MAX || channels > INT_MAX || !width || width %
QK_K ||
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) ||
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;
159 unsigned char *packed = (
unsigned char *)work;
160 for (
size_t row = 0; row < rows; ++row) {
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,
170 memcpy(output, task.staged, out *
sizeof(
float));
177 output_elements, rows, width, channels, work, work_elements,
sizeof(
block_q4_K),
184 output_elements, rows, width, channels, work, work_elements,
sizeof(
block_q6_K),
188static void iq2_xs_matvec(
float *out,
const void *weight,
const void *activation,
int rows,
int width)
191 const size_t blocks = (size_t)width /
QK_K;
192 for (
int row = 0; row < rows; ++row)
199 output_elements, rows, width, channels, work, work_elements,
sizeof(
ck_block_iq2_xs),
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.
void gemv_q5_k_q8_k(float *y, const void *W, const void *x_q8, int M, int 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)
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 uint8_t size_t count