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

2020-11-24 Thread GitBox


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



##
File path: src/target/source/codegen_params.cc
##
@@ -0,0 +1,218 @@
+/*
+ * 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
+ */
+
+#include "codegen_params.h"
+
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+namespace tvm {
+namespace codegen {
+
+namespace {
+class DLManagedTensorDeleter {
+ public:
+  void operator()(DLManagedTensor* ptr) { ptr->deleter(ptr); }
+};
+}  // namespace
+
+static constexpr const int kMaxLineLength = 80;
+
+template ::value>>
+void PrintArray(void* data, size_t num_elements, int elements_per_row, 
std::string indent_str,
+std::ostream& os) {
+  for (size_t i = 0; i < num_elements; i++) {
+int64_t elem = static_cast(data)[i];
+if (std::is_signed::value) {
+  uint64_t to_print;
+  if (elem < 0) {
+os << "-";
+to_print = -elem;
+  } else {
+os << "+";
+to_print = elem;
+  }
+  os << "0x" << std::setw(sizeof(T) * 8 / 4) << 
static_cast(to_print);
+} else {
+  os << "0x" << std::setw(sizeof(T) * 8 / 4) << 
static_cast(elem);
+}
+if (i < num_elements - 1) {
+  os << ", ";
+}
+if (((i + 1) % elements_per_row) == 0) {
+  os << "\n" << indent_str;
+}
+  }
+}
+
+template ::value>>
+void PrintArray(void* data, size_t num_elements, int one_element_size_bytes, 
int elements_per_row,
+std::string indent_str, std::ostream& os) {
+  std::stringstream ss;
+  if (std::is_signed::value) {
+ss.setf(std::ios::hex | std::ios::showbase | std::ios::fixed | 
std::ios::scientific,
+std::ios::basefield | std::ios::showbase | std::ios::floatfield);
+  } else {
+ss.setf(std::ios::hex | std::ios::fixed | std::ios::scientific,
+std::ios::basefield | std::ios::showbase | std::ios::floatfield);
+  }
+  for (size_t i = 0; i < num_elements; i++) {
+T elem = static_cast(data)[i];
+if (std::isinf(elem)) {
+  // C99 standard.
+  os << (elem < 0 ? "-" : " ") << std::setw(one_element_size_bytes - 1) << 
"INFINITY";
+} else if (std::isnan(elem)) {
+  // GNU extension, implemenatation-dependent.
+  os << std::setw(one_element_size_bytes) << "NAN";
+} else {
+  ss << elem;
+  os << std::setw(one_element_size_bytes) << ss.str();
+  ss.str("");
+}
+if (i < num_elements - 1) {
+  os << ", ";
+}
+if (((i + 1) % elements_per_row) == 0) {
+  os << "\n" << indent_str;
+}
+  }
+}
+
+void NDArrayDataToC(::tvm::runtime::NDArray arr, int indent_chars, 
std::ostream& os) {

Review comment:
   [Clarification] Can we use a raw binary string (e.g. "\xeb\x2a) to 
represent the content here ? Do we need to show the element wise break down? I 
would reckon DLTensors would anyway take it as a void* right ?





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




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

2020-11-24 Thread GitBox


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



##
File path: src/target/source/codegen_params.cc
##
@@ -0,0 +1,218 @@
+/*
+ * 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
+ */
+
+#include "codegen_params.h"
+
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+namespace tvm {
+namespace codegen {
+
+namespace {
+class DLManagedTensorDeleter {
+ public:
+  void operator()(DLManagedTensor* ptr) { ptr->deleter(ptr); }
+};
+}  // namespace
+
+static constexpr const int kMaxLineLength = 80;
+
+template ::value>>
+void PrintArray(void* data, size_t num_elements, int elements_per_row, 
std::string indent_str,
+std::ostream& os) {
+  for (size_t i = 0; i < num_elements; i++) {
+int64_t elem = static_cast(data)[i];
+if (std::is_signed::value) {
+  uint64_t to_print;
+  if (elem < 0) {
+os << "-";
+to_print = -elem;
+  } else {
+os << "+";
+to_print = elem;
+  }
+  os << "0x" << std::setw(sizeof(T) * 8 / 4) << 
static_cast(to_print);
+} else {
+  os << "0x" << std::setw(sizeof(T) * 8 / 4) << 
static_cast(elem);
+}
+if (i < num_elements - 1) {
+  os << ", ";
+}
+if (((i + 1) % elements_per_row) == 0) {
+  os << "\n" << indent_str;
+}
+  }
+}
+
+template ::value>>
+void PrintArray(void* data, size_t num_elements, int one_element_size_bytes, 
int elements_per_row,
+std::string indent_str, std::ostream& os) {
+  std::stringstream ss;
+  if (std::is_signed::value) {
+ss.setf(std::ios::hex | std::ios::showbase | std::ios::fixed | 
std::ios::scientific,
+std::ios::basefield | std::ios::showbase | std::ios::floatfield);
+  } else {
+ss.setf(std::ios::hex | std::ios::fixed | std::ios::scientific,
+std::ios::basefield | std::ios::showbase | std::ios::floatfield);
+  }
+  for (size_t i = 0; i < num_elements; i++) {
+T elem = static_cast(data)[i];
+if (std::isinf(elem)) {
+  // C99 standard.
+  os << (elem < 0 ? "-" : " ") << std::setw(one_element_size_bytes - 1) << 
"INFINITY";
+} else if (std::isnan(elem)) {
+  // GNU extension, implemenatation-dependent.
+  os << std::setw(one_element_size_bytes) << "NAN";
+} else {
+  ss << elem;
+  os << std::setw(one_element_size_bytes) << ss.str();
+  ss.str("");
+}
+if (i < num_elements - 1) {
+  os << ", ";
+}
+if (((i + 1) % elements_per_row) == 0) {
+  os << "\n" << indent_str;
+}
+  }
+}
+
+void NDArrayDataToC(::tvm::runtime::NDArray arr, int indent_chars, 
std::ostream& os) {

Review comment:
   Do we need to have this ASCII encoded as such ? Im thinking of why this 
cant be a raw binary string and get rid of "0x", commas and brackets. IIUC, the 
DLTensor just needs a void* and would be casted to appropriate data type in the 
function in which it consumes the constant data.
   
   If the reasoning is for debugging then probably we should have a debug flag 
and do this if required and default could be a raw binary string (if thats 
possible), I think it will save compilation time.





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




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

2020-11-24 Thread GitBox


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



##
File path: src/target/source/codegen_c_host.cc
##
@@ -57,6 +60,48 @@ void CodeGenCHost::AddFunction(const PrimFunc& f) {
   CodeGenC::AddFunction(f);
 }
 
+void CodeGenCHost::LinkParameters(Map params) {
+  PrintFuncPrefix();
+  stream << " " << tvm::runtime::symbol::tvm_lookup_linked_param
+ << "(void* args, int* arg_type_ids, int num_args, void* 
out_ret_value, "
+ << "int* out_ret_tcode, void* resource_handle) {\n";
+  ICHECK_EQ(GetUniqueName(tvm::runtime::symbol::tvm_lookup_linked_param),
+tvm::runtime::symbol::tvm_lookup_linked_param)
+  << "builtin PackedFunc name already taken: " << 
tvm::runtime::symbol::tvm_lookup_linked_param;
+  stream << "switch (((int64_t*) args)[0]) {\n"
+ << "default:\n"
+ << "out_ret_tcode[0] = " << kTVMNullptr << ";\n"
+ << "return 0;\n";
+
+  function_names_.emplace_back(tvm::runtime::symbol::tvm_lookup_linked_param);
+  for (auto kv : params) {
+decl_stream << "\n"
+<< "#ifdef __cplusplus\n"
+<< "extern \"C\" {\n"
+<< "#endif\n"
+<< "static const ";
+int64_t num_elements = 1;
+for (int64_t dim : kv.second->param.Shape()) {
+  num_elements *= dim;
+}
+PrintType(kv.second->param.DataType(), decl_stream);
+decl_stream << " " << ::tvm::runtime::symbol::tvm_param_prefix << kv.first 
<< "["
+<< num_elements << "] = {\n";
+NDArrayDataToC(kv.second->param, 4, decl_stream);
+decl_stream << "};\n"
+<< "#ifdef __cplusplus\n"
+<< "}  // extern \"C\"\n"
+<< "#endif\n";
+stream << "case " << kv.second->id << ":\n"
+   << "((uint64_t*)out_ret_value)[0] = (uint64_t) (uintptr_t) "
+   << ::tvm::runtime::symbol::tvm_param_prefix << kv.first << ";\n"
+   << "out_ret_tcode[0] = " << kTVMOpaqueHandle << ";\n"
+   << "return 0;\n";

Review comment:
   Don't we need a default here ? prolly to give a meaningful error ? 

##
File path: src/target/source/codegen_c_host.cc
##
@@ -57,6 +60,48 @@ void CodeGenCHost::AddFunction(const PrimFunc& f) {
   CodeGenC::AddFunction(f);
 }
 
+void CodeGenCHost::LinkParameters(Map params) {
+  PrintFuncPrefix();
+  stream << " " << tvm::runtime::symbol::tvm_lookup_linked_param
+ << "(void* args, int* arg_type_ids, int num_args, void* 
out_ret_value, "
+ << "int* out_ret_tcode, void* resource_handle) {\n";
+  ICHECK_EQ(GetUniqueName(tvm::runtime::symbol::tvm_lookup_linked_param),
+tvm::runtime::symbol::tvm_lookup_linked_param)
+  << "builtin PackedFunc name already taken: " << 
tvm::runtime::symbol::tvm_lookup_linked_param;
+  stream << "switch (((int64_t*) args)[0]) {\n"
+ << "default:\n"
+ << "out_ret_tcode[0] = " << kTVMNullptr << ";\n"
+ << "return 0;\n";
+
+  function_names_.emplace_back(tvm::runtime::symbol::tvm_lookup_linked_param);
+  for (auto kv : params) {
+decl_stream << "\n"
+<< "#ifdef __cplusplus\n"
+<< "extern \"C\" {\n"
+<< "#endif\n"
+<< "static const ";
+int64_t num_elements = 1;
+for (int64_t dim : kv.second->param.Shape()) {
+  num_elements *= dim;
+}
+PrintType(kv.second->param.DataType(), decl_stream);
+decl_stream << " " << ::tvm::runtime::symbol::tvm_param_prefix << kv.first 
<< "["
+<< num_elements << "] = {\n";
+NDArrayDataToC(kv.second->param, 4, decl_stream);
+decl_stream << "};\n"
+<< "#ifdef __cplusplus\n"
+<< "}  // extern \"C\"\n"
+<< "#endif\n";
+stream << "case " << kv.second->id << ":\n"
+   << "((uint64_t*)out_ret_value)[0] = (uint64_t) (uintptr_t) "

Review comment:
   Looks like a copy is introduced (Correct me if Im wrong), which is 
something we would want to avoid if possible in memory constrained devices.

##
File path: src/target/source/codegen_params.cc
##
@@ -0,0 +1,218 @@
+/*
+ * 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 

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

2020-11-24 Thread GitBox


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



##
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:
   fine by me, then.





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




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

2020-11-19 Thread GitBox


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



##
File path: src/relay/backend/graph_runtime_codegen.cc
##
@@ -56,7 +56,7 @@ struct LoweredOutput {
   std::string graph_json;
   Map lowered_funcs;
   Array external_mods;
-  std::unordered_map params;
+  std::unordered_map> 
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.
   
   Am I missing something here @zhiics @comaniac ? The CI did not break 
anything.





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




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

2020-11-19 Thread GitBox


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



##
File path: src/relay/backend/graph_runtime_codegen.cc
##
@@ -56,7 +56,7 @@ struct LoweredOutput {
   std::string graph_json;
   Map lowered_funcs;
   Array external_mods;
-  std::unordered_map params;
+  std::unordered_map> 
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.
   
   Am I missing something here @zhiics @comaniac ? The CI did not break 
anything.





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




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

2020-11-19 Thread GitBox


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 
+#include 
+
+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 
tensor(arr.ToDLPack());
+  std::vector 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(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 
+#include 
+
+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 
tensor(arr.ToDLPack());
+  std::vector 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(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