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 <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;
+
+template <typename T, typename = std::enable_if<std::is_integral<T>::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<T*>(data)[i];
+    if (std::is_signed<T>::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<std::uint64_t>(to_print);
+    } else {
+      os << "0x" << std::setw(sizeof(T) * 8 / 4) << 
static_cast<std::uint64_t>(elem);
+    }
+    if (i < num_elements - 1) {
+      os << ", ";
+    }
+    if (((i + 1) % elements_per_row) == 0) {
+      os << "\n" << indent_str;
+    }
+  }
+}
+
+template <typename T, typename = 
std::enable_if<std::is_floating_point<T>::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<T>::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<T*>(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:
[email protected]


Reply via email to