|
| 1 | +import os |
| 2 | + |
| 3 | +import torch |
| 4 | +import torch.distributed as dist |
| 5 | +import torch.nn as nn |
| 6 | +from parameterized import parameterized |
| 7 | +from torch.testing._internal.common_utils import run_tests |
| 8 | + |
| 9 | + |
| 10 | +def set_environment_variables(): |
| 11 | + os.environ["WORLD_SIZE"] = str(1) |
| 12 | + os.environ["RANK"] = str(0) |
| 13 | + os.environ["MASTER_ADDR"] = "127.0.0.1" |
| 14 | + os.environ["MASTER_PORT"] = str(29500) |
| 15 | + os.environ["USE_TRTLLM_PLUGINS"] = "1" |
| 16 | + |
| 17 | + |
| 18 | +set_environment_variables() |
| 19 | +dist.init_process_group(backend="nccl", init_method="env://") |
| 20 | +group = dist.new_group(ranks=[0]) |
| 21 | +group_name = group.group_name |
| 22 | +world_size = 1 |
| 23 | + |
| 24 | +from conversion.harness import DispatchTestCase |
| 25 | + |
| 26 | + |
| 27 | +class TestGatherNcclOpsConverter(DispatchTestCase): |
| 28 | + @parameterized.expand([(8)]) |
| 29 | + def test_nccl_ops(self, linear_layer_dim): |
| 30 | + class DistributedGatherModel(nn.Module): |
| 31 | + def __init__(self, input_dim): |
| 32 | + super().__init__() |
| 33 | + self.fc = torch.nn.Linear(input_dim, input_dim) |
| 34 | + |
| 35 | + def forward(self, x): |
| 36 | + x = self.fc(x) |
| 37 | + gathered_tensor = torch.ops._c10d_functional.all_gather_into_tensor( |
| 38 | + x, world_size, group_name |
| 39 | + ) |
| 40 | + gathered_tensor = torch.ops._c10d_functional.wait_tensor( |
| 41 | + gathered_tensor |
| 42 | + ) |
| 43 | + return gathered_tensor |
| 44 | + |
| 45 | + inputs = [torch.randn(1, linear_layer_dim).to("cuda")] |
| 46 | + self.run_test( |
| 47 | + DistributedGatherModel(linear_layer_dim).cuda(), |
| 48 | + inputs, |
| 49 | + use_dynamo_tracer=True, |
| 50 | + fuse_distributed_ops=True, |
| 51 | + ) |
| 52 | + |
| 53 | + @parameterized.expand([(8)]) |
| 54 | + def test_nccl_ops_scatter(self, linear_layer_dim): |
| 55 | + |
| 56 | + class DistributedReduceScatterModel(nn.Module): |
| 57 | + def __init__(self, input_dim): |
| 58 | + super().__init__() |
| 59 | + self.fc = torch.nn.Linear(input_dim, input_dim) |
| 60 | + |
| 61 | + def forward(self, x): |
| 62 | + x = self.fc(x) |
| 63 | + scatter_reduce_tensor = ( |
| 64 | + torch.ops._c10d_functional.reduce_scatter_tensor( |
| 65 | + x, "sum", world_size, group_name |
| 66 | + ) |
| 67 | + ) |
| 68 | + scatter_reduce_tensor = torch.ops._c10d_functional.wait_tensor( |
| 69 | + scatter_reduce_tensor |
| 70 | + ) |
| 71 | + return scatter_reduce_tensor |
| 72 | + |
| 73 | + inputs = [torch.zeros(1, linear_layer_dim).to("cuda")] |
| 74 | + |
| 75 | + self.run_test( |
| 76 | + DistributedReduceScatterModel(linear_layer_dim).cuda(), |
| 77 | + inputs, |
| 78 | + use_dynamo_tracer=True, |
| 79 | + fuse_distributed_ops=True, |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + run_tests() |
0 commit comments