Go to the documentation of this file.
23 #ifndef MXNET_TUPLE_H_
24 #define MXNET_TUPLE_H_
27 #include <type_traits>
56 template <
typename ValueType>
91 inline Tuple(std::initializer_list<ValueType> init) {
92 this->
assign(init.begin(), init.end());
98 inline Tuple(std::vector<ValueType> init) {
99 this->
assign(init.begin(), init.end());
115 template <
typename RandomAccessIterator>
121 using namespace runtime;
122 ADT adt = Downcast<ADT, ObjectRef>(src);
124 for (
int i = 0; i <
ndim_; ++i) {
125 this->
begin()[i] = Downcast<Integer, ObjectRef>(adt[i])->value;
135 template <
typename RandomAccessIterator>
157 if (src.
ndim() == -1) {
179 this->
assign(init.begin(), init.end());
198 return !(*
this == s);
201 inline const ValueType*
begin()
const {
209 inline const ValueType*
end()
const {
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
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
253 std::vector<ValueType> tmp(
begin(),
end());
261 std::vector<ValueType> tmp;
263 this->
assign(tmp.begin(), tmp.end());
272 if (t.
ndim() == -1) {
281 const ValueType*
end = t.
end();
282 for (
const ValueType* it =
begin; it !=
end; ++it) {
300 if (
isdigit(ch) || ch ==
'-') {
308 if (ch ==
'(' || ch ==
'[')
314 if (tmp_val ==
"one") {
319 is.setstate(std::ios::failbit);
328 if (is.peek() ==
')' || is.peek() ==
']') {
335 std::vector<ValueType> tmp;
342 if (std::is_integral<ValueType>::value && ch ==
'L') {
352 if (ch ==
')' || ch ==
']') {
358 if (ch ==
')' || ch ==
']')
360 }
else if (ch ==
')' || ch ==
']') {
363 is.setstate(std::ios::failbit);
367 t.
assign(tmp.begin(), tmp.end());
376 template <
typename DType = ValueType,
typename TStream>
377 inline void Save(TStream* strm)
const;
385 template <
typename DType = ValueType,
typename TStream>
386 inline bool Load(TStream* strm);
401 CHECK_GE(
ndim, -1) <<
"ndim cannot be less than -1, received " <<
ndim;
417 CHECK_GE(
ndim, -1) <<
"shape ndim must be >= -1, while received " <<
ndim;
423 CHECK_GE(dim_size, -1) <<
"shape dim size must be >= -1, while received " << dim_size;
424 return dim_size != -1;
462 if (s.
ndim() == -1) {
472 inline TShape(std::initializer_list<dim_t> init) {
473 this->
assign(init.begin(), init.end());
490 template <
typename RandomAccessIterator,
491 typename std::enable_if<
492 std::is_same<typename std::iterator_traits<RandomAccessIterator>::iterator_category,
493 std::random_access_iterator_tag>::value,
506 if (src.
ndim() == -1) {
527 for (
const dim_t* it = start; it != fin; ++it) {
528 CHECK(
dim_size_is_known(*it)) <<
"Shape dim size cannot be a negative value " << *it;
538 inline size_t ProdShape(
int dimstart,
int dimend)
const {
540 CHECK_GE(dimstart, 0) <<
"dimstart must be >= 0, while received " << dimstart;
541 CHECK_LE(dimend, this->
ndim())
542 <<
"dimend must be <= " << this->
ndim() <<
", while received " << dimend;
544 const dim_t* d = this->data();
545 for (
int i = dimstart; i < dimend; ++i) {
546 CHECK(
dim_size_is_known(d[i])) <<
"Shape dim size must be known, while received " << d[i];
559 #ifdef MSHADOW_XINLINE
567 this->
assign(s.shape_, s.shape_ + dim);
587 CHECK_EQ(dim,
ndim()) <<
"dimension do not match target dimension " << dim <<
" vs " <<
ndim();
588 const dim_t* d = this->data();
590 for (
int i = 0; i < dim; ++i) {
604 const dim_t* d = this->data();
607 for (
int i = 1; i <
ndim(); ++i) {
620 CHECK(axis_end >= axis_begin);
625 const dim_t* d = this->data();
630 for (
int i = 0; i < axis_begin; ++i) {
633 for (
int i = axis_begin; i <= axis_end; ++i) {
636 for (
int i = axis_end + 1; i <
ndim(); ++i) {
647 return FlatTo3D(axis, axis);
649 inline bool operator==(
const TShape& s)
const {
650 if (
ndim() != s.ndim())
652 return std::equal(
begin(),
end(), s.begin());
654 inline bool operator!=(
const TShape& s)
const {
655 return !(*
this == s);
667 for (
size_t i = 0; i < dim; ++i) {
680 return !(*
this == s);
692 CHECK(idx >= 0 && idx < x.
ndim())
693 <<
"idx = " << idx <<
" exceeds shape dimension range [0, " << x.
ndim() <<
")";
702 for (
int i = 0; i < x.
ndim(); ++i) {
710 for (
const TShape& shape : shapes) {
718 template <
typename SrcIter,
typename DstIter>
720 typedef typename std::iterator_traits<SrcIter>::value_type SrcDType;
721 typedef typename std::iterator_traits<DstIter>::value_type DstDType;
722 auto cast = [](
const SrcDType& dim) {
return static_cast<DstDType
>(dim); };
723 return std::transform(
begin,
end, dst_begin, cast);
727 template <
typename SrcIter>
736 template <
typename ValueType>
737 template <
typename DType,
typename TStream>
740 if (
typeid(DType) ==
typeid(ValueType)) {
741 strm->Write(
begin(),
sizeof(ValueType) *
ndim_);
743 std::vector<DType> buffer(
ndim_);
745 strm->Write(buffer.data(),
sizeof(DType) *
ndim_);
750 template <
typename ValueType>
751 template <
typename DType,
typename TStream>
756 size_t nread =
sizeof(DType) *
ndim_;
757 if (
typeid(DType) ==
typeid(ValueType)) {
758 if (strm->Read(
begin(), nread) != nread)
761 std::vector<DType> buffer(
ndim_);
762 if (strm->Read(buffer.data(), nread) != nread)
773 template <
typename T>
777 std::hash<int> hash_int;
778 size_t res = hash_int(val.
ndim());
779 for (
int i = 0; i < val.
ndim(); ++i) {
791 std::hash<int> hash_int;
792 size_t res = hash_int(val.
ndim());
793 for (
int i = 0; i < val.
ndim(); ++i) {
806 #if !(defined(_MSC_VER) && _MSC_VER < 1900)
807 template <
typename T>
810 return "tuple of <" + type_name<T>() +
">";
846 #endif // MXNET_TUPLE_H_
const ValueType * begin() const
Definition: tuple.h:201
namespace of mxnet
Definition: api_registry.h:33
bool ndim_is_known(const int ndim)
Definition: tuple.h:416
void Read(ValueType *out_value)
Read next ValueType.
static const int kStackCache
Definition: tuple.h:390
TShape & operator=(const Tuple< dim_t > &src)
assignment function from tshape
Definition: tuple.h:505
helper class to construct a string that represents type name
Definition: type_traits.h:86
int ndim_
number of dimension of the tuple
Definition: tuple.h:392
const ValueType * end() const
Definition: tuple.h:209
A dynamic sized array data structure that is optimized for storing small number of elements with same...
Definition: tuple.h:57
Data structures that can appear in operator attributes.
TShape(const ObjectRef &src)
Definition: tuple.h:499
TShape & operator=(Tuple< dim_t > &&src)
move assignment function from tshape
Definition: tuple.h:518
Tuple(const Tuple< ValueType > &s)
copy constructor from another tuple
Definition: tuple.h:80
void Write(const ValueType &value)
Write value to json.
bool isspace(char c)
Inline implementation of isspace(). Tests whether the given character is a whitespace letter.
Definition: strtonum.h:26
namespace for dmlc
Definition: array_view.h:12
Tuple(Tuple< ValueType > &&src)
move constructor from Tuple
Definition: tuple.h:106
bool shape_is_known(const TShape &x)
Definition: tuple.h:699
Tuple< ValueType > & operator=(Tuple< ValueType > &&src)
assignment from rvalue of another tuple.
Definition: tuple.h:169
TShape(std::initializer_list< dim_t > init)
constructor from initializer list
Definition: tuple.h:472
Tuple< ValueType > & operator=(const Tuple< ValueType > &src)
assignment from another tuple.
Definition: tuple.h:156
void Load(dmlc::JSONReader *reader)
Load Tuple from JSON.
Definition: tuple.h:260
static std::string value()
Definition: tuple.h:809
#define DMLC_DECLARE_TYPE_NAME(Type, Name)
macro to quickly declare traits information
Definition: type_traits.h:133
bool isdigit(char c)
Inline implementation of isdigit(). Tests whether the given character is a decimal digit.
Definition: strtonum.h:46
Tuple(std::initializer_list< ValueType > init)
constructor from initializer list
Definition: tuple.h:91
TShape(RandomAccessIterator begin, RandomAccessIterator end)
construct the Tuple from content of iterator. This function is enforced with template arguments of ra...
Definition: tuple.h:495
int ndim() const
Definition: tuple.h:217
TShape(Tuple< dim_t > &&s)
move constructor.
Definition: tuple.h:479
ValueType data_stack_[kStackCache]
in stack space used to store shape when it is small
Definition: tuple.h:396
bool operator==(const Tuple< ValueType > &s) const
Definition: tuple.h:186
Base expr nodes in MXNet.
Tuple(const runtime::ObjectRef &src)
Definition: tuple.h:120
nnvm::FInferNodeEntryAttr< mxnet::TShape > FInferShape
Shape inference function. Update the shapes given the input shape information. TShape....
Definition: tuple.h:842
size_t HashCombine(size_t key, const T &value)
hash an object and combines the key with previous keys
Definition: common.h:37
ValueType * end()
Definition: tuple.h:213
Data structures that can appear in graph attributes.
int64_t dim_t
data type to store dim size
Definition: c_api.h:69
index_t shape_[kDimension]
storing the dimension information
Definition: tensor.h:86
Tuple(std::vector< ValueType > init)
constructor from vector
Definition: tuple.h:98
size_t operator()(const mxnet::Tuple< T > &val) const
hash a Tuple into unsigned int
Definition: tuple.h:776
TShape()
default constructor
Definition: tuple.h:443
int num_heap_allocated_
number of cells allocated in data_heap_
Definition: tuple.h:394
TShape(const int ndim, const dim_t value)
Definition: tuple.h:451
friend std::ostream & operator<<(std::ostream &os, const Tuple< ValueType > &t)
allow output string of tuple to ostream
Definition: tuple.h:271
size_t ProdShape(int dimstart, int dimend) const
Definition: tuple.h:538
dim_t * data()
Definition: tuple.h:556
int64_t dim_t
data type to store dim size
Definition: tuple.h:39
Tuple()=default
default constructor
TShape(const Tuple< dim_t > &s)
copy constructor of TShape
Definition: tuple.h:461
Lightweight json to write any STL compositions.
Definition: json.h:190
Tuple(RandomAccessIterator begin, RandomAccessIterator end)
construct the Tuple from content of iterator
Definition: tuple.h:116
ValueType * begin()
Definition: tuple.h:205
size_t Size() const
Definition: tuple.h:523
const dim_t * data() const
Definition: tuple.h:552
bool operator!=(const Tuple< ValueType > &s) const
Definition: tuple.h:197
Configuation of nnvm as well as basic data structure.
bool dim_size_is_known(const dim_t dim_size)
Definition: tuple.h:422
MSHADOW_XINLINE Shape< 2 > Shape2(index_t s0, index_t s1)
construct a two dimension shape, stride will equal s0
Definition: tensor.h:230
friend std::istream & operator>>(std::istream &is, Tuple< ValueType > &t)
read tuple from the istream
Definition: tuple.h:296
Base class of all object reference.
Definition: object.h:500
Definition: optional.h:251
std::vector< mxnet::TShape > ShapeVector
The result holder of shape of each NodeEntry in the graph.
Definition: tuple.h:830
Tuple< ValueType > & operator=(std::initializer_list< ValueType > init)
assignment from initializer list
Definition: tuple.h:178
size_t operator()(const mxnet::TShape &val) const
hash a TShape into unsigned int
Definition: tuple.h:790
Tuple(const int ndim, const dim_t value)
Definition: tuple.h:70
A Shape class that is used to represent shape of each tensor.
Definition: tuple.h:440
DstIter ShapeTypeCast(const SrcIter begin, const SrcIter end, DstIter dst_begin)
helper function to cast type of container elements
Definition: tuple.h:719
void Save(dmlc::JSONWriter *writer) const
Save Tuple to JSON.
Definition: tuple.h:252
void assign(RandomAccessIterator begin, RandomAccessIterator end)
Assign content to tuple from iterator.
Definition: tuple.h:136
const ValueType & operator[](int i) const
get corresponding index
Definition: tuple.h:239
ValueType & operator[](int i)
get corresponding index
Definition: tuple.h:225
Lightweight JSON Reader to read any STL compositions and structs. The user need to know the schema of...
Definition: json.h:44
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
ValueType * data_heap_
space to store shape when dimension is big
Definition: tuple.h:398
~Tuple()
destructor
Definition: tuple.h:62
void swap(Tuple< ValueType > &other)
Swap current object with other.
Definition: tuple.h:145
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:241
A managed object in MXNet runtime.
Pass that can be applied to a graph.
void SetDim(int ndim)
Definition: tuple.h:400