-
Notifications
You must be signed in to change notification settings - Fork 6
[Feature] Multi-GPU support #55
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
Open
yoshikisd
wants to merge
19
commits into
master
Choose a base branch
from
multigpu
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0268f5b
Created the distributed module
yoshikisd fded573
Added additional functions to distributed
yoshikisd 10229e4
Renamed cdtools.tools.distributed to cdtools.tools.multigpu. Also add…
yoshikisd 8d8da15
Multi-GPU compatability added for base Reconstructor and AdamReconstr…
yoshikisd b0604d4
Prevent dataset from plotting/saving outside of Rank 0
yoshikisd 0f1f990
Added a check for init_method in multigpu.setup
yoshikisd b784620
Multi-GPU compatibility added for CDIModel
yoshikisd 6f15c36
Added example scripts for running multi-GPU jobs with spawn or torchrun
yoshikisd 842bb49
Added example script for running a multi-GPU speed test
yoshikisd 1a3d0cd
Modified runtime error message in multigpu.setup
yoshikisd cad3d9c
Fixed linting issues on multigpu
yoshikisd cd584fc
multigpu.setup now recognizes MASTER_ADDR and MASTER_PORT when settin…
yoshikisd a8d2bfa
Simplified the logic in MASTER_ADDR and MASTER_PORT definition in mul…
yoshikisd 32e9256
Added multi-GPU pytests
yoshikisd e5996a4
Merge branch 'master' into multigpu
yoshikisd ebe5b54
Merge remote-tracking branch 'origin/master' into multigpu
yoshikisd 9792b14
Merge remote-tracking branch 'origin/master' into multigpu
yoshikisd e059c18
Make the timeout default 30 seconds and enforce timeout in units of s…
yoshikisd 9795f80
Merge remote-tracking branch 'origin/master' into multigpu
yoshikisd 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,91 @@ | ||
| import cdtools | ||
| from matplotlib import pyplot as plt | ||
| import torch as t | ||
|
|
||
| # If you're noticing that the multi-GPU job is hanging (especially with 100% | ||
| # GPU use across all participating devices), you might want to try disabling | ||
| # the environment variable NCCL_P2P_DISABLE. | ||
| import os | ||
| os.environ['NCCL_P2P_DISABLE'] = str(int(True)) | ||
|
|
||
|
|
||
| # The entire reconstruction script needs to be wrapped in a function | ||
| def reconstruct(rank, world_size): | ||
|
|
||
| # In the multigpu setup, we need to explicitly define the rank and | ||
| # world_size. The master address and master port should also be | ||
| # defined if we don't specify the `init_method` parameter. | ||
| cdtools.tools.multigpu.setup(rank=rank, | ||
| world_size=world_size, | ||
| master_addr='localhost', | ||
| master_port='6666') | ||
|
|
||
| filename = 'example_data/AuBalls_700ms_30nmStep_3_6SS_filter.cxi' | ||
| dataset = cdtools.datasets.Ptycho2DDataset.from_cxi(filename) | ||
|
|
||
| pad = 10 | ||
| dataset.pad(pad) | ||
| dataset.inspect() | ||
| model = cdtools.models.FancyPtycho.from_dataset( | ||
| dataset, | ||
| n_modes=3, | ||
| probe_support_radius=50, | ||
| propagation_distance=2e-6, | ||
| units='um', | ||
| probe_fourier_crop=pad | ||
| ) | ||
| model.translation_offsets.data += 0.7 * \ | ||
| t.randn_like(model.translation_offsets) | ||
| model.weights.requires_grad = False | ||
|
|
||
| # We need to manually define the rank parameter for the model, or else | ||
| # all plots will be duplicated by the number of GPUs used. | ||
| model.rank = rank | ||
|
|
||
| device = 'cuda' | ||
| model.to(device=device) | ||
| dataset.get_as(device=device) | ||
|
|
||
| # Rank and world_size also needs to be explicitly defined here | ||
| recon = cdtools.reconstructors.AdamReconstructor(model, | ||
| dataset, | ||
| rank=rank, | ||
| world_size=world_size) | ||
|
|
||
| with model.save_on_exception( | ||
| 'example_reconstructions/gold_balls_earlyexit.h5', dataset): | ||
|
|
||
| for loss in recon.optimize(20, lr=0.005, batch_size=50): | ||
| if rank == 0: | ||
| print(model.report()) | ||
| if model.epoch % 10 == 0: | ||
| model.inspect(dataset) | ||
|
|
||
| for loss in recon.optimize(50, lr=0.002, batch_size=100, | ||
| schedule=True): | ||
| if rank == 0: | ||
| print(model.report()) | ||
|
|
||
| if model.epoch % 10 == 0: | ||
| model.inspect(dataset) | ||
|
|
||
| for loss in recon.optimize(100, lr=0.001, batch_size=100, | ||
| schedule=True): | ||
| if rank == 0: | ||
| print(model.report()) | ||
| if model.epoch % 10 == 0: | ||
| model.inspect(dataset) | ||
|
|
||
| cdtools.tools.multigpu.cleanup() | ||
|
|
||
| model.tidy_probes() | ||
| model.save_to_h5('example_reconstructions/gold_balls.h5', dataset) | ||
| model.inspect(dataset) | ||
| model.compare(dataset) | ||
| plt.show() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| # Specify the number of GPUs we want to use, then spawn the multi-GPU job | ||
| ngpus = 2 | ||
| t.multiprocessing.spawn(reconstruct, args=(ngpus,), nprocs=ngpus) |
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,95 @@ | ||
| import cdtools | ||
| import torch as t | ||
|
|
||
| # If you're noticing that the multi-GPU job is hanging (especially with 100% | ||
| # GPU use across all participating devices), you might want to try disabling | ||
| # the environment variable NCCL_P2P_DISABLE. | ||
| import os | ||
| os.environ['NCCL_P2P_DISABLE'] = str(int(True)) | ||
|
|
||
|
|
||
| # For running a speed test, we need to add an additional `conn` parameter | ||
| # that the speed test uses to send loss-versus-time curves to the | ||
| # speed test function. | ||
| def reconstruct(rank, world_size, conn): | ||
|
|
||
| # We define the setup in the same manner as we did in the spawn example | ||
| # (speed test uses spawn) | ||
| cdtools.tools.multigpu.setup(rank=rank, | ||
| world_size=world_size, | ||
| master_addr='localhost', | ||
| master_port='6666') | ||
|
|
||
| filename = 'example_data/AuBalls_700ms_30nmStep_3_6SS_filter.cxi' | ||
| dataset = cdtools.datasets.Ptycho2DDataset.from_cxi(filename) | ||
|
|
||
| pad = 10 | ||
| dataset.pad(pad) | ||
| dataset.inspect() | ||
| model = cdtools.models.FancyPtycho.from_dataset( | ||
| dataset, | ||
| n_modes=3, | ||
| probe_support_radius=50, | ||
| propagation_distance=2e-6, | ||
| units='um', | ||
| probe_fourier_crop=pad | ||
| ) | ||
| model.translation_offsets.data += 0.7 * \ | ||
| t.randn_like(model.translation_offsets) | ||
| model.weights.requires_grad = False | ||
|
|
||
| # Unless you're plotting data within the reconstruct function, setting | ||
| # the rank parameter is not necessary. | ||
| # model.rank = rank | ||
|
|
||
| device = 'cuda' | ||
| model.to(device=device) | ||
| dataset.get_as(device=device) | ||
|
|
||
| # Rank and world_size also needs to be explicitly defined here | ||
| recon = cdtools.reconstructors.AdamReconstructor(model, | ||
| dataset, | ||
| rank=rank, | ||
| world_size=world_size) | ||
|
|
||
| with model.save_on_exception( | ||
| 'example_reconstructions/gold_balls_earlyexit.h5', dataset): | ||
|
|
||
| # It is recommended to comment out/remove all plotting-related methods | ||
| # for the speed test. | ||
| for loss in recon.optimize(20, lr=0.005, batch_size=50): | ||
| if rank == 0: | ||
| print(model.report()) | ||
|
|
||
| for loss in recon.optimize(50, lr=0.002, batch_size=100): | ||
| if rank == 0: | ||
| print(model.report()) | ||
|
|
||
| for loss in recon.optimize(100, lr=0.001, batch_size=100, | ||
| schedule=True): | ||
| if rank == 0: | ||
| print(model.report()) | ||
|
|
||
| # We now use the conn parameter to send the loss-versus-time data to the | ||
| # main process running the speed test. | ||
| conn.send((model.loss_times, model.loss_history)) | ||
|
|
||
| # And, as always, we need to clean up at the end. | ||
| cdtools.tools.multigpu.cleanup() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| # We call the run_speed_test function instead of calling | ||
| # t.multiprocessing.spawn. We specify the number of runs | ||
| # we want to perform per GPU count along with how many | ||
| # GPU counts we want to test. | ||
| # | ||
| # Here, we test both 1 and 2 GPUs using 3 runs for both. | ||
| # The show_plots will report the mean +- standard deviation | ||
| # of loss-versus-time/epoch curves across the 3 runs. | ||
| # The plot will also show the mean +- standard deviation of | ||
| # the GPU-dependent speed ups across the 3 runs. | ||
| cdtools.tools.multigpu.run_speed_test(reconstruct, | ||
| gpu_counts=(1, 2), | ||
| runs=3, | ||
| show_plot=True) |
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,70 @@ | ||
| import cdtools | ||
| from matplotlib import pyplot as plt | ||
| import torch as t | ||
|
|
||
| # At the beginning of the script we need to setup the multi-GPU job | ||
| # by initializing the process group and sycnronizing the RNG seed | ||
| # across all participating GPUs. | ||
| cdtools.tools.multigpu.setup() | ||
|
|
||
| # To avoid redundant print statements, we first grab the GPU "rank" | ||
| # (an ID number between 0 and max number of GPUs minus 1). | ||
| rank = cdtools.tools.multigpu.get_rank() | ||
|
|
||
| filename = 'example_data/AuBalls_700ms_30nmStep_3_6SS_filter.cxi' | ||
| dataset = cdtools.datasets.Ptycho2DDataset.from_cxi(filename) | ||
|
|
||
| pad = 10 | ||
| dataset.pad(pad) | ||
| dataset.inspect() | ||
| model = cdtools.models.FancyPtycho.from_dataset( | ||
| dataset, | ||
| n_modes=3, | ||
| probe_support_radius=50, | ||
| propagation_distance=2e-6, | ||
| units='um', | ||
| probe_fourier_crop=pad | ||
| ) | ||
| model.translation_offsets.data += 0.7 * t.randn_like(model.translation_offsets) | ||
| model.weights.requires_grad = False | ||
| device = 'cuda' | ||
| model.to(device=device) | ||
| dataset.get_as(device=device) | ||
|
|
||
| recon = cdtools.reconstructors.AdamReconstructor(model, dataset) | ||
|
|
||
| with model.save_on_exception( | ||
| 'example_reconstructions/gold_balls_earlyexit.h5', dataset): | ||
|
|
||
| for loss in recon.optimize(20, lr=0.005, batch_size=50): | ||
| # We ensure that only the GPU with rank of 0 runs print statement. | ||
| if rank == 0: | ||
| print(model.report()) | ||
|
|
||
| # But we don't need to do rank checking for any plotting- or saving- | ||
| # related methods; this checking is handled internernally. | ||
| if model.epoch % 10 == 0: | ||
| model.inspect(dataset) | ||
|
|
||
| for loss in recon.optimize(50, lr=0.002, batch_size=100): | ||
| if rank == 0: | ||
| print(model.report()) | ||
|
|
||
| if model.epoch % 10 == 0: | ||
| model.inspect(dataset) | ||
|
|
||
| for loss in recon.optimize(100, lr=0.001, batch_size=100, schedule=True): | ||
| if rank == 0: | ||
| print(model.report()) | ||
| if model.epoch % 10 == 0: | ||
| model.inspect(dataset) | ||
|
|
||
| # After the reconstruction is completed, we need to cleanup things by | ||
| # destroying the process group. | ||
| cdtools.tools.multigpu.cleanup() | ||
|
|
||
| model.tidy_probes() | ||
| model.save_to_h5('example_reconstructions/gold_balls.h5', dataset) | ||
| model.inspect(dataset) | ||
| model.compare(dataset) | ||
| plt.show() | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment or docstring indicating how it should be run