ptrendx commented on a change in pull request #15167: [WIP] Pointwise fusion 
for GPU
URL: https://github.com/apache/incubator-mxnet/pull/15167#discussion_r295553056
 
 

 ##########
 File path: src/operator/fusion/fused_op.cu
 ##########
 @@ -0,0 +1,396 @@
+/*
+ * 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 <nvrtc.h>
+#include <cuda.h>
+#include <nnvm/pass_functions.h>
+#include <algorithm>
+#include <mutex>
+#include "./fused_op.h"
+#include "./fused_op-inl.h"
+#include "../operator_common.h"
+#include "../elemwise_op_common.h"
+#include "../../executor/exec_pass.h"
+#include "../../common/cuda_utils.h"
+
+namespace mxnet {
+
+namespace detail {
+
+inline std::string mshadowTypeToString(int type) {
+  switch (type) {
+    case mshadow::kFloat32:
+      return "float";
+    case mshadow::kFloat64:
+      return "double";
+    case mshadow::kFloat16:
+      return "half";
+    case mshadow::kUint8:
+      return "unsigned char";
+    case mshadow::kInt8:
+      return "char";
+    case mshadow::kInt32:
+      return "int";
+    case mshadow::kInt64:
+      return "long long";
+    default:
+      LOG(FATAL) << "Unknown type enum " << type;
+  }
+  return "";
+}
+
+}  // namespace detail
+
+void FusedOp::GenerateCode(const std::vector<OpReqType> &req) {
+  const auto& g = this->symbol_.indexed_graph();
+  std::string code = "";
+  int temp_name_counter = 0;
+  using NodeEntry = nnvm::IndexedGraph::NodeEntry;
+  std::map<std::pair<int, int>, std::string> variables;
+
+  std::vector<uint32_t> outputs(g.num_nodes());
+
+  for (size_t i = 0; i < g.num_nodes(); ++i) {
+    const auto& node = g[i];
+    if (node.source != nullptr) {
+      outputs[i] = node.source->num_outputs();
+    } else {
+      outputs[i] = 0;
+    }
+  }
+
+  for (size_t i = 0; i < g.num_nodes(); ++i) {
+    const auto& node = g[i];
+    const auto* source = node.source;
+    if (source != nullptr) {
+      std::string var_name = "temp" + std::to_string(temp_name_counter++);
+      if (source->is_variable()) {
+        code += "const auto " + var_name + " = load(" + source->attrs.name + 
", i);\n";
+        CHECK_EQ(outputs[i], 1);
+        variables[{i, 0}] = var_name;
+      } else {
+        std::string op_name = source->op()->name;
+        if (detail::fused_op_binary_ops.find(op_name) != 
detail::fused_op_binary_ops.end()) {
+          std::string op = detail::fused_op_binary_ops.at(op_name);
+          const auto& arg1 = variables[{node.inputs[0].node_id, 
node.inputs[0].index}];
+          const auto& arg2 = variables[{node.inputs[1].node_id, 
node.inputs[1].index}];
+          code += "const auto " + var_name + " = " + op +
+                  "(" + arg1 + ", " + arg2 + ");\n";
+          CHECK_EQ(outputs[i], 1);
+          variables[{i, 0}] = var_name;
+          continue;
+        }
+
+        if (detail::fused_op_unary_ops.find(op_name) != 
detail::fused_op_unary_ops.end()) {
+          std::string op = detail::fused_op_unary_ops.at(op_name);
+          const auto& arg1 = variables[{node.inputs[0].node_id, 
node.inputs[0].index}];
+          code += "const auto " + var_name + " = " + op +
+                  "(" + arg1 + ");\n";
+          CHECK_EQ(outputs[i], 1);
+          variables[{i, 0}] = var_name;
+          continue;
+        }
+
+        if (detail::fused_op_special_ops.find(op_name) != 
detail::fused_op_special_ops.end()) {
+          const std::vector<std::string>& op_desc = 
detail::fused_op_special_ops.at(op_name);
+          std::string fmt = op_desc[0];
+          for (size_t j = 1; j < op_desc.size(); ++j) {
+            const std::string& desc = op_desc[j];
+            std::string sub;
+            if (desc[0] == '_') {
+              // Argument
+              int arg_id = std::stoi(desc.substr(1));
+              sub = variables[{node.inputs[arg_id].node_id, 
node.inputs[arg_id].index}];
+            } else {
+              sub = source->attrs.dict.at(desc);
+            }
+            size_t pos = fmt.find("%");
+            CHECK_NE(pos, std::string::npos);
+            fmt.replace(pos, 1, sub);
+          }
+          code += "const auto " + var_name + " = " + fmt + ";\n";
+          CHECK_EQ(outputs[i], 1);
+          variables[{i, 0}] = var_name;
+          continue;
+        }
+
+        if (detail::fused_op_mimo_ops.find(op_name) != 
detail::fused_op_mimo_ops.end()) {
+          const std::vector<std::vector<std::string>>& op_descs =
+            detail::fused_op_mimo_ops.at(op_name);
+          CHECK_EQ(outputs[i], op_descs.size());
+          size_t count = 0;
+          for (const auto& op_desc : op_descs) {
+            var_name = "temp" + std::to_string(temp_name_counter++);
+            std::string fmt = op_desc[0];
+            for (size_t j = 1; j < op_desc.size(); ++j) {
+              const std::string& desc = op_desc[j];
+              std::string sub;
+              if (desc[0] == '_') {
+                // Argument
+                int arg_id = std::stoi(desc.substr(1));
+                sub = variables[{node.inputs[arg_id].node_id, 
node.inputs[arg_id].index}];
+              } else {
+                sub = source->attrs.dict.at(desc);
+              }
+              size_t pos = fmt.find("%");
+              CHECK_NE(pos, std::string::npos);
+              fmt.replace(pos, 1, sub);
+            }
+            code += "const auto " + var_name + " = " + fmt + ";\n";
+            variables[{i, count}] = var_name;
+            ++count;
+          }
+          continue;
+        }
+
+        // Special cases with variable number
+        // of inputs/outputs, listed in
+        // detail::fused_op_variable_io_ops
+        if (op_name == "add_n") {
+          CHECK_EQ(outputs[i], 1);
+          const auto& arg = variables[{node.inputs[0].node_id, 
node.inputs[0].index}];
+          code += "auto " + var_name + " = " + arg + ";\n";
+          for (size_t inp = 1; inp < node.inputs.size(); ++inp) {
+            const auto& temp_arg = variables[{node.inputs[inp].node_id, 
node.inputs[inp].index}];
+            code += var_name + " = add(" + var_name + ", " + temp_arg + ");\n";
+          }
+          variables[{i, 0}] = var_name;
+          continue;
+        }
+
+        if (op_name == "_backward_Activation") {
+          CHECK_EQ(outputs[i], 1);
+          std::string act_type = node.source->attrs.dict.at("act_type");
+          std::string rhs, lhs;
+          rhs = variables[{node.inputs[0].node_id, node.inputs[0].index}];
+          if (act_type == "relu" ||
+              act_type == "sigmoid" ||
+              act_type == "tanh") {
+            lhs = variables[{node.inputs[1].node_id, node.inputs[1].index}];
+          } else {
+            lhs = variables[{node.inputs[2].node_id, node.inputs[2].index}];
+          }
+          code += "const auto " + var_name + " = backward_" + act_type +
+                  "(" + lhs + ", " + rhs + ");\n";
+
+          variables[{i, 0}] = var_name;
+          continue;
+        }
+        LOG(FATAL) << "Unrecognized op " + op_name;
+      }
+    } else {
+      LOG(FATAL) << "Encountered node with NULL source.";
+    }
+  }
+
+  int counter = 0;
 
 Review comment:
   ok

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


With regards,
Apache Git Services

Reply via email to