You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2021/06/07 01:02:02 UTC

[GitHub] [tvm] ZihengJiang opened a new pull request #8200: [RUNTIME] ShapeTuple Container

ZihengJiang opened a new pull request #8200:
URL: https://github.com/apache/tvm/pull/8200


   Thanks for contributing to TVM!   Please refer to guideline https://tvm.apache.org/docs/contribute/ for useful information and tips. After the pull request is submitted, please request code reviews from [Reviewers](https://github.com/apache/incubator-tvm/blob/master/CONTRIBUTORS.md#reviewers) by @ them in the pull request thread.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] ZihengJiang commented on pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
ZihengJiang commented on pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#issuecomment-858156751


   Merged. Thanks @tqchen @junrushao1994 @tkonolige for review.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] ZihengJiang commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
ZihengJiang commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r647965117



##########
File path: python/tvm/runtime/container.py
##########
@@ -137,3 +137,24 @@ def __from_tvm_object__(cls, obj):
         val = str.__new__(cls, content)
         val.__tvm_object__ = obj
         return val
+
+
+@tvm._ffi.register_object("runtime.ShapeTuple")
+class ShapeTuple(Object):

Review comment:
       the iteration has been supported by `__getitem__`, see the test case 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] ZihengJiang commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
ZihengJiang commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r647964668



##########
File path: src/relay/transforms/pattern_utils.h
##########
@@ -441,7 +441,7 @@ static inline long double ToScalar(const runtime::NDArray& array, size_t i = 0)
 static inline Array<Integer> ToVector(const runtime::NDArray& array) {
   size_t ndim = array.Shape().size();
   ICHECK_EQ(ndim, 1) << "This function should only be used for 1D NDArrays";
-  size_t len = array.Shape().front();
+  size_t len = array.Shape().at(0);

Review comment:
       not sure whether we need those for a tuple object




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r647961021



##########
File path: python/tvm/runtime/container.py
##########
@@ -137,3 +137,24 @@ def __from_tvm_object__(cls, obj):
         val = str.__new__(cls, content)
         val.__tvm_object__ = obj
         return val
+
+
+@tvm._ffi.register_object("runtime.ShapeTuple")
+class ShapeTuple(Object):

Review comment:
       perhaps add __iter__?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] ZihengJiang commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
ZihengJiang commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r648739431



##########
File path: include/tvm/runtime/container/shape_tuple.h
##########
@@ -0,0 +1,180 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/container/shape_tuple.h
+ * \brief Runtime ShapeTuple container types.
+ */
+#ifndef TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+#define TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+
+#include <utility>
+#include <vector>
+
+#include "./base.h"
+
+namespace tvm {
+namespace runtime {
+
+/*! \brief An object representing a shape tuple. */
+class ShapeTupleObj : public Object {
+ public:
+  /*! \brief The type of shape index element. */
+  using index_type = int64_t;
+  /*! \brief The pointer to shape tuple data. */
+  index_type* data;

Review comment:
       To make sure that the memory layout is POD. Actually the memory will be managed by std::vector (see FromStd).




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] tqchen commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
tqchen commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r646803566



##########
File path: include/tvm/runtime/container/shape_tuple.h
##########
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/container/shape_tuple.h
+ * \brief Runtime ShapeTuple container types.
+ */
+#ifndef TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+#define TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+
+#include <utility>
+#include <vector>
+
+#include "./base.h"
+
+namespace tvm {
+namespace runtime {
+
+class ShapeTupleObj : public Object {
+ public:
+  using index_type = int64_t;
+  index_type* data;
+  uint64_t size;
+
+  static constexpr const uint32_t _type_index = runtime::TypeIndex::kRuntimeShapeTuple;
+  static constexpr const char* _type_key = "runtime.ShapeTuple";
+  TVM_DECLARE_FINAL_OBJECT_INFO(ShapeTupleObj, Object);
+
+ private:
+  class FromStd;
+  friend class ShapeTuple;
+};
+
+class ShapeTupleObj::FromStd : public ShapeTupleObj {
+ public:
+  using index_type = ShapeTupleObj::index_type;
+  explicit FromStd(std::vector<index_type> other) : data_container{other} {}
+
+ private:
+  /*! \brief Container that holds the memory. */
+  std::vector<index_type> data_container;
+
+  friend class ShapeTuple;
+};
+
+class ShapeTuple : public ObjectRef {
+ public:
+  using index_type = ShapeTupleObj::index_type;
+  ShapeTuple() : ShapeTuple(std::vector<index_type>()) {}
+  template <typename Iterator>
+  ShapeTuple(Iterator begin, Iterator end) : ShapeTuple(std::vector<index_type>(begin, end)) {}
+  ShapeTuple(std::initializer_list<index_type> shape) : ShapeTuple(shape.begin(), shape.end()) {}
+
+  ShapeTuple(std::vector<index_type> shape);  // NOLINT(*)
+
+  const index_type* data() const { return get()->data; }
+  size_t size() const { return get()->size; }
+  index_type operator[](size_t idx) const { return this->data()[idx]; }

Review comment:
       as long as you do not return reference it should be OK




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] tqchen commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
tqchen commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r647853956



##########
File path: include/tvm/runtime/container/shape_tuple.h
##########
@@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/container/shape_tuple.h
+ * \brief Runtime ShapeTuple container types.
+ */
+#ifndef TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+#define TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+
+#include <utility>
+#include <vector>
+
+#include "./base.h"
+
+namespace tvm {
+namespace runtime {
+
+/*! \brief An object representing a shape tuple. */
+class ShapeTupleObj : public Object {
+ public:
+  using index_type = int64_t;

Review comment:
       document

##########
File path: src/runtime/container.cc
##########
@@ -185,7 +184,32 @@ TVM_REGISTER_GLOBAL("runtime.MapItems").set_body([](TVMArgs args, TVMRetValue* r
 TVM_DLL constexpr uint64_t DenseMapNode::kNextProbeLocation[];
 #endif
 
+// Closure
 TVM_REGISTER_OBJECT_TYPE(ClosureObj);
 
+// ShapeTuple
+TVM_REGISTER_OBJECT_TYPE(ShapeTupleObj);
+
+TVM_REGISTER_GLOBAL("runtime.ShapeTuple").set_body([](TVMArgs args, TVMRetValue* rv) {
+  std::vector<ShapeTuple::index_type> shape;
+  for (int i = 0; i < args.size(); i++) {
+    shape.push_back(args[i]);
+  }
+  *rv = ShapeTuple(shape);
+});
+
+TVM_REGISTER_GLOBAL("runtime.GetShapeTupleSize").set_body([](TVMArgs args, TVMRetValue* rv) {

Review comment:
       use use set_body_typed

##########
File path: src/runtime/container.cc
##########
@@ -185,7 +184,32 @@ TVM_REGISTER_GLOBAL("runtime.MapItems").set_body([](TVMArgs args, TVMRetValue* r
 TVM_DLL constexpr uint64_t DenseMapNode::kNextProbeLocation[];
 #endif
 
+// Closure
 TVM_REGISTER_OBJECT_TYPE(ClosureObj);
 
+// ShapeTuple
+TVM_REGISTER_OBJECT_TYPE(ShapeTupleObj);
+
+TVM_REGISTER_GLOBAL("runtime.ShapeTuple").set_body([](TVMArgs args, TVMRetValue* rv) {

Review comment:
       use use set_body_typed
   
   

##########
File path: src/runtime/container.cc
##########
@@ -185,7 +184,32 @@ TVM_REGISTER_GLOBAL("runtime.MapItems").set_body([](TVMArgs args, TVMRetValue* r
 TVM_DLL constexpr uint64_t DenseMapNode::kNextProbeLocation[];
 #endif
 
+// Closure
 TVM_REGISTER_OBJECT_TYPE(ClosureObj);
 
+// ShapeTuple
+TVM_REGISTER_OBJECT_TYPE(ShapeTupleObj);
+
+TVM_REGISTER_GLOBAL("runtime.ShapeTuple").set_body([](TVMArgs args, TVMRetValue* rv) {
+  std::vector<ShapeTuple::index_type> shape;
+  for (int i = 0; i < args.size(); i++) {
+    shape.push_back(args[i]);
+  }
+  *rv = ShapeTuple(shape);
+});
+
+TVM_REGISTER_GLOBAL("runtime.GetShapeTupleSize").set_body([](TVMArgs args, TVMRetValue* rv) {
+  ObjectRef obj = args[0];
+  const auto& shape = Downcast<ShapeTuple>(obj);
+  *rv = static_cast<int64_t>(shape.size());
+});
+
+TVM_REGISTER_GLOBAL("runtime.GetShapeTupleElem").set_body([](TVMArgs args, TVMRetValue* rv) {

Review comment:
       use set_body_typed([](ShapeTuple tuple, int64_t index) -> int64_t {})




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r647961955



##########
File path: src/relay/transforms/partial_eval.cc
##########
@@ -753,7 +753,7 @@ class PartialEvaluator : public ExprFunctor<PStatic(const Expr& e, LetList* ll)>
   size_t GetFTValue(const PStatic& ps) {
     if (ps->pstatic.defined()) {
       if (auto* st = ps->pstatic.as<STensorNode>()) {
-        if (st->data.Shape().empty()) {
+        if (st->data.Shape().size() == 0) {

Review comment:
       add `.empty()`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] ZihengJiang commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
ZihengJiang commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r646215772



##########
File path: include/tvm/runtime/container/shape_tuple.h
##########
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/container/shape_tuple.h
+ * \brief Runtime ShapeTuple container types.
+ */
+#ifndef TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+#define TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+
+#include <utility>
+#include <vector>
+
+#include "./base.h"
+
+namespace tvm {
+namespace runtime {
+
+class ShapeTupleObj : public Object {
+ public:
+  using index_type = int64_t;
+  index_type* data;
+  uint64_t size;
+
+  static constexpr const uint32_t _type_index = runtime::TypeIndex::kRuntimeShapeTuple;
+  static constexpr const char* _type_key = "runtime.ShapeTuple";
+  TVM_DECLARE_FINAL_OBJECT_INFO(ShapeTupleObj, Object);
+
+ private:
+  class FromStd;
+  friend class ShapeTuple;
+};
+
+class ShapeTupleObj::FromStd : public ShapeTupleObj {
+ public:
+  using index_type = ShapeTupleObj::index_type;
+  explicit FromStd(std::vector<index_type> other) : data_container{other} {}
+
+ private:
+  /*! \brief Container that holds the memory. */
+  std::vector<index_type> data_container;
+
+  friend class ShapeTuple;
+};
+
+class ShapeTuple : public ObjectRef {
+ public:
+  using index_type = ShapeTupleObj::index_type;
+  ShapeTuple() : ShapeTuple(std::vector<index_type>()) {}
+  template <typename Iterator>
+  ShapeTuple(Iterator begin, Iterator end) : ShapeTuple(std::vector<index_type>(begin, end)) {}
+  ShapeTuple(std::initializer_list<index_type> shape) : ShapeTuple(shape.begin(), shape.end()) {}
+
+  ShapeTuple(std::vector<index_type> shape);  // NOLINT(*)
+
+  const index_type* data() const { return get()->data; }
+  size_t size() const { return get()->size; }
+  index_type operator[](size_t idx) const { return this->data()[idx]; }

Review comment:
       Do we need to only allow read-only access here? That might mean we need a proxy class for it.

##########
File path: include/tvm/runtime/container/shape_tuple.h
##########
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/container/shape_tuple.h
+ * \brief Runtime ShapeTuple container types.
+ */
+#ifndef TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+#define TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+
+#include <utility>
+#include <vector>
+
+#include "./base.h"
+
+namespace tvm {
+namespace runtime {
+
+class ShapeTupleObj : public Object {
+ public:
+  using index_type = int64_t;
+  index_type* data;
+  uint64_t size;
+
+  static constexpr const uint32_t _type_index = runtime::TypeIndex::kRuntimeShapeTuple;
+  static constexpr const char* _type_key = "runtime.ShapeTuple";
+  TVM_DECLARE_FINAL_OBJECT_INFO(ShapeTupleObj, Object);
+
+ private:
+  class FromStd;
+  friend class ShapeTuple;
+};
+
+class ShapeTupleObj::FromStd : public ShapeTupleObj {
+ public:
+  using index_type = ShapeTupleObj::index_type;
+  explicit FromStd(std::vector<index_type> other) : data_container{other} {}
+
+ private:
+  /*! \brief Container that holds the memory. */
+  std::vector<index_type> data_container;
+
+  friend class ShapeTuple;
+};
+
+class ShapeTuple : public ObjectRef {
+ public:
+  using index_type = ShapeTupleObj::index_type;
+  ShapeTuple() : ShapeTuple(std::vector<index_type>()) {}
+  template <typename Iterator>
+  ShapeTuple(Iterator begin, Iterator end) : ShapeTuple(std::vector<index_type>(begin, end)) {}
+  ShapeTuple(std::initializer_list<index_type> shape) : ShapeTuple(shape.begin(), shape.end()) {}
+
+  ShapeTuple(std::vector<index_type> shape);  // NOLINT(*)
+
+  const index_type* data() const { return get()->data; }
+  size_t size() const { return get()->size; }
+  index_type operator[](size_t idx) const { return this->data()[idx]; }
+  index_type at(size_t idx) const { return this->data()[idx]; }
+
+  const index_type* begin() const { return get()->data; }

Review comment:
       this requires `const_cast` when convert shape to `dl_tensor.shape`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r647960076



##########
File path: include/tvm/runtime/container/shape_tuple.h
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/container/shape_tuple.h
+ * \brief Runtime ShapeTuple container types.
+ */
+#ifndef TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+#define TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+
+#include <utility>
+#include <vector>
+
+#include "./base.h"
+
+namespace tvm {
+namespace runtime {
+
+/*! \brief An object representing a shape tuple. */
+class ShapeTupleObj : public Object {
+ public:
+  /*! \brief The type of shape index element. */
+  using index_type = int64_t;
+  /*! \brief The pointer to shape tuple data. */
+  index_type* data;
+  /*! \brief The size of the shape tuple object. */
+  uint64_t size;
+
+  static constexpr const uint32_t _type_index = runtime::TypeIndex::kRuntimeShapeTuple;
+  static constexpr const char* _type_key = "runtime.ShapeTuple";
+  TVM_DECLARE_FINAL_OBJECT_INFO(ShapeTupleObj, Object);
+
+ private:
+  /*! \brief ShapeTuple object which is moved from std::vector container. */
+  class FromStd;
+
+  friend class ShapeTuple;
+};
+
+/*! \brief An object representing shape tuple moved from std::vector. */
+class ShapeTupleObj::FromStd : public ShapeTupleObj {
+ public:
+  /*! \brief The type of shape index element. */
+  using index_type = ShapeTupleObj::index_type;
+  /*!
+   * \brief Construct a new FromStd object
+   *
+   * \param other The moved/copied std::vector object
+   *
+   * \note If user passes const reference, it will trigger copy. If it's rvalue,
+   * it will be moved into other.
+   */
+  explicit FromStd(std::vector<index_type> other) : data_container{other} {}
+
+ private:
+  /*! \brief Container that holds the memory. */
+  std::vector<index_type> data_container;
+
+  friend class ShapeTuple;
+};
+
+/*!
+ * \brief Reference to shape tuple objects.
+ */
+class ShapeTuple : public ObjectRef {
+ public:
+  /*! \brief The type of shape index element. */
+  using index_type = ShapeTupleObj::index_type;
+  /*!
+   * \brief Construct an empty shape tuple.
+   */
+  ShapeTuple() : ShapeTuple(std::vector<index_type>()) {}
+  /*!
+   * \brief Constructor from iterator
+   * \param begin begin of iterator
+   * \param end end of iterator
+   * \tparam IterType The type of iterator
+   */
+  template <typename IterType>
+  ShapeTuple(IterType begin, IterType end) : ShapeTuple(std::vector<index_type>(begin, end)) {}
+  /*!
+   * \brief constructor from initializer list
+   * \param shape The initializer list
+   */
+  ShapeTuple(std::initializer_list<index_type> shape) : ShapeTuple(shape.begin(), shape.end()) {}
+  /*!
+   * \brief Construct a new ShapeTuple object
+   *
+   * \param shape The moved/copied std::vector object
+   *
+   * \note If user passes const reference, it will trigger copy. If it's rvalue,
+   * it will be moved into other.
+   */
+  ShapeTuple(std::vector<index_type> shape);  // NOLINT(*)
+
+  /*!
+   * \brief Return the data pointer
+   *
+   * \return const index_type* data pointer
+   */
+  const index_type* data() const { return get()->data; }
+  /*!
+   * \brief Return the size of the shape tuple
+   *
+   * \return size_t shape tuple size
+   */
+  size_t size() const { return get()->size; }
+  /*!
+   * \brief Immutably read i-th element from the shape tuple.
+   * \param idx The index
+   * \return the i-th element.
+   */
+  index_type operator[](size_t idx) const { return this->data()[idx]; }
+  /*!
+   * \brief Immutably read i-th element from the shape tuple.
+   * \param idx The index
+   * \return the i-th element.
+   */
+  index_type at(size_t idx) const { return this->data()[idx]; }

Review comment:
       Shall we do boundary checking here?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] tqchen commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
tqchen commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r646804224



##########
File path: include/tvm/runtime/container/shape_tuple.h
##########
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/container/shape_tuple.h
+ * \brief Runtime ShapeTuple container types.
+ */
+#ifndef TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+#define TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+
+#include <utility>
+#include <vector>
+
+#include "./base.h"
+
+namespace tvm {
+namespace runtime {
+
+class ShapeTupleObj : public Object {
+ public:
+  using index_type = int64_t;
+  index_type* data;
+  uint64_t size;
+
+  static constexpr const uint32_t _type_index = runtime::TypeIndex::kRuntimeShapeTuple;
+  static constexpr const char* _type_key = "runtime.ShapeTuple";
+  TVM_DECLARE_FINAL_OBJECT_INFO(ShapeTupleObj, Object);
+
+ private:
+  class FromStd;
+  friend class ShapeTuple;
+};
+
+class ShapeTupleObj::FromStd : public ShapeTupleObj {
+ public:
+  using index_type = ShapeTupleObj::index_type;
+  explicit FromStd(std::vector<index_type> other) : data_container{other} {}
+
+ private:
+  /*! \brief Container that holds the memory. */
+  std::vector<index_type> data_container;
+
+  friend class ShapeTuple;
+};
+
+class ShapeTuple : public ObjectRef {
+ public:
+  using index_type = ShapeTupleObj::index_type;
+  ShapeTuple() : ShapeTuple(std::vector<index_type>()) {}
+  template <typename Iterator>
+  ShapeTuple(Iterator begin, Iterator end) : ShapeTuple(std::vector<index_type>(begin, end)) {}
+  ShapeTuple(std::initializer_list<index_type> shape) : ShapeTuple(shape.begin(), shape.end()) {}
+
+  ShapeTuple(std::vector<index_type> shape);  // NOLINT(*)
+
+  const index_type* data() const { return get()->data; }
+  size_t size() const { return get()->size; }
+  index_type operator[](size_t idx) const { return this->data()[idx]; }
+  index_type at(size_t idx) const { return this->data()[idx]; }
+
+  const index_type* begin() const { return get()->data; }

Review comment:
       starting from const is not a bad idea




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] ZihengJiang commented on pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
ZihengJiang commented on pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#issuecomment-856717164


   cc @junrushao1994 @zxybazh for review


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] tkonolige commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
tkonolige commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r648688459



##########
File path: include/tvm/runtime/container/shape_tuple.h
##########
@@ -0,0 +1,180 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/runtime/container/shape_tuple.h
+ * \brief Runtime ShapeTuple container types.
+ */
+#ifndef TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+#define TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+
+#include <utility>
+#include <vector>
+
+#include "./base.h"
+
+namespace tvm {
+namespace runtime {
+
+/*! \brief An object representing a shape tuple. */
+class ShapeTupleObj : public Object {
+ public:
+  /*! \brief The type of shape index element. */
+  using index_type = int64_t;
+  /*! \brief The pointer to shape tuple data. */
+  index_type* data;

Review comment:
       Why are we using a raw pointer here?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] ZihengJiang commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
ZihengJiang commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r647861397



##########
File path: src/runtime/container.cc
##########
@@ -185,7 +184,32 @@ TVM_REGISTER_GLOBAL("runtime.MapItems").set_body([](TVMArgs args, TVMRetValue* r
 TVM_DLL constexpr uint64_t DenseMapNode::kNextProbeLocation[];
 #endif
 
+// Closure
 TVM_REGISTER_OBJECT_TYPE(ClosureObj);
 
+// ShapeTuple
+TVM_REGISTER_OBJECT_TYPE(ShapeTupleObj);
+
+TVM_REGISTER_GLOBAL("runtime.ShapeTuple").set_body([](TVMArgs args, TVMRetValue* rv) {

Review comment:
       we cannot use set_body_typed here since we need to unwrap variable length argument?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] tqchen commented on pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
tqchen commented on pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#issuecomment-860211675


   @ZihengJiang please follow up


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] ZihengJiang commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
ZihengJiang commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r647965266



##########
File path: src/relay/transforms/partial_eval.cc
##########
@@ -753,7 +753,7 @@ class PartialEvaluator : public ExprFunctor<PStatic(const Expr& e, LetList* ll)>
   size_t GetFTValue(const PStatic& ps) {
     if (ps->pstatic.defined()) {
       if (auto* st = ps->pstatic.as<STensorNode>()) {
-        if (st->data.Shape().empty()) {
+        if (st->data.Shape().size() == 0) {

Review comment:
       sounds good




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r647963663



##########
File path: python/tvm/runtime/container.py
##########
@@ -137,3 +137,24 @@ def __from_tvm_object__(cls, obj):
         val = str.__new__(cls, content)
         val.__tvm_object__ = obj
         return val
+
+
+@tvm._ffi.register_object("runtime.ShapeTuple")
+class ShapeTuple(Object):
+    """TVM runtime ShapeTuple object.
+    Parameters
+    ----------
+    shape : list[int]
+        The shape list used to construct the object.
+    """
+
+    def __init__(self, shape):
+        for x in shape:

Review comment:
       Perhaps checks if `shape` is instance of `list` as well?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] ZihengJiang merged pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
ZihengJiang merged pull request #8200:
URL: https://github.com/apache/tvm/pull/8200


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r647965316



##########
File path: src/relay/transforms/pattern_utils.h
##########
@@ -441,7 +441,7 @@ static inline long double ToScalar(const runtime::NDArray& array, size_t i = 0)
 static inline Array<Integer> ToVector(const runtime::NDArray& array) {
   size_t ndim = array.Shape().size();
   ICHECK_EQ(ndim, 1) << "This function should only be used for 1D NDArrays";
-  size_t len = array.Shape().front();
+  size_t len = array.Shape().at(0);

Review comment:
       Several lines of code that are good for future development. @zhiics asked me to add them to `runtime::Array` last time, and it turns out to be useful right now :-)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r647961857



##########
File path: src/runtime/container.cc
##########
@@ -185,7 +184,32 @@ TVM_REGISTER_GLOBAL("runtime.MapItems").set_body([](TVMArgs args, TVMRetValue* r
 TVM_DLL constexpr uint64_t DenseMapNode::kNextProbeLocation[];
 #endif
 
+// Closure
 TVM_REGISTER_OBJECT_TYPE(ClosureObj);
 
+// ShapeTuple
+TVM_REGISTER_OBJECT_TYPE(ShapeTupleObj);
+
+TVM_REGISTER_GLOBAL("runtime.ShapeTuple").set_body([](TVMArgs args, TVMRetValue* rv) {

Review comment:
       Yeah set_body_typed cannot be used here




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] junrushao1994 commented on pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#issuecomment-858749690


   @masahi looks like “0 <= unsigned-integer” always hold, so the comparison is always true. @ZihengJiang would you like to act on this?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] masahi commented on pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#issuecomment-858535884


   I've got this warning when building tvm:
   ```
   ../include/tvm/runtime/container/shape_tuple.h(136): warning: pointless comparison of unsigned integer with zero
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r647961021



##########
File path: python/tvm/runtime/container.py
##########
@@ -137,3 +137,24 @@ def __from_tvm_object__(cls, obj):
         val = str.__new__(cls, content)
         val.__tvm_object__ = obj
         return val
+
+
+@tvm._ffi.register_object("runtime.ShapeTuple")
+class ShapeTuple(Object):

Review comment:
       perhaps add `__iter__?`

##########
File path: python/tvm/runtime/container.py
##########
@@ -137,3 +137,24 @@ def __from_tvm_object__(cls, obj):
         val = str.__new__(cls, content)
         val.__tvm_object__ = obj
         return val
+
+
+@tvm._ffi.register_object("runtime.ShapeTuple")
+class ShapeTuple(Object):

Review comment:
       perhaps add `__iter__`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8200: [RUNTIME] ShapeTuple Container

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8200:
URL: https://github.com/apache/tvm/pull/8200#discussion_r647961490



##########
File path: src/relay/transforms/pattern_utils.h
##########
@@ -441,7 +441,7 @@ static inline long double ToScalar(const runtime::NDArray& array, size_t i = 0)
 static inline Array<Integer> ToVector(const runtime::NDArray& array) {
   size_t ndim = array.Shape().size();
   ICHECK_EQ(ndim, 1) << "This function should only be used for 1D NDArrays";
-  size_t len = array.Shape().front();
+  size_t len = array.Shape().at(0);

Review comment:
       let's add `.front()` and `.back()` like what is done in `runtime::Array`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org