mxnet
tuple.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  */
23 #ifndef MXNET_TUPLE_H_
24 #define MXNET_TUPLE_H_
25 
26 #include <vector>
27 #include <type_traits>
28 #include <algorithm>
29 #include <utility>
30 #include <iostream>
31 #include <string>
32 #include "nnvm/op_attr_types.h"
33 #include "nnvm/graph_attr_types.h"
34 #include "nnvm/graph.h"
35 #include "nnvm/pass.h"
36 #include "runtime/object.h"
37 #include "runtime/ffi_helper.h"
38 #include "node/container.h"
39 #include "ir/expr.h"
40 
41 namespace mxnet {
42 
56 template<typename ValueType>
57 class Tuple {
58  public:
60  Tuple() = default;
62  inline ~Tuple() {
63  delete [] data_heap_;
64  }
70  inline Tuple(const int ndim, const dim_t value) { // NOLINT(*)
71  this->SetDim(ndim);
72  if (ndim > 0) {
73  std::fill_n(begin(), ndim, value);
74  }
75  }
80  inline Tuple(const Tuple<ValueType>& s) {
81  if (s.ndim() == -1) {
82  this->SetDim(-1);
83  } else {
84  this->assign(s.begin(), s.end());
85  }
86  }
91  inline Tuple(std::initializer_list<ValueType> init) {
92  this->assign(init.begin(), init.end());
93  }
98  inline Tuple(std::vector<ValueType> init) { // NOLINT(runtime/explicit)
99  this->assign(init.begin(), init.end());
100  }
106  inline Tuple(Tuple<ValueType>&& src) { // NOLINT(runtime/explicit)
107  this->swap(src);
108  }
115  template<typename RandomAccessIterator>
116  inline Tuple(RandomAccessIterator begin,
117  RandomAccessIterator end) {
118  this->assign(begin, end);
119  }
120 
121  inline explicit Tuple(const runtime::ObjectRef& src) {
122  using namespace runtime;
123  ADT adt = Downcast<ADT, ObjectRef>(src);
124  this->SetDim(adt.size());
125  for (int i = 0; i < ndim_; ++i) {
126  this->begin()[i] = Downcast<Integer, ObjectRef>(adt[i])->value;
127  }
128  }
129 
136  template<typename RandomAccessIterator>
137  inline void assign(RandomAccessIterator begin,
138  RandomAccessIterator end) {
139  this->SetDim(end - begin);
140  CHECK_GE(ndim(), 0);
141  std::copy(begin, end, this->begin());
142  }
147  inline void swap(Tuple<ValueType>& other) { // NOLINT(*)
148  std::swap(ndim_, other.ndim_);
149  std::swap(num_heap_allocated_, other.num_heap_allocated_);
150  std::swap(data_stack_, other.data_stack_);
151  std::swap(data_heap_, other.data_heap_);
152  }
159  if (src.ndim() == -1) {
160  this->SetDim(-1);
161  } else {
162  this->assign(src.begin(), src.end());
163  }
164  return *this;
165  }
172  Tuple<ValueType>(std::move(src)).swap(*this);
173  return *this;
174  }
180  inline Tuple<ValueType> &operator=(std::initializer_list<ValueType> init) {
181  this->assign(init.begin(), init.end());
182  return *this;
183  }
188  inline bool operator==(const Tuple<ValueType> &s) const {
189  if (ndim_ != s.ndim_) return false;
190  if (ndim() == -1) return true;
191  return std::equal(begin(), end(), s.begin());
192  }
197  inline bool operator!=(const Tuple<ValueType> &s) const {
198  return !(*this == s);
199  }
201  inline const ValueType *begin() const {
202  return ndim_ <= kStackCache ? data_stack_ : data_heap_;
203  }
205  inline ValueType *begin() {
206  return ndim_ <= kStackCache ? data_stack_ : data_heap_;
207  }
209  inline const ValueType* end() const {
210  return ndim_ <= kStackCache ? (data_stack_ + ndim_): (data_heap_ + ndim_);
211  }
213  inline ValueType* end() {
214  return ndim_ <= kStackCache ? (data_stack_ + ndim_): (data_heap_ + ndim_);
215  }
217  inline int ndim() const {
218  return ndim_;
219  }
225  inline ValueType& operator[](int i) {
226  // it fixes the false alarm of assuming signed overflow does not occur
227  // when assuming that (X - c) > X is always false [-Werror=strict-overflow]
228  #pragma GCC diagnostic push
229  #pragma GCC diagnostic ignored "-Wstrict-overflow"
230  CHECK(i >= 0 && i < ndim()) << "index = " << i << " must be in range [0, " << ndim() << ")";
231  #pragma GCC diagnostic pop
232  return begin()[i];
233  }
239  inline const ValueType& operator[](int i) const {
240  // it fixes the false alarm of assuming signed overflow does not occur
241  // when assuming that (X - c) > X is always false [-Werror=strict-overflow]
242  #pragma GCC diagnostic push
243  #pragma GCC diagnostic ignored "-Wstrict-overflow"
244  CHECK(i >= 0 && i < ndim()) << "index = " << i << " must be in range [0, " << ndim() << ")";
245  #pragma GCC diagnostic pop
246  return begin()[i];
247  }
252  inline void Save(dmlc::JSONWriter* writer) const {
253  std::vector<ValueType> tmp(begin(), end());
254  writer->Write(tmp);
255  }
260  inline void Load(dmlc::JSONReader* reader) {
261  std::vector<ValueType> tmp;
262  reader->Read(&tmp);
263  this->assign(tmp.begin(), tmp.end());
264  }
271  friend std::ostream &operator<<(std::ostream &os, const Tuple<ValueType> &t) {
272  if (t.ndim() == -1) {
273  // If t is an unknown shape, return string "None".
274  // This is consistent with returning unknown shape in Python and generating
275  // C++ operator APIs by OpWrapperGenerator.py (defaultString) in cpp-package.
276  os << "None";
277  return os;
278  }
279  os << '[';
280  const ValueType* begin = t.begin();
281  const ValueType* end = t.end();
282  for (const ValueType* it = begin; it != end; ++it) {
283  if (it != begin) os << ',';
284  os << *it;
285  }
286  os << ']';
287  return os;
288  }
295  friend std::istream &operator>>(std::istream &is, Tuple<ValueType> &t) {
296  // get (
297  while (true) {
298  char ch = is.peek();
299  if (isdigit(ch) || ch == '-') {
300  ValueType idx;
301  if (is >> idx) {
302  t.assign(&idx, &idx + 1);
303  }
304  return is;
305  }
306  is.get();
307  if (ch == '(' || ch == '[') break;
308  if (!isspace(ch)) {
309  if (ch == 'N') {
310  std::string tmp_val;
311  is >> tmp_val;
312  if (tmp_val == "one") { // is stores "None"
313  t.SetDim(-1);
314  return is;
315  }
316  }
317  is.setstate(std::ios::failbit);
318  return is;
319  }
320  }
321  // Handle empty tuple. A tensor whose shape is an empty tuple
322  // represents a scalar with ndim = 0.
323  while (isspace(is.peek())) {
324  is.get();
325  }
326  if (is.peek() == ')' || is.peek() == ']') {
327  is.get();
328  t.SetDim(0);
329  return is;
330  }
331  // Handle non-empty tuple
332  ValueType idx;
333  std::vector<ValueType> tmp;
334  while (is >> idx) {
335  tmp.push_back(idx);
336  char ch;
337  do {
338  ch = is.get();
339  } while (isspace(ch));
340  if (std::is_integral<ValueType>::value && ch == 'L') {
341  ch = is.get();
342  }
343  if (ch == ',') {
344  while (true) {
345  ch = is.peek();
346  if (isspace(ch)) {
347  is.get(); continue;
348  }
349  if (ch == ')' || ch == ']') {
350  is.get(); break;
351  }
352  break;
353  }
354  if (ch == ')' || ch == ']') break;
355  } else if (ch == ')' || ch == ']') {
356  break;
357  } else {
358  is.setstate(std::ios::failbit);
359  return is;
360  }
361  }
362  t.assign(tmp.begin(), tmp.end());
363  return is;
364  }
371  template<typename DType = ValueType, typename TStream>
372  inline void Save(TStream *strm) const;
380  template<typename DType = ValueType, typename TStream>
381  inline bool Load(TStream *strm);
382 
383  protected:
384  // stack cache size
385  static const int kStackCache = 4;
387  int ndim_{0};
393  ValueType* data_heap_{nullptr};
394  // internal function to change the dimension
395  inline void SetDim(int ndim) {
396  CHECK_GE(ndim, -1) << "ndim cannot be less than -1, received " << ndim;
397  if (ndim > kStackCache &&
398  ndim > num_heap_allocated_) {
399  delete [] data_heap_;
400  data_heap_ = new ValueType[ndim];
402  } else if (ndim <= 0 && data_heap_ != nullptr) {
403  delete [] data_heap_;
404  data_heap_ = nullptr;
406  }
407  ndim_ = ndim;
408  }
409 };
410 
411 
413 inline bool ndim_is_known(const int ndim) {
414  CHECK_GE(ndim, -1) << "shape ndim must be >= -1, while received " << ndim;
415  return ndim != -1;
416 }
417 
419 inline bool dim_size_is_known(const dim_t dim_size) {
420  CHECK_GE(dim_size, -1) << "shape dim size must be >= -1, while received " << dim_size;
421  return dim_size != -1;
422 }
423 
437 class TShape : public Tuple<dim_t> {
438  public:
440  TShape() {
441  this->SetDim(-1);
442  }
448  inline TShape(const int ndim, const dim_t value) { // NOLINT(*)
449  this->SetDim(ndim);
450  if (ndim > 0) {
451  std::fill_n(begin(), ndim, value);
452  }
453  }
458  inline TShape(const Tuple<dim_t>& s) { // NOLINT(*)
459  if (s.ndim() == -1) {
460  this->SetDim(-1);
461  } else {
462  this->assign(s.begin(), s.end());
463  }
464  }
469  inline TShape(std::initializer_list<dim_t> init) {
470  this->assign(init.begin(), init.end());
471  }
476  inline TShape(Tuple<dim_t>&& s) { // NOLINT(*)
477  this->swap(s);
478  }
487  template<typename RandomAccessIterator,
488  typename std::enable_if<
489  std::is_same<typename std::iterator_traits<RandomAccessIterator>::iterator_category,
490  std::random_access_iterator_tag>::value, int>::type = 0>
491  inline TShape(RandomAccessIterator begin,
492  RandomAccessIterator end) {
493  this->assign(begin, end);
494  }
495 
496  inline explicit TShape(const ObjectRef& src): Tuple(src) {}
502  inline TShape& operator=(const Tuple<dim_t>& src) {
503  if (src.ndim() == -1) {
504  this->SetDim(-1);
505  } else {
506  this->assign(src.begin(), src.end());
507  }
508  return *this;
509  }
515  inline TShape& operator=(Tuple<dim_t>&& src) { // NOLINT(*)
516  TShape(std::move(src)).swap(*this); // NOLINT(*)
517  return *this;
518  }
520  inline size_t Size() const {
521  CHECK(ndim_is_known(this->ndim())) << "Shape is unknown.";
522  dim_t size = 1;
523  const dim_t* start = begin(), *fin = end();
524  for (const dim_t* it = start; it != fin; ++it) {
525  CHECK(dim_size_is_known(*it)) << "Shape dim size cannot be a negative value " << *it;
526  size *= *it;
527  }
528  return size;
529  }
535  inline size_t ProdShape(int dimstart, int dimend) const {
536  CHECK(ndim_is_known(this->ndim())) << "Shape is unknown.";
537  CHECK_GE(dimstart, 0) << "dimstart must be >= 0, while received " << dimstart;
538  CHECK_LE(dimend, this->ndim()) << "dimend must be <= " << this->ndim()
539  << ", while received " << dimend;
540  dim_t num = 1;
541  const dim_t *d = this->data();
542  for (int i = dimstart; i < dimend; ++i) {
543  CHECK(dim_size_is_known(d[i])) << "Shape dim size must be known, while received " << d[i];
544  num *= d[i];
545  }
546  return num;
547  }
549  inline const dim_t *data() const {
550  return begin();
551  }
553  inline dim_t *data() {
554  return begin();
555  }
556 #ifdef MSHADOW_XINLINE
557  template<int dim>
558  inline TShape(const mshadow::Shape<dim> &s) {// NOLINT(*)
559  this->assign(s.shape_, s.shape_ + dim);
560  }
561 
562  template<int dim>
563  inline TShape(mshadow::Shape<dim> &&s) {// NOLINT(*)
564  this->assign(s.shape_, s.shape_ + dim);
565  }
572  template<int dim>
573  inline TShape &operator=(const mshadow::Shape<dim> &shape) {
574  this->assign(shape.shape_, shape.shape_ + dim);
575  return *this;
576  }
582  template<int dim>
583  inline mshadow::Shape<dim> get() const {
584  CHECK_EQ(dim, ndim())
585  << "dimension do not match target dimension " << dim << " vs " << ndim();
586  const dim_t *d = this->data();
588  for (int i = 0; i < dim; ++i) {
589  s[i] = d[i];
590  }
591  return s;
592  }
597  inline mshadow::Shape<2> FlatTo2D(void) const {
599  CHECK(ndim_is_known(ndim())) << "shape must have a valid ndim";
600  if (ndim() == 0) return mshadow::Shape2(1, 1);
601  const dim_t *d = this->data();
602  s.shape_[1] = d[ndim() - 1];
603  dim_t ymax = 1;
604  for (int i = 1; i < ndim(); ++i) {
605  ymax *= d[i - 1];
606  }
607  s.shape_[0] = ymax;
608  return s;
609  }
616  inline mshadow::Shape<3> FlatTo3D(int axis_begin, int axis_end) const {
617  CHECK(axis_end >= axis_begin);
619  CHECK(ndim_is_known(ndim())) << "shape must have a valid ndim";
620  if (ndim() == 0) return mshadow::Shape3(1, 1, 1);
621  const dim_t *d = this->data();
622  s.shape_[0] = 1;
623  s.shape_[1] = 1;
624  s.shape_[2] = 1;
625 
626  for (int i = 0; i < axis_begin; ++i) {
627  s.shape_[0] *= d[i];
628  }
629  for (int i = axis_begin; i <= axis_end; ++i) {
630  s.shape_[1] *= d[i];
631  }
632  for (int i = axis_end + 1; i < ndim(); ++i) {
633  s.shape_[2] *= d[i];
634  }
635  return s;
636  }
642  inline mshadow::Shape<3> FlatTo3D(int axis) const {
643  return FlatTo3D(axis, axis);
644  }
645  inline bool operator==(const TShape &s) const {
646  if (ndim() != s.ndim()) return false;
647  return std::equal(begin(), end(), s.begin());
648  }
649  inline bool operator!=(const TShape &s) const {
650  return !(*this == s);
651  }
657  template<int dim>
658  inline bool operator==(const mshadow::Shape<dim> &s) const {
659  if (ndim_ != dim) return false;
660  const dim_t *d = dim <= kStackCache ? data_stack_ : data_heap_;
661  for (size_t i = 0; i < dim; ++i) {
662  if (d[i] != s.shape_[i]) return false;
663  }
664  return true;
665  }
671  template<int dim>
672  inline bool operator!=(const mshadow::Shape<dim> &s) const {
673  return !(*this == s);
674  }
675 #endif
676 };
677 
679 inline bool ndim_is_known(const TShape& x) {
680  return ndim_is_known(x.ndim());
681 }
682 
684 inline bool dim_size_is_known(const TShape& x, const int idx) {
685  CHECK(idx >= 0 && idx < x.ndim())
686  << "idx = " << idx << " exceeds shape dimension range [0, " << x.ndim() << ")";
687  return dim_size_is_known(x[idx]);
688 }
689 
692 inline bool shape_is_known(const TShape& x) {
693  if (!ndim_is_known(x)) return false;
694  for (int i = 0; i < x.ndim(); ++i) {
695  if (!dim_size_is_known(x, i)) return false;
696  }
697  return true;
698 }
699 
700 inline bool shape_is_known(const std::vector<TShape>& shapes) {
701  for (const TShape& shape : shapes) {
702  if (!shape_is_known(shape)) return false;
703  }
704  return true;
705 }
706 
708 template<typename SrcIter, typename DstIter>
709 inline DstIter ShapeTypeCast(const SrcIter begin,
710  const SrcIter end,
711  DstIter dst_begin) {
712  typedef typename std::iterator_traits<SrcIter>::value_type SrcDType;
713  typedef typename std::iterator_traits<DstIter>::value_type DstDType;
714  auto cast = [](const SrcDType& dim) { return static_cast<DstDType>(dim); };
715  return std::transform(begin, end, dst_begin, cast);
716 }
717 
719 template<typename SrcIter>
720 inline TShape ShapeTypeCast(const SrcIter begin, const SrcIter end) {
721  size_t ndim = std::distance(begin, end);
722  TShape res(ndim, -1);
723  ShapeTypeCast(begin, end, res.begin());
724  return res;
725 }
726 
728 template<typename ValueType>
729 template<typename DType, typename TStream>
730 inline void Tuple<ValueType>::Save(TStream *strm) const {
731  strm->Write(&ndim_, sizeof(ndim_));
732  if (typeid(DType) == typeid(ValueType)) {
733  strm->Write(begin(), sizeof(ValueType) * ndim_);
734  } else {
735  std::vector<DType> buffer(ndim_);
736  ShapeTypeCast(begin(), end(), buffer.data());
737  strm->Write(buffer.data(), sizeof(DType) * ndim_);
738  }
739 }
740 
742 template<typename ValueType>
743 template<typename DType, typename TStream>
744 inline bool Tuple<ValueType>::Load(TStream *strm) {
745  if (strm->Read(&ndim_, sizeof(ndim_)) != sizeof(ndim_)) return false;
746  this->SetDim(ndim_);
747  size_t nread = sizeof(DType) * ndim_;
748  if (typeid(DType) == typeid(ValueType)) {
749  if (strm->Read(begin(), nread) != nread) return false;
750  } else {
751  std::vector<DType> buffer(ndim_);
752  if (strm->Read(buffer.data(), nread) != nread) return false;
753  ShapeTypeCast(buffer.begin(), buffer.end(), begin());
754  }
755  return true;
756 }
757 
758 } // namespace mxnet
759 
760 namespace std {
762 template<typename T>
763 struct hash<mxnet::Tuple<T> > {
765  size_t operator()(const mxnet::Tuple<T>& val) const {
766  std::hash<int> hash_int;
767  size_t res = hash_int(val.ndim());
768  for (int i = 0; i < val.ndim(); ++i) {
769  res = dmlc::HashCombine(res, val[i]);
770  }
771  return res;
772  }
773 };
774 
776 template<>
777 struct hash<mxnet::TShape> {
779  size_t operator()(const mxnet::TShape& val) const {
780  std::hash<int> hash_int;
781  size_t res = hash_int(val.ndim());
782  for (int i = 0; i < val.ndim(); ++i) {
783  res = dmlc::HashCombine(res, val[i]);
784  }
785  return res;
786  }
787 };
788 } // namespace std
789 
790 namespace dmlc {
792 DMLC_DECLARE_TYPE_NAME(optional<mxnet::TShape>, "Shape or None");
793 DMLC_DECLARE_TYPE_NAME(optional<mxnet::Tuple<int>>, "Shape or None");
794 // avoid low version of MSVC
795 #if !(defined(_MSC_VER) && _MSC_VER < 1900)
796 template<typename T>
798  static inline std::string value() {
799  return "tuple of <" + type_name<T>() + ">";
800  }
801 };
802 #endif
803 } // namespace dmlc
804 
805 namespace mxnet {
819 using ShapeVector = std::vector<mxnet::TShape>;
820 
832 
833 } // namespace mxnet
834 
835 #endif // MXNET_TUPLE_H_
size_t operator()(const mxnet::TShape &val) const
hash a TShape into unsigned int
Definition: tuple.h:779
#define DMLC_DECLARE_TYPE_NAME(Type, Name)
macro to quickly declare traits information
Definition: type_traits.h:133
bool operator!=(const Tuple< ValueType > &s) const
Definition: tuple.h:197
TShape(const ObjectRef &src)
Definition: tuple.h:496
helper class to construct a string that represents type name
Definition: type_traits.h:86
Tuple< ValueType > & operator=(const Tuple< ValueType > &src)
assignment from another tuple.
Definition: tuple.h:158
size_t ProdShape(int dimstart, int dimend) const
Definition: tuple.h:535
A dynamic sized array data structure that is optimized for storing small number of elements with same...
Definition: tuple.h:51
ValueType * data_heap_
space to store shape when dimension is big
Definition: tuple.h:327
uint32_t ndim_
number of dimension of the tuple
Definition: tuple.h:321
Tuple< ValueType > & operator=(Tuple< ValueType > &&src)
assignment from rvalue of another tuple.
Definition: tuple.h:171
dim_t * data()
Definition: tuple.h:553
TShape(const Tuple< dim_t > &s)
copy constructor of TShape
Definition: tuple.h:458
ValueType data_stack_[kStackCache]
in stack space used to store shape when it is small
Definition: tuple.h:391
namespace of mxnet
Definition: api_registry.h:33
TShape & operator=(Tuple< dim_t > &&src)
move assignment function from tshape
Definition: tuple.h:515
Tuple(std::initializer_list< ValueType > init)
constructor from initializer list
Definition: tuple.h:91
const ValueType * end() const
Definition: tuple.h:209
ValueType data_stack_[kStackCache]
in stack space used to store shape when it is small
Definition: tuple.h:325
TShape()
default constructor
Definition: tuple.h:440
int64_t dim_t
data type to store dim size
Definition: tuple.h:38
Definition: optional.h:251
DstIter ShapeTypeCast(const SrcIter begin, const SrcIter end, DstIter dst_begin)
helper function to cast type of container elements
Definition: tuple.h:709
Tuple(Tuple< ValueType > &&src)
move constructor from Tuple
Definition: tuple.h:106
void Load(dmlc::JSONReader *reader)
Load Tuple from JSON.
Definition: tuple.h:260
void assign(RandomAccessIterator begin, RandomAccessIterator end)
Assign content to tuple from iterator.
Definition: tuple.h:137
Base class of all object reference.
Definition: object.h:499
const ValueType * end() const
Definition: tuple.h:172
ValueType * end()
Definition: tuple.h:213
TShape(RandomAccessIterator begin, RandomAccessIterator end)
construct the Tuple from content of iterator. This function is enforced with template arguments of ra...
Definition: tuple.h:491
Data structures that can appear in graph attributes.
ValueType & operator[](int i)
get corresponding index
Definition: tuple.h:225
void SetDim(uint32_t ndim)
Definition: tuple.h:329
Tuple(const runtime::ObjectRef &src)
Definition: tuple.h:121
bool operator==(const Tuple< ValueType > &s) const
Definition: tuple.h:188
size_t HashCombine(size_t key, const T &value)
hash an object and combines the key with previous keys
Definition: common.h:37
std::vector< mxnet::TShape > ShapeVector
The result holder of shape of each NodeEntry in the graph.
Definition: tuple.h:819
Tuple(const Tuple< ValueType > &s)
copy constructor from another tuple
Definition: tuple.h:80
Lightweight JSON Reader to read any STL compositions and structs. The user need to know the schema of...
Definition: json.h:44
bool isspace(char c)
Inline implementation of isspace(). Tests whether the given character is a whitespace letter...
Definition: strtonum.h:26
~Tuple()
destructor
Definition: tuple.h:62
Tuple(std::vector< ValueType > init)
constructor from vector
Definition: tuple.h:98
namespace for dmlc
Definition: array_view.h:12
Tuple(RandomAccessIterator begin, RandomAccessIterator end)
construct the Tuple from content of iterator
Definition: tuple.h:116
void Write(const ValueType &value)
Write value to json.
bool dim_size_is_known(const dim_t dim_size)
Definition: tuple.h:419
Tuple()=default
default constructor
int num_heap_allocated_
number of cells allocated in data_heap_
Definition: tuple.h:389
static std::string value()
Definition: tuple.h:798
static const int kStackCache
Definition: tuple.h:385
void SetDim(int ndim)
Definition: tuple.h:395
const dim_t * data() const
Definition: tuple.h:549
std::function< bool(const NodeAttrs &attrs, std::vector< AttrType > *in_attrs, std::vector< AttrType > *out_attrs)> FInferNodeEntryAttr
Inference function of certain type.
Definition: op_attr_types.h:94
void swap(Tuple< ValueType > &other)
Swap current object with other.
Definition: tuple.h:147
index_t shape_[kDimension]
storing the dimension information
Definition: tensor.h:75
Pass that can be applied to a graph.
TShape(Tuple< dim_t > &&s)
move constructor.
Definition: tuple.h:476
void Read(ValueType *out_value)
Read next ValueType.
MSHADOW_XINLINE Shape< 2 > Shape2(index_t s0, index_t s1)
construct a two dimension shape, stride will equal s0
Definition: tensor.h:216
size_t operator()(const mxnet::Tuple< T > &val) const
hash a Tuple into unsigned int
Definition: tuple.h:765
A dynamic sized array data structure that is optimized for storing small number of elements with same...
Definition: tuple.h:57
uint32_t ndim() const
Definition: tuple.h:180
TShape(std::initializer_list< dim_t > init)
constructor from initializer list
Definition: tuple.h:469
Configuation of nnvm as well as basic data structure.
A managed object in MXNet runtime.
void assign(RandomAccessIterator begin, RandomAccessIterator end)
Assign content to tuple from iterator.
Definition: tuple.h:106
int ndim_
number of dimension of the tuple
Definition: tuple.h:387
size_t Size() const
Definition: tuple.h:520
A Shape class that is used to represent shape of each tensor.
Definition: tuple.h:437
nnvm::FInferNodeEntryAttr< mxnet::TShape > FInferShape
Shape inference function. Update the shapes given the input shape information. TShape.ndim() == -1 means the shape is still unknown.
Definition: tuple.h:831
TShape & operator=(const Tuple< dim_t > &src)
assignment function from tshape
Definition: tuple.h:502
TShape(const int ndim, const dim_t value)
Definition: tuple.h:448
Base expr nodes in MXNet.
Tuple< ValueType > & operator=(std::initializer_list< ValueType > init)
assignment from initializer list
Definition: tuple.h:180
bool shape_is_known(const TShape &x)
Definition: tuple.h:692
bool ndim_is_known(const int ndim)
Definition: tuple.h:413
MSHADOW_XINLINE Shape< 3 > Shape3(index_t s0, index_t s1, index_t s2)
construct a three dimension shape, stride will equal s0
Definition: tensor.h:227
const ValueType & operator[](int i) const
get corresponding index
Definition: tuple.h:239
ValueType * data_heap_
space to store shape when dimension is big
Definition: tuple.h:393
ValueType * begin()
Definition: tuple.h:205
friend std::istream & operator>>(std::istream &is, Tuple< ValueType > &t)
read tuple from the istream
Definition: tuple.h:295
int64_t dim_t
data type to store dim size
Definition: c_api.h:61
const ValueType * begin() const
Definition: tuple.h:201
const ValueType * begin() const
Definition: tuple.h:164
uint32_t num_heap_allocated_
number of cells allocated in data_heap_
Definition: tuple.h:323
void Save(dmlc::JSONWriter *writer) const
Save Tuple to JSON.
Definition: tuple.h:252
int ndim() const
Definition: tuple.h:217
Tuple(const int ndim, const dim_t value)
Definition: tuple.h:70
bool isdigit(char c)
Inline implementation of isdigit(). Tests whether the given character is a decimal digit...
Definition: strtonum.h:46
Data structures that can appear in operator attributes.
Lightweight json to write any STL compositions.
Definition: json.h:190