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/23 15:59:50 UTC

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

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



##########
File path: src/target/llvm/codegen_params.cc
##########
@@ -0,0 +1,172 @@
+/*
+ * 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
+
+template <typename T, typename E = void>
+struct LLVMConstantGetter {
+  static llvm::Constant* getElement(llvm::Type* ty, T t);
+};
+
+template <typename T>
+struct LLVMConstantGetter<
+    T, std::enable_if_t<(std::is_integral<T>::value && std::is_signed<T>::value)>> {
+  static llvm::Constant* getElement(llvm::Type* ty, T t) {
+    return llvm::ConstantInt::getSigned(ty, t);
+  }
+};
+
+template <typename T>
+struct LLVMConstantGetter<
+    T, std::enable_if_t<(std::is_integral<T>::value && !std::is_signed<T>::value)>> {
+  static llvm::Constant* getElement(llvm::Type* ty, T t) { return llvm::ConstantInt::get(ty, t); }
+};
+
+template <typename T>
+struct LLVMConstantGetter<T, std::enable_if_t<std::is_floating_point<T>::value>> {
+  static llvm::Constant* getElement(llvm::Type* ty, T t) { return llvm::ConstantFP::get(ty, t); }
+};
+
+template <typename T, typename = std::enable_if<std::is_pod<T>::value>>
+void BuildLLVMVector(llvm::Type* element_type, void* tensor_data, size_t num_elements,
+                     std::vector<llvm::Constant*>* elements) {
+  for (size_t i = 0; i < num_elements; i++) {

Review comment:
       `std::transform` perhaps?

##########
File path: src/target/source/codegen_params.cc
##########
@@ -0,0 +1,297 @@
+/*
+ * 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 <dlpack/dlpack.h>
+
+#include <cmath>
+#include <iomanip>
+#include <memory>
+#include <string>
+
+namespace tvm {
+namespace codegen {
+
+namespace {
+class DLManagedTensorDeleter {
+ public:
+  void operator()(DLManagedTensor* ptr) { ptr->deleter(ptr); }
+};
+}  // namespace
+
+static constexpr const int kMaxLineLength = 80;
+
+void NDArrayDataToC(::tvm::runtime::NDArray arr, int indent_chars, std::ostream& os) {
+  auto arr_type = arr.DataType();
+  CHECK_EQ(arr_type.lanes(), 1) << "CodegenParams: only support generating 1-lane parameters; saw "
+                                << arr_type.lanes();
+
+  int one_element_size_bytes = (arr_type.bits() / 4) + (2 /* "0x" */) + (2 /* ", " */);
+  if (arr_type.code() == runtime::DataType::TypeCode::kInt) {
+    one_element_size_bytes += 1;  // sign bit
+    if (arr_type.bits() > 32) {
+      one_element_size_bytes += 2;  // "UL"
+    }

Review comment:
       `UL` isn't guaranteed to be at least 64 bits.  On 32-bit systems it may still be 32 bits long.  You can use `ULL` instead.
   
   Also, you're checking for the type being `Int`, so it's signed.

##########
File path: src/target/source/codegen_params.cc
##########
@@ -0,0 +1,297 @@
+/*
+ * 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 <dlpack/dlpack.h>
+
+#include <cmath>
+#include <iomanip>
+#include <memory>
+#include <string>
+
+namespace tvm {
+namespace codegen {
+
+namespace {
+class DLManagedTensorDeleter {
+ public:
+  void operator()(DLManagedTensor* ptr) { ptr->deleter(ptr); }
+};
+}  // namespace
+
+static constexpr const int kMaxLineLength = 80;
+
+void NDArrayDataToC(::tvm::runtime::NDArray arr, int indent_chars, std::ostream& os) {
+  auto arr_type = arr.DataType();
+  CHECK_EQ(arr_type.lanes(), 1) << "CodegenParams: only support generating 1-lane parameters; saw "
+                                << arr_type.lanes();
+
+  int one_element_size_bytes = (arr_type.bits() / 4) + (2 /* "0x" */) + (2 /* ", " */);
+  if (arr_type.code() == runtime::DataType::TypeCode::kInt) {
+    one_element_size_bytes += 1;  // sign bit
+    if (arr_type.bits() > 32) {
+      one_element_size_bytes += 2;  // "UL"
+    }
+  } else if (arr_type.code() == runtime::DataType::TypeCode::kUInt) {
+    if (arr_type.bits() > 32) {
+      one_element_size_bytes += 1;  // "L"
+    }
+  } else if (arr_type.code() == runtime::DataType::TypeCode::kFloat) {
+    // Floats and doubles are printed as hex but casted.
+    one_element_size_bytes += 1 /* sign */ + 1 /* decimal point */ + 1 /* exponent sign */ +
+                              1 /* extra decimal digit in exponent */;
+  }
+
+  int elements_per_row = 16;
+  while (elements_per_row > 1 &&
+         (elements_per_row * one_element_size_bytes) > (kMaxLineLength - indent_chars)) {
+    elements_per_row /= 2;
+  }
+
+  std::string indent_str(indent_chars, ' ');
+  os << indent_str;
+
+  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());
+  auto old_fmtflags = os.flags();
+  os.setf(std::ios::internal | std::ios::hex,
+          std::ios::adjustfield | std::ios::basefield | std::ios::showbase);
+  os.fill('0');
+  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";
+
+      if (arr_type.bits() == 8) {

Review comment:
       You can print all 8/16/32/64-bit values via `uint64_t`.  You can use the `bits()` value as the argument to `setw`.

##########
File path: src/target/source/codegen_params.cc
##########
@@ -0,0 +1,297 @@
+/*
+ * 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 <dlpack/dlpack.h>
+
+#include <cmath>
+#include <iomanip>
+#include <memory>
+#include <string>
+
+namespace tvm {
+namespace codegen {
+
+namespace {
+class DLManagedTensorDeleter {
+ public:
+  void operator()(DLManagedTensor* ptr) { ptr->deleter(ptr); }
+};
+}  // namespace
+
+static constexpr const int kMaxLineLength = 80;
+
+void NDArrayDataToC(::tvm::runtime::NDArray arr, int indent_chars, std::ostream& os) {
+  auto arr_type = arr.DataType();
+  CHECK_EQ(arr_type.lanes(), 1) << "CodegenParams: only support generating 1-lane parameters; saw "
+                                << arr_type.lanes();
+
+  int one_element_size_bytes = (arr_type.bits() / 4) + (2 /* "0x" */) + (2 /* ", " */);
+  if (arr_type.code() == runtime::DataType::TypeCode::kInt) {
+    one_element_size_bytes += 1;  // sign bit

Review comment:
       `sign bit` -> `sign character`

##########
File path: src/target/source/codegen_params.cc
##########
@@ -0,0 +1,297 @@
+/*
+ * 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 <dlpack/dlpack.h>
+
+#include <cmath>
+#include <iomanip>
+#include <memory>
+#include <string>
+
+namespace tvm {
+namespace codegen {
+
+namespace {
+class DLManagedTensorDeleter {
+ public:
+  void operator()(DLManagedTensor* ptr) { ptr->deleter(ptr); }
+};
+}  // namespace
+
+static constexpr const int kMaxLineLength = 80;
+
+void NDArrayDataToC(::tvm::runtime::NDArray arr, int indent_chars, std::ostream& os) {
+  auto arr_type = arr.DataType();
+  CHECK_EQ(arr_type.lanes(), 1) << "CodegenParams: only support generating 1-lane parameters; saw "
+                                << arr_type.lanes();
+
+  int one_element_size_bytes = (arr_type.bits() / 4) + (2 /* "0x" */) + (2 /* ", " */);
+  if (arr_type.code() == runtime::DataType::TypeCode::kInt) {
+    one_element_size_bytes += 1;  // sign bit
+    if (arr_type.bits() > 32) {
+      one_element_size_bytes += 2;  // "UL"
+    }
+  } else if (arr_type.code() == runtime::DataType::TypeCode::kUInt) {
+    if (arr_type.bits() > 32) {
+      one_element_size_bytes += 1;  // "L"

Review comment:
       Same here, plus the type here is unsigned.

##########
File path: src/target/source/codegen_params.cc
##########
@@ -0,0 +1,297 @@
+/*
+ * 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 <dlpack/dlpack.h>
+
+#include <cmath>
+#include <iomanip>
+#include <memory>
+#include <string>
+
+namespace tvm {
+namespace codegen {
+
+namespace {
+class DLManagedTensorDeleter {
+ public:
+  void operator()(DLManagedTensor* ptr) { ptr->deleter(ptr); }
+};
+}  // namespace
+
+static constexpr const int kMaxLineLength = 80;
+
+void NDArrayDataToC(::tvm::runtime::NDArray arr, int indent_chars, std::ostream& os) {
+  auto arr_type = arr.DataType();
+  CHECK_EQ(arr_type.lanes(), 1) << "CodegenParams: only support generating 1-lane parameters; saw "
+                                << arr_type.lanes();
+
+  int one_element_size_bytes = (arr_type.bits() / 4) + (2 /* "0x" */) + (2 /* ", " */);

Review comment:
       In hexadecimal floating point, the exponent is still in decimal, only the significant is in hex.  This calculation (the `bits/4` part) isn't entirely correct for fp.

##########
File path: src/target/llvm/codegen_llvm.cc
##########
@@ -184,6 +188,90 @@ void CodeGenLLVM::AddFunctionInternal(const PrimFunc& f, bool ret_void) {
   }
 }
 
+void CodeGenLLVM::LinkParameters(const Map<String, LinkedParam> params) {
+  // It would be nice to de-dupe these declarations frm src/tir/transforms/make_packed_api.cc,
+  // but they are at a different layer in the compiler...
+  std::vector<llvm::Type*> param_types;
+  // args
+  param_types.push_back(t_void_->getPointerTo(GetGlobalAddressSpace()));
+  // tcodes
+  param_types.push_back(t_int_->getPointerTo(GetGlobalAddressSpace()));
+  // num_args
+  param_types.push_back(t_int_);
+  // ret_args
+  param_types.push_back(t_void_->getPointerTo(GetGlobalAddressSpace()));
+  // ret_tcodes
+  param_types.push_back(t_int_->getPointerTo(GetGlobalAddressSpace()));
+  // resource_handle
+  param_types.push_back(t_void_->getPointerTo(GetGlobalAddressSpace()));
+
+  llvm::FunctionType* ftype = llvm::FunctionType::get(t_int_, param_types, false);
+
+  llvm::Function* function =
+      llvm::Function::Create(ftype, llvm::Function::ExternalLinkage,
+                             ::tvm::runtime::symbol::tvm_lookup_linked_param, module_.get());
+  function->setCallingConv(llvm::CallingConv::C);
+  function->setDLLStorageClass(llvm::GlobalValue::DLLStorageClassTypes::DLLExportStorageClass);
+
+  llvm::BasicBlock* entry = llvm::BasicBlock::Create(*ctx_, "entry", function);
+  builder_->SetInsertPoint(entry);
+  std::vector<llvm::Value*> zero_index_list{llvm::ConstantInt::get(t_int32_, 0)};
+  std::vector<llvm::Value*> zero_array_index_list{llvm::ConstantInt::get(t_int32_, 0),
+                                                  llvm::ConstantInt::get(t_int32_, 0)};
+  auto args_array = builder_->CreateBitCast(
+#if TVM_LLVM_VERSION >= 50
+      &function->arg_begin()[0],
+#else
+      &(*(function->arg_begin())),
+#endif
+      llvm::ArrayType::get(t_void_->getPointerTo(GetGlobalAddressSpace()), 1));
+  llvm::Value* sid = builder_->CreateBitCast(
+      builder_->CreateLoad(t_void_->getPointerTo(GetGlobalAddressSpace()),
+                           builder_->CreateInBoundsGEP(args_array, zero_index_list)),

Review comment:
       You don't need this GEP.

##########
File path: src/target/source/codegen_params.cc
##########
@@ -0,0 +1,297 @@
+/*
+ * 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 <dlpack/dlpack.h>
+
+#include <cmath>
+#include <iomanip>
+#include <memory>
+#include <string>
+
+namespace tvm {
+namespace codegen {
+
+namespace {
+class DLManagedTensorDeleter {
+ public:
+  void operator()(DLManagedTensor* ptr) { ptr->deleter(ptr); }
+};
+}  // namespace
+
+static constexpr const int kMaxLineLength = 80;
+
+void NDArrayDataToC(::tvm::runtime::NDArray arr, int indent_chars, std::ostream& os) {
+  auto arr_type = arr.DataType();
+  CHECK_EQ(arr_type.lanes(), 1) << "CodegenParams: only support generating 1-lane parameters; saw "
+                                << arr_type.lanes();
+
+  int one_element_size_bytes = (arr_type.bits() / 4) + (2 /* "0x" */) + (2 /* ", " */);
+  if (arr_type.code() == runtime::DataType::TypeCode::kInt) {
+    one_element_size_bytes += 1;  // sign bit
+    if (arr_type.bits() > 32) {
+      one_element_size_bytes += 2;  // "UL"
+    }
+  } else if (arr_type.code() == runtime::DataType::TypeCode::kUInt) {
+    if (arr_type.bits() > 32) {
+      one_element_size_bytes += 1;  // "L"
+    }
+  } else if (arr_type.code() == runtime::DataType::TypeCode::kFloat) {
+    // Floats and doubles are printed as hex but casted.
+    one_element_size_bytes += 1 /* sign */ + 1 /* decimal point */ + 1 /* exponent sign */ +
+                              1 /* extra decimal digit in exponent */;
+  }
+
+  int elements_per_row = 16;
+  while (elements_per_row > 1 &&
+         (elements_per_row * one_element_size_bytes) > (kMaxLineLength - indent_chars)) {
+    elements_per_row /= 2;
+  }
+
+  std::string indent_str(indent_chars, ' ');
+  os << indent_str;
+
+  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());
+  auto old_fmtflags = os.flags();
+  os.setf(std::ios::internal | std::ios::hex,
+          std::ios::adjustfield | std::ios::basefield | std::ios::showbase);
+  os.fill('0');
+  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";
+
+      if (arr_type.bits() == 8) {
+        for (int i = 0; i < num_elements; i++) {
+          // NOTE: for special types int8_t and uint8_t, need to promote to int type to avoid
+          // printing as a char.
+          int8_t elem = static_cast<int8_t*>(tensor->dl_tensor.data)[i];
+          uint16_t to_print;
+          if (elem < 0) {
+            os << "-";
+            to_print = -elem;
+          } else {
+            os << "+";
+            to_print = elem;
+          }
+          os << "0x" << std::setw(2) << +static_cast<std::uint8_t>(to_print);
+          if (i < num_elements - 1) {
+            os << ", ";
+          }
+          if (((i + 1) % elements_per_row) == 0) {
+            os << "\n" << indent_str;
+          }
+        }
+      } else if (arr_type.bits() == 16) {
+        for (int i = 0; i < num_elements; i++) {
+          int16_t elem = static_cast<int16_t*>(tensor->dl_tensor.data)[i];
+          uint16_t to_print;
+          if (elem < 0) {
+            os << "-";
+            to_print = -elem;
+          } else {
+            os << "+";
+            to_print = elem;
+          }
+          os << "0x" << std::setw(4) << to_print;
+          if (i < num_elements - 1) {
+            os << ", ";
+          }
+          if (((i + 1) % elements_per_row) == 0) {
+            os << "\n" << indent_str;
+          }
+        }
+      } else if (arr_type.bits() == 32) {
+        for (int i = 0; i < num_elements; i++) {
+          int32_t elem = static_cast<int32_t*>(tensor->dl_tensor.data)[i];
+          uint32_t to_print;
+          if (elem < 0) {
+            os << "-";
+            to_print = -elem;
+          } else {
+            os << "+";
+            to_print = elem;
+          }
+          os << "0x" << std::setw(8) << to_print;
+          if (i < num_elements - 1) {
+            os << ", ";
+          }
+          if (((i + 1) % elements_per_row) == 0) {
+            os << "\n" << indent_str;
+          }
+        }
+      } else if (arr_type.bits() == 64) {
+        for (int i = 0; i < num_elements; i++) {
+          int64_t elem = static_cast<int64_t*>(tensor->dl_tensor.data)[i];
+          uint64_t to_print;
+          if (elem < 0) {
+            os << "-";
+            to_print = -elem;
+          } else {
+            os << "+";
+            to_print = elem;
+          }
+          os << "0x" << std::setw(16) << to_print;
+          if (i < num_elements - 1) {
+            os << ", ";
+          }
+          if (((i + 1) % elements_per_row) == 0) {
+            os << "\n" << indent_str;
+          }
+        }
+      } 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";
+
+      if (arr_type.bits() == 8) {
+        for (int i = 0; i < num_elements; i++) {
+          // NOTE: for special types int8_t and uint8_t, need to promote to int type to avoid
+          // printing as a char.
+          os << "0x" << std::setw(2)
+             << +static_cast<std::uint8_t>(static_cast<uint8_t*>(tensor->dl_tensor.data)[i]);
+          if (i < num_elements - 1) {
+            os << ", ";
+          }
+          if (((i + 1) % elements_per_row) == 0) {
+            os << "\n" << indent_str;
+          }
+        }
+      } else if (arr_type.bits() == 16) {
+        for (int i = 0; i < num_elements; i++) {
+          os << "0x" << std::setw(4) << static_cast<uint16_t*>(tensor->dl_tensor.data)[i];
+          if (i < num_elements - 1) {
+            os << ", ";
+          }
+          if (((i + 1) % elements_per_row) == 0) {
+            os << "\n" << indent_str;
+          }
+        }
+      } else if (arr_type.bits() == 32) {
+        for (int i = 0; i < num_elements; i++) {
+          os << "0x" << std::setw(8) << static_cast<uint32_t*>(tensor->dl_tensor.data)[i];
+          if (i < num_elements - 1) {
+            os << ", ";
+          }
+          if (((i + 1) % elements_per_row) == 0) {
+            os << "\n" << indent_str;
+          }
+        }
+      } else if (arr_type.bits() == 64) {
+        for (int i = 0; i < num_elements; i++) {
+          os << "0x" << std::setw(16) << static_cast<uint64_t*>(tensor->dl_tensor.data)[i] << "UL";
+          if (i < num_elements - 1) {
+            os << ", ";
+          }
+          if (((i + 1) % elements_per_row) == 0) {
+            os << "\n" << indent_str;
+          }
+        }
+      } else {
+        CHECK(false) << "should not get here";
+      }
+      break;
+
+    case runtime::DataType::TypeCode::kFloat: {
+      std::stringstream ss;
+      ss.setf(std::ios::hex | std::ios::showbase | std::ios::fixed | std::ios::scientific,
+              std::ios::basefield | std::ios::showbase | std::ios::floatfield);

Review comment:
       `std::showbase` is there twice.




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