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

Go to the source code of this file.

Functions

static float finish (float x, int relu, int *bad)
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_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)
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)
static void pointwise (size_t begin, size_t end, void *context)
static void spatial (size_t begin, size_t end, void *context)

Function Documentation

◆ finish()

float finish ( float x,
int relu,
int * bad )
inlinestatic

Definition at line 19 of file image_hwc_conv_checked.c.

20{
21 if (!relu) return x;
22 *bad |= !isfinite(x);
23 return x < 0.0f ? -0.0f : x;
24}

Referenced by pointwise(), and spatial().

◆ image_concat_hwc_f32()

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 )

Definition at line 252 of file image_hwc_conv_checked.c.

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)
const char * left
Definition true_bpe.h:140
const char const char * right
Definition true_bpe.h:141

References ck_checked_overlap(), ck_checked_region(), left, and right.

◆ image_conv_hwc_f32()

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 )

Definition at line 213 of file image_hwc_conv_checked.c.

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}
static void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
static void spatial(size_t begin, size_t end, void *context)
static void pointwise(size_t begin, size_t end, void *context)

References ck_checked_overlap(), ck_checked_parallel_for(), ck_checked_region(), pointwise(), and spatial().

◆ image_nearest2x_hwc_f32()

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 )

Definition at line 235 of file image_hwc_conv_checked.c.

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}

References ck_checked_overlap(), and ck_checked_region().

◆ pointwise()

void pointwise ( size_t begin,
size_t end,
void * context )
static

Definition at line 93 of file image_hwc_conv_checked.c.

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}
static float finish(float x, int relu, int *bad)
int32_t int32_t int32_t int32_t int32_t mask
Definition tokenizer.h:235
uint32_t end
Definition utf8.c:215

References end, finish(), and mask.

Referenced by image_conv_hwc_f32().

◆ spatial()

void spatial ( size_t begin,
size_t end,
void * context )
static

Definition at line 163 of file image_hwc_conv_checked.c.

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}

References end, finish(), and mask.

Referenced by ck_audio_conv2d_whc_grouped_f32_range(), and image_conv_hwc_f32().