op_registry_test.cc 11.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

D
dzhwinter 已提交
15
#include <glog/logging.h>
16
#include <gtest/gtest.h>
Q
Qiao Longfei 已提交
17

Y
Yi Wang 已提交
18
#include "paddle/fluid/framework/op_registry.h"
D
dzhwinter 已提交
19

20 21
namespace pd = paddle::framework;

22 23
namespace paddle {
namespace framework {
D
dzhwinter 已提交
24

Y
Yu Yang 已提交
25
class CosineOp : public OperatorBase {
26
 public:
Y
Yu Yang 已提交
27
  using OperatorBase::OperatorBase;
28 29 30 31

 private:
  void RunImpl(const Scope& scope,
               const platform::Place& place) const override {}
32 33 34 35
};

class CosineOpProtoAndCheckerMaker : public OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
36
  void Make() {
37 38 39 40
    AddInput("input", "input of cosine op");
    AddOutput("output", "output of cosine op");
    AddAttr<float>("scale", "scale of cosine op")
        .SetDefault(1.0)
F
fengjiayi 已提交
41
        .GreaterThan(0.0);
42 43 44 45
    AddComment("This is cos op");
  }
};

Y
Yu Yang 已提交
46 47
class MyTestOp : public OperatorBase {
 public:
Y
Yu Yang 已提交
48
  using OperatorBase::OperatorBase;
49 50 51 52

 private:
  void RunImpl(const Scope& scope,
               const platform::Place& place) const override {}
53 54 55 56
};

class MyTestOpProtoAndCheckerMaker : public OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
57
  void Make() {
Y
Yu Yang 已提交
58 59
    AddInput("input", "input of cosine op").AsDuplicable();
    AddOutput("output", "output of cosine op").AsIntermediate();
60 61 62 63 64 65 66 67 68 69 70
    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 已提交
71 72
static void BuildVar(const std::string& param_name,
                     std::initializer_list<const char*> arguments,
73
                     paddle::framework::proto::OpDesc::Var* var) {
Y
Yu Yang 已提交
74 75
  var->set_parameter(param_name);
  for (auto& arg_name : arguments) {
Y
Yu Yang 已提交
76
    var->add_arguments(arg_name);
Y
Yu Yang 已提交
77 78
  }
}
F
fengjiayi 已提交
79 80 81 82
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 已提交
83

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

Q
Qiao Longfei 已提交
90
  float scale = 3.3;
91 92
  auto attr = op_desc.mutable_attrs()->Add();
  attr->set_name("scale");
93
  attr->set_type(paddle::framework::proto::AttrType::FLOAT);
Q
Qiao Longfei 已提交
94
  attr->set_f(scale);
95

96
  auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
97
  paddle::framework::Scope scope;
D
dzhwinter 已提交
98 99
  paddle::platform::CPUPlace cpu_place;
  op->Run(scope, cpu_place);
Y
Yu Yang 已提交
100
  float scale_get = op->Attr<float>("scale");
Q
Qiao Longfei 已提交
101
  ASSERT_EQ(scale_get, scale);
102 103 104
}

TEST(OpRegistry, IllegalAttr) {
105
  paddle::framework::proto::OpDesc op_desc;
106
  op_desc.set_type("cos_sim");
Y
Yu Yang 已提交
107 108
  BuildVar("input", {"aa"}, op_desc.add_inputs());
  BuildVar("output", {"bb"}, op_desc.add_outputs());
109 110 111

  auto attr = op_desc.mutable_attrs()->Add();
  attr->set_name("scale");
112
  attr->set_type(paddle::framework::proto::AttrType::FLOAT);
113 114 115 116
  attr->set_f(-2.0);

  bool caught = false;
  try {
117
    paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
118
  } catch (paddle::platform::EnforceNotMet err) {
119 120
    caught = true;
    std::string msg = "larger_than check fail";
121 122
    std::string err_msg = err.what();
    ASSERT_TRUE(err_msg.find(msg) != std::string::npos);
123 124 125 126 127
  }
  ASSERT_TRUE(caught);
}

TEST(OpRegistry, DefaultValue) {
128
  paddle::framework::proto::OpDesc op_desc;
129
  op_desc.set_type("cos_sim");
Y
Yu Yang 已提交
130 131
  BuildVar("input", {"aa"}, op_desc.add_inputs());
  BuildVar("output", {"bb"}, op_desc.add_outputs());
132

Q
Qiao Longfei 已提交
133 134
  ASSERT_TRUE(op_desc.IsInitialized());

135
  auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
136
  paddle::framework::Scope scope;
D
dzhwinter 已提交
137 138
  paddle::platform::CPUPlace cpu_place;
  op->Run(scope, cpu_place);
Y
Yu Yang 已提交
139
  ASSERT_EQ(op->Attr<float>("scale"), 1.0);
140 141 142
}

TEST(OpRegistry, CustomChecker) {
143
  paddle::framework::proto::OpDesc op_desc;
144
  op_desc.set_type("my_test_op");
Y
Yu Yang 已提交
145 146
  BuildVar("input", {"ii"}, op_desc.add_inputs());
  BuildVar("output", {"oo"}, op_desc.add_outputs());
147 148 149 150

  // attr 'test_attr' is not set
  bool caught = false;
  try {
151
    paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
152
  } catch (paddle::platform::EnforceNotMet err) {
153 154
    caught = true;
    std::string msg = "Attribute 'test_attr' is required!";
155 156
    std::string err_msg = err.what();
    ASSERT_TRUE(err_msg.find(msg) != std::string::npos);
157 158 159 160 161 162
  }
  ASSERT_TRUE(caught);

  // set 'test_attr' set to an illegal value
  auto attr = op_desc.mutable_attrs()->Add();
  attr->set_name("test_attr");
163
  attr->set_type(paddle::framework::proto::AttrType::INT);
164 165 166
  attr->set_i(3);
  caught = false;
  try {
167
    paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yu Yang 已提交
168
  } catch (paddle::platform::EnforceNotMet err) {
169 170
    caught = true;
    std::string msg = "'test_attr' must be even!";
171 172
    std::string err_msg = err.what();
    ASSERT_TRUE(err_msg.find(msg) != std::string::npos);
173 174 175 176 177 178 179
  }
  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");
180
  attr->set_type(paddle::framework::proto::AttrType::INT);
181
  attr->set_i(4);
182
  auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
D
dzhwinter 已提交
183
  paddle::platform::CPUPlace cpu_place;
Y
Yu Yang 已提交
184
  paddle::framework::Scope scope;
D
dzhwinter 已提交
185
  op->Run(scope, cpu_place);
Y
Yu Yang 已提交
186
  int test_attr = op->Attr<int>("test_attr");
Q
Qiao Longfei 已提交
187
  ASSERT_EQ(test_attr, 4);
Q
Qiao Longfei 已提交
188
}
189 190

TEST(OperatorRegistrar, Test) {
191
  paddle::framework::OperatorRegistrar<
Y
yuyang18 已提交
192 193
      paddle::framework::CosineOp,
      paddle::framework::CosineOpProtoAndCheckerMaker>
194
      reg("cos");
195
}
D
dzhwinter 已提交
196 197 198 199 200 201

namespace paddle {
namespace framework {

class OpKernelTestMaker : public OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
202
  void Make() { AddComment("NoGradOp, same input output. no Grad"); }
D
dzhwinter 已提交
203 204 205 206 207 208 209 210 211
};

class OpWithKernelTest : public OperatorWithKernel {
 public:
  using OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(InferShapeContext* ctx) const override {}

212
  framework::OpKernelType GetExpectedKernelType(
D
dzhwinter 已提交
213
      const framework::ExecutionContext& ctx) const override {
214
    return framework::OpKernelType(proto::VarType::FP32, ctx.device_context());
D
dzhwinter 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
  }
};

template <typename DeviceContext, typename T>
class OpKernelTest : public paddle::framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const {}
};

}  // namespace framework
}  // namespace paddle

REGISTER_OP_WITHOUT_GRADIENT(op_with_kernel,
                             paddle::framework::OpWithKernelTest,
                             paddle::framework::OpKernelTestMaker);
REGISTER_OP_CPU_KERNEL(
    op_with_kernel,
    paddle::framework::OpKernelTest<paddle::platform::CPUDeviceContext, float>);

REGISTER_OP_CUDA_KERNEL(op_with_kernel,
                        paddle::framework::OpKernelTest<
                            paddle::platform::CUDADeviceContext, float>);

TEST(OperatorRegistrar, CPU) {
  paddle::framework::proto::OpDesc op_desc;
  paddle::platform::CPUPlace cpu_place;
  paddle::framework::Scope scope;

  op_desc.set_type("op_with_kernel");
  auto op = paddle::framework::OpRegistry::CreateOp(op_desc);

  op->Run(scope, cpu_place);
}

TEST(OperatorRegistrar, CUDA) {
  paddle::framework::proto::OpDesc op_desc;
  paddle::platform::CUDAPlace cuda_place(0);
  paddle::framework::Scope scope;

  op_desc.set_type("op_with_kernel");
  auto op = paddle::framework::OpRegistry::CreateOp(op_desc);

  op->Run(scope, cuda_place);
}
D
dzhwinter 已提交
259 260 261 262 263

static int op_test_value = 0;

using paddle::platform::CPUDeviceContext;
using paddle::platform::CUDADeviceContext;
Y
Yu Yang 已提交
264
using paddle::platform::DeviceContext;
D
dzhwinter 已提交
265 266 267 268 269 270 271 272 273 274 275 276

namespace paddle {
namespace framework {

class OpWithMultiKernelTest : public OperatorWithKernel {
 public:
  using OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(InferShapeContext* ctx) const override {}

  framework::OpKernelType GetExpectedKernelType(
277
      const framework::ExecutionContext& ctx) const override {
278 279 280
    return framework::OpKernelType(proto::VarType::FP32, platform::CUDAPlace(0),
                                   DataLayout::kAnyLayout,
                                   framework::LibraryType::kCUDNN);
D
dzhwinter 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
  }
};

template <typename DeviceContext, typename T>
class OpMultiKernelTest : public paddle::framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const;
};

template <typename T>
class OpMultiKernelTest<CPUDeviceContext, T>
    : public paddle::framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const {
    ++op_test_value;
  }
};

template <typename T>
class OpMultiKernelTest<CUDADeviceContext, T>
    : public paddle::framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const {
    --op_test_value;
  }
};

template <typename DeviceContext, typename T>
class OpMultiKernelTest2 : public paddle::framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const;
};

template <typename T>
class OpMultiKernelTest2<CPUDeviceContext, T>
    : public paddle::framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const {
    op_test_value += 10;
  }
};

template <typename T>
class OpMultiKernelTest2<CUDADeviceContext, T>
    : public paddle::framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const {
    op_test_value -= 10;
  }
};

}  // namespace framework
}  // namespace paddle

REGISTER_OP_WITHOUT_GRADIENT(op_with_multi_kernel,
                             paddle::framework::OpWithMultiKernelTest,
                             paddle::framework::OpKernelTestMaker);
REGISTER_OP_KERNEL(
    op_with_multi_kernel, CPU, paddle::platform::CPUPlace,
    paddle::framework::OpMultiKernelTest<CPUDeviceContext, float>);
REGISTER_OP_KERNEL(
    op_with_multi_kernel, MKLDNN, paddle::platform::CPUPlace,
    paddle::framework::OpMultiKernelTest2<CPUDeviceContext, float>);
REGISTER_OP_KERNEL(
    op_with_multi_kernel, CUDA, paddle::platform::CUDAPlace,
    paddle::framework::OpMultiKernelTest<CUDADeviceContext, float>);
REGISTER_OP_KERNEL(
    op_with_multi_kernel, CUDNN, paddle::platform::CUDAPlace,
    paddle::framework::OpMultiKernelTest2<CUDADeviceContext, float>);

TEST(OperatorRegistrar, OpWithMultiKernel) {
  paddle::framework::proto::OpDesc op_desc;
  paddle::platform::CUDAPlace cuda_place(0);
  paddle::platform::CPUPlace cpu_place;
  paddle::framework::Scope scope;

  op_desc.set_type("op_with_multi_kernel");
  auto op = paddle::framework::OpRegistry::CreateOp(op_desc);

360
  // TODO(qiao) add priority back
D
dzhwinter 已提交
361 362 363 364
  // use all available kernels
  op->Run(scope, cuda_place);
  EXPECT_EQ(op_test_value, -10);
}