未验证 提交 9ed58bff 编写于 作者: W Wang Xin 提交者: GitHub

move c_identity to phi (#56215)

上级 5c6ae96b
......@@ -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<T> {
} // 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<paddle::framework::OpDesc>,
ops::CIdentityOpGradMaker<paddle::imperative::OpBase>,
ops::CIdentityOpMaker);
PD_REGISTER_STRUCT_KERNEL(c_identity,
CPU,
ALL_LAYOUT,
ops::CIdentityOpCPUKernel,
float,
double,
int,
int64_t,
plat::float16) {}
ops::CIdentityOpMaker,
CIdentityShapeFunctor);
/* 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 <algorithm>
#include <utility>
#include <vector>
#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 <typename T, typename DeviceContext>
class CIdentityOpCPUKernel : public framework::OpKernel<T> {
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 <typename T, typename DeviceContext>
class CIdentityOpKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
auto x = ctx.Input<phi::DenseTensor>("X");
auto out = ctx.Output<phi::DenseTensor>("Out");
int rid = ctx.Attr<int>("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<T>(ctx.GetPlace());
paddle::framework::TensorCopy(*x, out->place(), out);
}
};
} // namespace operators
} // namespace paddle
......@@ -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<T> {
}
};
template <typename DeviceContext, typename T>
class CIdentityOpCustomDeviceKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
auto x = ctx.Input<phi::DenseTensor>("X");
auto out = ctx.Output<phi::DenseTensor>("Out");
int rid = ctx.Attr<int>("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<T>(out);
paddle::framework::TensorCopy(*x, out->place(), out);
}
};
template <typename DeviceContext, typename T>
class CSplitOpCustomDeviceKernel : public framework::OpKernel<T> {
public:
......@@ -1363,17 +1381,22 @@ void RegisterCustomDeviceCommonKernel(const std::string& dev_type) {
REGISTER_OP_CUSTOM_DEVICE_KERNEL(
c_identity,
device_type,
paddle::operators::
CIdentityOpKernel<float, paddle::platform::CustomDeviceContext>,
paddle::operators::
CIdentityOpKernel<double, paddle::platform::CustomDeviceContext>,
paddle::operators::
CIdentityOpKernel<int, paddle::platform::CustomDeviceContext>,
paddle::operators::
CIdentityOpKernel<int64_t, paddle::platform::CustomDeviceContext>,
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,
......
......@@ -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);
......
......@@ -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,
......
/* 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 <typename T, typename Context>
void CIdentityKernel(const Context& dev_ctx,
const DenseTensor& x,
int ring_id,
bool use_calc_stream,
bool use_model_parallel,
DenseTensor* out);
} // namespace phi
/* 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 <typename T, typename Context>
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) {}
/* Copyright (c) 2021 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.
......@@ -12,21 +12,19 @@ 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/phi/kernels/c_identity_kernel.h"
#include "paddle/phi/kernels/impl/c_identity_kernel_impl.h"
namespace ops = paddle::operators;
namespace plat = paddle::platform;
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
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) {
}
PD_REGISTER_KERNEL(c_identity,
GPU,
ALL_LAYOUT,
phi::CIdentityKernel,
float,
double,
int,
int64_t,
phi::dtype::bfloat16,
phi::dtype::float16) {}
// 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 <typename T, typename Context>
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<T>(out);
phi::Copy(dev_ctx, x, dev_ctx.GetPlace(), false, out);
}
} // namespace phi
/* 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) {}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册