This is an automated email from the ASF dual-hosted git repository.
tqchen pushed a commit to branch unity
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/unity by this push:
new c604012310 [Runtime] ShapeTuple.Product and ShapeTuple Printing
(#15652)
c604012310 is described below
commit c6040123108a4f45b0c464f6511c77dcdaba9146
Author: Junru Shao <[email protected]>
AuthorDate: Sat Sep 2 04:08:44 2023 -0700
[Runtime] ShapeTuple.Product and ShapeTuple Printing (#15652)
This PR adds two convenient methods for `ShapeTuple`.
```C++
// Returns the number of elements in the shape,
// i.e. the product of all dimensions
ShapeTupleObj::index_type ShapeTupleObj::Product();
// Printing method for shape
std::ostream& operator<<(std::ostream& os, const ShapeTuple& shape);
```
---
include/tvm/runtime/container/shape_tuple.h | 24 ++++++++++++++++++++++++
src/node/container_printing.cc | 10 +---------
2 files changed, 25 insertions(+), 9 deletions(-)
diff --git a/include/tvm/runtime/container/shape_tuple.h
b/include/tvm/runtime/container/shape_tuple.h
index 3a6f1843d4..d4af8228f0 100644
--- a/include/tvm/runtime/container/shape_tuple.h
+++ b/include/tvm/runtime/container/shape_tuple.h
@@ -24,6 +24,7 @@
#ifndef TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
#define TVM_RUNTIME_CONTAINER_SHAPE_TUPLE_H_
+#include <ostream>
#include <utility>
#include <vector>
@@ -42,6 +43,9 @@ class ShapeTupleObj : public Object {
/*! \brief The size of the shape tuple object. */
uint64_t size;
+ /*! \brief Get "numel", meaning the number of elements of an array if the
array has this shape */
+ index_type Product() const;
+
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);
@@ -170,6 +174,26 @@ inline ShapeTuple::ShapeTuple(std::vector<index_type>
shape) {
data_ = std::move(ptr);
}
+inline ShapeTupleObj::index_type ShapeTupleObj::Product() const {
+ index_type numel = 1;
+ for (int i = 0, n = this->size; i < n; ++i) {
+ numel *= this->data[i];
+ }
+ return numel;
+}
+
+inline std::ostream& operator<<(std::ostream& os, const ShapeTuple& shape) {
+ os << '[';
+ for (size_t i = 0; i < shape->size; ++i) {
+ if (i != 0) {
+ os << ", ";
+ }
+ os << shape->data[i];
+ }
+ os << ']';
+ return os;
+}
+
} // namespace runtime
// expose the functions to the root namespace.
diff --git a/src/node/container_printing.cc b/src/node/container_printing.cc
index 1565630cc6..78318c4308 100644
--- a/src/node/container_printing.cc
+++ b/src/node/container_printing.cc
@@ -62,14 +62,6 @@ TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
.set_dispatch<ShapeTupleObj>([](const ObjectRef& node, ReprPrinter* p) {
- auto* op = static_cast<const ShapeTupleObj*>(node.get());
- p->stream << '[';
- for (size_t i = 0; i < op->size; ++i) {
- if (i != 0) {
- p->stream << ", ";
- }
- p->stream << op->data[i];
- }
- p->stream << ']';
+ p->stream << Downcast<ShapeTuple>(node);
});
} // namespace tvm