op_registry_test.cc 7.1 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
  void Run(const Scope& scope,
Y
Yu Yang 已提交
11
           const platform::DeviceContext& dev_ctx) const override {}
Y
Yu Yang 已提交
12
  void InferShape(const Scope& scope) const override {}
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
};

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 已提交
28 29
class MyTestOp : public OperatorBase {
 public:
Y
Yu Yang 已提交
30 31
  void InferShape(const Scope& scope) const override {}
  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").SetMultiple();
    AddOutput("output", "output of cosine op").SetTemporary();
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 54 55 56
REGISTER_OP(cos_sim, paddle::framework::CosineOp,
            paddle::framework::CosineOpProtoAndCheckerMaker);
REGISTER_OP(my_test_op, paddle::framework::MyTestOp,
            paddle::framework::MyTestOpProtoAndCheckerMaker);

57 58 59
TEST(OpRegistry, CreateOp) {
  paddle::framework::OpDesc op_desc;
  op_desc.set_type("cos_sim");
Y
Yu Yang 已提交
60 61 62 63 64 65 66
  auto input = op_desc.add_inputs();
  input->set_op_proto_name("input");
  *input->mutable_var_names()->Add() = "aa";

  auto output = op_desc.add_outputs();
  output->set_op_proto_name("output");
  *output->mutable_var_names()->Add() = "bb";
67

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

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

TEST(OpRegistry, IllegalAttr) {
  paddle::framework::OpDesc op_desc;
  op_desc.set_type("cos_sim");
Y
Yu Yang 已提交
86 87 88 89 90 91 92
  auto input = op_desc.add_inputs();
  input->set_op_proto_name("input");
  *input->mutable_var_names()->Add() = "aa";

  auto output = op_desc.add_outputs();
  output->set_op_proto_name("output");
  *output->mutable_var_names()->Add() = "bb";
93 94 95 96 97 98 99 100

  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 已提交
101
    paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
102
  } catch (paddle::platform::EnforceNotMet err) {
103 104 105 106 107 108 109 110 111 112 113 114 115
    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 已提交
116 117 118 119 120 121 122
  auto input = op_desc.add_inputs();
  input->set_op_proto_name("input");
  *input->mutable_var_names()->Add() = "aa";

  auto output = op_desc.add_outputs();
  output->set_op_proto_name("output");
  *output->mutable_var_names()->Add() = "bb";
123

Q
Qiao Longfei 已提交
124 125
  ASSERT_TRUE(op_desc.IsInitialized());

Y
Yu Yang 已提交
126
  std::shared_ptr<paddle::framework::OperatorBase> op =
127
      paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
128
  paddle::framework::Scope scope;
Y
Yu Yang 已提交
129 130
  paddle::platform::CPUDeviceContext dev_ctx;
  op->Run(scope, dev_ctx);
Q
Qiao Longfei 已提交
131
  ASSERT_EQ(op->GetAttr<float>("scale"), 1.0);
132 133
}

134 135 136 137 138 139 140 141
static void SetInputFormat(paddle::framework::OpDesc* desc) {
  auto attr = desc->add_attrs();
  attr->set_name("input_format");
  attr->set_type(paddle::framework::INTS);
  attr->mutable_ints()->Add(0);
  attr->mutable_ints()->Add(1);
}

142 143 144
TEST(OpRegistry, CustomChecker) {
  paddle::framework::OpDesc op_desc;
  op_desc.set_type("my_test_op");
Y
Yu Yang 已提交
145 146 147 148 149 150 151
  auto input = op_desc.add_inputs();
  input->set_op_proto_name("input");
  *input->mutable_var_names()->Add() = "ii";

  auto output = op_desc.add_outputs();
  output->set_op_proto_name("output");
  *output->mutable_var_names()->Add() = "oo";
152
  SetInputFormat(&op_desc);
153 154 155 156

  // attr 'test_attr' is not set
  bool caught = false;
  try {
Y
Yu Yang 已提交
157
    paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
158
  } catch (paddle::platform::EnforceNotMet err) {
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    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 已提交
175
    paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
176
  } catch (paddle::platform::EnforceNotMet err) {
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    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);
192
  SetInputFormat(&op_desc);
Y
Yu Yang 已提交
193
  auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
194
  paddle::platform::CPUDeviceContext dev_ctx;
Y
Yu Yang 已提交
195
  paddle::framework::Scope scope;
Y
Yu Yang 已提交
196
  op->Run(scope, dev_ctx);
Q
Qiao Longfei 已提交
197 198
  int test_attr = op->GetAttr<int>("test_attr");
  ASSERT_EQ(test_attr, 4);
D
dongzhihong 已提交
199
}
200 201 202 203 204 205 206 207 208 209 210 211 212 213

class TestAttrProtoMaker : public pd::OpProtoAndCheckerMaker {
 public:
  TestAttrProtoMaker(pd::OpProto* proto, pd::OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddAttr<float>("scale", "scale of test op");
    AddAttr<float>("scale", "scale of test op");
  }
};

TEST(ProtoMaker, DuplicatedAttr) {
  pd::OpProto op_proto;
  pd::OpAttrChecker op_checker;
  auto proto_maker = TestAttrProtoMaker(&op_proto, &op_checker);
Y
Yu Yang 已提交
214
  ASSERT_THROW(proto_maker.Validate(), paddle::platform::EnforceNotMet);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
}

class TestInOutProtoMaker : public pd::OpProtoAndCheckerMaker {
 public:
  TestInOutProtoMaker(pd::OpProto* proto, pd::OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("input", "input of test op");
    AddInput("input", "input of test op");
  }
};

TEST(ProtoMaker, DuplicatedInOut) {
  pd::OpProto op_proto;
  pd::OpAttrChecker op_checker;
  auto proto_maker = TestInOutProtoMaker(&op_proto, &op_checker);
Y
Yu Yang 已提交
230
  ASSERT_THROW(proto_maker.Validate(), paddle::platform::EnforceNotMet);
231
}