-
Notifications
You must be signed in to change notification settings - Fork 27
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
add spawn cuda process testing #535
Draft
sichu2023
wants to merge
1
commit into
main
Choose a base branch
from
sichu/distributed_test
base: main
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.
Draft
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -13,11 +13,15 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from contextlib import contextmanager | ||
|
||
import pytest | ||
import torch | ||
import torch.distributed as dist | ||
import torch.multiprocessing.spawn | ||
from megatron.core import parallel_state | ||
from nemo import lightning as nl | ||
from pytest import MonkeyPatch | ||
|
||
from bionemo.testing import megatron_parallel_state_utils | ||
|
||
|
@@ -106,3 +110,47 @@ def test_reduce_scatter(): | |
|
||
dist.reduce_scatter(output_tensor, to_reduce_scatter) | ||
assert tuple(output_tensor.shape) == (3, 3) | ||
|
||
|
||
# def test_all_reduce_sum(): | ||
# with megatron_parallel_state_utils.mock_distributed_parallel_state(world_size=2, rank=1): | ||
# tensor = torch.tensor([dist.get_rank()+1]) | ||
# dist.all_reduce(tensor) | ||
# assert tensor.item() == (1+2) / 2 # TODO does not work; there is no barrier for the actual communication; got 2 | ||
|
||
|
||
# move to src | ||
@contextmanager | ||
def dist_environment( | ||
world_size: int = 1, | ||
rank: int = 1, | ||
): | ||
with MonkeyPatch.context() as context: | ||
torch.cuda.empty_cache() | ||
parallel_state.destroy_model_parallel() | ||
|
||
context.setenv("MASTER_ADDR", "localhost") | ||
context.setenv("MASTER_PORT", "29500") | ||
dist.init_process_group(backend="nccl", world_size=world_size, rank=rank) | ||
yield | ||
dist.destroy_process_group() | ||
torch.cuda.empty_cache() | ||
parallel_state.destroy_model_parallel() | ||
|
||
|
||
def _test_all_reduce_sum(rank: int, world_size: int): | ||
with dist_environment(rank=rank, world_size=world_size): | ||
device = torch.device(f"cuda:{rank}") | ||
tensor = torch.tensor([rank + 1], device=device) | ||
dist.all_reduce(tensor) | ||
assert tensor.item() == world_size * (world_size + 1) / 2 | ||
|
||
|
||
@pytest.mark.skipif(torch.cuda.device_count() > 1, reason=f"Requires 2 devices but got {torch.cuda.device_count()}") | ||
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. shouldn't this be skipif device_count() < 2? |
||
def test_all_reduce_sum(): | ||
world_size = 2 | ||
torch.multiprocessing.spawn( | ||
fn=_test_all_reduce_sum, | ||
args=(world_size,), | ||
nprocs=world_size, | ||
) |
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.
Would be nice to eventually not need any kind of mocking etc, and have some kind of vanilla comparison of results through some kind of standard lightning etc function that's distributed with different levels of different kinds of parallelism asserting that results are equal to the base case (DDP only).