op_registry_test.cc 5.4 KB
Newer Older
1 2
#include "paddle/framework/op_registry.h"
#include <gtest/gtest.h>
Q
Qiao Longfei 已提交
3 4

using namespace paddle::framework;
5

6 7
namespace paddle {
namespace framework {
Y
Yu Yang 已提交
8
class CosineOp : public OperatorBase {
9
 public:
Y
Yu Yang 已提交
10 11 12
  void Run(const std::shared_ptr<Scope>& scope,
           const platform::DeviceContext& dev_ctx) const override {}
  void InferShape(const std::shared_ptr<Scope>& scope) const override {}
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
};

class CosineOpProtoAndCheckerMaker : public OpProtoAndCheckerMaker {
 public:
  CosineOpProtoAndCheckerMaker(OpProto* proto, OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("input", "input of cosine op");
    AddOutput("output", "output of cosine op");
    AddAttr<float>("scale", "scale of cosine op")
        .SetDefault(1.0)
        .LargerThan(0.0);
    AddType("cos");
    AddComment("This is cos op");
  }
};

Q
Qiao Longfei 已提交
29
REGISTER_OP(cos_sim, CosineOp, CosineOpProtoAndCheckerMaker);
30

Y
Yu Yang 已提交
31 32 33 34 35 36
class MyTestOp : public OperatorBase {
 public:
  void InferShape(const std::shared_ptr<Scope>& scope) const override {}
  void Run(const std::shared_ptr<Scope>& scope,
           const platform::DeviceContext& dev_ctx) const override {}

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 public:
};

class MyTestOpProtoAndCheckerMaker : public OpProtoAndCheckerMaker {
 public:
  MyTestOpProtoAndCheckerMaker(OpProto* proto, OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("input", "input of cosine op");
    AddOutput("output", "output of cosine op");
    auto my_checker = [](int i) {
      PADDLE_ENFORCE(i % 2 == 0, "'test_attr' must be even!");
    };
    AddAttr<int>("test_attr", "a simple test attribute")
        .AddCustomChecker(my_checker);
    AddType("my_test_op");
    AddComment("This is my_test op");
  }
};

Q
Qiao Longfei 已提交
56
REGISTER_OP(my_test_op, MyTestOp, MyTestOpProtoAndCheckerMaker);
57 58 59
}  // namespace framework
}  // namespace paddle

60 61 62 63 64 65
TEST(OpRegistry, CreateOp) {
  paddle::framework::OpDesc op_desc;
  op_desc.set_type("cos_sim");
  op_desc.add_inputs("aa");
  op_desc.add_outputs("bb");

Q
Qiao Longfei 已提交
66
  float scale = 3.3;
67 68 69
  auto attr = op_desc.mutable_attrs()->Add();
  attr->set_name("scale");
  attr->set_type(paddle::framework::AttrType::FLOAT);
Q
Qiao Longfei 已提交
70
  attr->set_f(scale);
71

Q
Qiao Longfei 已提交
72
  paddle::framework::OperatorBase* op =
73
      paddle::framework::OpRegistry::CreateOp(op_desc);
Q
Qiao Longfei 已提交
74
  auto scope = std::make_shared<Scope>();
Y
Yu Yang 已提交
75 76
  paddle::platform::CPUDeviceContext dev_ctx;
  op->Run(scope, dev_ctx);
Q
Qiao Longfei 已提交
77 78
  float scale_get = op->GetAttr<float>("scale");
  ASSERT_EQ(scale_get, scale);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
}

TEST(OpRegistry, IllegalAttr) {
  paddle::framework::OpDesc op_desc;
  op_desc.set_type("cos_sim");
  op_desc.add_inputs("aa");
  op_desc.add_outputs("bb");

  auto attr = op_desc.mutable_attrs()->Add();
  attr->set_name("scale");
  attr->set_type(paddle::framework::AttrType::FLOAT);
  attr->set_f(-2.0);

  bool caught = false;
  try {
Q
Qiao Longfei 已提交
94
    paddle::framework::OperatorBase* op __attribute__((unused)) =
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        paddle::framework::OpRegistry::CreateOp(op_desc);
  } catch (paddle::framework::EnforceNotMet err) {
    caught = true;
    std::string msg = "larger_than check fail";
    const char* err_msg = err.what();
    for (size_t i = 0; i < msg.length(); ++i) {
      ASSERT_EQ(err_msg[i], msg[i]);
    }
  }
  ASSERT_TRUE(caught);
}

TEST(OpRegistry, DefaultValue) {
  paddle::framework::OpDesc op_desc;
  op_desc.set_type("cos_sim");
  op_desc.add_inputs("aa");
  op_desc.add_outputs("bb");

Q
Qiao Longfei 已提交
113 114 115
  ASSERT_TRUE(op_desc.IsInitialized());

  paddle::framework::OperatorBase* op =
116
      paddle::framework::OpRegistry::CreateOp(op_desc);
Q
Qiao Longfei 已提交
117
  auto scope = std::make_shared<Scope>();
Y
Yu Yang 已提交
118 119
  paddle::platform::CPUDeviceContext dev_ctx;
  op->Run(scope, dev_ctx);
Q
Qiao Longfei 已提交
120
  ASSERT_EQ(op->GetAttr<float>("scale"), 1.0);
121 122 123 124 125 126 127 128 129 130 131
}

TEST(OpRegistry, CustomChecker) {
  paddle::framework::OpDesc op_desc;
  op_desc.set_type("my_test_op");
  op_desc.add_inputs("ii");
  op_desc.add_outputs("oo");

  // attr 'test_attr' is not set
  bool caught = false;
  try {
Q
Qiao Longfei 已提交
132
    paddle::framework::OperatorBase* op __attribute__((unused)) =
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        paddle::framework::OpRegistry::CreateOp(op_desc);
  } catch (paddle::framework::EnforceNotMet err) {
    caught = true;
    std::string msg = "Attribute 'test_attr' is required!";
    const char* err_msg = err.what();
    for (size_t i = 0; i < msg.length(); ++i) {
      ASSERT_EQ(err_msg[i], msg[i]);
    }
  }
  ASSERT_TRUE(caught);

  // set 'test_attr' set to an illegal value
  auto attr = op_desc.mutable_attrs()->Add();
  attr->set_name("test_attr");
  attr->set_type(paddle::framework::AttrType::INT);
  attr->set_i(3);
  caught = false;
  try {
Q
Qiao Longfei 已提交
151
    paddle::framework::OperatorBase* op __attribute__((unused)) =
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        paddle::framework::OpRegistry::CreateOp(op_desc);
  } catch (paddle::framework::EnforceNotMet err) {
    caught = true;
    std::string msg = "'test_attr' must be even!";
    const char* err_msg = err.what();
    for (size_t i = 0; i < msg.length(); ++i) {
      ASSERT_EQ(err_msg[i], msg[i]);
    }
  }
  ASSERT_TRUE(caught);

  // set 'test_attr' set to a legal value
  op_desc.mutable_attrs()->Clear();
  attr = op_desc.mutable_attrs()->Add();
  attr->set_name("test_attr");
  attr->set_type(paddle::framework::AttrType::INT);
  attr->set_i(4);
Q
Qiao Longfei 已提交
169
  paddle::framework::OperatorBase* op =
170
      paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
171
  paddle::platform::CPUDeviceContext dev_ctx;
Q
Qiao Longfei 已提交
172
  auto scope = std::make_shared<Scope>();
Y
Yu Yang 已提交
173
  op->Run(scope, dev_ctx);
Q
Qiao Longfei 已提交
174 175
  int test_attr = op->GetAttr<int>("test_attr");
  ASSERT_EQ(test_attr, 4);
D
dongzhihong 已提交
176
}
177 178 179 180 181

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}