diff --git a/paddle/fluid/operators/collective/c_identity_op.cc b/paddle/fluid/operators/collective/c_identity_op.cc index ca47e3c983cb82..c067c061b8613a 100644 --- a/paddle/fluid/operators/collective/c_identity_op.cc +++ b/paddle/fluid/operators/collective/c_identity_op.cc @@ -12,7 +12,10 @@ 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/fluid/operators/collective/c_identity_op.h" +#include "paddle/fluid/framework/infershape_utils.h" +#include "paddle/fluid/framework/op_registry.h" + +#include "paddle/phi/infermeta/unary.h" namespace paddle { namespace operators { @@ -79,20 +82,14 @@ class CIdentityOpGradMaker : public framework::SingleGradOpMaker { } // namespace paddle namespace ops = paddle::operators; -namespace plat = paddle::platform; + +DECLARE_INFER_SHAPE_FUNCTOR(c_identity, + CIdentityShapeFunctor, + PD_INFER_META(phi::CIdentityInferMeta)); REGISTER_OPERATOR(c_identity, ops::CIdentityOp, ops::CIdentityOpGradMaker, ops::CIdentityOpGradMaker, - ops::CIdentityOpMaker); - -PD_REGISTER_STRUCT_KERNEL(c_identity, - CPU, - ALL_LAYOUT, - ops::CIdentityOpCPUKernel, - float, - double, - int, - int64_t, - plat::float16) {} + ops::CIdentityOpMaker, + CIdentityShapeFunctor); diff --git a/paddle/fluid/operators/collective/c_identity_op.cu.cc b/paddle/fluid/operators/collective/c_identity_op.cu.cc deleted file mode 100644 index 9571168db152c6..00000000000000 --- a/paddle/fluid/operators/collective/c_identity_op.cu.cc +++ /dev/null @@ -1,32 +0,0 @@ -/* Copyright (c) 2021 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/fluid/operators/collective/c_identity_op.h" - -namespace ops = paddle::operators; -namespace plat = paddle::platform; - -PD_REGISTER_STRUCT_KERNEL(c_identity, - GPU, - ALL_LAYOUT, - ops::CIdentityOpKernel, - float, - double, - int, - int64_t, -#if NCCL_VERSION_CODE >= 21000 && CUDA_VERSION >= 11000 - plat::bfloat16, -#endif - plat::float16) { -} diff --git a/paddle/fluid/operators/collective/c_identity_op.h b/paddle/fluid/operators/collective/c_identity_op.h deleted file mode 100644 index f6fdc28d9ec1c3..00000000000000 --- a/paddle/fluid/operators/collective/c_identity_op.h +++ /dev/null @@ -1,57 +0,0 @@ -/* Copyright (c) 2021 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. */ - -#pragma once - -#include -#include -#include - -#include "paddle/fluid/framework/data_type.h" -#include "paddle/fluid/framework/lod_tensor.h" -#include "paddle/fluid/framework/op_registry.h" - -namespace paddle { -namespace operators { - -template -class CIdentityOpCPUKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& ctx UNUSED) const override { - PADDLE_THROW(platform::errors::Unavailable( - "Do not support c_identity for cpu kernel now.")); - } -}; - -template -class CIdentityOpKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& ctx) const override { - auto x = ctx.Input("X"); - auto out = ctx.Output("Out"); - - int rid = ctx.Attr("ring_id"); - PADDLE_ENFORCE_GE( - rid, - 0, - platform::errors::InvalidArgument( - "The ring_id (%d) for c_identity op must be non-negative.", rid)); - out->mutable_data(ctx.GetPlace()); - - paddle::framework::TensorCopy(*x, out->place(), out); - } -}; - -} // namespace operators -} // namespace paddle diff --git a/paddle/fluid/operators/custom_device_common_op_registry.cc b/paddle/fluid/operators/custom_device_common_op_registry.cc index d681bdcd58d8ff..df4d65af1d03f0 100644 --- a/paddle/fluid/operators/custom_device_common_op_registry.cc +++ b/paddle/fluid/operators/custom_device_common_op_registry.cc @@ -15,7 +15,6 @@ limitations under the License. */ #include "paddle/fluid/operators/custom_device_common_op_registry.h" #include "paddle/fluid/distributed/collective/process_group.h" #include "paddle/fluid/operators/collective/c_concat_op.h" -#include "paddle/fluid/operators/collective/c_identity_op.h" #include "paddle/fluid/operators/load_combine_op.h" #include "paddle/fluid/operators/run_program_op.h" #include "paddle/fluid/operators/save_combine_op.h" @@ -147,6 +146,25 @@ class CConcatOpCustomDeviceKernel : public framework::OpKernel { } }; +template +class CIdentityOpCustomDeviceKernel : public framework::OpKernel { + public: + void Compute(const framework::ExecutionContext& ctx) const override { + auto x = ctx.Input("X"); + auto out = ctx.Output("Out"); + + int rid = ctx.Attr("ring_id"); + PADDLE_ENFORCE_GE( + rid, + 0, + platform::errors::InvalidArgument( + "The ring_id (%d) for c_identity op must be non-negative.", rid)); + ctx.device_context().Alloc(out); + + paddle::framework::TensorCopy(*x, out->place(), out); + } +}; + template class CSplitOpCustomDeviceKernel : public framework::OpKernel { public: @@ -1363,17 +1381,22 @@ void RegisterCustomDeviceCommonKernel(const std::string& dev_type) { REGISTER_OP_CUSTOM_DEVICE_KERNEL( c_identity, device_type, - paddle::operators:: - CIdentityOpKernel, - paddle::operators:: - CIdentityOpKernel, - paddle::operators:: - CIdentityOpKernel, - paddle::operators:: - CIdentityOpKernel, - paddle::operators::CIdentityOpKernel< - paddle::platform::float16, - paddle::platform::CustomDeviceContext>) {} + paddle::operators::CIdentityOpCustomDeviceKernel< + paddle::platform::CustomDeviceContext, + float>, + paddle::operators::CIdentityOpCustomDeviceKernel< + paddle::platform::CustomDeviceContext, + double>, + paddle::operators::CIdentityOpCustomDeviceKernel< + paddle::platform::CustomDeviceContext, + int>, + paddle::operators::CIdentityOpCustomDeviceKernel< + paddle::platform::CustomDeviceContext, + int64_t>, + paddle::operators::CIdentityOpCustomDeviceKernel< + paddle::platform::CustomDeviceContext, + paddle::platform::float16>) {} + REGISTER_OP_CUSTOM_DEVICE_KERNEL( c_sync_calc_stream, device_type, diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index e30a1098b71517..db4964dc3dd408 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -461,6 +461,20 @@ void ClipByNormInferMeta(const MetaTensor& x, float max_norm, MetaTensor* out) { out->share_lod(x); } +void CIdentityInferMeta(const MetaTensor& x, + int ring_id, + bool use_calc_stream, + bool use_model_parallel, + MetaTensor* out) { + PADDLE_ENFORCE_GE( + ring_id, + 0, + errors::InvalidArgument( + "The ring_id (%d) for c_identity must be non-negative.", ring_id)); + out->set_dims(x.dims()); + out->set_dtype(x.dtype()); +} + void CreateLikeInferMeta(const MetaTensor& x, DataType dtype, MetaTensor* out) { out->set_dims(x.dims()); out->set_dtype(dtype == DataType::UNDEFINED ? x.dtype() : dtype); diff --git a/paddle/phi/infermeta/unary.h b/paddle/phi/infermeta/unary.h index 3a3f6f41f6abbe..14c3ec0f857bda 100644 --- a/paddle/phi/infermeta/unary.h +++ b/paddle/phi/infermeta/unary.h @@ -102,6 +102,12 @@ void ClassCenterSampleInferMeta(const MetaTensor& label, void ClipByNormInferMeta(const MetaTensor& x, float max_norm, MetaTensor* out); +void CIdentityInferMeta(const MetaTensor& x, + int ring_id, + bool use_calc_stream, + bool use_model_parallel, + MetaTensor* out); + void CreateLikeInferMeta(const MetaTensor& x, DataType dtype, MetaTensor* out); void CropInferMeta(const MetaTensor& x, diff --git a/paddle/fluid/operators/collective/c_identity_op_xpu.cc b/paddle/phi/kernels/c_identity_kernel.h similarity index 50% rename from paddle/fluid/operators/collective/c_identity_op_xpu.cc rename to paddle/phi/kernels/c_identity_kernel.h index a627d2748b6d71..6e6d1414c5ea52 100644 --- a/paddle/fluid/operators/collective/c_identity_op_xpu.cc +++ b/paddle/phi/kernels/c_identity_kernel.h @@ -1,25 +1,28 @@ -/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +/* Copyright (c) 2023 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/fluid/operators/collective/c_identity_op.h" +#pragma once + +#include "paddle/phi/core/dense_tensor.h" -namespace ops = paddle::operators; -namespace plat = paddle::platform; +namespace phi { -PD_REGISTER_STRUCT_KERNEL(c_identity, - XPU, - ALL_LAYOUT, - ops::CIdentityOpKernel, - float, - double, - int, - int64_t, - plat::float16) {} +template +void CIdentityKernel(const Context& dev_ctx, + const DenseTensor& x, + int ring_id, + bool use_calc_stream, + bool use_model_parallel, + DenseTensor* out); +} // namespace phi diff --git a/paddle/phi/kernels/cpu/c_identity_kernel.cc b/paddle/phi/kernels/cpu/c_identity_kernel.cc new file mode 100644 index 00000000000000..d93e8b6b1f2a96 --- /dev/null +++ b/paddle/phi/kernels/cpu/c_identity_kernel.cc @@ -0,0 +1,43 @@ +/* Copyright (c) 2023 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/c_identity_kernel.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/core/kernel_registry.h" + +namespace phi { + +template +void CIdentityKernel(const Context& dev_ctx, + const DenseTensor& x, + int ring_id, + bool use_calc_stream, + bool use_model_parallel, + DenseTensor* out) { + PADDLE_THROW( + errors::Unavailable("Do not support c_identity for cpu kernel now.")); +} + +} // namespace phi + +PD_REGISTER_KERNEL(c_identity, + CPU, + ALL_LAYOUT, + phi::CIdentityKernel, + float, + double, + int, + int64_t, + phi::dtype::float16) {} diff --git a/paddle/phi/kernels/gpu/c_identity_kernel.cu b/paddle/phi/kernels/gpu/c_identity_kernel.cu new file mode 100644 index 00000000000000..34efc54ab0b56b --- /dev/null +++ b/paddle/phi/kernels/gpu/c_identity_kernel.cu @@ -0,0 +1,30 @@ +/* Copyright (c) 2023 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/c_identity_kernel.h" +#include "paddle/phi/kernels/impl/c_identity_kernel_impl.h" + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/core/kernel_registry.h" + +PD_REGISTER_KERNEL(c_identity, + GPU, + ALL_LAYOUT, + phi::CIdentityKernel, + float, + double, + int, + int64_t, + phi::dtype::bfloat16, + phi::dtype::float16) {} diff --git a/paddle/phi/kernels/impl/c_identity_kernel_impl.h b/paddle/phi/kernels/impl/c_identity_kernel_impl.h new file mode 100644 index 00000000000000..0ea0e1fd203aa2 --- /dev/null +++ b/paddle/phi/kernels/impl/c_identity_kernel_impl.h @@ -0,0 +1,41 @@ +// Copyright (c) 2023 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. + +#pragma once + +#include "paddle/phi/kernels/c_identity_kernel.h" + +#include "paddle/phi/core/kernel_registry.h" + +namespace phi { + +template +void CIdentityKernel(const Context& dev_ctx, + const DenseTensor& x, + int ring_id, + bool use_calc_stream, + bool use_model_parallel, + DenseTensor* out) { + PADDLE_ENFORCE_GE( + ring_id, + 0, + errors::InvalidArgument( + "The ring_id (%d) for c_identity op must be non-negative.", ring_id)); + + dev_ctx.template Alloc(out); + + phi::Copy(dev_ctx, x, dev_ctx.GetPlace(), false, out); +} + +} // namespace phi diff --git a/paddle/phi/kernels/xpu/c_identity_kernel.cc b/paddle/phi/kernels/xpu/c_identity_kernel.cc new file mode 100644 index 00000000000000..fddf7ec94d6be5 --- /dev/null +++ b/paddle/phi/kernels/xpu/c_identity_kernel.cc @@ -0,0 +1,29 @@ +/* Copyright (c) 2023 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/c_identity_kernel.h" +#include "paddle/phi/kernels/impl/c_identity_kernel_impl.h" + +#include "paddle/phi/backends/xpu/xpu_context.h" +#include "paddle/phi/core/kernel_registry.h" + +PD_REGISTER_KERNEL(c_identity, + XPU, + ALL_LAYOUT, + phi::CIdentityKernel, + float, + double, + int, + int64_t, + phi::dtype::float16) {}