-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Maxim Kalashnikov
committed
Apr 12, 2024
1 parent
1080072
commit 018c80e
Showing
3 changed files
with
24 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,20 @@ | ||
import torch | ||
import torch.distributed as dist | ||
|
||
class AllGather(torch.autograd.Function): | ||
@staticmethod | ||
def forward(ctx, tensor): | ||
gathered = [torch.zeros_like(tensor) for _ in range(dist.get_world_size())] | ||
dist.all_gather(gathered, tensor) | ||
return tuple(gathered) | ||
|
||
@staticmethod | ||
def backward(ctx, *grad_outs): | ||
# if os.environ.get('REDUCE_GRADS'): | ||
# grad_outs = torch.stack(grad_outs) | ||
# dist.all_reduce(grad_outs) | ||
return grad_outs[dist.get_rank()] | ||
|
||
def all_gather_and_cat(tensor): | ||
return torch.cat(AllGather.apply(tensor)) | ||
|
This file contains 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