test_perf_benchmark.py 8.08 KB
Newer Older
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
# Copyright 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#  * Neither the name of NVIDIA CORPORATION nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


import asyncio

import numpy
import pytest
import ucp
33

34
35
36
from triton_distributed.icp.nats_request_plane import NatsRequestPlane
from triton_distributed.icp.ucp_data_plane import UcpDataPlane
from triton_distributed.worker.deployment import Deployment
37
from triton_distributed.worker.logger import get_logger
38
39
40
41
42
43
44
45
46
from triton_distributed.worker.operator import OperatorConfig
from triton_distributed.worker.remote_operator import RemoteOperator
from triton_distributed.worker.worker import WorkerConfig

NATS_PORT = 4223
MODEL_REPOSITORY = (
    "/workspace/worker/tests/python/integration/operators/triton_core_models"
)
OPERATORS_REPOSITORY = "/workspace/worker/tests/python/integration/operators"
47
TRITON_LOG_LEVEL = 0
48

49
logger = get_logger(__name__)
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

# TODO
# Decide if this should be
# pre merge, nightly, or weekly
pytestmark = pytest.mark.pre_merge


@pytest.fixture
def workers(log_dir, request, number_workers=1):
    store_outputs_in_response = request.getfixturevalue("store_outputs_in_response")

    # Add configs for identity operator
    operator_name = "identity"
    operator_config = OperatorConfig(
        name=operator_name,
        implementation="identity:Identity",
        version=1,
        max_inflight_requests=10,
        parameters={"store_outputs_in_response": store_outputs_in_response},
        repository=OPERATORS_REPOSITORY,
    )

    worker_configs = []

    test_log_dir = log_dir / request.node.name
    test_log_dir.mkdir(parents=True, exist_ok=True)

    for i in range(number_workers):
        # Set the logging directory
        worker_log_dir = test_log_dir / (operator_name + "_" + str(i))
        worker_configs.append(
            WorkerConfig(
82
                name=operator_name,
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
                request_plane=NatsRequestPlane,
                data_plane=UcpDataPlane,
                request_plane_args=(
                    [],
                    {"request_plane_uri": f"nats://localhost:{NATS_PORT}"},
                ),
                log_level=TRITON_LOG_LEVEL,
                log_dir=str(worker_log_dir),
                operators=[operator_config],
            )
        )

    worker_deployment = Deployment(worker_configs)

    worker_deployment.start()
    yield worker_deployment
    worker_deployment.shutdown()


def _create_inputs(number, tensor_size_in_kb):
    inputs = []
    outputs = []

    elem_cnt = int(tensor_size_in_kb * 1024 / 4)
    for _ in range(number):
        input_ = numpy.random.randint(low=1, high=100, size=[elem_cnt])

        expected_ = {}

        expected_["output"] = input_

        inputs.append(input_)
        outputs.append(expected_)
    return inputs, outputs


def run(
    aio_benchmark,
    store_inputs_in_request,
    store_outputs_in_response,
    tensor_size_in_kb,
    data_plane_tracker,
):
    if data_plane_tracker.is_first_run:
        ucp.reset()
        data_plane_tracker._data_plane = UcpDataPlane()
        data_plane_tracker._data_plane.connect()

    request_plane = NatsRequestPlane(f"nats://localhost:{NATS_PORT}")
    asyncio.get_event_loop().run_until_complete(request_plane.connect())

    identity_operator = RemoteOperator(
Neelay Shah's avatar
Neelay Shah committed
135
        "identity", request_plane, data_plane_tracker._data_plane
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
    )

    inputs, outputs = _create_inputs(1, tensor_size_in_kb)

    aio_benchmark(
        post_requests,
        identity_operator,
        inputs,
        outputs,
        store_inputs_in_request,
        store_outputs_in_response,
    )

    timeout = 5
    asyncio.get_event_loop().run_until_complete(request_plane.close())

    if data_plane_tracker.is_last_run:
        data_plane_tracker._data_plane.close(timeout)


async def post_requests(
    identity_model, inputs, outputs, store_inputs_in_request, store_outputs_in_response
):
    results = []
    expected_results = {}

    for i, input_ in enumerate(inputs):
        request_id = str(i)
        request = identity_model.create_request(
            inputs={"input": input_}, request_id=request_id
        )
        if store_inputs_in_request:
            request.store_inputs_in_request.add("input")
        results.append(identity_model.async_infer(request))
        expected_results[request_id] = outputs[i]

    for result in asyncio.as_completed(results):
        responses = await result
        async for response in responses:
            for output_name, expected_value in expected_results[
                response.request_id
            ].items():
                output = response.outputs[output_name]
                _ = numpy.from_dlpack(output.to_host())

                del output

            del response


@pytest.fixture(scope="module")
def data_plane_tracker():
    class Tracker:
        def __init__(self):
            self.total_runs = 0
            self.current_run = 0
            self._data_plane = None

        def increment_run(self):
            self.current_run += 1

        @property
        def is_first_run(self):
            return self.current_run == 1

        @property
        def is_last_run(self):
            return self.current_run == self.total_runs

    return Tracker()


# FIXME: NATS default size limit is 1 MB. However, even when the tensor_size_in_kb
# is set as 600, which corresponds to 0.6144 MB, we are hiting MaxPayloadError.
# Need to investigate why the limit is being hit.
@pytest.mark.skipif(
    "(not os.path.exists('/usr/local/bin/nats-server'))",
    reason="NATS.io not present or test is configured to run with mock disaggregated_serving",
)
@pytest.mark.parametrize(
    ["store_inputs_in_request", "store_outputs_in_response"],
    [(True, True), (False, False)],
)
@pytest.mark.parametrize(
    "tensor_size_in_kb",
    [10, 100, 500],
)
223
@pytest.mark.benchmark(min_rounds=100, max_time=1)
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
def test_identity(
    request,
    nats_server,
    workers,
    aio_benchmark,
    store_inputs_in_request,
    store_outputs_in_response,
    tensor_size_in_kb,
    data_plane_tracker,
):
    """
    This benchmark test checks the latency of a simple operator which returns input in its output
    without any processing.
    NOTE: We can not use benchmark fixture in the child process. Hence, we are required to use the
    same process for opening then data plane object as pytest.
    This means that the pytest main process cannot create another data plane object in any other
    tests. Hence, we will use a run tracker to open and close the data plane
    """
    if data_plane_tracker.total_runs == 0:
        data_plane_tracker.total_runs = 6  # Set this to the number of parameters
    data_plane_tracker.increment_run()
    run(
        aio_benchmark,
        store_inputs_in_request,
        store_outputs_in_response,
        tensor_size_in_kb,
        data_plane_tracker,
    )