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/01/29 02:57:22 UTC

[GitHub] [tvm] zhiics commented on a change in pull request #7369: [Refactor][VM] Port memory_alloc to c++

zhiics commented on a change in pull request #7369:
URL: https://github.com/apache/tvm/pull/7369#discussion_r566547639



##########
File path: src/relay/transforms/memory_alloc.cc
##########
@@ -0,0 +1,508 @@
+/*
+ * 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 src/relay/transforms/memory_alloc.cc
+ * \brief A pass for manifesting explicit memory allocations.
+ */
+
+#include <tvm/node/structural_equal.h>
+#include <tvm/node/structural_hash.h>
+#include <tvm/relay/analysis.h>
+#include <tvm/relay/attrs/device_copy.h>
+#include <tvm/relay/attrs/memory.h>
+#include <tvm/relay/expr.h>
+#include <tvm/relay/expr_functor.h>
+#include <tvm/relay/op.h>
+#include <tvm/relay/transform.h>
+#include <tvm/support/logging.h>
+#include <tvm/target/target.h>
+
+#include <cstdint>
+#include <cstdio>
+#include <string>
+#include <unordered_set>
+#include <vector>
+
+#include "../backend/compile_engine.h"
+#include "let_list.h"
+
+using namespace tvm::runtime;
+
+namespace tvm {
+namespace relay {
+
+extern bool IsDynamic(const Type& ty);
+extern Expr ToTupleType(const Type& ty, const std::vector<Expr>& exprs);
+extern std::vector<Expr> FromTupleType(const Type& type, const Expr& expr);
+extern std::vector<TensorType> FlattenTupleType(const Type& type);
+
+using AnalysisResultMap =
+    std::unordered_map<Expr, TVMContext, runtime::ObjectPtrHash, runtime::ObjectPtrEqual>;
+
+inline Constant MakeConstant(int64_t value) {
+  auto tensor = NDArray::Empty({}, {kDLInt, 64, 1}, {kDLCPU, 0});
+  reinterpret_cast<int64_t*>(tensor->data)[0] = value;
+  return std::move(Constant(tensor));
+}
+
+inline Constant MakeConstant(const std::vector<int64_t>& value) {
+  auto tensor = NDArray::Empty({static_cast<int>(value.size())}, {kDLInt, 64, 1}, {kDLCPU, 0});
+  for (size_t i = 0; i < value.size(); i++) {
+    reinterpret_cast<int64_t*>(tensor->data)[i] = value[i];
+  }
+  return std::move(Constant(tensor));
+}
+
+inline Expr AllocTensor(const Expr& storage, tvm::relay::Expr shape, DataType dtype,
+                        Array<IndexExpr> assert_shape) {
+  auto f = runtime::Registry::Get("relay.op.memory._make.alloc_tensor");
+  CHECK(f != nullptr) << "unable to find alloc_tensor op";
+  auto offset = MakeConstant(0);
+  return (*f)(storage, offset, shape, dtype, assert_shape);
+}
+
+// A pass to check if the fused op contains only reshape ops.
+class CheckReshapeOnly : public ExprVisitor {
+ public:
+  CheckReshapeOnly()
+      : reshape_(Op::Get("reshape")),
+        contr_reshape_(Op::Get("contrib_reverse_reshape")),
+        dyn_reshape_(Op::Get("dyn.reshape")) {}
+
+  void VisitExpr_(const CallNode* cn) final {
+    if (!reshape_only) return;
+    if (cn->op != reshape_ && cn->op != contr_reshape_ && cn->op != dyn_reshape_) {
+      reshape_only = false;
+    }
+    for (auto arg : cn->args) ExprVisitor::VisitExpr(arg);
+  }
+
+  void VisitExpr_(const VarNode* vn) final {
+    if (!vn->checked_type_->IsInstance<TensorTypeNode>()) {
+      reshape_only = false;
+    }
+  }
+
+  const Op& reshape_;
+  const Op& contr_reshape_;
+  const Op& dyn_reshape_;
+  bool reshape_only{true};
+};
+
+// Check if the primitive function contains only reshape ops.
+bool IsReshapeOnly(const Expr& expr) {
+  auto check = CheckReshapeOnly();
+  check.VisitExpr(expr);
+  return check.reshape_only;
+}
+
+class DialectRewriter : public ExprMutator {

Review comment:
       Yeah, this is international. The pass is essentially rewriting to the memory dialects and it is internal as well. `ManifestAllocPass` is used for invocation purpose as the old python pass does.




----------------------------------------------------------------
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