tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 <utility>
28 #include <vector>
29 
30 #include "./base.h"
31 
32 namespace tvm {
33 namespace runtime {
34 
36 class ShapeTupleObj : public Object {
37  public:
39  using index_type = int64_t;
43  uint64_t size;
44 
45  static constexpr const uint32_t _type_index = runtime::TypeIndex::kRuntimeShapeTuple;
46  static constexpr const char* _type_key = "runtime.ShapeTuple";
48 
49  private:
51  class FromStd;
52 
53  friend class ShapeTuple;
54 };
55 
58  public:
69  explicit FromStd(std::vector<index_type> other) : data_container{other} {}
70 
71  private:
73  std::vector<index_type> data_container;
74 
75  friend class ShapeTuple;
76 };
77 
81 class ShapeTuple : public ObjectRef {
82  public:
85 
89  ShapeTuple() : ShapeTuple(std::vector<index_type>()) {}
90 
97  template <typename IterType>
98  ShapeTuple(IterType begin, IterType end) : ShapeTuple(std::vector<index_type>(begin, end)) {}
99 
104  ShapeTuple(std::initializer_list<index_type> shape) : ShapeTuple(shape.begin(), shape.end()) {}
105 
114  ShapeTuple(std::vector<index_type> shape); // NOLINT(*)
115 
121  const index_type* data() const { return get()->data; }
122 
128  size_t size() const { return get()->size; }
129 
135  index_type operator[](size_t idx) const {
136  ICHECK(idx < this->size()) << "IndexError: indexing " << idx << " on an array of size "
137  << this->size();
138  return this->data()[idx];
139  }
140 
146  index_type at(size_t idx) const { return this->operator[](idx); }
147 
149  bool empty() const { return size() == 0; }
150 
152  index_type front() const { return this->at(0); }
153 
155  index_type back() const { return this->at(this->size() - 1); }
156 
158  const index_type* begin() const { return get()->data; }
159 
161  const index_type* end() const { return (get()->data + size()); }
162 
164 };
165 
166 inline ShapeTuple::ShapeTuple(std::vector<index_type> shape) {
167  auto ptr = make_object<ShapeTupleObj::FromStd>(std::move(shape));
168  ptr->size = ptr->data_container.size();
169  ptr->data = ptr->data_container.data();
170  data_ = std::move(ptr);
171 }
172 
173 } // namespace runtime
174 
175 // expose the functions to the root namespace.
176 using runtime::ShapeTuple;
178 } // namespace tvm
179 
180 #endif // TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
Base class of all object reference.
Definition: object.h:517
const Object * get() const
Definition: object.h:552
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:603
base class of all object containers.
Definition: object.h:169
An object representing shape tuple moved from std::vector.
Definition: shape_tuple.h:57
FromStd(std::vector< index_type > other)
Construct a new FromStd object.
Definition: shape_tuple.h:69
ShapeTupleObj::index_type index_type
The type of shape index element.
Definition: shape_tuple.h:60
An object representing a shape tuple.
Definition: shape_tuple.h:36
index_type * data
The pointer to shape tuple data.
Definition: shape_tuple.h:41
int64_t index_type
The type of shape index element.
Definition: shape_tuple.h:39
TVM_DECLARE_FINAL_OBJECT_INFO(ShapeTupleObj, Object)
uint64_t size
The size of the shape tuple object.
Definition: shape_tuple.h:43
static constexpr const uint32_t _type_index
Definition: shape_tuple.h:45
static constexpr const char * _type_key
Definition: shape_tuple.h:46
Reference to shape tuple objects.
Definition: shape_tuple.h:81
index_type at(size_t idx) const
Immutably read i-th element from the shape tuple.
Definition: shape_tuple.h:146
const index_type * data() const
Return the data pointer.
Definition: shape_tuple.h:121
index_type back() const
Definition: shape_tuple.h:155
index_type operator[](size_t idx) const
Immutably read i-th element from the shape tuple.
Definition: shape_tuple.h:135
size_t size() const
Return the size of the shape tuple.
Definition: shape_tuple.h:128
ShapeTuple(IterType begin, IterType end)
Constructor from iterator.
Definition: shape_tuple.h:98
ShapeTuple(std::initializer_list< index_type > shape)
constructor from initializer list
Definition: shape_tuple.h:104
bool empty() const
Definition: shape_tuple.h:149
const index_type * begin() const
Definition: shape_tuple.h:158
ShapeTuple()
Construct an empty shape tuple.
Definition: shape_tuple.h:89
const index_type * end() const
Definition: shape_tuple.h:161
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ShapeTuple, ObjectRef, ShapeTupleObj)
ShapeTupleObj::index_type index_type
The type of shape index element.
Definition: shape_tuple.h:84
index_type front() const
Definition: shape_tuple.h:152
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:1766
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
@ kRuntimeShapeTuple
runtime::ShapeTuple.
Definition: object.h:72