manupa-arm commented on a change in pull request #6917:
URL: https://github.com/apache/incubator-tvm/pull/6917#discussion_r526805665



##########
File path: src/target/llvm/codegen_params.cc
##########
@@ -0,0 +1,151 @@
+/*
+ * 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 codegen_params.cc
+ */
+#ifdef TVM_LLVM_VERSION
+
+#include "codegen_params.h"
+
+#include <memory>
+#include <vector>
+
+namespace tvm {
+namespace codegen {
+
+namespace {
+class DLManagedTensorDeleter {
+ public:
+  void operator()(DLManagedTensor* ptr) { ptr->deleter(ptr); }
+};
+}  // namespace
+
+llvm::ConstantArray* NDArrayToLLVMArray(llvm::LLVMContext* ctx, 
::tvm::runtime::NDArray arr) {
+  llvm::Type* element_type = nullptr;
+
+  auto arr_type = arr.DataType();
+  CHECK_EQ(arr_type.lanes(), 1) << "CodegenParams: only support generating 
1-lane parameters; saw "
+                                << arr_type.lanes();
+
+  auto shape = arr.Shape();
+  int num_elements = 1;
+  for (auto shape_elem : shape) {
+    num_elements *= shape_elem;
+  }
+
+  std::unique_ptr<DLManagedTensor, DLManagedTensorDeleter> 
tensor(arr.ToDLPack());
+  std::vector<llvm::Constant*> elements;
+
+  switch (arr_type.code()) {
+    case runtime::DataType::kInt:
+      CHECK(arr_type.bits() == 8 || arr_type.bits() == 16 || arr_type.bits() 
== 32 ||
+            arr_type.bits() == 64)
+          << "CodegenParams: only support generating 8-, 16-, 32-, or 64-bit 
integer params; saw "
+          << arr_type.bits() << "-bit array";
+      element_type = llvm::Type::getIntNTy(*ctx, arr_type.bits());
+
+      if (arr_type.bits() == 8) {
+        int8_t* data_buf = static_cast<int8_t*>(tensor->dl_tensor.data);

Review comment:
       [Style] any reason int8_t is treated in a different way? 
   If not, I would suggest we could create a templated function and cut down 
code repetition here

##########
File path: src/target/llvm/codegen_params.cc
##########
@@ -0,0 +1,151 @@
+/*
+ * 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 codegen_params.cc
+ */
+#ifdef TVM_LLVM_VERSION
+
+#include "codegen_params.h"
+
+#include <memory>
+#include <vector>
+
+namespace tvm {
+namespace codegen {
+
+namespace {
+class DLManagedTensorDeleter {
+ public:
+  void operator()(DLManagedTensor* ptr) { ptr->deleter(ptr); }
+};
+}  // namespace
+
+llvm::ConstantArray* NDArrayToLLVMArray(llvm::LLVMContext* ctx, 
::tvm::runtime::NDArray arr) {
+  llvm::Type* element_type = nullptr;
+
+  auto arr_type = arr.DataType();
+  CHECK_EQ(arr_type.lanes(), 1) << "CodegenParams: only support generating 
1-lane parameters; saw "
+                                << arr_type.lanes();
+
+  auto shape = arr.Shape();
+  int num_elements = 1;
+  for (auto shape_elem : shape) {
+    num_elements *= shape_elem;
+  }
+
+  std::unique_ptr<DLManagedTensor, DLManagedTensorDeleter> 
tensor(arr.ToDLPack());
+  std::vector<llvm::Constant*> elements;
+
+  switch (arr_type.code()) {
+    case runtime::DataType::kInt:
+      CHECK(arr_type.bits() == 8 || arr_type.bits() == 16 || arr_type.bits() 
== 32 ||
+            arr_type.bits() == 64)
+          << "CodegenParams: only support generating 8-, 16-, 32-, or 64-bit 
integer params; saw "
+          << arr_type.bits() << "-bit array";
+      element_type = llvm::Type::getIntNTy(*ctx, arr_type.bits());
+
+      if (arr_type.bits() == 8) {
+        int8_t* data_buf = static_cast<int8_t*>(tensor->dl_tensor.data);
+        for (int i = 0; i < num_elements; i++) {
+          elements.emplace_back(llvm::ConstantInt::getSigned(element_type, 
data_buf[i]));
+        }
+      } else if (arr_type.bits() == 16) {
+        for (int i = 0; i < num_elements; i++) {
+          elements.emplace_back(llvm::ConstantInt::getSigned(
+              element_type, 
reinterpret_cast<int16_t*>(tensor->dl_tensor.data)[i]));
+        }
+      } else if (arr_type.bits() == 32) {
+        for (int i = 0; i < num_elements; i++) {
+          elements.emplace_back(llvm::ConstantInt::getSigned(
+              element_type, 
reinterpret_cast<int32_t*>(tensor->dl_tensor.data)[i]));
+        }
+      } else if (arr_type.bits() == 64) {
+        for (int i = 0; i < num_elements; i++) {
+          elements.emplace_back(llvm::ConstantInt::getSigned(
+              element_type, 
reinterpret_cast<int64_t*>(tensor->dl_tensor.data)[i]));
+        }
+      } else {
+        CHECK(false) << "should not get here";
+      }
+      break;
+
+    case runtime::DataType::TypeCode::kUInt:
+      CHECK(arr_type.bits() == 8 || arr_type.bits() == 16 || arr_type.bits() 
== 32 ||
+            arr_type.bits() == 64)
+          << "CodegenParams: only support generating 8-, 16-, 32-, or 64-bit 
integer params; saw "
+          << arr_type.bits() << "-bit array";
+      element_type = llvm::Type::getIntNTy(*ctx, arr_type.bits());
+
+      if (arr_type.bits() == 8) {
+        for (int i = 0; i < num_elements; i++) {
+          elements.emplace_back(llvm::ConstantInt::get(
+              element_type, 
reinterpret_cast<int8_t*>(tensor->dl_tensor.data)[i]));
+        }
+      } else if (arr_type.bits() == 16) {
+        for (int i = 0; i < num_elements; i++) {
+          elements.emplace_back(llvm::ConstantInt::get(
+              element_type, 
reinterpret_cast<int16_t*>(tensor->dl_tensor.data)[i]));
+        }
+      } else if (arr_type.bits() == 32) {
+        for (int i = 0; i < num_elements; i++) {
+          elements.emplace_back(llvm::ConstantInt::get(
+              element_type, 
reinterpret_cast<int32_t*>(tensor->dl_tensor.data)[i]));
+        }
+      } else if (arr_type.bits() == 64) {
+        for (int i = 0; i < num_elements; i++) {
+          elements.emplace_back(llvm::ConstantInt::get(
+              element_type, 
reinterpret_cast<int64_t*>(tensor->dl_tensor.data)[i]));
+        }
+      } else {
+        CHECK(false) << "should not get here";
+      }
+      break;
+
+    case runtime::DataType::TypeCode::kFloat:
+      if (arr_type.bits() == 32) {
+        element_type = llvm::Type::getFloatTy(*ctx);

Review comment:
       Same comment as above

##########
File path: python/tvm/contrib/binutils.py
##########
@@ -16,61 +16,10 @@
 # under the License.
 
 """Utilities for binary file manipulation"""
-import os
+import logging
 import subprocess
-import tvm._ffi
-from . import utils
 
-# TODO does this file still belong in `contrib`. is it too µTVM-specific?
-
-# TODO shouldn't need so many `ALIGN` directives
-RELOCATION_LD_SCRIPT_TEMPLATE = """
-/* linker symbol for use in UTVMInit */

Review comment:
       Is this change in the scope of the PR/RFC ? if not suggest we break it 
out to another PR.

##########
File path: include/tvm/runtime/crt/graph_runtime.h
##########
@@ -61,14 +61,20 @@ typedef struct TVMGraphRuntime TVMGraphRuntime;
  * \brief Allocate a new GraphRuntime with vmalloc and initialize it.
  *
  * \param sym_json JSON-encoded graph.
- * \param m TVM Module that exposes the functions to call.
+ * \param module_handle TVM Module that exposes the functions to call.
  * \param ctxs runtime execution context.
  */
-TVMGraphRuntime* TVMGraphRuntime_Create(const char* sym_json, const struct 
TVMModule* m,
+TVMGraphRuntime* TVMGraphRuntime_Create(const char* sym_json, TVMModuleHandle 
module_handle,
                                         const TVMContext* ctxs);
 
 int TVMGraphRuntime_GetInputIndex(TVMGraphRuntime* runtime, const char* name);
 
+/*!
+ * \brief get number of input tensors allocated.
+ * \return integer number of tensors available to use.
+ */
+int TVMGraphRuntime_GetNumInputs();

Review comment:
       Maybe its just me, but I cant see how this is relevant to linked params ?

##########
File path: include/tvm/runtime/crt/platform.h
##########
@@ -39,6 +41,21 @@ extern "C" {
  */
 void __attribute__((noreturn)) TVMPlatformAbort(tvm_crt_error_t code);
 
+/*! \brief Called by the microTVM RPC server to implement TVMLogf.

Review comment:
       Is this change relevant to the PR/RFC? If not suggest break it out to a 
different PR.

##########
File path: src/relay/backend/graph_runtime_codegen.cc
##########
@@ -56,7 +56,7 @@ struct LoweredOutput {
   std::string graph_json;
   Map<String, IRModule> lowered_funcs;
   Array<tvm::runtime::Module> external_mods;
-  std::unordered_map<std::string, tvm::runtime::NDArray> params;
+  std::unordered_map<std::string, std::pair<int, const tvm::runtime::NDArray>> 
params;

Review comment:
       This change will affect UpdateConstants() in line 379. Either we need to 
modify the UpdateConstants function or have something similiar to 
param_storage_ids in LoweredOutput separately 

##########
File path: src/target/llvm/codegen_params.cc
##########
@@ -0,0 +1,151 @@
+/*
+ * 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 codegen_params.cc
+ */
+#ifdef TVM_LLVM_VERSION
+
+#include "codegen_params.h"
+
+#include <memory>
+#include <vector>
+
+namespace tvm {
+namespace codegen {
+
+namespace {
+class DLManagedTensorDeleter {
+ public:
+  void operator()(DLManagedTensor* ptr) { ptr->deleter(ptr); }
+};
+}  // namespace
+
+llvm::ConstantArray* NDArrayToLLVMArray(llvm::LLVMContext* ctx, 
::tvm::runtime::NDArray arr) {
+  llvm::Type* element_type = nullptr;
+
+  auto arr_type = arr.DataType();
+  CHECK_EQ(arr_type.lanes(), 1) << "CodegenParams: only support generating 
1-lane parameters; saw "
+                                << arr_type.lanes();
+
+  auto shape = arr.Shape();
+  int num_elements = 1;
+  for (auto shape_elem : shape) {
+    num_elements *= shape_elem;
+  }
+
+  std::unique_ptr<DLManagedTensor, DLManagedTensorDeleter> 
tensor(arr.ToDLPack());
+  std::vector<llvm::Constant*> elements;
+
+  switch (arr_type.code()) {
+    case runtime::DataType::kInt:
+      CHECK(arr_type.bits() == 8 || arr_type.bits() == 16 || arr_type.bits() 
== 32 ||
+            arr_type.bits() == 64)
+          << "CodegenParams: only support generating 8-, 16-, 32-, or 64-bit 
integer params; saw "
+          << arr_type.bits() << "-bit array";
+      element_type = llvm::Type::getIntNTy(*ctx, arr_type.bits());
+
+      if (arr_type.bits() == 8) {
+        int8_t* data_buf = static_cast<int8_t*>(tensor->dl_tensor.data);
+        for (int i = 0; i < num_elements; i++) {
+          elements.emplace_back(llvm::ConstantInt::getSigned(element_type, 
data_buf[i]));
+        }
+      } else if (arr_type.bits() == 16) {
+        for (int i = 0; i < num_elements; i++) {
+          elements.emplace_back(llvm::ConstantInt::getSigned(
+              element_type, 
reinterpret_cast<int16_t*>(tensor->dl_tensor.data)[i]));
+        }
+      } else if (arr_type.bits() == 32) {
+        for (int i = 0; i < num_elements; i++) {
+          elements.emplace_back(llvm::ConstantInt::getSigned(
+              element_type, 
reinterpret_cast<int32_t*>(tensor->dl_tensor.data)[i]));
+        }
+      } else if (arr_type.bits() == 64) {
+        for (int i = 0; i < num_elements; i++) {
+          elements.emplace_back(llvm::ConstantInt::getSigned(
+              element_type, 
reinterpret_cast<int64_t*>(tensor->dl_tensor.data)[i]));
+        }
+      } else {
+        CHECK(false) << "should not get here";
+      }
+      break;
+
+    case runtime::DataType::TypeCode::kUInt:
+      CHECK(arr_type.bits() == 8 || arr_type.bits() == 16 || arr_type.bits() 
== 32 ||

Review comment:
       Same comment as above.

##########
File path: include/tvm/runtime/crt/graph_runtime.h
##########
@@ -77,6 +83,12 @@ int TVMGraphRuntime_GetInputIndex(TVMGraphRuntime* runtime, 
const char* name);
  */
 void TVMGraphRuntime_SetInput(TVMGraphRuntime* runtime, const char* name, 
DLTensor* data_in);
 
+/*!
+ * \brief get number of output tensors allocated.
+ * \return integer number of output tensors allocated.
+ */
+int TVMGraphRuntime_GetNumOutputs();

Review comment:
       Maybe its just me, but I cant see how this is relevant to linked params ?




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to