|
| 1 | +// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include "paddle/phi/backends/all_context.h" |
| 18 | +#include "paddle/phi/backends/device_manager.h" |
| 19 | +#include "paddle/phi/core/distributed/collective/process_group.h" |
| 20 | +#include "paddle/phi/core/distributed/comm_context_manager.h" |
| 21 | +#include "paddle/phi/core/distributed/xccl_comm_context.h" |
| 22 | +#include "paddle/phi/core/kernel_registry.h" |
| 23 | +#include "paddle/phi/core/tensor_utils.h" |
| 24 | +#ifdef PADDLE_WITH_CUSTOM_DEVICE |
| 25 | +namespace phi { |
| 26 | + |
| 27 | +template <typename T, typename Context, phi::ccl::CCLReduceOp red_type> |
| 28 | +void CAllReduceKernel(const Context& dev_ctx, |
| 29 | + const DenseTensor& x_in, |
| 30 | + int ring_id, |
| 31 | + bool use_calc_stream, |
| 32 | + bool use_model_parallel, |
| 33 | + DenseTensor* out) { |
| 34 | + auto in = &x_in; |
| 35 | + int rid = ring_id; |
| 36 | + |
| 37 | + auto place = dev_ctx.GetPlace(); |
| 38 | + auto dtype = in->dtype(); |
| 39 | + int64_t numel = in->numel(); |
| 40 | + const void* sendbuff = in->data<T>(); |
| 41 | + out->Resize(in->dims()); |
| 42 | + void* recvbuff = dev_ctx.template Alloc<T>(out); |
| 43 | + |
| 44 | + auto map = phi::distributed::ProcessGroupMapFromGid::getInstance(); |
| 45 | + if (map->has(rid)) { |
| 46 | + // Use ProcessGroup |
| 47 | + phi::distributed::ProcessGroup* pg = map->get(rid); |
| 48 | + std::vector<phi::DenseTensor> in_tensor; |
| 49 | + std::vector<phi::DenseTensor> out_tensor; |
| 50 | + in_tensor.push_back(*in); |
| 51 | + out_tensor.push_back(*out); |
| 52 | + |
| 53 | + phi::distributed::AllreduceOptions opts; |
| 54 | + switch (red_type) { |
| 55 | + case phi::ccl::CCLReduceOp::SUM: |
| 56 | + opts.reduce_op = phi::distributed::ReduceOp::SUM; |
| 57 | + break; |
| 58 | + |
| 59 | + case phi::ccl::CCLReduceOp::MAX: |
| 60 | + opts.reduce_op = phi::distributed::ReduceOp::MAX; |
| 61 | + break; |
| 62 | + |
| 63 | + case phi::ccl::CCLReduceOp::MIN: |
| 64 | + opts.reduce_op = phi::distributed::ReduceOp::MIN; |
| 65 | + break; |
| 66 | + |
| 67 | + case phi::ccl::CCLReduceOp::PRODUCT: |
| 68 | + opts.reduce_op = phi::distributed::ReduceOp::PRODUCT; |
| 69 | + break; |
| 70 | + |
| 71 | + default: |
| 72 | + PADDLE_THROW(common::errors::InvalidArgument("Invalid reduce type: %d", |
| 73 | + red_type)); |
| 74 | + } |
| 75 | + |
| 76 | + auto task = pg->AllReduce(in_tensor, out_tensor, opts); |
| 77 | + task->Wait(); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + auto comm = reinterpret_cast<phi::distributed::XCCLCommContext*>( |
| 82 | + phi::distributed::CommContextManager::GetInstance().Get( |
| 83 | + std::to_string(rid))); |
| 84 | + |
| 85 | + std::shared_ptr<phi::stream::Stream> stream; |
| 86 | + if (use_calc_stream) { |
| 87 | + stream = dev_ctx.GetStream(); |
| 88 | + } else { |
| 89 | + stream = comm->GetStream(); |
| 90 | + } |
| 91 | + phi::DeviceManager::CCLAllReduce(place.GetDeviceType(), |
| 92 | + const_cast<void*>(sendbuff), |
| 93 | + recvbuff, |
| 94 | + numel, |
| 95 | + dtype, |
| 96 | + red_type, |
| 97 | + comm->GetXcclComm(), |
| 98 | + *stream); |
| 99 | +} |
| 100 | +} // namespace phi |
| 101 | + |
| 102 | +#endif |
0 commit comments