-
Notifications
You must be signed in to change notification settings - Fork 83
[WIP] Support multiprocessing training #141
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
Closed
+61
−10
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c57cc63
Support multiprocessing training
vandanavk 42780f4
Fix pre-commit
vandanavk 24f4334
Fix pre-commit
vandanavk 8df5198
Check if torch.distributed is used
vandanavk 9612919
Delete env var after test
vandanavk 49ea13c
use monkeypatch and workerrank env var
vandanavk 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
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
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 |
---|---|---|
|
@@ -63,8 +63,9 @@ def train(model, device, optimizer, num_steps=10): | |
optimizer.step() | ||
|
||
|
||
def run(rank, size, include_workers="one", num_epochs=10, batch_size=128, num_batches=10): | ||
def run(monkeypatch, rank, size, include_workers="one", num_epochs=10, batch_size=128, num_batches=10): | ||
"""Distributed function to be implemented later.""" | ||
monkeypatch.setenv("SMDEBUG_WORKER_RANK", str(rank)) | ||
torch.manual_seed(1234) | ||
device = torch.device("cpu") | ||
model = Net().to(device) | ||
|
@@ -90,11 +91,13 @@ def run(rank, size, include_workers="one", num_epochs=10, batch_size=128, num_ba | |
loss = F.mse_loss(output, target) | ||
epoch_loss += loss.item() | ||
loss.backward() | ||
average_gradients(model) | ||
if hasattr(dist, "is_initialized") and dist.is_initialized(): | ||
average_gradients(model) | ||
Comment on lines
+94
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reason? |
||
optimizer.step() | ||
# print(f"Rank {dist.get_rank()}, epoch {epoch}: {epoch_loss / num_batches}") | ||
|
||
assert hook._get_worker_name() == f"worker_{dist.get_rank()}" | ||
if hasattr(dist, "is_initialized") and dist.is_initialized(): | ||
assert hook._get_worker_name() == f"worker_{dist.get_rank()}" | ||
Comment on lines
+99
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same her? |
||
# Race condition here where both workers attempt to move | ||
# /tmp/{out_dir}/END_OF_JOB.ts to {out_dir}/END_OF_JOB.ts | ||
try: | ||
|
@@ -179,3 +182,45 @@ def test_run_net_distributed_save_one_worker(): | |
trial = _run_net_distributed(include_workers="one") | ||
assert len(trial.workers()) == 1, f"trial.workers() = {trial.workers()}" | ||
assert len(trial.steps()) == 3, f"trial.steps() = {trial.steps()}" | ||
|
||
|
||
@pytest.mark.slow | ||
def test_run_net_distributed_multiproc_save_all_workers(monkeypatch): | ||
size = 2 | ||
monkeypatch.setenv("SMDEBUG_NUM_WORKERS", str(size)) | ||
processes = [] | ||
for rank in range(size): | ||
p = Process(target=run, args=(monkeypatch, rank, size, "all")) | ||
p.start() | ||
processes.append(p) | ||
|
||
for p in processes: | ||
p.join() | ||
|
||
out_dir = "/tmp/run" | ||
trial = create_trial(path=out_dir) | ||
assert len(trial.workers()) == 2, f"trial.workers() = {trial.workers()}" | ||
assert len(trial.steps()) == 3, f"trial.steps() = {trial.steps()}" | ||
|
||
del os.environ["SMDEBUG_NUM_WORKERS"] | ||
|
||
|
||
@pytest.mark.slow | ||
def test_run_net_distributed_multiproc_save_one_worker(monkeypatch): | ||
size = 2 | ||
monkeypatch.setenv("SMDEBUG_NUM_WORKERS", str(size)) | ||
processes = [] | ||
for rank in range(size): | ||
p = Process(target=run, args=(monkeypatch, rank, size, "one")) | ||
p.start() | ||
processes.append(p) | ||
|
||
for p in processes: | ||
p.join() | ||
|
||
out_dir = "/tmp/run" | ||
trial = create_trial(path=out_dir) | ||
assert len(trial.workers()) == 1, f"trial.workers() = {trial.workers()}" | ||
assert len(trial.steps()) == 3, f"trial.steps() = {trial.steps()}" | ||
|
||
del os.environ["SMDEBUG_NUM_WORKERS"] |
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.
reason for these changes?
Uh oh!
There was an error while loading. Please reload this page.
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.
when using the multiprocessing approach, torch.distributed is not used (init_process_group is not called). so, any reference to dist.get_rank or dist.get_world_size() will error out.