10 const unsigned char *weights, *packed;
11 unsigned char *staged;
12 size_t width, channels, row_bytes;
18 struct q8_projection *task = opaque;
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) {
30 atomic_store_explicit(&task->failed, 1, memory_order_relaxed);
33 memcpy(output + c *
sizeof(
dot), &
dot,
sizeof(
dot));
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)
49 input_elements < in || output_elements < out)
return -2;
51 if (channels > SIZE_MAX / row_bytes || weight_bytes != channels * row_bytes)
return -2;
52 if (!weight || (uintptr_t)weight > UINTPTR_MAX - weight_bytes ||
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;
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;
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;
75 const unsigned char *packed = weight;
77 unsigned char *staged = activation + rows * row_bytes;
78 for (
size_t r = 0; r < rows; ++r) {
82 struct q8_projection task = {
83 .weights = packed, .packed = activation, .staged = staged,
84 .width = width, .channels = channels, .row_bytes = row_bytes,
86 atomic_init(&task.failed, 0);
89 if (atomic_load_explicit(&task.failed, memory_order_relaxed))
return -3;
90 memcpy(output, staged, out *
sizeof(
float));
95 for (
size_t r = 0; r < rows; ++r) {
96 const float *x = input + r * width;
98 for (
size_t c = 0; c < channels; ++c) {
99 const unsigned char *w = packed + c * row_bytes;
102 if (!isfinite(
dot))
return -3;
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)
116 output_elements, rows, width, channels, NULL, 0);
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)
125 if (!activation)
return -1;
127 output_elements, rows, width, channels, activation, activation_bytes);
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 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