-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
[Bugfix] fix xpu communicator #13368
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from typing import Optional | ||
|
||
import torch | ||
import torch.distributed as dist | ||
from torch.distributed import ProcessGroup | ||
|
||
from .base_device_communicator import DeviceCommunicatorBase | ||
|
||
|
||
class XpuCommunicator(DeviceCommunicatorBase): | ||
|
||
def __init__(self, | ||
cpu_group: ProcessGroup, | ||
device: Optional[torch.device] = None, | ||
device_group: Optional[ProcessGroup] = None, | ||
unique_name: str = ""): | ||
super().__init__(cpu_group, device, device_group, unique_name) | ||
|
||
def all_reduce(self, input_) -> torch.Tensor: | ||
dist.all_reduce(input_, group=self.device_group) | ||
return input_ | ||
|
||
def gather(self, | ||
input_: torch.Tensor, | ||
dst: int = 0, | ||
dim: int = -1) -> Optional[torch.Tensor]: | ||
assert -input_.dim() <= dim < input_.dim(), ( | ||
f"Invalid dim ({dim}) for input tensor with shape {input_.size()}") | ||
if dim < 0: | ||
# Convert negative dim to positive. | ||
dim += input_.dim() | ||
# For xpu path, gather doesn't work properly together with ray | ||
# cluster so we use all_gather instead for now. | ||
input_size = input_.size() | ||
# Allocate output tensor. | ||
output_tensor = torch.empty((self.world_size, ) + input_size, | ||
dtype=input_.dtype, | ||
device=input_.device) | ||
# All-gather. | ||
dist.all_gather_into_tensor(output_tensor, | ||
input_, | ||
group=self.device_group) | ||
if self.rank_in_group == dst: | ||
# Reshape | ||
output_tensor = output_tensor.movedim(0, dim) | ||
output_tensor = output_tensor.reshape(input_size[:dim] + | ||
(self.world_size * | ||
input_size[dim], ) + | ||
input_size[dim + 1:]) | ||
else: | ||
output_tensor = None | ||
return output_tensor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@youkaichao #13208 breaks XPU path since here we use
all_gather_into_tensor
as workaround. Please help take a look at this quick fix. Thanks.