From 61b2bb577830e4f18c08bcd2b2dd51156e038042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Mon, 14 Aug 2023 14:28:38 +0800 Subject: [PATCH] [xdoctest] reformat example code with google style in No. 231 (#56213) --- .../paddle/distributed/parallel_with_gloo.py | 234 +++++++++--------- 1 file changed, 117 insertions(+), 117 deletions(-) diff --git a/python/paddle/distributed/parallel_with_gloo.py b/python/paddle/distributed/parallel_with_gloo.py index 3204b6460d3..f205a5d2612 100755 --- a/python/paddle/distributed/parallel_with_gloo.py +++ b/python/paddle/distributed/parallel_with_gloo.py @@ -54,44 +54,44 @@ def gloo_init_parallel_env(rank_id, rank_num, server_endpoint): Examples: .. code-block:: python - import paddle - import multiprocessing - from contextlib import closing - import socket - - port_set = set() - - def find_free_port(): - def _free_port(): - with closing(socket.socket(socket.AF_INET, - socket.SOCK_STREAM)) as s: - s.bind(('', 0)) - return s.getsockname()[1] - while True: - port = _free_port() - if port not in port_set: - port_set.add(port) - return port - - def test_gloo_init(id, rank_num, server_endpoint): - paddle.distributed.gloo_init_parallel_env( - id, rank_num, server_endpoint) - - def test_gloo_init_with_multiprocess(num_of_ranks): - jobs = [] - server_endpoint = "127.0.0.1:%s" % (find_free_port()) - for id in range(num_of_ranks): - p = multiprocessing.Process( - target=test_gloo_init, - args=(id, num_of_ranks, server_endpoint)) - jobs.append(p) - p.start() - for proc in jobs: - proc.join() - - if __name__ == '__main__': - # Arg: number of ranks (processes) - test_gloo_init_with_multiprocess(2) + >>> import paddle + >>> import multiprocessing + >>> from contextlib import closing + >>> import socket + + >>> port_set = set() + + >>> def find_free_port(): + ... def _free_port(): + ... with closing(socket.socket(socket.AF_INET, + ... socket.SOCK_STREAM)) as s: + ... s.bind(('', 0)) + ... return s.getsockname()[1] + ... while True: + ... port = _free_port() + ... if port not in port_set: + ... port_set.add(port) + ... return port + + >>> def test_gloo_init(id, rank_num, server_endpoint): + ... paddle.distributed.gloo_init_parallel_env( + ... id, rank_num, server_endpoint) + + >>> def test_gloo_init_with_multiprocess(num_of_ranks): + ... jobs = [] + ... server_endpoint = "127.0.0.1:%s" % (find_free_port()) + ... for id in range(num_of_ranks): + ... p = multiprocessing.Process( + ... target=test_gloo_init, + ... args=(id, num_of_ranks, server_endpoint)) + ... jobs.append(p) + ... p.start() + ... for proc in jobs: + ... proc.join() + + >>> if __name__ == '__main__': + ... # Arg: number of ranks (processes) + ... test_gloo_init_with_multiprocess(2) """ assert ( @@ -149,45 +149,45 @@ def gloo_barrier(): Examples: .. code-block:: python - import paddle - import multiprocessing - from contextlib import closing - import socket - - port_set = set() - - def find_free_port(): - def _free_port(): - with closing(socket.socket(socket.AF_INET, - socket.SOCK_STREAM)) as s: - s.bind(('', 0)) - return s.getsockname()[1] - while True: - port = _free_port() - if port not in port_set: - port_set.add(port) - return port - - def test_gloo_barrier(id, rank_num, server_endpoint): - paddle.distributed.gloo_init_parallel_env( - id, rank_num, server_endpoint) - paddle.distributed.gloo_barrier() - - def test_gloo_barrier_with_multiprocess(num_of_ranks): - jobs = [] - server_endpoint = "127.0.0.1:%s" % (find_free_port()) - for id in range(num_of_ranks): - p = multiprocessing.Process( - target=test_gloo_barrier, - args=(id, num_of_ranks, server_endpoint)) - jobs.append(p) - p.start() - for proc in jobs: - proc.join() - - if __name__ == '__main__': - # Arg: number of ranks (processes) - test_gloo_barrier_with_multiprocess(2) + >>> import paddle + >>> import multiprocessing + >>> from contextlib import closing + >>> import socket + + >>> port_set = set() + + >>> def find_free_port(): + ... def _free_port(): + ... with closing(socket.socket(socket.AF_INET, + ... socket.SOCK_STREAM)) as s: + ... s.bind(('', 0)) + ... return s.getsockname()[1] + ... while True: + ... port = _free_port() + ... if port not in port_set: + ... port_set.add(port) + ... return port + + >>> def test_gloo_barrier(id, rank_num, server_endpoint): + ... paddle.distributed.gloo_init_parallel_env( + ... id, rank_num, server_endpoint) + ... paddle.distributed.gloo_barrier() + + >>> def test_gloo_barrier_with_multiprocess(num_of_ranks): + ... jobs = [] + ... server_endpoint = "127.0.0.1:%s" % (find_free_port()) + ... for id in range(num_of_ranks): + ... p = multiprocessing.Process( + ... target=test_gloo_barrier, + ... args=(id, num_of_ranks, server_endpoint)) + ... jobs.append(p) + ... p.start() + ... for proc in jobs: + ... proc.join() + + >>> if __name__ == '__main__': + ... # Arg: number of ranks (processes) + ... test_gloo_barrier_with_multiprocess(2) """ assert _global_gloo_ctx is not None, "gloo context is not initialzed." @@ -207,46 +207,46 @@ def gloo_release(): Examples: .. code-block:: python - import paddle - import multiprocessing - from contextlib import closing - import socket - - port_set = set() - - def find_free_port(): - def _free_port(): - with closing(socket.socket(socket.AF_INET, - socket.SOCK_STREAM)) as s: - s.bind(('', 0)) - return s.getsockname()[1] - while True: - port = _free_port() - if port not in port_set: - port_set.add(port) - return port - - def test_gloo_release(id, rank_num, server_endpoint): - paddle.distributed.gloo_init_parallel_env( - id, rank_num, server_endpoint) - paddle.distributed.gloo_barrier() - paddle.distributed.gloo_release() - - def test_gloo_release_with_multiprocess(num_of_ranks): - jobs = [] - server_endpoint = "127.0.0.1:%s" % (find_free_port()) - for id in range(num_of_ranks): - p = multiprocessing.Process( - target=test_gloo_release, - args=(id, num_of_ranks, server_endpoint)) - jobs.append(p) - p.start() - for proc in jobs: - proc.join() - - if __name__ == '__main__': - # Arg: number of ranks (processes) - test_gloo_release_with_multiprocess(2) + >>> import paddle + >>> import multiprocessing + >>> from contextlib import closing + >>> import socket + + >>> port_set = set() + + >>> def find_free_port(): + ... def _free_port(): + ... with closing(socket.socket(socket.AF_INET, + ... socket.SOCK_STREAM)) as s: + ... s.bind(('', 0)) + ... return s.getsockname()[1] + ... while True: + ... port = _free_port() + ... if port not in port_set: + ... port_set.add(port) + ... return port + + >>> def test_gloo_release(id, rank_num, server_endpoint): + ... paddle.distributed.gloo_init_parallel_env( + ... id, rank_num, server_endpoint) + ... paddle.distributed.gloo_barrier() + ... paddle.distributed.gloo_release() + + >>> def test_gloo_release_with_multiprocess(num_of_ranks): + ... jobs = [] + ... server_endpoint = "127.0.0.1:%s" % (find_free_port()) + ... for id in range(num_of_ranks): + ... p = multiprocessing.Process( + ... target=test_gloo_release, + ... args=(id, num_of_ranks, server_endpoint)) + ... jobs.append(p) + ... p.start() + ... for proc in jobs: + ... proc.join() + + >>> if __name__ == '__main__': + ... # Arg: number of ranks (processes) + ... test_gloo_release_with_multiprocess(2) """ if _global_gloo_ctx is not None: -- GitLab