|
| 1 | +// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <string> |
| 16 | + |
| 17 | +#include "paddle/fluid/framework/ir/graph_viz_pass.h" |
| 18 | +#include "paddle/fluid/framework/ir/shuffle_channel_detect_pass.h" |
| 19 | + |
| 20 | +namespace paddle { |
| 21 | +namespace framework { |
| 22 | +namespace ir { |
| 23 | + |
| 24 | +#define GET_IR_NODE(node__) GET_IR_NODE_FROM_SUBGRAPH(node__, node__, pattern); |
| 25 | +#define GET_NODES \ |
| 26 | + GET_IR_NODE(reshape1_op); \ |
| 27 | + GET_IR_NODE(reshape1_out); \ |
| 28 | + GET_IR_NODE(transpose_op); \ |
| 29 | + GET_IR_NODE(transpose_out); \ |
| 30 | + GET_IR_NODE(reshape2_op); \ |
| 31 | + GET_IR_NODE(reshape2_out); |
| 32 | + |
| 33 | +void ShuffleChannelDetectPass::ApplyImpl(ir::Graph* graph) const { |
| 34 | + const std::string pattern_name = "shufflechannel_pattern"; |
| 35 | + FusePassBase::Init(pattern_name, graph); |
| 36 | + |
| 37 | + GraphPatternDetector gpd; |
| 38 | + auto* x = gpd.mutable_pattern() |
| 39 | + ->NewNode("x") |
| 40 | + ->assert_is_op_input("reshape2", "X") |
| 41 | + ->AsInput(); |
| 42 | + |
| 43 | + patterns::ShuffleChannelPattern pattern(gpd.mutable_pattern(), pattern_name); |
| 44 | + pattern(x); |
| 45 | + |
| 46 | + auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph, |
| 47 | + Graph* g) { |
| 48 | + GET_NODES; |
| 49 | + |
| 50 | + PADDLE_ENFORCE(subgraph.count(x)); |
| 51 | + auto* input_node = subgraph.at(x); |
| 52 | + auto reshape1_desc = reshape1_op->Op(); |
| 53 | + auto reshape2_desc = reshape2_op->Op(); |
| 54 | + std::string input_name = input_node->Name(); |
| 55 | + std::string output_name = reshape2_out->Name(); |
| 56 | + |
| 57 | + auto reshape1_shape = |
| 58 | + boost::get<std::vector<int>>(reshape1_desc->GetAttr("shape")); |
| 59 | + auto reshape2_shape = |
| 60 | + boost::get<std::vector<int>>(reshape2_desc->GetAttr("shape")); |
| 61 | + |
| 62 | + int i_c = reshape1_shape[2]; |
| 63 | + int o_c = reshape2_shape[1]; |
| 64 | + int group = o_c / i_c; |
| 65 | + |
| 66 | + framework::OpDesc new_op_desc; |
| 67 | + new_op_desc.SetType("shuffle_channel"); |
| 68 | + new_op_desc.SetInput("X", {input_name}); |
| 69 | + new_op_desc.SetOutput("Out", {output_name}); |
| 70 | + |
| 71 | + new_op_desc.SetAttr("group", group); |
| 72 | + new_op_desc.Flush(); |
| 73 | + |
| 74 | + // Create a new node for the fused op. |
| 75 | + auto* new_op = graph->CreateOpNode(&new_op_desc); |
| 76 | + |
| 77 | + IR_NODE_LINK_TO(input_node, new_op); |
| 78 | + IR_NODE_LINK_TO(new_op, reshape2_out); |
| 79 | + |
| 80 | + // Delete the unneeded nodes. |
| 81 | + GraphSafeRemoveNodes(graph, {reshape1_op, reshape1_out, transpose_op, |
| 82 | + transpose_out, reshape2_op}); |
| 83 | + }; |
| 84 | + |
| 85 | + gpd(graph, handler); |
| 86 | +} |
| 87 | + |
| 88 | +} // namespace ir |
| 89 | +} // namespace framework |
| 90 | +} // namespace paddle |
| 91 | + |
| 92 | +REGISTER_PASS(shuffle_channel_detect_pass, |
| 93 | + paddle::framework::ir::ShuffleChannelDetectPass); |
0 commit comments