From e29fb304a047d548ac77c229bee2f64709858370 Mon Sep 17 00:00:00 2001 From: BrilliantYuKaimin <91609464+BrilliantYuKaimin@users.noreply.github.com> Date: Thu, 21 Apr 2022 12:02:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4channel=5Fshuffle=E5=8F=8A?= =?UTF-8?q?=E5=85=B6=E6=A2=AF=E5=BA=A6=E7=9A=84=E6=A0=B8=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E7=9A=84=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../phi/kernels/channel_shuffle_grad_kernel.h | 2 +- paddle/phi/kernels/channel_shuffle_kernel.h | 2 +- .../cpu/channel_shuffle_grad_kernel.cc | 26 +++++++++++++++++ .../phi/kernels/cpu/channel_shuffle_kernel.cc | 26 +++++++++++++++++ .../gpu/channel_shuffle_grad_kernel.cu | 26 +++++++++++++++++ .../phi/kernels/gpu/channel_shuffle_kernel.cu | 26 +++++++++++++++++ .../channel_shuffle_grad_kernel_impl.h} | 28 ++++--------------- .../channel_shuffle_kernel_impl.h} | 28 ++++--------------- 8 files changed, 118 insertions(+), 46 deletions(-) create mode 100644 paddle/phi/kernels/cpu/channel_shuffle_grad_kernel.cc create mode 100644 paddle/phi/kernels/cpu/channel_shuffle_kernel.cc create mode 100644 paddle/phi/kernels/gpu/channel_shuffle_grad_kernel.cu create mode 100644 paddle/phi/kernels/gpu/channel_shuffle_kernel.cu rename paddle/phi/kernels/{channel_shuffle_grad_kernel.cc => impl/channel_shuffle_grad_kernel_impl.h} (71%) rename paddle/phi/kernels/{channel_shuffle_kernel.cc => impl/channel_shuffle_kernel_impl.h} (71%) diff --git a/paddle/phi/kernels/channel_shuffle_grad_kernel.h b/paddle/phi/kernels/channel_shuffle_grad_kernel.h index 4dbfd627d9da1..ac89f3336bc76 100644 --- a/paddle/phi/kernels/channel_shuffle_grad_kernel.h +++ b/paddle/phi/kernels/channel_shuffle_grad_kernel.h @@ -20,7 +20,7 @@ namespace phi { template -void ChannelShuffleGradKernel(const Context& ctx, +void ChannelShuffleGradKernel(const Context& dev_ctx, const DenseTensor& out_grad, int groups, const std::string& data_format, diff --git a/paddle/phi/kernels/channel_shuffle_kernel.h b/paddle/phi/kernels/channel_shuffle_kernel.h index 4f86f30c00b61..12de25606dd96 100644 --- a/paddle/phi/kernels/channel_shuffle_kernel.h +++ b/paddle/phi/kernels/channel_shuffle_kernel.h @@ -20,7 +20,7 @@ namespace phi { template -void ChannelShuffleKernel(const Context& ctx, +void ChannelShuffleKernel(const Context& dev_ctx, const DenseTensor& x, int groups, const std::string& data_format, diff --git a/paddle/phi/kernels/cpu/channel_shuffle_grad_kernel.cc b/paddle/phi/kernels/cpu/channel_shuffle_grad_kernel.cc new file mode 100644 index 0000000000000..fcc91b2191673 --- /dev/null +++ b/paddle/phi/kernels/cpu/channel_shuffle_grad_kernel.cc @@ -0,0 +1,26 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "paddle/phi/kernels/channel_shuffle_grad_kernel.h" +#include "paddle/phi/kernels/impl/channel_shuffle_grad_kernel_impl.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/core/kernel_registry.h" + +PD_REGISTER_KERNEL(channel_shuffle_grad, + CPU, + ALL_LAYOUT, + phi::ChannelShuffleGradKernel, + float, + double) {} diff --git a/paddle/phi/kernels/cpu/channel_shuffle_kernel.cc b/paddle/phi/kernels/cpu/channel_shuffle_kernel.cc new file mode 100644 index 0000000000000..95d19ec6a7746 --- /dev/null +++ b/paddle/phi/kernels/cpu/channel_shuffle_kernel.cc @@ -0,0 +1,26 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "paddle/phi/kernels/channel_shuffle_kernel.h" +#include "paddle/phi/kernels/impl/channel_shuffle_kernel_impl.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/core/kernel_registry.h" + +PD_REGISTER_KERNEL(channel_shuffle, + CPU, + ALL_LAYOUT, + phi::ChannelShuffleKernel, + float, + double) {} diff --git a/paddle/phi/kernels/gpu/channel_shuffle_grad_kernel.cu b/paddle/phi/kernels/gpu/channel_shuffle_grad_kernel.cu new file mode 100644 index 0000000000000..63d3d4a554f81 --- /dev/null +++ b/paddle/phi/kernels/gpu/channel_shuffle_grad_kernel.cu @@ -0,0 +1,26 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "paddle/phi/kernels/channel_shuffle_grad_kernel.h" +#include "paddle/phi/kernels/impl/channel_shuffle_grad_kernel_impl.h" + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/core/kernel_registry.h" + +PD_REGISTER_KERNEL(channel_shuffle_grad, + GPU, + ALL_LAYOUT, + phi::ChannelShuffleGradKernel, + float, + double) {} diff --git a/paddle/phi/kernels/gpu/channel_shuffle_kernel.cu b/paddle/phi/kernels/gpu/channel_shuffle_kernel.cu new file mode 100644 index 0000000000000..f85cb4aafd1dc --- /dev/null +++ b/paddle/phi/kernels/gpu/channel_shuffle_kernel.cu @@ -0,0 +1,26 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "paddle/phi/kernels/channel_shuffle_kernel.h" +#include "paddle/phi/kernels/impl/channel_shuffle_kernel_impl.h" + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/core/kernel_registry.h" + +PD_REGISTER_KERNEL(channel_shuffle, + GPU, + ALL_LAYOUT, + phi::ChannelShuffleKernel, + float, + double) {} diff --git a/paddle/phi/kernels/channel_shuffle_grad_kernel.cc b/paddle/phi/kernels/impl/channel_shuffle_grad_kernel_impl.h similarity index 71% rename from paddle/phi/kernels/channel_shuffle_grad_kernel.cc rename to paddle/phi/kernels/impl/channel_shuffle_grad_kernel_impl.h index 9ffc3302cbc7c..26bee763eca52 100644 --- a/paddle/phi/kernels/channel_shuffle_grad_kernel.cc +++ b/paddle/phi/kernels/impl/channel_shuffle_grad_kernel_impl.h @@ -12,25 +12,25 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/phi/kernels/channel_shuffle_grad_kernel.h" +#pragma once + #include #include -#include "paddle/phi/backends/all_context.h" + #include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/funcs/math_function.h" namespace phi { template -void ChannelShuffleGradKernel(const Context& ctx, +void ChannelShuffleGradKernel(const Context& dev_ctx, const DenseTensor& out_grad, int groups, const std::string& data_format, DenseTensor* x_grad) { auto* dout = &out_grad; auto* dx = x_grad; - ctx.template Alloc(dx); + dev_ctx.template Alloc(dx); bool channel_last = (data_format == "NHWC"); auto do_dims = dout->dims(); auto dx_dims = dx->dims(); @@ -51,24 +51,8 @@ void ChannelShuffleGradKernel(const Context& ctx, o.Resize({dx_dims[0], dx_dims[1], dx_dims[2], groups, dx_dims[3] / groups}); } phi::funcs::Transpose trans; - trans(ctx, t, &o, axis); + trans(dev_ctx, t, &o, axis); dx->Resize(dx_dims); } } // namespace phi - -PD_REGISTER_KERNEL(channel_shuffle_grad, - CPU, - ALL_LAYOUT, - phi::ChannelShuffleGradKernel, - float, - double) {} - -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -PD_REGISTER_KERNEL(channel_shuffle_grad, - GPU, - ALL_LAYOUT, - phi::ChannelShuffleGradKernel, - float, - double) {} -#endif diff --git a/paddle/phi/kernels/channel_shuffle_kernel.cc b/paddle/phi/kernels/impl/channel_shuffle_kernel_impl.h similarity index 71% rename from paddle/phi/kernels/channel_shuffle_kernel.cc rename to paddle/phi/kernels/impl/channel_shuffle_kernel_impl.h index 84781f4be2c1b..c723cd3622af9 100644 --- a/paddle/phi/kernels/channel_shuffle_kernel.cc +++ b/paddle/phi/kernels/impl/channel_shuffle_kernel_impl.h @@ -12,24 +12,24 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/phi/kernels/channel_shuffle_kernel.h" +#pragma once + #include #include -#include "paddle/phi/backends/all_context.h" + #include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/funcs/math_function.h" namespace phi { template -void ChannelShuffleKernel(const Context& ctx, +void ChannelShuffleKernel(const Context& dev_ctx, const DenseTensor& x, int groups, const std::string& data_format, DenseTensor* out) { auto* in = &x; - ctx.template Alloc(out); + dev_ctx.template Alloc(out); bool channel_last = (data_format == "NHWC"); auto in_dims = in->dims(); auto o_dims = out->dims(); @@ -50,24 +50,8 @@ void ChannelShuffleKernel(const Context& ctx, o.Resize({in_dims[0], in_dims[1], in_dims[2], in_dims[3] / groups, groups}); } phi::funcs::Transpose trans; - trans(ctx, t, &o, axis); + trans(dev_ctx, t, &o, axis); out->Resize(o_dims); } } // namespace phi - -PD_REGISTER_KERNEL(channel_shuffle, - CPU, - ALL_LAYOUT, - phi::ChannelShuffleKernel, - float, - double) {} - -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -PD_REGISTER_KERNEL(channel_shuffle, - GPU, - ALL_LAYOUT, - phi::ChannelShuffleKernel, - float, - double) {} -#endif