/* Copyright (c) 2016 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/licenseshashernless 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 #include #include "paddle/fluid/framework/lod_tensor.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/nccl/nccl_gpu_common.h" namespace paddle { namespace operators { using framework::Tensor; using platform::Communicator; using framework::LoDTensor; template class NCCLTypeWrapper; template <> class NCCLTypeWrapper { public: static const ncclDataType_t type = ncclFloat; }; template <> class NCCLTypeWrapper { public: static const ncclDataType_t type = ncclDouble; }; static ncclRedOp_t str_to_nccl_red_type(std::string reduction) { static const std::unordered_map str_to_type = { {"ncclSum", ncclSum}, {"ncclMin", ncclMin}, {"ncclMax", ncclMax}, {"ncclProd", ncclProd}, }; auto it = str_to_type.find(reduction); PADDLE_ENFORCE_EQ(it != str_to_type.end(), true, platform::errors::InvalidArgument( "Invalid nccl reduction. Must be ncclMin | ncclMax | " "ncclProd | ncclSum")); return it->second; } template class NCCLAllReduceKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { PADDLE_ENFORCE_EQ(platform::is_gpu_place(ctx.GetPlace()), true, platform::errors::PreconditionNotMet( "This kernel only runs on GPU device.")); auto* x = ctx.Input("X"); auto* out = ctx.Output("Out"); auto* comm = ctx.Input("Communicator"); std::string reduction = ctx.Attr("reduction"); auto reduction_op_ = str_to_nccl_red_type(reduction); // device id int gpu_id = boost::get(ctx.GetPlace()).GetDeviceId(); int idx = comm->GetCommId(gpu_id); VLOG(3) << "gpu : " << " invoke allreduce. send " << x->numel() << " recv " << out->numel(); PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::ncclAllReduce( x->data(), out->mutable_data(ctx.GetPlace()), out->numel(), NCCLTypeWrapper::type, reduction_op_, comm->comms().at(idx), ctx.cuda_device_context().stream())); VLOG(3) << "gpu : " << " finished allreduce. send " << x->numel() << " recv " << out->numel(); } }; template class NCCLReduceKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { PADDLE_ENFORCE_EQ(platform::is_gpu_place(ctx.GetPlace()), true, platform::errors::InvalidArgument( "This kernel only runs on GPU device.")); auto x = ctx.Input("X"); // x0, x1, x2 auto out = ctx.Output("Out"); auto* comm = ctx.Input("Communicator"); int root = ctx.Attr("root"); std::string reduction = ctx.Attr("reduction"); auto reduction_op_ = str_to_nccl_red_type(reduction); // device id int gpu_id = boost::get(ctx.GetPlace()).GetDeviceId(); int idx = comm->GetCommId(gpu_id); T* recvbuffer = nullptr; if (root == gpu_id) { recvbuffer = out->mutable_data(ctx.GetPlace()); } else { out->Resize(framework::make_ddim({0})); } VLOG(3) << "gpu : " << gpu_id << " invoke reduce. send " << x->numel() << " recv " << out->numel(); PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::ncclReduce( x->data(), recvbuffer, x->numel(), NCCLTypeWrapper::type, reduction_op_, root, comm->comms().at(idx), ctx.cuda_device_context().stream())); VLOG(3) << "gpu : " << gpu_id << " finished reduce. send " << x->numel() << " recv " << out->numel(); } }; template class NCCLBcastKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { PADDLE_ENFORCE_EQ(platform::is_gpu_place(ctx.GetPlace()), true, platform::errors::InvalidArgument( "This kernel only runs on GPU device.")); int root = ctx.Attr("root"); auto* comm = ctx.Input("Communicator"); // device id int gpu_id = boost::get(ctx.GetPlace()).GetDeviceId(); int idx = comm->GetCommId(gpu_id); if (idx == root) { auto* x = ctx.Input("X"); VLOG(3) << "gpu : " << gpu_id << " invoke Bcast. send " << x->numel(); PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::ncclBcast( reinterpret_cast(const_cast(x->data())), x->numel(), NCCLTypeWrapper::type, root, comm->comms().at(idx), ctx.cuda_device_context().stream())); VLOG(3) << "gpu : " << gpu_id << " finished Bcast."; } else { auto* out = ctx.Output("Out"); VLOG(3) << "gpu : " << gpu_id << " invoke Bcast. recv buffer " << framework::product(out->dims()); PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::ncclBcast( out->mutable_data(ctx.GetPlace()), out->numel(), NCCLTypeWrapper::type, root, comm->comms().at(idx), ctx.cuda_device_context().stream())); VLOG(3) << "gpu : " << gpu_id << " finished Bcast. recv " << out->numel(); } } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OP_CUDA_KERNEL(ncclAllReduce, ops::NCCLAllReduceKernel); REGISTER_OP_CUDA_KERNEL(ncclBcast, ops::NCCLBcastKernel); REGISTER_OP_CUDA_KERNEL(ncclReduce, ops::NCCLReduceKernel);