net_op_test.cc 2.7 KB
Newer Older
Y
Yan Chunwei 已提交
1 2
#include "paddle/operators/net_op.h"

Q
Qiao Longfei 已提交
3
#include <gtest/gtest.h>
Y
Yan Chunwei 已提交
4

D
dongzhihong 已提交
5
namespace paddle {
Y
Yan Chunwei 已提交
6
namespace operators {
D
dongzhihong 已提交
7 8
using Scope = framework::Scope;
using DeviceContext = platform::DeviceContext;
Q
Qiao Longfei 已提交
9 10 11

static int run_cnt = 0;

D
dongzhihong 已提交
12
class TestOp : public framework::OperatorBase {
13
 public:
Y
Yu Yang 已提交
14
  using framework::OperatorBase::OperatorBase;
Y
Yu Yang 已提交
15
  DEFINE_OP_CLONE_METHOD(TestOp);
D
dongzhihong 已提交
16 17
  void Run(const Scope& scope,
           const platform::DeviceContext& dev_ctx) const override {
Q
Qiao Longfei 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    ++run_cnt;
  }
};

template <typename T>
void AssertSameVectorWithoutOrder(const std::vector<T>& expected,
                                  const std::vector<T>& actual) {
  ASSERT_EQ(expected.size(), actual.size());
  std::unordered_set<T> expected_set;
  for (auto& tmp : expected) {
    expected_set.insert(tmp);
  }
  for (auto& act : actual) {
    ASSERT_NE(expected_set.end(), expected_set.find(act));
  }
}

TEST(OpKernel, all) {
Y
Yu Yang 已提交
36
  auto net = std::make_shared<NetOp>();
D
dongzhihong 已提交
37 38
  ASSERT_NE(net, nullptr);

Y
Yu Yang 已提交
39
  net->AppendOp(std::unique_ptr<TestOp>(
Y
Yu Yang 已提交
40
      new TestOp("test", {{"X", {"x"}}, {"W", {"w1"}}, {"b", {"b1"}}},
Y
Yiqun Liu 已提交
41
                 {{"Out", {"y"}}}, framework::AttributeMap{})));
Y
Yu Yang 已提交
42
  net->AppendOp(std::unique_ptr<TestOp>(
Y
Yu Yang 已提交
43
      new TestOp("test", {{"X", {"y"}}, {"W", {"w2"}}, {"b", {"b2"}}},
Y
Yiqun Liu 已提交
44
                 {{"Out", {"z"}}}, framework::AttributeMap{})));
D
dongzhihong 已提交
45 46

  net->CompleteAddOp();
Y
Yu Yang 已提交
47
  AssertSameVectorWithoutOrder({"x", "w1", "b1", "w2", "b2"},
Q
qiaolongfei 已提交
48 49
                               net->Inputs(NetOp::kAll));
  AssertSameVectorWithoutOrder({"y", "z"}, net->Outputs(NetOp::kAll));
D
dongzhihong 已提交
50

51
  auto final_outs = net->OutputVars(false);
D
dongzhihong 已提交
52

53 54
  ASSERT_EQ(final_outs.size(), 1UL);
  ASSERT_EQ(final_outs[0], "z");
D
dongzhihong 已提交
55 56
}

Y
Yan Chunwei 已提交
57
TEST(NetOp, insert_op) {
Y
Yu Yang 已提交
58
  NetOp net;
Y
Yu Yang 已提交
59
  auto op1 = std::unique_ptr<framework::NOP>(
F
fengjiayi 已提交
60
      new framework::NOP("empty", {{"X", {"x"}}, {"W", {"w1"}}, {"b", {"b1"}}},
Y
Yiqun Liu 已提交
61
                         {{"Out", {"y"}}}, framework::AttributeMap{}));
Y
Yu Yang 已提交
62
  net.AppendOp(*op1);
Y
Yu Yang 已提交
63
  net.InsertOp(0, *op1);
Y
Yu Yang 已提交
64
  ASSERT_EQ(2UL, net.ops_.size());
Y
Yu Yang 已提交
65
  net.InsertOp(2, std::move(op1));
Y
Yu Yang 已提交
66 67
  ASSERT_EQ(3UL, net.ops_.size());
}
D
dongzhihong 已提交
68

Y
Yu Yang 已提交
69 70
TEST(NetOp, Clone) {
  NetOp net;
Y
Yiqun Liu 已提交
71 72 73 74 75 76
  net.AppendOp(std::unique_ptr<framework::NOP>(new framework::NOP{
      "empty", framework::VariableNameMap{}, framework::VariableNameMap{},
      framework::AttributeMap{}}));
  net.AppendOp(std::unique_ptr<framework::NOP>(new framework::NOP{
      "empty2", framework::VariableNameMap{}, framework::VariableNameMap{},
      framework::AttributeMap{}}));
Y
Yu Yang 已提交
77
  net.CompleteAddOp(true);
Y
Yu Yang 已提交
78
  auto new_net_op = net.Clone();
Y
Yu Yang 已提交
79 80
  ASSERT_NE(new_net_op, nullptr);
  ASSERT_TRUE(new_net_op->IsNetOp());
Y
Yu Yang 已提交
81
  auto* new_net = static_cast<NetOp*>(new_net_op.get());
Q
qiaolongfei 已提交
82
  ASSERT_EQ(2UL, new_net->ops_.size());
Y
Yu Yang 已提交
83 84 85 86
  ASSERT_EQ(new_net->ops_[0]->Type(), "empty");
  ASSERT_EQ(new_net->ops_[1]->Type(), "empty2");
}

Y
Yan Chunwei 已提交
87
}  // namespace operators
D
dongzhihong 已提交
88
}  // namespace paddle