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

"""Tests for BenchmarkRegistry module."""

import re

8
from superbench.benchmarks import Platform, Framework, BenchmarkType, BenchmarkRegistry, ReturnCode
9
from superbench.benchmarks.micro_benchmarks import MicroBenchmark
10
from superbench.benchmarks.micro_benchmarks.sharding_matmul import ShardingMode
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30


class AccumulationBenchmark(MicroBenchmark):
    """Benchmark that do accumulation from lower_bound to upper_bound."""
    def __init__(self, name, parameters=''):
        """Constructor.

        Args:
            name: benchmark name.
            parameters: benchmark parameters.
        """
        super().__init__(name, parameters)

    def add_parser_arguments(self):
        """Add the specified arguments."""
        super().add_parser_arguments()

        self._parser.add_argument(
            '--lower_bound',
            type=int,
31
            required=True,
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
            help='The lower bound for accumulation.',
        )

        self._parser.add_argument(
            '--upper_bound',
            type=int,
            default=2,
            required=False,
            help='The upper bound for accumulation.',
        )

    def _benchmark(self):
        """Implementation for benchmarking."""
        raw_data = []
        result = 0
        for i in range(self._args.lower_bound, self._args.upper_bound):
            result += i
            raw_data.append(str(result))

        metric = 'accumulation_result'
52
        self._result.add_raw_data(metric, ','.join(raw_data), self._args.log_raw_data)
53
54
55
56
57
58
59
60
61
62
        self._result.add_result(metric, result)

        return True


def test_register_benchmark():
    """Test interface BenchmarkRegistry.register_benchmark()."""
    # Register the benchmark for all platform if use default platform.
    BenchmarkRegistry.register_benchmark('accumulation', AccumulationBenchmark)
    for platform in Platform:
63
        context = BenchmarkRegistry.create_benchmark_context('accumulation', platform=platform)
64
65
66
67
        assert (BenchmarkRegistry.is_benchmark_registered(context))

    # Register the benchmark for CUDA platform if use platform=Platform.CUDA.
    BenchmarkRegistry.register_benchmark('accumulation-cuda', AccumulationBenchmark, platform=Platform.CUDA)
68
    context = BenchmarkRegistry.create_benchmark_context('accumulation-cuda', platform=Platform.CUDA)
69
    assert (BenchmarkRegistry.is_benchmark_registered(context))
70
    context = BenchmarkRegistry.create_benchmark_context('accumulation-cuda', platform=Platform.ROCM)
71
72
73
74
75
76
    assert (BenchmarkRegistry.is_benchmark_registered(context) is False)


def test_is_benchmark_context_valid():
    """Test interface BenchmarkRegistry.is_benchmark_context_valid()."""
    # Positive case.
77
    context = BenchmarkRegistry.create_benchmark_context('accumulation', platform=Platform.CPU)
78
79
80
81
82
83
84
85
86
87
88
89
    assert (BenchmarkRegistry.is_benchmark_context_valid(context))

    # Negative case.
    context = 'context'
    assert (BenchmarkRegistry.is_benchmark_context_valid(context) is False)
    context = None
    assert (BenchmarkRegistry.is_benchmark_context_valid(context) is False)


def test_get_benchmark_name():
    """Test interface BenchmarkRegistry.get_benchmark_name()."""
    # Register benchmarks for testing.
90
    benchmark_names = ['accumulation', 'pytorch-accumulation', 'tf1-accumulation', 'onnxruntime-accumulation']
91
92
93
94
    for name in benchmark_names:
        BenchmarkRegistry.register_benchmark(name, AccumulationBenchmark)

    # Test benchmark name for different Frameworks.
95
    benchmark_frameworks = [Framework.NONE, Framework.PYTORCH, Framework.TENSORFLOW1, Framework.ONNXRUNTIME]
96
    for i in range(len(benchmark_names)):
97
98
99
        context = BenchmarkRegistry.create_benchmark_context(
            'accumulation', platform=Platform.CPU, framework=benchmark_frameworks[i]
        )
100
101
102
103
104
105
106
107
108
109
110
111
        name = BenchmarkRegistry._BenchmarkRegistry__get_benchmark_name(context)
        assert (name == benchmark_names[i])


def test_get_benchmark_configurable_settings():
    """Test BenchmarkRegistry interface.

    BenchmarkRegistry.get_benchmark_configurable_settings().
    """
    # Register benchmarks for testing.
    BenchmarkRegistry.register_benchmark('accumulation', AccumulationBenchmark)

112
    context = BenchmarkRegistry.create_benchmark_context('accumulation', platform=Platform.CPU)
113
114
115
116
    settings = BenchmarkRegistry.get_benchmark_configurable_settings(context)

    expected = """optional arguments:
  --duration int     The elapsed time of benchmark in seconds.
117
118
  --log_raw_data     Log raw data into file instead of saving it into result
                     object.
119
  --lower_bound int  The lower bound for accumulation.
120
  --run_count int    The run count of benchmark.
121
122
123
124
125
126
127
128
  --upper_bound int  The upper bound for accumulation."""
    assert (settings == expected)


def test_launch_benchmark():
    """Test interface BenchmarkRegistry.launch_benchmark()."""
    # Register benchmarks for testing.
    BenchmarkRegistry.register_benchmark(
129
        'accumulation', AccumulationBenchmark, parameters='--upper_bound 5', platform=Platform.CPU
130
131
132
    )

    # Launch benchmark.
133
    context = BenchmarkRegistry.create_benchmark_context(
134
        'accumulation', platform=Platform.CPU, parameters='--lower_bound 1'
135
136
137
138
139
140
141
142
143
    )

    benchmark = BenchmarkRegistry.launch_benchmark(context)
    assert (benchmark)
    assert (benchmark.name == 'accumulation')
    assert (benchmark.type == BenchmarkType.MICRO)
    assert (benchmark.run_count == 1)
    assert (benchmark.return_code == ReturnCode.SUCCESS)
    assert (benchmark.raw_data == {'accumulation_result': ['1,3,6,10']})
144
    assert (benchmark.result == {'return_code': [0], 'accumulation_result': [10]})
145
146
147
148
149
150
151

    # Replace the timestamp as null.
    result = re.sub(r'\"\d+-\d+-\d+ \d+:\d+:\d+\"', 'null', benchmark.serialized_result)
    expected = (
        '{"name": "accumulation", "type": "micro", "run_count": 1, '
        '"return_code": 0, "start_time": null, "end_time": null, '
        '"raw_data": {"accumulation_result": ["1,3,6,10"]}, '
152
        '"result": {"return_code": [0], "accumulation_result": [10]}, '
153
        '"reduce_op": {"return_code": null, "accumulation_result": null}}'
154
155
    )
    assert (result == expected)
156
157

    # Launch benchmark with overridden parameters.
158
    context = BenchmarkRegistry.create_benchmark_context(
159
        'accumulation', platform=Platform.CPU, parameters='--lower_bound 1 --upper_bound 4'
160
161
162
163
164
165
166
167
    )
    benchmark = BenchmarkRegistry.launch_benchmark(context)
    assert (benchmark)
    assert (benchmark.name == 'accumulation')
    assert (benchmark.type == BenchmarkType.MICRO)
    assert (benchmark.run_count == 1)
    assert (benchmark.return_code == ReturnCode.SUCCESS)
    assert (benchmark.raw_data == {'accumulation_result': ['1,3,6']})
168
    assert (benchmark.result == {'return_code': [0], 'accumulation_result': [6]})
169
170
171
172
173
174
175

    # Replace the timestamp as null.
    result = re.sub(r'\"\d+-\d+-\d+ \d+:\d+:\d+\"', 'null', benchmark.serialized_result)
    expected = (
        '{"name": "accumulation", "type": "micro", "run_count": 1, '
        '"return_code": 0, "start_time": null, "end_time": null, '
        '"raw_data": {"accumulation_result": ["1,3,6"]}, '
176
        '"result": {"return_code": [0], "accumulation_result": [6]}, '
177
        '"reduce_op": {"return_code": null, "accumulation_result": null}}'
178
179
    )
    assert (result == expected)
180

181
    # Failed to launch benchmark due to 'benchmark not found'.
182
    context = BenchmarkRegistry.create_benchmark_context(
183
        'accumulation-fail', Platform.CPU, parameters='--lower_bound 1 --upper_bound 4', framework=Framework.PYTORCH
184
    )
185
186
187
188
189
    benchmark = BenchmarkRegistry.launch_benchmark(context)
    assert (benchmark is None)

    # Failed to launch benchmark due to 'unknown arguments'.
    context = BenchmarkRegistry.create_benchmark_context(
190
        'accumulation', platform=Platform.CPU, parameters='--lower_bound 1 --test 4'
191
192
193
194
195
196
197
    )
    benchmark = BenchmarkRegistry.launch_benchmark(context)
    assert (benchmark)
    assert (benchmark.return_code == ReturnCode.INVALID_ARGUMENT)

    # Failed to launch benchmark due to 'invalid arguments'.
    context = BenchmarkRegistry.create_benchmark_context(
198
        'accumulation', platform=Platform.CPU, parameters='--lower_bound 1 --upper_bound x'
199
200
201
202
    )
    benchmark = BenchmarkRegistry.launch_benchmark(context)
    assert (benchmark)
    assert (benchmark.return_code == ReturnCode.INVALID_ARGUMENT)
203
204
205
206
207
208
209
210
211
212
213


def test_get_all_benchmark_predefine_settings():
    """Test interface BenchmarkRegistry.get_all_benchmark_predefine_settings()."""
    benchmark_params = BenchmarkRegistry.get_all_benchmark_predefine_settings()

    # Choose benchmark 'pytorch-sharding-matmul' for testing.
    benchmark_name = 'pytorch-sharding-matmul'
    assert (benchmark_name in benchmark_params)
    assert (benchmark_params[benchmark_name]['run_count'] == 1)
    assert (benchmark_params[benchmark_name]['duration'] == 0)
214
215
216
    assert (benchmark_params[benchmark_name]['n'] == 12288)
    assert (benchmark_params[benchmark_name]['k'] == 12288)
    assert (benchmark_params[benchmark_name]['m'] == 16000)
217
218
219
    assert (benchmark_params[benchmark_name]['mode'] == [ShardingMode.ALLREDUCE, ShardingMode.ALLGATHER])
    assert (benchmark_params[benchmark_name]['num_warmup'] == 10)
    assert (benchmark_params[benchmark_name]['num_steps'] == 500)