← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
linear_rows_packed_k_checked.c File Reference
#include "checked_float_buffer.h"
#include "checked_parallel.h"
#include "checked_q4_k_activation.h"
#include "packed_iq2_xs_dot.h"
#include "packed_k_dot_ordered.h"
#include <stdatomic.h>

Go to the source code of this file.

Macros

#define LINEAR_ARGS

Typedefs

typedef void(* packed_matvec_fn) (float *, const void *, const void *, int, int)

Functions

void gemv_q5_k_q8_k (float *, const void *, const void *, int, int)
void gemv_q6_k_q8_k_checked_cpu (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_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)
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))
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_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)
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_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_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)
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)
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_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)
static void packed_k_matvec_ranges (size_t begin, size_t end, void *opaque)
static void packed_linear_outputs (size_t begin, size_t end, void *opaque)

Macro Definition Documentation

◆ LINEAR_ARGS

#define LINEAR_ARGS
Value:
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

Definition at line 9 of file linear_rows_packed_k_checked.c.

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

Referenced by linear_rows_iq2_xs_q8_k_checked_f32(), linear_rows_packed_checked(), linear_rows_packed_k_q8_k_checked(), linear_rows_q3_k_checked_f32(), linear_rows_q4_k_checked_f32(), linear_rows_q4_k_q8_k_checked_f32(), linear_rows_q5_1_checked_f32(), linear_rows_q5_k_checked_f32(), linear_rows_q5_k_q8_k_checked_f32(), linear_rows_q6_k_checked_f32(), and linear_rows_q6_k_q8_k_checked_f32().

Typedef Documentation

◆ packed_matvec_fn

typedef void(* packed_matvec_fn) (float *, const void *, const void *, int, int)

Definition at line 110 of file linear_rows_packed_k_checked.c.

Function Documentation

◆ gemv_q5_k_q8_k()

void gemv_q5_k_q8_k ( float * y,
const void * W,
const void * x_q8,
int M,
int K )

Definition at line 1007 of file gemm_kernels_q5_k.c.

1011{
1012#if defined(__AVX512F__)
1013 /* TODO: AVX-512 implementation */
1014 gemv_q5_k_q8_k_ref(y, W, x_q8, M, K);
1015#elif defined(__AVX2__)
1016 /* TODO: AVX-2 implementation */
1017 gemv_q5_k_q8_k_ref(y, W, x_q8, M, K);
1018#elif defined(__AVX__)
1019 /* TODO: AVX implementation */
1020 gemv_q5_k_q8_k_ref(y, W, x_q8, M, K);
1021#elif defined(__SSE4_1__)
1022 /* TODO: SSE4.1 implementation */
1023 gemv_q5_k_q8_k_ref(y, W, x_q8, M, K);
1024#else
1025 gemv_q5_k_q8_k_ref(y, W, x_q8, M, K);
1026#endif
1027}
void gemv_q5_k_q8_k_ref(float *y, const void *W, const void *x_q8, int M, int K)

Referenced by gemm_q5_k_q8_k_compact_rows4().

◆ gemv_q6_k_q8_k_checked_cpu()

void gemv_q6_k_q8_k_checked_cpu ( float * out,
const void * weights,
const void * input,
int rows,
int width )

Definition at line 810 of file gemm_kernels_q6k_q8k.c.

811{
812#if defined(CK_Q6_RUNTIME_AVX2)
813 if (__builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma") &&
814 __builtin_cpu_supports("f16c")) {
815 const block_q6_K *blocks = weights;
816 const size_t per_row = (size_t)width / QK_K;
817 for (int row = 0; row < rows; ++row)
818 out[row] = dot_q6_k_q8_k_avx2(blocks + (size_t)row * per_row, input, width);
819 return;
820 }
821#endif
822 gemv_q6_k_q8_k_ref(out, weights, input, rows, width);
823}
#define QK_K
void gemv_q6_k_q8_k_ref(float *y, const void *W, const void *x_q8, int M, int K)

References gemv_q6_k_q8_k_ref(), and QK_K.

Referenced by linear_rows_q6_k_q8_k_checked_f32().

◆ iq2_xs_matvec()

void iq2_xs_matvec ( float * out,
const void * weight,
const void * activation,
int rows,
int width )
static

Definition at line 188 of file linear_rows_packed_k_checked.c.

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}
static float ck_dot_iq2_xs_q8_k(size_t blocks, const ck_block_iq2_xs *weights, const block_q8_K *input)

References ck_dot_iq2_xs_q8_k(), and QK_K.

Referenced by linear_rows_iq2_xs_q8_k_checked_f32().

◆ linear_rows_iq2_xs_q8_k_checked_f32()

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 )

Definition at line 196 of file linear_rows_packed_k_checked.c.

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}
static void iq2_xs_matvec(float *out, const void *weight, const void *activation, int rows, int width)
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)

References iq2_xs_matvec(), LINEAR_ARGS, and linear_rows_packed_k_q8_k_checked().

◆ linear_rows_packed_checked()

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

Definition at line 43 of file linear_rows_packed_k_checked.c.

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}
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)
static void packed_linear_outputs(size_t begin, size_t end, void *opaque)

References ck_checked_finite(), ck_checked_overlap(), ck_checked_parallel_for(), ck_checked_region(), ck_checked_span(), dot(), LINEAR_ARGS, and packed_linear_outputs().

Referenced by linear_rows_q3_k_checked_f32(), linear_rows_q4_k_checked_f32(), linear_rows_q5_1_checked_f32(), linear_rows_q5_k_checked_f32(), and linear_rows_q6_k_checked_f32().

◆ linear_rows_packed_k_q8_k_checked()

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

Definition at line 133 of file linear_rows_packed_k_checked.c.

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}
static int ck_pack_q4_input_q8_k(const float *input, unsigned char *packed, size_t width)
static void packed_k_matvec_ranges(size_t begin, size_t end, void *opaque)

References ck_checked_finite(), ck_checked_overlap(), ck_checked_parallel_for(), ck_checked_region(), ck_checked_span(), ck_pack_q4_input_q8_k(), LINEAR_ARGS, packed_k_matvec_ranges(), and QK_K.

Referenced by linear_rows_iq2_xs_q8_k_checked_f32(), linear_rows_q4_k_q8_k_checked_f32(), linear_rows_q5_k_q8_k_checked_f32(), and linear_rows_q6_k_q8_k_checked_f32().

◆ linear_rows_q3_k_checked_f32()

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 )

Definition at line 72 of file linear_rows_packed_k_checked.c.

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}
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))
static float ck_packed_q3_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)

References ck_packed_q3_k_dot_ordered(), LINEAR_ARGS, linear_rows_packed_checked(), and QK_K.

◆ linear_rows_q4_k_checked_f32()

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 )

Definition at line 79 of file linear_rows_packed_k_checked.c.

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}
static float ck_packed_q4_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)

References ck_packed_q4_k_dot_ordered(), LINEAR_ARGS, linear_rows_packed_checked(), and QK_K.

◆ linear_rows_q4_k_q8_k_checked_f32()

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 )

Definition at line 174 of file linear_rows_packed_k_checked.c.

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}
void gemv_q4_k_q8_k_avx2(float *, const void *, const void *, int, int)

References gemv_q4_k_q8_k_avx2(), LINEAR_ARGS, and linear_rows_packed_k_q8_k_checked().

◆ linear_rows_q5_1_checked_f32()

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 )

Definition at line 86 of file linear_rows_packed_k_checked.c.

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}
#define QK5_1
static float ck_packed_q5_1_dot_ordered(const float *input, const unsigned char *weight, size_t width)

References ck_packed_q5_1_dot_ordered(), LINEAR_ARGS, linear_rows_packed_checked(), and QK5_1.

◆ linear_rows_q5_k_checked_f32()

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 )

Definition at line 100 of file linear_rows_packed_k_checked.c.

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}
static float ck_packed_q5_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)

References ck_packed_q5_k_dot_ordered(), LINEAR_ARGS, linear_rows_packed_checked(), and QK_K.

◆ linear_rows_q5_k_q8_k_checked_f32()

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 )

Definition at line 203 of file linear_rows_packed_k_checked.c.

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

References CK_DT_Q5_K, ck_dtype_block_bytes(), gemv_q5_k_q8_k(), LINEAR_ARGS, and linear_rows_packed_k_q8_k_checked().

◆ linear_rows_q6_k_checked_f32()

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 )

Definition at line 93 of file linear_rows_packed_k_checked.c.

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}
static float ck_packed_q6_k_dot_ordered(const float *input, const unsigned char *weight, size_t width)

References ck_packed_q6_k_dot_ordered(), LINEAR_ARGS, linear_rows_packed_checked(), and QK_K.

◆ linear_rows_q6_k_q8_k_checked_f32()

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 )

Definition at line 181 of file linear_rows_packed_k_checked.c.

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}
void gemv_q6_k_q8_k_checked_cpu(float *out, const void *weights, const void *input, int rows, int width)

References gemv_q6_k_q8_k_checked_cpu(), LINEAR_ARGS, and linear_rows_packed_k_q8_k_checked().

◆ packed_k_matvec_ranges()

void packed_k_matvec_ranges ( size_t begin,
size_t end,
void * opaque )
static

Definition at line 119 of file linear_rows_packed_k_checked.c.

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}
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

References count, end, left, and QK_K.

Referenced by linear_rows_packed_k_q8_k_checked().

◆ packed_linear_outputs()

void packed_linear_outputs ( size_t begin,
size_t end,
void * opaque )
static

Definition at line 23 of file linear_rows_packed_k_checked.c.

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}

References end.

Referenced by linear_rows_packed_checked().