tqchen commented on a change in pull request #4258: [WIP][TVM] Bring Your Own 
Codegen to TVM
URL: https://github.com/apache/incubator-tvm/pull/4258#discussion_r353497058
 
 

 ##########
 File path: src/relay/backend/contrib/gcc/codegen.cc
 ##########
 @@ -0,0 +1,229 @@
+/*
+ * 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.
+ */
+#include <tvm/relay/expr_functor.h>
+#include <tvm/relay/transform.h>
+#include <tvm/relay/type.h>
+#include <tvm/runtime/module.h>
+#include <tvm/runtime/object.h>
+
+#include <fstream>
+#include <sstream>
+
+#include "../contrib_codegen.h"
+
+namespace tvm {
+namespace relay {
+namespace contrib {
+
+/*!
+ * \brief An example codegen that is only used for quick prototyping and 
testing
+ * purpose. Only several binary options are covered in the GCC builder. Users
+ * may need to extend them to cover more operators.
+ */
+class GccBuilder : public ExprVisitor, public ExternSourcePrinter {
+ public:
+  explicit GccBuilder(const std::string& id) { this->subgraph_id_ = id; }
+
+  void VisitExpr_(const VarNode* node) {
+    subgraph_args_.push_back(node->name_hint());
+    out_.clear();
+    out_.push_back({node->name_hint(), 0});
+  }
+
+  void VisitExpr_(const CallNode* call) final {
+    std::ostringstream macro_stream;
+    std::ostringstream decl_stream;
+    std::ostringstream buf_stream;
+
+    auto op_node = call->op.as<OpNode>();
+    std::string func_name = subgraph_id_ + "_" + std::to_string(func_idx++);
+
+    // Make function declaration
+    macro_stream << "GCC_BINARY_OP_" << call->args.size() << "D(" << func_name 
<< ", ";
+
+    if (GetRef<Op>(op_node) == Op::Get("add")) {
+      macro_stream << "+";
+    } else if (GetRef<Op>(op_node) == Op::Get("subtract")) {
+      macro_stream << "-";
+    } else if (GetRef<Op>(op_node) == Op::Get("multiply")) {
+      macro_stream << "*";
+    } else {
+      LOG(FATAL) << "Unrecognized op";
+    }
+
+    auto in_shape = GetShape(call->args[0]->checked_type());
+    for (size_t i = 0; i < in_shape.size(); ++i) {
+      macro_stream << ", " << in_shape[i];
+    }
+    macro_stream << ");";
+    func_decl_.push_back(macro_stream.str());
+
+    // Make function call when visiting arguments
+    bool first = true;
+    decl_stream << func_name << "(";
+    for (size_t i = 0; i < call->args.size(); ++i) {
+      VisitExpr(call->args[i]);
+      for (auto out : out_) {
+        if (!first) {
+          decl_stream << ", ";
+        }
+        first = false;
+        decl_stream << out.first;
+      }
+    }
+
+    auto type_node = call->checked_type().as<TensorTypeNode>();
+    CHECK(type_node != nullptr && runtime::TypeMatch(type_node->dtype, 
kDLFloat, 32))
+        << "Only support single output tensor with float type";
+    std::string out = "buf_" + std::to_string(buf_idx_++);
+    auto out_shape = GetShape(call->checked_type());
+    int out_size = 1;
+    for (size_t i = 0; i < out_shape.size(); ++i) {
+      out_size *= out_shape[i];
+    }
+    buf_stream << "float* " << out << " = (float*)std::malloc(4 * " << 
out_size << ");";
+    buf_decl_.push_back(buf_stream.str());
+
+    decl_stream << ", " << out << ");";
+    subgraph_body.push_back(decl_stream.str());
+
+    // Update output buffer
+    out_.clear();
+    out_.push_back({out, out_size});
+  }
+
+  /*!
+   * \brief Emit the source code that invokes gcc compatible wrappers.
+   *
+   * \return The emitted code.
+   */
+  std::string JIT() {
+    // Write function macros
+    for (auto decl : func_decl_) {
+      code_stream_ << decl << "\n";
+    }
+    return JitImpl(subgraph_id_, subgraph_args_, buf_decl_, subgraph_body, 
out_);
+  }
+
+ private:
+  /*! \brief The subgraph id that represents an GCC external function. */
+  std::string subgraph_id_ = "";
+  /*! \brief The index of an external function. */
+  int func_idx = 0;
+  /*! \brief The index of allocated buffers. */
+  int buf_idx_ = 0;
+  /*! \brief The arguments of a GCC compatible external function. */
+  std::vector<std::string> subgraph_args_;
+  /*! \brief The statements of a GCC compatible external function. */
+  std::vector<std::string> subgraph_body;
+  /*! \brief The declaration statements of a GCC compatible external function. 
*/
+  std::vector<std::string> func_decl_;
+  /*! \brief The declaration statements of buffers. */
+  std::vector<std::string> buf_decl_;
+  /*! \brief The name and index pairs for output. */
+  std::vector<std::pair<std::string, int>> out_;
+};
+
+class GccCodegen : public ExternCodegenBase {
 
 Review comment:
   GccCodgen Codes not necessarily makes sense, as the code backend is not gcc 
specific (works for any c compiler)

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


With regards,
Apache Git Services

Reply via email to