From 04f042a5d507ad98f7f2cfc3cbc44b06d7a7f45c Mon Sep 17 00:00:00 2001 From: zyfncg Date: Mon, 17 Jan 2022 13:30:34 +0800 Subject: [PATCH] remove MakePtenDenseTensor in op compute (#38910) --- paddle/fluid/operators/cast_op.h | 5 +- paddle/fluid/operators/cholesky_solve_op.h | 5 +- paddle/fluid/operators/conj_op.h | 4 +- paddle/fluid/operators/dot_op.h | 17 ++----- paddle/fluid/operators/fill_any_like_op.h | 4 +- paddle/fluid/operators/flatten_op.h | 21 ++------ paddle/fluid/operators/lu_op.h | 14 ++---- paddle/fluid/operators/matmul_v2_op.h | 56 ++++------------------ paddle/fluid/operators/scale_op.h | 6 +-- paddle/fluid/operators/sign_op.h | 5 +- 10 files changed, 24 insertions(+), 113 deletions(-) diff --git a/paddle/fluid/operators/cast_op.h b/paddle/fluid/operators/cast_op.h index c54c811b25..e82eeb240c 100644 --- a/paddle/fluid/operators/cast_op.h +++ b/paddle/fluid/operators/cast_op.h @@ -63,14 +63,11 @@ class CastOpKernel : public framework::OpKernel { out->mutable_data(dev_ctx.GetPlace(), static_cast(out_dtype)); - auto pt_x = paddle::experimental::MakePtenDenseTensor(*in); - auto pt_out = paddle::experimental::MakePtenDenseTensor(*out); - auto pt_out_dtype = pten::TransToPtenDataType( static_cast(out_dtype)); // call new kernel - pten::CastKernel(dev_ctx, *pt_x.get(), pt_out_dtype, pt_out.get()); + pten::CastKernel(dev_ctx, *in, pt_out_dtype, out); } }; diff --git a/paddle/fluid/operators/cholesky_solve_op.h b/paddle/fluid/operators/cholesky_solve_op.h index 94b68bff8f..157679f4fc 100644 --- a/paddle/fluid/operators/cholesky_solve_op.h +++ b/paddle/fluid/operators/cholesky_solve_op.h @@ -202,10 +202,7 @@ class CholeskySolveGradKernel : public framework::OpKernel { commonterm_for_range(commonterm_functor); commonterm_conj = helper.Transpose(commonterm_conj); - auto pt_x = paddle::experimental::MakePtenDenseTensor(commonterm); - auto pt_y = paddle::experimental::MakePtenDenseTensor(commonterm_conj); - auto pt_z = paddle::experimental::MakePtenDenseTensor(commonterm); - pten::AddKernel(dev_ctx, *pt_x.get(), *pt_y.get(), -1, pt_z.get()); + pten::AddKernel(dev_ctx, commonterm, commonterm_conj, -1, &commonterm); auto mat_dim_u = math::CreateMatrixDescriptor(u_bst.dims(), 0, false); auto mat_dim_c = diff --git a/paddle/fluid/operators/conj_op.h b/paddle/fluid/operators/conj_op.h index 6df982abb8..e6958ed18b 100644 --- a/paddle/fluid/operators/conj_op.h +++ b/paddle/fluid/operators/conj_op.h @@ -34,11 +34,9 @@ class ConjKernel : public framework::OpKernel { out->mutable_data(context.GetPlace(), size_t(x->numel() * sizeof(T))); auto& dev_ctx = context.device_context(); - auto pt_x = paddle::experimental::MakePtenDenseTensor(*x); - auto pt_out = paddle::experimental::MakePtenDenseTensor(*out); // call new kernel - pten::ConjKernel(dev_ctx, *pt_x.get(), pt_out.get()); + pten::ConjKernel(dev_ctx, *x, out); } }; diff --git a/paddle/fluid/operators/dot_op.h b/paddle/fluid/operators/dot_op.h index ceb8a28e8a..ed8eb64d9e 100644 --- a/paddle/fluid/operators/dot_op.h +++ b/paddle/fluid/operators/dot_op.h @@ -40,13 +40,8 @@ class DotKernel : public framework::OpKernel { auto& dev_ctx = ctx.device_context(); out->mutable_data(x->place()); - auto pt_x = paddle::experimental::MakePtenDenseTensor(*x); - auto pt_y = paddle::experimental::MakePtenDenseTensor(*y); - auto pt_out = paddle::experimental::MakePtenDenseTensor(*out); - // call new kernel - pten::DotKernel(dev_ctx, *pt_x.get(), *pt_y.get(), - pt_out.get()); + pten::DotKernel(dev_ctx, *x, *y, out); } }; @@ -63,17 +58,11 @@ class DotGradKernel : public framework::OpKernel { if (tensor_dx) tensor_dx->mutable_data(ctx.GetPlace()); if (tensor_dy) tensor_dy->mutable_data(ctx.GetPlace()); - auto pt_x = paddle::experimental::MakePtenDenseTensor(*tensor_x); - auto pt_y = paddle::experimental::MakePtenDenseTensor(*tensor_y); - auto pt_dout = paddle::experimental::MakePtenDenseTensor(*tensor_dout); - auto pt_dx = paddle::experimental::MakePtenDenseTensor(*tensor_dx); - auto pt_dy = paddle::experimental::MakePtenDenseTensor(*tensor_dy); - auto& dev_ctx = ctx.device_context(); // call new kernel - pten::DotGradKernel(dev_ctx, *pt_x, *pt_y, *pt_dout, pt_dx.get(), - pt_dy.get()); + pten::DotGradKernel(dev_ctx, *tensor_x, *tensor_y, *tensor_dout, + tensor_dx, tensor_dy); } }; diff --git a/paddle/fluid/operators/fill_any_like_op.h b/paddle/fluid/operators/fill_any_like_op.h index 19f6e7a4ef..d5b3d89c3d 100644 --- a/paddle/fluid/operators/fill_any_like_op.h +++ b/paddle/fluid/operators/fill_any_like_op.h @@ -60,11 +60,9 @@ class FillAnyLikeKernel : public framework::OpKernel { std::isnan(value), false, platform::errors::InvalidArgument("The filled value is NaN.")); - auto pt_out = paddle::experimental::MakePtenDenseTensor(*out); - const auto& dev_ctx = context.template device_context(); // call new kernel - pten::FullLikeKernel(dev_ctx, value, pt_out.get()); + pten::FullLikeKernel(dev_ctx, value, out); } }; diff --git a/paddle/fluid/operators/flatten_op.h b/paddle/fluid/operators/flatten_op.h index 8e54ecb922..624bc9c567 100644 --- a/paddle/fluid/operators/flatten_op.h +++ b/paddle/fluid/operators/flatten_op.h @@ -131,12 +131,9 @@ class FlattenContiguousRangeKernel : public framework::OpKernel { auto &stop_axis = context.Attr("stop_axis"); auto &dev_ctx = context.device_context(); - auto pt_x = paddle::experimental::MakePtenDenseTensor(*in); - auto pt_out = paddle::experimental::MakePtenDenseTensor(*out); - // call new kernel - pten::FlattenKernel(dev_ctx, *pt_x.get(), start_axis, - stop_axis, pt_out.get()); + pten::FlattenKernel(dev_ctx, *in, start_axis, stop_axis, + out); } }; @@ -152,20 +149,8 @@ class FlattenContiguousRangeGradKernel : public framework::OpKernel { d_x->mutable_data(ctx.GetPlace(), d_out->type()); auto &dev_ctx = ctx.device_context(); - auto pt_d_x = paddle::experimental::MakePtenDenseTensor(*d_x); - auto pt_d_out = paddle::experimental::MakePtenDenseTensor(*d_out); - - // Because the holder of xshape may be nullptr, we can't use - // MakePtenDenseTensor. - // So, we create a new DenseTensor to save the dims of xshape. - pten::DenseTensorMeta xshape_meta{pten::TransToPtenDataType(d_x->type()), - xshape->dims(), d_x->layout()}; - auto pt_xshape = - pten::Empty(dev_ctx, std::move(xshape_meta)); - // call new kernel - pten::FlattenGradKernel(dev_ctx, *pt_d_out.get(), - pt_xshape, pt_d_x.get()); + pten::FlattenGradKernel(dev_ctx, *d_out, *xshape, d_x); } }; diff --git a/paddle/fluid/operators/lu_op.h b/paddle/fluid/operators/lu_op.h index da448fbd35..f241caa857 100644 --- a/paddle/fluid/operators/lu_op.h +++ b/paddle/fluid/operators/lu_op.h @@ -220,11 +220,8 @@ void Tensor_Add(const DeviceContext& dev_ctx, const framework::Tensor& src1, const framework::Tensor& src2, framework::Tensor* out) { out->Resize(src1.dims()); out->mutable_data(dev_ctx.GetPlace()); - auto pt_x = paddle::experimental::MakePtenDenseTensor(src1); - auto pt_y = paddle::experimental::MakePtenDenseTensor(src2); - auto pt_z = paddle::experimental::MakePtenDenseTensor(*out); - pten::AddKernel(dev_ctx, *pt_x.get(), *pt_y.get(), -1, - pt_z.get()); + + pten::AddKernel(dev_ctx, src1, src2, -1, out); } template @@ -232,11 +229,8 @@ void Tensor_Sub(const DeviceContext& dev_ctx, const framework::Tensor& src1, const framework::Tensor& src2, framework::Tensor* out) { out->Resize(src1.dims()); out->mutable_data(dev_ctx.GetPlace()); - auto pt_x = paddle::experimental::MakePtenDenseTensor(src1); - auto pt_y = paddle::experimental::MakePtenDenseTensor(src2); - auto pt_z = paddle::experimental::MakePtenDenseTensor(*out); - pten::SubtractKernel(dev_ctx, *pt_x.get(), *pt_y.get(), -1, - pt_z.get()); + + pten::SubtractKernel(dev_ctx, src1, src2, -1, out); } template diff --git a/paddle/fluid/operators/matmul_v2_op.h b/paddle/fluid/operators/matmul_v2_op.h index 9ab77cdcae..67b0fec734 100644 --- a/paddle/fluid/operators/matmul_v2_op.h +++ b/paddle/fluid/operators/matmul_v2_op.h @@ -52,13 +52,8 @@ class MatMulV2Kernel : public framework::OpKernel { auto& dev_ctx = ctx.device_context(); Out->mutable_data(X->place()); - auto pt_x = paddle::experimental::MakePtenDenseTensor(*X); - auto pt_y = paddle::experimental::MakePtenDenseTensor(*Y); - auto pt_out = paddle::experimental::MakePtenDenseTensor(*Out); - // call new kernel - pten::MatmulKernel(dev_ctx, *pt_x, *pt_y, trans_x, trans_y, - pt_out.get()); + pten::MatmulKernel(dev_ctx, *X, *Y, trans_x, trans_y, Out); } }; @@ -151,19 +146,11 @@ class MatMulV2GradKernel : public framework::OpKernel { if (dx) dx->mutable_data(ctx.GetPlace()); if (dy) dy->mutable_data(ctx.GetPlace()); - auto pt_x = paddle::experimental::MakePtenDenseTensor(*x); - auto pt_y = paddle::experimental::MakePtenDenseTensor(*y); - auto pt_dout = paddle::experimental::MakePtenDenseTensor(*dout); - auto pt_dx = dx ? paddle::experimental::MakePtenDenseTensor(*dx) - : std::unique_ptr(nullptr); - auto pt_dy = dy ? paddle::experimental::MakePtenDenseTensor(*dy) - : std::unique_ptr(nullptr); - auto& dev_ctx = ctx.device_context(); // call new kernel - pten::MatmulGradKernel(dev_ctx, *pt_x, *pt_y, *pt_dout, transpose_x, - transpose_y, pt_dx.get(), pt_dy.get()); + pten::MatmulGradKernel(dev_ctx, *x, *y, *dout, transpose_x, transpose_y, + dx, dy); } }; @@ -188,21 +175,11 @@ class MatMulV2DoubleGradKernel : public framework::OpKernel { if (dy) dy->mutable_data(context.GetPlace()); if (ddout) ddout->mutable_data(context.GetPlace()); - auto pt_x = paddle::experimental::MakePtenDenseTensor(*x); - auto pt_y = paddle::experimental::MakePtenDenseTensor(*y); - auto pt_dout = paddle::experimental::MakePtenDenseTensor(*dout); - auto pt_ddx = paddle::experimental::MakePtenDenseTensor(*ddx); - auto pt_ddy = paddle::experimental::MakePtenDenseTensor(*ddy); - auto pt_dx = paddle::experimental::MakePtenDenseTensor(*dx); - auto pt_dy = paddle::experimental::MakePtenDenseTensor(*dy); - auto pt_ddout = paddle::experimental::MakePtenDenseTensor(*ddout); - auto& dev_ctx = context.device_context(); // call new kernel - pten::MatmulDoubleGradKernel(dev_ctx, *pt_x, *pt_y, *pt_dout, *pt_ddx, - *pt_ddy, transpose_x, transpose_y, - pt_dx.get(), pt_dy.get(), pt_ddout.get()); + pten::MatmulDoubleGradKernel(dev_ctx, *x, *y, *dout, *ddx, *ddy, + transpose_x, transpose_y, dx, dy, ddout); } }; @@ -238,28 +215,11 @@ class MatMulV2TripleGradKernel : public framework::OpKernel { if (out_d_ddx) out_d_ddx->mutable_data(context.GetPlace()); if (out_d_ddy) out_d_ddy->mutable_data(context.GetPlace()); - auto pt_x = paddle::experimental::MakePtenDenseTensor(*x); - auto pt_y = paddle::experimental::MakePtenDenseTensor(*y); - auto pt_dout = paddle::experimental::MakePtenDenseTensor(*dout); - auto pt_ddx = paddle::experimental::MakePtenDenseTensor(*ddx); - auto pt_ddy = paddle::experimental::MakePtenDenseTensor(*ddy); - auto pt_d_dx = paddle::experimental::MakePtenDenseTensor(*d_dx); - auto pt_d_dy = paddle::experimental::MakePtenDenseTensor(*d_dy); - auto pt_d_ddout = paddle::experimental::MakePtenDenseTensor(*d_ddout); - - auto pt_out_d_x = paddle::experimental::MakePtenDenseTensor(*out_d_x); - auto pt_out_d_y = paddle::experimental::MakePtenDenseTensor(*out_d_y); - auto pt_out_d_dout = paddle::experimental::MakePtenDenseTensor(*out_d_dout); - auto pt_out_d_ddx = paddle::experimental::MakePtenDenseTensor(*out_d_ddx); - auto pt_out_d_ddy = paddle::experimental::MakePtenDenseTensor(*out_d_ddy); - auto& dev_ctx = context.device_context(); // call new kernel - pten::MatmulTripleGradKernel(dev_ctx, *pt_x, *pt_y, *pt_dout, *pt_ddx, - *pt_ddy, *pt_d_dx, *pt_d_dy, *pt_d_ddout, - transpose_x, transpose_y, pt_out_d_x.get(), - pt_out_d_y.get(), pt_out_d_dout.get(), - pt_out_d_ddx.get(), pt_out_d_ddy.get()); + pten::MatmulTripleGradKernel( + dev_ctx, *x, *y, *dout, *ddx, *ddy, *d_dx, *d_dy, *d_ddout, transpose_x, + transpose_y, out_d_x, out_d_y, out_d_dout, out_d_ddx, out_d_ddy); } }; diff --git a/paddle/fluid/operators/scale_op.h b/paddle/fluid/operators/scale_op.h index a6f4f6e272..da7c8c607a 100644 --- a/paddle/fluid/operators/scale_op.h +++ b/paddle/fluid/operators/scale_op.h @@ -65,12 +65,8 @@ class ScaleKernel : public framework::OpKernel { out->mutable_data(in->place()); auto& dev_ctx = ctx.device_context(); - auto pt_x = paddle::experimental::MakePtenDenseTensor(*in); - auto pt_out = paddle::experimental::MakePtenDenseTensor(*out); - // call new kernel - pten::ScaleKernel(dev_ctx, *pt_x.get(), scale, bias, bias_after_scale, - pt_out.get()); + pten::ScaleKernel(dev_ctx, *in, scale, bias, bias_after_scale, out); } }; diff --git a/paddle/fluid/operators/sign_op.h b/paddle/fluid/operators/sign_op.h index 8294cd2c5f..737560d314 100644 --- a/paddle/fluid/operators/sign_op.h +++ b/paddle/fluid/operators/sign_op.h @@ -34,11 +34,8 @@ class SignKernel : public framework::OpKernel { auto& dev_ctx = context.device_context(); out->mutable_data(x->place()); - auto pt_x = paddle::experimental::MakePtenDenseTensor(*x); - auto pt_out = paddle::experimental::MakePtenDenseTensor(*out); - // call new kernel - pten::SignKernel(dev_ctx, *pt_x.get(), pt_out.get()); + pten::SignKernel(dev_ctx, *x, out); } }; -- GitLab