Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / kernel_selector / core / common / jitter.h
1 /*
2 // Copyright (c) 2016-2018 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 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #pragma once
19
20 #include "kernel_selector_common.h"
21
22 #include <sstream>
23 #include <cmath>
24
25
26 namespace kernel_selector {
27
28 struct base_params;
29
30 using JitDefinitions = std::vector<std::pair<std::string, std::string>>;
31
32 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
33 // Helpers
34 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
35 template <typename T>
36 inline std::string GetTypeName() { throw std::runtime_error("Implement me"); }
37 template <>
38 inline std::string GetTypeName<int8_t>() { return "char"; }
39 template <>
40 inline std::string GetTypeName<uint8_t>() { return "uchar"; }
41 template <>
42 inline std::string GetTypeName<int16_t>() { return "short"; }
43 template <>
44 inline std::string GetTypeName<uint16_t>() { return "ushort"; }
45 template <>
46 inline std::string GetTypeName<int32_t>() { return "int"; }
47 template <>
48 inline std::string GetTypeName<uint32_t>() { return "uint"; }
49 template <>
50 inline std::string GetTypeName<int64_t>() { return "long"; } 
51 template <>
52 inline std::string GetTypeName<uint64_t>() { return "ulong"; }
53 template <>
54 inline std::string GetTypeName<float>() { return "float"; }
55 template <>
56 inline std::string GetTypeName<double>() { return "double"; }
57
58 std::string toCLType(WeightsType wType);
59 std::string toCLType(Datatype dType);
60 std::string getMeanOpString(MeanOp op);
61
62 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63 // ToCodeString functions
64 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
65 // TODO improve to_code_string specializations
66 template<typename T>
67 std::string toCodeString(T val) { return std::to_string(val); }
68
69 inline std::string toCodeString(const std::string& val) { return val; }
70 inline std::string toCodeString(const char* val) { return val; }
71 inline std::string toCodeString(bool val) { return val ? "1" : "0"; }
72 std::string toCodeString(float val);
73 std::string toCodeString(double val);
74
75 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
76 // JitConstant
77 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
78 template <typename VecT, typename ValT, typename Func>
79 inline std::string toVectorString(const VecT& vec, const std::string& vectorType, size_t maxDim, ValT padFillingVal, Func fetchFunc)
80 {
81     std::stringstream ss;
82     ss << "(" << vectorType << " []){ ";
83     for (size_t i = 0; i < vec.size(); i++)
84         ss << toCodeString(fetchFunc(vec[i])) << ",";
85     for (size_t i = vec.size(); i < maxDim; i++)
86         ss << padFillingVal << ",";
87     ss << " } ";
88     return ss.str();
89 }
90
91 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
92 // JitConstant
93 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
94 class JitConstant 
95 {
96 protected:
97     const std::string _name;
98     JitConstant(const std::string& name):_name(name){}
99
100 public:
101     virtual JitDefinitions GetDefinitions() const = 0;
102     virtual ~JitConstant() {}
103 };
104
105 class simple_jit_constant : public JitConstant 
106 {
107     const std::string _value;
108
109 public:
110     simple_jit_constant(const std::string& name, const std::string& value)
111         : JitConstant(name), _value(value) {}
112
113     JitDefinitions GetDefinitions() const override 
114     {
115         return JitDefinitions{ {_name, _value} };
116     }
117 };
118
119 template<typename T>
120 std::shared_ptr<JitConstant> MakeJitConstant(const std::string& name, T value) 
121 {
122     return std::static_pointer_cast<JitConstant>(std::make_shared<simple_jit_constant>(name, toCodeString(value)));
123 }
124
125 std::shared_ptr<JitConstant> MakeJitConstant(const std::string& name, const struct Tensor::DataTensor& value);
126 std::shared_ptr<JitConstant> MakeJitConstant(const std::string& name, const struct Tensor::WeightsTensor& value);
127
128 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
129 // VectorDataJitConstant
130 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
131 template <typename T>
132 class VectorDataJitConstant : public JitConstant 
133 {
134     const std::vector<T> _data;
135
136 public:
137     VectorDataJitConstant(const std::string& name, const std::vector<T>& data) : JitConstant(name), _data(data) {}
138
139     JitDefinitions GetDefinitions() const override 
140     {
141         JitDefinitions result{
142             { _name + "_SIZE", toCodeString(_data.size()) },
143             { _name, toVectorString(_data, GetTypeName<T>(), _data.size(), 1, [](const T& v) {return v; } ) },
144         };
145         return result;
146     }
147 };
148
149 template <typename T>
150 inline  std::shared_ptr<JitConstant> MakeJitConstant(const std::string& name, const std::vector<T>& value) 
151 {
152     return std::static_pointer_cast<JitConstant>(std::make_shared<VectorDataJitConstant<T>>(name, value));
153 }
154
155 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
156 // Size
157 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
158 template <typename T>
159 class SizeJitConstant : public JitConstant
160 {
161     const Size<T> _size;
162
163 public:
164     SizeJitConstant(const std::string& name, const Size<T>& size) : JitConstant(name), _size(size) {}
165
166     JitDefinitions GetDefinitions() const override
167     {
168         JitDefinitions definitions{
169             { _name + "_SIZE_X",        toCodeString(_size.x) },
170             { _name + "_SIZE_Y",        toCodeString(_size.y) },
171         };
172         return definitions;
173     }
174 };
175
176 template <typename T>
177 inline std::shared_ptr<JitConstant> MakeJitConstant(const std::string& name, const Size<T>& value)
178 {
179     return std::static_pointer_cast<JitConstant>(std::make_shared<SizeJitConstant<T>>(name, value));
180 }
181
182 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
183 // DimTensor
184 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
185 template <typename T>
186 class DimVectorJitConstant : public JitConstant
187 {
188     const DimTensor<T> _dims;
189
190 public:
191     DimVectorJitConstant(const std::string& name, const DimTensor<T>& size) : JitConstant(name), _dims(size) {}
192
193     JitDefinitions GetDefinitions() const override
194     {
195         JitDefinitions definitions{
196             { _name + "_BATCH_NUM",   toCodeString(_dims.b) },
197             { _name + "_FEATURE_NUM", toCodeString(_dims.f) },
198             { _name + "_SIZE_Y",      toCodeString(_dims.y) },
199             { _name + "_SIZE_X",      toCodeString(_dims.x) },
200         };
201         return definitions;
202     }
203 };
204
205 template <typename T>
206 std::shared_ptr<JitConstant> MakeJitConstant(const std::string& name, const DimTensor<T>& value)
207 {
208     return std::make_shared<DimVectorJitConstant<T>>(name, value);
209 }
210
211 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
212 // jit_constants
213 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
214 class JitConstants 
215 {
216     std::vector<std::shared_ptr<JitConstant>> _constants;
217 public:
218     JitConstants(std::initializer_list<std::shared_ptr<JitConstant>> constants) :_constants(constants) {}
219
220     inline void AddConstant(std::shared_ptr<JitConstant> constant)
221     {
222         _constants.push_back(constant);
223     }
224
225     inline void AddConstants(const std::vector<std::shared_ptr<JitConstant>>& constants)
226     {
227         for (const auto& c : constants)
228         {
229             _constants.push_back(c);
230         }
231     }
232
233     inline void Merge(const JitConstants& jit)
234     {
235         AddConstants(jit._constants);
236     }
237
238     JitDefinitions GetDefinitions() const;
239 };
240
241 JitConstants MakeActivationJitConstants(const base_activation_params& params, const std::string& suffix="");
242 JitConstants MakeBaseParamsJitConstants(const base_params& params);
243 JitConstants MakeLoopUnrollParamsJitConstants(uint32_t loopCount);
244 JitConstants MakeUnitTypeJitConstants(Datatype dataType);
245
246 }