|
| 1 | +"""HCCL all_to_all_single split/numel validation on 2/4/8 NPUs.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import time |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +SCRIPT = r''' |
| 12 | +import os, sys, time |
| 13 | +src_dir = os.environ.get("MINDTORCH_V2_SRC") |
| 14 | +if src_dir: |
| 15 | + sys.path.insert(0, src_dir) |
| 16 | +
|
| 17 | +import mindtorch_v2 as torch |
| 18 | +import mindtorch_v2.distributed as dist |
| 19 | +
|
| 20 | +rank = int(os.environ["RANK"]) |
| 21 | +world_size = int(os.environ["WORLD_SIZE"]) |
| 22 | +
|
| 23 | +mode = os.environ["CASE_MODE"] |
| 24 | +
|
| 25 | +device = torch.Device(f"npu:{rank}") |
| 26 | +time.sleep(0.05 * rank) |
| 27 | +dist.init_process_group("hccl", device_id=device) |
| 28 | +
|
| 29 | +base_in = [1 if i == rank else 2 for i in range(world_size)] |
| 30 | +base_out = [1 if j == rank else 2 for j in range(world_size)] |
| 31 | +
|
| 32 | +if mode == "input_sum_mismatch": |
| 33 | + input_split = list(base_in) |
| 34 | + output_split = list(base_out) |
| 35 | + # Make split sum larger than input numel. |
| 36 | + input_split[rank] += 1 |
| 37 | + inp_numel = sum(base_in) |
| 38 | + out_numel = sum(output_split) |
| 39 | +elif mode == "output_sum_mismatch": |
| 40 | + input_split = list(base_in) |
| 41 | + output_split = list(base_out) |
| 42 | + # Make split sum larger than output numel. |
| 43 | + output_split[rank] += 1 |
| 44 | + inp_numel = sum(input_split) |
| 45 | + out_numel = sum(base_out) |
| 46 | +else: |
| 47 | + raise RuntimeError(f"unexpected mode: {mode}") |
| 48 | +
|
| 49 | +inp = torch.zeros(inp_numel, device=device) |
| 50 | +out = torch.zeros(out_numel, device=device) |
| 51 | +
|
| 52 | +try: |
| 53 | + dist.all_to_all_single( |
| 54 | + out, |
| 55 | + inp, |
| 56 | + output_split_sizes=output_split, |
| 57 | + input_split_sizes=input_split, |
| 58 | + async_op=True, |
| 59 | + ) |
| 60 | +except ValueError as exc: |
| 61 | + msg = str(exc) |
| 62 | + assert "numel" in msg and "split" in msg, msg |
| 63 | +else: |
| 64 | + raise AssertionError("expected ValueError for split sum and tensor numel mismatch") |
| 65 | +
|
| 66 | +dist.destroy_process_group() |
| 67 | +print(f"[rank {rank}] HCCL split/numel validation {mode} {world_size}card PASS") |
| 68 | +''' |
| 69 | + |
| 70 | + |
| 71 | +def _run_once(world_size, master_port, mode): |
| 72 | + env = os.environ.copy() |
| 73 | + env["MASTER_ADDR"] = "127.0.0.1" |
| 74 | + env["MASTER_PORT"] = str(master_port) |
| 75 | + env["WORLD_SIZE"] = str(world_size) |
| 76 | + env["CASE_MODE"] = mode |
| 77 | + src_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "src")) |
| 78 | + env["MINDTORCH_V2_SRC"] = src_dir |
| 79 | + env["PYTHONPATH"] = src_dir + \ |
| 80 | + (":" + env["PYTHONPATH"] if "PYTHONPATH" in env else "") |
| 81 | + |
| 82 | + worker_file = f"/tmp/_hccl_all_to_all_single_split_numel_validation_{world_size}card.py" |
| 83 | + with open(worker_file, "w") as f: |
| 84 | + f.write(SCRIPT) |
| 85 | + |
| 86 | + failed = [] |
| 87 | + outputs = [] |
| 88 | + procs = [] |
| 89 | + |
| 90 | + for r in range(world_size): |
| 91 | + p = subprocess.Popen( |
| 92 | + [sys.executable, worker_file], |
| 93 | + env={**env, "RANK": str(r)}, |
| 94 | + stdout=subprocess.PIPE, |
| 95 | + stderr=subprocess.STDOUT, |
| 96 | + ) |
| 97 | + procs.append(p) |
| 98 | + |
| 99 | + timeout = 420 if world_size <= 4 else 900 |
| 100 | + for r, p in enumerate(procs): |
| 101 | + try: |
| 102 | + out, _ = p.communicate(timeout=timeout) |
| 103 | + txt = out.decode("utf-8", errors="replace") |
| 104 | + except subprocess.TimeoutExpired: |
| 105 | + p.kill() |
| 106 | + out, _ = p.communicate() |
| 107 | + txt = "TIMEOUT\n" + out.decode("utf-8", errors="replace") |
| 108 | + outputs.append(txt) |
| 109 | + if p.returncode != 0: |
| 110 | + failed.append(r) |
| 111 | + |
| 112 | + return failed, outputs |
| 113 | + |
| 114 | + |
| 115 | +def _run_case(world_size, master_port, mode): |
| 116 | + retries = 3 |
| 117 | + for attempt in range(1, retries + 1): |
| 118 | + failed, outputs = _run_once(world_size, master_port, mode) |
| 119 | + if not failed: |
| 120 | + return |
| 121 | + |
| 122 | + joined = "\n".join(outputs) |
| 123 | + transient = "resource unavailable" in joined |
| 124 | + if transient and attempt < retries: |
| 125 | + print( |
| 126 | + f"HCCL transient init failure on {world_size} cards ({mode}), " |
| 127 | + f"retry {attempt}/{retries}" |
| 128 | + ) |
| 129 | + time.sleep(5) |
| 130 | + continue |
| 131 | + |
| 132 | + for r, txt in enumerate(outputs): |
| 133 | + print(f"=== RANK {r} ===") |
| 134 | + print(txt) |
| 135 | + raise AssertionError( |
| 136 | + f"HCCL split/numel validation {mode} {world_size}card failed on ranks: {failed}" |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +@pytest.mark.parametrize( |
| 141 | + "world_size,master_port", |
| 142 | + [ |
| 143 | + (2, 29716), |
| 144 | + (4, 29726), |
| 145 | + (8, 29736), |
| 146 | + ], |
| 147 | +) |
| 148 | +@pytest.mark.parametrize("mode", ["input_sum_mismatch", "output_sum_mismatch"]) |
| 149 | +def test_hccl_all_to_all_single_split_numel_validation_multicard(world_size, master_port, mode): |
| 150 | + _run_case(world_size, master_port, mode) |
0 commit comments