-
Notifications
You must be signed in to change notification settings - Fork 505
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
Showing
4 changed files
with
87 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import torch | ||
import torch.distributed as dist | ||
import torch_xla | ||
from typing import List | ||
from torch_xla import runtime as xr | ||
import torch_xla.core.xla_model as xm | ||
import torch_xla.debug.metrics as met | ||
|
||
|
||
def my_compiler(gm: torch.fx.GraphModule, example_inputs: List[torch.Tensor]): | ||
print("my_compiler() called with FX graph:") | ||
gm.graph.print_tabular() | ||
return gm.forward # return a python callable | ||
|
||
|
||
|
||
def dummy_collective_fn(input: torch.Tensor): | ||
# output_tensor = xm.all_reduce(xm.REDUCE_SUM, input) | ||
# output_tensor = dist.all_reduce(input, dist.ReduceOp.SUM) | ||
output_tensor = torch.Tensor([[0, 0, 0, 0]]) | ||
# dist.all_gather_into_tensor(output_tensor, input, None) | ||
dist.all_gather(output_tensor, input, None) | ||
return output_tensor | ||
|
||
def _mp_fn(index): | ||
dist.init_process_group("xla", init_method='xla://') | ||
device = xm.xla_device() | ||
world_size = xr.world_size() | ||
if xm.xla_device_hw(device) not in ('TPU', 'CUDA', 'NEURON'): | ||
print(f'skip this test for hw {xm.xla_device_hw(device)}') | ||
return | ||
ordinal_tensor = torch.tensor([index+1], dtype=torch.float).to(device) | ||
met.clear_all() | ||
compiled_collective = torch.compile( | ||
dummy_collective_fn, backend=my_compiler, dynamic=False) | ||
dummy_collective_fn | ||
res_tensor = compiled_collective(ordinal_tensor) | ||
print(res_tensor) | ||
# expected_tensor = torch.tensor( | ||
# [world_size * world_size / 2] * world_size, dtype=torch.float) + 3.0 | ||
# torch_xla.sync() | ||
# torch.allclose(res_tensor.cpu(), expected_tensor) | ||
# assert met.metric_data("ExecuteTime")[0] == 1 | ||
print(met.metric_data("ExecuteTime")) | ||
|
||
if __name__ == '__main__': | ||
torch_xla.launch(_mp_fn, args=(), debug_single_process=False) |
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
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