-
Notifications
You must be signed in to change notification settings - Fork 564
Adding support for torchrun in xla backend #3609
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8771725
Adding support for torchrun in xla backend
amithrm 997356b
Adding support for custom specification of devices and associated tests
amithrm 8f84716
Revert "Adding support for custom specification of devices and associ…
amithrm 3336030
Adding support for custom specification of devices and associated tests
amithrm e0c4ff9
Updated xrt initialization to work with TPU based flow
amithrm 6833150
Cleanup xrt initialization to work with torchrun/slurm
amithrm d0fbbd8
Adding tests to CI pipeline
amithrm b7c02c9
Fix Lint/CI issues
amithrm 1d2be47
Fixing Lint Error
amithrm 262813e
Lint changes
amithrm 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,56 @@ | ||
| import argparse | ||
| import os | ||
| import torch | ||
| import torch_xla | ||
| import torch_xla.core.xla_model as xm | ||
| import torch.distributed as dist | ||
| import torch_xla.distributed.xla_multiprocessing as xmp | ||
| from torch_xla.distributed.xrt_init import init_xrt_context | ||
| import torch_xla.distributed.xla_backend | ||
|
|
||
|
|
||
| def _mp_fn_xrt_init(): | ||
| rank = int(os.environ['RANK']) | ||
| size = int(os.environ['WORLD_SIZE']) | ||
|
|
||
| init_xrt_context() | ||
|
|
||
| device = xm.xla_device() | ||
| ones = torch.ones((2, 3)) | ||
| xones = ones.to(device) | ||
| result = xm.all_reduce('sum', xones) | ||
|
|
||
| result_cpu = result.cpu() | ||
| expected = torch.ones((2, 3)) * size | ||
| assert torch.all(result_cpu == expected), f'{result_cpu} != {expected}' | ||
|
|
||
|
|
||
| def _mp_fn_xla_backend(): | ||
| rank = int(os.environ['RANK']) | ||
| size = int(os.environ['WORLD_SIZE']) | ||
|
|
||
| dist.init_process_group('xla') | ||
| device = xm.xla_device() | ||
|
|
||
| ones = torch.ones((2, 3)) | ||
| xones = ones.to(device) | ||
| dist.all_reduce(xones, op=torch.distributed.ReduceOp.SUM) | ||
|
|
||
| result_cpu = xones.cpu() | ||
| expected = torch.ones((2, 3)) * size | ||
| assert torch.all(xones.cpu() == expected), f'{xones} != {expected}' | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| print( | ||
| 'master_port:{}, master_addr:{}, rank:{}, local_rank:{}, size:{}'.format( | ||
| os.environ['MASTER_PORT'], os.environ['MASTER_ADDR'], | ||
| os.environ['RANK'], os.environ['LOCAL_RANK'], | ||
| os.environ['WORLD_SIZE'])) | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument('--use_xla_backend', action="store_true") | ||
| args = parser.parse_args() | ||
| if args.use_xla_backend: | ||
| _mp_fn_xla_backend() | ||
| else: | ||
| _mp_fn_xrt_init() | ||
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
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,26 @@ | ||
| import os | ||
| import subprocess | ||
| import pathlib | ||
|
|
||
|
|
||
| def test_local_torchrun_xrt_init(): | ||
| # This test launches a allreduce using torchrun launcher, uses native xla_model CCop | ||
| ci_dir = pathlib.Path(__file__).parent.resolve() | ||
| cmd = f'torchrun --nproc_per_node=2 --master_addr=127.0.0.1 --master_port=2020 {ci_dir}/allreduce_torchrun.py' | ||
| proc = subprocess.Popen(cmd, shell=True) | ||
| return_code = proc.wait() | ||
| assert return_code == 0 | ||
|
|
||
|
|
||
| def test_local_torchrun_xla_backend(): | ||
| # This test launches a allreduce using torchrun launcher, uses xla backend | ||
| ci_dir = pathlib.Path(__file__).parent.resolve() | ||
| cmd = f'torchrun --nproc_per_node=2 --master_addr=127.0.0.1 --master_port=2020 {ci_dir}/allreduce_torchrun.py --use_xla_backend' | ||
| proc = subprocess.Popen(cmd, shell=True) | ||
| return_code = proc.wait() | ||
| assert return_code == 0 | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| test_local_torchrun_xrt_init() | ||
| test_local_torchrun_xla_backend() |
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,45 @@ | ||
| """ | ||
| This script is for starting the xrt_server. It also polls the PID and | ||
| checks if it exist. It would kill the server, when the process whose | ||
| PID it was tracking dies. | ||
| NOTE: This script should be used only by xrt_init.py and not anyone else. | ||
| """ | ||
| import os | ||
| import argparse | ||
| import psutil | ||
| import time | ||
| import signal | ||
| import multiprocessing | ||
| import torch_xla | ||
|
|
||
|
|
||
| def _polling(pid_to_track): | ||
|
|
||
| def is_pid_alive(pid): | ||
| # The idea behind this is: if the process doesn't exist, | ||
| # getting a process status should throw an error. | ||
| # If the process exist, then we check if it hasn't gone | ||
| # into zombie state. This can happen when we run torchrun | ||
| # from neuron_parallel_compile. | ||
| try: | ||
| return psutil.Process(pid).status() != psutil.STATUS_ZOMBIE | ||
| except: | ||
| return False | ||
|
|
||
| while is_pid_alive(pid_to_track): | ||
| time.sleep(10) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--port", required=True) | ||
| parser.add_argument("--pid_to_track", default=None) | ||
| args = parser.parse_args() | ||
| polling_process = multiprocessing.Process( | ||
| target=_polling, args=(int(args.pid_to_track),)) | ||
| server_process = multiprocessing.Process( | ||
| target=torch_xla._XLAC._run_xrt_local_service, args=(int(args.port),)) | ||
| polling_process.start() | ||
| server_process.start() | ||
| polling_process.join() | ||
| os.kill(server_process.pid, signal.SIGKILL) |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.