op_registry_test.cc 5.6 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 {}
Y
Yu Yang 已提交
13
  void InferShape(const Scope& scope) const override {}
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);
    AddComment("This is cos op");
  }
};

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

class MyTestOpProtoAndCheckerMaker : public OpProtoAndCheckerMaker {
 public:
  MyTestOpProtoAndCheckerMaker(OpProto* proto, OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
Y
Yu Yang 已提交
41 42
    AddInput("input", "input of cosine op").AsDuplicable();
    AddOutput("output", "output of cosine op").AsIntermediate();
43 44 45 46 47 48 49 50 51 52 53
    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 已提交
54 55 56
static void BuildVar(const std::string& param_name,
                     std::initializer_list<const char*> arguments,
                     paddle::framework::OpDesc::Var* var) {
Y
Yu Yang 已提交
57 58
  var->set_parameter(param_name);
  for (auto& arg_name : arguments) {
Y
Yu Yang 已提交
59
    var->add_arguments(arg_name);
Y
Yu Yang 已提交
60 61
  }
}
F
fengjiayi 已提交
62 63 64 65
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 已提交
66

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

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

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

TEST(OpRegistry, IllegalAttr) {
  paddle::framework::OpDesc op_desc;
  op_desc.set_type("cos_sim");
Y
Yu Yang 已提交
90 91
  BuildVar("input", {"aa"}, op_desc.add_inputs());
  BuildVar("output", {"bb"}, op_desc.add_outputs());
92 93 94 95 96 97 98 99

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

Q
Qiao Longfei 已提交
118 119
  ASSERT_TRUE(op_desc.IsInitialized());

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

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

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