Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / kernel_selector / core / kernel_selector_common.h
1 /*
2 // Copyright (c) 2016 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #pragma once
18
19 #include "kernel_selector_params.h"
20
21 #include <cfloat>
22 #include <cstdint>
23 #include <limits>
24 #include <memory>
25 #include <sstream>
26 #include <cfloat>
27 #include <string>
28 #include <vector>
29
30 #define AGE_BASED "-cl-no-subgroup-ifp"
31 #define DEFAULT ""
32 #define NO_PRERA_SCH "-cl-intel-no-prera-scheduling"
33
34 namespace kernel_selector {
35
36 #ifndef UNUSED
37 #define UNUSED(a) (void)a
38 #endif
39
40
41 // TODO: current solution until we will have kernel selection time based
42 #define FORCE_PRIORITY_1 (0.0000001f)
43 #define FORCE_PRIORITY_2 (0.0000002f)
44 #define FORCE_PRIORITY_3 (0.0000003f)
45 #define FORCE_PRIORITY_4 (0.0000004f)
46 #define FORCE_PRIORITY_5 (0.0000005f)
47 #define FORCE_PRIORITY_6 (0.0000006f)
48 #define FORCE_PRIORITY_7 (0.0000007f)
49 #define FORCE_PRIORITY_8 (0.0000008f)
50 #define FORCE_PRIORITY_9 (0.0000009f)
51 #define DONT_USE_IF_HAVE_SOMETHING_ELSE (1000000.f)
52 #define TUTORIAL_PRIORITY (DONT_USE_IF_HAVE_SOMETHING_ELSE + 1.f)
53 #define NOT_SUPPORTED (FLT_MAX)
54
55     std::string GetStringEnv(const char* varName);
56
57     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
58     // KernelString
59     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
60     struct KernelString
61     {
62         std::string str;
63         std::string jit;
64         std::string options;
65         std::string entry_point;
66         bool        batch_compilation;
67
68         KernelString() :
69             str(""), jit(""),
70             options(""), entry_point(""),
71             batch_compilation(false)
72         {};
73
74         std::string get_hash()
75         {
76             return str + jit + options + entry_point;
77         }
78     };
79
80     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
81     // WorkGroupSizes
82     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
83     struct WorkGroupSizes
84     {
85         std::vector<size_t> global;
86         std::vector<size_t> local;
87     };
88
89     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90     // Scalar
91     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
92     struct ScalarDescriptor
93     {
94         union ValueT
95         {
96             uint8_t  u8;
97             uint16_t u16;
98             uint32_t u32;
99             uint64_t u64;
100             int8_t   s8;
101             int16_t  s16;
102             int32_t  s32;
103             int64_t  s64;
104             float    f32;
105             double   f64;
106         };
107
108         enum class Types
109         {
110             UINT8,
111             UINT16,
112             UINT32,
113             UINT64,
114             INT8,
115             INT16,
116             INT32,
117             INT64,
118             FLOAT32,
119             FLOAT64,
120         };
121
122         Types t;
123         ValueT v;
124     };
125
126     using Scalars = std::vector<ScalarDescriptor>;
127
128     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
129     // ArgumentDescpirtor
130     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
131     struct ArgumentDescriptor
132     {
133         enum class Types
134         {
135             INPUT,
136             OUTPUT,
137             WEIGHTS,
138             BIAS,
139             PREV_WEIGHTS_GRADIENT,
140             PREV_BIAS_GRADIENT,
141             SCALE_TABLE,
142             SLOPE,
143             SPLIT,
144             INTERNAL_BUFFER,
145             SCALAR,
146             WEIGHTS_QUANTIZATION_FACTORS,
147             OUTPUT_CALIBRATION_FACTORS,
148             RECURRENT, // RNN/LSTM/GRU recurrent weights
149             HIDDEN,    // RNN/LSTM/GRU hidden input
150             CELL,      // LSTM cell input
151             LSTM_PACK, // LSTM packed output
152             LEARNING_RATE
153         };
154
155         enum class ScalarTypes
156         {
157             UINT8,
158             UINT16,
159             UINT32,
160             UINT64,
161             INT8,
162             INT16,
163             INT32,
164             INT64,
165             FLOAT32,
166             FLOAT64,
167         };
168
169         Types t;
170         uint32_t index;
171     };
172
173     using Arguments = std::vector<ArgumentDescriptor>;
174
175     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
176     // clKernelData
177     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
178     struct clKernelData
179     {
180         std::shared_ptr<KernelString>   kernelString;
181         WorkGroupSizes                  workGroups;
182         Arguments                       arguments;
183         Scalars                         scalars;
184         std::string                     layerID;            // TODO: in order to support run single layer. think about more appropriate place
185     };
186
187     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
188     // CPUKernel
189     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
190     struct CPUKernel
191     {
192         virtual WeightsType   GetExpectedInputType() = 0;
193         virtual WeightsLayout GetExpectedInputLayout() const { return WeightsLayout::oiyx; }
194         virtual void Execute(void* input, size_t input_size, void* output, size_t output_size) const = 0;
195     };
196
197     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
198     // GenericKernelParams
199     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
200     struct GenericKernelParams
201     {
202         enum class Engine
203         {
204             NONE,
205             CPU,
206             GPU
207         };
208
209         Engine engine = Engine::NONE;
210         std::shared_ptr<clKernelData> clKernel;
211         std::shared_ptr<CPUKernel> cpuKernel;
212     };
213
214     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
215     // WeightsReorderParams
216     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
217     struct WeightsReorderParams : public GenericKernelParams
218     {
219         size_t newBufferSize = 0;
220         WeightsType dtype = WeightsType::F16;
221         WeightsLayout destLayout = WeightsLayout::oiyx;
222         bool toImageType = false;
223     };
224
225     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
226     // KernelData
227     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
228     struct KernelData
229     {
230         std::shared_ptr<Params> params;
231         std::vector<clKernelData> kernels;
232         std::vector<size_t> internalBufferSizes;
233         float estimatedTime = DONT_USE_IF_HAVE_SOMETHING_ELSE;
234         uint64_t runTime = std::numeric_limits<uint64_t>::max(); // kernel run time in nanoseconds
235
236         bool reorderInput = false;
237         WeightsReorderParams weightsReorderParams;
238         std::string kernelName;
239
240         int autoTuneIndex = -1;
241
242         template <typename T>
243         inline static KernelData Default(const Params& _params, size_t kernel_nums = 1)
244         {
245             KernelData kd;
246             const T& orgParams = static_cast<const T&>(_params);
247             kd.params = std::make_shared<T>(orgParams);
248             kd.kernels.resize(kernel_nums);
249             kd.estimatedTime = DONT_USE_IF_HAVE_SOMETHING_ELSE; // for KW
250             kd.runTime = std::numeric_limits<uint64_t>::max();
251             kd.reorderInput = false; // for KW
252             kd.autoTuneIndex = -1;
253             return kd;
254         }
255     };
256
257     using KernelsData = std::vector<KernelData>;
258
259     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
260     // to string functions
261     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
262     std::string toString(ActivationFunction activation);
263     std::string toString(DataLayout l);
264     std::string toString(Datatype dType);
265     std::string toString(WeightsType wType);
266     std::string toString(KernelType kt);
267     std::string toString(EltwiseMode b_mode);
268     std::string toString(ReorderMode mode);
269     std::string toString(MeanSubtractMode mode);
270     std::string toString(ArgMaxMinOut mode);
271     std::string toString(ArgMaxMinAxis mode);
272     std::string toString(LookUpTableAxis mode);
273     std::string toString(PoolType mode);
274     std::string toString(LRNMode mode);
275     std::string toString(KernelDividerMode mode);
276     std::string toString(SoftmaxDim d);
277     std::string toString(NormalizeMode mode);
278     std::string toString(MVNMode mode);
279     std::string toString(WeightsLayout layout);
280     std::string toString(ConcatAxis a);
281     std::string toString(TileAxis a);
282     std::string toString(GatherAxis a);
283     std::string toString(SampleType type);
284     std::string toString(const BorderType type);
285     std::string toString(const Tensor::Dim& dim);
286     std::string toString(const DataTensor& tensor);
287     std::string toString(const IndexSelectAxis& axis);
288     inline std::uint64_t create_hash(const unsigned char* begin, const unsigned char* end)
289     {
290         // Compatible with VS std::hash.
291         constexpr auto start_acc  = static_cast<std::uint64_t>(UINT64_C(14695981039346656037));
292         constexpr auto mul_factor = static_cast<std::uint64_t>(UINT64_C(1099511628211));
293
294         std::uint64_t acc = start_acc;
295         for (auto elem_it = begin; elem_it != end; ++elem_it)
296         {
297             acc ^= static_cast<std::uint64_t>(*elem_it);
298             acc *= mul_factor;
299         }
300
301         return acc;
302     }
303
304     template <typename ElemTy>
305     std::uint64_t create_hash(const ElemTy* begin, const std::size_t size)
306     {
307         return create_hash(reinterpret_cast<const unsigned char*>(begin), reinterpret_cast<const unsigned char*>(begin + size));
308     }
309
310     template <typename CharTy, typename CharTraits, typename AllocatorTy>
311     std::uint64_t create_hash(const std::basic_string<CharTy, CharTraits, AllocatorTy>& value)
312     {
313         return create_hash<CharTy>(value.data(), value.size());
314     }
315 }