oneAPI Deep Neural Network Library (oneDNN)
Performance library for Deep Learning
1.96.0
dnnl.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2016-2020 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 
19 
20 #ifndef ONEAPI_DNNL_DNNL_HPP
21 #define ONEAPI_DNNL_DNNL_HPP
22 
23 #include "oneapi/dnnl/dnnl_config.h"
24 
26 #include <algorithm>
27 #include <cstdlib>
28 #include <iterator>
29 #include <memory>
30 #include <string>
31 #include <vector>
32 #include <unordered_map>
33 
34 #include "oneapi/dnnl/dnnl.h"
35 
37 
38 // __cpp_exceptions is referred from
39 // https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_exceptions.html
40 // gcc < 5 does not define __cpp_exceptions but __EXCEPTIONS,
41 // Microsoft C++ Compiler does not provide an option to disable exceptions
42 #ifndef DNNL_ENABLE_EXCEPTIONS
43 #if __cpp_exceptions || __EXCEPTIONS \
44  || (defined(_MSC_VER) && !defined(__clang__))
45 #define DNNL_ENABLE_EXCEPTIONS 1
46 #else
47 #define DNNL_ENABLE_EXCEPTIONS 0
48 #endif
49 #endif
50 
51 #if defined(__GNUC__) || defined(__clang__)
52 #define DNNL_TRAP() __builtin_trap()
53 #elif defined(__INTEL_COMPILER) || defined(_MSC_VER)
54 #define DNNL_TRAP() __debugbreak()
55 #else
56 #error "unknown compiler"
57 #endif
58 
59 #if DNNL_ENABLE_EXCEPTIONS
60 #define DNNL_THROW_ERROR(status, msg) throw error(status, msg)
61 #else
62 #include <cstdio>
63 #define DNNL_THROW_ERROR(status, msg) \
64  do { \
65  fputs(msg, stderr); \
66  DNNL_TRAP(); \
67  } while (0)
68 #endif
69 
72 
74 namespace dnnl {
75 
79 
84 struct error : public std::exception {
86  const char *message;
87 
92  error(dnnl_status_t status, const char *message)
93  : status(status), message(message) {}
94 
96  const char *what() const noexcept override { return message; }
97 
103  static void wrap_c_api(dnnl_status_t status, const char *message) {
104  if (status != dnnl_success) DNNL_THROW_ERROR(status, message);
105  }
106 };
107 
109 template <typename T>
110 void validate_container_size(const T &v, const char *error_message,
111  int min_size = 1, int max_size = -1) {
112  const int size = (int)v.size();
113  if (size < min_size || (max_size >= 0 && size > max_size))
114  DNNL_THROW_ERROR(dnnl_invalid_arguments, error_message);
115 }
117 
119 template <typename T>
120 struct handle_traits {};
121 
135 template <typename T, typename traits = handle_traits<T>>
136 struct handle {
137 private:
138  static dnnl_status_t dummy_destructor(T) { return dnnl_success; }
139  std::shared_ptr<typename std::remove_pointer<T>::type> data_ {0};
140 
141 protected:
142  bool operator==(const T other) const { return other == data_.get(); }
143  bool operator!=(const T other) const { return !(*this == other); }
144 
145 public:
153  handle() = default;
154 
156  handle(const handle<T, traits> &) = default;
158  handle<T, traits> &operator=(const handle<T, traits> &) = default;
160  handle(handle<T, traits> &&) = default;
163 
169  explicit handle(T t, bool weak = false) { reset(t, weak); }
170 
176  void reset(T t, bool weak = false) {
177  data_.reset(t, weak ? &dummy_destructor : traits::destructor);
178  }
179 
185  T get(bool allow_empty = false) const {
186  T result = data_.get();
187  if (allow_empty == false && result == nullptr)
188  DNNL_THROW_ERROR(
189  dnnl_invalid_arguments, "object is not initialized");
190  return result;
191  }
192 
197  explicit operator T() const { return get(true); }
198 
202  explicit operator bool() const { return get(true) != nullptr; }
203 
210  bool operator==(const handle<T, traits> &other) const {
211  return other.data_.get() == data_.get();
212  }
213 
220  bool operator!=(const handle &other) const { return !(*this == other); }
221 };
222 
224 template <>
225 struct handle_traits<dnnl_memory_t> {
226  static dnnl_status_t destructor(dnnl_memory_t p) {
227  return dnnl_memory_destroy(p);
228  }
229 };
230 
231 template <>
232 struct handle_traits<dnnl_primitive_desc_t> {
233  static dnnl_status_t destructor(dnnl_primitive_desc_t p) {
234  return dnnl_primitive_desc_destroy(p);
235  }
236 };
237 
238 template <>
239 struct handle_traits<dnnl_primitive_t> {
240  static dnnl_status_t destructor(dnnl_primitive_t p) {
241  return dnnl_primitive_destroy(p);
242  }
243 };
244 
245 template <>
246 struct handle_traits<dnnl_primitive_desc_iterator_t> {
247  static dnnl_status_t destructor(dnnl_primitive_desc_iterator_t p) {
249  }
250 };
252 
254 
255 struct stream;
256 struct memory;
257 struct primitive_desc;
258 
263 
267 
269 struct primitive : public handle<dnnl_primitive_t> {
271  enum class kind {
281  sum = dnnl_sum,
293  lrn = dnnl_lrn,
301  rnn = dnnl_rnn,
315  prelu = dnnl_prelu,
316  };
317 
318  using handle::handle;
319 
321  primitive() = default;
322 
327 
331  primitive(const primitive_desc &pd);
332 
338 
342  inline kind get_kind() const;
343 
356  void execute(const stream &astream,
357  const std::unordered_map<int, memory> &args) const;
358 };
359 
365  return static_cast<dnnl_primitive_kind_t>(akind);
366 }
367 
371  "could not get a primitive descriptor from a primitive");
372  return pd;
373 }
374 
377  // TODO (Roma): the code below is only needed because get_primitive_desc
378  // returns a C type.
381  pd, dnnl_query_primitive_kind, 0, (void *)&kind),
382  "could not get a primitive kind from a primitive descriptor");
383  return static_cast<dnnl::primitive::kind>(kind);
384 }
385 
387 
399 
401 enum class scratchpad_mode {
424 };
425 
431  return static_cast<dnnl_scratchpad_mode_t>(mode);
432 }
433 
435 enum class prop_kind {
459 };
460 
466  return static_cast<dnnl_prop_kind_t>(akind);
467 }
468 
470 enum class algorithm {
472  undef = dnnl_alg_kind_undef,
594 };
595 
600  return static_cast<dnnl_alg_kind_t>(aalgorithm);
601 }
602 
604 
607 
609 enum class normalization_flags : unsigned {
615 
624 
631 
637 };
638 
643  return static_cast<dnnl_normalization_flags_t>(flags);
644 }
645 
647 
650 
652 enum class rnn_flags : unsigned {
655 };
656 
661  return static_cast<dnnl_rnn_flags_t>(flags);
662 }
663 
664 #define DNNL_DEFINE_BITMASK_OPS(enum_name) \
665  inline enum_name operator|(enum_name lhs, enum_name rhs) { \
666  return static_cast<enum_name>( \
667  static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs)); \
668  } \
669 \
670  inline enum_name operator&(enum_name lhs, enum_name rhs) { \
671  return static_cast<enum_name>( \
672  static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs)); \
673  } \
674 \
675  inline enum_name operator^(enum_name lhs, enum_name rhs) { \
676  return static_cast<enum_name>( \
677  static_cast<unsigned>(lhs) ^ static_cast<unsigned>(rhs)); \
678  } \
679 \
680  inline enum_name &operator|=(enum_name &lhs, enum_name rhs) { \
681  lhs = static_cast<enum_name>( \
682  static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs)); \
683  return lhs; \
684  } \
685 \
686  inline enum_name &operator&=(enum_name &lhs, enum_name rhs) { \
687  lhs = static_cast<enum_name>( \
688  static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs)); \
689  return lhs; \
690  } \
691 \
692  inline enum_name &operator^=(enum_name &lhs, enum_name rhs) { \
693  lhs = static_cast<enum_name>( \
694  static_cast<unsigned>(lhs) ^ static_cast<unsigned>(rhs)); \
695  return lhs; \
696  } \
697 \
698  inline enum_name operator~(enum_name rhs) { \
699  return static_cast<enum_name>(~static_cast<unsigned>(rhs)); \
700  }
701 
702 DNNL_DEFINE_BITMASK_OPS(normalization_flags)
703 DNNL_DEFINE_BITMASK_OPS(rnn_flags)
704 
705 enum class rnn_direction {
719 };
720 
725  return static_cast<dnnl_rnn_direction_t>(dir);
726 }
727 
729 
732 
739 enum class query {
742 
747 
752 
759 
764 
769 
772 
775 
810 
829 };
830 
835  return static_cast<dnnl_query_t>(aquery);
836 }
837 
839 
841 
852 
854 template <>
855 struct handle_traits<dnnl_engine_t> {
856  static dnnl_status_t destructor(dnnl_engine_t p) {
857  return dnnl_engine_destroy(p);
858  }
859 };
861 
863 struct engine : public handle<dnnl_engine_t> {
864  friend struct primitive;
865  friend struct reorder;
866 
868  enum class kind {
872  cpu = dnnl_cpu,
874  gpu = dnnl_gpu,
875  };
876 
877  using handle::handle;
878 
881  engine() = default;
882 
887  static size_t get_count(kind akind) {
888  return dnnl_engine_get_count(convert_to_c(akind));
889  }
890 
896  engine(kind akind, size_t index) {
899  dnnl_engine_create(&engine, convert_to_c(akind), index),
900  "could not create an engine");
901  reset(engine);
902  }
903 
909  dnnl_engine_t c_engine;
912  dnnl::convert_to_c(dnnl::query::engine), 0, &c_engine),
913  "could not get an engine from a primitive_desc");
914  reset(c_engine, true);
915  }
916 
919  kind get_kind() const {
922  "could not get kind of an engine");
923  return static_cast<engine::kind>(kind);
924  }
925 
931  template <typename primitive_desc>
932  static engine query(const primitive_desc &pd) {
933  return query(pd, dnnl::query::engine);
934  }
935 
936 private:
937  static dnnl_engine_kind_t convert_to_c(kind akind) {
938  return static_cast<dnnl_engine_kind_t>(akind);
939  }
940 
941  template <typename primitive_desc>
942  static engine query(const primitive_desc &pd, dnnl::query what) {
943  dnnl_engine_t c_engine;
945  dnnl::convert_to_c(what), 0, &c_engine),
946  "could not get an engine from a primitive_desc");
947  return engine(c_engine, true);
948  }
949 };
950 
956  return static_cast<dnnl_engine_kind_t>(akind);
957 }
958 
960 
968 
970 template <>
971 struct handle_traits<dnnl_stream_t> {
972  static dnnl_status_t destructor(dnnl_stream_t p) {
973  return dnnl_stream_destroy(p);
974  }
975 };
977 
979 struct stream : public handle<dnnl_stream_t> {
980  using handle::handle;
981 
983  enum class flags : unsigned {
985  in_order = dnnl_stream_in_order,
990  };
991 
994  stream() = default;
995 
1001  stream(const engine &aengine, flags aflags = flags::default_flags) {
1004  static_cast<dnnl_stream_flags_t>(aflags)),
1005  "could not create a stream");
1006  reset(stream);
1007  }
1008 
1010  engine get_engine() const {
1011  dnnl_engine_t c_engine;
1012  error::wrap_c_api(dnnl_stream_get_engine(get(), &c_engine),
1013  "could not get an engine from a stream object");
1014  return engine(c_engine, true);
1015  }
1016 
1021  dnnl_stream_wait(get()), "could not wait on a stream");
1022  return *this;
1023  }
1024 };
1025 
1026 DNNL_DEFINE_BITMASK_OPS(stream::flags)
1027 
1028 
1095 
1102 struct memory : public handle<dnnl_memory_t> {
1103  using handle::handle;
1104 
1106  typedef dnnl_dim_t dim;
1109  typedef std::vector<dim> dims;
1110 
1117  template <typename T>
1118  static void validate_dims(const std::vector<T> &v, int min_size = 0) {
1119  validate_container_size(
1120  v, "dimensions are invalid", min_size, DNNL_MAX_NDIMS);
1121  }
1122 
1124  enum class data_type {
1128  f16 = dnnl_f16,
1131  bf16 = dnnl_bf16,
1133  f32 = dnnl_f32,
1135  s32 = dnnl_s32,
1137  s8 = dnnl_s8,
1139  u8 = dnnl_u8,
1140  };
1141 
1143  enum class format_kind {
1148  any = dnnl_format_kind_any,
1152  blocked = dnnl_blocked,
1154  wino = dnnl_format_kind_wino,
1156  packed = dnnl_format_kind_rnn_packed,
1157  };
1158 
1199  enum class format_tag {
1204  any = dnnl_format_tag_any,
1205 
1207  a = dnnl_a,
1208 
1210  ab = dnnl_ab,
1212  ba = dnnl_ba,
1213 
1215  abc = dnnl_abc,
1217  acb = dnnl_acb,
1219  bac = dnnl_bac,
1221  bca = dnnl_bca,
1223  cba = dnnl_cba,
1224 
1226  abcd = dnnl_abcd,
1228  abdc = dnnl_abdc,
1230  acdb = dnnl_acdb,
1232  bacd = dnnl_bacd,
1234  bcda = dnnl_bcda,
1236  cdba = dnnl_cdba,
1238  dcab = dnnl_dcab,
1239 
1241  abcde = dnnl_abcde,
1243  abdec = dnnl_abdec,
1245  acbde = dnnl_acbde,
1247  acdeb = dnnl_acdeb,
1249  bacde = dnnl_bacde,
1251  bcdea = dnnl_bcdea,
1253  cdeba = dnnl_cdeba,
1255  decab = dnnl_decab,
1257  abced = dnnl_abced,
1258 
1260  abcdef = dnnl_abcdef,
1262  abdfce = dnnl_abdfce,
1264  acbdef = dnnl_acbdef,
1266  abdefc = dnnl_abdefc,
1268  defcab = dnnl_defcab,
1270  abcdfe = dnnl_abcdfe,
1271 
1273  abcdefg = dnnl_abcdefg,
1275  abcdegf = dnnl_abcdegf,
1276 
1278  abcdefgh = dnnl_abcdefgh,
1280  abcdefhg = dnnl_abcdefhg,
1281 
1283  abcdefghi = dnnl_abcdefghi,
1285  abcdefgih = dnnl_abcdefgih,
1286 
1288  abcdefghij = dnnl_abcdefghij,
1290  abcdefghji = dnnl_abcdefghji,
1291 
1293  abcdefghijk = dnnl_abcdefghijk,
1295  abcdefghikj = dnnl_abcdefghikj,
1296 
1298  abcdefghijkl = dnnl_abcdefghijkl,
1300  abcdefghijlk = dnnl_abcdefghijlk,
1301 
1303  x = a,
1305  nc = ab,
1307  cn = ba,
1309  tn = ab,
1311  nt = ba,
1313  ncw = abc,
1315  nwc = acb,
1317  nchw = abcd,
1319  nhwc = acdb,
1321  chwn = bcda,
1323  ncdhw = abcde,
1325  ndhwc = acdeb,
1326 
1328  oi = ab,
1330  io = ba,
1332  oiw = abc,
1334  owi = acb,
1336  wio = cba,
1338  iwo = bca,
1340  oihw = abcd,
1342  hwio = cdba,
1344  ohwi = acdb,
1346  ihwo = bcda,
1348  iohw = bacd,
1350  oidhw = abcde,
1352  dhwio = cdeba,
1354  odhwi = acdeb,
1356  iodhw = bacde,
1358  idhwo = bcdea,
1359 
1361  goiw = abcd,
1363  gowi = abdc,
1365  wigo = dcab,
1367  gohwi = abdec,
1369  goihw = abcde,
1371  hwigo = decab,
1373  giohw = acbde,
1375  goidhw = abcdef,
1377  giodhw = acbdef,
1379  godhwi = abdefc,
1381  dhwigo = defcab,
1382 
1384  tnc = abc,
1386  ntc = bac,
1389  ldnc = abcd,
1396  ldigo = abcde,
1403  ldgoi = abdec,
1406  ldio = abcd,
1409  ldoi = abdc,
1416  ldgo = abcd,
1417 
1418  // Opaque blocked formats
1419 
1420  AB16b16a = dnnl_AB16b16a,
1421  AB16b32a = dnnl_AB16b32a,
1422  AB16b64a = dnnl_AB16b64a,
1423  AB8b16a2b = dnnl_AB8b16a2b,
1424  AB8b32a2b = dnnl_AB8b32a2b,
1425  AB8b64a2b = dnnl_AB8b64a2b,
1426  AB4b16a4b = dnnl_AB4b16a4b,
1427  AB4b32a4b = dnnl_AB4b32a4b,
1428  AB4b64a4b = dnnl_AB4b64a4b,
1429  Abc16a = dnnl_Abc16a,
1430  ABc16a16b = dnnl_ABc16a16b,
1431  ABc4a4b = dnnl_ABc4a4b,
1432  aBc16b = dnnl_aBc16b,
1433  aBc32b = dnnl_aBc32b,
1434  ABc16b16a = dnnl_ABc16b16a,
1435  ABc16b32a = dnnl_ABc16b32a,
1436  ABc16b64a = dnnl_ABc16b64a,
1437  Abc4a = dnnl_Abc4a,
1438  aBc4b = dnnl_aBc4b,
1439  ABc4b16a4b = dnnl_ABc4b16a4b,
1440  ABc4b32a4b = dnnl_ABc4b32a4b,
1441  ABc4b64a4b = dnnl_ABc4b64a4b,
1442  ABc2b8a4b = dnnl_ABc2b8a4b,
1443  ABc16b16a4b = dnnl_ABc16b16a4b,
1444  ABc16b16a2b = dnnl_ABc16b16a2b,
1445  ABc4b4a = dnnl_ABc4b4a,
1446  ABc8a16b2a = dnnl_ABc8a16b2a,
1447  ABc8a8b = dnnl_ABc8a8b,
1448  ABc8a4b = dnnl_ABc8a4b,
1449  aBc8b = dnnl_aBc8b,
1450  ABc8b16a2b = dnnl_ABc8b16a2b,
1451  ABc8b32a2b = dnnl_ABc8b32a2b,
1452  ABc8b64a2b = dnnl_ABc8b64a2b,
1453  ABc8b8a = dnnl_ABc8b8a,
1454  Abcd8a = dnnl_Abcd8a,
1455  Abcd16a = dnnl_Abcd16a,
1456  Abcd32a = dnnl_Abcd32a,
1457  ABcd16a16b = dnnl_ABcd16a16b,
1458  aBcd16b = dnnl_aBcd16b,
1459  aBcd32b = dnnl_aBcd32b,
1460  ABcd16b16a = dnnl_ABcd16b16a,
1461  ABcd16b32a = dnnl_ABcd16b32a,
1462  ABcd16b64a = dnnl_ABcd16b64a,
1463  aBCd16b16c = dnnl_aBCd16b16c,
1464  aBCd16c16b = dnnl_aBCd16c16b,
1465  Abcd4a = dnnl_Abcd4a,
1466  aBcd4b = dnnl_aBcd4b,
1467  ABcd4b16a4b = dnnl_ABcd4b16a4b,
1468  ABcd4b32a4b = dnnl_ABcd4b32a4b,
1469  ABcd4b64a4b = dnnl_ABcd4b64a4b,
1470  ABcd2b8a4b = dnnl_ABcd2b8a4b,
1471  ABcd4b4a = dnnl_ABcd4b4a,
1472  ABcd4a4b = dnnl_ABcd4a4b,
1473  aBCd4c16b4c = dnnl_aBCd4c16b4c,
1474  aBCd2c8b4c = dnnl_aBCd2c8b4c,
1475  ABcd16b16a4b = dnnl_ABcd16b16a4b,
1476  ABcd16b16a2b = dnnl_ABcd16b16a2b,
1477  aBCd16c16b4c = dnnl_aBCd16c16b4c,
1478  aBCd16c16b2c = dnnl_aBCd16c16b2c,
1479  aBCd4c4b = dnnl_aBCd4c4b,
1480  aBCd4b4c = dnnl_aBCd4b4c,
1481  ABcd8a16b2a = dnnl_ABcd8a16b2a,
1482  ABcd8a8b = dnnl_ABcd8a8b,
1483  ABcd8a4b = dnnl_ABcd8a4b,
1485  aBcd8b = dnnl_aBcd8b,
1486  ABcd8b16a2b = dnnl_ABcd8b16a2b,
1487  ABcd8b32a2b = dnnl_ABcd8b32a2b,
1488  ABcd8b64a2b = dnnl_ABcd8b64a2b,
1489  aBCd8b16c2b = dnnl_aBCd8b16c2b,
1491  ABcd8b8a = dnnl_ABcd8b8a,
1492  aBCd8b8c = dnnl_aBCd8b8c,
1493  aBCd8b4c = dnnl_aBCd8b4c,
1494  aBCd8c16b2c = dnnl_aBCd8c16b2c,
1495  aBCd8c8b = dnnl_aBCd8c8b,
1496  Abcde16a = dnnl_Abcde16a,
1497  Abcde32a = dnnl_Abcde32a,
1498  ABcde16a16b = dnnl_ABcde16a16b,
1499  aBcde16b = dnnl_aBcde16b,
1500  aBcde32b = dnnl_aBcde32b,
1501  ABcde16b16a = dnnl_ABcde16b16a,
1502  ABcde16b32a = dnnl_ABcde16b32a,
1503  ABcde16b64a = dnnl_ABcde16b64a,
1504  aBCde16b16c = dnnl_aBCde16b16c,
1505  aBCde16c16b = dnnl_aBCde16c16b,
1506  aBCde2c8b4c = dnnl_aBCde2c8b4c,
1507  Abcde4a = dnnl_Abcde4a,
1508  aBcde4b = dnnl_aBcde4b,
1509  ABcde4b4a = dnnl_ABcde4b4a,
1510  ABcde4a4b = dnnl_ABcde4a4b,
1511  aBCde4b4c = dnnl_aBCde4b4c,
1512  aBCde4c16b4c = dnnl_aBCde4c16b4c,
1513  aBCde16c16b4c = dnnl_aBCde16c16b4c,
1514  aBCde16c16b2c = dnnl_aBCde16c16b2c,
1515  aBCde4c4b = dnnl_aBCde4c4b,
1516  Abcde8a = dnnl_Abcde8a,
1517  ABcde8a8b = dnnl_ABcde8a8b,
1518  ABcde8a4b = dnnl_ABcde8a4b,
1519  aBcde8b = dnnl_aBcde8b,
1520  ABcde8b16a2b = dnnl_ABcde8b16a2b,
1521  ABcde8b32a2b = dnnl_ABcde8b32a2b,
1522  ABcde8b64a2b = dnnl_ABcde8b64a2b,
1523  ABcde4b16a4b = dnnl_ABcde4b16a4b,
1524  ABcde4b32a4b = dnnl_ABcde4b32a4b,
1525  ABcde4b64a4b = dnnl_ABcde4b64a4b,
1526  ABcde2b8a4b = dnnl_ABcde2b8a4b,
1527  aBCde8b16c2b = dnnl_aBCde8b16c2b,
1528  ABcde8b8a = dnnl_ABcde8b8a,
1529  aBCde8b8c = dnnl_aBCde8b8c,
1530  aBCde8b4c = dnnl_aBCde8b4c,
1531  ABcd4a8b8a4b = dnnl_ABcd4a8b8a4b,
1532  ABcd2a8b8a2b = dnnl_ABcd2a8b8a2b,
1533  aBCde4b8c8b4c = dnnl_aBCde4b8c8b4c,
1534  aBCde2b8c8b2c = dnnl_aBCde2b8c8b2c,
1535  aBCde8c16b2c = dnnl_aBCde8c16b2c,
1536  aBCde8c8b = dnnl_aBCde8c8b,
1537  aBcdef16b = dnnl_aBcdef16b,
1538  aBCdef16b16c = dnnl_aBCdef16b16c,
1539  aBCdef16c16b = dnnl_aBCdef16c16b,
1540  aBcdef4b = dnnl_aBcdef4b,
1541  aBCdef2c8b4c = dnnl_aBCdef2c8b4c,
1542  aBCdef4c4b = dnnl_aBCdef4c4b,
1543  aBCdef4b4c = dnnl_aBCdef4b4c,
1544  aBCdef8b8c = dnnl_aBCdef8b8c,
1545  aBCdef8b4c = dnnl_aBCdef8b4c,
1546  aBCdef8c16b2c = dnnl_aBCdef8c16b2c,
1547  aBCdef4c16b4c = dnnl_aBCdef4c16b4c,
1548  aBCdef8c8b = dnnl_aBCdef8c8b,
1549  aBdc16b = dnnl_aBdc16b,
1550  aBdc4b = dnnl_aBdc4b,
1551  aBdc8b = dnnl_aBdc8b,
1552  aBdec16b = dnnl_aBdec16b,
1553  aBdec4b = dnnl_aBdec4b,
1554  aBdec8b = dnnl_aBdec8b,
1555  aBdefc16b = dnnl_aBdefc16b,
1556  aCBdef16c16b = dnnl_aCBdef16c16b,
1557  aCBdef16b16c = dnnl_aCBdef16b16c,
1558  aBdefc4b = dnnl_aBdefc4b,
1559  aBdefc8b = dnnl_aBdefc8b,
1560  Acb16a = dnnl_Acb16a,
1561  Acb4a = dnnl_Acb4a,
1562  Acb8a = dnnl_Acb8a,
1563  aCBd16b16c = dnnl_aCBd16b16c,
1564  aCBd16c16b = dnnl_aCBd16c16b,
1565  aCBde16b16c = dnnl_aCBde16b16c,
1566  aCBde16c16b = dnnl_aCBde16c16b,
1567  Acdb16a = dnnl_Acdb16a,
1568  Acdb4a = dnnl_Acdb4a,
1569  Acdb8a = dnnl_Acdb8a,
1570  Acdeb16a = dnnl_Acdeb16a,
1571  Acdeb4a = dnnl_Acdeb4a,
1572  Acdeb8a = dnnl_Acdeb8a,
1573  BAc16a16b = dnnl_BAc16a16b,
1574  BAc16b16a = dnnl_BAc16b16a,
1575  BAcd16a16b = dnnl_BAcd16a16b,
1576  BAcd16b16a = dnnl_BAcd16b16a,
1577  ABcd32a32b = dnnl_ABcd32a32b,
1578  BAcde16b16a = dnnl_BAcde16b16a,
1579  BAcde16a16b = dnnl_BAcde16a16b,
1580  aBdec32b = dnnl_aBdec32b,
1581  Abcdef16a = dnnl_Abcdef16a,
1582  Abcdef32a = dnnl_Abcdef32a,
1583  Acdb32a = dnnl_Acdb32a,
1584  aBCd2b4c2b = dnnl_aBCd2b4c2b,
1585  aBCde2b4c2b = dnnl_aBCde2b4c2b,
1586  aBCdef2b4c2b = dnnl_aBCdef2b4c2b,
1587  aBCd2c4b2c = dnnl_aBCd2c4b2c,
1588  aBCde2c4b2c = dnnl_aBCde2c4b2c,
1589  aBCdef2c4b2c = dnnl_aBCdef2c4b2c,
1590  aBCd4b8c2b = dnnl_aBCd4b8c2b,
1591  aBCde4b8c2b = dnnl_aBCde4b8c2b,
1592  aBCdef4b8c2b = dnnl_aBCdef4b8c2b,
1593  aBCd4c8b2c = dnnl_aBCd4c8b2c,
1594  aBCde4c8b2c = dnnl_aBCde4c8b2c,
1595  aBCdef4c8b2c = dnnl_aBCdef4c8b2c,
1596  AB32a32b8a4b = dnnl_AB32a32b8a4b,
1597  AB32a32b8a2b = dnnl_AB32a32b8a2b,
1598  AB8a4b = dnnl_AB8a4b,
1599  AB8a2b = dnnl_AB8a2b,
1600  abDc32d = dnnl_abDc32d,
1601  abDC32d4c = dnnl_abDC32d4c,
1602  abdEc32e = dnnl_abdEc32e,
1603  abdEC32e2c = dnnl_abdEC32e2c,
1604  abdEC32e4c = dnnl_abdEC32e4c,
1605 
1606  format_tag_last = dnnl_format_tag_last,
1607 
1608  nCdhw16c = dnnl_nCdhw16c,
1609  nCdhw4c = dnnl_nCdhw4c,
1610  nCdhw8c = dnnl_nCdhw8c,
1611  nChw16c = dnnl_nChw16c,
1612  nChw4c = dnnl_nChw4c,
1613  nChw8c = dnnl_nChw8c,
1614  nCw16c = dnnl_nCw16c,
1615  nCw4c = dnnl_nCw4c,
1616  nCw8c = dnnl_nCw8c,
1617  NCw16n16c = dnnl_NCw16n16c,
1618  NChw16n16c = dnnl_NChw16n16c,
1619  NCdhw16n16c = dnnl_NCdhw16n16c,
1620  NCdhw32n32c = dnnl_NCdhw32n32c,
1621  NChw32n32c = dnnl_NChw32n32c,
1622  IOhw16i16o = dnnl_IOhw16i16o,
1623  OI16i16o = dnnl_OI16i16o,
1624  OI16i32o = dnnl_OI16i32o,
1625  OI16i64o = dnnl_OI16i64o,
1626  OI8i16o2i = dnnl_OI8i16o2i,
1627  OI8i32o2i = dnnl_OI8i32o2i,
1628  OI8i64o2i = dnnl_OI8i64o2i,
1629  OI4i16o4i = dnnl_OI4i16o4i,
1630  OI4i32o4i = dnnl_OI4i32o4i,
1631  OI4i64o4i = dnnl_OI4i64o4i,
1632  Ohwi32o = dnnl_Ohwi32o,
1633  IOdhw16i16o = dnnl_IOdhw16i16o,
1634  gIOhw16i16o = dnnl_gIOhw16i16o,
1635  gOhwi32o = dnnl_gOhwi32o,
1636  Goidhw16g = dnnl_Goidhw16g,
1637  IOw16o16i = dnnl_IOw16o16i,
1638  OIw16i16o = dnnl_OIw16i16o,
1639  OIw16i32o = dnnl_OIw16i32o,
1640  OIw16i64o = dnnl_OIw16i64o,
1641  IOw16i16o = dnnl_IOw16i16o,
1642  gIOw16i16o = dnnl_gIOw16i16o,
1643  OIw16o16i = dnnl_OIw16o16i,
1644  Oiw16o = dnnl_Oiw16o,
1645  OIw4i16o4i = dnnl_OIw4i16o4i,
1646  OIw4i32o4i = dnnl_OIw4i32o4i,
1647  OIw4i64o4i = dnnl_OIw4i64o4i,
1648  OIw2i8o4i = dnnl_OIw2i8o4i,
1649  OIw4i4o = dnnl_OIw4i4o,
1650  OIw4o4i = dnnl_OIw4o4i,
1651  Oiw4o = dnnl_Oiw4o,
1652  OIw8i16o2i = dnnl_OIw8i16o2i,
1653  OIw8i32o2i = dnnl_OIw8i32o2i,
1654  OIw8i64o2i = dnnl_OIw8i64o2i,
1655  OIw8i8o = dnnl_OIw8i8o,
1656  OIw8o16i2o = dnnl_OIw8o16i2o,
1657  OIw8o8i = dnnl_OIw8o8i,
1658  OIw8o4i = dnnl_OIw8o4i,
1659  Owi16o = dnnl_Owi16o,
1660  OwI16o2i = dnnl_OwI16o2i,
1661  Owi4o = dnnl_Owi4o,
1662  Owi8o = dnnl_Owi8o,
1663  IOhw16o16i = dnnl_IOhw16o16i,
1664  Ohwi16o = dnnl_Ohwi16o,
1665  OhwI16o2i = dnnl_OhwI16o2i,
1666  Ohwi4o = dnnl_Ohwi4o,
1667  Ohwi8o = dnnl_Ohwi8o,
1668  OIhw16i16o = dnnl_OIhw16i16o,
1669  OIhw16i32o = dnnl_OIhw16i32o,
1670  OIhw16i64o = dnnl_OIhw16i64o,
1671  OIhw16o16i = dnnl_OIhw16o16i,
1672  Oihw16o = dnnl_Oihw16o,
1673  OIhw4i16o4i = dnnl_OIhw4i16o4i,
1674  OIhw4i32o4i = dnnl_OIhw4i32o4i,
1675  OIhw4i64o4i = dnnl_OIhw4i64o4i,
1676  OIhw4i4o = dnnl_OIhw4i4o,
1677  OIhw4o4i = dnnl_OIhw4o4i,
1678  Oihw4o = dnnl_Oihw4o,
1679  OIhw8i16o2i = dnnl_OIhw8i16o2i,
1680  OIhw8i32o2i = dnnl_OIhw8i32o2i,
1681  OIhw8i64o2i = dnnl_OIhw8i64o2i,
1682  OIhw8i8o = dnnl_OIhw8i8o,
1683  OIhw8o16i2o = dnnl_OIhw8o16i2o,
1684  OIhw8o8i = dnnl_OIhw8o8i,
1685  OIhw8o4i = dnnl_OIhw8o4i,
1686  OIhw2i8o4i = dnnl_OIhw2i8o4i,
1687  IOdhw16o16i = dnnl_IOdhw16o16i,
1688  Odhwi16o = dnnl_Odhwi16o,
1689  OdhwI16o2i = dnnl_OdhwI16o2i,
1690  Odhwi4o = dnnl_Odhwi4o,
1691  Odhwi8o = dnnl_Odhwi8o,
1692  OIdhw16i16o = dnnl_OIdhw16i16o,
1693  OIdhw16i32o = dnnl_OIdhw16i32o,
1694  OIdhw16i64o = dnnl_OIdhw16i64o,
1695  OIdhw16o16i = dnnl_OIdhw16o16i,
1696  Oidhw16o = dnnl_Oidhw16o,
1697  OIdhw4i4o = dnnl_OIdhw4i4o,
1698  OIdhw4o4i = dnnl_OIdhw4o4i,
1699  Oidhw4o = dnnl_Oidhw4o,
1700  OIdhw8i16o2i = dnnl_OIdhw8i16o2i,
1701  OIdhw8i32o2i = dnnl_OIdhw8i32o2i,
1702  OIdhw8i64o2i = dnnl_OIdhw8i64o2i,
1703  OIdhw4i16o4i = dnnl_OIdhw4i16o4i,
1704  OIdhw4i32o4i = dnnl_OIdhw4i32o4i,
1705  OIdhw4i64o4i = dnnl_OIdhw4i64o4i,
1706  OIdhw2i8o4i = dnnl_OIdhw2i8o4i,
1707  OIdhw8i8o = dnnl_OIdhw8i8o,
1708  OIdhw8o8i = dnnl_OIdhw8o8i,
1709  OIdhw8o4i = dnnl_OIdhw8o4i,
1710  gIOw16o16i = dnnl_gIOw16o16i,
1711  gOIw16i16o = dnnl_gOIw16i16o,
1712  gOIw16o16i = dnnl_gOIw16o16i,
1713  gOiw16o = dnnl_gOiw16o,
1714  gOIw4i16o4i = dnnl_gOIw4i16o4i,
1715  gOIw2i8o4i = dnnl_gOIw2i8o4i,
1716  gOIw4i4o = dnnl_gOIw4i4o,
1717  gOIw4o4i = dnnl_gOIw4o4i,
1718  gOiw4o = dnnl_gOiw4o,
1719  gOIw8i16o2i = dnnl_gOIw8i16o2i,
1720  gOIw8i8o = dnnl_gOIw8i8o,
1721  gOIw8o16i2o = dnnl_gOIw8o16i2o,
1722  gOIw8o8i = dnnl_gOIw8o8i,
1723  gOIw8o4i = dnnl_gOIw8o4i,
1724  gOwi16o = dnnl_gOwi16o,
1725  gOwI16o2i = dnnl_gOwI16o2i,
1726  gOwi4o = dnnl_gOwi4o,
1727  gOwi8o = dnnl_gOwi8o,
1728  Goiw8g = dnnl_Goiw8g,
1729  Goiw16g = dnnl_Goiw16g,
1730  gIOhw16o16i = dnnl_gIOhw16o16i,
1731  gOhwi16o = dnnl_gOhwi16o,
1732  gOhwI16o2i = dnnl_gOhwI16o2i,
1733  gOhwi4o = dnnl_gOhwi4o,
1734  gOhwi8o = dnnl_gOhwi8o,
1735  Goihw16g = dnnl_Goihw16g,
1736  gOIhw16i16o = dnnl_gOIhw16i16o,
1737  gOIhw16o16i = dnnl_gOIhw16o16i,
1738  gOihw16o = dnnl_gOihw16o,
1739  gOIhw4i16o4i = dnnl_gOIhw4i16o4i,
1740  gOIhw2i8o4i = dnnl_gOIhw2i8o4i,
1741  gOIhw4i4o = dnnl_gOIhw4i4o,
1742  gOIhw4o4i = dnnl_gOIhw4o4i,
1743  gOihw4o = dnnl_gOihw4o,
1744  Goihw8g = dnnl_Goihw8g,
1745  gOIhw8i16o2i = dnnl_gOIhw8i16o2i,
1746  gOIhw8i8o = dnnl_gOIhw8i8o,
1747  gOIhw8o16i2o = dnnl_gOIhw8o16i2o,
1748  OIw4o8i8o4i = dnnl_OIw4o8i8o4i,
1749  OIdhw4o8i8o4i = dnnl_OIdhw4o8i8o4i,
1750  OIhw4o8i8o4i = dnnl_OIhw4o8i8o4i,
1751  OIhw2o8i8o2i = dnnl_OIhw2o8i8o2i,
1752  gOIw4o8i8o4i = dnnl_gOIw4o8i8o4i,
1753  gOIdhw4o8i8o4i = dnnl_gOIdhw4o8i8o4i,
1754  gOIhw4o8i8o4i = dnnl_gOIhw4o8i8o4i,
1755  gOIhw2o8i8o2i = dnnl_gOIhw2o8i8o2i,
1756  OIhw16i16o4i = dnnl_OIhw16i16o4i,
1757  OIhw16i16o2i = dnnl_OIhw16i16o2i,
1758  gOIhw16i16o4i = dnnl_gOIhw16i16o4i,
1759  gOIhw16i16o2i = dnnl_gOIhw16i16o2i,
1760  gOIhw8o8i = dnnl_gOIhw8o8i,
1761  gOIhw8o4i = dnnl_gOIhw8o4i,
1762  gIOdhw16i16o = dnnl_gIOdhw16i16o,
1763  gIOdhw16o16i = dnnl_gIOdhw16o16i,
1764  gOdhwi16o = dnnl_gOdhwi16o,
1765  gOdhwI16o2i = dnnl_gOdhwI16o2i,
1766  gOdhwi4o = dnnl_gOdhwi4o,
1767  gOdhwi8o = dnnl_gOdhwi8o,
1768  gOIdhw16i16o = dnnl_gOIdhw16i16o,
1769  gOIdhw16o16i = dnnl_gOIdhw16o16i,
1770  gOidhw16o = dnnl_gOidhw16o,
1771  gOIdhw4i4o = dnnl_gOIdhw4i4o,
1772  gOIdhw4o4i = dnnl_gOIdhw4o4i,
1773  gOidhw4o = dnnl_gOidhw4o,
1774  gOIdhw8i16o2i = dnnl_gOIdhw8i16o2i,
1775  gOIdhw4i16o4i = dnnl_gOIdhw4i16o4i,
1776  gOIdhw2i8o4i = dnnl_gOIdhw2i8o4i,
1777  gOIdhw8i8o = dnnl_gOIdhw8i8o,
1778  gOIdhw8o8i = dnnl_gOIdhw8o8i,
1779  gOIdhw8o4i = dnnl_gOIdhw8o4i,
1780  gOIw2i4o2i = dnnl_gOIw2i4o2i,
1781  gOIhw2i4o2i = dnnl_gOIhw2i4o2i,
1782  gOIdhw2i4o2i = dnnl_gOIdhw2i4o2i,
1783  gOIw2o4i2o = dnnl_gOIw2o4i2o,
1784  gOIhw2o4i2o = dnnl_gOIhw2o4i2o,
1785  gOIdhw2o4i2o = dnnl_gOIdhw2o4i2o,
1786  gOIw4i8o2i = dnnl_gOIw4i8o2i,
1787  gOIhw4i8o2i = dnnl_gOIhw4i8o2i,
1788  gOIdhw4i8o2i = dnnl_gOIdhw4i8o2i,
1789  gOIw4o8i2o = dnnl_gOIw4o8i2o,
1790  gOIhw4o8i2o = dnnl_gOIhw4o8i2o,
1791  gOIdhw4o8i2o = dnnl_gOIdhw4o8i2o,
1792  ldOi32o = abDc32d,
1793  ldOI32o4i = abDC32d4c,
1794  ldgOi32o = abdEc32e,
1795  ldgOI32o2i = abdEC32e2c,
1796  ldgOI32o4i = abdEC32e4c,
1797  };
1798 
1800  struct desc {
1801  friend struct memory;
1804 
1807  desc() : data() {}
1808 
1824  desc(const dims &adims, data_type adata_type, format_tag aformat_tag,
1825  bool allow_empty = false)
1826  : data() {
1827  validate_dims(adims);
1829  (int)adims.size(), adims.data(), convert_to_c(adata_type),
1830  convert_to_c(aformat_tag));
1831  if (!allow_empty)
1833  "could not construct a memory descriptor using a "
1834  "format tag");
1835  }
1836 
1852  desc(const dims &adims, data_type adata_type, const dims &strides,
1853  bool allow_empty = false)
1854  : data() {
1855  validate_dims(adims);
1856  if (!strides.empty()) validate_dims(strides, (int)adims.size());
1858  (int)adims.size(), adims.data(), convert_to_c(adata_type),
1859  strides.empty() ? nullptr : &strides[0]);
1860  if (!allow_empty)
1862  "could not construct a memory descriptor using "
1863  "strides");
1864  }
1865 
1869  desc(const dnnl_memory_desc_t &data) : data(data) {}
1870 
1873  //
1882  desc submemory_desc(const dims &adims, const dims &offsets,
1883  bool allow_empty = false) const {
1884  validate_dims(adims, data.ndims);
1885  validate_dims(offsets, data.ndims);
1888  &sub_md, &data, adims.data(), offsets.data());
1889  if (!allow_empty)
1890  error::wrap_c_api(status, "could not construct a sub-memory");
1891  return desc(sub_md);
1892  }
1893 
1938  desc reshape(const dims &adims, bool allow_empty = false) const {
1939  if (data.ndims) validate_dims(adims, 1);
1942  &out_md, &data, (int)adims.size(), adims.data());
1943  if (!allow_empty)
1945  status, "could not reshape a memory descriptor");
1946  return desc(out_md);
1947  }
1948 
1986  desc permute_axes(const std::vector<int> &permutation,
1987  bool allow_empty = false) const {
1988  validate_dims(permutation, data.ndims);
1991  &out_md, &data, permutation.data());
1992  if (!allow_empty)
1994  "could not permute axes of a memory descriptor");
1995  return desc(out_md);
1996  }
1997 
2002  memory::dims dims() const {
2003  return memory::dims(data.dims, data.dims + data.ndims);
2004  }
2005 
2009  return static_cast<memory::data_type>(data.data_type);
2010  }
2011 
2016  size_t get_size() const { return dnnl_memory_desc_get_size(&data); }
2017 
2021  bool is_zero() const { return data.ndims == 0; }
2022 
2027  bool operator==(const desc &other) const {
2028  return dnnl_memory_desc_equal(&data, &other.data) != 0;
2029  }
2030 
2035  bool operator!=(const desc &other) const { return !operator==(other); }
2036 
2040  explicit operator bool() const { return data.ndims != 0; }
2041  };
2042 
2047  memory() = default;
2048 
2068  memory(const desc &md, const engine &aengine, void *handle) {
2069  dnnl_memory_t result;
2071  dnnl_memory_create(&result, &md.data, aengine.get(), handle),
2072  "could not create a memory object");
2073  reset(result);
2074  }
2075 
2082  memory(const desc &md, const engine &aengine)
2083  : memory(md, aengine, DNNL_MEMORY_ALLOCATE) {}
2084 
2086  desc get_desc() const {
2087  const dnnl_memory_desc_t *cdesc;
2089  "could not get a memory descriptor from a memory object");
2090  return desc(*cdesc);
2091  }
2092 
2094  engine get_engine() const {
2095  dnnl_engine_t c_engine;
2096  error::wrap_c_api(dnnl_memory_get_engine(get(), &c_engine),
2097  "could not get an engine from a memory object");
2098  return engine(c_engine, true);
2099  }
2100 
2105  void *get_data_handle() const {
2106  void *handle;
2108  "could not get a native handle from a memory object");
2109  return handle;
2110  }
2111 
2140  void set_data_handle(void *handle, const stream &astream) const {
2142  get(), handle, astream.get(true)),
2143  "could not set native handle of a memory object");
2144  }
2145 
2156  void set_data_handle(void *handle) const {
2158  dnnl_memory_set_data_handle_v2(get(), handle, nullptr),
2159  "could not set native handle of a memory object");
2160  }
2161 
2183  template <typename T = void>
2184  T *map_data() const {
2185  void *mapped_ptr;
2186  error::wrap_c_api(dnnl_memory_map_data(get(), &mapped_ptr),
2187  "could not map memory object data");
2188  return static_cast<T *>(mapped_ptr);
2189  }
2190 
2201  void unmap_data(void *mapped_ptr) const {
2202  error::wrap_c_api(dnnl_memory_unmap_data(get(), mapped_ptr),
2203  "could not unmap memory object data");
2204  }
2205 
2206  static dnnl_data_type_t convert_to_c(data_type adata_type) {
2207  return static_cast<dnnl_data_type_t>(adata_type);
2208  }
2209  static dnnl_format_tag_t convert_to_c(format_tag format) {
2210  return static_cast<dnnl_format_tag_t>(format);
2211  }
2212 };
2213 
2214 inline bool operator==(dnnl_data_type_t a, memory::data_type b) {
2215  return a == memory::convert_to_c(b);
2216 }
2217 inline bool operator!=(dnnl_data_type_t a, memory::data_type b) {
2218  return !(a == b);
2219 }
2220 inline bool operator==(memory::data_type a, dnnl_data_type_t b) {
2221  return b == a;
2222 }
2223 inline bool operator!=(memory::data_type a, dnnl_data_type_t b) {
2224  return !(a == b);
2225 }
2226 
2227 inline bool operator==(dnnl_format_tag_t a, memory::format_tag b) {
2228  return a == memory::convert_to_c(b);
2229 }
2230 inline bool operator!=(dnnl_format_tag_t a, memory::format_tag b) {
2231  return !(a == b);
2232 }
2233 inline bool operator==(memory::format_tag a, dnnl_format_tag_t b) {
2234  return b == a;
2235 }
2236 inline bool operator!=(memory::format_tag a, dnnl_format_tag_t b) {
2237  return !(a == b);
2238 }
2239 
2241 
2249 
2251 template <>
2252 struct handle_traits<dnnl_post_ops_t> {
2253  static dnnl_status_t destructor(dnnl_post_ops_t p) {
2254  return dnnl_post_ops_destroy(p);
2255  }
2256 };
2258 
2266 struct post_ops : public handle<dnnl_post_ops_t> {
2268 
2271  dnnl_post_ops_t result;
2273  dnnl_post_ops_create(&result), "could not create post-ops");
2274  reset(result);
2275  }
2276 
2278  int len() const { return dnnl_post_ops_len(get()); }
2279 
2283  primitive::kind kind(int index) const {
2285  "post-ops index is out of range");
2286  return static_cast<primitive::kind>(
2287  dnnl_post_ops_get_kind(get(), index));
2288  }
2289 
2318  void append_sum(float scale = 1.f,
2320  if (data_type == memory::data_type::undef)
2322  "could not append a sum post-op");
2323  else
2325  memory::convert_to_c(data_type)),
2326  "could not append a sum post-op");
2327  }
2328 
2333  void get_params_sum(int index, float &scale) const {
2334  error::wrap_c_api(dnnl_post_ops_get_params_sum(get(), index, &scale),
2335  "could not get parameters of a sum post-op");
2336  }
2337 
2344  int index, float &scale, memory::data_type &data_type) const {
2345  dnnl_data_type_t c_data_type;
2347  get(), index, &scale, &c_data_type),
2348  "could not get parameters of a sum post-op");
2349  data_type = static_cast<memory::data_type>(c_data_type);
2350  }
2351 
2366  float scale, algorithm aalgorithm, float alpha, float beta) {
2368  convert_to_c(aalgorithm), alpha, beta),
2369  "could not append an elementwise post-op");
2370  }
2371 
2379  void get_params_eltwise(int index, float &scale, algorithm &aalgorithm,
2380  float &alpha, float &beta) const {
2381  dnnl_alg_kind_t c_alg;
2383  get(), index, &scale, &c_alg, &alpha, &beta),
2384  "could not get parameters of an elementwise post-op");
2385  aalgorithm = static_cast<dnnl::algorithm>(c_alg);
2386  }
2387 
2416  void append_dw_k3s1p1(memory::data_type weights_data_type,
2417  memory::data_type bias_data_type, memory::data_type dst_data_type,
2418  int mask, const std::vector<float> &scales) {
2419 
2421  memory::convert_to_c(weights_data_type),
2422  memory::convert_to_c(bias_data_type),
2423  memory::convert_to_c(dst_data_type),
2424  scales.size(), mask, &scales[0]),
2425  "could not append depthwise post-op");
2426  }
2427 
2442  void get_params_dw_k3s1p1(int index, memory::data_type &weights_data_type,
2443  memory::data_type &bias_data_type, memory::data_type &dst_data_type,
2444  int &mask, std::vector<float> &scales) const {
2445 
2446  dnnl_data_type_t c_weights_data_type;
2447  dnnl_data_type_t c_bias_data_type;
2448  dnnl_data_type_t c_dst_data_type;
2449  dnnl_dim_t count;
2450  int c_mask;
2451  const float *c_scales;
2453  &c_weights_data_type, &c_bias_data_type,
2454  &c_dst_data_type, &count, &c_mask, &c_scales),
2455  "could not get parameters of depthwise post-op");
2456 
2457  weights_data_type = static_cast<memory::data_type>(c_weights_data_type);
2458  bias_data_type = static_cast<memory::data_type>(c_bias_data_type);
2459  dst_data_type = static_cast<memory::data_type>(c_dst_data_type);
2460  scales.resize(count);
2461 
2462  mask = c_mask;
2463  for (dnnl_dim_t c = 0; c < count; ++c)
2464  scales[c] = c_scales[c];
2465  return;
2466  }
2467 
2501  void append_dw_k3s2p1(memory::data_type weights_data_type,
2502  memory::data_type bias_data_type, memory::data_type dst_data_type,
2503  int mask, const std::vector<float> &scales) {
2504 
2506  memory::convert_to_c(weights_data_type),
2507  memory::convert_to_c(bias_data_type),
2508  memory::convert_to_c(dst_data_type),
2509  scales.size(), mask, &scales[0]),
2510  "could not append depthwise post-op");
2511  }
2512 
2527  void get_params_dw_k3s2p1(int index, memory::data_type &weights_data_type,
2528  memory::data_type &bias_data_type, memory::data_type &dst_data_type,
2529  int &mask, std::vector<float> &scales) const {
2530 
2531  dnnl_data_type_t c_weights_data_type;
2532  dnnl_data_type_t c_bias_data_type;
2533  dnnl_data_type_t c_dst_data_type;
2534  dnnl_dim_t count;
2535  int c_mask;
2536  const float *c_scales;
2538  &c_weights_data_type, &c_bias_data_type,
2539  &c_dst_data_type, &count, &c_mask, &c_scales),
2540  "could not get parameters of depthwise post-op");
2541 
2542  weights_data_type = static_cast<memory::data_type>(c_weights_data_type);
2543  bias_data_type = static_cast<memory::data_type>(c_bias_data_type);
2544  dst_data_type = static_cast<memory::data_type>(c_dst_data_type);
2545  scales.resize(count);
2546 
2547  mask = c_mask;
2548  for (dnnl_dim_t c = 0; c < count; ++c)
2549  scales[c] = c_scales[c];
2550  return;
2551  }
2552 
2567  void append_binary(algorithm aalgorithm, const memory::desc &src1_desc) {
2569  convert_to_c(aalgorithm), &src1_desc.data),
2570  "could not append a binary post-op");
2571  }
2572 
2579  int index, algorithm &aalgorithm, memory::desc &src1_desc) const {
2580  dnnl_alg_kind_t c_alg;
2581  const dnnl_memory_desc_t *data;
2583  dnnl_post_ops_get_params_binary(get(), index, &c_alg, &data),
2584  "could not get parameters of a binary post-op");
2585  aalgorithm = static_cast<dnnl::algorithm>(c_alg);
2586  src1_desc.data = *data;
2587  }
2588 };
2589 
2591 template <>
2592 struct handle_traits<dnnl_primitive_attr_t> {
2593  static dnnl_status_t destructor(dnnl_primitive_attr_t p) {
2594  return dnnl_primitive_attr_destroy(p);
2595  }
2596 };
2598 
2602 struct primitive_attr : public handle<dnnl_primitive_attr_t> {
2604 
2607  dnnl_primitive_attr_t result;
2609  "could not create primitive attribute");
2610  reset(result);
2611  }
2612 
2619  : handle<dnnl_primitive_attr_t>(attr) {}
2620 
2623  dnnl_scratchpad_mode_t result;
2626  "could not get scratchpad mode primitive attribute");
2627  return scratchpad_mode(result);
2628  }
2629 
2635  get(), dnnl::convert_to_c(mode)),
2636  "could not set scratchpad mode primitive attribute");
2637  }
2638 
2648  void get_output_scales(int &mask, std::vector<float> &scales) const {
2649  dnnl_dim_t count;
2650  int c_mask;
2651  const float *c_scales;
2653  get(), &count, &c_mask, &c_scales),
2654  "could not get output scales primitive attribute");
2655  scales.resize(count);
2656 
2657  mask = c_mask;
2658  for (dnnl_dim_t c = 0; c < count; ++c)
2659  scales[c] = c_scales[c];
2660  }
2661 
2704  void set_output_scales(int mask, const std::vector<float> &scales) {
2707  get(), (dnnl_dim_t)scales.size(), mask, scales.data()),
2708  "could not set output scales primitive attribute");
2709  }
2710 
2722  void get_scales(int arg, int &mask, std::vector<float> &scales) const {
2723  dnnl_dim_t count;
2724  int c_mask;
2725  const float *c_scales;
2727  get(), arg, &count, &c_mask, &c_scales),
2728  "could not get scales primitive attributes");
2729  scales.resize(count);
2730 
2731  mask = c_mask;
2732  for (dnnl_dim_t c = 0; c < count; ++c)
2733  scales[c] = c_scales[c];
2734  }
2735 
2752  void set_scales(int arg, int mask, const std::vector<float> &scales) {
2754  dnnl_primitive_attr_set_scales(get(), arg,
2755  (dnnl_dim_t)scales.size(), mask, scales.data()),
2756  "could not set scales primitive attribute");
2757  }
2758 
2770  int arg, int &mask, std::vector<int32_t> &zero_points) const {
2771  dnnl_dim_t count;
2772  int c_mask;
2773  const int32_t *c_zero_points;
2775  get(), arg, &count, &c_mask, &c_zero_points),
2776  "could not get zero points primitive attribute");
2777  zero_points.resize(count);
2778 
2779  mask = c_mask;
2780  for (dnnl_dim_t c = 0; c < count; ++c)
2781  zero_points[c] = c_zero_points[c];
2782  }
2783 
2805  int arg, int mask, const std::vector<int32_t> &zero_points) {
2807  (dnnl_dim_t)zero_points.size(), mask,
2808  zero_points.data()),
2809  "could not set zero points primitive attribute");
2810  }
2811 
2815  const post_ops get_post_ops() const {
2816  post_ops result;
2817  const_dnnl_post_ops_t c_result;
2819  "could not get post-ops primitive attribute");
2820  result.reset(const_cast<dnnl_post_ops_t>(c_result), true);
2821  return result;
2822  }
2823 
2832  void set_post_ops(const post_ops ops) {
2834  "could not set post-ops primitive attribute");
2835  }
2836 
2870  void set_rnn_data_qparams(float scale, float shift) {
2872  dnnl_primitive_attr_set_rnn_data_qparams(get(), scale, shift),
2873  "could not set RNN data quantization parameters primitive "
2874  "attribute");
2875  }
2876 
2886  void get_rnn_data_qparams(float &scale, float &shift) {
2887  float c_scale, c_shift;
2889  get(), &c_scale, &c_shift),
2890  "could not set RNN data quantization parameters primitive "
2891  "attribute");
2892  scale = c_scale;
2893  shift = c_shift;
2894  }
2895 
2922  void set_rnn_weights_qparams(int mask, const std::vector<float> &scales) {
2924  (int)scales.size(), mask, scales.data()),
2925  "could not set RNN weights quantization parameters primitive "
2926  "attribute");
2927  }
2928 
2948  void get_rnn_weights_qparams(int &mask, std::vector<float> &scales) {
2949  dnnl_dim_t count;
2950  int c_mask;
2951  const float *c_scales;
2953  get(), &count, &c_mask, &c_scales),
2954  "could not get primitive RNN weights quantization "
2955  "parameters attributes");
2956  scales.resize(count);
2957 
2958  mask = c_mask;
2959  for (dnnl_dim_t c = 0; c < count; c++)
2960  scales[c] = c_scales[c];
2961  }
2962 
2964  // The low-precision configuration of the RNN primitives expect input
2965  // weights to use the signed 8-bit integer data type. The scaling factors
2966  // are used to quantize floating-point data to signed integer and must be
2990  int mask, const std::vector<float> &scales) {
2993  get(), (int)scales.size(), mask, scales.data()),
2994  "could not set primitive RNN weights projection quantization "
2995  "parameters attributes");
2996  }
2997 
3018  int &mask, std::vector<float> &scales) {
3019  dnnl_dim_t count;
3020  int c_mask;
3021  const float *c_scales;
3024  get(), &count, &c_mask, &c_scales),
3025  "could not get primitive RNN weights projection quantization "
3026  "parameters attributes");
3027  scales.resize(count);
3028 
3029  mask = c_mask;
3030  for (dnnl_dim_t c = 0; c < count; c++)
3031  scales[c] = c_scales[c];
3032  }
3033 };
3034 
3036 
3039 
3041 struct primitive_desc_base : public handle<dnnl_primitive_desc_t> {
3043 
3045  primitive_desc_base() = default;
3046 
3049  engine get_engine() const { return engine::query(*this); }
3050 
3053  const char *impl_info_str() const {
3054  const char *res;
3056  get(), dnnl_query_impl_info_str, 0, &res),
3057  "could not retrieve implementation info string from a "
3058  "primitive descriptor");
3059  return res;
3060  }
3061 
3066  memory::dim res;
3068  get(), dnnl::convert_to_c(what), 0, &res);
3069  return status == dnnl_success ? res : 0;
3070  }
3071 
3086  memory::desc query_md(query what, int idx = 0) const {
3087  std::vector<query> valid_q {query::src_md, query::diff_src_md,
3091  if (!std::any_of(valid_q.cbegin(), valid_q.cend(),
3092  [=](query q) { return what == q; }))
3093  DNNL_THROW_ERROR(dnnl_invalid_arguments,
3094  "memory descriptor query is invalid");
3095 
3097  get(), dnnl::convert_to_c(what), idx);
3098  return cdesc ? memory::desc(*cdesc) : memory::desc();
3099  }
3100 
3106  memory::desc src_desc(int idx) const {
3107  return query_md(query::src_md, idx);
3108  }
3109 
3115  memory::desc dst_desc(int idx) const {
3116  return query_md(query::dst_md, idx);
3117  }
3118 
3124  memory::desc weights_desc(int idx) const {
3125  return query_md(query::weights_md, idx);
3126  }
3127 
3133  memory::desc diff_src_desc(int idx) const {
3134  return query_md(query::diff_src_md, idx);
3135  }
3136 
3142  memory::desc diff_dst_desc(int idx) const {
3143  return query_md(query::diff_dst_md, idx);
3144  }
3145 
3152  return query_md(query::diff_weights_md, idx);
3153  }
3154 
3155  // Separate versions without the index argument for documentation
3156  // purposes.
3157 
3162  memory::desc src_desc() const { return src_desc(0); }
3163 
3168  memory::desc dst_desc() const { return dst_desc(0); }
3169 
3174  memory::desc weights_desc() const { return weights_desc(0); }
3175 
3181 
3187 
3193 
3199  return query_md(query::workspace_md, 0);
3200  }
3201 
3208  return query_md(query::scratchpad_md, 0);
3209  }
3210 
3214  dnnl_engine_t c_engine;
3217  0, &c_engine),
3218  "could not retrieve scratchpad engine from a primitive "
3219  "descriptor");
3220  return engine(c_engine, true);
3221  }
3222 
3226  const_dnnl_primitive_attr_t const_c_attr;
3227  error::wrap_c_api(dnnl_primitive_desc_get_attr(get(), &const_c_attr),
3228  "could not get attributes from a primitive descriptor");
3229  dnnl_primitive_attr_t c_attr;
3230  error::wrap_c_api(dnnl_primitive_attr_clone(&c_attr, const_c_attr),
3231  "could not clone primitive attributes");
3232  return primitive_attr(c_attr);
3233  }
3234 
3238  dnnl_primitive_kind_t kind;
3240  dnnl_query_primitive_kind, 0, (void *)&kind),
3241  "could not get primitive kind from a primitive descriptor");
3242  return static_cast<dnnl::primitive::kind>(kind);
3243  }
3244 
3245 protected:
3250  dnnl_primitive_desc_t new_pd;
3252  "could not clone a primitive descriptor");
3253  reset(new_pd);
3254  }
3255 
3271  : primitive_desc_base(pd, prim_kind, dnnl::prop_kind::undef) {}
3272 
3285  dnnl::primitive::kind prim_kind, dnnl::prop_kind aprop_kind)
3286  : primitive_desc_base(pd, prim_kind, aprop_kind, aprop_kind) {}
3287 
3302  dnnl::primitive::kind prim_kind, dnnl::prop_kind prop_kind1,
3303  dnnl::prop_kind prop_kind2) {
3304  // It is OK to pass an empty primitive descriptor
3305  if (pd == nullptr) return;
3306 
3307  dnnl_status_t rc;
3308 
3309  dnnl_primitive_kind_t c_prim_kind = convert_to_c(prim_kind);
3310  dnnl_prop_kind_t c_prop_kind1 = convert_to_c(prop_kind1);
3311  dnnl_prop_kind_t c_prop_kind2 = convert_to_c(prop_kind2);
3312 
3313  // Check that primitive kind matches
3314  dnnl_primitive_kind_t pd_kind;
3316  pd, dnnl_query_primitive_kind, 0, (void *)&pd_kind);
3318  rc, "could not get primitive kind from a primitive descriptor");
3319  if (pd_kind != c_prim_kind)
3320  DNNL_THROW_ERROR(dnnl_invalid_arguments,
3321  "primitive descriptor operation kind mismatch");
3322 
3323  // Check that propagation kind matches
3324  dnnl_prop_kind_t pd_prop_kind;
3326  pd, dnnl_query_prop_kind, 0, (void *)&pd_prop_kind);
3327 
3328  // Something went wrong
3329  if (rc != dnnl_success && rc != dnnl_unimplemented)
3330  DNNL_THROW_ERROR(dnnl_invalid_arguments,
3331  "could not get propagation kind from the primitive "
3332  "descriptor");
3333 
3334  // Everything is fine
3335  if ((rc == dnnl_unimplemented && c_prop_kind1 == dnnl_prop_kind_undef)
3336  || (rc == dnnl_success
3337  && (pd_prop_kind == c_prop_kind1
3338  || pd_prop_kind == c_prop_kind2))) {
3339  reset_with_clone(pd);
3340  return;
3341  }
3342 
3343  // We could get the propagation kind but there is a mismatch
3344  DNNL_THROW_ERROR(dnnl_invalid_arguments,
3345  "primitive descriptor propagation kind mismatch");
3346  }
3347 
3348  using base = primitive_desc_base;
3349 };
3350 
3352 
3361 
3363 struct reorder : public primitive {
3367 
3369  primitive_desc() = default;
3370 
3388  primitive_desc(const engine &src_engine, const memory::desc &src_md,
3389  const engine &dst_engine, const memory::desc &dst_md,
3390  const primitive_attr &attr = primitive_attr(),
3391  bool allow_empty = false) {
3392  dnnl_primitive_desc_t result;
3394  &src_md.data, src_engine.get(), &dst_md.data,
3395  dst_engine.get(), attr.get());
3396  if (!allow_empty)
3398  "could not create a primitive descriptor for a reorder "
3399  "primitive");
3401  }
3402 
3414  primitive_desc(const memory &src, const memory &dst,
3415  const primitive_attr &attr = primitive_attr(),
3416  bool allow_empty = false) {
3417  dnnl_primitive_desc_t result;
3418  auto src_md = src.get_desc();
3419  auto dst_md = dst.get_desc();
3421  &src_md.data, src.get_engine().get(), &dst_md.data,
3422  dst.get_engine().get(), attr.get());
3423  if (!allow_empty)
3425  "could not create a primitive descriptor for a reorder "
3426  "primitive");
3428  }
3429 
3436 
3441  }
3442 
3447  }
3448 
3450  memory::desc src_desc() const { return base::src_desc(0); }
3451 
3453  memory::desc dst_desc() const { return base::dst_desc(0); }
3454  };
3455 
3457  reorder() = default;
3458 
3461  reorder(const primitive_desc &pd) : primitive(pd.get()) {}
3462 
3470  reorder(const memory &src, const memory &dst,
3471  const primitive_attr &attr = primitive_attr())
3472  : primitive(primitive_desc(src, dst, attr).get()) {}
3473 
3474  using primitive::execute;
3475 
3482  void execute(const stream &astream, memory &src, memory &dst) const {
3483  primitive::execute(astream, {{DNNL_ARG_FROM, src}, {DNNL_ARG_TO, dst}});
3484  }
3485 };
3486 
3488 
3496 
3498 inline std::vector<dnnl_memory_desc_t> convert_to_c(
3499  const std::vector<memory::desc> &mems) {
3500  std::vector<dnnl_memory_desc_t> c_mems;
3501  c_mems.reserve(mems.size());
3502  for (const auto &s : mems)
3503  c_mems.push_back(s.data);
3504  return c_mems;
3505 }
3507 
3509 struct concat : public primitive {
3513 
3515  primitive_desc() = default;
3516 
3527  primitive_desc(const memory::desc &dst, int concat_dimension,
3528  const std::vector<memory::desc> &srcs, const engine &aengine,
3529  const primitive_attr &attr = primitive_attr()) {
3530  auto c_srcs = convert_to_c(srcs);
3531 
3532  dnnl_primitive_desc_t result;
3535  (int)c_srcs.size(), concat_dimension, c_srcs.data(),
3536  attr.get(), aengine.get()),
3537  "could not create a primitive descriptor for a concat "
3538  "primitive");
3539  reset(result);
3540  }
3541 
3554  primitive_desc(int concat_dimension,
3555  const std::vector<memory::desc> &srcs, const engine &aengine,
3556  const primitive_attr &attr = primitive_attr()) {
3557  auto c_api_srcs = convert_to_c(srcs);
3558 
3559  dnnl_primitive_desc_t result;
3561  dnnl_concat_primitive_desc_create(&result, nullptr,
3562  (int)c_api_srcs.size(), concat_dimension,
3563  c_api_srcs.data(), attr.get(), aengine.get()),
3564  "could not create a primitive descriptor for a concat "
3565  "primitive");
3566  reset(result);
3567  }
3568 
3575 
3577  memory::desc src_desc(int idx = 0) const { return base::src_desc(idx); }
3578 
3580  memory::desc dst_desc() const { return base::dst_desc(0); }
3581  };
3582 
3584  concat() = default;
3585 
3588  concat(const primitive_desc &pd) : primitive(pd.get()) {}
3589 };
3590 
3592 
3600 
3602 struct sum : public primitive {
3606 
3608  primitive_desc() = default;
3609 
3619  const std::vector<float> &scales,
3620  const std::vector<memory::desc> &srcs, const engine &aengine,
3621  const primitive_attr &attr = primitive_attr()) {
3622  validate_container_size(scales,
3623  "counts of scales and sources are not equal",
3624  (int)srcs.size(), (int)srcs.size());
3625 
3626  auto c_api_srcs = convert_to_c(srcs);
3627 
3628  dnnl_primitive_desc_t result;
3630  dnnl_sum_primitive_desc_create(&result, &dst.data,
3631  (int)c_api_srcs.size(), scales.data(),
3632  c_api_srcs.data(), attr.get(), aengine.get()),
3633  "could not create a primitive descriptor for a sum "
3634  "primitive");
3635  reset(result);
3636  }
3637 
3648  primitive_desc(const std::vector<float> &scales,
3649  const std::vector<memory::desc> &srcs, const engine &aengine,
3650  const primitive_attr &attr = primitive_attr()) {
3651  validate_container_size(scales,
3652  "counts of scales and sources are not equal",
3653  (int)srcs.size(), (int)srcs.size());
3654 
3655  auto c_api_srcs = convert_to_c(srcs);
3656  dnnl_primitive_desc_t result;
3658  dnnl_sum_primitive_desc_create(&result, nullptr,
3659  (int)c_api_srcs.size(), scales.data(),
3660  c_api_srcs.data(), attr.get(), aengine.get()),
3661  "could not create a primitive descriptor for a sum "
3662  "primitive");
3663  reset(result);
3664  }
3665 
3672 
3674  memory::desc src_desc(int idx = 0) const { return base::src_desc(idx); }
3675 
3677  memory::desc dst_desc() const { return base::dst_desc(0); }
3678  };
3679 
3681  sum() = default;
3682 
3685  sum(const primitive_desc &pd) : primitive(pd.get()) {}
3686 };
3687 
3689 
3692 
3697 
3698  primitive_desc() = default;
3699 
3723  const engine &aengine, const_dnnl_primitive_desc_t hint_fwd_pd,
3724  bool allow_empty = false)
3725  : allow_empty_(allow_empty) {
3726  dnnl_primitive_desc_iterator_t iterator = nullptr;
3728  desc, attr ? attr->get() : nullptr, aengine.get(), hint_fwd_pd);
3729  if (!allow_empty)
3731  status, "could not create a primitive descriptor iterator");
3732  pd_iterator.reset(iterator);
3733  fetch_impl();
3734  }
3735 
3740  bool next_impl() {
3742  = dnnl_primitive_desc_iterator_next(pd_iterator.get());
3743  if (status == dnnl_iterator_ends) return false;
3745  status, "could not advance a primitive descriptor iterator");
3746  fetch_impl();
3747  return true;
3748  }
3749 
3750 private:
3751  bool allow_empty_ = false;
3753  void fetch_impl() {
3755  pd_iterator.get(allow_empty_));
3756  error::wrap_c_api(pd != nullptr || allow_empty_ ? dnnl_success
3758  "could not fetch a primitive descriptor from a primitive "
3759  "descriptor iterator");
3760  reset(pd);
3761  }
3762 };
3763 
3765 
3775 
3779  struct desc {
3781 
3812  desc(prop_kind aprop_kind, algorithm aalgorithm,
3813  const memory::desc &src_desc, const memory::desc &weights_desc,
3814  const memory::desc &bias_desc, const memory::desc &dst_desc,
3815  const memory::dims &strides, const memory::dims &padding_l,
3816  const memory::dims &padding_r) {
3817  memory::validate_dims(strides, src_desc.data.ndims - 2);
3818  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
3819  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
3822  dnnl::convert_to_c(aprop_kind),
3823  convert_to_c(aalgorithm), &src_desc.data,
3824  &weights_desc.data, &bias_desc.data, &dst_desc.data,
3825  &strides[0], &padding_l[0], &padding_r[0]),
3826  "could not create a descriptor for a convolution forward "
3827  "propagation primitive");
3828  }
3829 
3858  desc(prop_kind aprop_kind, algorithm aalgorithm,
3859  const memory::desc &src_desc, const memory::desc &weights_desc,
3860  const memory::desc &dst_desc, const memory::dims &strides,
3861  const memory::dims &padding_l, const memory::dims &padding_r) {
3862  memory::validate_dims(strides, src_desc.data.ndims - 2);
3863  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
3864  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
3867  dnnl::convert_to_c(aprop_kind),
3868  convert_to_c(aalgorithm), &src_desc.data,
3869  &weights_desc.data, nullptr, &dst_desc.data,
3870  &strides[0], &padding_l[0], &padding_r[0]),
3871  "could not create a descriptor for a convolution forward "
3872  "propagation primitive");
3873  }
3874 
3907  desc(prop_kind aprop_kind, algorithm aalgorithm,
3908  const memory::desc &src_desc, const memory::desc &weights_desc,
3909  const memory::desc &bias_desc, const memory::desc &dst_desc,
3910  const memory::dims &strides, const memory::dims &dilates,
3911  const memory::dims &padding_l, const memory::dims &padding_r) {
3912  memory::validate_dims(strides, src_desc.data.ndims - 2);
3913  memory::validate_dims(dilates, src_desc.data.ndims - 2);
3914  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
3915  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
3917  dnnl::convert_to_c(aprop_kind),
3918  convert_to_c(aalgorithm), &src_desc.data,
3919  &weights_desc.data, &bias_desc.data,
3920  &dst_desc.data, &strides[0], &dilates[0],
3921  &padding_l[0], &padding_r[0]),
3922  "could not create a descriptor for a dilated convolution "
3923  "forward propagation primitive");
3924  }
3925 
3956  desc(prop_kind aprop_kind, algorithm aalgorithm,
3957  const memory::desc &src_desc, const memory::desc &weights_desc,
3958  const memory::desc &dst_desc, const memory::dims &strides,
3959  const memory::dims &dilates, const memory::dims &padding_l,
3960  const memory::dims &padding_r) {
3961  memory::validate_dims(strides, src_desc.data.ndims - 2);
3962  memory::validate_dims(dilates, src_desc.data.ndims - 2);
3963  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
3964  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
3966  dnnl::convert_to_c(aprop_kind),
3967  convert_to_c(aalgorithm), &src_desc.data,
3968  &weights_desc.data, nullptr,
3969  &dst_desc.data, &strides[0], &dilates[0],
3970  &padding_l[0], &padding_r[0]),
3971  "could not create a descriptor for a dilated convolution "
3972  "forward propagation primitive");
3973  }
3974  };
3975 
3979  primitive_desc() = default;
3980 
3991  primitive_desc(const desc &adesc, const engine &aengine,
3992  bool allow_empty = false)
3993  : dnnl::primitive_desc(
3994  &adesc.data, nullptr, aengine, nullptr, allow_empty) {}
3995 
4007  primitive_desc(const desc &adesc, const primitive_attr &attr,
4008  const engine &aengine, bool allow_empty = false)
4009  : dnnl::primitive_desc(
4010  &adesc.data, &attr, aengine, nullptr, allow_empty) {}
4011 
4019  : dnnl::primitive_desc(pd, dnnl::primitive::kind::convolution,
4022 
4024  memory::desc src_desc() const { return base::src_desc(0); }
4025 
4028 
4030  memory::desc dst_desc() const { return base::dst_desc(0); }
4031 
4037  };
4038 
4040  convolution_forward() = default;
4041 
4046 };
4047 
4050 
4052  struct desc {
4054 
4080  desc(algorithm aalgorithm, const memory::desc &diff_src_desc,
4081  const memory::desc &weights_desc,
4082  const memory::desc &diff_dst_desc, const memory::dims &strides,
4083  const memory::dims &padding_l, const memory::dims &padding_r) {
4084  memory::validate_dims(strides, diff_src_desc.data.ndims - 2);
4085  memory::validate_dims(padding_l, diff_src_desc.data.ndims - 2);
4086  memory::validate_dims(padding_r, diff_src_desc.data.ndims - 2);
4089  convert_to_c(aalgorithm), &diff_src_desc.data,
4090  &weights_desc.data, &diff_dst_desc.data,
4091  &strides[0], &padding_l[0], &padding_r[0]),
4092  "could not create a descriptor for a convolution backward "
4093  "propagation primitive");
4094  }
4095 
4123  desc(algorithm aalgorithm, const memory::desc &diff_src_desc,
4124  const memory::desc &weights_desc,
4125  const memory::desc &diff_dst_desc, const memory::dims &strides,
4126  const memory::dims &dilates, const memory::dims &padding_l,
4127  const memory::dims &padding_r) {
4128  memory::validate_dims(strides, diff_src_desc.data.ndims - 2);
4129  memory::validate_dims(dilates, diff_src_desc.data.ndims - 2);
4130  memory::validate_dims(padding_l, diff_src_desc.data.ndims - 2);
4131  memory::validate_dims(padding_r, diff_src_desc.data.ndims - 2);
4134  convert_to_c(aalgorithm), &diff_src_desc.data,
4135  &weights_desc.data, &diff_dst_desc.data,
4136  &strides[0], &dilates[0], &padding_l[0],
4137  &padding_r[0]),
4138  "could not create a descriptor for a dilated convolution "
4139  "backward propagation primitive");
4140  }
4141  };
4142 
4146  primitive_desc() = default;
4147 
4161  primitive_desc(const desc &adesc, const engine &aengine,
4162  const convolution_forward::primitive_desc &hint_fwd_pd,
4163  bool allow_empty = false)
4164  : dnnl::primitive_desc(&adesc.data, nullptr, aengine,
4165  hint_fwd_pd.get(), allow_empty) {}
4166 
4181  primitive_desc(const desc &adesc, const primitive_attr &attr,
4182  const engine &aengine,
4183  const convolution_forward::primitive_desc &hint_fwd_pd,
4184  bool allow_empty = false)
4185  : dnnl::primitive_desc(&adesc.data, &attr, aengine,
4186  hint_fwd_pd.get(), allow_empty) {}
4187 
4195  : dnnl::primitive_desc(pd, dnnl::primitive::kind::convolution,
4197 
4200 
4203 
4206  };
4207 
4209  convolution_backward_data() = default;
4210 
4215 };
4216 
4220  struct desc {
4222 
4250  desc(algorithm aalgorithm, const memory::desc &src_desc,
4251  const memory::desc &diff_weights_desc,
4252  const memory::desc &diff_bias_desc,
4253  const memory::desc &diff_dst_desc, const memory::dims &strides,
4254  const memory::dims &padding_l, const memory::dims &padding_r) {
4255  memory::validate_dims(strides, src_desc.data.ndims - 2);
4256  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
4257  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
4260  convert_to_c(aalgorithm), &src_desc.data,
4261  &diff_weights_desc.data, &diff_bias_desc.data,
4262  &diff_dst_desc.data, &strides[0], &padding_l[0],
4263  &padding_r[0]),
4264  "could not create a descriptor for a convolution weights "
4265  "update primitive");
4266  }
4267 
4293  desc(algorithm aalgorithm, const memory::desc &src_desc,
4294  const memory::desc &diff_weights_desc,
4295  const memory::desc &diff_dst_desc, const memory::dims &strides,
4296  const memory::dims &padding_l, const memory::dims &padding_r) {
4297  memory::validate_dims(strides, src_desc.data.ndims - 2);
4298  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
4299  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
4301  convert_to_c(aalgorithm), &src_desc.data,
4302  &diff_weights_desc.data, nullptr,
4303  &diff_dst_desc.data, &strides[0],
4304  &padding_l[0], &padding_r[0]),
4305  "could not create a descriptor for a convolution weights "
4306  "update primitive");
4307  }
4308 
4338  desc(algorithm aalgorithm, const memory::desc &src_desc,
4339  const memory::desc &diff_weights_desc,
4340  const memory::desc &diff_bias_desc,
4341  const memory::desc &diff_dst_desc, const memory::dims &strides,
4342  const memory::dims &dilates, const memory::dims &padding_l,
4343  const memory::dims &padding_r) {
4344  memory::validate_dims(strides, src_desc.data.ndims - 2);
4345  memory::validate_dims(dilates, src_desc.data.ndims - 2);
4346  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
4347  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
4350  convert_to_c(aalgorithm), &src_desc.data,
4351  &diff_weights_desc.data, &diff_bias_desc.data,
4352  &diff_dst_desc.data, &strides[0], &dilates[0],
4353  &padding_l[0], &padding_r[0]),
4354  "could not create a descriptor for a dilated convolution "
4355  "weights gradient primitive");
4356  }
4357 
4385  desc(algorithm aalgorithm, const memory::desc &src_desc,
4386  const memory::desc &diff_weights_desc,
4387  const memory::desc &diff_dst_desc, const memory::dims &strides,
4388  const memory::dims &dilates, const memory::dims &padding_l,
4389  const memory::dims &padding_r) {
4390  memory::validate_dims(strides, src_desc.data.ndims - 2);
4391  memory::validate_dims(dilates, src_desc.data.ndims - 2);
4392  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
4393  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
4396  convert_to_c(aalgorithm), &src_desc.data,
4397  &diff_weights_desc.data, nullptr,
4398  &diff_dst_desc.data, &strides[0], &dilates[0],
4399  &padding_l[0], &padding_r[0]),
4400  "could not create a descriptor for a dilated convolution "
4401  "weights gradient primitive");
4402  }
4403  };
4404 
4408  primitive_desc() = default;
4409 
4422  primitive_desc(const desc &adesc, const engine &aengine,
4423  const convolution_forward::primitive_desc &hint_fwd_pd,
4424  bool allow_empty = false)
4425  : dnnl::primitive_desc(&adesc.data, nullptr, aengine,
4426  hint_fwd_pd.get(), allow_empty) {}
4427 
4441  primitive_desc(const desc &adesc, const primitive_attr &attr,
4442  const engine &aengine,
4443  const convolution_forward::primitive_desc &hint_fwd_pd,
4444  bool allow_empty = false)
4445  : dnnl::primitive_desc(&adesc.data, &attr, aengine,
4446  hint_fwd_pd.get(), allow_empty) {}
4447 
4455  : dnnl::primitive_desc(pd, dnnl::primitive::kind::convolution,
4457 
4459  memory::desc src_desc() const { return base::src_desc(0); }
4460 
4463  return base::diff_weights_desc(0);
4464  }
4465 
4468 
4474  return base::diff_weights_desc(1);
4475  }
4476  };
4477 
4479  convolution_backward_weights() = default;
4480 
4485 };
4486 
4488 //
4496 
4500  struct desc {
4502 
4532  desc(prop_kind aprop_kind, algorithm aalgorithm,
4533  const memory::desc &src_desc, const memory::desc &weights_desc,
4534  const memory::desc &bias_desc, const memory::desc &dst_desc,
4535  const memory::dims &strides, const memory::dims &padding_l,
4536  const memory::dims &padding_r) {
4537  memory::validate_dims(strides, src_desc.data.ndims - 2);
4538  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
4539  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
4542  dnnl::convert_to_c(aprop_kind),
4543  convert_to_c(aalgorithm), &src_desc.data,
4544  &weights_desc.data, &bias_desc.data, &dst_desc.data,
4545  &strides[0], &padding_l[0], &padding_r[0]),
4546  "could not create a descriptor for a deconvolution forward "
4547  "propagation primitive");
4548  }
4549 
4577  desc(prop_kind aprop_kind, algorithm aalgorithm,
4578  const memory::desc &src_desc, const memory::desc &weights_desc,
4579  const memory::desc &dst_desc, const memory::dims &strides,
4580  const memory::dims &padding_l, const memory::dims &padding_r) {
4581  memory::validate_dims(strides, src_desc.data.ndims - 2);
4582  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
4583  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
4586  dnnl::convert_to_c(aprop_kind),
4587  convert_to_c(aalgorithm), &src_desc.data,
4588  &weights_desc.data, nullptr, &dst_desc.data,
4589  &strides[0], &padding_l[0], &padding_r[0]),
4590  "could not create a descriptor for a deconvolution forward "
4591  "propagation primitive");
4592  }
4593 
4625  desc(prop_kind aprop_kind, algorithm aalgorithm,
4626  const memory::desc &src_desc, const memory::desc &weights_desc,
4627  const memory::desc &bias_desc, const memory::desc &dst_desc,
4628  const memory::dims &strides, const memory::dims &dilates,
4629  const memory::dims &padding_l, const memory::dims &padding_r) {
4630  memory::validate_dims(strides, src_desc.data.ndims - 2);
4631  memory::validate_dims(dilates, src_desc.data.ndims - 2);
4632  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
4633  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
4635  &data, dnnl::convert_to_c(aprop_kind),
4636  convert_to_c(aalgorithm), &src_desc.data,
4637  &weights_desc.data, &bias_desc.data,
4638  &dst_desc.data, &strides[0], &dilates[0],
4639  &padding_l[0], &padding_r[0]),
4640  "could not create a descriptor for a dilated deconvolution "
4641  "forward propagation primitive");
4642  }
4643 
4673  desc(prop_kind aprop_kind, algorithm aalgorithm,
4674  const memory::desc &src_desc, const memory::desc &weights_desc,
4675  const memory::desc &dst_desc, const memory::dims &strides,
4676  const memory::dims &dilates, const memory::dims &padding_l,
4677  const memory::dims &padding_r) {
4678  memory::validate_dims(strides, src_desc.data.ndims - 2);
4679  memory::validate_dims(dilates, src_desc.data.ndims - 2);
4680  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
4681  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
4683  &data, dnnl::convert_to_c(aprop_kind),
4684  convert_to_c(aalgorithm), &src_desc.data,
4685  &weights_desc.data, nullptr,
4686  &dst_desc.data, &strides[0], &dilates[0],
4687  &padding_l[0], &padding_r[0]),
4688  "could not create a descriptor for a dilated deconvolution "
4689  "forward propagation primitive");
4690  }
4691  };
4692 
4696  primitive_desc() = default;
4697 
4708  primitive_desc(const desc &adesc, const engine &aengine,
4709  bool allow_empty = false)
4710  : dnnl::primitive_desc(
4711  &adesc.data, nullptr, aengine, nullptr, allow_empty) {}
4712 
4724  primitive_desc(const desc &adesc, const primitive_attr &attr,
4725  const engine &aengine, bool allow_empty = false)
4726  : dnnl::primitive_desc(
4727  &adesc.data, &attr, aengine, nullptr, allow_empty) {}
4728 
4736  : dnnl::primitive_desc(pd, dnnl::primitive::kind::deconvolution,
4739 
4741  memory::desc src_desc() const { return base::src_desc(0); }
4742 
4745 
4747  memory::desc dst_desc() const { return base::dst_desc(0); }
4748 
4751  };
4752 
4754  deconvolution_forward() = default;
4755 
4760 };
4761 
4765  struct desc {
4767 
4792  desc(algorithm aalgorithm, const memory::desc &diff_src_desc,
4793  const memory::desc &weights_desc,
4794  const memory::desc &diff_dst_desc, const memory::dims &strides,
4795  const memory::dims &padding_l, const memory::dims &padding_r) {
4796  memory::validate_dims(strides, diff_src_desc.data.ndims - 2);
4797  memory::validate_dims(padding_l, diff_src_desc.data.ndims - 2);
4798  memory::validate_dims(padding_r, diff_src_desc.data.ndims - 2);
4801  convert_to_c(aalgorithm), &diff_src_desc.data,
4802  &weights_desc.data, &diff_dst_desc.data,
4803  &strides[0], &padding_l[0], &padding_r[0]),
4804  "could not create a descriptor for a deconvolution "
4805  "backward propagation primitive");
4806  }
4807 
4834  desc(algorithm aalgorithm, const memory::desc &diff_src_desc,
4835  const memory::desc &weights_desc,
4836  const memory::desc &diff_dst_desc, const memory::dims &strides,
4837  const memory::dims &dilates, const memory::dims &padding_l,
4838  const memory::dims &padding_r) {
4839  memory::validate_dims(strides, diff_src_desc.data.ndims - 2);
4840  memory::validate_dims(dilates, diff_src_desc.data.ndims - 2);
4841  memory::validate_dims(padding_l, diff_src_desc.data.ndims - 2);
4842  memory::validate_dims(padding_r, diff_src_desc.data.ndims - 2);
4845  convert_to_c(aalgorithm), &diff_src_desc.data,
4846  &weights_desc.data, &diff_dst_desc.data,
4847  &strides[0], &dilates[0], &padding_l[0],
4848  &padding_r[0]),
4849  "could not create a descriptor for a dilated deconvolution "
4850  "backward propagation primitive");
4851  }
4852  };
4853 
4857  primitive_desc() = default;
4858 
4872  primitive_desc(const desc &adesc, const engine &aengine,
4873  const deconvolution_forward::primitive_desc &hint_fwd_pd,
4874  bool allow_empty = false)
4875  : dnnl::primitive_desc(&adesc.data, nullptr, aengine,
4876  hint_fwd_pd.get(), allow_empty) {}
4877 
4892  primitive_desc(const desc &adesc, const primitive_attr &attr,
4893  const engine &aengine,
4894  const deconvolution_forward::primitive_desc &hint_fwd_pd,
4895  bool allow_empty = false)
4896  : dnnl::primitive_desc(&adesc.data, &attr, aengine,
4897  hint_fwd_pd.get(), allow_empty) {}
4898 
4906  : dnnl::primitive_desc(pd, dnnl::primitive::kind::deconvolution,
4908 
4911 
4914 
4917  };
4918 
4920  deconvolution_backward_data() = default;
4921 
4926 };
4927 
4931  struct desc {
4933 
4960  desc(algorithm aalgorithm, const memory::desc &src_desc,
4961  const memory::desc &diff_weights_desc,
4962  const memory::desc &diff_bias_desc,
4963  const memory::desc &diff_dst_desc, const memory::dims &strides,
4964  const memory::dims &padding_l, const memory::dims &padding_r) {
4965  memory::validate_dims(strides, src_desc.data.ndims - 2);
4966  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
4967  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
4970  convert_to_c(aalgorithm), &src_desc.data,
4971  &diff_weights_desc.data, &diff_bias_desc.data,
4972  &diff_dst_desc.data, &strides[0], &padding_l[0],
4973  &padding_r[0]),
4974  "could not create a descriptor for a deconvolution weights "
4975  "update primitive");
4976  }
4977 
5002  desc(algorithm aalgorithm, const memory::desc &src_desc,
5003  const memory::desc &diff_weights_desc,
5004  const memory::desc &diff_dst_desc, const memory::dims &strides,
5005  const memory::dims &padding_l, const memory::dims &padding_r) {
5006  memory::validate_dims(strides, src_desc.data.ndims - 2);
5007  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
5008  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
5010  &data, convert_to_c(aalgorithm),
5011  &src_desc.data, &diff_weights_desc.data,
5012  nullptr, &diff_dst_desc.data, &strides[0],
5013  &padding_l[0], &padding_r[0]),
5014  "could not create a descriptor for a deconvolution weights "
5015  "update primitive");
5016  }
5017 
5046  desc(algorithm aalgorithm, const memory::desc &src_desc,
5047  const memory::desc &diff_weights_desc,
5048  const memory::desc &diff_bias_desc,
5049  const memory::desc &diff_dst_desc, const memory::dims &strides,
5050  const memory::dims &dilates, const memory::dims &padding_l,
5051  const memory::dims &padding_r) {
5052  memory::validate_dims(strides, src_desc.data.ndims - 2);
5053  memory::validate_dims(dilates, src_desc.data.ndims - 2);
5054  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
5055  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
5058  convert_to_c(aalgorithm), &src_desc.data,
5059  &diff_weights_desc.data, &diff_bias_desc.data,
5060  &diff_dst_desc.data, &strides[0], &dilates[0],
5061  &padding_l[0], &padding_r[0]),
5062  "could not create a descriptor for a dilated deconvolution "
5063  "weights gradient primitive");
5064  }
5065 
5092  desc(algorithm aalgorithm, const memory::desc &src_desc,
5093  const memory::desc &diff_weights_desc,
5094  const memory::desc &diff_dst_desc, const memory::dims &strides,
5095  const memory::dims &dilates, const memory::dims &padding_l,
5096  const memory::dims &padding_r) {
5097  memory::validate_dims(strides, src_desc.data.ndims - 2);
5098  memory::validate_dims(dilates, src_desc.data.ndims - 2);
5099  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
5100  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
5103  convert_to_c(aalgorithm), &src_desc.data,
5104  &diff_weights_desc.data, nullptr,
5105  &diff_dst_desc.data, &strides[0], &dilates[0],
5106  &padding_l[0], &padding_r[0]),
5107  "could not create a descriptor for a dilated deconvolution "
5108  "weights gradient primitive");
5109  }
5110  };
5111 
5115  primitive_desc() = default;
5116 
5130  primitive_desc(const desc &adesc, const engine &aengine,
5131  const deconvolution_forward::primitive_desc &hint_fwd_pd,
5132  bool allow_empty = false)
5133  : dnnl::primitive_desc(&adesc.data, nullptr, aengine,
5134  hint_fwd_pd.get(), allow_empty) {}
5135 
5150  primitive_desc(const desc &adesc, const primitive_attr &attr,
5151  const engine &aengine,
5152  const deconvolution_forward::primitive_desc &hint_fwd_pd,
5153  bool allow_empty = false)
5154  : dnnl::primitive_desc(&adesc.data, &attr, aengine,
5155  hint_fwd_pd.get(), allow_empty) {}
5156 
5164  : dnnl::primitive_desc(pd, dnnl::primitive::kind::deconvolution,
5166 
5168  memory::desc src_desc() const { return base::src_desc(0); }
5169 
5172  return base::diff_weights_desc(0);
5173  }
5174 
5177 
5180  return base::diff_weights_desc(1);
5181  }
5182  };
5183 
5185  deconvolution_backward_weights() = default;
5186 
5191 };
5192 
5194 
5203 
5205 struct lrn_forward : public primitive {
5207  struct desc {
5208  dnnl_lrn_desc_t data;
5209 
5223  desc(prop_kind aprop_kind, algorithm aalgorithm,
5224  const memory::desc &data_desc, memory::dim local_size,
5225  float alpha, float beta, float k = 1.f) {
5227  dnnl::convert_to_c(aprop_kind),
5228  convert_to_c(aalgorithm), &data_desc.data,
5229  local_size, alpha, beta, k),
5230  "could not create a descriptor for a lrn forward "
5231  "propagation primitive");
5232  }
5233  };
5234 
5238  primitive_desc() = default;
5239 
5249  primitive_desc(const desc &adesc, const engine &aengine,
5250  bool allow_empty = false)
5251  : dnnl::primitive_desc(
5252  &adesc.data, nullptr, aengine, nullptr, allow_empty) {}
5253 
5264  primitive_desc(const desc &adesc, const primitive_attr &attr,
5265  const engine &aengine, bool allow_empty = false)
5266  : dnnl::primitive_desc(
5267  &adesc.data, &attr, aengine, nullptr, allow_empty) {}
5268 
5276  : dnnl::primitive_desc(pd, dnnl::primitive::kind::lrn,
5279 
5281  memory::desc src_desc() const { return base::src_desc(0); }
5282 
5284  memory::desc dst_desc() const { return base::dst_desc(0); }
5285 
5288  };
5289 
5291  lrn_forward() = default;
5292 
5297 };
5298 
5300 struct lrn_backward : public primitive {
5302  struct desc {
5303  dnnl_lrn_desc_t data;
5304 
5317  desc(algorithm aalgorithm, const memory::desc &data_desc,
5318  const memory::desc &diff_data_desc, memory::dim local_size,
5319  float alpha, float beta, float k = 1.f) {
5321  dnnl_lrn_backward_desc_init(&data, convert_to_c(aalgorithm),
5322  &diff_data_desc.data, &data_desc.data, local_size,
5323  alpha, beta, k),
5324  "could not create a descriptor for a lrn backward "
5325  "propagation primitive");
5326  }
5327  };
5328 
5332  primitive_desc() = default;
5333 
5346  primitive_desc(const desc &adesc, const engine &aengine,
5347  const lrn_forward::primitive_desc &hint_fwd_pd,
5348  bool allow_empty = false)
5349  : dnnl::primitive_desc(&adesc.data, nullptr, aengine,
5350  hint_fwd_pd.get(), allow_empty) {}
5351 
5365  primitive_desc(const desc &adesc, const primitive_attr &attr,
5366  const engine &aengine,
5367  const lrn_forward::primitive_desc &hint_fwd_pd,
5368  bool allow_empty = false)
5369  : dnnl::primitive_desc(&adesc.data, &attr, aengine,
5370  hint_fwd_pd.get(), allow_empty) {}
5371 
5379  : dnnl::primitive_desc(pd, dnnl::primitive::kind::lrn,
5381 
5384 
5387 
5390  };
5391 
5393  lrn_backward() = default;
5394 
5399 };
5400 
5402 
5410 
5412 struct pooling_forward : public primitive {
5414  struct desc {
5415  dnnl_pooling_desc_t data;
5416 
5441  desc(prop_kind aprop_kind, algorithm aalgorithm,
5442  const memory::desc &src_desc, const memory::desc &dst_desc,
5443  const memory::dims &strides, const memory::dims &kernel,
5444  const memory::dims &padding_l, const memory::dims &padding_r) {
5445  memory::validate_dims(strides, src_desc.data.ndims - 2);
5446  memory::validate_dims(kernel, src_desc.data.ndims - 2);
5447  memory::validate_dims(padding_l, src_desc.data.ndims - 2);
5448  memory::validate_dims(padding_r, src_desc.data.ndims - 2);
5450  dnnl::convert_to_c(aprop_kind),
5451  convert_to_c(aalgorithm), &src_desc.data,
5452  &dst_desc.data, &strides[0], &kernel[0],
5453  &padding_l[0], &padding_r[0]),
5454  "could not create a descriptor for a pooling forward "
5455  "propagation primitive");
5456  }
5457  };
5458 
5462  primitive_desc() = default;
5463 
5473  primitive_desc(const desc &adesc, const engine &aengine,
5474  bool allow_empty = false)
5475  : dnnl::primitive_desc(
5476  &adesc.data, nullptr, aengine, nullptr, allow_empty) {}
5477 
5488  primitive_desc(const desc &adesc, const primitive_attr &attr,
5489  const engine &aengine, bool allow_empty = false)
5490  : dnnl::primitive_desc(
5491  &adesc.data, &attr, aengine, nullptr, allow_empty) {}
5492 
5500  : dnnl::primitive_desc(pd, dnnl::primitive::kind::pooling,
5503 
5505  memory::desc src_desc() const { return base::src_desc(0); }
5506 
5508  memory::desc dst_desc() const { return base::dst_desc(0); }
5509 
5512  };
5513 
5515  pooling_forward() = default;
5516 
5521 };
5522 
5524 struct pooling_backward : public primitive {
5526  struct desc {
5527  dnnl_pooling_desc_t data;
5528 
5550  desc(algorithm aalgorithm, const memory::desc &diff_src_desc,
5551  const memory::desc &diff_dst_desc, const memory::dims &strides,
5552  const memory::dims &kernel, const memory::dims &padding_l,
5553  const memory::dims &padding_r) {
5554  memory::validate_dims(strides, diff_src_desc.data.ndims - 2);
5555  memory::validate_dims(kernel, diff_src_desc.data.ndims - 2);
5556  memory::validate_dims(padding_l, diff_src_desc.data.ndims - 2);
5557  memory::validate_dims(padding_r, diff_src_desc.data.ndims - 2);
5560  convert_to_c(aalgorithm), &diff_src_desc.data,
5561  &diff_dst_desc.data, &strides[0], &kernel[0],
5562  &padding_l[0], &padding_r[0]),
5563  "could not create a descriptor for a pooling backward "
5564  "propagation primitive");
5565  }
5566  };
5567 
5571  primitive_desc() = default;
5572 
5585  primitive_desc(const desc &adesc, const engine &aengine,
5586  const pooling_forward::primitive_desc &hint_fwd_pd,
5587  bool allow_empty = false)
5588  : dnnl::primitive_desc(&adesc.data, nullptr, aengine,
5589  hint_fwd_pd.get(), allow_empty) {}
5590 
5604  primitive_desc(const desc &adesc, const primitive_attr &attr,
5605  const engine &aengine,
5606  const pooling_forward::primitive_desc &hint_fwd_pd,
5607  bool allow_empty = false)
5608  : dnnl::primitive_desc(&adesc.data, &attr, aengine,
5609  hint_fwd_pd.get(), allow_empty) {}
5610 
5618  : dnnl::primitive_desc(pd, dnnl::primitive::kind::pooling,
5620 
5623 
5626 
5629  };
5630 
5632  pooling_backward() = default;
5633 
5638 };
5639 
5641 
5662 
5664 struct eltwise_forward : public primitive {
5666  struct desc {
5667  dnnl_eltwise_desc_t data;
5668 
5681  desc(prop_kind aprop_kind, algorithm aalgorithm,
5682  const memory::desc &data_desc, float alpha = 0,
5683  float beta = 0) {
5685  dnnl::convert_to_c(aprop_kind),
5686  dnnl::convert_to_c(aalgorithm),
5687  &data_desc.data, alpha, beta),
5688  "could not create a descriptor for an eltwise forward "
5689  "propagation primitive");
5690  }
5691  };
5692 
5696  primitive_desc() = default;
5697 
5708  primitive_desc(const desc &adesc, const engine &aengine,
5709  bool allow_empty = false)
5710  : dnnl::primitive_desc(
5711  &adesc.data, nullptr, aengine, nullptr, allow_empty) {}
5712 
5724  primitive_desc(const desc &adesc, const primitive_attr &attr,
5725  const engine &aengine, bool allow_empty = false)
5726  : dnnl::primitive_desc(
5727  &adesc.data, &attr, aengine, nullptr, allow_empty) {}
5728 
5736  : dnnl::primitive_desc(pd, dnnl::primitive::kind::eltwise,
5739 
5741  memory::desc src_desc() const { return base::src_desc(0); }
5742 
5744  memory::desc dst_desc() const { return base::dst_desc(0); }
5745  };
5746 
5748  eltwise_forward() = default;
5749 
5754 };
5755 
5757 struct eltwise_backward : public primitive {
5759  struct desc {
5760  dnnl_eltwise_desc_t data;
5761 
5773  desc(algorithm aalgorithm, const memory::desc &diff_data_desc,
5774  const memory::desc &data_desc, float alpha = 0,
5775  float beta = 0) {
5778  dnnl::convert_to_c(aalgorithm),
5779  &diff_data_desc.data, &data_desc.data, alpha, beta),
5780  "could not create a descriptor for an eltwise backward "
5781  "propagation primitive");
5782  }
5783  };
5784 
5788  primitive_desc() = default;
5789 
5803  primitive_desc(const desc &adesc, const engine &aengine,
5804  const eltwise_forward::primitive_desc &hint_fwd_pd,
5805  bool allow_empty = false)
5806  : dnnl::primitive_desc(&adesc.data, nullptr, aengine,
5807  hint_fwd_pd.get(), allow_empty) {}
5808 
5823  primitive_desc(const desc &adesc, const primitive_attr &attr,
5824  const engine &aengine,
5825  const eltwise_forward::primitive_desc &hint_fwd_pd,
5826  bool allow_empty = false)
5827  : dnnl::primitive_desc(&adesc.data, &attr, aengine,
5828  hint_fwd_pd.get(), allow_empty) {}
5829 
5837  : dnnl::primitive_desc(pd, dnnl::primitive::kind::eltwise,
5839 
5841  memory::desc src_desc() const { return base::src_desc(0); }
5842 
5845 
5848  };
5849 
5851  eltwise_backward() = default;
5852 
5857 };
5858 
5860 
5868 
5870 struct softmax_forward : public primitive {
5872  struct desc {
5873  dnnl_softmax_desc_t data;
5874 
5876  desc() = default;
5877 
5886  desc(prop_kind aprop_kind, const memory::desc &data_desc,
5887  int softmax_axis) {
5889  dnnl::convert_to_c(aprop_kind),
5890  &data_desc.data, softmax_axis),
5891  "could not create a descriptor for a softmax forward "
5892  "propagation primitive");
5893  }
5894  };
5895 
5899  primitive_desc() = default;
5900 
5911  primitive_desc(const desc &adesc, const engine &aengine,
5912  bool allow_empty = false)
5913  : dnnl::primitive_desc(
5914  &adesc.data, nullptr, aengine, nullptr, allow_empty) {}
5915 
5927  primitive_desc(const desc &adesc, const primitive_attr &attr,
5928  const engine &aengine, bool allow_empty = false)
5929  : dnnl::primitive_desc(
5930  &adesc.data, &attr, aengine, nullptr, allow_empty) {}
5931 
5939  : dnnl::primitive_desc(pd, dnnl::primitive::kind::softmax,
5942 
5944  memory::desc src_desc() const { return base::src_desc(0); }
5945 
5947  memory::desc dst_desc() const { return base::dst_desc(0); }
5948  };
5949 
5951  softmax_forward() = default;
5952 
5957 };
5958 
5960 struct softmax_backward : public primitive {
5962  struct desc {
5963  dnnl_softmax_desc_t data;
5964 
5966  desc() = default;
5967 
5975  desc(const memory::desc &diff_data_desc, const memory::desc &data_desc,
5976  int softmax_axis) {
5978  dnnl_softmax_backward_desc_init(&data, &diff_data_desc.data,
5979  &data_desc.data, softmax_axis),
5980  "could not create a descriptor for a softmax backward "
5981  "propagation primitive");
5982  }
5983  };
5984 
5988  primitive_desc() = default;
5989 
6003  primitive_desc(const desc &adesc, const engine &aengine,
6004  const softmax_forward::primitive_desc &hint_fwd_pd,
6005  bool allow_empty = false)
6006  : dnnl::primitive_desc(&adesc.data, nullptr, aengine,
6007  hint_fwd_pd.get(), allow_empty) {}
6008 
6023  primitive_desc(const desc &adesc, const primitive_attr &attr,
6024  const engine &aengine,
6025  const softmax_forward::primitive_desc &hint_fwd_pd,
6026  bool allow_empty = false)
6027  : dnnl::primitive_desc(&adesc.data, &attr, aengine,
6028  hint_fwd_pd.get(), allow_empty) {}
6029 
6037  : dnnl::primitive_desc(pd, dnnl::primitive::kind::softmax,
6039 
6041  memory::desc dst_desc() const { return base::dst_desc(0); }
6042 
6045 
6048  };
6049 
6051  softmax_backward() = default;
6052 
6057 };
6058 
6060 
6068 
6072  struct desc {
6074 
6076  desc() = default;
6077 
6086  desc(prop_kind aprop_kind, const memory::desc &data_desc,
6087  int logsoftmax_axis) {
6089  dnnl::convert_to_c(aprop_kind),
6090  &data_desc.data, logsoftmax_axis),
6091  "could not create a descriptor for a logsoftmax forward "
6092  "propagation primitive");
6093  }
6094  };
6095 
6099  primitive_desc() = default;
6100 
6111  primitive_desc(const desc &adesc, const engine &aengine,
6112  bool allow_empty = false)
6113  : dnnl::primitive_desc(
6114  &adesc.data, nullptr, aengine, nullptr, allow_empty) {}
6115 
6127  primitive_desc(const desc &adesc, const primitive_attr &attr,
6128  const engine &aengine, bool allow_empty = false)
6129  : dnnl::primitive_desc(
6130  &adesc.data, &attr, aengine, nullptr, allow_empty) {}
6131 
6139  : dnnl::primitive_desc(pd,
6140  // Logsoftmax and softmax share the implementation and
6141  // currently report the same primitive kind. Hence this
6142  // must be softmax and not logsoftmax.
6143  dnnl::primitive::kind::softmax,
6146 
6148  memory::desc src_desc() const { return base::src_desc(0); }
6149 
6151  memory::desc dst_desc() const { return base::dst_desc(0); }
6152  };
6153 
6155  logsoftmax_forward() = default;
6156 
6161 };
6162 
6166  struct desc {
6168 
6170  desc() = default;
6171 
6179  desc(const memory::desc &diff_data_desc, const memory::desc &data_desc,
6180  int logsoftmax_axis) {
6182  &diff_data_desc.data, &data_desc.data,
6183  logsoftmax_axis),
6184  "could not create a descriptor for a logsoftmax backward "
6185  "propagation primitive");
6186  }
6187  };
6188 
6192  primitive_desc() = default;
6193 
6207  primitive_desc(const desc &adesc, const engine &aengine,
6208  const logsoftmax_forward::primitive_desc &hint_fwd_pd,
6209  bool allow_empty = false)
6210  : dnnl::primitive_desc(&adesc.data, nullptr, aengine,
6211  hint_fwd_pd.get(), allow_empty) {}
6212 
6227  primitive_desc(const desc &adesc, const primitive_attr &attr,
6228  const engine &aengine,
6229  const logsoftmax_forward::primitive_desc &hint_fwd_pd,
6230  bool allow_empty = false)
6231  : dnnl::primitive_desc(&adesc.data, &attr, aengine,
6232  hint_fwd_pd.get(), allow_empty) {}
6233 
6241  : dnnl::primitive_desc(pd,
6242  // Logsoftmax and softmax share the implementation and
6243  // currently report the same primitive kind. Hence this
6244  // must be softmax and not logsoftmax.
6245  dnnl::primitive::kind::softmax,
6247 
6249  memory::desc dst_desc() const { return base::dst_desc(0); }
6250 
6253 
6256  };
6257 
6259  logsoftmax_backward() = default;
6260 
6265 };
6266 
6268 
6288 
6292  struct desc {
6294 
6309  desc(prop_kind aprop_kind, const memory::desc &data_desc, float epsilon,
6310  normalization_flags flags) {
6313  dnnl::convert_to_c(aprop_kind), &data_desc.data,
6314  epsilon, convert_to_c(flags)),
6315  "could not create a descriptor for a batch normalization "
6316  "forward propagation primitive");
6317  }
6318  };
6319 
6324  primitive_desc() = default;
6325 
6336  primitive_desc(const desc &adesc, const engine &aengine,
6337  bool allow_empty = false)
6338  : dnnl::primitive_desc(
6339  &adesc.data, nullptr, aengine, nullptr, allow_empty) {}
6340 
6352  primitive_desc(const desc &adesc, const primitive_attr &attr,
6353  const engine &aengine, bool allow_empty = false)
6354  : dnnl::primitive_desc(
6355  &adesc.data, &attr, aengine, nullptr, allow_empty) {}
6356 
6364  : dnnl::primitive_desc(pd,
6365  dnnl::primitive::kind::batch_normalization,
6368 
6370  memory::desc src_desc() const { return base::src_desc(0); }
6371 
6373  memory::desc dst_desc() const { return base::dst_desc(0); }
6374 
6377 
6380 
6383  memory::desc mean_desc() const { return stat_desc(mean); }
6384 
6387  memory::desc variance_desc() const { return stat_desc(var); }
6388 
6389  private:
6390  enum {
6391  mean = 1,
6392  var = 2,
6393  };
6394  memory::desc stat_desc(int kind) const {
6399  &p),
6400  "could not retrieve a descriptor from a primitive "
6401  "descriptor for batch normalization forward propagation "
6402  "primitive");
6403  return query_md(p->flags & dnnl_use_global_stats ? query::src_md
6404  : query::dst_md,
6405  kind);
6406  }
6407  };
6408 
6410  batch_normalization_forward() = default;
6411 
6416 };
6417 
6421  struct desc {
6423 
6436  desc(prop_kind aprop_kind, const memory::desc &diff_data_desc,
6437  const memory::desc &data_desc, float epsilon,
6438  normalization_flags flags) {
6440  dnnl::convert_to_c(aprop_kind),
6441  &diff_data_desc.data, &data_desc.data,
6442  epsilon, convert_to_c(flags)),
6443  "could not create a descriptor for a batch normalization "
6444  "backward propagation primitive");
6445  }
6446  };
6447 
6452  primitive_desc() = default;
6453 
6467  primitive_desc(const desc &adesc, const engine &aengine,
6469  bool allow_empty = false)
6470  : dnnl::primitive_desc(&adesc.data, nullptr, aengine,
6471  hint_fwd_pd.get(), allow_empty) {}
6472 
6487  primitive_desc(const desc &adesc, const primitive_attr &attr,
6488  const engine &aengine,
6490  bool allow_empty = false)
6491  : dnnl::primitive_desc(&adesc.data, &attr, aengine,
6492  hint_fwd_pd.get(), allow_empty) {}
6493 
6501  : dnnl::primitive_desc(pd,
6502  dnnl::primitive::kind::batch_normalization,
6504  }
6505 
6507  memory::desc src_desc() const { return base::src_desc(0); }
6508 
6511 
6513  memory::desc dst_desc() const { return base::dst_desc(0); }
6514 
6517 
6520 
6523  return base::diff_weights_desc(0);
6524  }
6525 
6528 
6531  return query_md(query::src_md, 2);
6532  }
6533 
6536  };
6537 
6539  batch_normalization_backward() = default;
6540 
6545 };
6546 
6548 
6570 
6574  struct desc {
6576 
6588  desc(prop_kind aprop_kind, const memory::desc &data_desc,
6589  const memory::desc &stat_desc, float epsilon,
6590  normalization_flags flags) {
6593  dnnl::convert_to_c(aprop_kind), &data_desc.data,
6594  &stat_desc.data, epsilon, convert_to_c(flags)),
6595  "could not create a descriptor for a layer normalization "
6596  "forward propagation primitive");
6597  }
6598 
6609  desc(prop_kind aprop_kind, const memory::desc &data_desc, float epsilon,
6610  normalization_flags flags) {
6613  dnnl::convert_to_c(aprop_kind), &data_desc.data,
6614  nullptr, epsilon, convert_to_c(flags)),
6615  "could not create a descriptor for a layer normalization "
6616  "forward propagation primitive");
6617  }
6618  };
6619 
6624  primitive_desc() = default;
6625 
6636  primitive_desc(const desc &adesc, const engine &aengine,
6637  bool allow_empty = false)
6638  : dnnl::primitive_desc(
6639  &adesc.data, nullptr, aengine, nullptr, allow_empty) {}
6640 
6652  primitive_desc(const desc &adesc, const primitive_attr &attr,
6653  const engine &aengine, bool allow_empty = false)
6654  : dnnl::primitive_desc(
6655  &adesc.data, &attr, aengine, nullptr, allow_empty) {}
6656 
6664  : dnnl::primitive_desc(pd,
6665  dnnl::primitive::kind::layer_normalization,
6668 
6670  memory::desc src_desc() const { return base::src_desc(0); }
6671 
6673  memory::desc dst_desc() const { return base::dst_desc(0); }
6674 
6677 
6680 
6682  memory::desc mean_desc() const { return stat_desc(mean); }
6683 
6685  memory::desc variance_desc() const { return stat_desc(var); }
6686 
6687  private:
6688  enum {
6689  mean = 1,
6690  var = 2,
6691  };
6692  memory::desc stat_desc(int kind) const {
6697  &p),
6698  "could not retrieve a descriptor from a primitive "
6699  "descriptor for layer normalization forward propagation "
6700  "primitive");
6701  return query_md(p->flags & dnnl_use_global_stats ? query::src_md
6702  : query::dst_md,
6703  kind);
6704  }
6705  };
6706 
6708  layer_normalization_forward() = default;
6709 
6714 };
6715 
6719  struct desc {
6721 
6735  desc(prop_kind aprop_kind, const memory::desc &diff_data_desc,
6736  const memory::desc &data_desc, const memory::desc &stat_desc,
6737  float epsilon, normalization_flags flags) {
6740  dnnl::convert_to_c(aprop_kind),
6741  &diff_data_desc.data, &data_desc.data,
6742  &stat_desc.data, epsilon, convert_to_c(flags)),
6743  "could not create a descriptor for a batch normalization "
6744  "backward propagation primitive");
6745  }
6746 
6759  desc(prop_kind aprop_kind, const memory::desc &diff_data_desc,
6760  const memory::desc &data_desc, float epsilon,
6761  normalization_flags flags) {
6763  dnnl::convert_to_c(aprop_kind),
6764  &diff_data_desc.data, &data_desc.data,
6765  nullptr, epsilon, convert_to_c(flags)),
6766  "could not create a descriptor for a batch normalization "
6767  "backward propagation primitive");
6768  }
6769  };
6770 
6775  primitive_desc() = default;
6776 
6790  primitive_desc(const desc &adesc, const engine &aengine,
6792  bool allow_empty = false)
6793  : dnnl::primitive_desc(&adesc.data, nullptr, aengine,
6794  hint_fwd_pd.get(), allow_empty) {}
6795 
6810  primitive_desc(const desc &adesc, const primitive_attr &attr,
6811  const engine &aengine,
6813  bool allow_empty = false)
6814  : dnnl::primitive_desc(&adesc.data, &attr, aengine,
6815  hint_fwd_pd.get(), allow_empty) {}
6816 
6824  : dnnl::primitive_desc(pd,
6825  dnnl::primitive::kind::layer_normalization,
6827  }
6828 
6830  memory::desc src_desc() const { return base::src_desc(0); }
6831 
6834 
6836  memory::desc dst_desc() const { return base::dst_desc(0); }
6837 
6840 
6843 
6846  return base::diff_weights_desc(0);
6847  }
6848 
6851 
6854  return query_md(query::src_md, 2);
6855  }
6856 
6859  };
6860 
6862  layer_normalization_backward() = default;
6863 
6868 };
6869 
6871 
6879 
6883  struct desc {
6885 
6900  desc(prop_kind aprop_kind, const memory::desc &src_desc,
6901  const memory::desc &weights_desc, const memory::desc &bias_desc,
6902  const memory::desc &dst_desc) {
6904  dnnl::convert_to_c(aprop_kind),
6905  &src_desc.data, &weights_desc.data,
6906  &bias_desc.data, &dst_desc.data),
6907  "could not create a descriptor for an inner product "
6908  "forward propagation primitive");
6909  }
6910 
6924  desc(prop_kind aprop_kind, const memory::desc &src_desc,
6925  const memory::desc &weights_desc,
6926  const memory::desc &dst_desc) {
6929  dnnl::convert_to_c(aprop_kind), &src_desc.data,
6930  &weights_desc.data, nullptr, &dst_desc.data),
6931  "could not create a descriptor for an inner product "
6932  "forward propagation primitive");
6933  }
6934  };
6935 
6939  primitive_desc() = default;
6940 
6951  primitive_desc(const desc &adesc, const engine &aengine,
6952  bool allow_empty = false)
6953  : dnnl::primitive_desc(
6954  &adesc.data, nullptr, aengine, nullptr, allow_empty) {}
6955 
6967  primitive_desc(const desc &adesc, const primitive_attr &attr,
6968  const engine &aengine, bool allow_empty = false)
6969  : dnnl::primitive_desc(
6970  &adesc.data, &attr, aengine, nullptr, allow_empty) {}
6971 
6979  : dnnl::primitive_desc(pd, dnnl::primitive::kind::inner_product,
6982 
6984  memory::desc src_desc() const { return base::src_desc(0); }
6985 
6988 
6990  memory::desc dst_desc() const { return base::dst_desc(0); }
6991 
6994  };
6995 
6997  inner_product_forward() = default;
6998 
7003 };
7004 
7008  struct desc {
7010 
7021  desc(const memory::desc &diff_src_desc,
7022  const memory::desc &weights_desc,
7023  const memory::desc &diff_dst_desc) {
7025  &diff_src_desc.data, &weights_desc.data,
7026  &diff_dst_desc.data),
7027  "could not create a descriptor for an inner product "
7028  "backward propagation primitive");
7029  }
7030  };
7031 
7036  primitive_desc() = default;
7037 
7051  primitive_desc(const desc &adesc, const engine &aengine,
7052  const inner_product_forward::primitive_desc &hint_fwd_pd,
7053  bool allow_empty = false)
7054  : dnnl::primitive_desc(&adesc.data, nullptr, aengine,
7055  hint_fwd_pd.get(), allow_empty) {}
7056 
7071  primitive_desc(const desc &adesc, const primitive_attr &attr,
7072  const engine &aengine,
7073  const inner_product_forward::primitive_desc &hint_fwd_pd,
7074  bool allow_empty = false)
7075  : dnnl::primitive_desc(&adesc.data, &attr, aengine,
7076  hint_fwd_pd.get(), allow_empty) {}
7077 
7085  : dnnl::primitive_desc(pd, dnnl::primitive::kind::inner_product,
7087 
7090 
7093 
7096  };
7097 
7099  inner_product_backward_data() = default;
7100 
7105 };
7106 
7110  struct desc {
7112 
7124  desc(const memory::desc &src_desc,
7125  const memory::desc &diff_weights_desc,
7126  const memory::desc &diff_bias_desc,
7127  const memory::desc &diff_dst_desc) {
7130  &src_desc.data, &diff_weights_desc.data,
7131  &diff_bias_desc.data, &diff_dst_desc.data),
7132  "could not create a descriptor for an inner product "
7133  "weights gradient primitive");
7134  }
7135 
7146  desc(const memory::desc &src_desc,
7147  const memory::desc &diff_weights_desc,
7148  const memory::desc &diff_dst_desc) {
7151  &src_desc.data, &diff_weights_desc.data, nullptr,
7152  &diff_dst_desc.data),
7153  "could not create a descriptor for an inner product "
7154  "weights gradient primitive");
7155  }
7156  };
7157 
7161  primitive_desc() = default;
7162 
7176  primitive_desc(const desc &adesc, const engine &aengine,
7177  const inner_product_forward::primitive_desc &hint_fwd_pd,
7178  bool allow_empty = false)
7179  : dnnl::primitive_desc(&adesc.data, nullptr, aengine,
7180  hint_fwd_pd.get(), allow_empty) {}
7181 
7196  primitive_desc(const desc &adesc, const primitive_attr &attr,
7197  const engine &aengine,
7198  const inner_product_forward::primitive_desc &hint_fwd_pd,
7199  bool allow_empty = false)
7200  : dnnl::primitive_desc(&adesc.data, &attr, aengine,
7201  hint_fwd_pd.get(), allow_empty) {}
7202 
7210  : dnnl::primitive_desc(pd, dnnl::primitive::kind::inner_product,
7212 
7214  memory::desc src_desc() const { return base::src_desc(0); }
7215 
7218  return base::diff_weights_desc(0);
7219  }
7220 
7223 
7226  return base::diff_weights_desc(1);
7227  }
7228  };
7229 
7231  inner_product_backward_weights() = default;
7232 
7237 };
7238 
7240 
7248 
7251  using primitive_desc::primitive_desc;
7252 
7254  rnn_primitive_desc_base() = default;
7255 
7264  dnnl::prop_kind aprop_kind, dnnl::algorithm cell_kind)
7265  : rnn_primitive_desc_base(pd, aprop_kind, aprop_kind, cell_kind) {}
7266 
7271  }
7272 
7279  }
7280 
7285  }
7286 
7291  }
7292 
7297  }
7298 
7303  }
7304 
7309  }
7310 
7317  }
7318 
7323  }
7324 
7331  }
7332 
7337  }
7338 
7343  }
7344 
7351  }
7352 
7357  }
7358 
7363  }
7364 
7369  }
7370 
7374  return base::query_md(
7376  }
7377 
7381  return base::query_md(
7383  }
7384 
7391  }
7392 
7397  }
7398 
7405  }
7406 
7411  }
7412 
7413 protected:
7414  using rnn_base = rnn_primitive_desc_base;
7415 
7416  // (Deliberately not using doxygen comments)
7417  //
7418  // Constructs an RNN primitive descriptor base from a C API primitive
7419  // descriptor while checking that it actually describes the expected
7420  // primitive by comparing propagation and primitive kinds. Caller can
7421  // pass two options propagation kinds. This is typically used to check
7422  // that propagation kind is inference or training forward propagation.
7423  //
7424  // @param pd C API primitive descriptor.
7425  // @param prop_kind1 Expected propagation kind.
7426  // @param prop_kind2 Expected propagation kind.
7427  // @param cell_kind Expected cell kind.
7429  dnnl::prop_kind prop_kind1, dnnl::prop_kind prop_kind2,
7430  dnnl::algorithm cell_kind) {
7432  dnnl_status_t rc;
7434  error::wrap_c_api(rc,
7435  "could not retrieve a descriptor from a primitive descriptor "
7436  "for an RNN primitive");
7437 
7438  dnnl_prop_kind_t c_prop_kind1 = convert_to_c(prop_kind1);
7439  dnnl_prop_kind_t c_prop_kind2 = convert_to_c(prop_kind2);
7440  dnnl_alg_kind_t c_cell_kind = convert_to_c(cell_kind);
7441 
7442  bool ok = rnn_d->primitive_kind == dnnl_rnn
7443  && (rnn_d->prop_kind == c_prop_kind1
7444  || rnn_d->prop_kind == c_prop_kind2)
7445  && rnn_d->cell_kind == c_cell_kind;
7446 
7447  if (!ok)
7448  DNNL_THROW_ERROR(dnnl_invalid_arguments,
7449  "mismatch between expected and provided descriptors for an "
7450  "RNN primitive");
7451 
7452  reset_with_clone(pd);
7453  }
7454 };
7455 
7459  struct desc {
7460  dnnl_rnn_desc_t data;
7461 
7502  desc(prop_kind aprop_kind, algorithm activation,
7503  rnn_direction direction, const memory::desc &src_layer_desc,
7504  const memory::desc &src_iter_desc,
7505  const memory::desc &weights_layer_desc,
7506  const memory::desc &weights_iter_desc,
7507  const memory::desc &bias_desc,
7508  const memory::desc &dst_layer_desc,
7509  const memory::desc &dst_iter_desc,
7510  rnn_flags flags = rnn_flags::undef, float alpha = 0.0f,
7511  float beta = 0.0f) {
7514  dnnl::convert_to_c(aprop_kind),
7515  dnnl::convert_to_c(activation),
7516  dnnl::convert_to_c(direction), &src_layer_desc.data,
7517  &src_iter_desc.data, &weights_layer_desc.data,
7518  &weights_iter_desc.data, &bias_desc.data,
7519  &dst_layer_desc.data, &dst_iter_desc.data,
7520  dnnl::convert_to_c(flags), alpha, beta),
7521  "could not create a descriptor for a vanilla RNN forward "
7522  "propagation primitive");
7523  }
7524  };
7525 
7529  primitive_desc() = default;
7530 
7541  primitive_desc(const desc &adesc, const engine &aengine,
7542  bool allow_empty = false)
7544  &adesc.data, nullptr, aengine, nullptr, allow_empty) {}
7545 
7557  primitive_desc(const desc &adesc, const primitive_attr &attr,
7558  const engine &aengine, bool allow_empty = false)
7560  &adesc.data, &attr, aengine, nullptr, allow_empty) {}
7561 
7571  dnnl::algorithm::vanilla_rnn) {}
7572 
7575  return rnn_base::src_layer_desc();
7576  }
7577 
7580 
7584  }
7585 
7588  return rnn_base::weights_iter_desc();
7589  }
7590 
7593 
7596  return rnn_base::dst_layer_desc();
7597  }
7598 
7601 
7604  return rnn_base::workspace_desc();
7605  }
7606  };
7607 
7609  vanilla_rnn_forward() = default;
7610 
7615 };
7616 
7620  struct desc {
7621  dnnl_rnn_desc_t data;
7622 
7675  desc(prop_kind aprop_kind, algorithm activation,
7676  rnn_direction direction, const memory::desc &src_layer_desc,
7677  const memory::desc &src_iter_desc,
7678  const memory::desc &weights_layer_desc,
7679  const memory::desc &weights_iter_desc,
7680  const memory::desc &bias_desc,
7681  const memory::desc &dst_layer_desc,
7682  const memory::desc &dst_iter_desc,
7683  const memory::desc &diff_src_layer_desc,
7684  const memory::desc &diff_src_iter_desc,
7685  const memory::desc &diff_weights_layer_desc,
7686  const memory::desc &diff_weights_iter_desc,
7687  const memory::desc &diff_bias_desc,
7688  const memory::desc &diff_dst_layer_desc,
7689  const memory::desc &diff_dst_iter_desc,
7690  rnn_flags flags = rnn_flags::undef, float alpha = 0.0f,
7691  float beta = 0.0f) {
7694  dnnl::convert_to_c(aprop_kind),
7695  dnnl::convert_to_c(activation),
7696  dnnl::convert_to_c(direction), &src_layer_desc.data,
7697  &src_iter_desc.data, &weights_layer_desc.data,
7698  &weights_iter_desc.data, &bias_desc.data,
7699  &dst_layer_desc.data, &dst_iter_desc.data,
7700  &diff_src_layer_desc.data, &diff_src_iter_desc.data,
7701  &diff_weights_layer_desc.data,
7702  &diff_weights_iter_desc.data, &diff_bias_desc.data,
7703  &diff_dst_layer_desc.data, &diff_dst_iter_desc.data,
7704  dnnl::convert_to_c(flags), alpha, beta),
7705  "could not create a descriptor for a vanilla RNN backward "
7706  "propagation primitive");
7707  }
7708  };
7709 
7713  primitive_desc() = default;
7714 
7728  primitive_desc(const desc &adesc, const engine &aengine,
7729  const vanilla_rnn_forward::primitive_desc &hint_fwd_pd,
7730  bool allow_empty = false)
7731  : rnn_primitive_desc_base(&adesc.data, nullptr, aengine,
7732  hint_fwd_pd.get(), allow_empty) {}
7733 
7748  primitive_desc(const desc &adesc, const primitive_attr &attr,
7749  const engine &aengine,
7750  const vanilla_rnn_forward::primitive_desc &hint_fwd_pd,
7751  bool allow_empty = false)
7752  : rnn_primitive_desc_base(&adesc.data, &attr, aengine,
7753  hint_fwd_pd.get(), allow_empty) {}
7754 
7763  dnnl::algorithm::vanilla_rnn) {}
7764 
7767  return rnn_base::src_layer_desc();
7768  }
7769 
7772 
7776  }
7777 
7780  return rnn_base::weights_iter_desc();
7781  }
7782 
7785 
7788  return rnn_base::dst_layer_desc();
7789  }
7790 
7793 
7796  return rnn_base::workspace_desc();
7797  }
7798 
7802  }
7803 
7807  }
7808 
7812  }
7813 
7817  }
7818 
7821  return rnn_base::diff_bias_desc();
7822  }
7823 
7827  }
7828 
7832  }
7833  };
7834 
7836  vanilla_rnn_backward() = default;
7837 
7842 };
7843 
7845 struct lstm_forward : public primitive {
7847  struct desc {
7848  dnnl_rnn_desc_t data;
7849 
7898  desc(prop_kind aprop_kind, rnn_direction direction,
7899  const memory::desc &src_layer_desc,
7900  const memory::desc &src_iter_desc,
7901  const memory::desc &src_iter_c_desc,
7902  const memory::desc &weights_layer_desc,
7903  const memory::desc &weights_iter_desc,
7904  const memory::desc &weights_peephole_desc,
7905  const memory::desc &weights_projection_desc,
7906  const memory::desc &bias_desc,
7907  const memory::desc &dst_layer_desc,
7908  const memory::desc &dst_iter_desc,
7909  const memory::desc &dst_iter_c_desc,
7910  rnn_flags flags = rnn_flags::undef) {
7913  dnnl::convert_to_c(aprop_kind),
7914  dnnl::convert_to_c(direction), &src_layer_desc.data,
7915  &src_iter_desc.data, &src_iter_c_desc.data,
7916  &weights_layer_desc.data, &weights_iter_desc.data,
7917  &weights_peephole_desc.data,
7918  &weights_projection_desc.data, &bias_desc.data,
7919  &dst_layer_desc.data, &dst_iter_desc.data,
7920  &dst_iter_c_desc.data, dnnl::convert_to_c(flags)),
7921  "could not create a descriptor for an LSTM forward "
7922  "propagation primitive");
7923  }
7924 
7966  desc(prop_kind aprop_kind, rnn_direction direction,
7967  const memory::desc &src_layer_desc,
7968  const memory::desc &src_iter_desc,
7969  const memory::desc &src_iter_c_desc,
7970  const memory::desc &weights_layer_desc,
7971  const memory::desc &weights_iter_desc,
7972  const memory::desc &weights_peephole_desc,
7973  const memory::desc &bias_desc,
7974  const memory::desc &dst_layer_desc,
7975  const memory::desc &dst_iter_desc,
7976  const memory::desc &dst_iter_c_desc,
7977  rnn_flags flags = rnn_flags::undef) {
7980  dnnl::convert_to_c(aprop_kind),
7981  dnnl::convert_to_c(direction), &src_layer_desc.data,
7982  &src_iter_desc.data, &src_iter_c_desc.data,
7983  &weights_layer_desc.data, &weights_iter_desc.data,
7984  &weights_peephole_desc.data, &bias_desc.data,
7985  &dst_layer_desc.data, &dst_iter_desc.data,
7986  &dst_iter_c_desc.data, dnnl::convert_to_c(flags)),
7987  "could not create a descriptor for an LSTM forward "
7988  "propagation primitive");
7989  }
7990 
8027  desc(prop_kind aprop_kind, rnn_direction direction,
8028  const memory::desc &src_layer_desc,
8029  const memory::desc &src_iter_desc,
8030  const memory::desc &src_iter_c_desc,
8031  const memory::desc &weights_layer_desc,
8032  const memory::desc &weights_iter_desc,
8033  const memory::desc &bias_desc,
8034  const memory::desc &dst_layer_desc,
8035  const memory::desc &dst_iter_desc,
8036  const memory::desc &dst_iter_c_desc,
8037  rnn_flags flags = rnn_flags::undef) {
8040  dnnl::convert_to_c(aprop_kind),
8041  dnnl::convert_to_c(direction), &src_layer_desc.data,
8042  &src_iter_desc.data, &src_iter_c_desc.data,
8043  &weights_layer_desc.data, &weights_iter_desc.data,
8044  &bias_desc.data, &dst_layer_desc.data,
8045  &dst_iter_desc.data, &dst_iter_c_desc.data,
8046  dnnl::convert_to_c(flags)),
8047  "could not create a descriptor for an LSTM forward "
8048  "propagation primitive");
8049  }
8050  };
8051 
8055  primitive_desc() = default;
8056 
8066  primitive_desc(const desc &adesc, const engine &aengine,
8067  bool allow_empty = false)
8069  &adesc.data, nullptr, aengine, nullptr, allow_empty) {}
8070 
8081  primitive_desc(const desc &adesc, const primitive_attr &attr,
8082  const engine &aengine, bool allow_empty = false)
8084  &adesc.data, &attr, aengine, nullptr, allow_empty) {}
8085 
8096 
8099  return rnn_base::src_layer_desc();
8100  }
8101 
8104 
8107  return rnn_base::src_iter_c_desc();
8108  }
8109 
8113  }
8114 
8117  return rnn_base::weights_iter_desc();
8118  }
8119 
8123  }
8124 
8128  }
8129 
8132 
8135  return rnn_base::dst_layer_desc();
8136  }
8137 
8140 
8143  return rnn_base::dst_iter_c_desc();
8144  }
8145 
8148  return rnn_base::workspace_desc();
8149  }
8150  };
8151 
8153  lstm_forward() = default;
8154 
8159 };
8160 
8162 struct lstm_backward : public primitive {
8164  struct desc {
8165  dnnl_rnn_desc_t data;
8166 
8242  desc(prop_kind aprop_kind, rnn_direction direction,
8243  const memory::desc &src_layer_desc,
8244  const memory::desc &src_iter_desc,
8245  const memory::desc &src_iter_c_desc,
8246  const memory::desc &weights_layer_desc,
8247  const memory::desc &weights_iter_desc,
8248  const memory::desc &weights_peephole_desc,
8249  const memory::desc &weights_projection_desc,
8250  const memory::desc &bias_desc,
8251  const memory::desc &dst_layer_desc,
8252  const memory::desc &dst_iter_desc,
8253  const memory::desc &dst_iter_c_desc,
8254  const memory::desc &diff_src_layer_desc,
8255  const memory::desc &diff_src_iter_desc,
8256  const memory::desc &diff_src_iter_c_desc,
8257  const memory::desc &diff_weights_layer_desc,
8258  const memory::desc &diff_weights_iter_desc,
8259  const memory::desc &diff_weights_peephole_desc,
8260  const memory::desc &diff_weights_projection_desc,
8261  const memory::desc &diff_bias_desc,
8262  const memory::desc &diff_dst_layer_desc,
8263  const memory::desc &diff_dst_iter_desc,
8264  const memory::desc &diff_dst_iter_c_desc,
8265  rnn_flags flags = rnn_flags::undef) {
8268  dnnl::convert_to_c(aprop_kind),
8269  dnnl::convert_to_c(direction), &src_layer_desc.data,
8270  &src_iter_desc.data, &src_iter_c_desc.data,
8271  &weights_layer_desc.data, &weights_iter_desc.data,
8272  &weights_peephole_desc.data,
8273  &weights_projection_desc.data, &bias_desc.data,
8274  &dst_layer_desc.data, &dst_iter_desc.data,
8275  &dst_iter_c_desc.data, &diff_src_layer_desc.data,
8276  &diff_src_iter_desc.data,
8277  &diff_src_iter_c_desc.data,
8278  &diff_weights_layer_desc.data,
8279  &diff_weights_iter_desc.data,
8280  &diff_weights_peephole_desc.data,
8281  &diff_weights_projection_desc.data,
8282  &diff_bias_desc.data, &diff_dst_layer_desc.data,
8283  &diff_dst_iter_desc.data,
8284  &diff_dst_iter_c_desc.data,
8285  dnnl::convert_to_c(flags)),
8286  "could not create a descriptor for an LSTM backward "
8287  "propagation primitive");
8288  }
8289 
8354  desc(prop_kind aprop_kind, rnn_direction direction,
8355  const memory::desc &src_layer_desc,
8356  const memory::desc &src_iter_desc,
8357  const memory::desc &src_iter_c_desc,
8358  const memory::desc &weights_layer_desc,
8359  const memory::desc &weights_iter_desc,
8360  const memory::desc &weights_peephole_desc,
8361  const memory::desc &bias_desc,
8362  const memory::desc &dst_layer_desc,
8363  const memory::desc &dst_iter_desc,
8364  const memory::desc &dst_iter_c_desc,
8365  const memory::desc &diff_src_layer_desc,
8366  const memory::desc &diff_src_iter_desc,
8367  const memory::desc &diff_src_iter_c_desc,
8368  const memory::desc &diff_weights_layer_desc,
8369  const memory::desc &diff_weights_iter_desc,
8370  const memory::desc &diff_weights_peephole_desc,
8371  const memory::desc &diff_bias_desc,
8372  const memory::desc &diff_dst_layer_desc,
8373  const memory::desc &diff_dst_iter_desc,
8374  const memory::desc &diff_dst_iter_c_desc,
8375  rnn_flags flags = rnn_flags::undef) {
8378  dnnl::convert_to_c(aprop_kind),
8379  dnnl::convert_to_c(direction), &src_layer_desc.data,
8380  &src_iter_desc.data, &src_iter_c_desc.data,
8381  &weights_layer_desc.data, &weights_iter_desc.data,
8382  &weights_peephole_desc.data, &bias_desc.data,
8383  &dst_layer_desc.data, &dst_iter_desc.data,
8384  &dst_iter_c_desc.data, &diff_src_layer_desc.data,
8385  &diff_src_iter_desc.data,
8386  &diff_src_iter_c_desc.data,
8387  &diff_weights_layer_desc.data,
8388  &diff_weights_iter_desc.data,
8389  &diff_weights_peephole_desc.data,
8390  &diff_bias_desc.data, &diff_dst_layer_desc.data,
8391  &diff_dst_iter_desc.data,
8392  &diff_dst_iter_c_desc.data,
8393  dnnl::convert_to_c(flags)),
8394  "could not create a descriptor for an LSTM backward "
8395  "propagation primitive");
8396  }
8397 
8453  desc(prop_kind aprop_kind, rnn_direction direction,
8454  const memory::desc &src_layer_desc,
8455  const memory::desc &src_iter_desc,
8456  const memory::desc &src_iter_c_desc,
8457  const memory::desc &weights_layer_desc,
8458  const memory::desc &weights_iter_desc,
8459  const memory::desc &bias_desc,
8460  const memory::desc &dst_layer_desc,
8461  const memory::desc &dst_iter_desc,
8462  const memory::desc &dst_iter_c_desc,
8463  const memory::desc &diff_src_layer_desc,
8464  const memory::desc &diff_src_iter_desc,
8465  const memory::desc &diff_src_iter_c_desc,
8466  const memory::desc &diff_weights_layer_desc,
8467  const memory::desc &diff_weights_iter_desc,
8468  const memory::desc &diff_bias_desc,
8469  const memory::desc &diff_dst_layer_desc,
8470  const memory::desc &diff_dst_iter_desc,
8471  const memory::desc &diff_dst_iter_c_desc,
8472  rnn_flags flags = rnn_flags::undef) {
8475  dnnl::convert_to_c(aprop_kind),
8476  dnnl::convert_to_c(direction), &src_layer_desc.data,
8477  &src_iter_desc.data, &src_iter_c_desc.data,
8478  &weights_layer_desc.data, &weights_iter_desc.data,
8479  &bias_desc.data, &dst_layer_desc.data,
8480  &dst_iter_desc.data, &dst_iter_c_desc.data,
8481  &diff_src_layer_desc.data, &diff_src_iter_desc.data,
8482  &diff_src_iter_c_desc.data,
8483  &diff_weights_layer_desc.data,
8484  &diff_weights_iter_desc.data, &diff_bias_desc.data,
8485  &diff_dst_layer_desc.data, &diff_dst_iter_desc.data,
8486  &diff_dst_iter_c_desc.data,
8487  dnnl::convert_to_c(flags)),
8488  "could not create a descriptor for an LSTM backward "
8489  "propagation primitive");
8490  }
8491  };
8492 
8496  primitive_desc() = default;
8497 
8510  primitive_desc(const desc &adesc, const engine &aengine,
8511  const lstm_forward::primitive_desc &hint_fwd_pd,
8512  bool allow_empty = false)
8513  : rnn_primitive_desc_base(&adesc.data, nullptr, aengine,
8514  hint_fwd_pd.get(), allow_empty) {}
8515 
8529  primitive_desc(const desc &adesc, const primitive_attr &attr,
8530  const engine &aengine,
8531  const lstm_forward::primitive_desc &hint_fwd_pd,
8532  bool allow_empty = false)
8533  : rnn_primitive_desc_base(&adesc.data, &attr, aengine,
8534  hint_fwd_pd.get(), allow_empty) {}
8535 
8545 
8548  return rnn_base::src_layer_desc();
8549  }
8550 
8553 
8556  return rnn_base::src_iter_c_desc();
8557  }
8558 
8562  }
8563 
8566  return rnn_base::weights_iter_desc();
8567  }
8568 
8572  }
8573 
8577  }
8578 
8581 
8584  return rnn_base::dst_layer_desc();
8585  }
8586 
8589 
8592  return rnn_base::dst_iter_c_desc();
8593  }
8594 
8597  return rnn_base::workspace_desc();
8598  }
8599 
8603  }
8604 
8608  }
8609 
8613  }
8614 
8618  }
8619 
8623  }
8624 
8628  }
8629 
8633  }
8634 
8637  return rnn_base::diff_bias_desc();
8638  }
8639 
8643  }
8644 
8648  }
8649 
8653  }
8654  };
8655 
8657  lstm_backward() = default;
8658 
8663 };
8664 
8666 struct gru_forward : public primitive {
8668  struct desc {
8669  dnnl_rnn_desc_t data;
8670 
8703  desc(prop_kind aprop_kind, rnn_direction direction,
8704  const memory::desc &src_layer_desc,
8705  const memory::desc &src_iter_desc,
8706  const memory::desc &weights_layer_desc,
8707  const memory::desc &weights_iter_desc,
8708  const memory::desc &bias_desc,
8709  const memory::desc &dst_layer_desc,
8710  const memory::desc &dst_iter_desc,
8711  rnn_flags flags = rnn_flags::undef) {
8714  dnnl::convert_to_c(aprop_kind),
8715  dnnl::convert_to_c(direction), &src_layer_desc.data,
8716  &src_iter_desc.data, &weights_layer_desc.data,
8717  &weights_iter_desc.data, &bias_desc.data,
8718  &dst_layer_desc.data, &dst_iter_desc.data,
8719  dnnl::convert_to_c(flags)),
8720  "could not create a descriptor for a GRU forward "
8721  "propagation primitive");
8722  }
8723  };
8724 
8728  primitive_desc() = default;
8729 
8739  primitive_desc(const desc &adesc, const engine &aengine,
8740  bool allow_empty = false)
8742  &adesc.data, nullptr, aengine, nullptr, allow_empty) {}
8743