From 0c024cb9808aa4800e2970899ab4752f61c91373 Mon Sep 17 00:00:00 2001 From: chentianyu03 Date: Mon, 28 Mar 2022 15:01:24 +0800 Subject: [PATCH] [Phi]Remove in_dtype, out_dtype in redcue grad (#40906) * remove in_dtype, out_dtype in redcue grad * set the dtype and layout in noneedbufferInputs func --- paddle/fluid/imperative/layer.cc | 2 + paddle/phi/kernels/cpu/reduce_grad_kernel.cc | 35 ++++----------- .../phi/kernels/frobenius_norm_grad_kernel.h | 2 - paddle/phi/kernels/gpu/reduce_grad.h | 14 ++---- paddle/phi/kernels/gpu/reduce_grad_kernel.cu | 26 ++--------- .../impl/frobenius_norm_grad_kernel_impl.h | 4 +- paddle/phi/kernels/impl/reduce_grad.h | 15 ++----- .../impl/reduce_max_grad_kernel_impl.h | 14 +----- .../impl/reduce_min_grad_kernel_impl.h | 14 +----- .../impl/reduce_prod_grad_kernel_impl.h | 14 +----- paddle/phi/kernels/reduce_grad_kernel.h | 10 ----- paddle/phi/ops/compat/frobenius_norm_sig.cc | 9 ++-- paddle/phi/ops/compat/reduce_sig.cc | 45 +++++++++---------- 13 files changed, 52 insertions(+), 152 deletions(-) diff --git a/paddle/fluid/imperative/layer.cc b/paddle/fluid/imperative/layer.cc index f3bd85ff29..038ea57524 100644 --- a/paddle/fluid/imperative/layer.cc +++ b/paddle/fluid/imperative/layer.cc @@ -573,6 +573,8 @@ void ClearNoNeedBufferInputs(OpBase* op) { auto& old_tensor = var.Get(); new_tensor->Resize(old_tensor.dims()); new_tensor->set_lod(old_tensor.lod()); + new_tensor->set_type(old_tensor.dtype()); + new_tensor->set_layout(old_tensor.layout()); each_var.reset(new_var); } } diff --git a/paddle/phi/kernels/cpu/reduce_grad_kernel.cc b/paddle/phi/kernels/cpu/reduce_grad_kernel.cc index 4b3b1fc16e..d634738611 100644 --- a/paddle/phi/kernels/cpu/reduce_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/reduce_grad_kernel.cc @@ -79,34 +79,25 @@ void ReduceSumGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad) { if (dims.size() == 1) { - if (out_dtype != DataType::UNDEFINED) { - DenseTensorMeta x_grad_meta(out_dtype, x_grad->dims(), x_grad->layout()); + if (out_grad.dtype() != x.dtype()) { + DenseTensorMeta x_grad_meta( + out_grad.dtype(), x_grad->dims(), x_grad->layout()); DenseTensor x_grad_tmp = phi::Empty(dev_ctx, std::move(x_grad_meta)); ComputeFromInput(dev_ctx, x, out_grad, dims, &x_grad_tmp); - phi::CastKernel(dev_ctx, x_grad_tmp, in_dtype, x_grad); + phi::CastKernel(dev_ctx, x_grad_tmp, x.dtype(), x_grad); } else { ComputeFromInput(dev_ctx, x, out_grad, dims, x_grad); } } - ReduceGradKernel(dev_ctx, - x, - paddle::none, - out_grad, - dims, - keep_dim, - reduce_all, - in_dtype, - out_dtype, - x_grad); + ReduceGradKernel( + dev_ctx, x, paddle::none, out_grad, dims, keep_dim, reduce_all, x_grad); } template @@ -116,19 +107,9 @@ void ReduceMeanGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad) { - ReduceGradKernel(dev_ctx, - x, - paddle::none, - out_grad, - dims, - keep_dim, - reduce_all, - in_dtype, - out_dtype, - x_grad); + ReduceGradKernel( + dev_ctx, x, paddle::none, out_grad, dims, keep_dim, reduce_all, x_grad); } } // namespace phi diff --git a/paddle/phi/kernels/frobenius_norm_grad_kernel.h b/paddle/phi/kernels/frobenius_norm_grad_kernel.h index edf3aed8b8..cfe8192d1a 100644 --- a/paddle/phi/kernels/frobenius_norm_grad_kernel.h +++ b/paddle/phi/kernels/frobenius_norm_grad_kernel.h @@ -27,7 +27,5 @@ void FrobeniusNormGradKernel(const Context& ctx, const std::vector& axis, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* dx); } // namespace phi diff --git a/paddle/phi/kernels/gpu/reduce_grad.h b/paddle/phi/kernels/gpu/reduce_grad.h index e32101b737..1e39a08e9c 100644 --- a/paddle/phi/kernels/gpu/reduce_grad.h +++ b/paddle/phi/kernels/gpu/reduce_grad.h @@ -52,14 +52,12 @@ void ReduceGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad) { auto* in_x = &x; auto* d_out = &out_grad; auto* d_x = x_grad; - auto pt_out_dtype = in_dtype; + auto pt_out_dtype = x.dtype(); // get reduce_dim and reduce_num for reduce_mean_grad int dim_size = in_x->dims().size(); @@ -76,17 +74,11 @@ void ReduceGradKernel(const Context& dev_ctx, DenseTensor new_d_out(d_out->dtype()); new_d_out.ShareDataWith(*d_out); new_d_out.Resize(phi::make_ddim(update_dims)); - if (in_dtype != DataType::UNDEFINED) { - dev_ctx.Alloc(d_x, in_dtype); - } else { - dev_ctx.Alloc(d_x, d_out->dtype()); - } + + dev_ctx.Alloc(d_x, x.dtype()); auto pt_d_out = new_d_out; auto pt_d_x = *d_x; - if (in_dtype == DataType::UNDEFINED) { - pt_out_dtype = d_out->dtype(); - } using MPType = typename kps::details::MPTypeTrait::Type; phi::ReduceGrad>( diff --git a/paddle/phi/kernels/gpu/reduce_grad_kernel.cu b/paddle/phi/kernels/gpu/reduce_grad_kernel.cu index 5256048267..0b59ba3ad3 100644 --- a/paddle/phi/kernels/gpu/reduce_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/reduce_grad_kernel.cu @@ -31,18 +31,9 @@ void ReduceSumGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad) { - ReduceGradKernel(dev_ctx, - x, - out_grad, - dims, - keep_dim, - reduce_all, - in_dtype, - out_dtype, - x_grad); + ReduceGradKernel( + dev_ctx, x, out_grad, dims, keep_dim, reduce_all, x_grad); } template @@ -52,18 +43,9 @@ void ReduceMeanGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad) { - ReduceGradKernel(dev_ctx, - x, - out_grad, - dims, - keep_dim, - reduce_all, - in_dtype, - out_dtype, - x_grad); + ReduceGradKernel( + dev_ctx, x, out_grad, dims, keep_dim, reduce_all, x_grad); } } // namespace phi diff --git a/paddle/phi/kernels/impl/frobenius_norm_grad_kernel_impl.h b/paddle/phi/kernels/impl/frobenius_norm_grad_kernel_impl.h index 1877a4ecc2..220de197c8 100644 --- a/paddle/phi/kernels/impl/frobenius_norm_grad_kernel_impl.h +++ b/paddle/phi/kernels/impl/frobenius_norm_grad_kernel_impl.h @@ -29,11 +29,9 @@ void FrobeniusNormGradKernel(const Context& ctx, const std::vector& axis, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* dx) { ReduceGradKernel( - ctx, x, out, dout, axis, keep_dim, reduce_all, in_dtype, out_dtype, dx); + ctx, x, out, dout, axis, keep_dim, reduce_all, dx); } } // namespace phi diff --git a/paddle/phi/kernels/impl/reduce_grad.h b/paddle/phi/kernels/impl/reduce_grad.h index 0b1c43b5f0..8dcd3c2ba8 100644 --- a/paddle/phi/kernels/impl/reduce_grad.h +++ b/paddle/phi/kernels/impl/reduce_grad.h @@ -33,8 +33,6 @@ void ComputeFromInput(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad) { auto* input0 = &x; auto* input1 = out.get_ptr(); @@ -92,11 +90,10 @@ void ReduceGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad) { - if (in_dtype != DataType::UNDEFINED) { - DenseTensorMeta x_grad_meta(out_dtype, x_grad->dims(), x_grad->layout()); + if (x.dtype() != out_grad.dtype()) { + DenseTensorMeta x_grad_meta( + out_grad.dtype(), x_grad->dims(), x_grad->layout()); DenseTensor x_grad_tmp = phi::Empty(dev_ctx, std::move(x_grad_meta)); ComputeFromInput( @@ -108,11 +105,9 @@ void ReduceGradKernel(const Context& dev_ctx, dims, keep_dim, reduce_all, - in_dtype, - out_dtype, &x_grad_tmp); - phi::CastKernel(dev_ctx, x_grad_tmp, in_dtype, x_grad); + phi::CastKernel(dev_ctx, x_grad_tmp, x.dtype(), x_grad); } else { ComputeFromInput( dev_ctx, @@ -123,8 +118,6 @@ void ReduceGradKernel(const Context& dev_ctx, dims, keep_dim, reduce_all, - in_dtype, - out_dtype, x_grad); } } diff --git a/paddle/phi/kernels/impl/reduce_max_grad_kernel_impl.h b/paddle/phi/kernels/impl/reduce_max_grad_kernel_impl.h index 0a0c1abac8..cf11eb4d5a 100644 --- a/paddle/phi/kernels/impl/reduce_max_grad_kernel_impl.h +++ b/paddle/phi/kernels/impl/reduce_max_grad_kernel_impl.h @@ -29,19 +29,9 @@ void ReduceMaxGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad) { - ReduceGradKernel(dev_ctx, - x, - out, - out_grad, - dims, - keep_dim, - reduce_all, - in_dtype, - out_dtype, - x_grad); + ReduceGradKernel( + dev_ctx, x, out, out_grad, dims, keep_dim, reduce_all, x_grad); } } // namespace phi diff --git a/paddle/phi/kernels/impl/reduce_min_grad_kernel_impl.h b/paddle/phi/kernels/impl/reduce_min_grad_kernel_impl.h index 965fc686e2..50d0645abc 100644 --- a/paddle/phi/kernels/impl/reduce_min_grad_kernel_impl.h +++ b/paddle/phi/kernels/impl/reduce_min_grad_kernel_impl.h @@ -29,19 +29,9 @@ void ReduceMinGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad) { - ReduceGradKernel(dev_ctx, - x, - out, - out_grad, - dims, - keep_dim, - reduce_all, - in_dtype, - out_dtype, - x_grad); + ReduceGradKernel( + dev_ctx, x, out, out_grad, dims, keep_dim, reduce_all, x_grad); } } // namespace phi diff --git a/paddle/phi/kernels/impl/reduce_prod_grad_kernel_impl.h b/paddle/phi/kernels/impl/reduce_prod_grad_kernel_impl.h index fb361e3420..a81d74c845 100644 --- a/paddle/phi/kernels/impl/reduce_prod_grad_kernel_impl.h +++ b/paddle/phi/kernels/impl/reduce_prod_grad_kernel_impl.h @@ -29,19 +29,9 @@ void ReduceProdGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad) { - ReduceGradKernel(dev_ctx, - x, - out, - out_grad, - dims, - keep_dim, - reduce_all, - in_dtype, - out_dtype, - x_grad); + ReduceGradKernel( + dev_ctx, x, out, out_grad, dims, keep_dim, reduce_all, x_grad); } } // namespace phi diff --git a/paddle/phi/kernels/reduce_grad_kernel.h b/paddle/phi/kernels/reduce_grad_kernel.h index a4b472c445..a88b8f92ae 100644 --- a/paddle/phi/kernels/reduce_grad_kernel.h +++ b/paddle/phi/kernels/reduce_grad_kernel.h @@ -25,8 +25,6 @@ void ReduceSumGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad); template @@ -36,8 +34,6 @@ void ReduceMeanGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad); template @@ -48,8 +44,6 @@ void ReduceProdGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad); template @@ -60,8 +54,6 @@ void ReduceMaxGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad); template @@ -72,8 +64,6 @@ void ReduceMinGradKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, bool reduce_all, - DataType in_dtype, - DataType out_dtype, DenseTensor* x_grad); } // namespace phi diff --git a/paddle/phi/ops/compat/frobenius_norm_sig.cc b/paddle/phi/ops/compat/frobenius_norm_sig.cc index c6dc5ad901..8fddee5edb 100644 --- a/paddle/phi/ops/compat/frobenius_norm_sig.cc +++ b/paddle/phi/ops/compat/frobenius_norm_sig.cc @@ -24,11 +24,10 @@ KernelSignature FrobeniusNormOpArgumentMapping( KernelSignature FrobeniusNormGradOpArgumentMapping( const ArgumentMappingContext& ctx) { - return KernelSignature( - "frobenius_norm_grad", - {"X", "Out", GradVarName("Out")}, - {"dim", "keep_dim", "reduce_all", "in_dtype", "out_dtype"}, - {GradVarName("X")}); + return KernelSignature("frobenius_norm_grad", + {"X", "Out", GradVarName("Out")}, + {"dim", "keep_dim", "reduce_all"}, + {GradVarName("X")}); } } // namespace phi diff --git a/paddle/phi/ops/compat/reduce_sig.cc b/paddle/phi/ops/compat/reduce_sig.cc index 273badee62..cf2edf9f20 100644 --- a/paddle/phi/ops/compat/reduce_sig.cc +++ b/paddle/phi/ops/compat/reduce_sig.cc @@ -129,47 +129,42 @@ KernelSignature ReduceAllOpArgumentMapping(const ArgumentMappingContext& ctx) { KernelSignature ReduceSumGradOpArgumentMapping( const ArgumentMappingContext& ctx) { - return KernelSignature( - "sum_grad", - {"X", GradVarName("Out")}, - {"dim", "keep_dim", "reduce_all", "in_dtype", "out_dtype"}, - {GradVarName("X")}); + return KernelSignature("sum_grad", + {"X", GradVarName("Out")}, + {"dim", "keep_dim", "reduce_all"}, + {GradVarName("X")}); } KernelSignature ReduceMeanGradOpArgumentMapping( const ArgumentMappingContext& ctx) { - return KernelSignature( - "mean_grad", - {"X", GradVarName("Out")}, - {"dim", "keep_dim", "reduce_all", "in_dtype", "out_dtype"}, - {GradVarName("X")}); + return KernelSignature("mean_grad", + {"X", GradVarName("Out")}, + {"dim", "keep_dim", "reduce_all"}, + {GradVarName("X")}); } KernelSignature ReduceMaxGradOpArgumentMapping( const ArgumentMappingContext& ctx) { - return KernelSignature( - "max_grad", - {"X", "Out", GradVarName("Out")}, - {"dim", "keep_dim", "reduce_all", "in_dtype", "out_dtype"}, - {GradVarName("X")}); + return KernelSignature("max_grad", + {"X", "Out", GradVarName("Out")}, + {"dim", "keep_dim", "reduce_all"}, + {GradVarName("X")}); } KernelSignature ReduceMinGradOpArgumentMapping( const ArgumentMappingContext& ctx) { - return KernelSignature( - "min_grad", - {"X", "Out", GradVarName("Out")}, - {"dim", "keep_dim", "reduce_all", "in_dtype", "out_dtype"}, - {GradVarName("X")}); + return KernelSignature("min_grad", + {"X", "Out", GradVarName("Out")}, + {"dim", "keep_dim", "reduce_all"}, + {GradVarName("X")}); } KernelSignature ReduceProdGradOpArgumentMapping( const ArgumentMappingContext& ctx) { - return KernelSignature( - "prod_grad", - {"X", "Out", GradVarName("Out")}, - {"dim", "keep_dim", "reduce_all", "in_dtype", "out_dtype"}, - {GradVarName("X")}); + return KernelSignature("prod_grad", + {"X", "Out", GradVarName("Out")}, + {"dim", "keep_dim", "reduce_all"}, + {GradVarName("X")}); } } // namespace phi -- GitLab