test_gpu_profiler.py 6.96 KB
Newer Older
1
2
3
4
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest

5
from vllm.config import ProfilerConfig
6
from vllm.config.profiler import _is_uri_path
7
from vllm.profiler.wrapper import WorkerProfiler
8
9
10
11
12
13
14


class ConcreteWorkerProfiler(WorkerProfiler):
    """
    A basic implementation of a worker profiler for testing purposes.
    """

15
    def __init__(self, profiler_config: ProfilerConfig):
16
17
18
        self.start_call_count = 0
        self.stop_call_count = 0
        self.should_fail_start = False
19
        super().__init__(profiler_config)
20
21
22
23
24
25
26
27
28
29

    def _start(self) -> None:
        if self.should_fail_start:
            raise RuntimeError("Simulated start failure")
        self.start_call_count += 1

    def _stop(self) -> None:
        self.stop_call_count += 1


30
31
32
33
34
35
36
37
@pytest.fixture
def default_profiler_config():
    return ProfilerConfig(
        profiler="torch",
        torch_profiler_dir="/tmp/mock",
        delay_iterations=0,
        max_iterations=0,
    )
38
39


40
def test_immediate_start_stop(default_profiler_config):
41
    """Test standard start without delay."""
42
    profiler = ConcreteWorkerProfiler(default_profiler_config)
43
44
45
46
47
48
49
50
51
52
53
    profiler.start()
    assert profiler._running is True
    assert profiler._active is True
    assert profiler.start_call_count == 1

    profiler.stop()
    assert profiler._running is False
    assert profiler._active is False
    assert profiler.stop_call_count == 1


54
def test_delayed_start(default_profiler_config):
55
    """Test that profiler waits for N steps before actually starting."""
56
57
    default_profiler_config.delay_iterations = 2
    profiler = ConcreteWorkerProfiler(default_profiler_config)
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

    # User requests start
    profiler.start()

    # Should be active (request accepted) but not running (waiting for delay)
    assert profiler._active is True
    assert profiler._running is False
    assert profiler.start_call_count == 0

    # Step 1
    profiler.step()
    assert profiler._running is False

    # Step 2 (Threshold reached)
    profiler.step()
    assert profiler._running is True
    assert profiler.start_call_count == 1


77
def test_max_iterations(default_profiler_config):
78
    """Test that profiler stops automatically after max iterations."""
79
80
    default_profiler_config.max_iterations = 2
    profiler = ConcreteWorkerProfiler(default_profiler_config)
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

    profiler.start()
    assert profiler._running is True

    # Iteration 1
    profiler.step()  # profiling_count becomes 1
    assert profiler._running is True

    # Iteration 2
    profiler.step()  # profiling_count becomes 2
    assert profiler._running is True

    # Iteration 3 (Exceeds max)
    profiler.step()  # profiling_count becomes 3

    # Should have stopped now
    assert profiler._running is False
    assert profiler.stop_call_count == 1


101
def test_delayed_start_and_max_iters(default_profiler_config):
102
    """Test combined delayed start and max iterations."""
103
104
105
    default_profiler_config.delay_iterations = 2
    default_profiler_config.max_iterations = 2
    profiler = ConcreteWorkerProfiler(default_profiler_config)
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
    profiler.start()

    # Step 1
    profiler.step()
    assert profiler._running is False
    assert profiler._active is True

    # Step 2 (Starts now)
    profiler.step()
    assert profiler._profiling_for_iters == 1
    assert profiler._running is True
    assert profiler._active is True

    # Next iteration
    profiler.step()
    assert profiler._profiling_for_iters == 2
    assert profiler._running is True

    # Iteration 2 (exceeds max)
    profiler.step()

    # Should have stopped now
    assert profiler._running is False
    assert profiler.stop_call_count == 1


132
def test_idempotency(default_profiler_config):
133
    """Test that calling start/stop multiple times doesn't break logic."""
134
    profiler = ConcreteWorkerProfiler(default_profiler_config)
135
136
137
138
139
140
141
142
143
144
145
146

    # Double Start
    profiler.start()
    profiler.start()
    assert profiler.start_call_count == 1  # Should only start once

    # Double Stop
    profiler.stop()
    profiler.stop()
    assert profiler.stop_call_count == 1  # Should only stop once


147
def test_step_inactive(default_profiler_config):
148
    """Test that stepping while inactive does nothing."""
149
150
    default_profiler_config.delay_iterations = 2
    profiler = ConcreteWorkerProfiler(default_profiler_config)
151
152
153
154
155
156
157
158
159

    # Not started yet
    profiler.step()
    profiler.step()

    # Even though we stepped 2 times, start shouldn't happen because active=False
    assert profiler.start_call_count == 0


160
def test_start_failure(default_profiler_config):
161
    """Test behavior when the underlying _start method raises exception."""
162
    profiler = ConcreteWorkerProfiler(default_profiler_config)
163
164
165
166
167
168
169
170
171
172
    profiler.should_fail_start = True

    profiler.start()

    # Exception caught in _call_start
    assert profiler._running is False  # Should not mark as running
    assert profiler._active is True  # Request is still considered active
    assert profiler.start_call_count == 0  # Logic failed inside start


173
def test_shutdown(default_profiler_config):
174
    """Test that shutdown calls stop only if running."""
175
    profiler = ConcreteWorkerProfiler(default_profiler_config)
176
177
178
179
180
181
182
183
184
185
186

    # Case 1: Not running
    profiler.shutdown()
    assert profiler.stop_call_count == 0

    # Case 2: Running
    profiler.start()
    profiler.shutdown()
    assert profiler.stop_call_count == 1


187
def test_mixed_delay_and_stop(default_profiler_config):
188
    """Test manual stop during the delay period."""
189
190
    default_profiler_config.delay_iterations = 5
    profiler = ConcreteWorkerProfiler(default_profiler_config)
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

    profiler.start()
    profiler.step()
    profiler.step()

    # User cancels before delay finishes
    profiler.stop()
    assert profiler._active is False

    # Further steps should not trigger start
    profiler.step()
    profiler.step()
    profiler.step()

    assert profiler.start_call_count == 0
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


class TestIsUriPath:
    """Tests for the _is_uri_path helper function."""

    @pytest.mark.parametrize(
        "path,expected",
        [
            # Valid URI schemes - should return True
            ("gs://bucket/path", True),
            ("s3://bucket/path", True),
            ("hdfs://cluster/path", True),
            ("abfs://container/path", True),
            ("http://example.com/path", True),
            ("https://example.com/path", True),
            # Local paths - should return False
            ("/tmp/local/path", False),
            ("./relative/path", False),
            ("relative/path", False),
            ("/absolute/path", False),
            # Windows drive letters - should return False (single char scheme)
            ("C://windows/path", False),
            ("D://drive/path", False),
            # Edge cases
            ("", False),
            ("no-scheme", False),
            ("scheme-no-slashes:", False),
            ("://no-scheme", False),
        ],
    )
    def test_is_uri_path(self, path, expected):
        """Test that _is_uri_path correctly identifies URI vs local paths."""
        assert _is_uri_path(path) == expected