import faulthandler
import logging
import multiprocessing
import os
import queue
import subprocess
import sys
import tempfile
import threading
import time
import traceback
import types
import unittest
from contextlib import contextmanager
from dataclasses import dataclass
from datetime import timedelta
from enum import Enum
from functools import partial, reduce, wraps
from io import StringIO
from typing import Dict, NamedTuple, Optional, Union
from unittest.mock import patch
import torch
import torch._dynamo.test_case
import torch.cuda.nccl
import torch.distributed as c10d
import torch.nn as nn
from torch.testing._internal.common_utils import (
FILE_SCHEMA,
find_free_port,
IS_SANDCASTLE,
retry_on_connect_failures,
sandcastle_skip,
sandcastle_skip_if,
TEST_WITH_ROCM,
TEST_WITH_TSAN,
TestCase,
)
from torch.testing._internal.distributed.multi_threaded_pg import (
_install_threaded_pg,
_uninstall_threaded_pg,
ProcessLocalGroup,
)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class TestSkip(NamedTuple):
exit_code: int
message: str
TEST_SKIPS = {
"backend_unavailable": TestSkip(
72, "Skipped because distributed backend is not available."
),
"small_worldsize": TestSkip(73, "Skipped due to small world size."),
"odd_worldsize": TestSkip(87, "Skipped due to odd world size."),
"no_cuda": TestSkip(74, "CUDA is not available."),
"multi-gpu-1": TestSkip(75, "Need at least 1 CUDA device"),
"multi-gpu-2": TestSkip(77, "Need at least 2 CUDA devices"),
"multi-gpu-3": TestSkip(80, "Need at least 3 CUDA devices"),
"multi-gpu-4": TestSkip(81, "Need at least 4 CUDA devices"),
"multi-gpu-5": TestSkip(82, "Need at least 5 CUDA devices"),
"multi-gpu-6": TestSkip(83, "Need at least 6 CUDA devices"),
"multi-gpu-7": TestSkip(84, "Need at least 7 CUDA devices"),
"multi-gpu-8": TestSkip(85, "Need at least 8 CUDA devices"),
"nccl": TestSkip(76, "c10d not compiled with NCCL support"),
"skipIfRocm": TestSkip(78, "Test skipped for ROCm"),
"no_peer_access": TestSkip(79, "Test skipped because no GPU peer access"),
"generic": TestSkip(
86, "Test skipped at subprocess level, look at subprocess log for skip reason"
),
"importerror": TestSkip(88, "Test skipped due to missing import"),
}
@dataclass
class DistTestCases:
# Backends that do not support a specific collective
skip_collective = {}
skip_collective["allgather_coalesced"] = {"nccl", "mpi", "ucc"}
skip_collective["reduce"] = set()
skip_collective["sendrecv anysource"] = {"nccl", "ucc"}
skip_collective["cpu barrier"] = {"nccl", "ucc"}
# Sets showing that something is implemented
backend_feature = {}
backend_feature["gpu"] = {"nccl", "gloo", "ucc"}
backend_feature["cuda"] = {"nccl", "gloo", "ucc"}
backend_feature["ddp"] = {"nccl", "gloo", "ucc"}
backend_feature["subgroup"] = {"nccl", "gloo", "ucc"}
backend_feature["plugin"] = set()
def skip_if_no_gpu(func):
"""Skips if the world size exceeds the number of GPUs, ensuring that if the
test is run, each rank has its own GPU via ``torch.cuda.device(rank)``."""
@wraps(func)
def wrapper(*args, **kwargs):
if not torch.cuda.is_available():
sys.exit(TEST_SKIPS["no_cuda"].exit_code)
world_size = int(os.environ["WORLD_SIZE"])
if torch.cuda.device_count() < world_size:
sys.exit(TEST_SKIPS[f"multi-gpu-{world_size}"].exit_code)
return func(*args, **kwargs)
return wrapper
def skip_if_small_worldsize(func):
@wraps(func)
def wrapper(*args, **kwargs):
if (os.environ["BACKEND"] != "mpi") and int(os.environ["WORLD_SIZE"]) <= 2:
sys.exit(TEST_SKIPS["small_worldsize"].exit_code)
return func(*args, **kwargs)
return wrapper
def skip_if_odd_worldsize(func):
@wraps(func)
def wrapper(*args, **kwargs):
if (os.environ["BACKEND"] != "mpi") and int(os.environ["WORLD_SIZE"]) % 2 == 1:
sys.exit(TEST_SKIPS["odd_worldsize"].exit_code)
return func(*args, **kwargs)
return wrapper
def require_n_gpus_for_nccl_backend(n, backend):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
if backend == "nccl" and torch.cuda.device_count() < n:
sys.exit(TEST_SKIPS[f"multi-gpu-{n}"].exit_code)
else:
return func(*args, **kwargs)
return wrapper
return decorator
def import_transformers_or_skip():
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
from transformers import ( # noqa: Unused
AutoModelForMaskedLM,
BertConfig,
)
return func(*args, **kwargs)
except ImportError:
sys.exit(TEST_SKIPS["importerror"].exit_code)
return wrapper
return decorator
def skip_if_lt_x_gpu(x):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
if torch.cuda.is_available() and torch.cuda.device_count() >= x:
return func(*args, **kwargs)
sys.exit(TEST_SKIPS[f"multi-gpu-{x}"].exit_code)
return wrapper
return decorator
# This decorator helps avoiding initializing cuda while testing other backends
def nccl_skip_if_lt_x_gpu(backend, x):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
if backend != "nccl":
return func(*args, **kwargs)
if torch.cuda.is_available() and torch.cuda.device_count() >= x:
return func(*args, **kwargs)
sys.exit(TEST_SKIPS[f"multi-gpu-{x}"].exit_code)
return wrapper
return decorator
def verify_ddp_error_logged(model_DDP, err_substr):
# Verify error was logged in ddp_logging_data.
ddp_logging_data = model_DDP._get_ddp_logging_data()
assert "iteration" in ddp_logging_data
assert "has_error" in ddp_logging_data
assert "error" in ddp_logging_data
logging_err = ddp_logging_data["error"]
# Remove C++ stacktrace if needed.
actual = (
err_substr
if err_substr.find("\nException raised from ") == -1
else err_substr.split("\nException raised from ")[0]
)
assert (
actual in logging_err
), f"Did not find expected {actual} in ddp logging data error: {logging_err}"
def with_nccl_blocking_wait(func):
"""
Convenience decorator to set/unset NCCL_BLOCKING_WAIT flag. Note that use of
this decorator will override the setting of NCCL_ASYNC_ERROR_HANDLING for
the particular test. After the test, both NCCL_BLOCKING_WAIT and
NCCL_ASYNC_ERROR_HANDLING will be restored to their original values.
"""
@wraps(func)
def wrapper(*args, **kwargs):
# Save and unset NCCL_ASYNC_ERROR_HANDLING
try:
cached_nccl_async_error_handling: Union[str, None] = os.environ[
"NCCL_ASYNC_ERROR_HANDLING"
]
del os.environ["NCCL_ASYNC_ERROR_HANDLING"]
except KeyError:
# NCCL_ASYNC_ERROR_HANDLING was unset
cached_nccl_async_error_handling = None
# Save val of NCCL_BLOCKING_WAIT and set it.
try:
cached_nccl_blocking_wait: Union[str, None] = os.environ[
"NCCL_BLOCKING_WAIT"
]
except KeyError:
cached_nccl_blocking_wait = None
finally:
os.environ["NCCL_BLOCKING_WAIT"] = "1"
try:
ret = func(*args, **kwargs)
return ret
finally:
# restore old values.
if cached_nccl_async_error_handling is not None:
os.environ[
"NCCL_ASYNC_ERROR_HANDLING"
] = cached_nccl_async_error_handling
if cached_nccl_blocking_wait is not None:
os.environ["NCCL_BLOCKING_WAIT"] = cached_nccl_blocking_wait
return wrapper
def with_dist_debug_levels(levels):
"""
Runs a test for each distributed debug level specified in levels.
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
old_level = os.environ.get("TORCH_DISTRIBUTED_DEBUG", None)
for level in levels:
os.environ["TORCH_DISTRIBUTED_DEBUG"] = level
c10d.set_debug_level_from_env()
ret = func(*args, **kwargs)
c10d.barrier()
if old_level is not None:
os.environ["TORCH_DISTRIBUTED_DEBUG"] = old_level
# Only returns test return for last test, but since these are
# unittests the return value is not really used and earlier tests
# would've raised had they failed.
return ret
return wrapper
return decorator
def requires_gloo():
return sandcastle_skip_if(
not c10d.is_gloo_available(),
"c10d was not compiled with the Gloo backend",
)
def requires_nccl_version(version, msg):
if not c10d.is_nccl_available():
return sandcastle_skip(
"c10d was not compiled with the NCCL backend",
)
else:
return sandcastle_skip_if(
torch.cuda.nccl.version() < version,
"Requires NCCL version greater than or equal to: {}, found: {}, reason: {}".format(
version, torch.cuda.nccl.version(), msg
),
)
def requires_nccl():
return sandcastle_skip_if(
not c10d.is_nccl_available(),
"c10d was not compiled with the NCCL backend",
)
def requires_ucc():
return sandcastle_skip_if(
not c10d.is_ucc_available(),
"c10d was not compiled with the UCC backend",
)
def requires_mpi():
return sandcastle_skip_if(
not c10d.is_mpi_available(),
"c10d was not compiled with the MPI backend",
)
def skip_if_rocm(func):
"""Skips a test for ROCm"""
func.skip_if_rocm = True
@wraps(func)
def wrapper(*args, **kwargs):
if not TEST_WITH_ROCM:
return func(*args, **kwargs)
sys.exit(TEST_SKIPS["skipIfRocm"].exit_code)
return wrapper
def skip_if_win32():
return sandcastle_skip_if(
sys.platform == "win32",
"This unit test case is not supported on Windows platform",
)
Loading ...