Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Copy link
Collaborator

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).

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()}")
Copy link
Collaborator

Choose a reason for hiding this comment

The 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,
)
Loading