DLPrimitives
definitions.hpp
1 #pragma once
2 #include <stdexcept>
3 #include <string>
4 
5 #if defined(__WIN32) || defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__)
6 # define DLPRIM_WINDOWS
7 # if defined(DLL_EXPORT)
8 # if defined(DLPRIM_SOURCE)
9 # define DLPRIM_API __declspec(dllexport)
10 # else
11 # define DLPRIM_API __declspec(dllimport)
12 # endif
13 # else
14 # define DLPRIM_API
15 # endif
16 #else // ELF BINARIES
17 # define DLPRIM_API __attribute__((visibility("default")))
18 #endif
19 
20 
24 namespace dlprim {
25  namespace json { class value; }
26 
30  class Error : public std::runtime_error {
31  public:
32  Error(std::string const &v) : std::runtime_error(v) {}
33  };
34 
38  class NotImplementedError : public Error {
39  public:
40  NotImplementedError(std::string const &v) : Error(v) {}
41  };
42 
46  class ValidationError : public Error {
47  public:
48  ValidationError(std::string const &v) : Error(v) {}
49  };
50 
54  class BuildError : public Error {
55  public:
56  BuildError(std::string const &msg,std::string const &log) : Error(msg), log_(log) {}
58  std::string const &log() const
59  {
60  return log_;
61  }
62  private:
63  std::string log_;
64  };
65 
66  #define DLPRIM_CHECK(x) \
67  do { if(!(x)) throw ValidationError(std::string("Failed " #x " at " __FILE__ ":") + std::to_string(__LINE__) ); } while(0)
68 
70  enum DataType {
71  double_data = 4 + (0 << 3),
72  int64_data = 4 + (2 << 3),
73  uint64_data = 4 + (3 << 3),
74 
75  float_data = 3 + (0 << 3),
76  int32_data = 3 + (2 << 3),
77  uint32_data = 3 + (3 << 3),
78 
79  half_data = 2 + (0 << 3),
80  bfloat16_data = 2 + (1 << 3),
81  int16_data = 2 + (2 << 3),
82  uint16_data = 2 + (3 << 3),
83 
84  int8_data = 1 + (2 << 3),
85  uint8_data = 1 + (3 << 3),
86  };
87 
90  {
91  return (d >> 3) < 2;
92  }
93 
94  template<typename T>
95  struct TypeTraits;
96 
97  template<>
98  struct TypeTraits<float> { static constexpr DataType data_type = float_data; };
99 
100  template<>
101  struct TypeTraits<uint16_t> { static constexpr DataType data_type = uint16_data; };
102 
103  template<>
104  struct TypeTraits<int16_t> { static constexpr DataType data_type = int16_data; };
105 
106  template<>
107  struct TypeTraits<uint8_t> { static constexpr DataType data_type = uint8_data; };
108 
109  template<>
110  struct TypeTraits<int8_t> { static constexpr DataType data_type = int8_data; };
111 
112 
113  template<>
114  struct TypeTraits<uint64_t> { static constexpr DataType data_type = uint64_data; };
115 
116  template<>
117  struct TypeTraits<int64_t> { static constexpr DataType data_type = int64_data; };
118 
119 
120  template<>
121  struct TypeTraits<double> { static constexpr DataType data_type = double_data; };
122 
123  template<>
124  struct TypeTraits<uint32_t> { static constexpr DataType data_type = int32_data; };
125 
126  template<>
127  struct TypeTraits<int32_t> { static constexpr DataType data_type = int32_data; };
128 
129  inline DataType string_to_data_type(std::string const &s)
130  {
131  if(s == "float" || s=="float32")
132  return float_data;
133  else if(s == "float16" || s=="half")
134  return half_data;
135  else if(s == "bfloat16")
136  return bfloat16_data;
137  else if(s == "int32" || s == "int")
138  return int32_data;
139  else if(s == "int8")
140  return int8_data;
141  else if(s == "uint8")
142  return uint8_data;
143  else if(s == "int16")
144  return int16_data;
145  else if(s == "uint16")
146  return uint16_data;
147  else if(s == "int64")
148  return int64_data;
149  else if(s == "uint64")
150  return uint64_data;
151  throw ValidationError("Unknown data type " + s);
152  }
153  inline std::string data_type_to_string(DataType dt)
154  {
155  switch(dt) {
156  case double_data: return "double";
157  case int64_data: return "int64";
158  case uint64_data: return "uint64";
159 
160  case float_data: return "float";
161  case int32_data: return "int32";
162  case uint32_data: return "uint32";
163 
164  case half_data: return "half";
165  case bfloat16_data: return "bfloat16";
166  case int16_data: return "int16";
167  case uint16_data: return "uint16";
168 
169  case int8_data: return "int8";
170  case uint8_data: return "uint8";
171  default:
172  return "unknown";
173  }
174 
175  }
176  enum DataTypeLimit {
177  dt_min_val,
178  dt_max_val,
179  };
180 
181  inline std::string data_type_to_opencl_numeric_limit(DataType dt,DataTypeLimit lmt)
182  {
184  std::string prefix;
185  switch(dt) {
186  case float_data:
187  case bfloat16_data:
188  prefix="FLT";
189  break;
190  case double_data : prefix="DBL"; break;
191  case half_data: prefix="HALF"; break;
192  default:
193  throw ValidationError("Unsupported type");
194  }
195  switch(lmt) {
196  case dt_min_val: return "(-" + prefix + "_MAX)";
197  case dt_max_val: return prefix + "_MAX";
198  };
199  }
200  else {
201  bool unsig = (dt & (3 << 3)) == (3<<3);
202  std::string prefix;
203  switch(dt) {
204  case int64_data: return "LONG";
205  case uint64_data: return "ULONG";
206 
207  case int32_data: return "INT";
208  case uint32_data: return "UINT";
209 
210  case int16_data: return "SHRT";
211  case uint16_data: return "USHRT";
212 
213  case int8_data: return "CHAR";
214  case uint8_data: return "UCHAR";
215  default:
216  throw NotImplementedError("Unsupported data type");
217  }
218  switch(lmt) {
219  case dt_min_val: return unsig ? "0" : prefix + "_MIN";
220  case dt_max_val: return prefix + "_MAX";
221  };
222  }
223  throw NotImplementedError("Unsupported data type");
224  }
225  inline std::string data_type_to_opencl_type(DataType dt,bool io_type=false)
226  {
227  switch(dt) {
228  case double_data: return "double";
229  case int64_data: return "long";
230  case uint64_data: return "ulong";
231 
232  case float_data: return "float";
233  case int32_data: return "int";
234  case uint32_data: return "uint";
235 
236  case half_data: return "half";
237  case bfloat16_data: return (io_type ? "ushort" : "float" );
238  case int16_data: return "short";
239  case uint16_data: return "ushort";
240 
241  case int8_data: return "char";
242  case uint8_data: return "uchar";
243  default:
244  throw NotImplementedError("Unsupported data type");
245  }
246  }
247 
248  constexpr int size_of_data_type(DataType d)
249  {
250  return 1 << ((d & 0x7) - 1);
251  }
252 
254  static constexpr int max_tensor_dim = 5;
255 
257  constexpr int forward_data = 1;
259  constexpr int backward_data = 2;
261  constexpr int backward_param = 3;
262 
266  enum class StandardActivations : int {
267  identity = 0,
268  relu = 1,
269  tanh = 2,
270  sigmoid = 3,
271  relu6 = 4,
272  };
273 
274  StandardActivations activation_from_name(std::string const &name);
275  char const *activation_to_name(StandardActivations act);
276  std::string activation_equation(StandardActivations act,std::string const &variable);
277  std::string activation_backward_equation(StandardActivations act,std::string const &dy,std::string const &y);
278 
279 
283  enum class CalculationsMode {
284  train,
285  predict
286  };
287 
288 
292  enum class GemmOpMode {
293  forward = 1,
294  backward_filter = 2,
295  backward_data = 3
296  };
297 
302  int channels_in = -1;
303  int channels_out = -1;
304  int kernel[2] = {1,1};
305  int stride[2] = {1,1};
306  int dilate[2] = {1,1};
307  int pad[2] = {0,0};
308  int groups = 1;
309  };
310 
311 
312 
313 }
315 
constexpr int backward_data
internal flag
Definition: definitions.hpp:259
Thrown if OpenCL kernel compilation failed.
Definition: definitions.hpp:54
CalculationsMode
Operation mode of layers - inference of training.
Definition: definitions.hpp:283
Convolution settings.
Definition: definitions.hpp:301
Base dlprim excetion.
Definition: definitions.hpp:30
static constexpr int max_tensor_dim
Maximal number of dimensions in tensor.
Definition: definitions.hpp:254
DataType
type definition
Definition: definitions.hpp:70
This class is central representation of json objects.
Definition: json.hpp:652
Definition: definitions.hpp:95
Thrown in case of invalid parameters.
Definition: definitions.hpp:46
Mane namespace.
Definition: context.hpp:9
bool is_floating_point_data_type(DataType d)
returns true of data is double, float, half or bfloat16 type
Definition: definitions.hpp:89
GemmOpMode
internal GEMM mode
Definition: definitions.hpp:292
StandardActivations
Parameterless Activations that can be embedded to general kernels like inner product or convolution...
Definition: definitions.hpp:266
Thrown if some stuff is not implemented yet.
Definition: definitions.hpp:38
constexpr int backward_param
internal flag
Definition: definitions.hpp:261
constexpr int forward_data
internal flag
Definition: definitions.hpp:257
std::string const & log() const
get full build log
Definition: definitions.hpp:58