Skip to content
Open
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
28 changes: 27 additions & 1 deletion vllm_ascend/patch/platform/patch_common/patch_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

import torch
import vllm.envs as envs_vllm
from torch.distributed import ProcessGroup, ReduceOp
from vllm.config import ParallelConfig
from vllm.distributed.utils import \
stateless_init_torch_distributed_process_group

from vllm_ascend.utils import is_310p

Expand All @@ -41,9 +44,32 @@ def parallel_config_get_dp_port(self) -> int:
return port


ParallelConfig.get_next_dp_init_port = parallel_config_get_dp_port
def ascend_stateless_init_dp_group(self) -> "ProcessGroup":
dp_group = stateless_init_torch_distributed_process_group(
self.data_parallel_master_ip,
self.get_next_dp_init_port(),
self.data_parallel_rank,
self.data_parallel_size,
backend="hccl")
return dp_group


def ascend_has_unfinished_dp(dp_group: "ProcessGroup",
has_unfinished: bool) -> bool:
tensor = torch.tensor([has_unfinished], dtype=torch.int32, device="npu")
# dp rank 0: has_unfinished_seqs=True
# dp rank 1: has_unfinished_seqs=False
# aggregated: has_unfinished_seqs=True
# so this is an OR operation, i.e. MAX in integers
torch.distributed.all_reduce(tensor, op=ReduceOp.MAX, group=dp_group)
aggregated_has_unfinished = bool(tensor.item())
return aggregated_has_unfinished
Comment on lines +59 to +66
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

For improved clarity and correctness, it's better to use a boolean tensor with a logical OR operation (ReduceOp.LOR) for this check. The current implementation uses an integer tensor with ReduceOp.MAX, which works but is less direct and requires comments to explain the logic. Using torch.bool and ReduceOp.LOR is more idiomatic for boolean reductions and makes the code's intent self-evident, removing the need for extra casting and explanatory comments.

Suggested change
tensor = torch.tensor([has_unfinished], dtype=torch.int32, device="npu")
# dp rank 0: has_unfinished_seqs=True
# dp rank 1: has_unfinished_seqs=False
# aggregated: has_unfinished_seqs=True
# so this is an OR operation, i.e. MAX in integers
torch.distributed.all_reduce(tensor, op=ReduceOp.MAX, group=dp_group)
aggregated_has_unfinished = bool(tensor.item())
return aggregated_has_unfinished
tensor = torch.tensor([has_unfinished], dtype=torch.bool, device="npu")
torch.distributed.all_reduce(tensor, op=ReduceOp.LOR, group=dp_group)
return tensor.item()



ParallelConfig.get_next_dp_init_port = parallel_config_get_dp_port
ParallelConfig.stateless_init_dp_group = ascend_stateless_init_dp_group
ParallelConfig.has_unfinished_dp = staticmethod(ascend_has_unfinished_dp)

class NullHandle:

def __init__(self):
Expand Down
Loading