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



##########
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:
       MetadataModule 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:
[email protected]


Reply via email to