prune_test.cc 7.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yang Yang 已提交
2

L
Luo Tao 已提交
3 4 5
Licensed 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
Y
Yang Yang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yang Yang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
Y
Yang Yang 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/framework/prune.h"
Y
Yang Yang 已提交
16

17
#include <gtest/gtest.h>
18
#include <set>
19
#include <string>
20
#include <vector>
21

Y
Yi Wang 已提交
22 23
#include "paddle/fluid/framework/attribute.h"
#include "paddle/fluid/framework/operator.h"
Y
Yang Yang 已提交
24

Y
Yi Wang 已提交
25 26 27
#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/framework/program_desc.h"
Y
Yang Yang 已提交
28 29 30 31 32

namespace f = paddle::framework;

void AddOp(const std::string &type, const f::VariableNameMap &inputs,
           const f::VariableNameMap &outputs, f::AttributeMap attrs,
Y
Yu Yang 已提交
33
           paddle::framework::BlockDesc *block) {
Y
Yang Yang 已提交
34 35 36
  // insert output
  for (auto kv : outputs) {
    for (auto v : kv.second) {
Y
Yang Yang 已提交
37
      auto var = block->Var(v);
38
      var->SetDataType(paddle::framework::proto::VarType::FP32);
Y
Yang Yang 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    }
  }

  // insert op
  auto op = block->AppendOp();
  op->SetType(type);
  for (auto &kv : inputs) {
    op->SetInput(kv.first, kv.second);
  }
  for (auto &kv : outputs) {
    op->SetOutput(kv.first, kv.second);
  }
  op->SetAttrMap(attrs);
}

TEST(Prune, one_operator) {
Y
Yu Yang 已提交
55 56
  f::ProgramDesc program;
  f::BlockDesc *block = program.MutableBlock(0);
Y
Yang Yang 已提交
57

Y
Yiqun Liu 已提交
58 59
  AddOp("one_one", {{"input", {"a"}}}, {{"output", {"b"}}}, f::AttributeMap{},
        block);
Y
Yang Yang 已提交
60

61 62
  f::proto::ProgramDesc *pdesc = program.Proto();
  f::proto::ProgramDesc pruned;
63 64
  std::set<std::string> feed_var_names = {};
  f::Prune(*pdesc, feed_var_names, &pruned);
65
  EXPECT_EQ(pruned.blocks(0).ops_size(), 0);
Y
Yang Yang 已提交
66

67
  feed_var_names.insert("a");
Y
Yang Yang 已提交
68
  pdesc->mutable_blocks(0)->mutable_ops(0)->set_is_target(true);
69
  f::Prune(*pdesc, feed_var_names, &pruned);
70
  EXPECT_EQ(pruned.blocks(0).ops_size(), 1);
Y
Yang Yang 已提交
71 72
}

Y
Yang Yang 已提交
73
TEST(Prune, forward) {
Y
Yu Yang 已提交
74 75
  f::ProgramDesc program;
  f::BlockDesc *block = program.MutableBlock(0);
Y
Yang Yang 已提交
76

Y
Yiqun Liu 已提交
77 78 79 80 81 82 83 84
  AddOp("one_one", {{"input", {"a"}}}, {{"output", {"b"}}}, f::AttributeMap{},
        block);
  AddOp("one_one", {{"input", {"b"}}}, {{"output", {"c"}}}, f::AttributeMap{},
        block);
  AddOp("one_one", {{"input", {"c"}}}, {{"output", {"d"}}}, f::AttributeMap{},
        block);
  AddOp("one_one", {{"input", {"d"}}}, {{"output", {"e"}}}, f::AttributeMap{},
        block);
Y
Yang Yang 已提交
85

86
  f::proto::ProgramDesc *pdesc = program.Proto();
87
  std::set<std::string> feed_var_names = {"a"};
Y
Yang Yang 已提交
88
  for (int i = 0; i < pdesc->blocks(0).ops_size(); ++i) {
89
    f::proto::ProgramDesc pruned;
Y
Yang Yang 已提交
90
    pdesc->mutable_blocks(0)->mutable_ops(i)->set_is_target(true);
91
    f::Prune(*pdesc, feed_var_names, &pruned);
92
    EXPECT_EQ(pruned.blocks(0).ops_size(), i + 1);
Y
Yang Yang 已提交
93 94 95 96
  }
}

TEST(Prune, multi_input_op) {
Y
Yu Yang 已提交
97 98
  f::ProgramDesc program;
  f::BlockDesc *block = program.MutableBlock(0);
Y
Yang Yang 已提交
99

Y
Yiqun Liu 已提交
100 101 102
  AddOp("one_one", {{"input", {"a0"}}}, {{"output", {"b0"}}}, f::AttributeMap{},
        block);
  AddOp("one_one", {{"input", {"a1"}}}, {{"output", {"b1"}}}, f::AttributeMap{},
Y
Yang Yang 已提交
103
        block);
Y
Yiqun Liu 已提交
104 105 106 107
  AddOp("one_one", {{"input", {"a2"}}}, {{"output", {"b2"}}}, f::AttributeMap{},
        block);
  AddOp("three_one", {{"input", {"b0", "b1", "b2"}}}, {{"output", {"c"}}},
        f::AttributeMap{}, block);
Y
Yang Yang 已提交
108

109
  f::proto::ProgramDesc *pdesc = program.Proto();
Y
Yang Yang 已提交
110 111
  pdesc->mutable_blocks(0)->mutable_ops(3)->set_is_target(true);

112
  f::proto::ProgramDesc pruned;
113 114
  std::set<std::string> feed_var_names = {"a0", "a1", "a2"};
  f::Prune(*pdesc, feed_var_names, &pruned);
115
  EXPECT_EQ(pruned.blocks(0).ops_size(), 4);
Y
Yang Yang 已提交
116 117 118
}

TEST(Prune, multi_output_op) {
Y
Yu Yang 已提交
119 120
  f::ProgramDesc program;
  f::BlockDesc *block = program.MutableBlock(0);
Y
Yang Yang 已提交
121

Y
Yiqun Liu 已提交
122 123 124 125 126 127
  AddOp("one_two", {{"input", {"a"}}}, {{"output", {"b", "c"}}},
        f::AttributeMap{}, block);
  AddOp("one_one", {{"input", {"b"}}}, {{"output", {"b1"}}}, f::AttributeMap{},
        block);
  AddOp("one_one", {{"input", {"c"}}}, {{"output", {"c1"}}}, f::AttributeMap{},
        block);
Y
Yang Yang 已提交
128

129
  f::proto::ProgramDesc *pdesc = program.Proto();
Y
Yang Yang 已提交
130 131
  pdesc->mutable_blocks(0)->mutable_ops(2)->set_is_target(true);

132
  f::proto::ProgramDesc pruned;
133 134
  std::set<std::string> feed_var_names = {"a"};
  f::Prune(*pdesc, feed_var_names, &pruned);
135
  EXPECT_EQ(pruned.blocks(0).ops_size(), 2);
Y
Yang Yang 已提交
136
}
Y
Yang Yang 已提交
137 138

TEST(Prune, multi_target) {
Y
Yu Yang 已提交
139 140
  f::ProgramDesc program;
  f::BlockDesc *block = program.MutableBlock(0);
Y
Yang Yang 已提交
141

Y
Yiqun Liu 已提交
142 143 144 145 146 147
  AddOp("one_two", {{"input", {"a"}}}, {{"output", {"b", "c"}}},
        f::AttributeMap{}, block);
  AddOp("one_one", {{"input", {"b"}}}, {{"output", {"b1"}}}, f::AttributeMap{},
        block);
  AddOp("one_one", {{"input", {"c"}}}, {{"output", {"c1"}}}, f::AttributeMap{},
        block);
Y
Yang Yang 已提交
148

149
  f::proto::ProgramDesc *pdesc = program.Proto();
Y
Yang Yang 已提交
150 151 152
  pdesc->mutable_blocks(0)->mutable_ops(1)->set_is_target(true);
  pdesc->mutable_blocks(0)->mutable_ops(2)->set_is_target(true);

153
  f::proto::ProgramDesc pruned;
154 155
  std::set<std::string> feed_var_names = {"a"};
  f::Prune(*pdesc, feed_var_names, &pruned);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  EXPECT_EQ(pruned.blocks(0).ops_size(), 3);
}

TEST(Prune, recurrrent_op) {
  f::ProgramDesc program;
  f::BlockDesc *block = program.MutableBlock(0);
  f::BlockDesc *sub_block = program.AppendBlock(*block);
  AddOp("one_two", {{"input", {"a"}}}, {{"output", {"b", "c"}}},
        f::AttributeMap{}, block);

  std::vector<std::string> state_var_name(1, "y");
  AddOp("recurrent", {{"input", {"b", "c"}}}, {{"output", {"b1, c1"}}},
        {{"ex_states", state_var_name},
         {"states", state_var_name},
         {"sub_block", sub_block}},
        block);

  EXPECT_TRUE(sub_block != nullptr);
  AddOp("rnn_memory_helper", {{"input", {"x"}}}, {{"output", {"y"}}},
        f::AttributeMap{}, sub_block);

  f::proto::ProgramDesc *pdesc = program.Proto();
  pdesc->mutable_blocks(0)->mutable_ops(1)->set_is_target(true);

  f::proto::ProgramDesc pruned;
  std::set<std::string> feed_var_names = {"a"};

  f::Prune(*pdesc, feed_var_names, &pruned);
  EXPECT_EQ(pruned.blocks_size(), 2);
  EXPECT_EQ(pruned.blocks(0).ops_size(), 2);
  EXPECT_EQ(pruned.blocks(1).ops_size(), 1);
Y
Yang Yang 已提交
187
}
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

// If the output of an op modifies feed vars, the op should not clip.
TEST(Prune, recurrrent_op_2) {
  f::ProgramDesc program;
  f::BlockDesc *block = program.MutableBlock(0);
  f::BlockDesc *sub_block = program.AppendBlock(*block);
  AddOp("one_two", {{"input", {"a"}}}, {{"output", {"b", "c"}}},
        f::AttributeMap{}, block);

  std::vector<std::string> state_var_name(1, "y");
  AddOp("recurrent", {{"input", {"b", "c"}}}, {{"output", {"b1, c1"}}},
        {{"ex_states", state_var_name},
         {"states", state_var_name},
         {"sub_block", sub_block}},
        block);

  EXPECT_TRUE(sub_block != nullptr);
  AddOp("rnn_memory_helper", {{"input", {"x"}}}, {{"output", {"a"}}},
        f::AttributeMap{}, sub_block);

  f::proto::ProgramDesc *pdesc = program.Proto();
  pdesc->mutable_blocks(0)->mutable_ops(1)->set_is_target(true);

  f::proto::ProgramDesc pruned;
  std::set<std::string> feed_var_names = {"x", "a"};

  f::Prune(*pdesc, feed_var_names, &pruned);
  EXPECT_EQ(pruned.blocks_size(), 2);
  EXPECT_EQ(pruned.blocks(0).ops_size(), 2);
  EXPECT_EQ(pruned.blocks(1).ops_size(), 1);
}