mxnet
utils.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
25 #ifndef MXNET_COMMON_UTILS_H_
26 #define MXNET_COMMON_UTILS_H_
27 
28 #include <dmlc/logging.h>
29 #include <dmlc/omp.h>
30 #include <nnvm/graph.h>
31 #include <mxnet/engine.h>
32 #include <mxnet/ndarray.h>
33 #include <mxnet/op_attr_types.h>
34 #include <mxnet/graph_attr_types.h>
35 #include <nnvm/graph_attr_types.h>
36 
37 #include <memory>
38 #include <vector>
39 #include <type_traits>
40 #include <utility>
41 #include <random>
42 #include <string>
43 #include <thread>
44 #include <algorithm>
45 #include <functional>
46 #include <limits>
47 
48 #include "../operator/mxnet_op.h"
49 #if MXNET_USE_MKLDNN == 1
50 #include "../operator/nn/mkldnn/mkldnn_base-inl.h"
51 #endif
52 
53 namespace mxnet {
54 namespace common {
55 
61  template<typename DType, typename IType>
62  MSHADOW_XINLINE static void Map(int i, DType* out, const IType* indptr,
63  const nnvm::dim_t end, const nnvm::dim_t idx_size) {
64  if (indptr[i+1] < 0 || indptr[i+1] < indptr[i] ||
65  (i == 0 && indptr[i] != 0) ||
66  (i == end - 1 && indptr[end] != idx_size))
67  *out = kCSRIndPtrErr;
68  }
69 };
70 
75 struct csr_idx_check {
76  template<typename DType, typename IType, typename RType>
77  MSHADOW_XINLINE static void Map(int i, DType* out, const IType* idx,
78  const RType* indptr, const nnvm::dim_t ncols) {
79  for (RType j = indptr[i]; j < indptr[i+1]; j++) {
80  if (idx[j] >= ncols || idx[j] < 0 ||
81  (j < indptr[i+1] - 1 && idx[j] >= idx[j+1])) {
82  *out = kCSRIdxErr;
83  break;
84  }
85  }
86  }
87 };
88 
93 struct rsp_idx_check {
94  template<typename DType, typename IType>
95  MSHADOW_XINLINE static void Map(int i, DType* out, const IType* idx,
96  const nnvm::dim_t end, const nnvm::dim_t nrows) {
97  if ((i < end && idx[i+1] <= idx[i])
98  || idx[i] < 0 || idx[i] >= nrows)
99  *out = kRSPIdxErr;
100  }
101 };
102 
103 template<typename xpu>
104 void CheckFormatWrapper(const RunContext &rctx, const NDArray &input,
105  const TBlob &err_cpu, const bool full_check);
106 
115 template<typename xpu>
116 void CheckFormatCSRImpl(const RunContext &rctx, const NDArray &input,
117  const TBlob &err_cpu, const bool full_check) {
118  using namespace op::mxnet_op;
119  CHECK_EQ(input.storage_type(), kCSRStorage)
120  << "CheckFormatCSRImpl is for CSRNDArray";
121  const TShape shape = input.shape();
122  const TShape idx_shape = input.aux_shape(csr::kIdx);
123  const TShape indptr_shape = input.aux_shape(csr::kIndPtr);
124  const TShape storage_shape = input.storage_shape();
125  if ((shape.ndim() != 2) ||
126  (idx_shape.ndim() != 1 || indptr_shape.ndim() != 1 || storage_shape.ndim() != 1) ||
127  (indptr_shape[0] != shape[0] + 1) ||
128  (idx_shape[0] != storage_shape[0])) {
129  MSHADOW_TYPE_SWITCH(err_cpu.type_flag_, DType, {
130  DType* err = err_cpu.dptr<DType>();
131  *err = kCSRShapeErr;
132  });
133  return;
134  }
135  if (full_check) {
136  MSHADOW_TYPE_SWITCH(err_cpu.type_flag_, DType, {
137  MSHADOW_IDX_TYPE_SWITCH(input.aux_type(csr::kIndPtr), RType, {
138  MSHADOW_IDX_TYPE_SWITCH(input.aux_type(csr::kIdx), IType, {
139  mshadow::Stream<xpu> *s = rctx.get_stream<xpu>();
140  NDArray ret_xpu = NDArray(mshadow::Shape1(1),
141  rctx.get_ctx(), false, err_cpu.type_flag_);
142  TBlob val_xpu = ret_xpu.data();
143  Kernel<set_to_int<kNormalErr>, xpu>::Launch(s, val_xpu.Size(), val_xpu.dptr<DType>());
144  Kernel<csr_indptr_check, xpu>::Launch(s, indptr_shape[0] - 1, val_xpu.dptr<DType>(),
145  input.aux_data(csr::kIndPtr).dptr<RType>(),
146  indptr_shape[0] - 1, idx_shape[0]);
147  // no need to check indices if indices are empty
148  if (idx_shape[0] != 0) {
149  Kernel<csr_idx_check, xpu>::Launch(s, indptr_shape[0] - 1, val_xpu.dptr<DType>(),
150  input.aux_data(csr::kIdx).dptr<IType>(),
151  input.aux_data(csr::kIndPtr).dptr<RType>(), shape[1]);
152  }
153  mshadow::Copy(err_cpu.get<cpu, 1, DType>(),
154  val_xpu.get<xpu, 1, DType>(s), s);
155  });
156  });
157  });
158  }
159 }
160 
169 template<typename xpu>
170 void CheckFormatRSPImpl(const RunContext &rctx, const NDArray &input,
171  const TBlob &err_cpu, const bool full_check) {
172  using namespace op::mxnet_op;
173  CHECK_EQ(input.storage_type(), kRowSparseStorage)
174  << "CheckFormatRSPImpl is for RSPNDArray";
175  const TShape idx_shape = input.aux_shape(rowsparse::kIdx);
176  if (idx_shape[0] != input.storage_shape()[0]) {
177  MSHADOW_TYPE_SWITCH(err_cpu.type_flag_, DType, {
178  DType* err = err_cpu.dptr<DType>();
179  *err = kRSPShapeErr;
180  });
181  return;
182  }
183  if (idx_shape[0] == 0) {
184  return;
185  }
186  if (full_check) {
187  MSHADOW_TYPE_SWITCH(err_cpu.type_flag_, DType, {
188  MSHADOW_IDX_TYPE_SWITCH(input.aux_type(rowsparse::kIdx), IType, {
189  mshadow::Stream<xpu> *s = rctx.get_stream<xpu>();
190  NDArray ret_xpu = NDArray(mshadow::Shape1(1),
191  rctx.get_ctx(), false, err_cpu.type_flag_);
192  TBlob val_xpu = ret_xpu.data();
193  Kernel<set_to_int<kNormalErr>, xpu>::Launch(s, val_xpu.Size(), val_xpu.dptr<DType>());
194 
195  Kernel<rsp_idx_check, xpu>::Launch(s, idx_shape[0],
196  val_xpu.dptr<DType>(), input.aux_data(rowsparse::kIdx).dptr<IType>(),
197  idx_shape[0] - 1, input.shape()[0]);
198  mshadow::Copy(err_cpu.get<cpu, 1, DType>(),
199  val_xpu.get<xpu, 1, DType>(s), s);
200  });
201  });
202  }
203 }
204 
205 template<typename xpu>
206 void CheckFormatImpl(const RunContext &rctx, const NDArray &input,
207  const TBlob &err_cpu, const bool full_check) {
208  int stype = input.storage_type();
209  if (stype == kCSRStorage) {
210  CheckFormatCSRImpl<xpu>(rctx, input, err_cpu, full_check);
211  } else if (stype == kRowSparseStorage) {
212  CheckFormatRSPImpl<xpu>(rctx, input, err_cpu, full_check);
213  } else if (stype == kDefaultStorage) {
214  // no-op for default storage
215  } else {
216  LOG(FATAL) << "Unknown storage type " << stype;
217  }
218 }
219 
223 template<typename xpu>
224 void SparseRetainOpForwardRspWrapper(mshadow::Stream<xpu> *s,
225  const NDArray& input_nd,
226  const TBlob& idx_data,
227  const OpReqType req,
228  NDArray* output_nd);
229 
230 /* \brief Casts tensor storage type to the new type.
231  */
232 template<typename xpu>
233 void CastStorageDispatch(const OpContext& ctx, const NDArray& input, const NDArray& output);
234 
238 inline bool ContainsOnlyStorage(const StorageTypeVector& vstorage,
239  const NDArrayStorageType stype) {
240  if (!vstorage.empty()) {
241  for (const auto& i : vstorage) {
242  if (i != stype) return false;
243  }
244  return true;
245  }
246  return false;
247 }
248 
253 inline bool ContainsOnlyStorage(const StorageTypeVector& vstorage,
254  const NDArrayStorageType stype1,
255  const NDArrayStorageType stype2,
256  bool *has_both) {
257  if (has_both) {
258  *has_both = false;
259  }
260  if (!vstorage.empty()) {
261  uint8_t has = 0;
262  for (const auto i : vstorage) {
263  if (i == stype1) {
264  has |= 1;
265  } else if (i == stype2) {
266  has |= 2;
267  } else {
268  return false;
269  }
270  }
271  if (has_both) {
272  *has_both = has == 3;
273  }
274  return true;
275  }
276  return false;
277 }
278 
282 inline bool ContainsOnlyStorage(const std::vector<NDArray>& ndarrays,
283  const NDArrayStorageType stype) {
284  if (!ndarrays.empty()) {
285  for (const auto& nd : ndarrays) {
286  if (nd.storage_type() != stype) {
287  return false;
288  }
289  }
290  return true;
291  }
292  return false;
293 }
294 
298 inline bool ContainsOnlyStorage(const std::vector<NDArray>& ndarrays,
299  const NDArrayStorageType stype1,
300  const NDArrayStorageType stype2,
301  bool *has_both) {
302  if (has_both) {
303  *has_both = false;
304  }
305  if (!ndarrays.empty()) {
306  uint8_t has = 0;
307  for (const auto& nd : ndarrays) {
308  const NDArrayStorageType stype = nd.storage_type();
309  if (stype == stype1) {
310  has |= 1;
311  } else if (stype == stype2) {
312  has |= 2;
313  } else {
314  return false;
315  }
316  }
317  if (has_both) {
318  *has_both = has == 3;
319  }
320  return true;
321  }
322  return false;
323 }
324 
328 inline bool ContainsStorageType(const std::vector<NDArray>& ndarrays,
329  const NDArrayStorageType stype) {
330  if (!ndarrays.empty()) {
331  for (const auto& nd : ndarrays) {
332  if (nd.storage_type() == stype) {
333  return true;
334  }
335  }
336  }
337  return false;
338 }
339 
343 inline bool ContainsStorageType(const std::vector<int>& ndstypes,
344  const NDArrayStorageType stype) {
345  if (!ndstypes.empty()) {
346  for (const auto& ndstype : ndstypes) {
347  if (ndstype == stype) {
348  return true;
349  }
350  }
351  }
352  return false;
353 }
354 
356 inline std::string dispatch_mode_string(const DispatchMode x) {
357  switch (x) {
359  return "fcompute";
361  return "fcompute_ex";
363  return "fcompute_fallback";
365  return "variable";
367  return "undefined";
368  }
369  return "unknown";
370 }
371 
372 
374 inline std::string stype_string(const int x) {
375  switch (x) {
376  case kDefaultStorage:
377  return "default";
378  case kCSRStorage:
379  return "csr";
380  case kRowSparseStorage:
381  return "row_sparse";
382  }
383  return "unknown";
384 }
385 
387 inline std::string dev_type_string(const int dev_type) {
388  switch (dev_type) {
389  case Context::kCPU:
390  return "cpu";
391  case Context::kGPU:
392  return "gpu";
393  case Context::kCPUPinned:
394  return "cpu_pinned";
395  case Context::kCPUShared:
396  return "cpu_shared";
397  }
398  return "unknown";
399 }
400 
402 inline std::string operator_stype_string(const nnvm::NodeAttrs& attrs,
403  const int dev_mask,
404  const std::vector<int>& in_attrs,
405  const std::vector<int>& out_attrs) {
406  std::ostringstream os;
407  os << "operator = " << attrs.op->name
408  << "\ninput storage types = [";
409  for (const int attr : in_attrs) {
410  os << stype_string(attr) << ", ";
411  }
412  os << "]\n"
413  << "output storage types = [";
414  for (const int attr : out_attrs) {
415  os << stype_string(attr) << ", ";
416  }
417  os << "]\n"
418  << "params = {";
419  for (auto kv : attrs.dict) {
420  os << "\"" << kv.first << "\" : " << kv.second << ", ";
421  }
422  os << "}\n"
423  << "context.dev_mask = " << dev_type_string(dev_mask);
424  return os.str();
425 }
426 
428 inline std::string operator_string(const nnvm::NodeAttrs& attrs,
429  const OpContext& ctx,
430  const std::vector<NDArray>& inputs,
431  const std::vector<OpReqType>& req,
432  const std::vector<NDArray>& outputs) {
433  std::string result = "";
434  std::vector<int> in_stypes;
435  std::vector<int> out_stypes;
436  in_stypes.reserve(inputs.size());
437  out_stypes.reserve(outputs.size());
438  auto xform = [](const NDArray arr) -> int { return arr.storage_type(); };
439  std::transform(inputs.begin(), inputs.end(), std::back_inserter(in_stypes), xform);
440  std::transform(outputs.begin(), outputs.end(), std::back_inserter(out_stypes), xform);
441  result += operator_stype_string(attrs, ctx.run_ctx.ctx.dev_mask(), in_stypes, out_stypes);
442  return result;
443 }
444 
446 inline void LogOnce(const std::string& message) {
447  typedef dmlc::ThreadLocalStore<std::unordered_set<std::string>> LogStore;
448  auto log_store = LogStore::Get();
449  if (log_store->find(message) == log_store->end()) {
450  LOG(INFO) << message;
451  log_store->insert(message);
452  }
453 }
454 
457 inline void LogStorageFallback(const nnvm::NodeAttrs& attrs,
458  const int dev_mask,
459  const std::vector<int>* in_attrs,
460  const std::vector<int>* out_attrs) {
461  static bool log = dmlc::GetEnv("MXNET_STORAGE_FALLBACK_LOG_VERBOSE", true);
462  if (!log) return;
463  const std::string op_str = operator_stype_string(attrs, dev_mask, *in_attrs, *out_attrs);
464  std::ostringstream os;
465  const char* warning = "\nThe operator with default storage type will be dispatched "
466  "for execution. You're seeing this warning message because the operator above is unable "
467  "to process the given ndarrays with specified storage types, context and parameter. "
468  "Temporary dense ndarrays are generated in order to execute the operator. "
469  "This does not affect the correctness of the programme. "
470  "You can set environment variable MXNET_STORAGE_FALLBACK_LOG_VERBOSE to "
471  "0 to suppress this warning.";
472  os << "\nStorage type fallback detected:\n" << op_str << warning;
473  LogOnce(os.str());
474 #if MXNET_USE_MKLDNN == 1
475  if (!MKLDNNEnvSet()) common::LogOnce("MXNET_MKLDNN_ENABLED flag is off. "
476  "You can re-enable by setting MXNET_MKLDNN_ENABLED=1");
477 #endif
478 }
479 
480 // heuristic to dermine number of threads per GPU
481 inline int GetNumThreadsPerGPU() {
482  // This is resource efficient option.
483  return dmlc::GetEnv("MXNET_GPU_WORKER_NTHREADS", 2);
484 }
485 
486 // heuristic to get number of matching colors.
487 // this decides how much parallelism we can get in each GPU.
488 inline int GetExecNumMatchColor() {
489  // This is resource efficient option.
490  int num_match_color = dmlc::GetEnv("MXNET_EXEC_NUM_TEMP", 1);
491  return std::min(num_match_color, GetNumThreadsPerGPU());
492 }
493 
494 template<typename T, typename V>
495 V ParallelAccumulate(const T* a, const int n, V start) {
496  V sum = start;
497 #pragma omp parallel for reduction(+:sum)
498  for (int i = 0; i < n; ++i) {
499  sum += a[i];
500  }
501  return sum;
502 }
503 
511 template<typename RandomIt, typename Compare>
512 void ParallelSortHelper(RandomIt first, size_t len,
513  size_t grainsize, const Compare& comp) {
514  if (len < grainsize) {
515  std::sort(first, first+len, comp);
516  } else {
517  std::thread thr(ParallelSortHelper<RandomIt, Compare>, first, len/2, grainsize, comp);
518  ParallelSortHelper(first+len/2, len - len/2, grainsize, comp);
519  thr.join();
520  std::inplace_merge(first, first+len/2, first+len, comp);
521  }
522 }
523 
533 template<typename RandomIt, typename Compare>
534 void ParallelSort(RandomIt first, RandomIt last, size_t num_threads, Compare comp) {
535  const auto num = std::distance(first, last);
536  size_t grainsize = std::max(num / num_threads + 5, static_cast<size_t>(1024*16));
537  ParallelSortHelper(first, num, grainsize, comp);
538 }
539 
549 template<typename RandomIt>
550 void ParallelSort(RandomIt first, RandomIt last, size_t num_threads) {
551  ParallelSort(first, last, num_threads,
552  std::less<typename std::iterator_traits<RandomIt>::value_type>());
553 }
554 
558 typedef std::mt19937 RANDOM_ENGINE;
559 
563 namespace helper {
564 
568 template <class T>
569 struct UniqueIf {
573  using SingleObject = std::unique_ptr<T>;
574 };
575 
579 template <class T>
580 struct UniqueIf<T[]> {
584  using UnknownBound = std::unique_ptr<T[]>;
585 };
586 
590 template <class T, size_t kSize>
591 struct UniqueIf<T[kSize]> {
595  using KnownBound = void;
596 };
597 
598 } // namespace helper
599 
611 template <class T, class... Args>
613  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
614 }
615 
625 template <class T>
627  using U = typename std::remove_extent<T>::type;
628  return std::unique_ptr<T>(new U[n]{});
629 }
630 
639 template <class T, class... Args>
640 typename helper::UniqueIf<T>::KnownBound MakeUnique(Args&&... args) = delete;
641 
642 template<typename FCompType>
643 FCompType GetFCompute(const nnvm::Op* op, const std::string& name,
644  const Context& ctx) {
645  static auto& fcompute_cpu = nnvm::Op::GetAttr<FCompType>(name + "<cpu>");
646  static auto& fcompute_gpu = nnvm::Op::GetAttr<FCompType>(name + "<gpu>");
647 
648  if (ctx.dev_mask() == cpu::kDevMask) {
649  return fcompute_cpu.get(op, nullptr);
650  } else if (ctx.dev_mask() == gpu::kDevMask) {
651  return fcompute_gpu.get(op, nullptr);
652  } else {
653  LOG(FATAL) << "Unknown device mask";
654  return nullptr;
655  }
656 }
657 
661 template <typename T>
662 constexpr size_t MaxIntegerValue() {
663  return std::is_integral<T>::value ?
664  std::numeric_limits<T>::max():
665  size_t(2) << (std::numeric_limits<T>::digits - 1);
666 }
667 
668 template <>
669 constexpr size_t MaxIntegerValue<mshadow::half::half_t>() {
670  return size_t(2) << 10;
671 }
672 
673 MSHADOW_XINLINE int ilog2ul(size_t a) {
674  int k = 1;
675  while (a >>= 1) ++k;
676  return k;
677 }
678 
679 MSHADOW_XINLINE int ilog2ui(unsigned int a) {
680  int k = 1;
681  while (a >>= 1) ++k;
682  return k;
683 }
684 
688 inline NDArray InitZeros(const NDArrayStorageType stype, const TShape &shape,
689  const Context &ctx, const int dtype) {
690  // NDArray with default storage
691  if (stype == kDefaultStorage) {
692  NDArray ret(shape, ctx, false, dtype);
693  ret = 0;
694  return ret;
695  }
696  // NDArray with non-default storage. Storage allocation is always delayed.
697  return NDArray(stype, shape, ctx, true, dtype);
698 }
699 
703 inline void EmplaceBackZeros(const NDArrayStorageType stype, const TShape &shape,
704  const Context &ctx, const int dtype,
705  std::vector<NDArray> *vec) {
706  // NDArray with default storage
707  if (stype == kDefaultStorage) {
708  vec->emplace_back(shape, ctx, false, dtype);
709  vec->back() = 0;
710  } else {
711  // NDArray with non-default storage. Storage allocation is always delayed.
712  vec->emplace_back(stype, shape, ctx, true, dtype);
713  }
714 }
715 
716 } // namespace common
717 } // namespace mxnet
718 #endif // MXNET_COMMON_UTILS_H_
Definition: ndarray.h:74
static MSHADOW_XINLINE void Map(int i, DType *out, const IType *idx, const RType *indptr, const nnvm::dim_t ncols)
Definition: utils.h:77
Definition: ndarray.h:63
NDArrayStorageType
Definition: ndarray.h:61
void CheckFormatCSRImpl(const RunContext &rctx, const NDArray &input, const TBlob &err_cpu, const bool full_check)
Check the validity of CSRNDArray.
Definition: utils.h:116
DeviceType dev_mask() const
Get corresponding device mask.
Definition: base.h:151
Definition: ndarray.h:54
NDArrayStorageType storage_type() const
Definition: ndarray.h:289
Engine that schedules all the operations according to dependency.
void CheckFormatImpl(const RunContext &rctx, const NDArray &input, const TBlob &err_cpu, const bool full_check)
Definition: utils.h:206
int GetNumThreadsPerGPU()
Definition: utils.h:481
void SparseRetainOpForwardRspWrapper(mshadow::Stream< xpu > *s, const NDArray &input_nd, const TBlob &idx_data, const OpReqType req, NDArray *output_nd)
Pick rows specified by user input index array from a row sparse ndarray and save them in the output s...
const TShape & storage_shape() const
Definition: ndarray.h:197
std::string operator_stype_string(const nnvm::NodeAttrs &attrs, const int dev_mask, const std::vector< int > &in_attrs, const std::vector< int > &out_attrs)
get string representation of the operator stypes
Definition: utils.h:402
namespace of mxnet
Definition: base.h:118
Additional operator attributes beside the ones provided by NNVM.
void KnownBound
Type of T.
Definition: utils.h:595
void ParallelSortHelper(RandomIt first, size_t len, size_t grainsize, const Compare &comp)
Helper function for ParallelSort. DO NOT call this function directly. Use the interface ParallelSort ...
Definition: utils.h:512
int type_flag_
type flag of the tensor blob
Definition: tensor_blob.h:74
FCompType GetFCompute(const nnvm::Op *op, const std::string &name, const Context &ctx)
Definition: utils.h:643
V ParallelAccumulate(const T *a, const int n, V start)
Definition: utils.h:495
void LogOnce(const std::string &message)
log message once. Intended for storage fallback warning messages.
Definition: utils.h:446
nnvm::TShape TShape
Shape data structure used to record shape information.
Definition: base.h:128
Context ctx
base Context
Definition: base.h:259
Definition: ndarray.h:72
execution time context. The information needed in runtime for actual execution.
Definition: base.h:257
DispatchMode
the dispatch mode of the operator
Definition: op_attr_types.h:113
NDArray InitZeros(const NDArrayStorageType stype, const TShape &shape, const Context &ctx, const int dtype)
Return an NDArray of all zeros.
Definition: utils.h:688
std::string stype_string(const int x)
get string representation of storage_type
Definition: utils.h:374
Definition: ndarray.h:65
void CastStorageDispatch(const OpContext &ctx, const NDArray &input, const NDArray &output)
void CheckFormatWrapper(const RunContext &rctx, const NDArray &input, const TBlob &err_cpu, const bool full_check)
Definition: base.h:136
void ParallelSort(RandomIt first, RandomIt last, size_t num_threads, Compare comp)
Sort the elements in the range [first, last) into the ascending order defined by the comparator comp...
Definition: utils.h:534
All the possible information needed by Operator.Forward and Backward This is the superset of RunConte...
Definition: op_attr_types.h:66
bool ContainsOnlyStorage(const StorageTypeVector &vstorage, const NDArrayStorageType stype)
returns true if all storage types in vstorage are the same as target stype. false is returned for emp...
Definition: utils.h:238
std::string operator_string(const nnvm::NodeAttrs &attrs, const OpContext &ctx, const std::vector< NDArray > &inputs, const std::vector< OpReqType > &req, const std::vector< NDArray > &outputs)
get string representation of the operator
Definition: utils.h:428
NDArray interface that handles array arithematics.
std::mt19937 RANDOM_ENGINE
Random Engine.
Definition: utils.h:558
void EmplaceBackZeros(const NDArrayStorageType stype, const TShape &shape, const Context &ctx, const int dtype, std::vector< NDArray > *vec)
Helper to add a NDArray of zeros to a std::vector.
Definition: utils.h:703
Indices of RSPNDArray should be non-negative, less than the size of first dimension and in ascending ...
Definition: utils.h:93
Definition: ndarray.h:58
Definition: base.h:138
const TShape & shape() const
Definition: ndarray.h:189
std::string dispatch_mode_string(const DispatchMode x)
get string representation of dispatch_mode
Definition: utils.h:356
std::string dev_type_string(const int dev_type)
get string representation of device type
Definition: utils.h:387
Helper for non-array type T.
Definition: utils.h:569
Definition: base.h:137
Definition: ndarray.h:54
Data structures that can appear in graph attributes.
Definition: ndarray.h:64
IndPtr should be non-negative, in non-decreasing order, start with 0 and end with value equal with si...
Definition: utils.h:60
Definition: ndarray.h:71
std::unique_ptr< T[]> UnknownBound
Type of T.
Definition: utils.h:584
OpReqType
operation request type to Forward and Backward
Definition: op_attr_types.h:45
nnvm::Op Op
operator structure from NNVM
Definition: base.h:130
bool ContainsStorageType(const std::vector< NDArray > &ndarrays, const NDArrayStorageType stype)
returns true if storage type of any array in ndarrays is the same as the target stype. false is returned for empty inputs.
Definition: utils.h:328
constexpr size_t MaxIntegerValue()
Return the max integer value representable in the type T without loss of precision.
Definition: utils.h:662
RunContext run_ctx
RunContext related resources.
Definition: op_attr_types.h:72
int64_t dim_t
data type to store dim size
Definition: c_api.h:62
std::unique_ptr< T > SingleObject
Type of T.
Definition: utils.h:573
void CheckFormatRSPImpl(const RunContext &rctx, const NDArray &input, const TBlob &err_cpu, const bool full_check)
Check the validity of RowSparseNDArray.
Definition: utils.h:170
int GetExecNumMatchColor()
Definition: utils.h:488
Definition: base.h:139
static MSHADOW_XINLINE void Map(int i, DType *out, const IType *idx, const nnvm::dim_t end, const nnvm::dim_t nrows)
Definition: utils.h:95
MSHADOW_XINLINE int ilog2ul(size_t a)
Definition: utils.h:673
void LogStorageFallback(const nnvm::NodeAttrs &attrs, const int dev_mask, const std::vector< int > *in_attrs, const std::vector< int > *out_attrs)
log storage fallback event
Definition: utils.h:457
helper::UniqueIf< T >::SingleObject MakeUnique(Args &&...args)
Constructs an object of type T and wraps it in a std::unique_ptr.
Definition: utils.h:612
Context information about the execution environment.
Definition: base.h:133
Indices should be non-negative, less than the number of columns and in ascending order per row...
Definition: utils.h:75
const TShape & aux_shape(size_t index) const
get the shape of aux_data(index)
Definition: ndarray.h:209
ndarray interface
Definition: ndarray.h:82
MSHADOW_XINLINE int ilog2ui(unsigned int a)
Definition: utils.h:679
static MSHADOW_XINLINE void Map(int i, DType *out, const IType *indptr, const nnvm::dim_t end, const nnvm::dim_t idx_size)
Definition: utils.h:62
std::vector< int > StorageTypeVector
The result holder of storage type of each NodeEntry in the graph.
Definition: graph_attr_types.h:45
tensor blob class that can be used to hold tensor of any dimension, any device and any data type...
Definition: tensor_blob.h:66