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

Go to the source code of this file.

Functions

int affine_add_u8 (const uint8_t *left, const uint8_t *right, uint8_t *output, size_t n, float left_ratio, float right_ratio, size_t left_zero, size_t right_zero, size_t output_zero)
int affine_dequantize_pc_u8_f32 (const uint8_t *input, float *output, size_t n, size_t positions, size_t channels, float scale, size_t zero)
int affine_pointwise_u8_i8_f32 (const float *input, size_t input_elements, const uint8_t *weights, size_t weight_bytes, const int32_t *corrected_bias, size_t bias_elements, const float *scales, size_t scale_elements, uint8_t *workspace, size_t workspace_bytes, float *output, size_t output_elements, size_t channels, size_t outputs, size_t positions, size_t blocks, float input_scale, size_t input_zero, float output_scale, size_t output_zero)
int affine_pointwise_u8_i8_u8 (const uint8_t *input, size_t input_bytes, const uint8_t *weights, size_t weight_bytes, const int32_t *bias, size_t bias_elements, const float *scales, size_t scale_elements, uint8_t *output, size_t output_bytes, size_t channels, size_t outputs, size_t positions, size_t blocks, size_t output_zero)
int affine_quantize_f32_u8 (const float *input, uint8_t *output, size_t n, float scale, size_t zero)
int affine_spatial_u8_i8_u8 (const uint8_t *input, const uint8_t *weights, const int32_t *bias, const float *scales, uint8_t *output, size_t iw, size_t ih, size_t ic, size_t ow, size_t oh, size_t oc, size_t kh, size_t kw, size_t sy, size_t sx, size_t py, size_t px, size_t input_zero, size_t output_zero, size_t ni, size_t nw, size_t ny)
static uint8_t byte_round (float x, int zero)
static float centered (float value, int zero)
static int nearest_even (float x)
static int overlaps (const void *a, size_t an, const void *b, size_t bn)
static void pointwise_range (size_t begin, size_t end, void *data)
static void spatial_range (size_t begin, size_t end, void *data)
static int valid_region (const void *p, size_t n)

Function Documentation

◆ affine_add_u8()

int affine_add_u8 ( const uint8_t * left,
const uint8_t * right,
uint8_t * output,
size_t n,
float left_ratio,
float right_ratio,
size_t left_zero,
size_t right_zero,
size_t output_zero )

Definition at line 388 of file affine_pointwise_checked.c.

391{
392 if (!valid_region(left,n) || !valid_region(right,n) || !valid_region(output,n) ||
393 overlaps(left,n,output,n) || overlaps(right,n,output,n) ||
394 !isfinite(left_ratio) || !isfinite(right_ratio) || left_ratio <= 0 || right_ratio <= 0 ||
395 left_ratio > FLT_MAX/1024 || right_ratio > FLT_MAX/1024 ||
396 left_zero > 255 || right_zero > 255 || output_zero > 255)
397 return -1;
398 size_t i = 0;
399#if defined(__AVX512F__) && defined(__AVX512VNNI__)
400 for (; n-i >= 16; i += 16) {
401 const __m512i a = _mm512_cvtepu8_epi32(_mm_loadu_si128((const __m128i *)(const void *)(left+i)));
402 const __m512i b = _mm512_cvtepu8_epi32(_mm_loadu_si128((const __m128i *)(const void *)(right+i)));
403 const __m512 l = _mm512_mul_ps(_mm512_cvtepi32_ps(_mm512_sub_epi32(a,_mm512_set1_epi32((int)left_zero))),
404 _mm512_set1_ps(left_ratio));
405 const __m512 r = _mm512_mul_ps(_mm512_cvtepi32_ps(_mm512_sub_epi32(b,_mm512_set1_epi32((int)right_zero))),
406 _mm512_set1_ps(right_ratio));
407 store_u8(output+i,_mm512_add_ps(l,r),(int)output_zero,16);
408 }
409#elif defined(__AVX2__)
410 for (; n-i>=8; i+=8) {
411 const __m256i a=_mm256_cvtepu8_epi32(load_bytes8(left+i));
412 const __m256i b=_mm256_cvtepu8_epi32(load_bytes8(right+i));
413 const __m256 l=_mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_sub_epi32(a,_mm256_set1_epi32((int)left_zero))),
414 _mm256_set1_ps(left_ratio));
415 const __m256 r=_mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_sub_epi32(b,_mm256_set1_epi32((int)right_zero))),
416 _mm256_set1_ps(right_ratio));
417 store_u8_256(output+i,_mm256_add_ps(l,r),(int)output_zero,8);
418 }
419#endif
420 for (; i < n; ++i)
421 output[i] = byte_round((left[i]-(int)left_zero)*left_ratio+
422 (right[i]-(int)right_zero)*right_ratio,(int)output_zero);
423 return 0;
424}
static int valid_region(const void *p, size_t n)
static int overlaps(const void *a, size_t an, const void *b, size_t bn)
static uint8_t byte_round(float x, int zero)
const char * left
Definition true_bpe.h:140
const char const char * right
Definition true_bpe.h:141

References byte_round(), left, overlaps(), right, and valid_region().

◆ affine_dequantize_pc_u8_f32()

int affine_dequantize_pc_u8_f32 ( const uint8_t * input,
float * output,
size_t n,
size_t positions,
size_t channels,
float scale,
size_t zero )

Definition at line 426 of file affine_pointwise_checked.c.

428{
429 if (!positions || positions > (1u<<16) || !channels || channels > 8192 ||
430 !isfinite(scale) || scale <= 0 || scale > FLT_MAX/255 || zero > 255)
431 return -1;
432 if (n != positions*channels) return -1;
433 if (!valid_region(input,n) || !ck_checked_region(output,n) || overlaps(input,n,output,n*4))
434 return -1;
435 for (size_t c = 0; c < channels; ++c)
436 for (size_t p = 0; p < positions; ++p)
437 output[c*positions+p] = (input[p*channels+c]-(int)zero)*scale;
438 return 0;
439}
static int ck_checked_region(const float *ptr, size_t n)

References ck_checked_region(), overlaps(), and valid_region().

◆ affine_pointwise_u8_i8_f32()

int affine_pointwise_u8_i8_f32 ( const float * input,
size_t input_elements,
const uint8_t * weights,
size_t weight_bytes,
const int32_t * corrected_bias,
size_t bias_elements,
const float * scales,
size_t scale_elements,
uint8_t * workspace,
size_t workspace_bytes,
float * output,
size_t output_elements,
size_t channels,
size_t outputs,
size_t positions,
size_t blocks,
float input_scale,
size_t input_zero,
float output_scale,
size_t output_zero )

Definition at line 267 of file affine_pointwise_checked.c.

273{
274 if (!channels || channels > 8192 || channels % 4 || !outputs || outputs > 8192 ||
275 !positions || positions > (1u<<16) || blocks != (outputs + 15)/16 ||
276 input_elements != channels * positions || weight_bytes != blocks * channels * 16 ||
277 bias_elements != blocks * 16 || scale_elements != bias_elements ||
278 workspace_bytes != input_elements || output_elements != outputs * positions ||
279 !isfinite(input_scale) || input_scale <= 0 || !isfinite(output_scale) ||
280 output_scale <= 0 || output_scale > FLT_MAX / 255.0f || input_zero > 255 || output_zero > 255 ||
281 !weights || !workspace || (uintptr_t)weights > UINTPTR_MAX-weight_bytes ||
282 (uintptr_t)workspace > UINTPTR_MAX-workspace_bytes ||
283 !ck_checked_region(input,input_elements) || !ck_checked_region(output,output_elements) ||
284 !ck_checked_region(scales,scale_elements) ||
285 !ck_checked_region((const float *)(const void *)corrected_bias,bias_elements) ||
286 !ck_checked_finite(input,input_elements))
287 return -1;
288 const void *sources[] = {input, weights, corrected_bias, scales};
289 const size_t sizes[] = {input_elements*4, weight_bytes, bias_elements*4, scale_elements*4};
290 for (size_t i = 0; i < 4; ++i)
291 if (overlaps(output,output_elements*4,sources[i],sizes[i]) ||
292 overlaps(workspace,workspace_bytes,sources[i],sizes[i]))
293 return -1;
294 if (overlaps(workspace,workspace_bytes,output,output_elements*4)) return -1;
295 const int64_t max_dot = (int64_t)channels * 255 * 128;
296 for (size_t i = 0; i < bias_elements; ++i)
297 if (!isfinite(scales[i]) || scales[i] < 0 || scales[i] > FLT_MAX / (float)INT32_MAX ||
298 (i < outputs && scales[i] == 0) ||
299 (int64_t)corrected_bias[i] - max_dot < INT32_MIN ||
300 (int64_t)corrected_bias[i] + max_dot > INT32_MAX)
301 return -1;
302 const int iz = (int)input_zero, oz = (int)output_zero;
303 size_t start = 0;
304#if defined(__AVX512F__) && defined(__AVX512VNNI__)
305 const __m512i lane = _mm512_setr_epi32(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
306 const __m512 iscale = _mm512_set1_ps(input_scale);
307 const __m512 ilo = _mm512_set1_ps((float)-iz), ihi = _mm512_set1_ps((float)(255-iz));
308 const __m512i zp = _mm512_set1_epi32(iz);
309 const __m512i istep = _mm512_mullo_epi32(lane, _mm512_set1_epi32((int)channels));
310 start = positions & ~(size_t)15;
311 for (size_t k = 0; k < channels; k += 4) {
312 const float *x = input+k*positions;
313 for (size_t p = 0; p < start; p += 16) {
314 __m512i a = quant16(x+p, iscale, ilo, ihi, zp);
315 const __m512i b = quant16(x+p+positions, iscale, ilo, ihi, zp);
316 const __m512i c = quant16(x+p+2*positions, iscale, ilo, ihi, zp);
317 const __m512i d = quant16(x+p+3*positions, iscale, ilo, ihi, zp);
318 a = _mm512_or_si512(a, _mm512_slli_epi32(b,8));
319 a = _mm512_or_si512(a, _mm512_slli_epi32(c,16));
320 a = _mm512_or_si512(a, _mm512_slli_epi32(d,24));
321 _mm512_i32scatter_epi32(workspace+p*channels+k, istep, a, 1);
322 }
323 }
324#endif
325 for (size_t k = 0; k < channels; ++k)
326 for (size_t p = start; p < positions; ++p)
327 workspace[p*channels+k] = (uint8_t)(nearest_even(centered(input[k*positions+p]/input_scale,iz))+iz);
328
329 struct pointwise_work work = {workspace, weights, corrected_bias, scales, output,
330 channels, outputs, positions, output_scale, oz, NULL};
331 if (positions * channels >= 4096)
332 ck_checked_parallel_for(blocks, 1, pointwise_range, &work);
333 else
334 pointwise_range(0, blocks, &work);
335 return 0;
336}
static float centered(float value, int zero)
static void pointwise_range(size_t begin, size_t end, void *data)
static int nearest_even(float x)
static int ck_checked_finite(const float *ptr, size_t n)
static void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
uint32_t start
Definition utf8.c:214

References centered(), ck_checked_finite(), ck_checked_parallel_for(), ck_checked_region(), nearest_even(), overlaps(), pointwise_range(), and start.

◆ affine_pointwise_u8_i8_u8()

int affine_pointwise_u8_i8_u8 ( const uint8_t * input,
size_t input_bytes,
const uint8_t * weights,
size_t weight_bytes,
const int32_t * bias,
size_t bias_elements,
const float * scales,
size_t scale_elements,
uint8_t * output,
size_t output_bytes,
size_t channels,
size_t outputs,
size_t positions,
size_t blocks,
size_t output_zero )

Definition at line 338 of file affine_pointwise_checked.c.

343{
344 if (!channels || channels > 8192 || channels % 4 || !outputs || outputs > 8192 ||
345 !positions || positions > (1u<<16) || blocks != (outputs+15)/16 ||
346 input_bytes != positions*channels || weight_bytes != blocks*channels*16 ||
347 bias_elements != blocks*16 || scale_elements != bias_elements ||
348 output_bytes != positions*outputs || output_zero > 255 ||
349 !valid_region(input,input_bytes) || !valid_region(weights,weight_bytes) ||
350 !valid_region(output,output_bytes) || !ck_checked_region(scales,scale_elements) ||
351 !ck_checked_region((const float *)(const void *)bias,bias_elements) ||
352 overlaps(output,output_bytes,input,input_bytes) || overlaps(output,output_bytes,weights,weight_bytes) ||
353 overlaps(output,output_bytes,bias,bias_elements*4) || overlaps(output,output_bytes,scales,scale_elements*4))
354 return -1;
355 const int64_t max_dot = (int64_t)channels*255*128;
356 for (size_t i = 0; i < bias_elements; ++i)
357 if (!isfinite(scales[i]) || scales[i] < 0 || scales[i] > FLT_MAX/(float)INT32_MAX ||
358 (i < outputs && scales[i] == 0) ||
359 (int64_t)bias[i]-max_dot < INT32_MIN || (int64_t)bias[i]+max_dot > INT32_MAX)
360 return -1;
361 struct pointwise_work work = {input,weights,bias,scales,NULL,channels,outputs,positions,0,
362 (int)output_zero,output};
363 if (positions*channels >= 4096) ck_checked_parallel_for(blocks,1,pointwise_range,&work);
364 else pointwise_range(0,blocks,&work);
365 return 0;
366}

References ck_checked_parallel_for(), ck_checked_region(), overlaps(), pointwise_range(), and valid_region().

◆ affine_quantize_f32_u8()

int affine_quantize_f32_u8 ( const float * input,
uint8_t * output,
size_t n,
float scale,
size_t zero )

Definition at line 368 of file affine_pointwise_checked.c.

369{
370 if (!ck_checked_region(input,n) || !valid_region(output,n) || !isfinite(scale) ||
371 scale <= 0 || zero > 255 || overlaps(input,n*4,output,n) || !ck_checked_finite(input,n))
372 return -1;
373 size_t i = 0;
374#if defined(__AVX512F__) && defined(__AVX512VNNI__)
375 for (; n-i >= 16; i += 16) {
376 const __m512i q = quant16(input+i,_mm512_set1_ps(scale),_mm512_set1_ps((float)-(int)zero),
377 _mm512_set1_ps((float)(255-zero)),_mm512_set1_epi32((int)zero));
378 _mm_storeu_si128((__m128i *)(void *)(output+i),_mm512_cvtusepi32_epi8(q));
379 }
380#elif defined(__AVX2__)
381 for (; n-i >= 8; i+=8)
382 store_u8_256(output+i,_mm256_div_ps(_mm256_loadu_ps(input+i),_mm256_set1_ps(scale)),(int)zero,8);
383#endif
384 for (; i < n; ++i) output[i] = byte_round(input[i]/scale,(int)zero);
385 return 0;
386}

References byte_round(), ck_checked_finite(), ck_checked_region(), overlaps(), and valid_region().

◆ affine_spatial_u8_i8_u8()

int affine_spatial_u8_i8_u8 ( const uint8_t * input,
const uint8_t * weights,
const int32_t * bias,
const float * scales,
uint8_t * output,
size_t iw,
size_t ih,
size_t ic,
size_t ow,
size_t oh,
size_t oc,
size_t kh,
size_t kw,
size_t sy,
size_t sx,
size_t py,
size_t px,
size_t input_zero,
size_t output_zero,
size_t ni,
size_t nw,
size_t ny )

Definition at line 515 of file affine_pointwise_checked.c.

520{
521 if (!iw || iw>4096 || !ih || ih>4096 || !oc || oc>8192 ||
522 (ic!=1 && ic!=oc) || !kh || kh>16 || !kw || kw>16 || !sy || sy>16 ||
523 !sx || sx>16 || py>16 || px>16 || ih+2*py<kh || iw+2*px<kw ||
524 oh!=(ih+2*py-kh)/sy+1 || ow!=(iw+2*px-kw)/sx+1 || input_zero>255 || output_zero>255)
525 return -1;
526 if (iw*ih > SIZE_MAX/ic || ow*oh > SIZE_MAX/oc ||
527 ni != iw*ih*ic || nw != kh*kw*oc || ny != ow*oh*oc) return -1;
528 if (!valid_region(input,ni) || !valid_region(weights,nw) || !valid_region(output,ny) ||
529 !ck_checked_region((const float *)(const void *)bias,oc) || !ck_checked_region(scales,oc) ||
530 overlaps(output,ny,input,ni) || overlaps(output,ny,weights,nw) ||
531 overlaps(output,ny,bias,oc*4) || overlaps(output,ny,scales,oc*4))
532 return -1;
533 const int64_t max_dot = (int64_t)kh*kw*255*128;
534 for (size_t c = 0; c < oc; ++c)
535 if (!isfinite(scales[c]) || scales[c]<=0 || scales[c]>FLT_MAX/(float)INT32_MAX ||
536 (int64_t)bias[c]-max_dot<INT32_MIN || (int64_t)bias[c]+max_dot>INT32_MAX)
537 return -1;
538 struct spatial_work args = {input,weights,bias,scales,output,iw,ih,ic,ow,oc,kh,kw,sy,sx,py,px,
539 (int)input_zero,(int)output_zero};
540 if (ny >= 32768) ck_checked_parallel_for(ow*oh,32,spatial_range,&args);
541 else spatial_range(0,ow*oh,&args);
542 return 0;
543}
static void spatial_range(size_t begin, size_t end, void *data)

References ck_checked_parallel_for(), ck_checked_region(), overlaps(), spatial_range(), and valid_region().

◆ byte_round()

uint8_t byte_round ( float x,
int zero )
inlinestatic

Definition at line 92 of file affine_pointwise_checked.c.

93{
94 return (uint8_t)(nearest_even(centered(x,zero))+zero);
95}

References centered(), and nearest_even().

Referenced by affine_add_u8(), affine_quantize_f32_u8(), pointwise_range(), and spatial_range().

◆ centered()

float centered ( float value,
int zero )
inlinestatic

Definition at line 25 of file affine_pointwise_checked.c.

26{
27 const float lo = (float)-zero, hi = (float)(255-zero);
28 return value < lo ? lo : value > hi ? hi : value;
29}

Referenced by affine_pointwise_u8_i8_f32(), byte_round(), ck_layernorm_rows_step_bf16_checked(), ck_video_mean_patch_rows_bf16_checked(), layernorm_forward_ggml_exact(), pointwise_range(), and standardize().

◆ nearest_even()

int nearest_even ( float x)
inlinestatic

Definition at line 16 of file affine_pointwise_checked.c.

17{
18 const int base = (int)x;
19 const float delta = x - (float)base;
20 if (delta > 0.5f || (delta == 0.5f && base % 2 != 0)) return base + 1;
21 if (delta < -0.5f || (delta == -0.5f && base % 2 != 0)) return base - 1;
22 return base;
23}

Referenced by affine_pointwise_u8_i8_f32(), byte_round(), and pointwise_range().

◆ overlaps()

int overlaps ( const void * a,
size_t an,
const void * b,
size_t bn )
static

Definition at line 31 of file affine_pointwise_checked.c.

32{
33 return (uintptr_t)a < (uintptr_t)b + bn && (uintptr_t)b < (uintptr_t)a + an;
34}

Referenced by affine_add_u8(), affine_dequantize_pc_u8_f32(), affine_pointwise_u8_i8_f32(), affine_pointwise_u8_i8_u8(), affine_quantize_f32_u8(), affine_spatial_u8_i8_u8(), and ck_linear_rows_bias_bf16_avx2_panel_checked().

◆ pointwise_range()

void pointwise_range ( size_t begin,
size_t end,
void * data )
static

Definition at line 169 of file affine_pointwise_checked.c.

170{
171 const struct pointwise_work *work = data;
172#if defined(__AVXVNNI__) && !(defined(__AVX512F__) && defined(__AVX512VNNI__))
173 if (work->byte_output) {
174 pointwise_byte_vnni256(work,begin,end);
175 return;
176 }
177#endif
178 const uint8_t *workspace = work->workspace, *weights = work->weights;
179 const int32_t *corrected_bias = work->bias;
180 const float *scales = work->scales;
181 float *output = work->output;
182 const size_t channels = work->channels, outputs = work->outputs, positions = work->positions;
183 const float output_scale = work->output_scale;
184 const int oz = work->output_zero;
185#if defined(__AVX512F__) && defined(__AVX512VNNI__)
186 const __m512i lane = _mm512_setr_epi32(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
187#endif
188 for (size_t block = begin; block < end; ++block) {
189 const uint8_t *panel = weights + block*16*channels;
190 const size_t remain = outputs-block*16, lanes = remain < 16 ? remain : 16;
191#if defined(__AVX512F__) && defined(__AVX512VNNI__)
192 const __m512i init = _mm512_loadu_si512(corrected_bias+block*16);
193 const __m512 mul = _mm512_loadu_ps(scales+block*16);
194 const __m512 lo = _mm512_set1_ps((float)-oz), hi = _mm512_set1_ps((float)(255-oz));
195 const __m512 oscale = _mm512_set1_ps(output_scale);
196 const __m512i stride = _mm512_mullo_epi32(_mm512_set1_epi32((int)positions), lane);
197 const __mmask16 valid = (__mmask16)((1u << lanes)-1u);
198 if (work->byte_output) {
199 for (size_t p = 0; p < positions;) {
200 __m512i a = init, b = init, c = init, d = init;
201 const uint8_t *x = workspace+p*channels;
202 const size_t rows = positions-p >= 4 ? 4 : 1;
203 if (rows == 4) {
204 for (size_t k = 0; k < channels; k += 4) {
205 const __m512i w = _mm512_loadu_si512(panel+k*16);
206 a = dot4(a,x+k,w); b = dot4(b,x+k+channels,w);
207 c = dot4(c,x+k+2*channels,w); d = dot4(d,x+k+3*channels,w);
208 }
209 } else {
210 for (size_t k = 0; k < channels; k += 4)
211 a = dot4(a,x+k,_mm512_loadu_si512(panel+k*16));
212 }
213 uint8_t *out = work->byte_output+p*outputs+block*16;
214 store_u8(out,_mm512_mul_ps(_mm512_cvtepi32_ps(a),mul),oz,lanes);
215 if (rows == 4) {
216 store_u8(out+outputs,_mm512_mul_ps(_mm512_cvtepi32_ps(b),mul),oz,lanes);
217 store_u8(out+2*outputs,_mm512_mul_ps(_mm512_cvtepi32_ps(c),mul),oz,lanes);
218 store_u8(out+3*outputs,_mm512_mul_ps(_mm512_cvtepi32_ps(d),mul),oz,lanes);
219 }
220 p += rows;
221 }
222 continue;
223 }
224 size_t p = 0;
225 for (; p + 4 <= positions; p += 4) {
226 __m512i a = init, b = init, c = init, d = init;
227 const uint8_t *x = workspace+p*channels;
228 for (size_t k = 0; k < channels; k += 4) {
229 const __m512i w = _mm512_loadu_si512(panel + k*16);
230 a = dot4(a, x+k, w);
231 b = dot4(b, x+k+channels, w);
232 c = dot4(c, x+k+2*channels, w);
233 d = dot4(d, x+k+3*channels, w);
234 }
235 float *out = output+block*16*positions+p;
236 store_result(out, a, mul, oscale, lo, hi, stride, valid);
237 store_result(out+1, b, mul, oscale, lo, hi, stride, valid);
238 store_result(out+2, c, mul, oscale, lo, hi, stride, valid);
239 store_result(out+3, d, mul, oscale, lo, hi, stride, valid);
240 }
241 for (; p < positions; ++p) {
242 __m512i sum = init;
243 for (size_t k = 0; k < channels; k += 4)
244 sum = dot4(sum, workspace+p*channels+k, _mm512_loadu_si512(panel+k*16));
245 store_result(output+block*16*positions+p, sum, mul, oscale, lo, hi, stride, valid);
246 }
247#else
248 for (size_t p = 0; p < positions; ++p)
249 for (size_t lane = 0; lane < lanes; ++lane) {
250 const size_t oc = block*16+lane;
251 int32_t acc = corrected_bias[oc];
252 for (size_t k = 0; k < channels; ++k) {
253 const unsigned byte = panel[(k/4)*64+lane*4+k%4];
254 const int weight = (int)(byte ^ 128u)-128;
255 acc += workspace[p*channels+k]*weight;
256 }
257 if (work->byte_output)
258 work->byte_output[p*outputs+oc] = byte_round((float)acc*scales[oc],oz);
259 else
260 output[oc*positions+p] = (float)nearest_even(centered((float)acc*scales[oc],oz))*output_scale;
261 }
262#endif
263 }
264}
static int valid(const uint16_t *p, size_t elements)
static int mul(size_t a, size_t b, size_t *out)
uint32_t end
Definition utf8.c:215

References byte_round(), centered(), end, mul(), nearest_even(), and valid().

Referenced by affine_pointwise_u8_i8_f32(), and affine_pointwise_u8_i8_u8().

◆ spatial_range()

void spatial_range ( size_t begin,
size_t end,
void * data )
static

Definition at line 450 of file affine_pointwise_checked.c.

451{
452 const struct spatial_work *a = data;
453 for (size_t p = begin; p < end; ++p) {
454 const int y0 = (int)((p/a->ow)*a->sy)-(int)a->py;
455 const int x0 = (int)((p%a->ow)*a->sx)-(int)a->px;
456 size_t c = 0;
457#if defined(__AVX512F__) && defined(__AVX512VNNI__)
458 for (; a->oc-c >= 16; c += 16) {
459 __m512i acc = _mm512_loadu_si512(a->bias+c);
460 for (size_t ky = 0; ky < a->kh; ++ky) {
461 const size_t y = (size_t)(y0+(int)ky);
462 if (y >= a->ih) continue;
463 for (size_t kx = 0; kx < a->kw; ++kx) {
464 const size_t x = (size_t)(x0+(int)kx);
465 if (x >= a->iw) continue;
466 const uint8_t *in = a->input+(y*a->iw+x)*a->ic;
467 const __m512i values = a->ic == 1 ? _mm512_set1_epi32(*in-a->iz) :
468 _mm512_sub_epi32(_mm512_cvtepu8_epi32(_mm_loadu_si128((const __m128i *)(const void *)(in+c))),
469 _mm512_set1_epi32(a->iz));
470 const uint8_t *w = a->weights+(ky*a->kw+kx)*a->oc+c;
471 const __m512i weight = _mm512_cvtepi8_epi32(_mm_loadu_si128((const __m128i *)(const void *)w));
472 acc = _mm512_add_epi32(acc,_mm512_mullo_epi32(weight,values));
473 }
474 }
475 store_u8(a->output+p*a->oc+c,_mm512_mul_ps(_mm512_cvtepi32_ps(acc),_mm512_loadu_ps(a->scales+c)),a->oz,16);
476 }
477#elif defined(__AVX2__)
478 for (; a->oc-c>=8; c+=8) {
479 __m256i acc=_mm256_loadu_si256((const __m256i *)(const void *)(a->bias+c));
480 for (size_t ky=0; ky<a->kh; ++ky) {
481 const size_t y=(size_t)(y0+(int)ky);
482 if (y>=a->ih) continue;
483 for (size_t kx=0; kx<a->kw; ++kx) {
484 const size_t x=(size_t)(x0+(int)kx);
485 if (x>=a->iw) continue;
486 const uint8_t *in=a->input+(y*a->iw+x)*a->ic;
487 const __m256i values=a->ic==1 ? _mm256_set1_epi32(*in-a->iz) :
488 _mm256_sub_epi32(_mm256_cvtepu8_epi32(load_bytes8(in+c)),_mm256_set1_epi32(a->iz));
489 const __m256i w=_mm256_cvtepi8_epi32(load_bytes8(a->weights+(ky*a->kw+kx)*a->oc+c));
490 acc=_mm256_add_epi32(acc,_mm256_mullo_epi32(values,w));
491 }
492 }
493 store_u8_256(a->output+p*a->oc+c,
494 _mm256_mul_ps(_mm256_cvtepi32_ps(acc),_mm256_loadu_ps(a->scales+c)),a->oz,8);
495 }
496#endif
497 for (; c < a->oc; ++c) {
498 int32_t acc = a->bias[c];
499 for (size_t ky = 0; ky < a->kh; ++ky) {
500 const size_t y = (size_t)(y0+(int)ky);
501 if (y >= a->ih) continue;
502 for (size_t kx = 0; kx < a->kw; ++kx) {
503 const size_t x = (size_t)(x0+(int)kx);
504 if (x >= a->iw) continue;
505 const unsigned b = a->weights[(ky*a->kw+kx)*a->oc+c];
506 const int v = a->input[(y*a->iw+x)*a->ic+(a->ic==1 ? 0 : c)]-a->iz;
507 acc += ((int)(b^128u)-128)*v;
508 }
509 }
510 a->output[p*a->oc+c] = byte_round((float)acc*a->scales[c],a->oz);
511 }
512 }
513}

References byte_round(), and end.

Referenced by affine_spatial_u8_i8_u8().

◆ valid_region()

int valid_region ( const void * p,
size_t n )
static

Definition at line 11 of file affine_pointwise_checked.c.

12{
13 return p && n && (uintptr_t)p <= UINTPTR_MAX-n;
14}

Referenced by affine_add_u8(), affine_dequantize_pc_u8_f32(), affine_pointwise_u8_i8_u8(), affine_quantize_f32_u8(), and affine_spatial_u8_i8_u8().