← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
linear_rows_nvfp4_checked.c
Go to the documentation of this file.
2#include "checked_parallel.h"
4
5struct nvfp4_projection {
6 const unsigned char *weights, *packed;
7 float *staged;
8 size_t width, channels, weight_stride, packed_stride;
9};
10
11static void nvfp4_projection_dot(size_t begin, size_t end, void *opaque)
12{
13 const struct nvfp4_projection *p = opaque;
14 while (begin < end) {
15 const size_t row = begin / p->channels;
16 const size_t channel = begin % p->channels;
17 const size_t count = end - begin < p->channels - channel ? end - begin : p->channels - channel;
18 gemv_nvfp4_q8_0(p->staged + begin, p->weights + channel * p->weight_stride, NULL,
19 p->packed + row * p->packed_stride, (int)count, (int)p->width);
20 begin += count;
21 }
22}
23
25 const float *input, size_t input_elements, const void *weight, size_t weight_bytes,
26 float *output, size_t output_elements, size_t rows, size_t width, size_t channels,
27 float *work, size_t work_elements)
28{
29 size_t in, out;
30 if (!width || width % QK_NVFP4 || width > INT_MAX || channels > INT_MAX ||
31 !ck_checked_span(rows, width, width, &in) ||
32 !ck_checked_span(rows, channels, channels, &out) ||
33 input_elements < in || output_elements < out) return -2;
34 const size_t weight_stride = width / QK_NVFP4 * sizeof(block_nvfp4);
35 const size_t packed_stride = width / QK8_0 * sizeof(block_q8_0);
36 if (channels > SIZE_MAX / weight_stride || weight_bytes != channels * weight_stride ||
37 rows > (SIZE_MAX - 3) / packed_stride) return -2;
38 const size_t packed_elements = (rows * packed_stride + 3) / 4;
39 if (packed_elements > work_elements || out > work_elements - packed_elements) return -2;
40 const size_t needed = packed_elements + out;
41 if (!weight || (uintptr_t)weight > UINTPTR_MAX - weight_bytes || !ck_checked_region(input, in) ||
42 !ck_checked_region(output, out) || !ck_checked_region(work, needed) ||
43 ck_checked_overlap(input, in, output, out) || ck_checked_overlap(work, needed, input, in) ||
44 ck_checked_overlap(work, needed, output, out)) return -1;
45 const uintptr_t src = (uintptr_t)weight, dst = (uintptr_t)output, tmp = (uintptr_t)work;
46 if ((dst < src + weight_bytes && src < dst + out * sizeof(float)) ||
47 (tmp < src + weight_bytes && src < tmp + needed * sizeof(float))) return -1;
48 if (!ck_checked_finite(input, in)) return -3;
49 unsigned char *packed = (unsigned char *)work;
50 for (size_t row = 0; row < rows; ++row)
51 if (ck_pack_q8_activation_rne(input + row * width, packed + row * packed_stride, width))
52 return -3;
53 struct nvfp4_projection task = {
54 .weights = weight, .packed = packed, .staged = work + packed_elements, .width = width,
55 .channels = channels, .weight_stride = weight_stride, .packed_stride = packed_stride,
56 };
58 if (!ck_checked_finite(task.staged, out)) return -3;
59 memcpy(output, task.staged, out * sizeof(float));
60 return 0;
61}
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_nvfp4_q8_0(float *output, const void *weights, const float *weight_scales, const void *activations, int rows, int cols)
#define QK_NVFP4
#define QK8_0
int linear_rows_nvfp4_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, float *work, size_t work_elements)
static void nvfp4_projection_dot(size_t begin, size_t end, void *opaque)
static int ck_pack_q8_activation_rne(const float *input, unsigned char *packed, size_t width)
const uint8_t size_t count
Definition true_bpe.h:211
uint32_t end
Definition utf8.c:215