diff --git a/paddle/fluid/operators/rnn_op.cc b/paddle/fluid/operators/rnn_op.cc index 2c1fcb104aafb71090ed28d60423b68385a4842e..a709b20b900c1e50802ff8c3169a4469b421fdc5 100644 --- a/paddle/fluid/operators/rnn_op.cc +++ b/paddle/fluid/operators/rnn_op.cc @@ -162,8 +162,10 @@ class RNNOpMaker : public framework::OpProtoAndCheckerMaker { AddAttr( "mode", "(string) rnn types, including: LSTM, GRU, RNN_RELU, RNN_TANH."); - AddAttr("is_test", "True if in test phase.").SetDefault(false); AddAttr("seed", "seed to used if fix_seed is True").SetDefault(0); + AddAttr("is_test", "True if in test phase.") + .SetDefault(false) + .AsExtra(); AddComment(R"DOC( )DOC"); } diff --git a/paddle/fluid/operators/rnn_op.h b/paddle/fluid/operators/rnn_op.h index 2b223e24cf8e63082fff221301cbe5000962a84f..ab64e50d450f02afe20c37f33f19aaa6c63a5509 100644 --- a/paddle/fluid/operators/rnn_op.h +++ b/paddle/fluid/operators/rnn_op.h @@ -230,7 +230,7 @@ template void dropout_cpu_function_inplace(const framework::ExecutionContext& context, Tensor* x, Tensor* y, Tensor* mask, const float& dropout_prob, - const int& seed_number, const bool& is_test, + const int& seed_number, bool is_test, bool* is_has_reset) { if (is_test) { return; @@ -816,7 +816,7 @@ void RnnFunc(const framework::ExecutionContext& ctx, const Tensor* input, Tensor* dropout_mask, const int& num_layers, const int& gate_num, const int& input_size, const int& hidden_size, const bool& is_bidirec, const std::string& cell_type, - const float& dropout_prob, const bool& is_test, const int& seed, + const float& dropout_prob, bool is_test, const int& seed, Tensor* reserve_data) { const int& direction_num = is_bidirec ? 2 : 1; const auto& init_h_dims = init_h->dims(); @@ -952,8 +952,8 @@ class RNNCPUKernel : public framework::OpKernel { const int& hidden_size = ctx.Attr("hidden_size"); const float& dropout_prob = ctx.Attr("dropout_prob"); const std::string& mode = ctx.Attr("mode"); - const bool& is_test = ctx.Attr("is_test"); const int& seed = ctx.Attr("seed"); + bool is_test = ctx.HasAttr("is_test") ? ctx.Attr("is_test") : false; bool has_seq_length = ctx.HasInput("SequenceLength"); const Tensor* sequence_length = nullptr; @@ -1809,7 +1809,8 @@ void RnnGradFunc(const framework::ExecutionContext& context, const int& num_layers = context.Attr("num_layers"); const bool& is_bidirec = context.Attr("is_bidirec"); const float& dropout_prob = context.Attr("dropout_prob"); - const bool& is_test = context.Attr("is_test"); + bool is_test = + context.HasAttr("is_test") ? context.Attr("is_test") : false; // get the input_size, batch_size, time_step, hidden_size const int& time_step = input->dims()[0]; diff --git a/paddle/fluid/operators/sequence_ops/sequence_pool_op.cc b/paddle/fluid/operators/sequence_ops/sequence_pool_op.cc index 8a3bb5318cb3bb40242a676896c4144752dbd109..01990ebb732915718a2734cea2fbf231d79cb45c 100644 --- a/paddle/fluid/operators/sequence_ops/sequence_pool_op.cc +++ b/paddle/fluid/operators/sequence_ops/sequence_pool_op.cc @@ -61,7 +61,8 @@ class SequencePoolOpMaker : public framework::OpProtoAndCheckerMaker { AddAttr("is_test", "(bool, default false) Set to true for inference only, false " "for training. Some layers may run faster when this is true.") - .SetDefault(false); + .SetDefault(false) + .AsExtra(); AddAttr( "pooltype", "(string, default 'AVERAGE') the pooling pooltype of SequencePoolOp.") diff --git a/paddle/fluid/operators/sequence_ops/sequence_pool_op.h b/paddle/fluid/operators/sequence_ops/sequence_pool_op.h index 37f9caf76ceba33b07404776768b40f8315c9509..dca65512e32bc399d113d3bdcd52bd2eda0b7b3d 100644 --- a/paddle/fluid/operators/sequence_ops/sequence_pool_op.h +++ b/paddle/fluid/operators/sequence_ops/sequence_pool_op.h @@ -67,7 +67,8 @@ class SequencePoolKernel : public framework::OpKernel { out->mutable_data(context.GetPlace()); Tensor* index = nullptr; - const bool is_test = context.Attr("is_test"); + bool is_test = + context.HasAttr("is_test") ? context.Attr("is_test") : false; // Do not create index buffer for inference (is_test) mode // TODO(jczaja): Skip index buffer creation for other devices eg. GPU diff --git a/paddle/fluid/operators/sequence_ops/sequence_softmax_op.cc b/paddle/fluid/operators/sequence_ops/sequence_softmax_op.cc index 9a7bb67bdfc871e22a856f1856495a58e65f2691..e7585f7ab04876de0dc8c94545944b9362ee982a 100644 --- a/paddle/fluid/operators/sequence_ops/sequence_softmax_op.cc +++ b/paddle/fluid/operators/sequence_ops/sequence_softmax_op.cc @@ -34,7 +34,8 @@ class SequenceSoftmaxOp : public framework::OperatorWithKernel { framework::OpKernelType GetExpectedKernelType( const framework::ExecutionContext& ctx) const override { // choose cudnn kernel if the runtime supported. - bool use_cudnn = ctx.Attr("use_cudnn"); + bool use_cudnn = + ctx.HasAttr("use_cudnn") ? ctx.Attr("use_cudnn") : false; bool runtime_cudnn_support = false; #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) if (platform::is_gpu_place(ctx.GetPlace())) { @@ -47,7 +48,9 @@ class SequenceSoftmaxOp : public framework::OperatorWithKernel { if (use_cudnn && runtime_cudnn_support) { library_ = framework::LibraryType::kCUDNN; } - std::string data_format = ctx.Attr("data_format"); + std::string data_format = ctx.HasAttr("data_format") + ? ctx.Attr("data_format") + : "AnyLayout"; return framework::OpKernelType( OperatorWithKernel::IndicateVarDataType(ctx, "X"), ctx.GetPlace(), framework::StringToDataLayout(data_format), library_); @@ -66,14 +69,16 @@ class SequenceSoftmaxOpMaker : public framework::OpProtoAndCheckerMaker { AddAttr( "use_cudnn", "(bool, default false) Only used in cudnn kernel, need install cudnn") - .SetDefault(false); + .SetDefault(false) + .AsExtra(); AddAttr( "data_format", "(string, default NCHW) Only used in " "An optional string from: \"NHWC\", \"NCHW\". " "Defaults to \"NHWC\". Specify the data format of the output data, " "the input will be transformed automatically. ") - .SetDefault("AnyLayout"); + .SetDefault("AnyLayout") + .AsExtra(); AddComment(R"DOC( Sequence Softmax Operator. @@ -130,7 +135,8 @@ class SequenceSoftmaxGradOp : public framework::OperatorWithKernel { framework::OpKernelType GetExpectedKernelType( const framework::ExecutionContext& ctx) const override { // choose cudnn kernel if the runtime supported. - bool use_cudnn = ctx.Attr("use_cudnn"); + bool use_cudnn = + ctx.HasAttr("use_cudnn") ? ctx.Attr("use_cudnn") : false; bool runtime_cudnn_support = false; #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) if (platform::is_gpu_place(ctx.GetPlace())) { @@ -143,7 +149,9 @@ class SequenceSoftmaxGradOp : public framework::OperatorWithKernel { if (use_cudnn && runtime_cudnn_support) { library_ = framework::LibraryType::kCUDNN; } - std::string data_format = ctx.Attr("data_format"); + std::string data_format = ctx.HasAttr("data_format") + ? ctx.Attr("data_format") + : "AnyLayout"; return framework::OpKernelType( OperatorWithKernel::IndicateVarDataType(ctx, "Out"), ctx.GetPlace(), framework::StringToDataLayout(data_format), library_);