|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +""" |
| 4 | +Multi-node integration test for MessageQueue TCP fallback. |
| 5 | +
|
| 6 | +Verifies that when writer and readers span separate nodes (Docker containers |
| 7 | +with isolated /dev/shm), `create_from_process_group` correctly detects |
| 8 | +cross-node ranks via `in_the_same_node_as()` and falls back to ZMQ TCP |
| 9 | +transport — and that data actually arrives. |
| 10 | +""" |
| 11 | + |
| 12 | +import numpy as np |
| 13 | +import torch.distributed as dist |
| 14 | + |
| 15 | +from vllm.distributed.device_communicators.shm_broadcast import MessageQueue |
| 16 | +from vllm.distributed.parallel_state import in_the_same_node_as |
| 17 | + |
| 18 | + |
| 19 | +def main(): |
| 20 | + dist.init_process_group(backend="gloo") |
| 21 | + |
| 22 | + rank = dist.get_rank() |
| 23 | + world_size = dist.get_world_size() |
| 24 | + assert world_size >= 2, ( |
| 25 | + f"Need at least 2 ranks across nodes, got world_size={world_size}" |
| 26 | + ) |
| 27 | + |
| 28 | + # Verify that in_the_same_node_as detects cross-node correctly |
| 29 | + status = in_the_same_node_as(dist.group.WORLD, source_rank=0) |
| 30 | + local_count = sum(status) |
| 31 | + print( |
| 32 | + f"[Rank {rank}] in_the_same_node_as(source=0): {status} " |
| 33 | + f"(local={local_count}/{world_size})" |
| 34 | + ) |
| 35 | + # With 2 Docker containers (1 proc each), rank 0 and rank 1 |
| 36 | + # should be on different nodes. |
| 37 | + assert local_count < world_size, ( |
| 38 | + f"Expected cross-node ranks but all {world_size} ranks appear local." |
| 39 | + ) |
| 40 | + |
| 41 | + # Create MessageQueue |
| 42 | + writer_rank = 0 |
| 43 | + mq = MessageQueue.create_from_process_group( |
| 44 | + dist.group.WORLD, |
| 45 | + max_chunk_bytes=1024 * 1024, # 1 MiB |
| 46 | + max_chunks=10, |
| 47 | + writer_rank=writer_rank, |
| 48 | + ) |
| 49 | + |
| 50 | + # Verify the transport path selection |
| 51 | + if rank == writer_rank: |
| 52 | + print( |
| 53 | + f"[Rank {rank}] Writer: n_local_reader={mq.n_local_reader}, " |
| 54 | + f"n_remote_reader={mq.n_remote_reader}" |
| 55 | + ) |
| 56 | + assert mq.n_remote_reader > 0, ( |
| 57 | + "Writer should have at least 1 remote (TCP) reader in a multi-node setup." |
| 58 | + ) |
| 59 | + else: |
| 60 | + if status[rank]: |
| 61 | + assert mq._is_local_reader, ( |
| 62 | + f"Rank {rank} is on the same node as writer but is not a local reader." |
| 63 | + ) |
| 64 | + print(f"[Rank {rank}] Reader: local (shared memory)") |
| 65 | + else: |
| 66 | + assert mq._is_remote_reader, ( |
| 67 | + f"Rank {rank} is on a different node but is not a remote (TCP) reader." |
| 68 | + ) |
| 69 | + print(f"[Rank {rank}] Reader: remote (TCP)") |
| 70 | + |
| 71 | + # Test data transfer: simple objects |
| 72 | + dist.barrier() |
| 73 | + if rank == writer_rank: |
| 74 | + mq.enqueue("hello_from_node0") |
| 75 | + else: |
| 76 | + msg = mq.dequeue(timeout=10) |
| 77 | + assert msg == "hello_from_node0" |
| 78 | + dist.barrier() |
| 79 | + print(f"[Rank {rank}] Simple object test passed") |
| 80 | + |
| 81 | + # Test data transfer: numpy arrays |
| 82 | + np.random.seed(42) |
| 83 | + arrays = [ |
| 84 | + np.random.randint(0, 100, size=np.random.randint(100, 5000)) for _ in range(100) |
| 85 | + ] |
| 86 | + |
| 87 | + dist.barrier() |
| 88 | + if rank == writer_rank: |
| 89 | + for arr in arrays: |
| 90 | + mq.enqueue(arr) |
| 91 | + else: |
| 92 | + for i, expected in enumerate(arrays): |
| 93 | + received = mq.dequeue(timeout=10) |
| 94 | + assert np.array_equal(expected, received), ( |
| 95 | + f"Array mismatch at index {i}: " |
| 96 | + f"expected shape {expected.shape}, got shape {received.shape}" |
| 97 | + ) |
| 98 | + dist.barrier() |
| 99 | + print(f"[Rank {rank}] Numpy array test passed") |
| 100 | + |
| 101 | + # Test data transfer: large payload (> max_chunk_bytes) |
| 102 | + dist.barrier() |
| 103 | + big_array = np.zeros(200_000, dtype=np.int64) # ~1.6 MiB > 1 MiB chunk |
| 104 | + if rank == writer_rank: |
| 105 | + mq.enqueue(big_array) |
| 106 | + else: |
| 107 | + received = mq.dequeue(timeout=10) |
| 108 | + assert np.array_equal(big_array, received) |
| 109 | + dist.barrier() |
| 110 | + print(f"[Rank {rank}] Large payload test passed") |
| 111 | + |
| 112 | + # Done -- cleanup |
| 113 | + dist.barrier() |
| 114 | + print(f"[Rank {rank}] All MessageQueue TCP multi-node tests passed!") |
| 115 | + dist.destroy_process_group() |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + main() |
0 commit comments