|
| 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 "paddle/fluid/framework/ir/seqpool_concat_fuse_pass.h" |
| 16 | +#include <gtest/gtest.h> |
| 17 | +#include "paddle/fluid/framework/op_proto_maker.h" |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | +namespace framework { |
| 21 | +namespace ir { |
| 22 | + |
| 23 | +void SetOp(ProgramDesc* prog, const std::string& type, |
| 24 | + const std::vector<std::string>& inputs, |
| 25 | + const std::vector<std::string>& outputs) { |
| 26 | + auto* op = prog->MutableBlock(0)->AppendOp(); |
| 27 | + op->SetType(type); |
| 28 | + if (type == "sequence_pool") { |
| 29 | + op->SetInput("X", {inputs[0]}); |
| 30 | + std::string pooltype = "SUM"; |
| 31 | + op->SetAttr("pooltype", pooltype); |
| 32 | + op->SetOutput("MaxIndex", {outputs[0]}); |
| 33 | + op->SetOutput("Out", {outputs[1]}); |
| 34 | + } else if (type == "concat") { |
| 35 | + op->SetInput("X", inputs); |
| 36 | + op->SetAttr("axis", 1); |
| 37 | + op->SetOutput("Out", {outputs[0]}); |
| 38 | + } else { |
| 39 | + op->SetInput("X", inputs); |
| 40 | + op->SetOutput("Out", outputs); |
| 41 | + } |
| 42 | + op->SetAttr(OpProtoAndCheckerMaker::OpRoleAttrName(), |
| 43 | + static_cast<int>(OpRole::kForward)); |
| 44 | +} |
| 45 | + |
| 46 | +int CountOpType(const ir::Graph* graph, |
| 47 | + const std::string& op_type = "fusion_seqpool_concat") { |
| 48 | + int count = 0; |
| 49 | + for (auto* node : graph->Nodes()) { |
| 50 | + if (node->IsOp() && node->Op()->Type() == op_type) { |
| 51 | + ++count; |
| 52 | + } |
| 53 | + } |
| 54 | + return count; |
| 55 | +} |
| 56 | + |
| 57 | +std::unique_ptr<ir::Graph> GetNumNodesOfBeforeAfter( |
| 58 | + std::unique_ptr<ir::Graph> graph, int* before, int* after, |
| 59 | + const std::string& pass_type = "seqpool_concat_fuse_pass") { |
| 60 | + auto pass = PassRegistry::Instance().Get(pass_type); |
| 61 | + *before = graph->Nodes().size(); |
| 62 | + graph = pass->Apply(std::move(graph)); |
| 63 | + *after = graph->Nodes().size(); |
| 64 | + return graph; |
| 65 | +} |
| 66 | + |
| 67 | +/* |
| 68 | + * Before fuse: |
| 69 | + * a b c |
| 70 | + * | | | |
| 71 | + * op1 op2 op3 |
| 72 | + * / \ / \ / \ |
| 73 | + * d e f g h i |
| 74 | + * \ | / |
| 75 | + * concat |
| 76 | + * | |
| 77 | + * j |
| 78 | + * Type of op1, op2 and op3 are sequence_pool, with "SUM" pooltype attr |
| 79 | + * |
| 80 | + * After fuse: |
| 81 | + * a b c |
| 82 | + * \ | / |
| 83 | + * fusion_seqpool_concat |
| 84 | + * | |
| 85 | + * j |
| 86 | + */ |
| 87 | +TEST(SeqPoolConcatFusePass, basic) { |
| 88 | + ProgramDesc prog; |
| 89 | + for (auto& v : std::vector<std::string>( |
| 90 | + {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"})) { |
| 91 | + auto* var = prog.MutableBlock(0)->Var(v); |
| 92 | + var->SetType(proto::VarType::LOD_TENSOR); |
| 93 | + } |
| 94 | + |
| 95 | + SetOp(&prog, "sequence_pool", std::vector<std::string>({"a"}), |
| 96 | + std::vector<std::string>({"d", "e"})); |
| 97 | + SetOp(&prog, "sequence_pool", std::vector<std::string>({"b"}), |
| 98 | + std::vector<std::string>({"f", "g"})); |
| 99 | + SetOp(&prog, "sequence_pool", std::vector<std::string>({"c"}), |
| 100 | + std::vector<std::string>({"h", "i"})); |
| 101 | + SetOp(&prog, "concat", std::vector<std::string>({"e", "g", "i"}), |
| 102 | + std::vector<std::string>({"j"})); |
| 103 | + |
| 104 | + std::unique_ptr<ir::Graph> graph(new ir::Graph(prog)); |
| 105 | + int before, after; |
| 106 | + graph = GetNumNodesOfBeforeAfter(std::move(graph), &before, &after); |
| 107 | + // Remove 10 Nodes: op1, op2, op3, d, e, f, g, h, i, concat_op |
| 108 | + // Add 1 Node: fusion_seqpool_concat |
| 109 | + EXPECT_EQ(after, before - 9); |
| 110 | + EXPECT_EQ(CountOpType(graph.get()), 1); |
| 111 | +} |
| 112 | + |
| 113 | +/* |
| 114 | + * Before fuse: |
| 115 | + * a b |
| 116 | + * | / \ |
| 117 | + * op1 op2 op3 |
| 118 | + * / \ / \ \ |
| 119 | + * c d e f g |
| 120 | + * \ / |
| 121 | + * concat |
| 122 | + * | |
| 123 | + * h |
| 124 | + * Type of op1 and op2 are sequence_pool, with "SUM" pooltype attr |
| 125 | + * |
| 126 | + * After fuse: |
| 127 | + * a b |
| 128 | + * \ / \ |
| 129 | + * fusion_seqpool_concat op3 |
| 130 | + * | | |
| 131 | + * h g |
| 132 | + */ |
| 133 | +TEST(SeqPoolConcatFusePass, advanced) { |
| 134 | + ProgramDesc prog; |
| 135 | + for (auto& v : |
| 136 | + std::vector<std::string>({"a", "b", "c", "d", "e", "f", "g", "h"})) { |
| 137 | + auto* var = prog.MutableBlock(0)->Var(v); |
| 138 | + var->SetType(proto::VarType::LOD_TENSOR); |
| 139 | + } |
| 140 | + |
| 141 | + SetOp(&prog, "sequence_pool", std::vector<std::string>({"a"}), |
| 142 | + std::vector<std::string>({"c", "d"})); |
| 143 | + SetOp(&prog, "sequence_pool", std::vector<std::string>({"b"}), |
| 144 | + std::vector<std::string>({"e", "f"})); |
| 145 | + SetOp(&prog, "op3", std::vector<std::string>({"b"}), |
| 146 | + std::vector<std::string>({"g"})); |
| 147 | + SetOp(&prog, "concat", std::vector<std::string>({"d", "f"}), |
| 148 | + std::vector<std::string>({"h"})); |
| 149 | + |
| 150 | + std::unique_ptr<ir::Graph> graph(new ir::Graph(prog)); |
| 151 | + int before, after; |
| 152 | + graph = GetNumNodesOfBeforeAfter(std::move(graph), &before, &after); |
| 153 | + // Remove 7 Nodes: op1, op2, c, d, e, f concat_op |
| 154 | + // Add 1 Node: fusion_seqpool_concat |
| 155 | + EXPECT_EQ(after, before - 6); |
| 156 | + EXPECT_EQ(CountOpType(graph.get()), 1); |
| 157 | +} |
| 158 | + |
| 159 | +ProgramDesc BuildProgramDesc(int num_inputs_of_concat) { |
| 160 | + ProgramDesc prog; |
| 161 | + auto new_var = [&](const std::string& name) { |
| 162 | + auto* var = prog.MutableBlock(0)->Var(name); |
| 163 | + var->SetType(proto::VarType::LOD_TENSOR); |
| 164 | + }; |
| 165 | + std::vector<std::string> concat_inputs; |
| 166 | + for (int i = 0; i < num_inputs_of_concat; ++i) { |
| 167 | + std::string prefix = "seqpool_op_" + i; |
| 168 | + new_var(prefix + "in"); |
| 169 | + new_var(prefix + "out"); |
| 170 | + new_var(prefix + "out_unused"); |
| 171 | + SetOp(&prog, "sequence_pool", std::vector<std::string>({prefix + "in"}), |
| 172 | + std::vector<std::string>({prefix + "out", prefix + "out_unused"})); |
| 173 | + concat_inputs.push_back(prefix + "out"); |
| 174 | + } |
| 175 | + SetOp(&prog, "concat", concat_inputs, |
| 176 | + std::vector<std::string>({"concat_out"})); |
| 177 | + return prog; |
| 178 | +} |
| 179 | + |
| 180 | +// test more inputs of concat |
| 181 | +TEST(SeqPoolConcatFusePass, more_inputs) { |
| 182 | + for (int num : {1, 2, 10}) { |
| 183 | + ProgramDesc prog = BuildProgramDesc(num); |
| 184 | + std::unique_ptr<ir::Graph> graph(new ir::Graph(prog)); |
| 185 | + int before, after; |
| 186 | + graph = GetNumNodesOfBeforeAfter(std::move(graph), &before, &after); |
| 187 | + // Remove Nodes: n * (seqpool_op, out, out_unused), and concat_op |
| 188 | + // Add Node: fusion_seqpool_concat op |
| 189 | + EXPECT_EQ(after, before - num * 3); |
| 190 | + EXPECT_EQ(CountOpType(graph.get()), 1); |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +} // namespace ir |
| 195 | +} // namespace framework |
| 196 | +} // namespace paddle |
| 197 | + |
| 198 | +USE_PASS(seqpool_concat_fuse_pass); |
0 commit comments