program_desc_test.cc 4.9 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* 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/program_desc.h"
#include "gtest/gtest.h"
#include "paddle/framework/block_desc.h"

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

  auto* y = global_block->Var("Y");
31
  y->SetType(proto::VarDesc_VarType_LOD_TENSOR);
Y
Yu Yang 已提交
32
  y->SetLoDLevel(0);
33
  y->SetDataType(proto::FP32);
Y
Yu Yang 已提交
34 35 36 37 38 39 40 41
  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");
42
  out->SetType(proto::VarDesc_VarType_LOD_TENSOR);
Y
Yu Yang 已提交
43 44
  op->SetOutput("Y", {out->Name()});

Y
Yu Yang 已提交
45
  ProgramDesc program_copy(program);
Y
Yu Yang 已提交
46

47
  auto* global_block_copy = program_copy.MutableBlock(0);
Y
Yu Yang 已提交
48 49
  ASSERT_NE(global_block, global_block_copy);

Y
Yu Yang 已提交
50
  auto assert_same_var = [&](const std::string& name, VarDesc* var_before) {
Y
Yu Yang 已提交
51 52 53 54 55 56 57 58 59 60 61
    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());
    ASSERT_EQ(copy->Shape(), var_before->Shape());
    ASSERT_EQ(copy->Proto()->SerializeAsString(),
              var_before->Proto()->SerializeAsString());
  };

  ASSERT_EQ(global_block->LocalVarNames(), global_block_copy->LocalVarNames());
62
  ASSERT_EQ(3UL, global_block_copy->LocalVarNames().size());
Y
Yu Yang 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  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);
    auto op_copy = global_block->Op(i);

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

    ASSERT_EQ(op_copy->Proto()->SerializeAsString(),
              op_origin->Proto()->SerializeAsString());
  }

  // Not check block's protostr are same it because the order of vars could be
  // different and it is correct.
}
82 83

TEST(ProgramDescBind, serialize_and_deserialize) {
Y
Yu Yang 已提交
84
  ProgramDesc program_origin;
85
  auto* global_block = program_origin.MutableBlock(0);
86
  auto* x = global_block->Var("X");
87
  x->SetType(proto::VarDesc_VarType_LOD_TENSOR);
88
  x->SetLoDLevel(0);
89
  x->SetDataType(proto::FP32);
90 91 92
  x->SetShape({1000, 784});

  auto* y = global_block->Var("Y");
93
  y->SetType(proto::VarDesc_VarType_LOD_TENSOR);
94
  y->SetLoDLevel(0);
95
  y->SetDataType(proto::FP32);
96 97 98 99 100 101 102 103
  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");
104
  out->SetType(proto::VarDesc_VarType_LOD_TENSOR);
105 106 107 108 109
  op->SetOutput("Y", {out->Name()});

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

Y
Yu Yang 已提交
110
  ProgramDesc program_restored(binary_str);
111
  auto* global_block_restored = program_restored.MutableBlock(0);
112 113
  ASSERT_NE(global_block, global_block_restored);

Y
Yu Yang 已提交
114
  auto assert_same_var = [&](const std::string& name, VarDesc* var_before) {
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    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());
    ASSERT_EQ(restored->Shape(), var_before->Shape());
    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);
    auto op_restored = global_block->Op(i);

    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 已提交
144
}  // namespace framework
Y
Yu Yang 已提交
145
}  // namespace paddle