tvm
shape_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  */
19 
24 #ifndef TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
25 #define TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
26 
27 #include <ostream>
28 #include <utility>
29 #include <vector>
30 
31 #include "./base.h"
32 
33 namespace tvm {
34 namespace runtime {
35 
37 class ShapeTupleObj : public Object {
38  public:
40  using index_type = int64_t;
44  uint64_t size;
45 
47  index_type Product() const;
48 
49  static constexpr const uint32_t _type_index = runtime::TypeIndex::kRuntimeShapeTuple;
50  static constexpr const char* _type_key = "runtime.ShapeTuple";
52 
53  private:
55  class FromStd;
56 
57  friend class ShapeTuple;
58 };
59 
62  public:
73  explicit FromStd(std::vector<index_type> other) : data_container{other} {}
74 
75  private:
77  std::vector<index_type> data_container;
78 
79  friend class ShapeTuple;
80 };
81 
85 class ShapeTuple : public ObjectRef {
86  public:
89 
93  ShapeTuple() : ShapeTuple(std::vector<index_type>()) {}
94 
101  template <typename IterType>
102  ShapeTuple(IterType begin, IterType end) : ShapeTuple(std::vector<index_type>(begin, end)) {}
103 
108  ShapeTuple(std::initializer_list<index_type> shape) : ShapeTuple(shape.begin(), shape.end()) {}
109 
118  ShapeTuple(std::vector<index_type> shape); // NOLINT(*)
119 
125  const index_type* data() const { return get()->data; }
126 
132  size_t size() const { return get()->size; }
133 
139  index_type operator[](size_t idx) const {
140  ICHECK(idx < this->size()) << "IndexError: indexing " << idx << " on an array of size "
141  << this->size();
142  return this->data()[idx];
143  }
144 
150  index_type at(size_t idx) const { return this->operator[](idx); }
151 
153  bool empty() const { return size() == 0; }
154 
156  index_type front() const { return this->at(0); }
157 
159  index_type back() const { return this->at(this->size() - 1); }
160 
162  const index_type* begin() const { return get()->data; }
163 
165  const index_type* end() const { return (get()->data + size()); }
166 
168 };
169 
170 inline ShapeTuple::ShapeTuple(std::vector<index_type> shape) {
171  auto ptr = make_object<ShapeTupleObj::FromStd>(std::move(shape));
172  ptr->size = ptr->data_container.size();
173  ptr->data = ptr->data_container.data();
174  data_ = std::move(ptr);
175 }
176 
178  index_type numel = 1;
179  for (int i = 0, n = this->size; i < n; ++i) {
180  numel *= this->data[i];
181  }
182  return numel;
183 }
184 
185 inline std::ostream& operator<<(std::ostream& os, const ShapeTuple& shape) {
186  os << '[';
187  for (size_t i = 0; i < shape->size; ++i) {
188  if (i != 0) {
189  os << ", ";
190  }
191  os << shape->data[i];
192  }
193  os << ']';
194  return os;
195 }
196 
199 
200 } // namespace runtime
201 
202 // expose the functions to the root namespace.
203 using runtime::IntTuple;
205 using runtime::ShapeTuple;
207 } // namespace tvm
208 
209 #endif // TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
Base class of all object reference.
Definition: object.h:519
const Object * get() const
Definition: object.h:554
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:605
base class of all object containers.
Definition: object.h:171
An object representing shape tuple moved from std::vector.
Definition: shape_tuple.h:61
FromStd(std::vector< index_type > other)
Construct a new FromStd object.
Definition: shape_tuple.h:73
ShapeTupleObj::index_type index_type
The type of shape index element.
Definition: shape_tuple.h:64
An object representing a shape tuple.
Definition: shape_tuple.h:37
index_type * data
The pointer to shape tuple data.
Definition: shape_tuple.h:42
int64_t index_type
The type of shape index element.
Definition: shape_tuple.h:40
TVM_DECLARE_FINAL_OBJECT_INFO(ShapeTupleObj, Object)
uint64_t size
The size of the shape tuple object.
Definition: shape_tuple.h:44
static constexpr const uint32_t _type_index
Definition: shape_tuple.h:49
index_type Product() const
Get "numel", meaning the number of elements of an array if the array has this shape.
Definition: shape_tuple.h:177
static constexpr const char * _type_key
Definition: shape_tuple.h:50
Reference to shape tuple objects.
Definition: shape_tuple.h:85
index_type at(size_t idx) const
Immutably read i-th element from the shape tuple.
Definition: shape_tuple.h:150
const index_type * data() const
Return the data pointer.
Definition: shape_tuple.h:125
index_type back() const
Definition: shape_tuple.h:159
index_type operator[](size_t idx) const
Immutably read i-th element from the shape tuple.
Definition: shape_tuple.h:139
size_t size() const
Return the size of the shape tuple.
Definition: shape_tuple.h:132
ShapeTuple(IterType begin, IterType end)
Constructor from iterator.
Definition: shape_tuple.h:102
ShapeTuple(std::initializer_list< index_type > shape)
constructor from initializer list
Definition: shape_tuple.h:108
bool empty() const
Definition: shape_tuple.h:153
const index_type * begin() const
Definition: shape_tuple.h:162
ShapeTuple()
Construct an empty shape tuple.
Definition: shape_tuple.h:93
const index_type * end() const
Definition: shape_tuple.h:165
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ShapeTuple, ObjectRef, ShapeTupleObj)
ShapeTupleObj::index_type index_type
The type of shape index element.
Definition: shape_tuple.h:88
index_type front() const
Definition: shape_tuple.h:156
ShapeTuple IntTuple
Definition: shape_tuple.h:197
ShapeTupleObj IntTupleObj
Definition: shape_tuple.h:198
std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:97
Tensor shape(const Tensor &src, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1853
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
@ kRuntimeShapeTuple
runtime::ShapeTuple.
Definition: object.h:72