test_runner.py 5.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""SuperBench Runner test."""

import unittest
import shutil
import tempfile
from pathlib import Path
10
from unittest import mock
11

12
import yaml
13
14
15
16
17
18
19
20
21
22
from omegaconf import OmegaConf

from superbench.runner import SuperBenchRunner


class RunnerTestCase(unittest.TestCase):
    """A class for runner test cases."""
    def setUp(self):
        """Hook method for setting up the test fixture before exercising it."""
        default_config_file = Path(__file__).parent / '../../superbench/config/default.yaml'
23
24
        with default_config_file.open() as fp:
            self.default_config = OmegaConf.create(yaml.load(fp, Loader=yaml.SafeLoader))
25
        self.sb_output_dir = tempfile.mkdtemp()
26

27
        self.runner = SuperBenchRunner(self.default_config, None, None, self.sb_output_dir)
28
29
30

    def tearDown(self):
        """Hook method for deconstructing the test fixture after testing it."""
31
        shutil.rmtree(self.sb_output_dir)
32
33
34

    def test_set_logger(self):
        """Test log file exists."""
35
        expected_log_file = Path(self.runner._sb_output_dir) / 'sb-run.log'
36
37
        self.assertTrue(expected_log_file.is_file())

38
39
40
41
    def test_get_mode_command(self):
        """Test __get_mode_command."""
        test_cases = [
            {
42
                'benchmark_name': 'foo',
43
44
45
                'mode': {
                    'name': 'non_exist',
                },
46
47
                'expected_command':
                f'sb exec --output-dir {self.sb_output_dir} -c sb.config.yaml -C superbench.enable=foo',
48
49
            },
            {
50
51
                'benchmark_name':
                'foo',
52
                'mode': {
53
54
                    'name': 'local',
                    'proc_num': 1,
55
                    'proc_rank': 0,
56
                    'prefix': '',
57
                },
58
                'expected_command':
59
                f'PROC_RANK=0 sb exec --output-dir {self.sb_output_dir} -c sb.config.yaml -C superbench.enable=foo',
60
61
62
63
64
65
66
67
68
69
            },
            {
                'benchmark_name':
                'foo',
                'mode': {
                    'name': 'local',
                    'proc_num': 8,
                    'proc_rank': 6,
                    'prefix': 'CUDA_VISIBLE_DEVICES={proc_rank} numactl -c $(({proc_rank}/2))'
                },
70
                'expected_command': (
71
                    'PROC_RANK=6 CUDA_VISIBLE_DEVICES=6 numactl -c $((6/2)) '
72
73
                    f'sb exec --output-dir {self.sb_output_dir} -c sb.config.yaml -C superbench.enable=foo'
                ),
74
75
            },
            {
76
77
                'benchmark_name':
                'foo',
78
79
80
81
82
83
                'mode': {
                    'name': 'local',
                    'proc_num': 16,
                    'proc_rank': 1,
                    'prefix': 'RANK={proc_rank} NUM={proc_num}'
                },
84
85
86
87
                'expected_command': (
                    'PROC_RANK=1 RANK=1 NUM=16 '
                    f'sb exec --output-dir {self.sb_output_dir} -c sb.config.yaml -C superbench.enable=foo'
                ),
88
89
            },
            {
90
91
                'benchmark_name':
                'foo',
92
93
94
95
96
97
98
99
100
101
                'mode': {
                    'name': 'torch.distributed',
                    'proc_num': 1,
                    'node_num': 'all',
                },
                'expected_command': (
                    'python3 -m torch.distributed.launch '
                    '--use_env --no_python --nproc_per_node=1 '
                    '--nnodes=$NNODES --node_rank=$NODE_RANK '
                    '--master_addr=$MASTER_ADDR --master_port=$MASTER_PORT '
102
                    f'sb exec --output-dir {self.sb_output_dir} -c sb.config.yaml -C superbench.enable=foo '
103
104
                    'superbench.benchmarks.foo.parameters.distributed_impl=ddp '
                    'superbench.benchmarks.foo.parameters.distributed_backend=nccl'
105
106
107
                ),
            },
            {
108
109
                'benchmark_name':
                'foo',
110
111
112
113
114
115
116
117
118
119
                'mode': {
                    'name': 'torch.distributed',
                    'proc_num': 8,
                    'node_num': 1,
                },
                'expected_command': (
                    'python3 -m torch.distributed.launch '
                    '--use_env --no_python --nproc_per_node=8 '
                    '--nnodes=1 --node_rank=$NODE_RANK '
                    '--master_addr=$MASTER_ADDR --master_port=$MASTER_PORT '
120
                    f'sb exec --output-dir {self.sb_output_dir} -c sb.config.yaml -C superbench.enable=foo '
121
122
                    'superbench.benchmarks.foo.parameters.distributed_impl=ddp '
                    'superbench.benchmarks.foo.parameters.distributed_backend=nccl'
123
124
125
126
127
128
129
                ),
            },
        ]
        for test_case in test_cases:
            with self.subTest(msg='Testing with case', test_case=test_case):
                self.assertEqual(
                    self.runner._SuperBenchRunner__get_mode_command(
130
                        test_case['benchmark_name'], OmegaConf.create(test_case['mode'])
131
132
133
                    ), test_case['expected_command']
                )

134
135
    def test_run_empty_benchmarks(self):
        """Test run empty benchmarks, nothing should happen."""
136
        self.runner._sb_enabled_benchmarks = []
137
        self.runner.run()
138
139
140
141
142
143
144
145
146
147

    @mock.patch('superbench.runner.ansible.AnsibleClient.run')
    def test_run_default_benchmarks(self, mock_ansible_client_run):
        """Test run default benchmarks, mock AnsibleClient.run function.

        Args:
            mock_ansible_client_run (function): Mocked AnsibleClient.run function.
        """
        mock_ansible_client_run.return_value = 0
        self.runner.run()