26 #ifdef FIXED_POINT_POSITION 29 #define MAX_OP(x, y, type, size) MAX_OP_EXPAND(x, y, type, size) 30 #define ADD_OP(x, y, type, size) ADD_SAT_OP_EXPAND((x), (y), type, size) 31 #define SUB_OP(x, y, type, size) SUB_SAT_OP_EXPAND((x), (y), type, size) 32 #define MUL_OP(x, y, type, size) MUL_SAT_OP_EXPAND((x), (y), type, size, FIXED_POINT_POSITION) 33 #define DIV_OP(x, y, type, size) DIV_SAT_OP_VEC_EXPAND((x), (y), type, size, FIXED_POINT_POSITION) 34 #define EXP_OP(x, type, size) EXP_OP_EXPAND((x), type, size, FIXED_POINT_POSITION) 36 #define MIN_VAL_EXPAND(type) type##_MIN 37 #define MIN_VAL(type) MIN_VAL_EXPAND(type) 38 #define MINVAL MIN_VAL(DATA_TYPE) 39 #define SELECT_DATA_TYPE EXPAND(DATA_TYPE) 43 #define MAX_OP(x, y, type, size) max((x), (y)) 44 #define ADD_OP(x, y, type, size) ((x) + (y)) 45 #define SUB_OP(x, y, type, size) ((x) - (y)) 46 #define MUL_OP(x, y, type, size) ((x) * (y)) 47 #define DIV_OP(x, y, type, size) ((x) / (y)) 48 #define EXP_OP(x, type, size) exp((x)) 51 #define MINVAL -HALF_MAX 52 #define SELECT_DATA_TYPE short 54 #define MINVAL -FLT_MAX 55 #define SELECT_DATA_TYPE int 61 #if !defined(GRID_SIZE) 68 __constant uint2
idx__ = (uint2)(0, 1);
70 #elif VECTOR_SIZE == 4 72 __constant uint4
idx__ = (uint4)(0, 1, 2, 3);
74 #elif VECTOR_SIZE == 8 76 __constant uint8
idx__ = (uint8)(0, 1, 2, 3, 4, 5, 6, 7);
79 #define VECTOR_SIZE 16 80 #define LOG_VECTOR_SIZE 4 82 __constant uint16
idx__ = (uint16)(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
87 __constant uint16
idx16 = (uint16)(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
88 __constant uint4
idx4 = (uint4)(0, 1, 2, 3);
130 DATA_TYPE sum_val = *((__global DATA_TYPE *)
offset(&sum, 0, get_global_id(1)));
132 data = vload16(0, (__global DATA_TYPE *)
offset(&src, 0, 0));
133 vstore16(
DIV_OP(data, sum_val, DATA_TYPE, 16), 0, (__global DATA_TYPE *)
offset(&dst, 0, 0));
202 for(uint i = 0; i < width_; i++)
209 #ifdef NON_MULTIPLE_OF_VECTOR_SIZE 218 #if VECTOR_SIZE == 16 219 max_val_vec.s01234567 =
MAX_OP(max_val_vec.s01234567, max_val_vec.s89ABCDEF, DATA_TYPE, 8);
222 max_val_vec.s0123 =
MAX_OP(max_val_vec.s0123, max_val_vec.s4567, DATA_TYPE, 4);
225 max_val_vec.s01 =
MAX_OP(max_val_vec.s01, max_val_vec.s23, DATA_TYPE, 2);
227 max_val_vec.s0 =
MAX_OP(max_val_vec.s0, max_val_vec.s1, DATA_TYPE, 1);
229 *((__global DATA_TYPE *)maxo.
ptr) = max_val_vec.s0;
234 DATA_TYPE max_val = *((__global DATA_TYPE *)
offset(&maxo, 0, 0));
241 for(uint i = 0; i < width_; i++)
251 (data, 0, (__global DATA_TYPE *)
offset(&dst, i << LOG_VECTOR_SIZE, 0));
255 #ifdef NON_MULTIPLE_OF_VECTOR_SIZE 264 data = select(0, data, widx);
266 (data, 0, (__global DATA_TYPE *)
offset(&dst, width_ << LOG_VECTOR_SIZE, 0));
271 #if VECTOR_SIZE == 16 272 sum1D.s01234567 =
ADD_OP(sum1D.s01234567, sum1D.s89ABCDEF, DATA_TYPE, 8);
275 sum1D.s0123 =
ADD_OP(sum1D.s0123, sum1D.s4567, DATA_TYPE, 4);
278 sum1D.s01 =
ADD_OP(sum1D.s01, sum1D.s23, DATA_TYPE, 2);
280 sum1D.s0 =
ADD_OP(sum1D.s0, sum1D.s1, DATA_TYPE, 1);
283 *((__global DATA_TYPE *)sum.
ptr) = sum1D.s0;
340 const uint lid = get_local_id(0);
350 __local DATA_TYPE max_local;
358 const uint width_ = row >> 2;
361 for(; i < width_; i++)
365 max_val_vec =
MAX_OP(data_max, max_val_vec, DATA_TYPE, 4);
367 #ifdef NON_MULTIPLE_OF_GRID_SIZE 369 int boundary_workitems = (width % (
GRID_SIZE * 4)) / 4;
370 if(lid < boundary_workitems)
374 max_val_vec =
MAX_OP(data_max, max_val_vec, DATA_TYPE, 4);
376 #ifdef NON_MULTIPLE_OF_VECTOR_SIZE 377 if(boundary_workitems == 0)
382 if(lid == (boundary_workitems - 1))
389 max_val_vec =
MAX_OP(max_val_vec, select(
type_min_, data_max, widx), DATA_TYPE, 4);
393 tmp_local[lid] = max_val_vec;
395 barrier(CLK_LOCAL_MEM_FENCE);
401 tmp_local[lid] =
MAX_OP(tmp_local[lid + 128], tmp_local[lid], DATA_TYPE, 4);
403 barrier(CLK_LOCAL_MEM_FENCE);
409 tmp_local[lid] =
MAX_OP(tmp_local[lid + 64], tmp_local[lid], DATA_TYPE, 4);
411 barrier(CLK_LOCAL_MEM_FENCE);
417 tmp_local[lid] =
MAX_OP(tmp_local[lid + 32], tmp_local[lid], DATA_TYPE, 4);
419 barrier(CLK_LOCAL_MEM_FENCE);
425 tmp_local[lid] =
MAX_OP(tmp_local[lid + 16], tmp_local[lid], DATA_TYPE, 4);
427 barrier(CLK_LOCAL_MEM_FENCE);
433 tmp_local[lid] =
MAX_OP(tmp_local[lid + 8], tmp_local[lid], DATA_TYPE, 4);
435 barrier(CLK_LOCAL_MEM_FENCE);
441 tmp_local[lid] =
MAX_OP(tmp_local[lid + 4], tmp_local[lid], DATA_TYPE, 4);
443 barrier(CLK_LOCAL_MEM_FENCE);
449 tmp_local[lid] =
MAX_OP(tmp_local[lid + 2], tmp_local[lid], DATA_TYPE, 4);
451 barrier(CLK_LOCAL_MEM_FENCE);
455 max_val_vec =
MAX_OP(tmp_local[lid + 1], tmp_local[lid], DATA_TYPE, 4);
456 max_val_vec.s01 =
MAX_OP(max_val_vec.s01, max_val_vec.s23, DATA_TYPE, 2);
457 max_val_vec.s0 =
MAX_OP(max_val_vec.s0, max_val_vec.s1, DATA_TYPE, 1);
458 max_local = max_val_vec.s0;
460 barrier(CLK_LOCAL_MEM_FENCE);
467 DATA_TYPE max_val = max_local;
470 for(i = 0; i < width_; i++)
474 data =
SUB_OP(data, max_val, DATA_TYPE, 4);
478 data =
EXP_OP(data, DATA_TYPE, 4);
480 (data, 0, (__global DATA_TYPE *)
offset(&dst, i * GRID_SIZE * 4, 0));
481 sum1D =
ADD_OP(sum1D, data, DATA_TYPE, 4);
483 #ifdef NON_MULTIPLE_OF_GRID_SIZE 484 boundary_workitems = (width % (
GRID_SIZE * 4)) / 4;
485 if(lid < boundary_workitems)
489 data =
SUB_OP(data, max_val, DATA_TYPE, 4);
493 data =
EXP_OP(data, DATA_TYPE, 4);
495 (data, 0, (__global DATA_TYPE *)
offset(&dst, i * GRID_SIZE * 4, 0));
496 sum1D =
ADD_OP(sum1D, data, DATA_TYPE, 4);
498 #ifdef NON_MULTIPLE_OF_VECTOR_SIZE 499 if(boundary_workitems == 0)
504 if(lid == (boundary_workitems - 1))
509 data =
SUB_OP(data, max_val, DATA_TYPE, 4);
513 data =
EXP_OP(data, DATA_TYPE, 4);
516 data = select(0, data, widx);
518 (data, 0, (__global DATA_TYPE *)
offset(&dst, (
GRID_SIZE * i * 4) + 4, 0));
519 sum1D =
ADD_OP(sum1D, data, DATA_TYPE, 4);
523 tmp_local[lid] = sum1D;
525 barrier(CLK_LOCAL_MEM_FENCE);
531 tmp_local[lid] =
ADD_OP(tmp_local[lid + 128], tmp_local[lid], DATA_TYPE, 4);
533 barrier(CLK_LOCAL_MEM_FENCE);
539 tmp_local[lid] =
ADD_OP(tmp_local[lid + 64], tmp_local[lid], DATA_TYPE, 4);
541 barrier(CLK_LOCAL_MEM_FENCE);
547 tmp_local[lid] =
ADD_OP(tmp_local[lid + 32], tmp_local[lid], DATA_TYPE, 4);
549 barrier(CLK_LOCAL_MEM_FENCE);
555 tmp_local[lid] =
ADD_OP(tmp_local[lid + 16], tmp_local[lid], DATA_TYPE, 4);
557 barrier(CLK_LOCAL_MEM_FENCE);
563 tmp_local[lid] =
ADD_OP(tmp_local[lid + 8], tmp_local[lid], DATA_TYPE, 4);
565 barrier(CLK_LOCAL_MEM_FENCE);
571 tmp_local[lid] =
ADD_OP(tmp_local[lid + 4], tmp_local[lid], DATA_TYPE, 4);
573 barrier(CLK_LOCAL_MEM_FENCE);
579 tmp_local[lid] =
ADD_OP(tmp_local[lid + 2], tmp_local[lid], DATA_TYPE, 4);
581 barrier(CLK_LOCAL_MEM_FENCE);
585 sum1D =
ADD_OP(tmp_local[lid + 1], tmp_local[lid], DATA_TYPE, 4);
587 sum1D.s01 =
ADD_OP(sum1D.s01, sum1D.s23, DATA_TYPE, 2);
588 sum1D.s0 =
ADD_OP(sum1D.s0, sum1D.s1, DATA_TYPE, 1);
589 *((__global DATA_TYPE *)sum.
ptr) = sum1D.s0;
#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(name)
#define EXP_OP(x, type, size)
#define MAX_OP(x, y, type, size)
DATA_TYPE sum(__global const DATA_TYPE *input)
Calculate sum of a vector.
__constant DATA_TYPE16 type_min_
#define ADD_OP(x, y, type, size)
#define DIV_OP(x, y, type, size)
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
__kernel void softmax_layer_norm(__global uchar *src_ptr, uint src_stride_x, uint src_step_x, uint src_stride_y, uint src_step_y, uint src_stride_z, uint src_step_z, uint src_offset_first_element_in_bytes, __global uchar *sum_ptr, uint sum_stride_x, uint sum_step_x, uint sum_stride_y, uint sum_step_y, uint sum_stride_z, uint sum_step_z, uint sum_offset_first_element_in_bytes, __global uchar *dst_ptr, uint dst_stride_x, uint dst_step_x, uint dst_stride_y, uint dst_step_y, uint dst_stride_z, uint dst_step_z, uint dst_offset_first_element_in_bytes)
Divides all the values of the input tensor by the sum calculated from softmax_layer_shift_exp_sum ker...
__kernel void softmax_layer_max_shift_exp_sum_parallel(__global uchar *src_ptr, uint src_stride_x, uint src_step_x, uint src_stride_y, uint src_step_y, uint src_stride_z, uint src_step_z, uint src_offset_first_element_in_bytes, __global uchar *maxo_ptr, uint maxo_stride_x, uint maxo_step_x, uint maxo_stride_y, uint maxo_step_y, uint maxo_stride_z, uint maxo_step_z, uint maxo_offset_first_element_in_bytes, __global uchar *dst_ptr, uint dst_stride_x, uint dst_step_x, uint dst_stride_y, uint dst_step_y, uint dst_stride_z, uint dst_step_z, uint dst_offset_first_element_in_bytes, __global uchar *sum_ptr, uint sum_stride_x, uint sum_step_x, uint sum_stride_y, uint sum_step_y, uint sum_stride_z, uint sum_step_z, uint sum_offset_first_element_in_bytes, uint width)
Identifies the maximum value across the 1st dimension and shifts the values of the input tensor by th...
#define MUL_OP(x, y, type, size)
Structure to hold Image information.
#define TENSOR3D_DECLARATION(name)
__global uchar * ptr
Pointer to the starting postion of the buffer.
#define CL_VEC_DATA_TYPE(type, size)
#define SUB_OP(x, y, type, size)
#define VEC_DATA_TYPE(type, size)
#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name)
__constant DATA_TYPE16 type_min
convolution configure & src
__kernel void softmax_layer_max_shift_exp_sum_serial(__global uchar *src_ptr, uint src_stride_x, uint src_step_x, uint src_stride_y, uint src_step_y, uint src_stride_z, uint src_step_z, uint src_offset_first_element_in_bytes, __global uchar *maxo_ptr, uint maxo_stride_x, uint maxo_step_x, uint maxo_stride_y, uint maxo_step_y, uint maxo_stride_z, uint maxo_step_z, uint maxo_offset_first_element_in_bytes, __global uchar *dst_ptr, uint dst_stride_x, uint dst_step_x, uint dst_stride_y, uint dst_step_y, uint dst_stride_z, uint dst_step_z, uint dst_offset_first_element_in_bytes, __global uchar *sum_ptr, uint sum_stride_x, uint sum_step_x, uint sum_stride_y, uint sum_step_y, uint sum_stride_z, uint sum_step_z, uint sum_offset_first_element_in_bytes, uint width)
Identifies the maximum value across the 1st dimension and shifts the values of the input tensor by th...