ptrendx commented on a change in pull request #15167: [WIP] Pointwise fusion for GPU URL: https://github.com/apache/incubator-mxnet/pull/15167#discussion_r299687710
########## File path: src/executor/pointwise_fusion_pass.cc ########## @@ -0,0 +1,279 @@ +/* + * 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. + */ + +/*! + * Copyright (c) 2019 by Contributors + * \file pointwise_fusion_pass.cc + * \brief + * \author Clement Fuji Tsang + */ + +#include <mxnet/base.h> +#include <mxnet/operator.h> +#include <mxnet/op_attr_types.h> +#include <nnvm/graph_attr_types.h> +#include <nnvm/pass_functions.h> +#include <algorithm> +#include "./simple_partition_pass.h" +#include "../operator/fusion/fused_op-inl.h" +#include "../operator/fusion/fused_op.h" +#include "../operator/operator_common.h" + +#if MXNET_USE_CUDA + +namespace mxnet { +namespace exec { +namespace { + bool IsFusionCompatible(nnvm::Node* n) { + using namespace mxnet::fusion; + if (n->op() == nullptr) + return false; + std::string op_name = n->op()->name; + if (ops_desc.count(op_name)) + return true; + if (slice_ops.count(op_name)) + return false; + if (std::find(variable_io_ops.begin(), + variable_io_ops.end(), + op_name) != + variable_io_ops.end()) + return true; + return false; + } + + bool IsInputsOnlyCompatible(nnvm::Node* n) { + using namespace mxnet::fusion; + if (n->op() == nullptr) + return false; + std::string op_name = n->op()->name; + if (slice_ops.count(op_name)) { + if (op_name == "slice") { + // slice with non-default step attribute is not supported + // currently + if (n->attrs.dict.count("step") && + !(n->attrs.dict.at("step") == "()" || + n->attrs.dict.at("step") == "[]")) { + return false; + } + } + return true; + } + return false; + } + + nnvm::NodePtr CreateSubgraphNode(const Graph& subgraph, size_t inputs_size) { + nnvm::Symbol subgraph_sym; + auto node = nnvm::Node::Create(); + subgraph_sym.outputs = subgraph.outputs; + node->attrs.subgraphs.emplace_back(std::make_shared<nnvm::Symbol>(subgraph_sym)); + std::ostringstream name_oss, params_oss; + // the name of the new node will be the concatenation of all the node names in the subgraph + DFSVisit(subgraph.outputs, [&name_oss](const nnvm::NodePtr n) { + if (n->op() != nullptr) + name_oss << n->op()->name << "_"; + }); + auto subgraph_name = name_oss.str(); + subgraph_name.pop_back(); + node->attrs.name = subgraph_name; + // in case the subgraph contains some of the weights + for (auto &e : subgraph_sym.ListInputNames(nnvm::Symbol::kAll)) { + params_oss << e << ";"; + } + auto params_names = params_oss.str(); + params_names.pop_back(); + node->attrs.dict["num_inputs"] = std::to_string(inputs_size); + node->attrs.dict["num_outputs"] = std::to_string(subgraph.outputs.size()); + node->attrs.op = Op::Get("_FusedOp"); + node->op()->attr_parser(&(node->attrs)); + return node; + } +} // namespace + +/*! + * \brief Replace a set of nodes by a subgraph node + */ +template<typename FCreateNode> +Graph ReplaceSubgraphsPointwise(Graph&& g, const std::vector<NodeRawPtrSet>& subgraph_sets, + FCreateNode create_subgraph_node) { + for (auto subgraph_set : subgraph_sets) { + // Create MXNet subgraph + Graph subgraph; + const auto sub_outputs_in_main = GetSubgraphOutputs(g, subgraph_set); + subgraph.outputs.resize(sub_outputs_in_main.size()); + for (auto p : sub_outputs_in_main) { + subgraph.outputs[p.second] = p.first; + } + // To generate a subgraph an input have to be replaced by data node (no op) + // and it have to be agnostic to the node from which it's an output + // (For exemple even if two inputs are two different outputs from the same node) Review comment: Sure, will make it clearer. ---------------------------------------------------------------- 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
