diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index eb7ad8ed94ec95e2ef8fabd85d816d949aa0953d..dcb822afb4cadbfeac111e53cbd3cecedf05c77d 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -2716,22 +2716,7 @@ proto::VarType::Type OperatorWithKernel::IndicateDataType( static_cast(-1); proto::VarType::Type data_type = dafault_data_type; - auto in_name_list = ctx.InNameList(); - if (Info().HasOpProtoAndChecker()) { - for (auto& attr : Info().Proto().attrs()) { - auto it = - std::find_if(in_name_list.begin(), - in_name_list.end(), - [&attr](const std::string* name) { - return attr.support_tensor() && *name == attr.name(); - }); - if (it != in_name_list.end()) { - in_name_list.erase(it); - } - } - } - - for (auto* name : in_name_list) { + for (auto* name : ctx.InNameList()) { if (ctx.InputSize(*name) == 1UL) { ParseInputDataType(ctx.InputVar(*name), *name, &data_type); } else { diff --git a/paddle/fluid/operators/pad_op.cc b/paddle/fluid/operators/pad_op.cc index 4e6a10a912a8888337b44363ad166bb1dcc043bc..2951091508dd6d82af7ea0ccf5817e088d1f42c9 100644 --- a/paddle/fluid/operators/pad_op.cc +++ b/paddle/fluid/operators/pad_op.cc @@ -30,6 +30,13 @@ class PadOp : public framework::OperatorWithKernel { OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "Pad"); OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "Pad"); } + + protected: + framework::OpKernelType GetExpectedKernelType( + const framework::ExecutionContext& ctx) const override { + return framework::OpKernelType( + OperatorWithKernel::IndicateVarDataType(ctx, "X"), ctx.GetPlace()); + } }; class PadOpMaker : public framework::OpProtoAndCheckerMaker { @@ -98,6 +105,14 @@ class PadOpGrad : public framework::OperatorWithKernel { ctx->SetOutputDim(x_grad_name, dout_dims); } } + + protected: + framework::OpKernelType GetExpectedKernelType( + const framework::ExecutionContext& ctx) const override { + return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType( + ctx, framework::GradVarName("Out")), + ctx.GetPlace()); + } }; template diff --git a/python/paddle/fluid/tests/unittests/test_pad_op.py b/python/paddle/fluid/tests/unittests/test_pad_op.py index 735f62c646b16af1a3033b00a7712591ca8b2503..ee42ce1625feccc80d8ce72a862395b1cdc6f756 100644 --- a/python/paddle/fluid/tests/unittests/test_pad_op.py +++ b/python/paddle/fluid/tests/unittests/test_pad_op.py @@ -187,9 +187,14 @@ class TestPaddingValueTensor3(unittest.TestCase): x = paddle.assign(np_x).astype('float32') pad_value = paddle.assign([0.0]).astype('float64') y = paddle.nn.functional.pad(x, [0, 1, 2, 3], value=pad_value) + loss = y.sum() + optimize_ops, params_grads = paddle.optimizer.SGD(0.01).minimize( + loss + ) exe = paddle.static.Executor(paddle.CPUPlace()) - [pd_out] = exe.run(main_prog, fetch_list=[y]) + res = exe.run(main_prog, fetch_list=[y] + [g for p, g in params_grads]) + pd_out = res[0] np_out = np.pad(np_x, [(0, 1), (2, 3)], constant_values=0.0) np.testing.assert_allclose(pd_out, np_out)