program_desc_test.cc 6.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yu 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
Yu Yang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yu 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
Yu Yang 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/framework/program_desc.h"
W
wanghuancoder 已提交
16

Y
Yu Yang 已提交
17 18 19 20
#include "gtest/gtest.h"

namespace paddle {
namespace framework {
W
wanghuancoder 已提交
21 22
class VarDesc;

Y
Yu Yang 已提交
23
TEST(ProgramDesc, copy_ctor) {
Y
Yu Yang 已提交
24
  ProgramDesc program;
25
  auto* global_block = program.MutableBlock(0);
Y
Yu Yang 已提交
26
  auto* x = global_block->Var("X");
27
  x->SetType(proto::VarType::LOD_TENSOR);
Y
Yu Yang 已提交
28
  x->SetLoDLevel(0);
29
  x->SetDataType(proto::VarType::FP32);
Y
Yu Yang 已提交
30 31 32
  x->SetShape({1000, 784});

  auto* y = global_block->Var("Y");
33
  y->SetType(proto::VarType::LOD_TENSOR);
Y
Yu Yang 已提交
34
  y->SetLoDLevel(0);
35
  y->SetDataType(proto::VarType::FP32);
Y
Yu Yang 已提交
36 37 38 39 40 41 42 43
  y->SetShape({784, 100});

  auto* op = global_block->AppendOp();
  op->SetType("mul");
  op->SetInput("X", {x->Name()});
  op->SetInput("Y", {y->Name()});

  auto* out = global_block->Var("Out");
44
  out->SetType(proto::VarType::LOD_TENSOR);
Y
Yu Yang 已提交
45 46
  op->SetOutput("Y", {out->Name()});

X
Xin Pan 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59
  BlockDesc* new_block = program.AppendBlock(*global_block);
  op = new_block->AppendOp();
  op->SetType("mul");

  op = global_block->AppendOp();
  op->SetType("op_with_subblock");
  op->SetAttr("sub_block", new_block);

  std::vector<BlockDesc*> sub_blocks;
  sub_blocks.push_back(program.AppendBlock(*global_block));
  sub_blocks.push_back(program.AppendBlock(*global_block));
  op->SetAttr("sub_blocks", sub_blocks);

Y
Yu Yang 已提交
60
  ProgramDesc program_copy(program);
Y
Yu Yang 已提交
61

62
  auto* global_block_copy = program_copy.MutableBlock(0);
Y
Yu Yang 已提交
63 64
  ASSERT_NE(global_block, global_block_copy);

Y
Yu Yang 已提交
65
  auto assert_same_var = [&](const std::string& name, VarDesc* var_before) {
Y
Yu Yang 已提交
66 67 68 69 70
    ASSERT_TRUE(global_block_copy->HasVar(name));
    auto* copy = global_block_copy->Var(name);
    ASSERT_NE(copy, var_before);
    ASSERT_EQ(copy->Name(), var_before->Name());
    ASSERT_EQ(copy->GetType(), var_before->GetType());
F
fengjiayi 已提交
71
    ASSERT_EQ(copy->GetShape(), var_before->GetShape());
Y
Yu Yang 已提交
72 73 74 75 76
    ASSERT_EQ(copy->Proto()->SerializeAsString(),
              var_before->Proto()->SerializeAsString());
  };

  ASSERT_EQ(global_block->LocalVarNames(), global_block_copy->LocalVarNames());
77
  ASSERT_EQ(3UL, global_block_copy->LocalVarNames().size());
Y
Yu Yang 已提交
78 79 80 81
  assert_same_var("X", x);
  assert_same_var("Y", y);
  assert_same_var("Out", out);

X
Xin Pan 已提交
82 83
  bool found_sub_block = false;
  bool found_sub_blocks = false;
Y
Yu Yang 已提交
84 85
  for (size_t i = 0; i < global_block->OpSize(); ++i) {
    auto op_origin = global_block->Op(i);
D
dongdaxiang 已提交
86
    auto op_copy = global_block_copy->Op(i);
Y
Yu Yang 已提交
87 88 89 90 91

    ASSERT_EQ(op_origin->Type(), op_copy->Type());
    ASSERT_EQ(op_origin->Inputs(), op_copy->Inputs());
    ASSERT_EQ(op_origin->Outputs(), op_copy->Outputs());

J
JiabinYang 已提交
92 93 94 95 96 97 98 99 100 101 102
    ASSERT_EQ(op_origin->Proto()->attrs().size(),
              op_copy->Proto()->attrs().size());
    for (auto it = op_origin->Proto()->attrs().begin();
         it != op_origin->Proto()->attrs().end(); ++it) {
      for (auto it_2 = op_copy->Proto()->attrs().begin();
           it_2 != op_copy->Proto()->attrs().end(); ++it_2) {
        if (it->name() == it_2->name()) {
          ASSERT_TRUE(it_2->SerializeAsString() == it->SerializeAsString());
        }
      }
    }
Y
Yu Yang 已提交
103

X
Xin Pan 已提交
104 105 106 107
    if (op->Type() == "op_with_subblock") {
      ASSERT_EQ(1, op->GetBlockAttrId("sub_block"));
      found_sub_block = true;

108
      ASSERT_EQ(2UL, op->GetBlocksAttrIds("sub_blocks").size());
X
Xin Pan 已提交
109 110 111 112 113
      found_sub_blocks = true;
    }
  }
  ASSERT_TRUE(found_sub_block);
  ASSERT_TRUE(found_sub_blocks);
Y
Yu Yang 已提交
114 115 116
  // Not check block's protostr are same it because the order of vars could be
  // different and it is correct.
}
117 118

TEST(ProgramDescBind, serialize_and_deserialize) {
Y
Yu Yang 已提交
119
  ProgramDesc program_origin;
120
  auto* global_block = program_origin.MutableBlock(0);
121
  auto* x = global_block->Var("X");
122
  x->SetType(proto::VarType::LOD_TENSOR);
123
  x->SetLoDLevel(0);
124
  x->SetDataType(proto::VarType::FP32);
125 126 127
  x->SetShape({1000, 784});

  auto* y = global_block->Var("Y");
128
  y->SetType(proto::VarType::LOD_TENSOR);
129
  y->SetLoDLevel(0);
130
  y->SetDataType(proto::VarType::FP32);
131 132 133 134 135 136 137 138
  y->SetShape({784, 100});

  auto* op = global_block->AppendOp();
  op->SetType("mul");
  op->SetInput("X", {x->Name()});
  op->SetInput("Y", {y->Name()});

  auto* out = global_block->Var("Out");
139
  out->SetType(proto::VarType::LOD_TENSOR);
140 141 142 143 144
  op->SetOutput("Y", {out->Name()});

  std::string binary_str;
  program_origin.Proto()->SerializeToString(&binary_str);

Y
Yu Yang 已提交
145
  ProgramDesc program_restored(binary_str);
146
  auto* global_block_restored = program_restored.MutableBlock(0);
147 148
  ASSERT_NE(global_block, global_block_restored);

Y
Yu Yang 已提交
149
  auto assert_same_var = [&](const std::string& name, VarDesc* var_before) {
150 151 152 153 154
    ASSERT_TRUE(global_block_restored->HasVar(name));
    auto* restored = global_block_restored->Var(name);
    ASSERT_NE(restored, var_before);
    ASSERT_EQ(restored->Name(), var_before->Name());
    ASSERT_EQ(restored->GetType(), var_before->GetType());
F
fengjiayi 已提交
155
    ASSERT_EQ(restored->GetShape(), var_before->GetShape());
156 157 158 159 160 161 162 163 164 165 166 167 168
    ASSERT_EQ(restored->Proto()->SerializeAsString(),
              var_before->Proto()->SerializeAsString());
  };

  ASSERT_EQ(global_block->LocalVarNames(),
            global_block_restored->LocalVarNames());
  ASSERT_EQ(3UL, global_block_restored->LocalVarNames().size());
  assert_same_var("X", x);
  assert_same_var("Y", y);
  assert_same_var("Out", out);

  for (size_t i = 0; i < global_block->OpSize(); ++i) {
    auto op_origin = global_block->Op(i);
D
dongdaxiang 已提交
169
    auto op_restored = global_block_restored->Op(i);
170 171 172 173 174 175 176 177 178

    ASSERT_EQ(op_origin->Type(), op_restored->Type());
    ASSERT_EQ(op_origin->Inputs(), op_restored->Inputs());
    ASSERT_EQ(op_origin->Outputs(), op_restored->Outputs());

    ASSERT_EQ(op_restored->Proto()->SerializeAsString(),
              op_origin->Proto()->SerializeAsString());
  }
}
Y
Yu Yang 已提交
179
}  // namespace framework
Y
Yu Yang 已提交
180
}  // namespace paddle