xref: /aosp_15_r20/external/pytorch/test/distributed/launcher/bin/test_script_is_torchelastic_launched.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1#!/usr/bin/env python3
2# Owner(s): ["oncall: r2p"]
3
4# Copyright (c) Facebook, Inc. and its affiliates.
5# All rights reserved.
6#
7# This source code is licensed under the BSD-style license found in the
8# LICENSE file in the root directory of this source tree.
9
10"""
11This is a test script that launches as part of the test cases in
12run_test.py, to validate the correctness of
13the method ``torch.distributed.is_torchelastic_launched()``. To do so,
14we run this script with and without torchelastic and validate that the
15boolean value written to the out_file is indeed what we expect (e.g.
16should be False when not launched with torchelastic, True when launched with)
17The script itself is not a test case hence no assertions are made in this script.
18
19see: - test/distributed/launcher/run_test.py#test_is_torchelastic_launched()
20     - test/distributed/launcher/run_test.py#test_is_not_torchelastic_launched()
21"""
22import argparse
23
24import torch.distributed as dist
25
26
27def parse_args():
28    parser = argparse.ArgumentParser(description="test script")
29    parser.add_argument(
30        "--out-file",
31        "--out_file",
32        help="file to write indicating whether this script was launched with torchelastic",
33    )
34    return parser.parse_args()
35
36
37def main():
38    args = parse_args()
39    with open(args.out_file, "w") as out:
40        out.write(f"{dist.is_torchelastic_launched()}")
41
42
43if __name__ == "__main__":
44    main()
45