-
Notifications
You must be signed in to change notification settings - Fork 87
feat: add NPU process group initialization and management. #456
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,10 @@ limitations under the License. | |
|
|
||
| #include "npu_process_group.h" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. npu_process_group.cpp should be deleted, because npu_process_group.h is enough, like cuda/mlu_process_group.h.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review. However, I strongly prefer to keep the .cpp file. Defining implementation directly in the header is generally considered bad practice. I hope you understand my decision to maintain this separation. |
||
|
|
||
| #include <c10d/ProcessGroup.hpp> | ||
| #include <c10d/TCPStore.hpp> | ||
| #include <torch_npu/csrc/distributed/ProcessGroupHCCL.hpp> | ||
|
|
||
| namespace { | ||
|
|
||
| #define HCCLCHECK(cmd) \ | ||
|
|
@@ -24,113 +28,68 @@ namespace { | |
| LOG(FATAL) << "Failed, HCCL error :" << HcclGetErrorString(r); \ | ||
| } \ | ||
| } while (0) | ||
| } // namespace | ||
|
|
||
| inline bool is_npu(const at::Tensor& tensor) { | ||
| if (!tensor.defined()) { | ||
| return false; | ||
| } | ||
| return tensor.device().is_privateuseone(); | ||
| } | ||
|
|
||
| inline bool is_npu(const at::TensorOptions& options) { | ||
| return options.device().is_privateuseone(); | ||
| } | ||
| namespace xllm { | ||
|
|
||
| inline bool is_npu(const at::Device& device) { | ||
| return device.is_privateuseone(); | ||
| } | ||
| ProcessGroupHCCL::ProcessGroupHCCL(int global_rank, | ||
| int world_size, | ||
| int rank_size, | ||
| int port, | ||
| bool trans, | ||
| const std::string& host, | ||
| const std::string& group_name, | ||
| const torch::Device& device) | ||
| : ProcessGroup(device) { | ||
| c10::intrusive_ptr<c10d_npu::ProcessGroupHCCL::Options> hccl_pg_options = | ||
| c10d_npu::ProcessGroupHCCL::Options::create(); | ||
| #if TORCH_VERSION_MAJOR > 2 || \ | ||
| (TORCH_VERSION_MAJOR == 2 && TORCH_VERSION_MINOR >= 7) | ||
| hccl_pg_options->group_name = group_name; | ||
| #endif | ||
| int rank = global_rank; | ||
| if (world_size != rank_size) { | ||
| auto [local_rank, group_ranks] = | ||
| get_group_rank(world_size, global_rank, rank_size, trans); | ||
| std::vector<uint32_t> uint32_ranks; | ||
| for (auto rank : group_ranks) { | ||
| uint32_ranks.push_back(static_cast<uint32_t>(rank)); | ||
| } | ||
| hccl_pg_options->global_ranks_in_group = uint32_ranks; | ||
| rank = local_rank; | ||
| } | ||
|
|
||
| at::Tensor flatten_for_scatter_gather(std::vector<at::Tensor>& tensors) { | ||
| auto& t = tensors[0]; | ||
| std::vector<int64_t> sizes{static_cast<int64_t>(tensors.size())}; | ||
| sizes.insert(sizes.end(), t.sizes().begin(), t.sizes().end()); | ||
| return at::empty(sizes, t.options()); | ||
| auto store = create_tcp_store(host, port, rank); | ||
| pg_ = std::make_unique<c10d_npu::ProcessGroupHCCL>( | ||
| store, rank, rank_size, hccl_pg_options); | ||
| } | ||
|
|
||
| HcclDataType to_hccl_data_type(const torch::Tensor& input) { | ||
| const auto type = input.scalar_type(); | ||
| switch (type) { | ||
| case at::kFloat: | ||
| return HCCL_DATA_TYPE_FP32; | ||
| case at::kHalf: | ||
| return HCCL_DATA_TYPE_FP16; | ||
| case at::kDouble: | ||
| return HCCL_DATA_TYPE_FP64; | ||
| case at::kLong: | ||
| return HCCL_DATA_TYPE_INT64; | ||
| case at::kInt: | ||
| return HCCL_DATA_TYPE_INT32; | ||
| case at::kChar: | ||
| return HCCL_DATA_TYPE_INT8; | ||
| case at::kByte: | ||
| return HCCL_DATA_TYPE_UINT8; | ||
| case at::kBool: | ||
| return HCCL_DATA_TYPE_UINT8; | ||
| case at::kBFloat16: | ||
| return HCCL_DATA_TYPE_BFP16; | ||
| default: | ||
| LOG(FATAL) << "Unconvertible HCCL type: " << type; | ||
| // Destructor. | ||
| ProcessGroupHCCL::~ProcessGroupHCCL() { | ||
| if (pg_) { | ||
| pg_->shutdown(); | ||
| } else { | ||
| HCCLCHECK(HcclCommDestroy(comm_)); | ||
| } | ||
| } | ||
|
|
||
| void check_input(torch::Tensor input) { | ||
| CHECK(is_npu(input)) << "input should be npu tensor"; | ||
| CHECK(input.is_contiguous()) << "input should be contiguous"; | ||
| CHECK(!input.is_sparse()) << "input have to be npu dense tensor"; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace xllm { | ||
|
|
||
| ProcessGroupHCCL::ProcessGroupHCCL(int rank, | ||
| int world_size, | ||
| const torch::Device& device, | ||
| HcclComm comm) | ||
| : ProcessGroup(device), comm_(comm) {} | ||
| // Destructor. | ||
| ProcessGroupHCCL::~ProcessGroupHCCL() { HCCLCHECK(HcclCommDestroy(comm_)); } | ||
|
|
||
| void ProcessGroupHCCL::allreduce(torch::Tensor& input) { | ||
| DCHECK(input.device() == device()) | ||
| << "input should be on the same device as the process group"; | ||
| check_input(input); | ||
| // inplace all reduce | ||
| // const auto count = input.numel(); | ||
| // const auto data_type = to_hccl_data_type(input); | ||
| // auto stream = c10_npu::getCurrentNPUStream(); | ||
| // torch::DeviceGuard device_guard(device()); | ||
| // HCCLCHECK(HcclAllReduce( | ||
| // /*sendbuff=*/input.data_ptr(), | ||
| // /*recvbuff=*/input.data_ptr(), | ||
| // /*count=*/count, | ||
| // /*datatype=*/data_type, | ||
| // /*op=*/HCCL_REDUCE_SUM, | ||
| // /*comm=*/comm_, | ||
| // /*stream=*/stream)); | ||
| } | ||
| void ProcessGroupHCCL::allgather(const torch::Tensor& input, | ||
| std::vector<torch::Tensor>& outputs) { | ||
| check_input(input); | ||
| // CHECK(outputs.size() == world_size()) | ||
| // << "outputs should have the same size as world_size"; | ||
| // DCHECK(input.device() == device()) | ||
| // << "input should be on the same device as the process group"; | ||
| // torch::DeviceGuard device_guard(device()); | ||
| // torch::Tensor flattened_output = flatten_for_scatter_gather(outputs); | ||
| // const auto count = input.numel(); | ||
| // const auto data_type = to_hccl_data_type(input); | ||
| // auto stream = c10_npu::getCurrentNPUStream(); | ||
| // HCCLCHECK(HcclAllGather( | ||
| // /*sendbuff=*/input.data_ptr(), | ||
| // /*recvbuff=*/flattened_output.data_ptr(), | ||
| // /*sendcount=*/count, | ||
| // /*datatype=*/data_type, | ||
| // /*comm=*/comm_, | ||
| // /*stream=*/stream)); | ||
| // // copy the flattened output tensors to the outputs. | ||
| // for (int i = 0; i < outputs.size(); ++i) { | ||
| // outputs[i].copy_(flattened_output[i], /*non_blocking=*/true); | ||
| // } | ||
| std::unique_ptr<xllm::ProcessGroup> create_process_group( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback. Regarding the suggestion to consolidate the create_process_group functions, I have a few concerns: Since ProcessGroupHCCL, ProcessGroupCncl, and ProcessGroupNccl are device-specific implementations , moving them to collective_communicator.cpp would introduce excessive #if/#elif preprocessor directives. |
||
| int rank, | ||
| int world_size, | ||
| int rank_size, | ||
| int port, | ||
| bool trans, | ||
| const std::string& host, | ||
| const std::string& group_name, | ||
| const torch::Device& device) { | ||
| return std::make_unique<ProcessGroupHCCL>( | ||
| rank, world_size, rank_size, port, trans, host, group_name, device); | ||
| } | ||
|
|
||
| } // namespace xllm | ||
Uh oh!
There was an error while loading. Please reload this page.