Spaces:
Build error
Build error
File size: 3,327 Bytes
b6af722 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import torch
import torch.distributed as dist
from torch.autograd import Function
class AllGather(Function):
@staticmethod
def forward(ctx, tensor, process_group):
world_size = dist.get_world_size(process_group)
ctx.world_size = world_size
ctx.rank = process_group.rank()
gathered_tensors = [torch.zeros_like(tensor) for _ in range(world_size)]
dist.all_gather(gathered_tensors, tensor.contiguous(), process_group)
return torch.cat(gathered_tensors, dim=0)
@staticmethod
def backward(ctx, grad_output):
world_size = ctx.world_size
rank = ctx.rank
# Split the gradient tensor
grad_chunks = grad_output.chunk(world_size)
# Select the gradient chunk for the current rank
grad_input = grad_chunks[rank]
return grad_input, None
def gather_along_first_dim(tensor, process_group):
return AllGather.apply(tensor, process_group)
class Scatter(Function):
@staticmethod
def forward(ctx, tensor, process_group):
world_size = dist.get_world_size(process_group)
ctx.world_size = world_size
ctx.process_group = process_group
rank = process_group.rank()
# Split the tensor
tensor_chunks = tensor.chunk(world_size)
# Select the tensor chunk for the current rank
return tensor_chunks[rank]
@staticmethod
def backward(ctx, grad_output):
world_size = ctx.world_size
process_group = ctx.process_group
# Gather the gradient tensor
gathered_grads = [torch.zeros_like(grad_output) for _ in range(world_size)]
dist.all_gather(gathered_grads, grad_output.contiguous(), process_group)
return torch.cat(gathered_grads, dim=0), None
def scatter_along_first_dim(tensor, process_group):
return Scatter.apply(tensor, process_group)
if __name__ == "__main__":
# Torch global setup for distributed training
local_rank = int(os.environ["LOCAL_RANK"])
rank = int(os.environ["RANK"])
world_size = int(os.environ["WORLD_SIZE"])
torch.cuda.set_device(local_rank)
torch.distributed.init_process_group(world_size=world_size, rank=rank)
# Create a tensor with gradients
x = torch.randn(10, 1, requires_grad=True, device="cuda")
# Perform all_gather with gradient support
y = gather_along_first_dim(x, dist.group.WORLD)
print(f"{y.shape=}")
y = scatter_along_first_dim(y, dist.group.WORLD)
print(f"{y.shape=}")
# Use the result in your computation
loss = y.sum()
loss.backward()
# x.grad now contains the gradients
print(x.grad)
|