Skip to content

[Comm] support comm init for inference in phi comm ops #69653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions paddle/fluid/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,28 @@ if(WITH_XPU)
common
op_compat_infos
type_info)
elseif(WITH_NCCL OR WITH_RCCL)
cc_library(
operator
SRCS operator.cc transfer_scope_cache.cc unused_var_check.cc
infershape_utils.cc
DEPS op_info
proto_desc
tensor
scope
glog
shape_inference
data_transform
lod_tensor
op_kernel_type
op_call_stack
detail_op_handle
phi_utils
phi
common
op_compat_infos
type_info
process_group_nccl)
else()
cc_library(
operator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#include "paddle/phi/core/kernel_context.h"
#include "paddle/phi/core/kernel_factory.h"
#include "paddle/phi/core/memory/stats.h"
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
#include "paddle/fluid/distributed/collective/process_group.h"
#include "paddle/fluid/distributed/collective/process_group_nccl.h"
#endif

#ifdef PADDLE_WITH_DNNL
#include "paddle/fluid/platform/onednn_helper.h"
Expand Down Expand Up @@ -865,6 +869,40 @@ void BuildOpFuncList(const phi::Place& place,
op_func_node.phi_kernel_->GetKernelRegisteredType() ==
phi::KernelRegisteredType::FUNCTION) {
VLOG(6) << op_type << " run function kernel";
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
auto attrs = op->Attrs();
if (attrs.find("ring_id") != attrs.end()) {
auto ring_id_attr = attrs.at("ring_id");
int ring_id = PADDLE_GET(int, ring_id_attr);
auto map = distributed::ProcessGroupMapFromGid::getInstance();
if (map->has(ring_id)) {
auto original_stream =
static_cast<phi::GPUContext*>(dev_ctx)->cuda_stream();
distributed::ProcessGroup* pg = map->get(ring_id);
auto comm_context =
static_cast<paddle::distributed::ProcessGroupNCCL*>(pg)
->GetOrCreateCommContext(place);
dev_ctx =
static_cast<phi::distributed::NCCLCommContext*>(comm_context)
->GetDevContext();
dev_ctx->SetCommContext(comm_context);

static_cast<phi::GPUContext*>(dev_ctx)->SetCUDAStream(
original_stream, false);
auto& instance =
paddle::memory::allocation::AllocatorFacade::Instance();
dev_ctx->SetAllocator(
instance
.GetAllocator(
place,
static_cast<phi::GPUContext*>(dev_ctx)->stream())
.get());
} else {
VLOG(3) << "ring_id " << ring_id
<< " not found in ProcessGroupMapFromGid ";
}
}
#endif
if (static_build) {
FakeInitializeOutputsForFunctionKernel(
*op,
Expand Down
28 changes: 27 additions & 1 deletion paddle/fluid/framework/new_executor/program_interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "paddle/phi/core/platform/cuda_graph_with_memory_pool.h"
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
#include "paddle/common/flags.h"
#include "paddle/fluid/distributed/collective/process_group.h"
#include "paddle/fluid/distributed/collective/process_group_nccl.h"
#include "paddle/fluid/platform/device/gpu/nccl_helper.h"
#include "paddle/phi/core/distributed/comm_context_manager.h"
#include "paddle/phi/core/distributed/nccl_comm_context.h"
Expand Down Expand Up @@ -1010,10 +1012,34 @@ void ProgramInterpreter::RunOperator(const Instruction& instr_node) {
VLOG(4) << "Run function kernel: " << op->Type();
VLOG(4) << instr_node.InnerRuntimeContext().get() << " "
<< &instr_node.DeviceContext();

auto dev_ctx =
const_cast<phi::DeviceContext*>(&instr_node.DeviceContext());
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
auto attrs = op->Attrs();
if (attrs.find("ring_id") != attrs.end()) {
auto ring_id_attr = attrs.at("ring_id");
int ring_id = PADDLE_GET(int, ring_id_attr);
auto map = distributed::ProcessGroupMapFromGid::getInstance();
if (map->has(ring_id)) {
distributed::ProcessGroup* pg = map->get(ring_id);
auto comm_context =
static_cast<paddle::distributed::ProcessGroupNCCL*>(pg)
->GetOrCreateCommContext(place);
dev_ctx =
static_cast<phi::distributed::NCCLCommContext*>(comm_context)
->GetDevContext();
dev_ctx->SetCommContext(comm_context);
} else {
VLOG(3) << "ring_id " << ring_id
<< " not found in ProcessGroupMapFromGid ";
}
}
#endif
phi::KernelContext phi_kernel_context;
op_with_kernel->BuildPhiKernelContext(
*instr_node.InnerRuntimeContext().get(),
const_cast<phi::DeviceContext*>(&instr_node.DeviceContext()),
dev_ctx,
&phi_kernel_context);

(*kernel)(&phi_kernel_context);
Expand Down