prune_test.cc 4.9 KB
Newer Older
Y
Yang Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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

   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 "paddle/framework/prune.h"

#include "paddle/framework/attribute.h"
#include "paddle/framework/operator.h"
#include "paddle/operators/net_op.h"

Y
Yang Yang 已提交
21 22 23
#include "paddle/framework/block_desc.h"
#include "paddle/framework/op_desc.h"
#include "paddle/framework/program_desc.h"
Y
Yang Yang 已提交
24

Y
Yang Yang 已提交
25
#include <gtest/gtest.h>
Y
Yang Yang 已提交
26 27 28 29 30 31 32 33 34 35

namespace f = paddle::framework;
namespace ops = paddle::operators;

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

  // 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
Yang Yang 已提交
54
  f::ProgramDescBind program;
55
  f::BlockDescBind *block = program.MutableBlock(0);
Y
Yang Yang 已提交
56

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

60 61
  f::proto::ProgramDesc *pdesc = program.Proto();
  f::proto::ProgramDesc pruned;
Y
Yang Yang 已提交
62

63
  f::Prune(*pdesc, &pruned);
Y
Yang Yang 已提交
64 65 66
  PADDLE_ENFORCE_EQ(pruned.blocks(0).ops_size(), 0);

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

Y
Yang Yang 已提交
71
TEST(Prune, forward) {
Y
Yang Yang 已提交
72
  f::ProgramDescBind program;
73
  f::BlockDescBind *block = program.MutableBlock(0);
Y
Yang Yang 已提交
74

Y
Yiqun Liu 已提交
75 76 77 78 79 80 81 82
  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 已提交
83

84
  f::proto::ProgramDesc *pdesc = program.Proto();
Y
Yang Yang 已提交
85 86

  for (int i = 0; i < pdesc->blocks(0).ops_size(); ++i) {
87
    f::proto::ProgramDesc pruned;
Y
Yang Yang 已提交
88
    pdesc->mutable_blocks(0)->mutable_ops(i)->set_is_target(true);
89
    f::Prune(*pdesc, &pruned);
Y
Yang Yang 已提交
90 91 92 93 94
    PADDLE_ENFORCE_EQ(pruned.blocks(0).ops_size(), i + 1);
  }
}

TEST(Prune, multi_input_op) {
Y
Yang Yang 已提交
95
  f::ProgramDescBind program;
96
  f::BlockDescBind *block = program.MutableBlock(0);
Y
Yang Yang 已提交
97

Y
Yiqun Liu 已提交
98 99 100
  AddOp("one_one", {{"input", {"a0"}}}, {{"output", {"b0"}}}, f::AttributeMap{},
        block);
  AddOp("one_one", {{"input", {"a1"}}}, {{"output", {"b1"}}}, f::AttributeMap{},
Y
Yang Yang 已提交
101
        block);
Y
Yiqun Liu 已提交
102 103 104 105
  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 已提交
106

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

110 111
  f::proto::ProgramDesc pruned;
  f::Prune(*pdesc, &pruned);
Y
Yang Yang 已提交
112 113 114 115
  PADDLE_ENFORCE_EQ(pruned.blocks(0).ops_size(), 4);
}

TEST(Prune, multi_output_op) {
Y
Yang Yang 已提交
116
  f::ProgramDescBind program;
117
  f::BlockDescBind *block = program.MutableBlock(0);
Y
Yang Yang 已提交
118

Y
Yiqun Liu 已提交
119 120 121 122 123 124
  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 已提交
125

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

129 130
  f::proto::ProgramDesc pruned;
  f::Prune(*pdesc, &pruned);
Y
Yang Yang 已提交
131 132
  PADDLE_ENFORCE_EQ(pruned.blocks(0).ops_size(), 2);
}
Y
Yang Yang 已提交
133 134

TEST(Prune, multi_target) {
Y
Yang Yang 已提交
135
  f::ProgramDescBind program;
136
  f::BlockDescBind *block = program.MutableBlock(0);
Y
Yang Yang 已提交
137

Y
Yiqun Liu 已提交
138 139 140 141 142 143
  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 已提交
144

145
  f::proto::ProgramDesc *pdesc = program.Proto();
Y
Yang Yang 已提交
146 147 148
  pdesc->mutable_blocks(0)->mutable_ops(1)->set_is_target(true);
  pdesc->mutable_blocks(0)->mutable_ops(2)->set_is_target(true);

149 150
  f::proto::ProgramDesc pruned;
  f::Prune(*pdesc, &pruned);
Y
Yang Yang 已提交
151 152
  PADDLE_ENFORCE_EQ(pruned.blocks(0).ops_size(), 3);
}