← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
image_hwc_conv_checked.c
Go to the documentation of this file.
2#include "checked_parallel.h"
3#include <float.h>
4#include <stdatomic.h>
5#include <string.h>
6
7#if defined(__AVX2__) || defined(__AVX512F__)
8#include <immintrin.h>
9#endif
10
11struct image_conv {
12 const float *input, *weight, *bias;
13 float *output;
14 size_t ih, iw, ic, ow, oc, kh, kw, sy, sx, py, px;
15 int relu;
16 atomic_int failed;
17};
18
19static inline float finish(float x, int relu, int *bad)
20{
21 if (!relu) return x;
22 *bad |= !isfinite(x);
23 return x < 0.0f ? -0.0f : x;
24}
25
26#if defined(__AVX512F__)
27typedef __m512 image_vec;
28typedef __mmask16 image_mask;
29enum { IMAGE_LANES = 16 };
30#define image_broadcast _mm512_set1_ps
31#define image_fma _mm512_fmadd_ps
32#define image_load _mm512_maskz_loadu_ps
33#define image_load_full _mm512_loadu_ps
34#define image_store_full _mm512_storeu_ps
35
36static inline image_vec finish_vec(image_vec x, image_mask valid, int relu, int *bad)
37{
38 if (relu) {
39 const __m512 mag = _mm512_castsi512_ps(_mm512_and_si512(
40 _mm512_castps_si512(x),_mm512_set1_epi32(0x7fffffff)));
41 *bad |= (valid & _mm512_cmp_ps_mask(mag,_mm512_set1_ps(FLT_MAX),_CMP_NLE_UQ)) != 0;
42 x = _mm512_mask_mov_ps(x,_mm512_cmp_ps_mask(x,_mm512_setzero_ps(),_CMP_LT_OQ),
43 _mm512_set1_ps(-0.0f));
44 }
45 return x;
46}
47
48static inline void write16(float *dst, image_vec x, image_mask valid, int relu, int *bad)
49{
50 _mm512_mask_storeu_ps(dst,valid,finish_vec(x,valid,relu,bad));
51}
52
53static inline __mmask16 lanes(size_t n)
54{
55 return n >= 16 ? (__mmask16)0xffff : (__mmask16)((1u<<n)-1);
56}
57#elif defined(__AVX2__) && defined(__FMA__)
58typedef __m256 image_vec;
59typedef __m256i image_mask;
60enum { IMAGE_LANES = 8 };
61#define image_broadcast _mm256_set1_ps
62#define image_fma _mm256_fmadd_ps
63#define image_load_full _mm256_loadu_ps
64#define image_store_full _mm256_storeu_ps
65
66static inline image_mask lanes(size_t n)
67{
68 return _mm256_cmpgt_epi32(_mm256_set1_epi32((int)n),_mm256_setr_epi32(0,1,2,3,4,5,6,7));
69}
70
71static inline image_vec image_load(image_mask mask, const float *src)
72{
73 return _mm256_maskload_ps(src,mask);
74}
75
76static inline image_vec finish_vec(image_vec x, image_mask valid, int relu, int *bad)
77{
78 if (relu) {
79 const __m256 mag = _mm256_andnot_ps(_mm256_set1_ps(-0.0f),x);
80 const __m256 nonfinite = _mm256_cmp_ps(mag,_mm256_set1_ps(FLT_MAX),_CMP_NLE_UQ);
81 *bad |= _mm256_movemask_ps(_mm256_and_ps(nonfinite,_mm256_castsi256_ps(valid))) != 0;
82 x = _mm256_blendv_ps(x,_mm256_set1_ps(-0.0f),_mm256_cmp_ps(x,_mm256_setzero_ps(),_CMP_LT_OQ));
83 }
84 return x;
85}
86
87static inline void write16(float *dst, image_vec x, image_mask valid, int relu, int *bad)
88{
89 _mm256_maskstore_ps(dst,valid,finish_vec(x,valid,relu,bad));
90}
91#endif
92
93static void pointwise(size_t begin, size_t end, void *context)
94{
95 struct image_conv *a = context;
96 int bad = 0;
97 size_t p = begin;
98#if defined(__AVX512F__) || (defined(__AVX2__) && defined(__FMA__))
99 for (; end-p >= 4; p += 4) {
100 const float *x = a->input+p*a->ic;
101 float *y = a->output+p*a->oc;
102 size_t c = 0;
103 for (; c < a->oc; c += IMAGE_LANES) {
104 const image_mask mask = lanes(a->oc-c);
105 const image_vec b = image_load(mask,a->bias+c);
106 image_vec s0=b,s1=b,s2=b,s3=b;
107 for (size_t k = 0; k < a->ic; ++k) {
108 const image_vec w = image_load(mask,a->weight+k*a->oc+c);
109 s0 = image_fma(image_broadcast(x[k]),w,s0);
110 s1 = image_fma(image_broadcast(x[k+a->ic]),w,s1);
111 s2 = image_fma(image_broadcast(x[k+2*a->ic]),w,s2);
112 s3 = image_fma(image_broadcast(x[k+3*a->ic]),w,s3);
113 }
114 write16(y+c,s0,mask,a->relu,&bad);
115 write16(y+c+a->oc,s1,mask,a->relu,&bad);
116 write16(y+c+2*a->oc,s2,mask,a->relu,&bad);
117 write16(y+c+3*a->oc,s3,mask,a->relu,&bad);
118 }
119 }
120#endif
121 for (; p < end; ++p)
122 for (size_t c = 0; c < a->oc; ++c) {
123 float v = a->bias[c];
124 for (size_t k = 0; k < a->ic; ++k)
125 v = fmaf(a->input[p*a->ic+k],a->weight[k*a->oc+c],v);
126 a->output[p*a->oc+c] = finish(v,a->relu,&bad);
127 }
128 if (bad) atomic_store_explicit(&a->failed,1,memory_order_relaxed);
129}
130
131#if defined(__AVX512F__) || (defined(__AVX2__) && defined(__FMA__))
132static inline image_vec spatial_row3(image_vec v, const float *in, const float *w,
133 size_t ic, size_t oc, int broadcast)
134{
135 for (size_t x=0; x<3; ++x) {
136 const float *src = in+x*ic;
137 const image_vec value = broadcast ? image_broadcast(*src) : image_load_full(src);
138 v = image_fma(value,image_load_full(w+x*oc),v);
139 }
140 return v;
141}
142
143static inline size_t spatial_interior3(const struct image_conv *a, const float *in, float *out,
144 int broadcast, int *bad)
145{
146 const size_t row = a->iw*a->ic;
147 const image_mask full = lanes(IMAGE_LANES);
148 size_t c = 0;
149 for (; a->oc-c >= IMAGE_LANES; c += IMAGE_LANES) {
150 const float *src = in+(broadcast ? 0 : c);
151 const float *w = a->weight+c;
152 image_vec v = image_load_full(a->bias+c);
153 // Keep bias-first, row-major tap order, with one FP32 FMA per tap.
154 v = spatial_row3(v,src,w,a->ic,a->oc,broadcast);
155 v = spatial_row3(v,src+row,w+3*a->oc,a->ic,a->oc,broadcast);
156 v = spatial_row3(v,src+2*row,w+6*a->oc,a->ic,a->oc,broadcast);
157 image_store_full(out+c,finish_vec(v,full,a->relu,bad));
158 }
159 return c;
160}
161#endif
162
163static void spatial(size_t begin, size_t end, void *context)
164{
165 struct image_conv *a = context;
166 int bad = 0;
167 for (size_t p=begin; p < end; ++p) {
168 const int y0=(int)(p/a->ow*a->sy)-(int)a->py, x0=(int)(p%a->ow*a->sx)-(int)a->px;
169 size_t c = 0;
170#if defined(__AVX512F__) || (defined(__AVX2__) && defined(__FMA__))
171 if (a->kh==3 && a->kw==3 && a->oc>=IMAGE_LANES &&
172 y0>=0 && x0>=0 && (size_t)y0+2<a->ih && (size_t)x0+2<a->iw) {
173 const float *in = a->input+((size_t)y0*a->iw+(size_t)x0)*a->ic;
174 float *out = a->output+p*a->oc;
175 c = a->ic==1 ? spatial_interior3(a,in,out,1,&bad)
176 : spatial_interior3(a,in,out,0,&bad);
177 }
178 for (; c < a->oc; c += IMAGE_LANES) {
179 const image_mask mask = lanes(a->oc-c);
180 image_vec v = image_load(mask,a->bias+c);
181 for (size_t ky=0; ky<a->kh; ++ky) {
182 const size_t y=(size_t)(y0+(int)ky);
183 if (y >= a->ih) continue;
184 for (size_t kx=0; kx<a->kw; ++kx) {
185 const size_t x=(size_t)(x0+(int)kx);
186 if (x >= a->iw) continue;
187 const float *in = a->input+(y*a->iw+x)*a->ic;
188 const image_vec value = a->ic==1 ? image_broadcast(*in) : image_load(mask,in+c);
189 v = image_fma(value,image_load(mask,a->weight+(ky*a->kw+kx)*a->oc+c),v);
190 }
191 }
192 write16(a->output+p*a->oc+c,v,mask,a->relu,&bad);
193 }
194#endif
195 for (; c<a->oc; ++c) {
196 float v=a->bias[c];
197 for(size_t ky=0; ky<a->kh; ++ky) {
198 const size_t y=(size_t)(y0+(int)ky);
199 if(y>=a->ih) continue;
200 for(size_t kx=0; kx<a->kw; ++kx) {
201 const size_t x=(size_t)(x0+(int)kx);
202 if(x>=a->iw) continue;
203 const float in=a->input[(y*a->iw+x)*a->ic+(a->ic==1?0:c)];
204 v=fmaf(in,a->weight[(ky*a->kw+kx)*a->oc+c],v);
205 }
206 }
207 a->output[p*a->oc+c]=finish(v,a->relu,&bad);
208 }
209 }
210 if(bad) atomic_store_explicit(&a->failed,1,memory_order_relaxed);
211}
212
213int image_conv_hwc_f32(const float *input, const float *weight, const float *bias, float *output,
214 size_t ih,size_t iw,size_t ic,size_t oh,size_t ow,size_t oc,size_t kh,size_t kw,
215 size_t sy,size_t sx,size_t py,size_t px,size_t groups,size_t relu,size_t ni,size_t nw,size_t no)
216{
217 if (!ih || ih>1024 || !iw || iw>1024 || !ic || ic>1024 || !oc || oc>1024 ||
218 !kh || kh>3 || !kw || kw>3 || !sy || sy>2 || !sx || sx>2 || py>1 || px>1 ||
219 relu>1 || ih+2*py<kh || iw+2*px<kw || oh!=(ih+2*py-kh)/sy+1 || ow!=(iw+2*px-kw)/sx+1)
220 return -1;
221 const int pw = kh==1 && kw==1 && sy==1 && sx==1 && py==0 && px==0 && groups==1;
222 if (!pw && !((groups==1 && ic==1) || (groups==ic && ic==oc))) return -1;
223 if (ni != ih*iw*ic || nw != kh*kw*oc*(pw ? ic : 1) || no != oh*ow*oc ||
224 !ck_checked_region(input,ni) || !ck_checked_region(weight,nw) ||
225 !ck_checked_region(bias,oc) || !ck_checked_region(output,no) ||
226 ck_checked_overlap(output,no,input,ni) || ck_checked_overlap(output,no,weight,nw) ||
227 ck_checked_overlap(output,no,bias,oc))
228 return -1;
229 struct image_conv a={input,weight,bias,output,ih,iw,ic,ow,oc,kh,kw,sy,sx,py,px,(int)relu,0};
231 /* On failure this graph's private activation arena is not published. */
232 return atomic_load_explicit(&a.failed,memory_order_relaxed) ? -1 : 0;
233}
234
235int image_nearest2x_hwc_f32(const float *input,float *output,size_t h,size_t w,size_t c,size_t ni,size_t no)
236{
237 if (!h || h>1024 || !w || w>1024 || !c || c>1024 || ni!=h*w*c || ni>SIZE_MAX/4 || no!=4*ni ||
238 !ck_checked_region(input,ni) || !ck_checked_region(output,no) ||
239 ck_checked_overlap(input,ni,output,no)) return -1;
240 for (size_t y=0; y<h; ++y) {
241 float *top=output+y*4*w*c;
242 for (size_t x=0; x<w; ++x) {
243 const float *src=input+(y*w+x)*c;
244 memcpy(top+2*x*c,src,c*4);
245 memcpy(top+(2*x+1)*c,src,c*4);
246 }
247 memcpy(top+2*w*c,top,2*w*c*4);
248 }
249 return 0;
250}
251
252int image_concat_hwc_f32(const float *left,const float *right,float *output,
253 size_t p,size_t l,size_t r,size_t oc,size_t nl,size_t nr,size_t no)
254{
255 if (!p || p>(1u<<20) || !l || l>512 || !r || r>512 || oc!=l+r ||
256 nl!=p*l || nr!=p*r || no!=p*oc ||
258 !ck_checked_region(output,no) || ck_checked_overlap(left,nl,output,no) ||
259 ck_checked_overlap(right,nr,output,no)) return -1;
260 for (size_t x=0; x<p; ++x) {
261 memcpy(output+x*oc,left+x*l,l*4);
262 memcpy(output+x*oc+l,right+x*r,r*4);
263 }
264 return 0;
265}
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 valid(const uint16_t *p, size_t elements)
static float finish(float x, int relu, int *bad)
static void spatial(size_t begin, size_t end, void *context)
int image_conv_hwc_f32(const float *input, const float *weight, const float *bias, float *output, size_t ih, size_t iw, size_t ic, size_t oh, size_t ow, size_t oc, size_t kh, size_t kw, size_t sy, size_t sx, size_t py, size_t px, size_t groups, size_t relu, size_t ni, size_t nw, size_t no)
static void pointwise(size_t begin, size_t end, void *context)
int image_concat_hwc_f32(const float *left, const float *right, float *output, size_t p, size_t l, size_t r, size_t oc, size_t nl, size_t nr, size_t no)
int image_nearest2x_hwc_f32(const float *input, float *output, size_t h, size_t w, size_t c, size_t ni, size_t no)
int32_t int32_t int32_t int32_t int32_t mask
Definition tokenizer.h:235
const char * left
Definition true_bpe.h:140
const char const char * right
Definition true_bpe.h:141
uint32_t end
Definition utf8.c:215