diff --git a/paddle/operators/.clang-format b/paddle/operators/.clang-format new file mode 100644 index 0000000000000000000000000000000000000000..47b8a85206ab457e2b3cb90a68b7a82a0753d327 --- /dev/null +++ b/paddle/operators/.clang-format @@ -0,0 +1,5 @@ +--- +Language: Cpp +BasedOnStyle: Google +Standard: Cpp11 +... diff --git a/paddle/operators/add_op.cc b/paddle/operators/add_op.cc index 85269a5f7445a1745d9be68417789e33eb725d5c..7fbdd84a391c7d0048fca473f7318561df50daa2 100644 --- a/paddle/operators/add_op.cc +++ b/paddle/operators/add_op.cc @@ -18,7 +18,7 @@ namespace paddle { namespace operators { class AddOp : public OperatorWithKernel { -protected: + protected: void InferShape(const InferShapeContext &ctx) const override { PADDLE_ENFORCE(ctx.InputSize() == 2, "Input size of AddOp must be two"); PADDLE_ENFORCE(ctx.OutputSize() == 1, "Output size of AddOp must be one"); @@ -33,7 +33,7 @@ protected: }; class AddOpMaker : public OpProtoAndCheckerMaker { -public: + public: AddOpMaker(OpProto *proto, OpAttrChecker *op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { AddInput("X", "The first input of add op"); @@ -48,7 +48,7 @@ The equation is: Out = X + Y }; class AddOpGrad : public OperatorWithKernel { -protected: + protected: void InferShape(const InferShapeContext &ctx) const override {} }; diff --git a/paddle/operators/add_op.h b/paddle/operators/add_op.h index 54d2231425293f6cfb3adc9cb34d903a75fcdcd0..9db19a61381fdb11350276d51d3ebbf083672022 100644 --- a/paddle/operators/add_op.h +++ b/paddle/operators/add_op.h @@ -20,7 +20,7 @@ namespace operators { template class AddKernel : public OpKernel { -public: + public: void Compute(const ExecutionContext& context) const override { auto input0 = context.Input(0); auto input1 = context.Input(1); diff --git a/paddle/operators/cross_entropy_op.cc b/paddle/operators/cross_entropy_op.cc index 4f5b935fde4d5b0d9efae66554cf890291e26941..4cf4e8e2be2fd7f18079406b838d2757317c4ffb 100644 --- a/paddle/operators/cross_entropy_op.cc +++ b/paddle/operators/cross_entropy_op.cc @@ -18,7 +18,7 @@ namespace paddle { namespace operators { class OnehotCrossEntropyOp : public OperatorWithKernel { -protected: + protected: void InferShape(const InferShapeContext &ctx) const override { PADDLE_ENFORCE(ctx.InputSize() == 2, "Input size of OnehotCrossEntropyOp must be two"); @@ -37,7 +37,7 @@ protected: }; class OnehotCrossEntropyOpMaker : public OpProtoAndCheckerMaker { -public: + public: OnehotCrossEntropyOpMaker(OpProto *proto, OpAttrChecker *op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { AddInput("X", "The first input of OnehotCrossEntropyOp"); @@ -54,8 +54,7 @@ OnehotCrossEntropy Operator. } // namespace operators } // namespace paddle -REGISTER_OP(onehot_cross_entropy, - ops::OnehotCrossEntropyOp, +REGISTER_OP(onehot_cross_entropy, ops::OnehotCrossEntropyOp, ops::OnehotCrossEntropyOpMaker); REGISTER_OP_CPU_KERNEL(onehot_cross_entropy, ops::OnehotCrossEntropyOpKernel); diff --git a/paddle/operators/cross_entropy_op.h b/paddle/operators/cross_entropy_op.h index c3a3728149950a5c7f2195122e8e0ff728492bdb..7f7fb8d269df7eb46086ae143929d0bf6b808575 100644 --- a/paddle/operators/cross_entropy_op.h +++ b/paddle/operators/cross_entropy_op.h @@ -20,7 +20,7 @@ namespace operators { template class OnehotCrossEntropyOpKernel : public OpKernel { -public: + public: constexpr T LOG_THRESHOLD() const { return static_cast(1e-20); } void Compute(const ExecutionContext& ctx) const override { diff --git a/paddle/operators/fc_op.cc b/paddle/operators/fc_op.cc index bd2c70c038188663f2e552b05b20ebb61256a0bf..b5cf236bac6bb5abe061f7b4ad469d20e0af76a9 100644 --- a/paddle/operators/fc_op.cc +++ b/paddle/operators/fc_op.cc @@ -18,31 +18,29 @@ namespace paddle { namespace operators { class FullyConnectedOp : public NetOp { -public: + public: void Init() override { AddOp(OpRegistry::CreateOp("mul", { Input("X"), Input("W"), }, - {Output("before_act")}, - {})); + {Output("before_act")}, {})); auto b = Input("b"); if (b != framework::kEmptyVarName) { AddOp(OpRegistry::CreateOp("rowwise_add", {Output("before_act"), Input("b")}, - {Output("before_act")}, - {})); + {Output("before_act")}, {})); } auto activation = GetAttr("activation"); - AddOp(OpRegistry::CreateOp( - activation, {Output("before_act")}, {Output("Y")}, {})); + AddOp(OpRegistry::CreateOp(activation, {Output("before_act")}, + {Output("Y")}, {})); CompleteAddOp(false); } }; class FullyConnectedOpMaker : public OpProtoAndCheckerMaker { -public: + public: FullyConnectedOpMaker(OpProto *proto, OpAttrChecker *op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { AddInput("X", "the input of fc operator"); diff --git a/paddle/operators/fill_zeros_like_op.cc b/paddle/operators/fill_zeros_like_op.cc index 79a0e3d7e911b728a7a96ceff573976ba2b2e37f..3d37d64c5a8c288684122f3e686262399d32ed7b 100644 --- a/paddle/operators/fill_zeros_like_op.cc +++ b/paddle/operators/fill_zeros_like_op.cc @@ -20,7 +20,7 @@ namespace paddle { namespace operators { class FillZerosLikeOp : public framework::OperatorWithKernel { -protected: + protected: void InferShape(const framework::InferShapeContext &ctx) const override { PADDLE_ENFORCE(ctx.InputSize() == 1UL, "Input size of FillZerosLikeOp must be one."); @@ -36,7 +36,7 @@ protected: }; class FillZerosLikeOpMaker : public framework::OpProtoAndCheckerMaker { -public: + public: FillZerosLikeOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker) : framework::OpProtoAndCheckerMaker(proto, op_checker) { @@ -52,8 +52,7 @@ The output will have the same size with input. } // namespace operators } // namespace paddle -REGISTER_OP(fill_zeros_like, - paddle::operators::FillZerosLikeOp, +REGISTER_OP(fill_zeros_like, paddle::operators::FillZerosLikeOp, paddle::operators::FillZerosLikeOpMaker); REGISTER_OP_CPU_KERNEL( fill_zeros_like, diff --git a/paddle/operators/fill_zeros_like_op.h b/paddle/operators/fill_zeros_like_op.h index 05272964abd43bdc2bd5c3cae8b128099e1c888c..4bff1fbfc15af1f4d1ce9c99fe48b0b0f11b5b3f 100644 --- a/paddle/operators/fill_zeros_like_op.h +++ b/paddle/operators/fill_zeros_like_op.h @@ -22,7 +22,7 @@ namespace operators { template class FillZerosLikeKernel : public framework::OpKernel { -public: + public: void Compute(const framework::ExecutionContext& context) const override { auto* output = context.Output(0); output->mutable_data(context.GetPlace()); diff --git a/paddle/operators/mean_op.cc b/paddle/operators/mean_op.cc index aeef0c0eaf7ec51d2e6f10f8cb80d9adf023ffbb..8a4981c7be7587a0cc5f72cabe71e05702112ac3 100644 --- a/paddle/operators/mean_op.cc +++ b/paddle/operators/mean_op.cc @@ -18,7 +18,7 @@ namespace paddle { namespace operators { class MeanOp : public OperatorWithKernel { -protected: + protected: void InferShape(const InferShapeContext &ctx) const override { PADDLE_ENFORCE(ctx.InputSize() == 1, "Input size of AddOp must be one"); PADDLE_ENFORCE(ctx.OutputSize() == 1, "Output size of AddOp must be one"); @@ -29,7 +29,7 @@ protected: }; class MeanOpMaker : public OpProtoAndCheckerMaker { -public: + public: MeanOpMaker(OpProto *proto, OpAttrChecker *op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { AddInput("X", "The input of mean op"); @@ -39,7 +39,7 @@ public: }; class MeanGradOp : public OperatorWithKernel { -protected: + protected: void InferShape(const InferShapeContext &ctx) const override { ctx.Output("X" + framework::kGradVarSuffix) ->Resize(ctx.Input("X")->dims()); diff --git a/paddle/operators/mean_op.h b/paddle/operators/mean_op.h index 267e6d903ebeca8d0b710da7edb5041403ef2141..40a1e2d099acad90b1bbac50f62ea7c4f691c1b4 100644 --- a/paddle/operators/mean_op.h +++ b/paddle/operators/mean_op.h @@ -20,7 +20,7 @@ namespace operators { template class MeanKernel : public OpKernel { -public: + public: void Compute(const ExecutionContext& context) const override { auto input = context.Input(0); auto output = context.Output(0); @@ -37,7 +37,7 @@ public: template class MeanGradKernel : public OpKernel { -public: + public: void Compute(const ExecutionContext& context) const override { auto OG = context.Input("Out" + framework::kGradVarSuffix); PADDLE_ENFORCE(framework::product(OG->dims()) == 1, diff --git a/paddle/operators/mul_op.cc b/paddle/operators/mul_op.cc index d127f3a302a340fe7558f918d6eeb2ea0a3fafe7..f41e95e9db494109925fb600ec6bbd47edf6cc74 100644 --- a/paddle/operators/mul_op.cc +++ b/paddle/operators/mul_op.cc @@ -18,7 +18,7 @@ namespace paddle { namespace operators { class MulOp : public OperatorWithKernel { -protected: + protected: void InferShape(const InferShapeContext &ctx) const override { PADDLE_ENFORCE(ctx.InputSize() == 2, "The mul op must take two inputs"); auto dim0 = ctx.Input(0)->dims(); @@ -34,7 +34,7 @@ protected: }; class MulOpMaker : public OpProtoAndCheckerMaker { -public: + public: MulOpMaker(OpProto *proto, OpAttrChecker *op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { AddInput("X", "The first input of mul op"); @@ -49,7 +49,7 @@ The equation is: Out = X * Y }; class MulOpGrad : public OperatorWithKernel { -protected: + protected: void InferShape(const InferShapeContext &ctx) const override {} std::string DebugString() const override { LOG(INFO) << "MulGrad"; diff --git a/paddle/operators/mul_op.h b/paddle/operators/mul_op.h index c7b78ad39045d25d73bfc2c930063c255a514864..7ecd6e8ac01c9efeabe9d2873da39503966ba8df 100644 --- a/paddle/operators/mul_op.h +++ b/paddle/operators/mul_op.h @@ -21,7 +21,7 @@ namespace operators { template class MulKernel : public OpKernel { -public: + public: void Compute(const ExecutionContext& context) const override { Eigen::array, 1> dim_pair = { {Eigen::IndexPair(1, 0)}}; diff --git a/paddle/operators/net_op.h b/paddle/operators/net_op.h index 13611e1ee83170db43e17d6088e4b04588ce6255..6e7af7f02ae23ec65459dfd15d950a43e96fec4d 100644 --- a/paddle/operators/net_op.h +++ b/paddle/operators/net_op.h @@ -40,7 +40,7 @@ namespace operators { * it defines. */ class NetOp : public framework::OperatorBase { -public: + public: /** * Infer all the operators' input and output variables' shapes, will be called * before every mini-batch @@ -90,7 +90,7 @@ public: std::vector> ops_; -private: + private: bool add_op_done_{false}; template diff --git a/paddle/operators/net_op_test.cc b/paddle/operators/net_op_test.cc index 18c5c60eb43250c23e2819a3c79ab8a96fec103e..c0a345464a34329d42c7bf753ca94fd07195b8e0 100644 --- a/paddle/operators/net_op_test.cc +++ b/paddle/operators/net_op_test.cc @@ -12,7 +12,7 @@ static int infer_shape_cnt = 0; static int run_cnt = 0; class TestOp : public OperatorBase { -public: + public: void InferShape(const framework::Scope& scope) const override { ++infer_shape_cnt; } @@ -23,7 +23,7 @@ public: }; class EmptyOp : public OperatorBase { -public: + public: void InferShape(const Scope& scope) const override {} void Run(const Scope& scope, const platform::DeviceContext& dev_ctx) const override {} diff --git a/paddle/operators/recurrent_op.cc b/paddle/operators/recurrent_op.cc index 2fdaaaf05c5a1428a25946452c97b3f6e2849f2f..9270a0eaa4a86407bc19ada4d934e768624aa025 100644 --- a/paddle/operators/recurrent_op.cc +++ b/paddle/operators/recurrent_op.cc @@ -28,14 +28,12 @@ namespace operators { namespace rnn { void SegmentInputs(const std::vector& step_scopes, - const std::vector& inlinks, - const size_t seq_len, + const std::vector& inlinks, const size_t seq_len, bool infer_shape_mode) { PADDLE_ENFORCE(!inlinks.empty(), "no in links are provided."); for (size_t i = 0; i < inlinks.size(); ++i) { auto input_var = step_scopes[0]->FindVar(inlinks[i].external); - PADDLE_ENFORCE(input_var != nullptr, - "input link [%s] is not in scope.", + PADDLE_ENFORCE(input_var != nullptr, "input link [%s] is not in scope.", inlinks[i].external); Tensor* input = input_var->GetMutable(); framework::DDim dims = input->dims(); @@ -54,13 +52,11 @@ void SegmentInputs(const std::vector& step_scopes, } void ConcatOutputs(const std::vector& step_scopes, - const std::vector& outlinks, - const size_t seq_len, + const std::vector& outlinks, const size_t seq_len, bool infer_shape_mode) { for (size_t i = 0; i < outlinks.size(); i++) { auto output_var = step_scopes[0]->FindVar(outlinks[i].external); - PADDLE_ENFORCE(output_var != nullptr, - "output link [%s] is not in scope.", + PADDLE_ENFORCE(output_var != nullptr, "output link [%s] is not in scope.", outlinks[i].external); Tensor* output = output_var->GetMutable(); if (infer_shape_mode) { @@ -87,22 +83,16 @@ void ConcatOutputs(const std::vector& step_scopes, void LinkMemories(const std::vector& scopes, const std::vector& memories, - const size_t step_id, - const int offset, + const size_t step_id, const int offset, bool infer_shape_mode) { PADDLE_ENFORCE(step_id < scopes.size(), - "step [%d] is out of range of step scopes' size [%d]", - step_id, + "step [%d] is out of range of step scopes' size [%d]", step_id, scopes.size()); PADDLE_ENFORCE(static_cast(step_id) + offset >= 0, - "offset [%d] must be large than -[%d]", - offset, - step_id); + "offset [%d] must be large than -[%d]", offset, step_id); PADDLE_ENFORCE(step_id + offset < scopes.size(), "offset [%d] is out of range, it must be less than (%d - %d)", - offset, - scopes.size(), - step_id); + offset, scopes.size(), step_id); auto scope = scopes[step_id]; auto linked_scope = scopes[step_id + offset]; for (auto& attr : memories) { @@ -116,8 +106,7 @@ void LinkMemories(const std::vector& scopes, } } -void InitArgument(const ArgumentName& name, - Argument* arg, +void InitArgument(const ArgumentName& name, Argument* arg, const OperatorBase& op) { arg->step_net = op.Input(name.step_net); arg->step_scopes = op.Output(name.step_scopes); @@ -126,8 +115,7 @@ void InitArgument(const ArgumentName& name, auto inlink_alias = op.GetAttr>(name.inlink_alias); PADDLE_ENFORCE(inlinks.size() == inlink_alias.size(), "the size of inlinks and inlink_alias don't match:%d,%d", - inlinks.size(), - inlink_alias.size()); + inlinks.size(), inlink_alias.size()); for (size_t i = 0; i < inlinks.size(); ++i) { rnn::Link link; link.external = inlinks[i]; @@ -139,8 +127,7 @@ void InitArgument(const ArgumentName& name, auto outlink_alias = op.GetAttr>(name.outlink_alias); PADDLE_ENFORCE(outlinks.size() == outlink_alias.size(), "the size of outlinks and outlink_alias don't match:%d,%d", - outlinks.size(), - outlink_alias.size()); + outlinks.size(), outlink_alias.size()); for (size_t i = 0; i < outlinks.size(); ++i) { rnn::Link link; link.external = outlinks[i]; @@ -156,12 +143,10 @@ void InitArgument(const ArgumentName& name, PADDLE_ENFORCE(memories.size() == boot_memories.size(), "the size of memories, boot_memories don't match:%d,%d", - memories.size(), - boot_memories.size()); + memories.size(), boot_memories.size()); PADDLE_ENFORCE(pre_memories.size() == boot_memories.size(), "the size of pre_memories, boot_memories don't match:%d,%d", - pre_memories.size(), - boot_memories.size()); + pre_memories.size(), boot_memories.size()); PADDLE_ENFORCE(memories.size() > 0, "more than 1 memories should be set"); for (size_t i = 0; i < memories.size(); ++i) { @@ -181,39 +166,39 @@ void RecurrentAlgorithm::InferShape(const Scope& scope) const { ->dims()[0]; CreateScopes(scope); auto step_scopes = GetStepScopes(scope); - rnn::SegmentInputs( - step_scopes, arg_->inlinks, seq_len_, true /*infer_shape_mode*/); + rnn::SegmentInputs(step_scopes, arg_->inlinks, seq_len_, + true /*infer_shape_mode*/); InitMemories(step_scopes[0], true /*infer_shape_mode*/); Variable* net = scope.FindVar(arg_->step_net); PADDLE_ENFORCE(net != nullptr, "failed to get step net"); for (size_t i = 0; i < seq_len_; i++) { if (i > 0) { - rnn::LinkMemories( - step_scopes, arg_->memories, i, -1, true /*infer_shape_mode*/); + rnn::LinkMemories(step_scopes, arg_->memories, i, -1, + true /*infer_shape_mode*/); } net->GetMutable()->InferShape(*step_scopes[i]); } - rnn::ConcatOutputs( - step_scopes, arg_->outlinks, seq_len_, true /*infer_shape_mode*/); + rnn::ConcatOutputs(step_scopes, arg_->outlinks, seq_len_, + true /*infer_shape_mode*/); } void RecurrentAlgorithm::Run(const Scope& scope, const platform::DeviceContext& dev_ctx) const { auto step_scopes = GetStepScopes(scope); - rnn::SegmentInputs( - step_scopes, arg_->inlinks, seq_len_, false /*infer_shape_mode*/); + rnn::SegmentInputs(step_scopes, arg_->inlinks, seq_len_, + false /*infer_shape_mode*/); InitMemories(step_scopes[0], false /*infer_shape_mode*/); Variable* net = scope.FindVar(arg_->step_net); for (size_t step_id = 0; step_id < seq_len_; step_id++) { if (step_id > 0) { - rnn::LinkMemories( - step_scopes, arg_->memories, step_id, -1, false /*infer_shape_mode*/); + rnn::LinkMemories(step_scopes, arg_->memories, step_id, -1, + false /*infer_shape_mode*/); } net->GetMutable()->Run(*step_scopes[step_id], dev_ctx); } - rnn::ConcatOutputs( - step_scopes, arg_->outlinks, seq_len_, false /*infer_shape_mode*/); + rnn::ConcatOutputs(step_scopes, arg_->outlinks, seq_len_, + false /*infer_shape_mode*/); } void RecurrentAlgorithm::CreateScopes(const Scope& scope) const { @@ -245,8 +230,7 @@ void RecurrentAlgorithm::InitMemories(Scope* step_scope, for (auto& attr : arg_->memories) { Tensor* pre_mem = step_scope->NewVar(attr.pre_var)->GetMutable(); PADDLE_ENFORCE(step_scope->FindVar(attr.boot_var) != nullptr, - "memory [%s]'s boot variable [%s] not exists", - attr.var, + "memory [%s]'s boot variable [%s] not exists", attr.var, attr.boot_var); Tensor* boot_mem = step_scope->FindVar(attr.boot_var)->GetMutable(); if (infer_shape_mode) { @@ -257,25 +241,15 @@ void RecurrentAlgorithm::InitMemories(Scope* step_scope, } } -const rnn::ArgumentName RecurrentOp::kArgName{"step_net", - "step_scopes", - "inlinks", - "outlinks", - "inlink_alias", - "outlink_alias", - "memories", - "pre_memories", - "boot_memories"}; - -const rnn::ArgumentName RecurrentGradientOp::kArgName{"step_net", - "step_scopes", - "outlink@grad", - "inlink@grad", - "inlink_alias", - "outlink_alias", - "memories", - "pre_memories", - "boot_memories@grad"}; +const rnn::ArgumentName RecurrentOp::kArgName{ + "step_net", "step_scopes", "inlinks", + "outlinks", "inlink_alias", "outlink_alias", + "memories", "pre_memories", "boot_memories"}; + +const rnn::ArgumentName RecurrentGradientOp::kArgName{ + "step_net", "step_scopes", "outlink@grad", + "inlink@grad", "inlink_alias", "outlink_alias", + "memories", "pre_memories", "boot_memories@grad"}; void RecurrentOp::Init() { OperatorBase::Init(); @@ -285,7 +259,7 @@ void RecurrentOp::Init() { } class RecurrentAlgorithmProtoAndCheckerMaker : public OpProtoAndCheckerMaker { -public: + public: RecurrentAlgorithmProtoAndCheckerMaker(OpProto* proto, OpAttrChecker* op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { @@ -316,31 +290,29 @@ public: void RecurrentGradientAlgorithm::Run( const Scope& scope, const platform::DeviceContext& dev_ctx) const { auto step_scopes = GetStepScopes(scope); - rnn::SegmentInputs( - step_scopes, arg_->inlinks, seq_len_, false /*infer_shape_mode*/); + rnn::SegmentInputs(step_scopes, arg_->inlinks, seq_len_, + false /*infer_shape_mode*/); Variable* net = scope.FindVar(arg_->step_net); PADDLE_ENFORCE(net != nullptr, "failed to get step net"); for (int step_id = seq_len_ - 1; step_id >= 0; --step_id) { if (static_cast(step_id) != seq_len_ - 1) { - rnn::LinkMemories( - step_scopes, arg_->memories, step_id, 1, false /*infer_shape_mode*/); + rnn::LinkMemories(step_scopes, arg_->memories, step_id, 1, + false /*infer_shape_mode*/); } net->GetMutable()->Run(*step_scopes[step_id], dev_ctx); } LinkBootMemoryGradients(step_scopes[0], false); - rnn::ConcatOutputs( - step_scopes, arg_->outlinks, seq_len_, false /*infer_shape_mode*/); + rnn::ConcatOutputs(step_scopes, arg_->outlinks, seq_len_, + false /*infer_shape_mode*/); } void RecurrentGradientAlgorithm::LinkBootMemoryGradients( Scope* step_scope, bool infer_shape_mode) const { for (auto& attr : arg_->memories) { PADDLE_ENFORCE(step_scope->FindVar(attr.var) != nullptr, - "memory variable [%s] does not exists", - attr.var); + "memory variable [%s] does not exists", attr.var); PADDLE_ENFORCE(step_scope->FindVar(attr.boot_var) != nullptr, - "boot variable [%s] does not exists", - attr.boot_var); + "boot variable [%s] does not exists", attr.boot_var); Tensor* mem_grad = step_scope->NewVar(attr.var)->GetMutable(); Tensor* boot_mem_grad = step_scope->NewVar(attr.boot_var)->GetMutable(); @@ -357,19 +329,19 @@ void RecurrentGradientAlgorithm::InferShape(const Scope& scope) const { ->GetMutable() ->dims()[0]; auto step_scopes = GetStepScopes(scope); - rnn::SegmentInputs( - step_scopes, arg_->inlinks, seq_len_, true /*infer_shape_mode*/); + rnn::SegmentInputs(step_scopes, arg_->inlinks, seq_len_, + true /*infer_shape_mode*/); Variable* net = scope.FindVar(arg_->step_net); PADDLE_ENFORCE(net != nullptr, "failed to get step net"); for (int step_id = seq_len_ - 1; step_id >= 0; --step_id) { if (static_cast(step_id) != seq_len_ - 1) { - rnn::LinkMemories( - step_scopes, arg_->memories, step_id, 1, true /*infer_shape_mode*/); + rnn::LinkMemories(step_scopes, arg_->memories, step_id, 1, + true /*infer_shape_mode*/); } net->GetMutable()->InferShape(*step_scopes[step_id]); } - rnn::ConcatOutputs( - step_scopes, arg_->outlinks, seq_len_, true /*infer_shape_mode*/); + rnn::ConcatOutputs(step_scopes, arg_->outlinks, seq_len_, + true /*infer_shape_mode*/); LinkBootMemoryGradients(step_scopes[0], true /*infer_shape_mode*/); } @@ -383,6 +355,5 @@ void RecurrentGradientOp::Init() { } // namespace operators } // namespace paddle -REGISTER_OP(recurrent_op, - paddle::operators::RecurrentOp, +REGISTER_OP(recurrent_op, paddle::operators::RecurrentOp, paddle::operators::RecurrentAlgorithmProtoAndCheckerMaker); diff --git a/paddle/operators/recurrent_op.h b/paddle/operators/recurrent_op.h index c5931773d1d601c4f85b35a031c00de5008a28f8..510ba41667515fd9949cd9aefd10d7a9f8adc5bb 100644 --- a/paddle/operators/recurrent_op.h +++ b/paddle/operators/recurrent_op.h @@ -69,23 +69,19 @@ struct ArgumentName { * Prepare inputs for each step net. */ void SegmentInputs(const std::vector& step_scopes, - const std::vector& inlinks, - const size_t seq_len, + const std::vector& inlinks, const size_t seq_len, bool infer_shape_mode); /** * Process outputs of step nets and merge to variables. */ void ConcatOutputs(const std::vector& step_scopes, - const std::vector& outlinks, - const size_t seq_len, + const std::vector& outlinks, const size_t seq_len, bool infer_shape_mode); void LinkMemories(const std::vector& step_scopes, - const std::vector& memories, - const size_t step_id, - const int offset, - bool infer_shape_mode); + const std::vector& memories, const size_t step_id, + const int offset, bool infer_shape_mode); void InitArgument(const ArgumentName& name, Argument* arg); @@ -100,7 +96,7 @@ void InitArgument(const ArgumentName& name, Argument* arg); // Refer to: https://arxiv.org/pdf/1502.02367.pdf class RecurrentAlgorithm { -public: + public: void Run(const framework::Scope& scope, const platform::DeviceContext& dev_ctx) const; @@ -111,7 +107,7 @@ public: */ void InferShape(const framework::Scope& scope) const; -protected: + protected: /* * The step scopes will be stored in the father scope as a variable. * @@ -128,7 +124,7 @@ protected: void InitMemories(framework::Scope* step_scopes, bool infer_shape_mode) const; -private: + private: std::unique_ptr arg_; mutable size_t seq_len_; }; @@ -144,7 +140,7 @@ class RecurrentGradientAlgorithm { * lot, and the latter is a wrapper acts like an dapter for it to make RNN an * operator. */ -public: + public: void Init(std::unique_ptr arg) { arg_ = std::move(arg); } void Run(const framework::Scope& scope, @@ -158,20 +154,20 @@ public: */ void InferShape(const framework::Scope& scope) const; -protected: + protected: inline const std::vector& GetStepScopes( const framework::Scope& scope) const { return *scope.FindVar(arg_->step_scopes) ->GetMutable>(); } -private: + private: std::unique_ptr arg_; mutable size_t seq_len_; }; class RecurrentOp final : public framework::OperatorBase { -public: + public: void Init() override; /** @@ -188,12 +184,12 @@ public: static const rnn::ArgumentName kArgName; -private: + private: RecurrentAlgorithm alg_; }; class RecurrentGradientOp final : public framework::OperatorBase { -public: + public: void Init() override; /** @@ -210,7 +206,7 @@ public: static const rnn::ArgumentName kArgName; -private: + private: RecurrentGradientAlgorithm alg_; }; diff --git a/paddle/operators/recurrent_op_test.cc b/paddle/operators/recurrent_op_test.cc index f450167c83e84c7f38dd5bc1a8debfc895f8ff00..409ebd250641ab3df990d6753d99ee89cbe4becf 100644 --- a/paddle/operators/recurrent_op_test.cc +++ b/paddle/operators/recurrent_op_test.cc @@ -29,7 +29,7 @@ using framework::make_ddim; using framework::DDim; class RecurrentOpTest : public ::testing::Test { -protected: + protected: virtual void SetUp() override { CreateGlobalVariables(); CreateStepNet(); @@ -174,7 +174,7 @@ TEST_F(RecurrentOpTest, Run) { } class RecurrentGradientAlgorithmTest : public ::testing::Test { -protected: + protected: virtual void SetUp() override { CreateGlobalVariables(); CreateStepScopes(); @@ -277,13 +277,11 @@ protected: LOG(INFO) << "create variable step_net"; Variable* var = scope_.NewVar("step_net"); auto net = var->GetMutable(); - net->AddOp(OpRegistry::CreateOp("mul", - {"rnn/h_pre", "rnn/w", "rnn/s_grad"}, - {"rnn/h_pre_grad", "rnn/w_grad"}, - {})); + net->AddOp(OpRegistry::CreateOp("mul", {"rnn/h_pre", "rnn/w", "rnn/s_grad"}, + {"rnn/h_pre_grad", "rnn/w_grad"}, {})); - net->AddOp(OpRegistry::CreateOp( - "add_two", {"rnn/h_grad"}, {"rnn/x_grad", "rnn/s_grad"}, {})); + net->AddOp(OpRegistry::CreateOp("add_two", {"rnn/h_grad"}, + {"rnn/x_grad", "rnn/s_grad"}, {})); net->CompleteAddOp(); } @@ -297,9 +295,7 @@ protected: inlink.internal = "rnn/x"; auto step_scopes = scope_.FindVar("step_scopes")->GetMutable>(); - rnn::SegmentInputs(*step_scopes, - std::vector{inlink}, - 10, + rnn::SegmentInputs(*step_scopes, std::vector{inlink}, 10, true /*infer_shape_mode*/); } @@ -314,8 +310,8 @@ protected: auto step_scopes = scope_.FindVar("step_scopes")->GetMutable>(); for (int i = 1; i < 10; ++i) { - rnn::LinkMemories( - *step_scopes, memories, i, -1, true /*infer_shape_mode*/); + rnn::LinkMemories(*step_scopes, memories, i, -1, + true /*infer_shape_mode*/); } } diff --git a/paddle/operators/rowwise_add_op.cc b/paddle/operators/rowwise_add_op.cc index 2ad2b66c8f385c858eb34c7ea766f168de9c817e..8d1a36f2b332faad516ced012a409ca428bbf689 100644 --- a/paddle/operators/rowwise_add_op.cc +++ b/paddle/operators/rowwise_add_op.cc @@ -17,7 +17,7 @@ namespace paddle { namespace operators { class RowWiseAddOp : public OperatorWithKernel { -protected: + protected: void InferShape(const InferShapeContext &ctx) const override { PADDLE_ENFORCE(ctx.InputSize() == 2UL, "Two inputs is needed by rowwise add"); @@ -33,7 +33,7 @@ protected: }; class RowWiseAddOpMaker : public OpProtoAndCheckerMaker { -public: + public: RowWiseAddOpMaker(OpProto *proto, OpAttrChecker *op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { AddInput("X", "The left input of row-wise add op, must be matrix"); diff --git a/paddle/operators/rowwise_add_op.h b/paddle/operators/rowwise_add_op.h index bd4d1128955fb718d3a84dfd96d8c68d7196e9cc..b52524c47c7b80d8ddc6a94a4a6d03db8034088d 100644 --- a/paddle/operators/rowwise_add_op.h +++ b/paddle/operators/rowwise_add_op.h @@ -20,7 +20,7 @@ namespace operators { template class RowWiseAddKernel : public OpKernel { -public: + public: void Compute(const ExecutionContext& context) const override { auto out = context.Output(0); out->mutable_data(context.GetPlace()); diff --git a/paddle/operators/sgd_op.cc b/paddle/operators/sgd_op.cc index 9a84dc8af3b3e649b776ca8a97dedba1fa3ff48d..6307583f4ee3f185845690d0e378945d066eae75 100644 --- a/paddle/operators/sgd_op.cc +++ b/paddle/operators/sgd_op.cc @@ -18,7 +18,7 @@ namespace paddle { namespace operators { class SGDOp : public OperatorWithKernel { -protected: + protected: void InferShape(const InferShapeContext &ctx) const override { PADDLE_ENFORCE(ctx.InputSize() == 2, "Input size of SGDOp must be two"); PADDLE_ENFORCE(ctx.OutputSize() == 1, "Output size of SGDOp must be one"); @@ -32,7 +32,7 @@ protected: }; class SGDOpMaker : public OpProtoAndCheckerMaker { -public: + public: SGDOpMaker(OpProto *proto, OpAttrChecker *op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { AddInput("param", "input parameter"); diff --git a/paddle/operators/sgd_op.h b/paddle/operators/sgd_op.h index 0c3a240f9a4a5fc7bc4898e82786810cee2f7010..bf5b195933fce7faa46bcc96032e784076178cf7 100644 --- a/paddle/operators/sgd_op.h +++ b/paddle/operators/sgd_op.h @@ -20,7 +20,7 @@ namespace operators { template class SGDOpKernel : public OpKernel { -public: + public: void Compute(const ExecutionContext& ctx) const override { auto param = ctx.Input("param"); auto grad = ctx.Input("grad"); diff --git a/paddle/operators/sigmoid_op.cc b/paddle/operators/sigmoid_op.cc index a81ab262cc6fe7bdff0045259e0030f3d46f503f..9d201eb93a2c0e34dd8e6869e97b43c4e278596e 100644 --- a/paddle/operators/sigmoid_op.cc +++ b/paddle/operators/sigmoid_op.cc @@ -17,7 +17,7 @@ namespace paddle { namespace operators { class SigmoidOp : public OperatorWithKernel { -protected: + protected: void InferShape(const InferShapeContext &ctx) const override { PADDLE_ENFORCE(ctx.InputSize() == 1, "Sigmoid Op only have one input"); PADDLE_ENFORCE(ctx.OutputSize() == 1, "Sigmoid Op only have one output"); @@ -26,7 +26,7 @@ protected: }; class SigmoidOpMaker : public OpProtoAndCheckerMaker { -public: + public: SigmoidOpMaker(OpProto *proto, OpAttrChecker *op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { AddInput("X", "sigmoid input"); @@ -36,7 +36,7 @@ public: }; class SigmoidOpGrad : public OperatorWithKernel { -protected: + protected: void InferShape(const InferShapeContext &ctx) const override {} std::string DebugString() const override { LOG(INFO) << "SigmoidGrad"; diff --git a/paddle/operators/sigmoid_op.h b/paddle/operators/sigmoid_op.h index 1412e4398440c8e946d3ab434a50e978079637ab..eb473920a5f866825b52ecb946653ccead7000ea 100644 --- a/paddle/operators/sigmoid_op.h +++ b/paddle/operators/sigmoid_op.h @@ -21,7 +21,7 @@ namespace operators { template class SigmoidKernel : public OpKernel { -public: + public: void Compute(const ExecutionContext& context) const override { auto input = context.Input(0); auto output = context.Output(0); diff --git a/paddle/operators/softmax_op.cc b/paddle/operators/softmax_op.cc index e8bb7032f852fed1e79eba8391aa4ddd50f8602b..a070458f5e55cf47253ab0df5af7a1163b4f8092 100644 --- a/paddle/operators/softmax_op.cc +++ b/paddle/operators/softmax_op.cc @@ -18,7 +18,7 @@ namespace paddle { namespace operators { class SoftmaxOp : public OperatorWithKernel { -protected: + protected: void InferShape(const InferShapeContext &ctx) const override { PADDLE_ENFORCE(ctx.InputSize() == 1UL, "Only one input is need for softmax"); @@ -31,7 +31,7 @@ protected: }; class SoftmaxOpMaker : public OpProtoAndCheckerMaker { -public: + public: SoftmaxOpMaker(OpProto *proto, OpAttrChecker *op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { AddInput("X", "input of softmax"); @@ -41,7 +41,7 @@ public: }; class SoftmaxOpGrad : public OperatorWithKernel { -protected: + protected: void InferShape(const InferShapeContext &ctx) const override { PADDLE_ENFORCE(ctx.InputSize() == 3UL, "Input of SoftmaxOpGrad should be 3, X, Y, YG"); diff --git a/paddle/operators/softmax_op.h b/paddle/operators/softmax_op.h index d9f3b2006ec04061bcbbf6988e149698b056495a..b2dbcf57edf1a64da8da0d9a4c14d708eec17f3f 100644 --- a/paddle/operators/softmax_op.h +++ b/paddle/operators/softmax_op.h @@ -24,7 +24,7 @@ namespace operators { template class SoftmaxKernel : public OpKernel { -public: + public: void Compute(const ExecutionContext& context) const override { auto input = context.Input("X"); auto output = context.Output("Y"); @@ -63,7 +63,7 @@ public: template class SoftmaxGradKernel : public OpKernel { -public: + public: void Compute(const ExecutionContext& context) const override { std::shared_ptr scale_ = std::make_shared(); diff --git a/paddle/operators/type_alias.h b/paddle/operators/type_alias.h index 931740e150946a939b8656be5a30185c6ee1cb8f..eac12d35dd8d2977191218167ebb0a6e638d5d73 100644 --- a/paddle/operators/type_alias.h +++ b/paddle/operators/type_alias.h @@ -26,21 +26,16 @@ using OperatorBase = framework::OperatorBase; using InferShapeContext = framework::InferShapeContext; using ExecutionContext = framework::ExecutionContext; using Variable = framework::Variable; -template using EigenScalar = framework::EigenScalar; -template using EigenVector = framework::EigenVector; -template using EigenMatrix = framework::EigenMatrix; -template using EigenTensor = framework::EigenTensor; using Tensor = framework::Tensor;