Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / kernel_selector / core / cl_kernels / fused_conv_eltwise_gpu_bfyx_os_iyx_osv16.cl
1 // Copyright (c) 2016-2017 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "include/common.cl"
16 #include "include/data_types.cl"
17
18
19 // ---------------------------------------------------------------------------------------------------------------------
20 // Just-in-time macro definitions:
21 // ---------------------------------------------------------------------------------------------------------------------
22
23 // Required JIT constants:
24 //  - INPUT                - [tensor] Input dimensions (batch, spatial and feature).
25 //  - OUTPUT               - [tensor] Output dimensions (batch, spatial and feature).
26 //  - STRIDE               - [tensor] Stride (only spatial). Factors that describe step size in X or Y dimension of
27 //                           input position of application of convolution filter when next ouput value
28 //                           (step 1 in in X or Y dimension of output) is computed.
29 //  - INPUT0_OFFSET        - [tensor] Offset for the first element
30 //                           initial offset input position of application of convolution filter and output position.
31 //  - FP16_SUPPORTED       - [0/1] Value indicating whether device supports FP16 OpenCL extension (cl_khr_fp16).
32 //  - FP16_UNIT_USED       - [0/1] Value indicating that current kernel should use FP16.
33 //  - UNIT_TYPE            - Type of unit of input/output/weight/bias.
34 //  - UNIT_VAL_ZERO        - Literal of current UNIT_TYPE that represents 0.
35 //  - RELU                 - [0/1] Indicates that ReLU activation function should be used on output.
36 //  - NEGATIVE_SLOPE       - [float] Factor for negative output values (required when ReLU is specified).
37 //
38 //  - SUB_GROUP_SIZE       - [int] Size of used subgroup (SIMD).
39 //  - LEFTOVERS            - [int] Optional parameter, required only when number of ofm is not dividable by SUB_GROUP_SIZE
40 //                           see comment for FEATURES_THREADS_PER_BATCH for more informations
41
42 /*
43 gpu::make_jit_constant("OUTPUT_LIMIT",              output_size),
44 gpu::make_jit_constant("FILTER",                    filter_mem.argument().size),
45 gpu::make_jit_constant("FILTER_ARRAY_NUM",          split),
46 gpu::make_jit_constant("OUTPUT_BLOCK_WIDTH",        _kernel_data.block_width));
47 gpu::make_jit_constant("OUTPUT_BLOCK_HEIGHT",       _kernel_data.block_height));
48 gpu::make_jit_constant("IN_BLOCK_ARRAY_SIZE",       _kernel_data.input_block_array_size));
49 gpu::make_jit_constant("IN_BLOCK_WIDTH",            _kernel_data.input_block_width));
50 gpu::make_jit_constant("PREFETCH",                  _kernel_data.prefetch));
51 if (_kernel_data.leftovers)
52     gpu::make_jit_constant("LEFTOVERS",             _kernel_data.leftovers));
53 */
54
55 // FEATURES_THREADS_PER_BATCH defines how many threads in z-dimension are processing single batch.
56 // ideally, z-dimension of value n should indicate processing of n-th output feature. however, since
57 // threads are stack in groups of SUB_GROUP_SIZE, when number of ofm is not dividable by SUB_GROUP_SIZE
58 // there are dummy threads added in z-dimension in count of LEFTOVERS. We need to take them into consideration
59 // while calculating batch's id (see lines 86-87). Values calculated by dummy threads are discarded at line 210.
60 #ifdef LEFTOVERS
61 #define FEATURES_THREADS_PER_BATCH (FILTER_OFM_NUM + LEFTOVERS)
62 #else
63 #define FEATURES_THREADS_PER_BATCH (FILTER_OFM_NUM)
64 #endif
65
66 __attribute__((intel_reqd_sub_group_size(SUB_GROUP_SIZE)))
67 __attribute__((reqd_work_group_size(1, 1, SUB_GROUP_SIZE)))
68 KERNEL(convolution_gpu_bfyx_os_iyx_osv16)(
69     const __global UNIT_TYPE* input,
70     __global UNIT_TYPE* output,
71     const __global UNIT_TYPE* weights,
72 #if BIAS_TERM
73     const __global UNIT_TYPE* bias,
74 #endif   
75     uint split_idx,
76     const __global UNIT_TYPE* eltw_input) // TODO: removing this parameter cause a performance degradation... :)
77 {
78     const uint oc  = (uint)get_global_id(0) * OUTPUT_BLOCK_WIDTH;  // oc = Output Column
79     const uint or  = (uint)get_global_id(1) * OUTPUT_BLOCK_HEIGHT; // or = Output Row
80     const uint fm  = get_global_id(2);                    // fm = Feature Map = od = Output Depth
81     const uint lid = get_sub_group_local_id();
82
83     uint batch_idx = fm / FEATURES_THREADS_PER_BATCH;
84     uint feature_idx = fm % FEATURES_THREADS_PER_BATCH;
85     uint fmg = feature_idx / SUB_GROUP_SIZE;
86
87     UNIT_TYPE in[IN_BLOCK_ARRAY_SIZE];
88     UNIT_TYPE out[OUTPUT_BLOCK_WIDTH * OUTPUT_BLOCK_HEIGHT];
89     UNIT_TYPE w[PREFETCH];
90     uint in_addr;
91     uint weight_addr = fmg * FILTER_IFM_NUM * FILTER_SIZE_X * FILTER_SIZE_Y * SUB_GROUP_SIZE + lid;
92
93     for(int i = 0; i < (OUTPUT_BLOCK_WIDTH * OUTPUT_BLOCK_HEIGHT); i++) {
94         out[i] = UNIT_VAL_ZERO;
95     }
96
97     uint in_split_offset = split_idx * INPUT0_FEATURE_PITCH * FILTER_IFM_NUM;
98     in_addr = batch_idx * INPUT0_BATCH_PITCH;
99     in_addr += in_split_offset + INPUT0_OFFSET_WITH_PADDING + or * STRIDE_SIZE_Y * INPUT0_Y_PITCH + oc * STRIDE_SIZE_X + lid;
100
101     for(int kd = 0; kd < FILTER_IFM_NUM; kd++)  // _ID = 3, RGB
102     {
103         uint tmp_in_addr = in_addr;
104
105 #if IN_BLOCK_WIDTH % SUB_GROUP_SIZE == 0
106         __attribute__((opencl_unroll_hint(IN_BLOCK_ARRAY_SIZE)))
107         for(uint in_block_pos = 0; in_block_pos < IN_BLOCK_ARRAY_SIZE * SUB_GROUP_SIZE; in_block_pos += SUB_GROUP_SIZE) {
108             // Horizontal position in input block after read.
109             const uint in_block_next_x_pos = in_block_pos % IN_BLOCK_WIDTH + SUB_GROUP_SIZE;
110
111             in[in_block_pos / SUB_GROUP_SIZE] = input[tmp_in_addr + in_block_pos % IN_BLOCK_WIDTH];
112
113             // If we have row break, move to the next row.
114             if (in_block_next_x_pos == IN_BLOCK_WIDTH)
115                 tmp_in_addr += INPUT0_Y_PITCH;
116         }
117 #elif (2 * IN_BLOCK_WIDTH) % SUB_GROUP_SIZE == 0
118         __attribute__((opencl_unroll_hint(IN_BLOCK_ARRAY_SIZE)))
119         for(uint in_block_pos = 0; in_block_pos < IN_BLOCK_ARRAY_SIZE * SUB_GROUP_SIZE; in_block_pos += SUB_GROUP_SIZE) {
120             // Horizontal position in input block after read.
121             const uint in_block_next_x_pos = in_block_pos % IN_BLOCK_WIDTH + SUB_GROUP_SIZE;
122
123             if (in_block_next_x_pos <= IN_BLOCK_WIDTH) { //
124                 in[in_block_pos / SUB_GROUP_SIZE] = input[tmp_in_addr + in_block_pos % IN_BLOCK_WIDTH];
125
126                 // If we have row break, move to the next row.
127                 if (in_block_next_x_pos == IN_BLOCK_WIDTH)
128                     tmp_in_addr += INPUT0_Y_PITCH;
129             }
130             else {
131                 // TODO: Generalize this step to relax IN_BLOCK_WIDTH restrictions.
132                 // Position in sub-group on which new row need to be read.
133                 const uint sg_br_pos = IN_BLOCK_WIDTH - in_block_pos % IN_BLOCK_WIDTH;
134
135                 if (lid < sg_br_pos)
136                     in[in_block_pos / SUB_GROUP_SIZE] = input[tmp_in_addr + in_block_pos % IN_BLOCK_WIDTH];
137                 // We have row break inside sub-group. Need to move to next line.
138                 tmp_in_addr += INPUT0_Y_PITCH;
139                 if (lid >= sg_br_pos)
140                     in[in_block_pos / SUB_GROUP_SIZE] = input[tmp_in_addr - sg_br_pos];
141
142                 // If we have another row break, move to the next row.
143                 if (in_block_next_x_pos == 2 * IN_BLOCK_WIDTH)
144                     tmp_in_addr += INPUT0_Y_PITCH;
145             }
146         }
147 #else
148     #error IN_BLOCK_WIDTH must be multiple of SUB_GROUP_SIZE or half of SUB_GROUP_SIZE. Other scenarios are not currently implemented.
149 #endif
150
151         //move to next filter
152         in_addr += INPUT0_FEATURE_PITCH;
153
154         for(int pf=0; pf<PREFETCH; pf++) {
155             w[pf] = weights[weight_addr]; weight_addr += SUB_GROUP_SIZE;
156         }
157
158         uint wi = 0;
159         uint kr = 0; // kr = Kernel Row
160         LOOP(FILTER_SIZE_Y, kr,  // LOOP is a macro that unrolls the loop.
161         {
162             uint kc = 0; // kc = Kernel Column
163             LOOP(FILTER_SIZE_X, kc,
164             {
165                 //w = weights[weight_addr];
166                 for(uint br=0; br<OUTPUT_BLOCK_HEIGHT; br++) {
167                     for(uint bc=0; bc<OUTPUT_BLOCK_WIDTH; bc++) {
168
169 #if IN_BLOCK_WIDTH != SUB_GROUP_SIZE
170                         //if we fix the programming model, then we could use a nice simple 2d array: val = in[br * STRIDE_SIZE_Y + kr][bc * STRIDE_SIZE_X + kc];
171                         UNIT_TYPE val = intel_sub_group_shuffle( in[(((br * STRIDE_SIZE_Y + kr * DILATION_SIZE_Y) * IN_BLOCK_WIDTH) + (bc * STRIDE_SIZE_X + kc * DILATION_SIZE_X)) / SUB_GROUP_SIZE],
172                                                                     (((br * STRIDE_SIZE_Y + kr * DILATION_SIZE_Y) * IN_BLOCK_WIDTH) + (bc * STRIDE_SIZE_X + kc * DILATION_SIZE_X)) % SUB_GROUP_SIZE);
173 #else
174                         UNIT_TYPE val = intel_sub_group_shuffle( in[br * STRIDE_SIZE_Y + kr * DILATION_SIZE_Y], bc * STRIDE_SIZE_X + kc * DILATION_SIZE_X);
175 #endif
176
177                         out[br * OUTPUT_BLOCK_WIDTH + bc] = mad(w[wi % PREFETCH], val, out[br * OUTPUT_BLOCK_WIDTH + bc]);
178                     }
179                 }
180                 w[wi % PREFETCH] = weights[weight_addr];
181                 weight_addr += SUB_GROUP_SIZE; // weights must be stored in just the right SIMD swizzled format for this to work, see host code for details.
182                 wi++;
183             });
184         });
185         // addr went beyond due to prefetch so move it back to correct location.
186         weight_addr -= PREFETCH * SUB_GROUP_SIZE;
187     }
188
189     uint out_split_offset = split_idx * OUTPUT_FEATURE_PITCH * FILTER_OFM_NUM;
190     uint out_addr = OUTPUT_OFFSET;
191     out_addr += batch_idx * OUTPUT_BATCH_PITCH;
192     out_addr += out_split_offset + feature_idx * OUTPUT_FEATURE_PITCH; // out_addr indices into start of 16 feature maps.
193     out_addr += or * OUTPUT_Y_PITCH + oc;  // offset for the 4x3 block that this workitem is working on;
194
195 #if BIAS_TERM
196     for(uint r = 0; r < OUTPUT_BLOCK_HEIGHT; r++) {
197         for(uint c = 0; c < OUTPUT_BLOCK_WIDTH; c++) {
198 #if BIAS_PER_OUTPUT
199             const unsigned bias_index = feature_idx*OUTPUT_SIZE_X*OUTPUT_SIZE_Y + or*OUTPUT_SIZE_X + oc;
200 #else
201             const unsigned bias_index = feature_idx;
202 #endif
203             out[r * OUTPUT_BLOCK_WIDTH + c] += bias[bias_index];
204         }
205     }
206 #endif
207
208
209     for(uint r = 0; r < OUTPUT_BLOCK_HEIGHT; r++) {
210         for(uint c = 0; c < OUTPUT_BLOCK_WIDTH; c++) {
211             out[r * OUTPUT_BLOCK_WIDTH + c] = ACTIVATION(out[r * OUTPUT_BLOCK_WIDTH + c], NL_M, NL_N);
212         }
213     }
214
215 #if IN_OUT_OPT != 1
216     // eltwise part
217     uint eltw_addr = INPUT1_OFFSET;
218     eltw_addr += batch_idx * INPUT1_BATCH_PITCH;
219     eltw_addr += out_split_offset + feature_idx * INPUT1_FEATURE_PITCH; // eltw_addr indices into start of 16 feature maps.
220     eltw_addr += (or * ELTW_STRIDE_Y) * INPUT1_Y_PITCH + (oc * ELTW_STRIDE_X);  // offset for the 4x3 block that this workitem is working on;
221
222     for(uint r = 0; r < OUTPUT_BLOCK_HEIGHT; r++) {
223         for(uint c = 0; c < OUTPUT_BLOCK_WIDTH; c++) {
224             out[r * OUTPUT_BLOCK_WIDTH + c] += eltw_input[eltw_addr + r * INPUT1_Y_PITCH * ELTW_STRIDE_Y + c * ELTW_STRIDE_X];
225             out[r * OUTPUT_BLOCK_WIDTH + c] = ACTIVATION_ELTW(out[r * OUTPUT_BLOCK_WIDTH + c], NL_M_ELTW, NL_N_ELTW);
226         }
227     }
228     // end of eltwise part
229 #endif
230
231 #ifdef LEFTOVERS
232     if (feature_idx < OUTPUT_FEATURE_NUM)
233 #endif
234     for(uint r = 0; r < OUTPUT_BLOCK_HEIGHT; r++) {
235         if(!(or + r >= OUTPUT_SIZE_Y))
236         {
237             for(uint c = 0; c < OUTPUT_BLOCK_WIDTH; c++) {
238                 // this does a scattered write to 16 different feature maps, so that data within one map is contiguous, thus ready for input to next layer.
239                 if(!(oc + c >= OUTPUT_SIZE_X))
240                 {
241 #if IN_OUT_OPT == 1
242                     out[r * OUTPUT_BLOCK_WIDTH + c] += output[out_addr + r * OUTPUT_Y_PITCH + c];
243                     out[r * OUTPUT_BLOCK_WIDTH + c] = ACTIVATION_ELTW(out[r * OUTPUT_BLOCK_WIDTH + c], NL_M_ELTW, NL_N_ELTW);
244 #endif
245                     output[out_addr + r * OUTPUT_Y_PITCH + c] = out[r * OUTPUT_BLOCK_WIDTH + c];
246                 }
247             }
248         }
249     }
250 }
251
252 #undef FEATURES_THREADS_PER_BATCH