|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"""Basic tests for a Disco nvshmem support""" |
| 18 | +# pylint: disable=missing-docstring |
| 19 | +import tempfile |
| 20 | + |
| 21 | +import numpy as np |
| 22 | +import pytest |
| 23 | +import subprocess |
| 24 | +import threading |
| 25 | +import sys |
| 26 | + |
| 27 | +import tvm |
| 28 | +import tvm.testing |
| 29 | +from tvm.runtime import ShapeTuple |
| 30 | +from tvm.runtime import disco as di |
| 31 | +from tvm.exec import disco_worker as _ # pylint: disable=unused-import |
| 32 | + |
| 33 | +_SOCKET_SESSION_TESTER = None |
| 34 | + |
| 35 | + |
| 36 | +def get_free_port(): |
| 37 | + import socket |
| 38 | + |
| 39 | + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 40 | + s.bind(("", 0)) |
| 41 | + port = s.getsockname()[1] |
| 42 | + s.close() |
| 43 | + return port |
| 44 | + |
| 45 | + |
| 46 | +class SocketSessionTester: |
| 47 | + def __init__(self, num_workers): |
| 48 | + num_nodes = 2 |
| 49 | + num_groups = 1 |
| 50 | + assert num_workers % num_nodes == 0 |
| 51 | + num_workers_per_node = num_workers // num_nodes |
| 52 | + server_host = "localhost" |
| 53 | + server_port = get_free_port() |
| 54 | + self.sess = None |
| 55 | + |
| 56 | + def start_server(): |
| 57 | + self.sess = di.SocketSession( |
| 58 | + num_nodes, num_workers_per_node, num_groups, server_host, server_port |
| 59 | + ) |
| 60 | + |
| 61 | + thread = threading.Thread(target=start_server) |
| 62 | + thread.start() |
| 63 | + |
| 64 | + cmd = "tvm.exec.disco_remote_socket_session" |
| 65 | + self.remote_nodes = [] |
| 66 | + for _ in range(num_nodes - 1): |
| 67 | + self.remote_nodes.append( |
| 68 | + subprocess.Popen( |
| 69 | + [ |
| 70 | + "python3", |
| 71 | + "-m", |
| 72 | + cmd, |
| 73 | + server_host, |
| 74 | + str(server_port), |
| 75 | + str(num_workers_per_node), |
| 76 | + ], |
| 77 | + stdout=sys.stdout, |
| 78 | + stderr=sys.stderr, |
| 79 | + ) |
| 80 | + ) |
| 81 | + |
| 82 | + thread.join() |
| 83 | + |
| 84 | + def __del__(self): |
| 85 | + for node in self.remote_nodes: |
| 86 | + node.kill() |
| 87 | + if self.sess is not None: |
| 88 | + self.sess.shutdown() |
| 89 | + del self.sess |
| 90 | + |
| 91 | + |
| 92 | +def create_socket_session(num_workers): |
| 93 | + global _SOCKET_SESSION_TESTER |
| 94 | + if _SOCKET_SESSION_TESTER is not None: |
| 95 | + del _SOCKET_SESSION_TESTER |
| 96 | + _SOCKET_SESSION_TESTER = SocketSessionTester(num_workers) |
| 97 | + assert _SOCKET_SESSION_TESTER.sess is not None |
| 98 | + return _SOCKET_SESSION_TESTER.sess |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize("num_workers", [2, 4]) |
| 102 | +def test_nvshmem_init(num_workers): |
| 103 | + if tvm.get_global_func("runtime.disco.nvshmem.init_nvshmem_uid", True) is None: |
| 104 | + return |
| 105 | + sess = create_socket_session(num_workers=num_workers) |
| 106 | + f_init_nvshmem_uid = tvm.get_global_func("runtime.disco.nvshmem.init_nvshmem_uid") |
| 107 | + uid = f_init_nvshmem_uid() |
| 108 | + init_dfunc = sess.get_global_func("runtime.disco.nvshmem.init_nvshmem") |
| 109 | + init_dfunc(uid, num_workers) |
| 110 | + sess.sync_worker_0() |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + tvm.testing.main() |
0 commit comments