提交 14424f31 编写于 作者: D dongzhihong

"use built-in operator"

上级 4876f358
...@@ -29,4 +29,4 @@ add_dependencies(framework_py_proto framework_py_proto_init) ...@@ -29,4 +29,4 @@ add_dependencies(framework_py_proto framework_py_proto_init)
proto_library(net_proto SRCS net_proto.proto DEPS op_proto) proto_library(net_proto SRCS net_proto.proto DEPS op_proto)
# cc_library(net SRCS net.cc DEPS operator net_proto op_registry fc_op) # cc_library(net SRCS net.cc DEPS operator net_proto op_registry fc_op)
cc_library(net SRCS net.cc DEPS operator net_proto op_registry) cc_library(net SRCS net.cc DEPS operator net_proto op_registry)
cc_test(net_op_test SRCS net_op_test.cc DEPS net my_fc_op) cc_test(net_op_test SRCS net_op_test.cc DEPS net add_op mul_op sigmoid_op)
...@@ -2,7 +2,10 @@ ...@@ -2,7 +2,10 @@
#include <paddle/framework/net.h> #include <paddle/framework/net.h>
#include <paddle/framework/op_registry.h> #include <paddle/framework/op_registry.h>
#include <paddle/framework/operator.h> #include <paddle/framework/operator.h>
#include "paddle/framework/fully_connected_op.h"
USE_OP(add_two);
USE_OP(mul);
USE_OP(sigmoid);
namespace paddle { namespace paddle {
namespace framework { namespace framework {
...@@ -65,14 +68,18 @@ TEST(OpKernel, all) { ...@@ -65,14 +68,18 @@ TEST(OpKernel, all) {
ASSERT_THROW(net->AddOp(op2), EnforceNotMet); ASSERT_THROW(net->AddOp(op2), EnforceNotMet);
} }
TEST(AddBackwardOp, TestGradOp) { TEST(AddBackwardOp, TestGradOp) {
auto net = std::make_shared<PlainNet>(); auto net = std::make_shared<PlainNet>();
ASSERT_NE(net, nullptr); ASSERT_NE(net, nullptr);
auto op1 = std::make_shared<FCOp>(); net->AddOp(framework::OpRegistry::CreateOp("mul", {"X", "Y"}, {"Out"}, {}));
op1->inputs_ = {"x", "w1", "b1"}; net->AddOp(
op1->outputs_ = {"y"}; framework::OpRegistry::CreateOp("add_two", {"X", "Y"}, {"Out"}, {}));
net->AddOp(op1); net->AddOp(framework::OpRegistry::CreateOp("add_two", {"X", "Y"}, {""}, {}));
// net->AddOp(framework::OpRegistry::CreateOp("fc"), {
// Input("X"), Input("W"), Input("b")},
// {Output("Y")},
// {}
// );
auto grad_ops = AddBackwardOp(net); auto grad_ops = AddBackwardOp(net);
for (auto& op : grad_ops->ops_) { for (auto& op : grad_ops->ops_) {
op->DebugString(); op->DebugString();
......
...@@ -470,11 +470,11 @@ class GradOpRegisterHelper { ...@@ -470,11 +470,11 @@ class GradOpRegisterHelper {
*/ */
#define REGISTER_GRADIENT_OP(__op_type, __op_class) \ #define REGISTER_GRADIENT_OP(__op_type, __op_class) \
STATIC_ASSERT_GLOBAL_NAMESPACE( \ STATIC_ASSERT_GLOBAL_NAMESPACE( \
__reg_op__##__op_type, \ __reg_gradient_op__##__op_type, \
"REGISTER_GRADIENT_OP must be in global namespace"); \ "REGISTER_GRADIENT_OP must be in global namespace"); \
static ::paddle::framework::GradOpRegisterHelper<__op_class> \ static ::paddle::framework::GradOpRegisterHelper<__op_class> \
__op_register_##__op_type##__(#__op_type); \ __op_gradient_register_##__op_type##__(#__op_type); \
int __op_register_##__op_type##_handle__() { return 0; } int __op_gradient_register_##__op_type##_handle__() { return 0; }
/** /**
* Macro to Register OperatorKernel. * Macro to Register OperatorKernel.
......
...@@ -49,10 +49,25 @@ The equation is: Out = X + Y ...@@ -49,10 +49,25 @@ The equation is: Out = X + Y
)DOC"); )DOC");
} }
}; };
class AddOpGrad : public framework::OperatorWithKernel {
protected:
void InferShape(
const std::vector<const framework::Tensor *> &inputs,
const std::vector<framework::Tensor *> &outputs) const override {}
std::string DebugString() const override {
LOG(INFO) << "AddOpGrad";
return "";
}
};
} // namespace operators } // namespace operators
} // namespace paddle } // namespace paddle
REGISTER_OP(add_two, paddle::operators::AddOp, paddle::operators::AddOpMaker); REGISTER_OP(add_two, paddle::operators::AddOp, paddle::operators::AddOpMaker);
REGISTER_GRADIENT_OP(add_two, paddle::operators::AddOpGrad);
typedef paddle::operators::AddKernel<::paddle::platform::CPUPlace, float> typedef paddle::operators::AddKernel<::paddle::platform::CPUPlace, float>
AddKernel_CPU_float; AddKernel_CPU_float;
REGISTER_OP_CPU_KERNEL(add_two, AddKernel_CPU_float); REGISTER_OP_CPU_KERNEL(add_two, AddKernel_CPU_float);
// REGISTER_OP_CPU_KERNEL(add_two, AddKernel_CPU_float);
...@@ -16,8 +16,13 @@ limitations under the License. */ ...@@ -16,8 +16,13 @@ limitations under the License. */
#define private public #define private public
#include <paddle/framework/op_registry.h> #include <paddle/framework/op_registry.h>
USE_OP(add_two); USE_OP(add_two);
// USE_OP(add_two_grad);
TEST(AddOp, GetOpProto) { TEST(AddOp, GetOpProto) {
auto& protos = paddle::framework::OpRegistry::protos(); auto& protos = paddle::framework::OpRegistry::protos();
auto it = protos.find("add_two"); auto it = protos.find("add_two");
ASSERT_NE(it, protos.end()); ASSERT_NE(it, protos.end());
} auto& grad_creators = paddle::framework::OpRegistry::grad_creators();
\ No newline at end of file auto it1 = grad_creators.find("add_two");
ASSERT_NE(it1, grad_creators.end());
}
...@@ -52,9 +52,22 @@ The equation is: Out = X * Y ...@@ -52,9 +52,22 @@ The equation is: Out = X * Y
} }
}; };
class MulOpGrad : public framework::OperatorWithKernel {
protected:
void InferShape(
const std::vector<const framework::Tensor *> &inputs,
const std::vector<framework::Tensor *> &outputs) const override {}
std::string DebugString() const override {
LOG(INFO) << "MulGrad";
return "";
}
};
} // namespace operators } // namespace operators
} // namespace paddle } // namespace paddle
REGISTER_OP(mul, paddle::operators::MulOp, paddle::operators::MulOpMaker); REGISTER_OP(mul, paddle::operators::MulOp, paddle::operators::MulOpMaker);
REGISTER_GRADIENT_OP(mul, paddle::operators::MulOpGrad);
REGISTER_OP_CPU_KERNEL( REGISTER_OP_CPU_KERNEL(
mul, paddle::operators::MulKernel<paddle::platform::CPUPlace>); mul, paddle::operators::MulKernel<paddle::platform::CPUPlace>);
...@@ -39,11 +39,24 @@ public: ...@@ -39,11 +39,24 @@ public:
} }
}; };
class SigmoidOpGrad : public framework::OperatorWithKernel {
protected:
void InferShape(
const std::vector<const framework::Tensor *> &inputs,
const std::vector<framework::Tensor *> &outputs) const override {}
std::string DebugString() const override {
LOG(INFO) << "SigmoidGrad";
return "";
}
};
} // namespace operators } // namespace operators
} // namespace paddle } // namespace paddle
REGISTER_OP(sigmoid, REGISTER_OP(sigmoid,
paddle::operators::SigmoidOp, paddle::operators::SigmoidOp,
paddle::operators::SigmoidOpMaker); paddle::operators::SigmoidOpMaker);
REGISTER_GRADIENT_OP(sigmoid, paddle::operators::SigmoidOpGrad);
REGISTER_OP_CPU_KERNEL( REGISTER_OP_CPU_KERNEL(
sigmoid, paddle::operators::SigmoidKernel<paddle::platform::CPUPlace>); sigmoid, paddle::operators::SigmoidKernel<paddle::platform::CPUPlace>);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册