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

4 5
namespace pd = paddle::framework;

6 7
namespace paddle {
namespace framework {
Y
Yu Yang 已提交
8
class CosineOp : public OperatorBase {
9
 public:
Y
Yu Yang 已提交
10
  using OperatorBase::OperatorBase;
Y
Yu Yang 已提交
11
  void Run(const Scope& scope,
Y
Yu Yang 已提交
12
           const platform::DeviceContext& dev_ctx) const override {}
13 14 15 16 17 18 19 20 21 22
};

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)
F
fengjiayi 已提交
23
        .GreaterThan(0.0);
24 25 26 27
    AddComment("This is cos op");
  }
};

Y
Yu Yang 已提交
28 29
class MyTestOp : public OperatorBase {
 public:
Y
Yu Yang 已提交
30
  using OperatorBase::OperatorBase;
Y
Yu Yang 已提交
31
  void Run(const Scope& scope,
Y
Yu Yang 已提交
32
           const platform::DeviceContext& dev_ctx) const override {}
33 34 35 36 37 38
};

class MyTestOpProtoAndCheckerMaker : public OpProtoAndCheckerMaker {
 public:
  MyTestOpProtoAndCheckerMaker(OpProto* proto, OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
Y
Yu Yang 已提交
39 40
    AddInput("input", "input of cosine op").AsDuplicable();
    AddOutput("output", "output of cosine op").AsIntermediate();
41 42 43 44 45 46 47 48 49 50 51
    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);
    AddComment("This is my_test op");
  }
};
}  // namespace framework
}  // namespace paddle

Y
Yu Yang 已提交
52 53
static void BuildVar(const std::string& param_name,
                     std::initializer_list<const char*> arguments,
54
                     paddle::framework::proto::OpDesc::Var* var) {
Y
Yu Yang 已提交
55 56
  var->set_parameter(param_name);
  for (auto& arg_name : arguments) {
Y
Yu Yang 已提交
57
    var->add_arguments(arg_name);
Y
Yu Yang 已提交
58 59
  }
}
F
fengjiayi 已提交
60 61 62 63
REGISTER_OP_WITHOUT_GRADIENT(cos_sim, paddle::framework::CosineOp,
                             paddle::framework::CosineOpProtoAndCheckerMaker);
REGISTER_OP_WITHOUT_GRADIENT(my_test_op, paddle::framework::MyTestOp,
                             paddle::framework::MyTestOpProtoAndCheckerMaker);
Y
Yu Yang 已提交
64

65
TEST(OpRegistry, CreateOp) {
66
  paddle::framework::proto::OpDesc op_desc;
67
  op_desc.set_type("cos_sim");
Y
Yu Yang 已提交
68 69
  BuildVar("input", {"aa"}, op_desc.add_inputs());
  BuildVar("output", {"bb"}, op_desc.add_outputs());
70

Q
Qiao Longfei 已提交
71
  float scale = 3.3;
72 73
  auto attr = op_desc.mutable_attrs()->Add();
  attr->set_name("scale");
74
  attr->set_type(paddle::framework::proto::AttrType::FLOAT);
Q
Qiao Longfei 已提交
75
  attr->set_f(scale);
76

77
  auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
78
  paddle::framework::Scope scope;
Y
Yu Yang 已提交
79 80
  paddle::platform::CPUDeviceContext dev_ctx;
  op->Run(scope, dev_ctx);
Y
Yu Yang 已提交
81
  float scale_get = op->Attr<float>("scale");
Q
Qiao Longfei 已提交
82
  ASSERT_EQ(scale_get, scale);
83 84 85
}

TEST(OpRegistry, IllegalAttr) {
86
  paddle::framework::proto::OpDesc op_desc;
87
  op_desc.set_type("cos_sim");
Y
Yu Yang 已提交
88 89
  BuildVar("input", {"aa"}, op_desc.add_inputs());
  BuildVar("output", {"bb"}, op_desc.add_outputs());
90 91 92

  auto attr = op_desc.mutable_attrs()->Add();
  attr->set_name("scale");
93
  attr->set_type(paddle::framework::proto::AttrType::FLOAT);
94 95 96 97
  attr->set_f(-2.0);

  bool caught = false;
  try {
98
    paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
99
  } catch (paddle::platform::EnforceNotMet err) {
100 101 102 103 104 105 106 107 108 109 110
    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) {
111
  paddle::framework::proto::OpDesc op_desc;
112
  op_desc.set_type("cos_sim");
Y
Yu Yang 已提交
113 114
  BuildVar("input", {"aa"}, op_desc.add_inputs());
  BuildVar("output", {"bb"}, op_desc.add_outputs());
115

Q
Qiao Longfei 已提交
116 117
  ASSERT_TRUE(op_desc.IsInitialized());

118
  auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
119
  paddle::framework::Scope scope;
Y
Yu Yang 已提交
120 121
  paddle::platform::CPUDeviceContext dev_ctx;
  op->Run(scope, dev_ctx);
Y
Yu Yang 已提交
122
  ASSERT_EQ(op->Attr<float>("scale"), 1.0);
123 124 125
}

TEST(OpRegistry, CustomChecker) {
126
  paddle::framework::proto::OpDesc op_desc;
127
  op_desc.set_type("my_test_op");
Y
Yu Yang 已提交
128 129
  BuildVar("input", {"ii"}, op_desc.add_inputs());
  BuildVar("output", {"oo"}, op_desc.add_outputs());
130 131 132 133

  // attr 'test_attr' is not set
  bool caught = false;
  try {
134
    paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
135
  } catch (paddle::platform::EnforceNotMet err) {
136 137 138 139 140 141 142 143 144 145 146 147
    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");
148
  attr->set_type(paddle::framework::proto::AttrType::INT);
149 150 151
  attr->set_i(3);
  caught = false;
  try {
152
    paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
153
  } catch (paddle::platform::EnforceNotMet err) {
154 155 156 157 158 159 160 161 162 163 164 165 166
    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");
167
  attr->set_type(paddle::framework::proto::AttrType::INT);
168
  attr->set_i(4);
169
  auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
170
  paddle::platform::CPUDeviceContext dev_ctx;
Y
Yu Yang 已提交
171
  paddle::framework::Scope scope;
Y
Yu Yang 已提交
172
  op->Run(scope, dev_ctx);
Y
Yu Yang 已提交
173
  int test_attr = op->Attr<int>("test_attr");
Q
Qiao Longfei 已提交
174
  ASSERT_EQ(test_attr, 4);
Q
Qiao Longfei 已提交
175
}
176 177 178 179 180 181 182 183 184 185

class CosineOpComplete : public paddle::framework::CosineOp {
 public:
  DEFINE_OP_CONSTRUCTOR(CosineOpComplete, paddle::framework::CosineOp);
  DEFINE_OP_CLONE_METHOD(CosineOpComplete);
};

TEST(OperatorRegistrar, Test) {
  using namespace paddle::framework;
  OperatorRegistrar<CosineOpComplete, CosineOpProtoAndCheckerMaker> reg("cos");
186
}