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 2020/06/12 16:03:02 UTC

[GitHub] [incubator-tvm] tqchen commented on a change in pull request #5770: [BYOC][runtime] Separate code and metadata for CSourceModule

tqchen commented on a change in pull request #5770:
URL: https://github.com/apache/incubator-tvm/pull/5770#discussion_r439504941



##########
File path: src/runtime/module_init_wrapper.cc
##########
@@ -0,0 +1,234 @@
+/*
+ * 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/runtime/module_init_wrapper.cc
+ * \brief A wrapper for initializing modules using metadata
+ */
+#include <tvm/node/container.h>
+#include <tvm/runtime/ndarray.h>
+#include <tvm/runtime/packed_func.h>
+#include <tvm/runtime/registry.h>
+
+#include <cstdint>
+#include <sstream>
+
+#include "file_util.h"
+
+namespace tvm {
+namespace runtime {
+
+using StringNDArrayMap = std::unordered_map<String, runtime::NDArray, ObjectHash, ObjectEqual>;
+
+class CSourceMetadataInitializer {
+ public:
+  explicit CSourceMetadataInitializer(const StringNDArrayMap& metadata) : metadata_(metadata) {}
+
+  template <typename T>
+  void GetElements(const std::string& var_name, const std::string& type_name,
+                   const runtime::NDArray& arr) {
+    // Get the number of elements.
+    int64_t num_elems = 1;
+    for (auto i : arr.Shape()) num_elems *= i;
+    stream_ << "static " << type_name << " " << var_name << "[" << num_elems << "] = {";
+    T* ptr = static_cast<T*>(arr->data);
+    for (int64_t i = 0; i < num_elems - 1; i++) {
+      stream_ << ptr[i] << ",";
+    }
+    if (num_elems > 0) stream_ << ptr[num_elems - 1];
+    stream_ << "};\n";
+  }
+
+  std::string Init() {
+    for (const auto& it : metadata_) {
+      std::string var_name = it.first.operator std::string();
+      runtime::NDArray data = it.second;
+      CHECK_EQ(data->dtype.lanes, 1U);
+      if (data->dtype.code == kDLFloat) {
+        if (data->dtype.bits == 32) {
+          stream_.precision(std::numeric_limits<float>::digits10 + 1);
+          GetElements<float>(var_name, "float", data);
+        } else {
+          CHECK_EQ(data->dtype.bits, 64);
+          stream_.precision(std::numeric_limits<double>::digits10 + 1);
+          GetElements<double>(var_name, "double", data);
+        }
+      } else if (data->dtype.code == kDLUInt) {
+        if (data->dtype.bits == 8) {
+          GetElements<uint8_t>(var_name, "uint8_t", data);
+        } else {
+          CHECK_EQ(data->dtype.bits, 32);
+          GetElements<uint32_t>(var_name, "uint32_t", data);
+        }
+      } else {
+        if (data->dtype.bits == 8) {
+          GetElements<int8_t>(var_name, "int8_t", data);
+        } else {
+          CHECK_EQ(data->dtype.bits, 32);
+          GetElements<int32_t>(var_name, "int32_t", data);
+        }
+      }
+    }
+    return stream_.str();
+  }
+
+ private:
+  /*! \brief The stream to print constant data. */
+  std::ostringstream stream_;
+  /*! \brief variable name to NDArray mapping. */
+  StringNDArrayMap metadata_;
+};
+
+class ModuleInitWrapper : public runtime::ModuleNode {

Review comment:
       Let us think a bit about the name. ModuleInitWrapper may not be the best name.

##########
File path: python/tvm/runtime/module.py
##########
@@ -33,6 +33,25 @@
 ProfileResult = namedtuple("ProfileResult", ["mean", "results"])
 
 
+def ModuleInitWrapper(variables, metadata):
+    """Create a module initialization wrapper.

Review comment:
       Do we need to expose ModuleInitWrapper in the python side? Is it possible to simply hide the Module as part of backend, instead a frontend entity?

##########
File path: src/target/source/source_module.cc
##########
@@ -152,8 +153,92 @@ runtime::Module DeviceSourceModuleCreate(
   return runtime::Module(n);
 }
 
+// A helper used to wrap different types of modules and pass through packedfunc.
+// This module will never be used for compilation and execution.
+class ModuleClassWrapperNode : public runtime::ModuleNode {
+ public:
+  ModuleClassWrapperNode() = default;
+  const char* type_key() const { return "module_class_wrapper"; }
+  PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) final {
+    LOG(FATAL) << "Cannot execute module wrapper";
+    return PackedFunc();
+  }
+};
+
+runtime::Module ModuleClassWrapperCreate() {
+  auto n = make_object<ModuleClassWrapperNode>();
+  return runtime::Module(n);
+}
+
+// Pack the source code and metadata, where source code could be any
+// user-defined code, i.e. c source code, json graph representation, etc.
+class SourceMetadataModuleNode final : public runtime::ModuleNode {
+ public:
+  SourceMetadataModuleNode(const String& func_symbol, const String& code, const String& source_type,
+                           const Array<String>& variables, const Array<runtime::NDArray>& metadata)
+      : func_symbol_(func_symbol),
+        code_(code),
+        source_type_(source_type),
+        variables_(variables),
+        metadata_(metadata) {}
+
+  PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) final {
+    if (name == "get_source") {
+      return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = this->code_; });
+    } else if (name == "get_source_type") {
+      return PackedFunc(
+          [sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = this->source_type_; });
+    } else if (name == "get_symbol") {
+      return PackedFunc(
+          [sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = this->func_symbol_; });
+    } else if (name == "get_vars") {
+      return PackedFunc(
+          [sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = this->variables_; });
+    } else if (name == "get_metadata") {
+      return PackedFunc(

Review comment:
       What is the relation between this class and the ModuleInitWrapper. It seems to be that at leastw can create a MetaDataModule without tying to CSourceModule(e.g. use it for LLVM target as well)

##########
File path: python/tvm/target/source_module.py
##########
@@ -0,0 +1,66 @@
+# License .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.
+# pylint: disable=no-else-return, unidiomatic-typecheck, undefined-variable, invalid-name, redefined-builtin
+"""
+Helper functions and classes for handling source and metdata.
+"""
+from tvm.runtime import _ffi_api
+
+class SourceMetadataModule:
+    """The module used to wrap both source and metadata."""

Review comment:
       Let us not expose it to the python side for now, as it is totally fine to get things from the mod itself. The only reason why have the graph runtime wrapper is that it is too commonly used




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