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/11/22 20:15:39 UTC

[GitHub] [incubator-tvm] areusch commented on a change in pull request #6917: Add Relay option to link parameters into runtime Modules

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



##########
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:
       not in this one. I had originally kept them handwritten separately while developing in case more things needed to be changed, and for speed. I agree, template is more maintanable here. done.

##########
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:
       done

##########
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:
       done

##########
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:
       broken out




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