File: api_test.py

package info (click to toggle)
pytorch-cuda 2.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 161,620 kB
  • sloc: python: 1,278,832; cpp: 900,322; ansic: 82,710; asm: 7,754; java: 3,363; sh: 2,811; javascript: 2,443; makefile: 597; ruby: 195; xml: 84; objc: 68
file content (940 lines) | stat: -rw-r--r-- 35,470 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
#!/usr/bin/env python3
# Owner(s): ["oncall: r2p"]

# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import asyncio
import ctypes
import multiprocessing
import os
import shutil
import signal
import sys
import tempfile
import time
from itertools import product
from typing import Callable, Dict, List, Union
from unittest import mock

import torch
import torch.multiprocessing as mp
from torch.distributed.elastic.multiprocessing import ProcessFailure, start_processes
from torch.distributed.elastic.multiprocessing.api import (
    _validate_full_rank,
    _wrap,
    DefaultLogsSpecs,
    MultiprocessContext,
    RunProcsResult,
    SignalException,
    Std,
    to_map,
)
from torch.distributed.elastic.multiprocessing.errors import ErrorHandler
from torch.testing._internal.common_utils import (
    IS_CI,
    IS_MACOS,
    IS_WINDOWS,
    NO_MULTIPROCESSING_SPAWN,
    run_tests,
    skip_but_pass_in_sandcastle_if,
    skip_if_pytest,
    TEST_WITH_ASAN,
    TEST_WITH_DEV_DBG_ASAN,
    TEST_WITH_TSAN,
    TestCase,
)


class RunProcResultsTest(TestCase):
    def setUp(self):
        super().setUp()
        self.test_dir = tempfile.mkdtemp(prefix=f"{self.__class__.__name__}_")

    def tearDown(self):
        super().tearDown()
        shutil.rmtree(self.test_dir)

    def test_is_failed(self):
        pr_success = RunProcsResult(return_values={0: "a", 1: "b"})
        self.assertFalse(pr_success.is_failed())

        fail0 = ProcessFailure(
            local_rank=0, pid=998, exitcode=1, error_file="ignored.json"
        )
        pr_fail = RunProcsResult(failures={0: fail0})
        self.assertTrue(pr_fail.is_failed())

    def test_get_failures(self):
        error_file0 = os.path.join(self.test_dir, "error0.json")
        error_file1 = os.path.join(self.test_dir, "error1.json")
        eh = ErrorHandler()
        with mock.patch.dict(os.environ, {"TORCHELASTIC_ERROR_FILE": error_file0}):
            eh.record_exception(RuntimeError("error 0"))

        with mock.patch.dict(os.environ, {"TORCHELASTIC_ERROR_FILE": error_file0}):
            eh.record_exception(RuntimeError("error 1"))

        fail0 = ProcessFailure(
            local_rank=0, pid=997, exitcode=1, error_file=error_file0
        )
        fail1 = ProcessFailure(
            local_rank=1, pid=998, exitcode=3, error_file=error_file1
        )
        fail2 = ProcessFailure(
            local_rank=2, pid=999, exitcode=15, error_file="no_exist.json"
        )

        self.assertLessEqual(fail0.timestamp, fail1.timestamp)
        self.assertLessEqual(fail1.timestamp, fail2.timestamp)


class StdTest(TestCase):
    def test_from_value(self):
        self.assertEqual(Std.NONE, Std.from_str("0"))
        self.assertEqual(Std.OUT, Std.from_str("1"))
        self.assertEqual(Std.ERR, Std.from_str("2"))
        self.assertEqual(Std.ALL, Std.from_str("3"))

    def test_from_value_map(self):
        self.assertEqual({0: Std.OUT}, Std.from_str("0:1"))
        self.assertEqual({0: Std.OUT, 1: Std.OUT}, Std.from_str("0:1,1:1"))

    def test_from_str_bad_input(self):
        bad_inputs = ["0:1,", "11", "0:1,1", "1,0:1"]
        for bad in bad_inputs:
            with self.subTest(bad=bad):
                with self.assertRaises(ValueError):
                    Std.from_str(bad)


def echo0(msg: str) -> None:
    """
    void function
    """
    print(msg)


def echo1(msg: str, exitcode: int = 0) -> str:
    """
    returns ``msg`` or exits with the given exitcode (if nonzero)
    """

    rank = int(os.environ["RANK"])
    if exitcode != 0:
        print(f"exit {exitcode} from {rank}", file=sys.stderr)
        sys.exit(exitcode)
    else:
        print(f"{msg} stdout from {rank}")
        print(f"{msg} stderr from {rank}", file=sys.stderr)
        return f"{msg}_{rank}"


def echo2(msg: str, fail: bool = False) -> str:
    """
    returns ``msg`` or raises a RuntimeError if ``fail`` is set
    """
    if fail:
        raise RuntimeError(msg)
    return msg


def echo_large(size: int) -> Dict[int, str]:
    """
    returns a large output ({0: test0", 1: "test1", ..., (size-1):f"test{size-1}"})
    """
    out = {}
    for idx in range(0, size):
        out[idx] = f"test{idx}"
    return out


def echo3(msg: str, fail: bool = False) -> str:
    """
    returns ``msg`` or induces a SIGSEGV if ``fail`` is set
    """
    if fail:
        ctypes.string_at(0)
    return msg


def dummy_compute() -> torch.Tensor:
    """
    returns a predefined size random Tensor
    """
    return torch.rand(100, 100)


def redirects_oss_test() -> List[Std]:
    return [
        Std.NONE,
    ]


def redirects_all() -> List[Std]:
    return [
        Std.NONE,
        Std.OUT,
        Std.ERR,
        Std.ALL,
    ]


def bin(name: str):
    dir = os.path.dirname(__file__)
    return os.path.join(dir, "bin", name)


def wait_fn(wait_time: int = 300) -> None:
    time.sleep(wait_time)
    print("Finished waiting")


def start_processes_zombie_test(
    idx: int,
    entrypoint: Union[str, Callable],
    mp_queue: mp.Queue,
    log_dir: str,
    nproc: int = 2,
) -> None:
    """
    Starts processes
    """

    args = {}
    envs = {}
    for idx in range(nproc):
        args[idx] = ()
        envs[idx] = {}

    pc = start_processes(
        name="zombie_test",
        entrypoint=entrypoint,
        args=args,
        envs=envs,
        logs_specs=DefaultLogsSpecs(log_dir=log_dir),
    )
    my_pid = os.getpid()
    mp_queue.put(my_pid)
    for child_pid in pc.pids().values():
        mp_queue.put(child_pid)

    try:
        pc.wait(period=1, timeout=300)
    except SignalException as e:
        pc.close(e.sigval)


class _StartProcessesTest(TestCase):
    def setUp(self):
        super().setUp()
        self.test_dir = tempfile.mkdtemp(prefix=f"{self.__class__.__name__}_")
        self._start_methods = ["spawn"]

    def tearDown(self):
        super().tearDown()
        shutil.rmtree(self.test_dir)

    def log_dir(self):
        return tempfile.mkdtemp(dir=self.test_dir)

    def assert_in_file(self, expected: List[str], filename: str) -> None:
        expected = [f"{line.rstrip()}\n" for line in expected]
        with open(filename) as fp:
            actual = fp.readlines()
            for line in expected:
                self.assertIn(line, actual)

    def assert_pids_noexist(self, pids: Dict[int, int]):
        for local_rank, pid in pids.items():
            with self.assertRaises(
                OSError, msg=f"local_rank: {local_rank} pid: {pid} should not exist"
            ):
                os.kill(pid, 0)

    def _test_zombie_workflow(
        self, entrypoint: Union[str, Callable], signal_to_send: signal.Signals
    ) -> None:
        mp_queue = mp.get_context("spawn").Queue()
        child_nproc = 2
        ctx = mp.spawn(
            start_processes_zombie_test,
            nprocs=1,
            args=(entrypoint, mp_queue, self.log_dir(), child_nproc),
            join=False,
        )
        total_processes = child_nproc + 1
        pids = []
        for _ in range(total_processes):
            pids.append(mp_queue.get(timeout=120))
        parent_pid = pids[0]
        child_pids = pids[1:]

        os.kill(parent_pid, signal.SIGTERM)
        # Wait to give time for signal handlers to finish work
        time.sleep(5)
        for child_pid in child_pids:
            # Killing parent should kill all children, we expect that each call to
            # os.kill would raise OSError
            with self.assertRaises(OSError):
                os.kill(child_pid, 0)


# tests incompatible with tsan or asan
if not (TEST_WITH_DEV_DBG_ASAN or IS_WINDOWS or IS_MACOS):

    class StartProcessesAsFuncTest(_StartProcessesTest):
        def test_to_map(self):
            local_world_size = 2
            self.assertEqual(
                {0: Std.OUT, 1: Std.OUT}, to_map(Std.OUT, local_world_size)
            )
            self.assertEqual(
                {0: Std.NONE, 1: Std.OUT}, to_map({1: Std.OUT}, local_world_size)
            )
            self.assertEqual(
                {0: Std.ERR, 1: Std.OUT},
                to_map({0: Std.ERR, 1: Std.OUT}, local_world_size),
            )

        def test_invalid_log_dir(self):
            with tempfile.NamedTemporaryFile(dir=self.test_dir) as not_a_dir:
                cases = {
                    not_a_dir.name: NotADirectoryError,
                }

                for log_dir, expected_error in cases.items():
                    with self.subTest(log_dir=log_dir, expected_error=expected_error):
                        with self.assertRaises(expected_error):
                            pc = None
                            try:
                                pc = start_processes(
                                    name="echo",
                                    entrypoint=echo1,
                                    args={0: ("hello",)},
                                    envs={0: {"RANK": "0"}},
                                    logs_specs=DefaultLogsSpecs(log_dir=log_dir),
                                )
                            finally:
                                if pc:
                                    pc.close()

        def test_args_env_len_mismatch(self):
            cases = [
                # 1 x args; 2 x envs
                {
                    "args": {0: ("hello",)},
                    "envs": {0: {"RANK": "0"}, 1: {"RANK": "1"}},
                },
                # 2 x args; 1 x envs
                {
                    "args": {0: ("hello",), 1: ("world",)},
                    "envs": {0: {"RANK": "0"}},
                },
            ]

            for kwds in cases:
                args = kwds["args"]
                envs = kwds["envs"]
                with self.subTest(args=args, envs=envs):
                    with self.assertRaises(RuntimeError):
                        start_processes(
                            name="echo",
                            entrypoint=echo1,
                            args=args,
                            envs=envs,
                            logs_specs=DefaultLogsSpecs(log_dir=self.log_dir()),
                        )

        def test_pcontext_wait(self):
            pc = start_processes(
                name="sleep",
                entrypoint=time.sleep,
                args={0: (1,)},
                envs={0: {}},
                logs_specs=DefaultLogsSpecs(log_dir=self.log_dir()),
                start_method="spawn",
            )

            self.assertIsNone(pc.wait(timeout=0.1, period=0.01))
            self.assertIsNotNone(pc.wait(period=0.1))
            self.assertTrue(pc._stderr_tail.stopped())
            self.assertTrue(pc._stdout_tail.stopped())

        def test_pcontext_wait_on_a_child_thread(self):
            asyncio.run(asyncio.to_thread(self.test_pcontext_wait))

        def test_multiprocess_context_close(self):
            pc = start_processes(
                name="sleep",
                entrypoint=time.sleep,
                args={0: (1,)},
                envs={0: {}},
                logs_specs=DefaultLogsSpecs(log_dir=self.log_dir()),
                start_method="spawn",
            )

            pids = pc.pids()
            pc.close()
            self.assert_pids_noexist(pids)
            self.assertTrue(pc._stderr_tail.stopped())
            self.assertTrue(pc._stdout_tail.stopped())

        def test_function_with_tensor(self):
            for start_method in self._start_methods:
                pc = start_processes(
                    name="dummy_compute",
                    entrypoint=dummy_compute,
                    args={0: ()},
                    envs={0: {}},
                    logs_specs=DefaultLogsSpecs(log_dir=self.log_dir()),
                    start_method=start_method,
                )

                results = pc.wait()
                self.assert_pids_noexist(pc.pids())
                for return_value in results.return_values.values():
                    self.assertIsInstance(return_value, torch.Tensor)
                    self.assertEqual((100, 100), return_value.shape)

        def test_void_function(self):
            for start_method in self._start_methods:
                with self.subTest(start_method=start_method):
                    pc = start_processes(
                        name="echo",
                        entrypoint=echo0,
                        args={0: ("hello",), 1: ("world",)},
                        envs={0: {}, 1: {}},
                        logs_specs=DefaultLogsSpecs(log_dir=self.log_dir()),
                        start_method=start_method,
                    )

                    results = pc.wait(period=0.1)
                    self.assertEqual({0: None, 1: None}, results.return_values)

        @skip_but_pass_in_sandcastle_if(
            TEST_WITH_DEV_DBG_ASAN, "tests incompatible with asan"
        )
        def test_function_large_ret_val(self):
            # python multiprocessing.queue module uses pipes and actually PipedQueues
            # This means that if a single object is greater than a pipe size
            # the writer process will block until reader process will start
            # reading the pipe.
            # This test makes a worker fn to return huge output, around ~10 MB

            size = 200000
            for start_method in self._start_methods:
                with self.subTest(start_method=start_method):
                    pc = start_processes(
                        logs_specs=DefaultLogsSpecs(log_dir=self.log_dir()),
                        name="echo",
                        entrypoint=echo_large,
                        args={0: (size,), 1: (size,), 2: (size,), 3: (size,)},
                        envs={0: {}, 1: {}, 2: {}, 3: {}},
                        start_method=start_method,
                    )

                    results = pc.wait(period=0.1)
                    for i in range(pc.nprocs):
                        self.assertEqual(size, len(results.return_values[i]))

        def test_function_raise(self):
            """
            run 2x copies of echo2, raise an exception on the first
            """
            RAISE = True

            for start_method in self._start_methods:
                with self.subTest(start_method=start_method):
                    log_dir = self.log_dir()
                    pc = start_processes(
                        name="echo",
                        entrypoint=echo2,
                        args={0: ("hello", RAISE), 1: ("world",)},
                        envs={
                            0: {"TORCHELASTIC_RUN_ID": "run_id"},
                            1: {"TORCHELASTIC_RUN_ID": "run_id"},
                        },
                        logs_specs=DefaultLogsSpecs(log_dir=log_dir),
                        start_method=start_method,
                    )

                    results = pc.wait(period=0.1)

                    self.assert_pids_noexist(pc.pids())
                    self.assertEqual(1, len(results.failures))
                    self.assertFalse(results.return_values)

                    failure = results.failures[0]
                    error_file = failure.error_file
                    error_file_data = failure.error_file_data

                    self.assertEqual(1, failure.exitcode)
                    self.assertEqual("<N/A>", failure.signal_name())
                    self.assertEqual(pc.pids()[0], failure.pid)
                    self.assertTrue(
                        error_file.startswith(os.path.join(log_dir, "run_id_"))
                    )
                    self.assertTrue(error_file.endswith("attempt_0/0/error.json"))
                    self.assertEqual(
                        int(error_file_data["message"]["extraInfo"]["timestamp"]),
                        int(failure.timestamp),
                    )
                    self.assertTrue(pc._stderr_tail.stopped())
                    self.assertTrue(pc._stdout_tail.stopped())

        def test_wait_for_all_child_procs_to_exit(self):
            """
            Tests that MultiprocessingContext actually waits for
            the child process to exit (not just that the entrypoint fn has
            finished running).
            """

            mpc = MultiprocessContext(
                name="echo",
                entrypoint=echo0,
                args={},
                envs={},
                start_method="spawn",
                logs_specs=DefaultLogsSpecs(log_dir=self.log_dir()),
            )

            with mock.patch.object(
                mpc, "_is_done", return_value=True
            ), mock.patch.object(mpc, "_pc"), mock.patch.object(
                mpc._pc, "join", side_effect=[True, False, False, True]
            ) as mock_join:
                mpc._poll()
                self.assertEqual(4, mock_join.call_count)

        @skip_but_pass_in_sandcastle_if(
            NO_MULTIPROCESSING_SPAWN,
            "Disabled for environments that \
                        don't support multiprocessing with spawn start method",
        )
        def test_multiprocessing_context_poll_raises_exception(self):
            mp_context = MultiprocessContext(
                name="test_mp",
                entrypoint=echo0,
                args={0: (0, 1)},
                envs={0: {}},
                logs_specs=DefaultLogsSpecs(
                    log_dir=self.log_dir(), redirects=Std.ALL, tee=Std.ALL
                ),
                start_method="spawn",
            )
            mp_context._pc = mock.Mock()
            # Using mock since we cannot just set exitcode on process
            mock_process = mock.Mock()
            mock_process.exitcode = -1
            mp_context._pc.processes = [mock_process]
            e = mp.ProcessRaisedException(msg="test msg", error_index=0, error_pid=123)
            mp_context._pc.join.side_effect = e
            with mock.patch.object(mp_context, "close"):
                run_result = mp_context._poll()
                self.assertEqual(1, len(run_result.failures))
                failure = run_result.failures[0]
                self.assertEqual(
                    "Signal 1 (SIGHUP) received by PID 123", failure.message
                )

    class StartProcessesAsBinaryTest(_StartProcessesTest):
        ########################################
        # start_processes as binary tests
        ########################################

        def test_subprocess_context_close(self):
            pc = start_processes(
                name="sleep",
                entrypoint=bin("zombie_test.py"),
                args={0: (1,)},
                envs={0: {}},
                logs_specs=DefaultLogsSpecs(log_dir=self.log_dir()),
            )

            pids = pc.pids()
            pc.close()
            self.assert_pids_noexist(pids)

        def test_binary_exit(self):
            FAIL = 138
            pc = start_processes(
                name="echo",
                entrypoint=bin("echo1.py"),
                args={0: ("--exitcode", FAIL, "foo"), 1: ("--exitcode", 0, "bar")},
                envs={0: {"RANK": "0"}, 1: {"RANK": "1"}},
                logs_specs=DefaultLogsSpecs(
                    log_dir=self.log_dir(),
                    redirects={0: Std.ALL},
                ),
            )

            results = pc.wait(period=0.1)

            self.assertTrue(results.is_failed())
            self.assertEqual(1, len(results.failures))

            failure = results.failures[0]
            self.assertEqual(138, failure.exitcode)
            self.assertEqual("<N/A>", failure.signal_name())
            self.assertEqual("<NONE>", failure.error_file_data["message"])
            self.assert_in_file([f"exit {FAIL} from 0"], results.stderrs[0])
            self.assert_in_file([], results.stdouts[0])
            self.assertFalse(results.stderrs[1])
            self.assertFalse(results.stdouts[1])
            self.assertTrue(pc._stderr_tail.stopped())
            self.assertTrue(pc._stdout_tail.stopped())

        def test_binary_raises(self):
            pc = start_processes(
                name="echo",
                entrypoint=bin("echo2.py"),
                args={0: ("--raises", "true", "foo"), 1: ("bar",)},
                envs={0: {"RANK": "0"}, 1: {"RANK": "1"}},
                logs_specs=DefaultLogsSpecs(log_dir=self.log_dir()),
            )

            results = pc.wait(period=0.1)

            self.assert_pids_noexist(pc.pids())
            self.assertTrue(results.is_failed())
            self.assertEqual(1, len(results.failures))

            failure = results.failures[0]
            self.assertEqual(1, failure.exitcode)
            self.assertEqual("<NONE>", failure.error_file_data["message"])
            self.assertEqual("<N/A>", failure.signal_name())

        def test_binary_incorrect_entrypoint(self):
            with self.assertRaises(FileNotFoundError):
                start_processes(
                    name="echo",
                    entrypoint="does_not_exist.py",
                    args={0: ("foo"), 1: ("bar",)},
                    envs={0: {}, 1: {}},
                    logs_specs=DefaultLogsSpecs(log_dir=self.log_dir()),
                )

        def test_validate_full_rank(self):
            with self.assertRaises(RuntimeError):
                _validate_full_rank({}, 10, "")


# tests incompatible with tsan or asan, the redirect functionality does not work on macos or windows
if not (TEST_WITH_DEV_DBG_ASAN or IS_WINDOWS or IS_MACOS):

    class StartProcessesListAsFuncTest(_StartProcessesTest):
        def test_function(self):
            for start_method, redirs in product(
                self._start_methods, redirects_oss_test()
            ):
                with self.subTest(start_method=start_method, redirs=redirs):
                    pc = start_processes(
                        name="echo",
                        entrypoint=echo1,
                        args={0: ("hello",), 1: ("hello",)},
                        envs={0: {"RANK": "0"}, 1: {"RANK": "1"}},
                        logs_specs=DefaultLogsSpecs(
                            log_dir=self.log_dir(),
                            redirects=redirs,
                        ),
                        start_method=start_method,
                    )

                    results = pc.wait(period=0.1)
                    nprocs = pc.nprocs

                    self.assert_pids_noexist(pc.pids())
                    self.assertEqual(
                        {i: f"hello_{i}" for i in range(nprocs)}, results.return_values
                    )

                    for i in range(nprocs):
                        if redirs & Std.OUT != Std.OUT:
                            self.assertFalse(results.stdouts[i])
                        if redirs & Std.ERR != Std.ERR:
                            self.assertFalse(results.stderrs[i])
                        if redirs & Std.OUT == Std.OUT:
                            self.assert_in_file(
                                [f"hello stdout from {i}"], results.stdouts[i]
                            )
                        if redirs & Std.ERR == Std.ERR:
                            self.assert_in_file(
                                [f"hello stderr from {i}"], results.stderrs[i]
                            )

    class StartProcessesListAsBinaryTest(_StartProcessesTest):
        ########################################
        # start_processes as binary tests
        ########################################
        def test_binary(self):
            for redirs in redirects_oss_test():
                with self.subTest(redirs=redirs):
                    pc = start_processes(
                        name="echo",
                        entrypoint=bin("echo1.py"),
                        args={0: ("hello",), 1: ("hello",)},
                        envs={0: {"RANK": "0"}, 1: {"RANK": "1"}},
                        logs_specs=DefaultLogsSpecs(
                            log_dir=self.log_dir(),
                            redirects=redirs,
                        ),
                        log_line_prefixes={0: "[rank0]:", 1: "[rank1]:"},
                    )

                    results = pc.wait(period=0.1)

                    self.assert_pids_noexist(pc.pids())
                    # currently binaries return {rank: None}
                    self.assertEqual(2, len(results.return_values))
                    self.assertFalse(results.is_failed())

                    nprocs = pc.nprocs
                    for i in range(nprocs):
                        if redirs & Std.OUT != Std.OUT:
                            self.assertFalse(results.stdouts[i])
                        if redirs & Std.ERR != Std.ERR:
                            self.assertFalse(results.stderrs[i])
                        if redirs & Std.OUT == Std.OUT:
                            self.assert_in_file(
                                [f"hello stdout from {i}"], results.stdouts[i]
                            )
                        if redirs & Std.ERR == Std.ERR:
                            self.assert_in_file(
                                [f"hello stderr from {i}"], results.stderrs[i]
                            )

        def test_binary_redirect_and_tee(self):
            pc = start_processes(
                name="trainer",
                entrypoint=bin("echo1.py"),
                args={0: ("hello",), 1: ("world",)},
                envs={0: {"RANK": "0"}, 1: {"RANK": "1"}},
                logs_specs=DefaultLogsSpecs(
                    log_dir=self.log_dir(),
                    redirects={0: Std.ERR, 1: Std.NONE},
                    tee={0: Std.OUT, 1: Std.ERR},
                ),
                log_line_prefixes={0: "[rank0]:", 1: "[rank1]:"},
                start_method="spawn",
            )

            result = pc.wait()

            self.assertFalse(result.is_failed())
            self.assert_in_file(["hello stdout from 0"], pc.stdouts[0])
            self.assert_in_file(["hello stderr from 0"], pc.stderrs[0])
            self.assert_in_file(["world stderr from 1"], pc.stderrs[1])
            self.assertFalse(pc.stdouts[1])
            self.assertTrue(pc._stderr_tail.stopped())
            self.assertTrue(pc._stdout_tail.stopped())


# tests incompatible with tsan or asan, the redirect functionality does not work on macos or windows
if not (TEST_WITH_DEV_DBG_ASAN or IS_WINDOWS or IS_MACOS or IS_CI):

    class StartProcessesNotCIAsFuncTest(_StartProcessesTest):
        @skip_if_pytest
        def test_wrap_bad(self):
            none = ""
            stdout_log = os.path.join(self.test_dir, "stdout.log")
            stderr_log = os.path.join(self.test_dir, "stderr.log")
            redirs = [
                (none, none),
                (none, stderr_log),
                (stdout_log, none),
                (stdout_log, stderr_log),
            ]

            for stdout_redir, stderr_redir in redirs:
                queue = multiprocessing.SimpleQueue()
                worker_finished_event_mock = mock.Mock()
                _wrap(
                    local_rank=0,
                    fn=echo1,
                    args={0: ("hello",)},
                    envs={0: {"RANK": "0"}},
                    stdout_redirects={0: stdout_redir},
                    stderr_redirects={0: stderr_redir},
                    ret_vals={0: queue},
                    queue_finished_reading_event=worker_finished_event_mock,
                )
                self.assertEqual("hello_0", queue.get())
                if stdout_redir:
                    self.assert_in_file(["hello stdout from 0"], stdout_log)
                if stderr_redir:
                    self.assert_in_file(["hello stderr from 0"], stderr_log)
                worker_finished_event_mock.wait.assert_called_once()

        def test_function_redirect_and_tee(self):
            for start_method in self._start_methods:
                with self.subTest(start_method=start_method):
                    pc = start_processes(
                        name="trainer",
                        entrypoint=echo1,
                        args={0: ("hello",), 1: ("world",)},
                        envs={0: {"RANK": "0"}, 1: {"RANK": "1"}},
                        logs_specs=DefaultLogsSpecs(
                            log_dir=self.log_dir(),
                            redirects={0: Std.ERR, 1: Std.NONE},
                            tee={0: Std.OUT, 1: Std.ERR},
                        ),
                        start_method="spawn",
                    )

                    result = pc.wait()

                    self.assertFalse(result.is_failed())
                    self.assert_in_file(["hello stdout from 0"], pc.stdouts[0])
                    self.assert_in_file(["hello stderr from 0"], pc.stderrs[0])
                    self.assert_in_file(["world stderr from 1"], pc.stderrs[1])
                    self.assertFalse(pc.stdouts[1])
                    self.assertTrue(pc._stderr_tail.stopped())
                    self.assertTrue(pc._stdout_tail.stopped())

        def test_function(self):
            for start_method, redirs in product(self._start_methods, redirects_all()):
                with self.subTest(start_method=start_method, redirs=redirs):
                    pc = start_processes(
                        name="echo",
                        entrypoint=echo1,
                        args={0: ("hello",), 1: ("hello",)},
                        envs={0: {"RANK": "0"}, 1: {"RANK": "1"}},
                        start_method=start_method,
                        logs_specs=DefaultLogsSpecs(
                            log_dir=self.log_dir(),
                            redirects=redirs,
                        ),
                    )

                    results = pc.wait(period=0.1)
                    nprocs = pc.nprocs

                    self.assert_pids_noexist(pc.pids())
                    self.assertEqual(
                        {i: f"hello_{i}" for i in range(nprocs)}, results.return_values
                    )

                    for i in range(nprocs):
                        if redirs & Std.OUT != Std.OUT:
                            self.assertFalse(results.stdouts[i])
                        if redirs & Std.ERR != Std.ERR:
                            self.assertFalse(results.stderrs[i])
                        if redirs & Std.OUT == Std.OUT:
                            self.assert_in_file(
                                [f"hello stdout from {i}"], results.stdouts[i]
                            )
                        if redirs & Std.ERR == Std.ERR:
                            self.assert_in_file(
                                [f"hello stderr from {i}"], results.stderrs[i]
                            )

        def test_function_exit(self):
            """
            run 2x copies of echo1 fail (exit) the first
            functions that exit from python do not generate an error file
            (even if they are decorated with @record)
            """

            FAIL = 138
            for start_method in self._start_methods:
                with self.subTest(start_method=start_method):
                    pc = start_processes(
                        name="echo",
                        entrypoint=echo1,
                        args={0: ("hello", FAIL), 1: ("hello",)},
                        envs={0: {"RANK": "0"}, 1: {"RANK": "1"}},
                        logs_specs=DefaultLogsSpecs(
                            log_dir=self.log_dir(),
                            redirects={0: Std.ERR},
                        ),
                        start_method=start_method,
                    )

                    results = pc.wait(period=0.1)

                    self.assert_pids_noexist(pc.pids())
                    self.assertTrue(results.is_failed())
                    self.assertEqual(1, len(results.failures))
                    self.assertFalse(results.return_values)

                    failure = results.failures[0]
                    error_file = failure.error_file

                    self.assertEqual(FAIL, failure.exitcode)
                    self.assertEqual("<N/A>", failure.signal_name())
                    self.assertEqual(pc.pids()[0], failure.pid)
                    self.assertEqual("<N/A>", error_file)
                    self.assertEqual(
                        "To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html",
                        failure.message,
                    )
                    self.assertLessEqual(failure.timestamp, int(time.time()))

                    self.assert_in_file([f"exit {FAIL} from 0"], results.stderrs[0])
                    self.assertFalse(results.stdouts[0])
                    self.assertFalse(results.stderrs[1])
                    self.assertFalse(results.stdouts[1])
                    self.assertTrue(pc._stderr_tail.stopped())
                    self.assertTrue(pc._stdout_tail.stopped())

        def test_no_zombie_process_function(self):
            signals = [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]
            for s in signals:
                self._test_zombie_workflow(wait_fn, s)

    class StartProcessesNotCIAsBinaryTest(_StartProcessesTest):
        def test_binary_signal(self):
            pc = start_processes(
                name="echo",
                entrypoint=bin("echo3.py"),
                args={0: ("--segfault", "true", "foo"), 1: ("bar",)},
                envs={0: {"RANK": "0"}, 1: {"RANK": "1"}},
                logs_specs=DefaultLogsSpecs(
                    log_dir=self.log_dir(),
                ),
            )

            results = pc.wait(period=0.1)

            self.assert_pids_noexist(pc.pids())
            self.assertTrue(results.is_failed())
            self.assertEqual(1, len(results.failures))

            failure = results.failures[0]
            self.assertNotEqual(signal.SIGSEGV, failure.exitcode)
            if TEST_WITH_ASAN or TEST_WITH_TSAN:
                # ASAN/TSAN exit code is 1.
                self.assertEqual("<N/A>", failure.signal_name())
            else:
                self.assertEqual("SIGSEGV", failure.signal_name())
            self.assertEqual("<NONE>", failure.error_file_data["message"])

        def test_no_zombie_process_binary(self):
            signals = [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]
            for s in signals:
                self._test_zombie_workflow(bin("zombie_test.py"), s)

    class ForkServerTest(
        StartProcessesAsFuncTest,
        StartProcessesListAsFuncTest,
        StartProcessesNotCIAsFuncTest,
    ):
        def setUp(self):
            super().setUp()
            self._start_methods = ["forkserver"]
            self.orig_paralell_env_val = os.environ.get(mp.ENV_VAR_PARALLEL_START)
            os.environ[mp.ENV_VAR_PARALLEL_START] = "1"

        def tearDown(self):
            super().tearDown()
            if self.orig_paralell_env_val is None:
                del os.environ[mp.ENV_VAR_PARALLEL_START]
            else:
                os.environ[mp.ENV_VAR_PARALLEL_START] = self.orig_paralell_env_val


if __name__ == "__main__":
    run_tests()