net_op_test.cc 2.3 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 12

static int infer_shape_cnt = 0;
static int run_cnt = 0;

D
dongzhihong 已提交
13
class TestOp : public framework::OperatorBase {
14
 public:
D
dongzhihong 已提交
15 16 17
  void InferShape(const Scope& scope) const override { ++infer_shape_cnt; }
  void Run(const Scope& scope,
           const platform::DeviceContext& dev_ctx) const override {
Q
Qiao Longfei 已提交
18 19 20 21
    ++run_cnt;
  }
};

D
dongzhihong 已提交
22
class EmptyOp : public framework::OperatorBase {
23
 public:
Y
Yu Yang 已提交
24
  void InferShape(const Scope& scope) const override {}
D
dongzhihong 已提交
25
  void Run(const Scope& scope, const DeviceContext& dev_ctx) const override {}
Y
Yu Yang 已提交
26 27
};

Q
Qiao Longfei 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41
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 已提交
42
  auto net = std::make_shared<NetOp>();
D
dongzhihong 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  ASSERT_NE(net, nullptr);

  auto op1 = std::make_shared<TestOp>();
  op1->inputs_ = {"x", "w1", "b1"};
  op1->outputs_ = {"y"};
  net->AddOp(op1);

  auto op2 = std::make_shared<TestOp>();
  op2->inputs_ = {"y", "w2", "b2"};
  op2->outputs_ = {"z"};
  net->AddOp(op2);

  net->CompleteAddOp();
  AssertSameVectorWithoutOrder({"x", "w1", "b1", "w2", "b2"}, net->inputs_);
  AssertSameVectorWithoutOrder({"y", "z"}, net->outputs_);
  auto tmp_idx_iter = net->attrs_.find("temporary_index");
  ASSERT_NE(net->attrs_.end(), tmp_idx_iter);
  auto& tmp_idx = boost::get<std::vector<int>>(tmp_idx_iter->second);
  ASSERT_EQ(1UL, tmp_idx.size());
  ASSERT_EQ("y", net->outputs_[tmp_idx[0]]);

Y
Yu Yang 已提交
64
  Scope scope;
D
dongzhihong 已提交
65 66 67 68 69 70
  platform::CPUDeviceContext dev_ctx;

  net->InferShape(scope);
  net->Run(scope, dev_ctx);
  ASSERT_EQ(2, infer_shape_cnt);
  ASSERT_EQ(2, run_cnt);
D
dongzhihong 已提交
71
  ASSERT_THROW(net->AddOp(op2), platform::EnforceNotMet);
D
dongzhihong 已提交
72 73
}

Y
Yan Chunwei 已提交
74
TEST(NetOp, insert_op) {
Y
Yu Yang 已提交
75 76 77 78 79 80 81 82 83 84
  NetOp net;
  auto op1 = std::make_shared<EmptyOp>();
  op1->inputs_ = {"x", "w1", "b1"};
  op1->outputs_ = {"y"};
  net.AddOp(op1);
  net.InsertOp(0, op1);
  ASSERT_EQ(2UL, net.ops_.size());
  net.InsertOp(2, op1);
  ASSERT_EQ(3UL, net.ops_.size());
}
D
dongzhihong 已提交
85

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