← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
linear_rows_bias_bf16_avx2_panel_checked.c
Go to the documentation of this file.
1#include "bf16_utils.h"
2
3#include <math.h>
4#include <string.h>
5
6#if defined(__AVX2__) && defined(__FMA__)
7#include "checked_parallel.h"
8#include <immintrin.h>
9#include <stdatomic.h>
10
11enum { PANEL = 16 };
12
13struct project_panel {
14 const uint16_t *input, *weight, *bias;
15 uint16_t *staged;
16 double *packed;
17 size_t rows, k, n;
18 atomic_int failed;
19};
20
21static int region(const void *ptr, size_t count, size_t size)
22{
23 return ptr && (uintptr_t)ptr % size == 0 &&
24 count <= (UINTPTR_MAX - (uintptr_t)ptr) / size;
25}
26
27static int overlaps(const void *left, size_t a, const void *right, size_t b)
28{
29 return (uintptr_t)left < (uintptr_t)right+b &&
30 (uintptr_t)right < (uintptr_t)left+a;
31}
32
33static int finite_bf16(const uint16_t *src, size_t n)
34{
35 for (size_t i = 0; i < n; ++i)
36 if ((src[i] & 0x7f80u) == 0x7f80u)
37 return 0;
38 return 1;
39}
40
41static void panel_range(size_t begin, size_t end, void *opaque)
42{
43 struct project_panel *p = opaque;
44 for (size_t part = begin; part < end; ++part) {
45 const size_t start = part*PANEL, count = p->n-start < PANEL ? p->n-start : PANEL;
46 double *panel = p->packed+part*p->k*PANEL;
47 for (size_t k = 0; k < p->k; ++k) {
48 for (size_t j = 0; j < count; ++j)
49 panel[k*PANEL+j] = bf16_to_float(p->weight[(start+j)*p->k+k]);
50 for (size_t j = count; j < PANEL; ++j)
51 panel[k*PANEL+j] = 0.0;
52 }
53 for (size_t row = 0; row < p->rows; row += 2) {
54 const uint16_t *x = p->input+row*p->k;
55 const int second = row+1 < p->rows;
56 const uint16_t *y = second ? x+p->k : x;
57 __m256d a0[4], a1[4];
58 for (size_t j = 0; j < 4; ++j)
59 a0[j] = a1[j] = _mm256_setzero_pd();
60 // BF16 products fit FP64 exactly; FMA retains each lane's ascending K sum.
61 for (size_t k = 0; k < p->k; ++k) {
62 const __m256d v0 = _mm256_set1_pd(bf16_to_float(x[k]));
63 const __m256d v1 = _mm256_set1_pd(bf16_to_float(y[k]));
64 for (size_t j = 0; j < 4; ++j) {
65 const __m256d w = _mm256_loadu_pd(panel+k*PANEL+j*4);
66 a0[j] = _mm256_fmadd_pd(v0,w,a0[j]);
67 a1[j] = _mm256_fmadd_pd(v1,w,a1[j]);
68 }
69 }
70 double vals[2][PANEL];
71 for (size_t j = 0; j < 4; ++j) {
72 _mm256_storeu_pd(vals[0]+j*4,a0[j]);
73 _mm256_storeu_pd(vals[1]+j*4,a1[j]);
74 }
75 for (int lane = 0; lane <= second; ++lane) {
76 uint16_t *dst = p->staged+(row+(size_t)lane)*p->n+start;
77 for (size_t j = 0; j < count; ++j) {
78 const uint16_t b = float_to_bf16((float)(vals[lane][j]+bf16_to_float(p->bias[start+j])));
79 if ((b & 0x7f80u) == 0x7f80u) {
80 atomic_store_explicit(&p->failed,1,memory_order_relaxed);
81 return;
82 }
83 dst[j] = b;
84 }
85 }
86 }
87 }
88}
89#endif
90
92 const uint16_t *input, const uint16_t *weight, const uint16_t *bias,
93 uint16_t *output, uint16_t *staged, double *packed, size_t input_elements,
94 size_t weight_elements, size_t bias_elements, size_t output_elements,
95 size_t packed_elements, size_t rows, size_t input_width, size_t output_width)
96{
97#if defined(__AVX2__) && defined(__FMA__)
98 if (!rows || !input_width || !output_width || output_width > SIZE_MAX-(PANEL-1) ||
99 rows > SIZE_MAX/input_width || rows > SIZE_MAX/output_width ||
100 output_width > SIZE_MAX/input_width)
101 return -2;
102 const size_t padded = (output_width+PANEL-1)/PANEL*PANEL;
103 if (padded > SIZE_MAX/input_width || input_elements != rows*input_width ||
104 weight_elements != output_width*input_width || bias_elements != output_width ||
105 output_elements != rows*output_width || packed_elements != padded*input_width)
106 return -2;
107 const void *ptrs[] = {input,weight,bias,output,staged,packed};
108 const size_t sizes[] = {2,2,2,2,2,8};
109 const size_t counts[] = {input_elements,weight_elements,bias_elements,output_elements,output_elements,packed_elements};
110 for (size_t i = 0; i < 6; ++i) {
111 if (!region(ptrs[i],counts[i],sizes[i])) return -1;
112 if (i >= 3)
113 for (size_t j = 0; j < i; ++j)
114 if (overlaps(ptrs[i],counts[i]*sizes[i],ptrs[j],counts[j]*sizes[j])) return -1;
115 }
116 if (!finite_bf16(input,input_elements) || !finite_bf16(weight,weight_elements) ||
117 !finite_bf16(bias,bias_elements))
118 return -3;
119 struct project_panel projection = {input,weight,bias,staged,packed,rows,input_width,output_width,0};
120 ck_checked_parallel_for(padded/PANEL,2,panel_range,&projection);
121 if (atomic_load_explicit(&projection.failed,memory_order_relaxed)) return -3;
122 memcpy(output,staged,output_elements*sizeof(*output));
123 return 0;
124#else
125 (void)input; (void)weight; (void)bias; (void)output; (void)staged; (void)packed;
126 (void)input_elements; (void)weight_elements; (void)bias_elements; (void)output_elements;
127 (void)packed_elements; (void)rows; (void)input_width; (void)output_width;
128 return -4;
129#endif
130}
static int overlaps(const void *a, size_t an, const void *b, size_t bn)
static uint16_t float_to_bf16(float f)
Definition bf16_utils.h:90
static float bf16_to_float(uint16_t v)
Definition bf16_utils.h:38
static void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
static int region(const void *p, size_t n)
int ck_linear_rows_bias_bf16_avx2_panel_checked(const uint16_t *input, const uint16_t *weight, const uint16_t *bias, uint16_t *output, uint16_t *staged, double *packed, size_t input_elements, size_t weight_elements, size_t bias_elements, size_t output_elements, size_t packed_elements, size_t rows, size_t input_width, size_t output_width)
static int finite_bf16(const uint16_t *p, size_t n)
const char * left
Definition true_bpe.h:140
const uint8_t size_t count
Definition true_bpe.h:211
const char const char * right
Definition true_bpe.h:141
uint32_t end
Definition utf8.c:215
uint32_t start
Definition utf8.c:214