Skip to content

Commit

Permalink
[CustomDevice] add mp_allreduce_sum op for all custom devices (Paddle…
Browse files Browse the repository at this point in the history
  • Loading branch information
ronny1996 authored and BeingGod committed Sep 9, 2023
1 parent c8e8965 commit 6f194f8
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions paddle/fluid/operators/custom_device_common_op_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,23 @@ template <typename DeviceContext, typename T, phi::ccl::CCLReduceOp red_type>
class CAllReduceOpCustomDeviceKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
if (ctx.HasInput("Cond")) {
auto cond = ctx.Input<phi::DenseTensor>("Cond");
auto place = cond->place();
PADDLE_ENFORCE_EQ(platform::is_cpu_place(place),
true,
platform::errors::PreconditionNotMet(
"The input `cond` tensor should be on cpu place"));
PADDLE_ENFORCE_EQ(cond->numel(),
1,
platform::errors::PreconditionNotMet(
"The input `cond` should be shape [1]"));
if (!cond->data<bool>()[0]) {
VLOG(4) << "Skip all reduce Op since cond is 0";
return;
}
}

auto in = ctx.Input<phi::DenseTensor>("X");
auto out = ctx.Output<phi::DenseTensor>("Out");
int rid = ctx.Attr<int>("ring_id");
Expand Down Expand Up @@ -1441,6 +1458,29 @@ void RegisterCustomDeviceCommonKernel(const std::string& dev_type) {
paddle::platform::CustomDeviceContext,
int64_t,
phi::ccl::CCLReduceOp::SUM>) {}
REGISTER_OP_CUSTOM_DEVICE_KERNEL(
mp_allreduce_sum,
device_type,
paddle::operators::CAllReduceOpCustomDeviceKernel<
paddle::platform::CustomDeviceContext,
float,
phi::ccl::CCLReduceOp::SUM>,
paddle::operators::CAllReduceOpCustomDeviceKernel<
paddle::platform::CustomDeviceContext,
double,
phi::ccl::CCLReduceOp::SUM>,
paddle::operators::CAllReduceOpCustomDeviceKernel<
paddle::platform::CustomDeviceContext,
paddle::platform::float16,
phi::ccl::CCLReduceOp::SUM>,
paddle::operators::CAllReduceOpCustomDeviceKernel<
paddle::platform::CustomDeviceContext,
int32_t,
phi::ccl::CCLReduceOp::SUM>,
paddle::operators::CAllReduceOpCustomDeviceKernel<
paddle::platform::CustomDeviceContext,
int64_t,
phi::ccl::CCLReduceOp::SUM>) {}
REGISTER_OP_CUSTOM_DEVICE_KERNEL(
c_allreduce_min,
device_type,
Expand Down

0 comments on commit 6f194f8

Please sign in to comment.