test_nvbandwidth.py 7.74 KB
Newer Older
1
2
3
4
5
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""Tests for nvbandwidth benchmark."""

6
7
import os

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import unittest

from tests.helper import decorator
from tests.helper.testcase import BenchmarkTestCase
from superbench.benchmarks import BenchmarkRegistry, ReturnCode, Platform


class TestNvBandwidthBenchmark(BenchmarkTestCase, unittest.TestCase):
    """Test class for NV Bandwidth benchmark."""
    @classmethod
    def setUpClass(cls):
        """Hook method for setting up class fixture before running tests in the class."""
        super().setUpClass()
        cls.createMockEnvs(cls)
        cls.createMockFiles(cls, ['bin/nvbandwidth'])

    def test_nvbandwidth_preprocess(self):
        """Test NV Bandwidth benchmark preprocess."""
        benchmark_name = 'nvbandwidth'
27
        (benchmark_class, _) = BenchmarkRegistry._BenchmarkRegistry__select_benchmark(benchmark_name, Platform.CUDA)
28
29
30
31
32
33
34
35
36
37
        assert (benchmark_class)

        # Test preprocess with default parameters
        benchmark = benchmark_class(benchmark_name, parameters='')
        assert benchmark._preprocess()
        assert benchmark.return_code == ReturnCode.SUCCESS

        # Test preprocess with specified parameters
        parameters = (
            '--buffer_size 256 '
38
            '--test_cases host_to_device_memcpy_ce device_to_host_bidirectional_memcpy_ce '
39
40
41
42
43
44
45
46
47
48
49
50
            '--skip_verification '
            '--disable_affinity '
            '--use_mean '
            '--num_loops 100'
        )
        benchmark = benchmark_class(benchmark_name, parameters=parameters)
        assert benchmark._preprocess()
        assert benchmark.return_code == ReturnCode.SUCCESS

        # Check command
        assert (1 == len(benchmark._commands))
        assert ('--bufferSize 256' in benchmark._commands[0])
51
        assert ('--testcase host_to_device_memcpy_ce device_to_host_bidirectional_memcpy_ce' in benchmark._commands[0])
52
53
54
55
56
        assert ('--skipVerification' in benchmark._commands[0])
        assert ('--disableAffinity' in benchmark._commands[0])
        assert ('--useMean' in benchmark._commands[0])
        assert ('--testSamples 100' in benchmark._commands[0])

57
58
59
60
61
62
63
64
        # Test preprocess with numa
        os.environ['NUMA_NODES'] = '0'
        os.environ['PROC_RANK'] = '0'
        benchmark = benchmark_class(benchmark_name, parameters=parameters)
        assert benchmark._preprocess()
        assert benchmark.return_code == ReturnCode.SUCCESS
        assert ('numactl --cpunodebind=0 --membind=0' in benchmark._commands[0])

65
66
67
68
    @decorator.load_data('tests/data/nvbandwidth_results.log')
    def test_nvbandwidth_result_parsing_real_output(self, results):
        """Test NV Bandwidth benchmark result parsing."""
        benchmark_name = 'nvbandwidth'
69
        (benchmark_class, _) = BenchmarkRegistry._BenchmarkRegistry__select_benchmark(benchmark_name, Platform.CUDA)
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        assert (benchmark_class)

        benchmark = benchmark_class(benchmark_name, parameters='')

        # Preprocess and validate command
        assert benchmark._preprocess()

        # Parse the provided raw output
        assert benchmark._process_raw_result(0, results)
        assert benchmark.return_code == ReturnCode.SUCCESS

        # Validate parsed results
        assert benchmark.result['host_to_device_memcpy_ce_cpu0_gpu0_bw'][0] == 369.36
        assert benchmark.result['host_to_device_memcpy_ce_cpu0_gpu1_bw'][0] == 269.33
        assert benchmark.result['host_to_device_memcpy_ce_sum_bw'][0] == 1985.60
        assert benchmark.result['device_to_host_memcpy_ce_cpu0_gpu1_bw'][0] == 312.11
        assert benchmark.result['device_to_host_memcpy_ce_sum_bw'][0] == 607.26
        assert benchmark.result['host_device_latency_sm_cpu0_gpu0_lat'][0] == 772.58
        assert benchmark.result['host_device_latency_sm_sum_lat'][0] == 772.58
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

    def test_nvbandwidth_process_raw_result_unsupported_testcases(self):
        """Test NV Bandwidth benchmark result parsing with unsupported test cases."""
        benchmark_name = 'nvbandwidth'
        (benchmark_class, _) = BenchmarkRegistry._BenchmarkRegistry__select_benchmark(benchmark_name, Platform.CUDA)
        assert (benchmark_class)

        benchmark = benchmark_class(benchmark_name, parameters='')

        # Preprocess and validate command
        assert benchmark._preprocess()

        # Mock raw output with unsupported test cases
        raw_output = """
        ERROR: Testcase unsupported_testcase_1 not found!
        ERROR: Testcase unsupported_testcase_2 not found!
        """

        # Parse the provided raw output
        assert not benchmark._process_raw_result(0, raw_output)

        # Validate unsupported test cases
        assert 'unsupported_testcase_1' in benchmark._result.raw_data
        assert benchmark._result.raw_data['unsupported_testcase_1'][0] == 'Not supported'
        assert 'unsupported_testcase_2' in benchmark._result.raw_data
        assert benchmark._result.raw_data['unsupported_testcase_1'][0] == 'Not supported'

    def test_nvbandwidth_process_raw_result_waived_testcases(self):
        """Test NV Bandwidth benchmark result parsing with waived test cases."""
        benchmark_name = 'nvbandwidth'
        (benchmark_class, _) = BenchmarkRegistry._BenchmarkRegistry__select_benchmark(benchmark_name, Platform.CUDA)
        assert (benchmark_class)

        benchmark = benchmark_class(benchmark_name, parameters='')

        # Preprocess and validate command
        assert benchmark._preprocess()

        # Mock raw output with no executed test cases
        raw_output = """
        """

        # Set test cases to include some that will be waived
        benchmark._args.test_cases = ['waived_testcase_1', 'waived_testcase_2']

        # Parse the provided raw output
        assert not benchmark._process_raw_result(0, raw_output)

        # Validate waived test cases
        assert 'waived_testcase_1' in benchmark._result.raw_data
        assert benchmark._result.raw_data['waived_testcase_1'][0] == 'waived'
        assert 'waived_testcase_2' in benchmark._result.raw_data
        assert benchmark._result.raw_data['waived_testcase_2'][0] == 'waived'

    def test_get_all_test_cases(self):
        """Test _get_all_test_cases method."""
        benchmark_name = 'nvbandwidth'
        (benchmark_class, _) = BenchmarkRegistry._BenchmarkRegistry__select_benchmark(benchmark_name, Platform.CUDA)
        assert (benchmark_class)

        benchmark = benchmark_class(benchmark_name, parameters='')

151
152
153
        # Call preprocess to initialize _args
        assert benchmark._preprocess()

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
        # Mock subprocess.run for successful execution with valid output
        with unittest.mock.patch('subprocess.run') as mock_run:
            mock_run.return_value.returncode = 0
            mock_run.return_value.stdout = (
                '1, host_to_device_memcpy_ce:\n'
                '2, device_to_host_bidirectional_memcpy_ce:'
            )
            mock_run.return_value.stderr = ''
            test_cases = benchmark._get_all_test_cases()
            assert test_cases == ['host_to_device_memcpy_ce', 'device_to_host_bidirectional_memcpy_ce']

        # Mock subprocess.run for execution with non-zero return code
        with unittest.mock.patch('subprocess.run') as mock_run:
            mock_run.return_value.returncode = 1
            mock_run.return_value.stdout = ''
            mock_run.return_value.stderr = 'Error'
            test_cases = benchmark._get_all_test_cases()
            assert test_cases == []

        # Mock subprocess.run for execution with error message in stderr
        with unittest.mock.patch('subprocess.run') as mock_run:
            mock_run.return_value.returncode = 0
            mock_run.return_value.stdout = ''
            mock_run.return_value.stderr = 'Error'
            test_cases = benchmark._get_all_test_cases()
            assert test_cases == []