Spaces:
Running
on
Zero
Running
on
Zero
File size: 21,546 Bytes
7931de6 |
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 82 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 135 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 223 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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import time
from threading import Event, Thread
import gevent
import numpy as np
import pytest
from gevent.hub import Hub as GeventHub
from pytriton.client import FuturesModelClient, ModelClient
from pytriton.client.exceptions import (
PyTritonClientClosedError,
PyTritonClientInvalidUrlError,
PyTritonClientQueueFullError,
PyTritonClientTimeoutError,
PyTritonClientValueError,
)
from pytriton.model_config import DeviceKind
from pytriton.model_config.triton_model_config import TensorSpec, TritonModelConfig
from .client_common import (
ADD_SUB_WITH_BATCHING_MODEL_CONFIG,
GRPC_LOCALHOST_URL,
HTTP_LOCALHOST_URL,
patch_server_model_addsub_no_batch_ready,
)
from .utils import (
patch_grpc_client__model_up_and_ready,
patch_grpc_client__server_up_and_ready,
patch_http_client__model_up_and_ready,
patch_http_client__server_up_and_ready,
)
logging.basicConfig(level=logging.DEBUG)
LOGGER = logging.getLogger("test_sync_client")
ADD_SUB_WITHOUT_BATCHING_MODEL_CONFIG = TritonModelConfig(
model_name="AddSub",
model_version=1,
batching=False,
instance_group={DeviceKind.KIND_CPU: 1},
inputs=[
TensorSpec(name="a", shape=(1,), dtype=np.float32),
TensorSpec(name="b", shape=(1,), dtype=np.float32),
],
outputs=[
TensorSpec(name="add", shape=(1,), dtype=np.float32),
TensorSpec(name="sub", shape=(1,), dtype=np.float32),
],
backend_parameters={"shared-memory-socket": "dummy/path"},
)
logging.basicConfig(level=logging.DEBUG)
LOGGER = logging.getLogger("test_sync_client")
def test_wait_for_model_raise_error_when_invalid_url_provided():
with pytest.raises(PyTritonClientInvalidUrlError, match="Invalid url"):
with FuturesModelClient(["localhost:8001"], "dummy") as client: # pytype: disable=wrong-arg-types
client.wait_for_model(timeout_s=0.1).result()
@patch_server_model_addsub_no_batch_ready
def test_wait_for_model_passes_timeout_to_client(mocker):
spy_client_close = mocker.spy(ModelClient, ModelClient.close.__name__)
mock_client_wait_for_model = mocker.patch.object(ModelClient, ModelClient.wait_for_model.__name__)
mock_client_wait_for_model.return_value = True
spy_thread_start = mocker.spy(Thread, Thread.start.__name__)
spy_thread_join = mocker.spy(Thread, Thread.join.__name__)
spy_get_hub = mocker.spy(gevent, gevent.get_hub.__name__)
spy_hub_destroy = mocker.spy(GeventHub, GeventHub.destroy.__name__)
with FuturesModelClient(
GRPC_LOCALHOST_URL,
ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name,
str(ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_version),
max_workers=1,
) as client:
future = client.wait_for_model(15)
result = future.result()
assert result is True
spy_client_close.assert_called_once()
mock_client_wait_for_model.assert_called_with(15)
spy_thread_start.assert_called_once()
spy_thread_join.assert_called_once()
spy_get_hub.assert_called_once()
spy_hub_destroy.assert_called_once()
@patch_server_model_addsub_no_batch_ready
def test_infer_raises_error_when_mixed_args_convention_used(mocker):
a = np.array([1], dtype=np.float32)
b = np.array([1], dtype=np.float32)
init_t_timeout_s = 15.0
with FuturesModelClient(
GRPC_LOCALHOST_URL, ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name, init_timeout_s=init_t_timeout_s
) as client:
with pytest.raises(
PyTritonClientValueError,
match="Use either positional either keyword method arguments convention",
):
client.infer_sample(a, b=b).result()
with FuturesModelClient(GRPC_LOCALHOST_URL, ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name) as client:
with pytest.raises(
PyTritonClientValueError,
match="Use either positional either keyword method arguments convention",
):
client.infer_batch(a, b=b).result()
@patch_server_model_addsub_no_batch_ready
def test_infer_sample_returns_values_creates_client(mocker):
a = np.array([1], dtype=np.float32)
b = np.array([2], dtype=np.float32)
c = np.array([3], dtype=np.float32)
init_t_timeout_s = 15.0
mock_client_wait_for_model = mocker.patch.object(ModelClient, ModelClient._wait_and_init_model_config.__name__)
mock_client_infer_sample = mocker.patch.object(ModelClient, ModelClient.infer_sample.__name__)
mock_client_infer_sample.return_value = c
with FuturesModelClient(
GRPC_LOCALHOST_URL, ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name, init_timeout_s=init_t_timeout_s
) as client:
result = client.infer_sample(a=a, b=b).result()
mock_client_wait_for_model.assert_called_once_with(init_t_timeout_s)
mock_client_infer_sample.assert_called_once_with(parameters=None, headers=None, a=a, b=b)
# Check the Python version and use different assertions for cancel_futures
assert result == c
@patch_server_model_addsub_no_batch_ready
def test_infer_sample_returns_values_creates_client_close_wait(mocker):
a = np.array([1], dtype=np.float32)
b = np.array([2], dtype=np.float32)
c = np.array([3], dtype=np.float32)
mock_client_infer_sample = mocker.patch.object(ModelClient, ModelClient.infer_sample.__name__)
# Prevent exit from closing the client
mocker.patch.object(FuturesModelClient, FuturesModelClient.__exit__.__name__)
mock_client_infer_sample.return_value = c
client = FuturesModelClient(GRPC_LOCALHOST_URL, ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name)
result = client.infer_sample(a, b).result()
client.close(wait=True)
mock_client_infer_sample.assert_called_once_with(a, b, parameters=None, headers=None)
assert result == c
@patch_server_model_addsub_no_batch_ready
def test_infer_batch_returns_values_creates_client(mocker):
a = np.array([1], dtype=np.float32)
b = np.array([2], dtype=np.float32)
c = np.array([3], dtype=np.float32)
init_t_timeout_s = 15.0
mock_client_infer_batch = mocker.patch.object(ModelClient, ModelClient.infer_batch.__name__)
mock_client_infer_batch.return_value = c
with FuturesModelClient(
GRPC_LOCALHOST_URL, ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name, init_timeout_s=init_t_timeout_s
) as client:
result = client.infer_batch(a=a, b=b).result()
model_config = client.model_config().result()
mock_client_infer_batch.assert_called_once_with(parameters=None, headers=None, a=a, b=b)
assert model_config.model_name == ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name
assert result == c
@patch_server_model_addsub_no_batch_ready
def test_infer_sample_list_passed_arguments_returns_arguments(mocker):
a = np.array([1], dtype=np.float32)
b = np.array([2], dtype=np.float32)
ret = np.array([3], dtype=np.float32)
patch_client_infer_sample = mocker.patch.object(ModelClient, ModelClient.infer_sample.__name__)
patch_client_infer_sample.return_value = ret
with FuturesModelClient(GRPC_LOCALHOST_URL, ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name) as client:
return_value = client.infer_sample(a, b).result()
assert return_value == ret
patch_client_infer_sample.assert_called_once_with(a, b, parameters=None, headers=None)
@patch_server_model_addsub_no_batch_ready
def test_infer_sample_dict_passed_arguments_returns_arguments(mocker):
a = np.array([1], dtype=np.float32)
b = np.array([2], dtype=np.float32)
ret = np.array([3], dtype=np.float32)
patch_client_infer_sample = mocker.patch.object(ModelClient, ModelClient.infer_sample.__name__)
patch_client_infer_sample.return_value = ret
with FuturesModelClient(GRPC_LOCALHOST_URL, ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name) as client:
return_value = client.infer_sample(a=a, b=b).result()
assert return_value == ret
patch_client_infer_sample.assert_called_once_with(a=a, b=b, parameters=None, headers=None)
@patch_server_model_addsub_no_batch_ready
def test_infer_batch_list_passed_arguments_returns_arguments(mocker):
a = np.array([1], dtype=np.float32)
b = np.array([2], dtype=np.float32)
ret = np.array([3], dtype=np.float32)
patch_client_infer_batch = mocker.patch.object(ModelClient, ModelClient.infer_batch.__name__)
patch_client_infer_batch.return_value = ret
with FuturesModelClient(GRPC_LOCALHOST_URL, ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name) as client:
return_value = client.infer_batch(a, b).result()
assert return_value == ret
patch_client_infer_batch.assert_called_once_with(a, b, parameters=None, headers=None)
@patch_server_model_addsub_no_batch_ready
def test_infer_batch_dict_passed_arguments_returns_arguments(mocker):
a = np.array([1], dtype=np.float32)
b = np.array([2], dtype=np.float32)
ret = np.array([3], dtype=np.float32)
patch_client_infer_batch = mocker.patch.object(ModelClient, ModelClient.infer_batch.__name__)
patch_client_infer_batch.return_value = ret
with FuturesModelClient(GRPC_LOCALHOST_URL, ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name) as client:
return_value = client.infer_batch(a=a, b=b).result()
assert return_value == ret
patch_client_infer_batch.assert_called_once_with(parameters=None, headers=None, a=a, b=b)
@patch_server_model_addsub_no_batch_ready
def test_infer_batch_blocking_behaviour(mocker):
a = np.array([1], dtype=np.float32)
b = np.array([2], dtype=np.float32)
c = np.array([2], dtype=np.float32)
ret = np.array([3], dtype=np.float32)
# Set up the queue return values to block the queue and then release it
infer_called_with_b_event = Event()
infer_called_with_c_event = Event()
queue_is_full_event = Event()
def mock_submit_side_effect(*args, **kwargs):
LOGGER.debug("mock_submit_side_effect called")
assert "b" in kwargs
if kwargs["b"] is b:
infer_called_with_b_event.set()
elif kwargs["b"] is c:
infer_called_with_c_event.set()
if not queue_is_full_event.is_set():
LOGGER.debug("mock_submit_side_effect waiting for queue to be full")
queue_is_full_event.wait() # Block until the event is set
LOGGER.debug("mock_submit_side_effect returning")
return ret
patch_client_infer_batch = mocker.patch.object(ModelClient, ModelClient.infer_batch.__name__)
patch_client_infer_batch.side_effect = mock_submit_side_effect
# Set up the client with a max_queue_size of 1 to easily simulate full condition
with FuturesModelClient(
GRPC_LOCALHOST_URL,
ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name,
max_workers=1,
max_queue_size=1,
non_blocking=False,
) as client:
client.model_config().result() # Wait for the model to be ready
LOGGER.debug("Client created")
first_future = client.infer_batch(a=a, b=b)
LOGGER.debug("First future created")
infer_called_with_b_event.wait() # Wait for the first call to be made
LOGGER.debug("First call made")
blocked_thread_start_event = Event()
blocked_thread_result = {}
def blocked_thread():
LOGGER.debug("Blocked thread started")
blocked_thread_start_event.set()
LOGGER.debug("Blocked thread waiting for queue to be full")
result = client.infer_batch(a=a, b=c).result()
LOGGER.debug("Blocked thread got result")
blocked_thread_result["ret"] = result
infer_thread = Thread(target=blocked_thread)
infer_thread.start()
LOGGER.debug("Waiting for blocked thread to start")
blocked_thread_start_event.wait() # Wait for the thread to start
LOGGER.debug("Blocked thread started")
time.sleep(0.1) # Wait a bit to make sure the thread is blocked
assert not infer_called_with_c_event.is_set(), "infer_batch should not have been called with c yet."
# The blocking call should be waiting by now, so let's release the block
LOGGER.debug("Releasing queue")
queue_is_full_event.set()
# Wait for the blocked thread to finish
LOGGER.debug("Waiting for blocked thread to finish")
infer_thread.join()
assert blocked_thread_result["ret"] is ret
# Wait for the first future to finish
assert first_future.result() is ret
assert (
patch_client_infer_batch.call_count == 2
), "infer_batch should have been called twice (one blocked, one released)."
@patch_server_model_addsub_no_batch_ready
def test_infer_batch_non_blocking_behaviour(mocker):
a = np.array([1], dtype=np.float32)
b = np.array([2], dtype=np.float32)
c = np.array([2], dtype=np.float32)
ret = np.array([3], dtype=np.float32)
# Set up the queue return values to block the queue and then release it
infer_called_with_b_event = Event()
queue_is_full_event = Event()
def mock_submit_side_effect(*args, **kwargs):
LOGGER.debug("mock_submit_side_effect called")
infer_called_with_b_event.set()
if not queue_is_full_event.is_set():
LOGGER.debug("mock_submit_side_effect waiting for queue to be full")
queue_is_full_event.wait() # Block until the event is set
LOGGER.debug("mock_submit_side_effect returning")
return ret
patch_client_infer_batch = mocker.patch.object(ModelClient, ModelClient.infer_batch.__name__)
patch_client_infer_batch.side_effect = mock_submit_side_effect
# Set up the client with a max_queue_size of 1 to easily simulate full condition
with FuturesModelClient(
GRPC_LOCALHOST_URL,
ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name,
max_workers=1,
max_queue_size=1,
non_blocking=True,
) as client:
LOGGER.debug("Client created")
while True:
try:
client.model_config().result() # Wait for the model to be ready
break
except PyTritonClientQueueFullError:
LOGGER.debug("Waiting for model to be ready")
time.sleep(0.1)
pass
first_future = client.infer_batch(a=a, b=b)
LOGGER.debug("First future created")
infer_called_with_b_event.wait() # Wait for the first call to be made
LOGGER.debug("First call made")
second_future = client.infer_batch(a=a, b=c)
LOGGER.debug("Second future created")
with pytest.raises(PyTritonClientQueueFullError):
LOGGER.debug("Calling infer_batch with queue full")
client.infer_batch(a=a, b=c)
# The blocking call should be waiting by now, so let's release the block
LOGGER.debug("Releasing queue")
queue_is_full_event.set()
# Wait for the first future to finish
assert first_future.result() is ret
assert second_future.result() is ret
assert patch_client_infer_batch.call_count == 2, "infer_batch should have been called once."
@patch_server_model_addsub_no_batch_ready
def test_infer_batch_queue_timeout(mocker):
a = np.array([1], dtype=np.float32)
b = np.array([2], dtype=np.float32)
c = np.array([2], dtype=np.float32)
ret = np.array([3], dtype=np.float32)
# Set up the queue return values to block the queue and then release it
infer_called_with_b_event = Event()
queue_is_full_event = Event()
def mock_submit_side_effect(*args, **kwargs):
LOGGER.debug("mock_submit_side_effect called")
infer_called_with_b_event.set()
if not queue_is_full_event.is_set():
LOGGER.debug("mock_submit_side_effect waiting for queue to be full")
queue_is_full_event.wait() # Block until the event is set
LOGGER.debug("mock_submit_side_effect returning")
return ret
patch_client_infer_batch = mocker.patch.object(ModelClient, ModelClient.infer_batch.__name__)
patch_client_infer_batch.side_effect = mock_submit_side_effect
# Set up the client with a max_queue_size of 1 to easily simulate full condition
with FuturesModelClient(
GRPC_LOCALHOST_URL,
ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name,
max_workers=1,
max_queue_size=1,
inference_timeout_s=0.1,
) as client:
LOGGER.debug("Client created")
client.model_config().result() # Wait for the model to be ready
first_future = client.infer_batch(a=a, b=b)
LOGGER.debug("First future created")
infer_called_with_b_event.wait() # Wait for the first call to be made
LOGGER.debug("First call made")
second_future = client.infer_batch(a=a, b=c)
LOGGER.debug("Second future created")
with pytest.raises(PyTritonClientQueueFullError):
LOGGER.debug("Calling infer_batch with queue full")
client.infer_batch(a=a, b=c)
# The blocking call should be waiting by now, so let's release the block
LOGGER.debug("Releasing queue")
queue_is_full_event.set()
# Wait for the first future to finish
assert first_future.result() is ret
assert second_future.result() is ret
assert patch_client_infer_batch.call_count == 2, "infer_batch should have been called once."
def test_init_raises_error_when_invalid_max_workers_provided(mocker):
with pytest.raises(ValueError):
with FuturesModelClient(GRPC_LOCALHOST_URL, ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name, max_workers=-1):
pass
def test_init_raises_error_when_invalid_max_queue_size_provided(mocker):
with pytest.raises(ValueError):
with FuturesModelClient(GRPC_LOCALHOST_URL, ADD_SUB_WITH_BATCHING_MODEL_CONFIG.model_name, max_queue_size=-1):
pass
@pytest.mark.timeout(1.0)
def test_init_http_passes_timeout(mocker):
with FuturesModelClient("http://localhost:6669", "dummy", init_timeout_s=0.2, inference_timeout_s=0.1) as client:
with pytest.raises(PyTritonClientTimeoutError):
client.wait_for_model(timeout_s=0.2).result()
@pytest.mark.timeout(5)
def test_init_grpc_passes_timeout_5(mocker):
with FuturesModelClient("grpc://localhost:6669", "dummy", init_timeout_s=0.2, inference_timeout_s=0.1) as client:
with pytest.raises(PyTritonClientTimeoutError):
client.wait_for_model(timeout_s=0.2).result()
@pytest.mark.timeout(5)
def test_init_http_spaws_several_threads(mocker):
spy_thread_start = mocker.spy(Thread, Thread.start.__name__)
with FuturesModelClient("http://localhost:6669", "dummy", init_timeout_s=1, inference_timeout_s=0.2) as client:
timeout_s = 0.2
# The list function is used to force the evaluation of the list comprehension before iterating over the futures and
# calling their result method. This is done to ensure that all the calls occur before the iteration starts,
# and to verify that five threads are created.
futures = list([client.wait_for_model(timeout_s=timeout_s) for _ in range(5)]) # noqa: C411
for future in futures:
with pytest.raises(PyTritonClientTimeoutError):
future.result()
# Reusing client configuration from existing clients forces wait in other threads to finish first configuration
# request. It sometimes prevents creation of a fifth thread because one of the existing threads can handle another request
# before the new thread is created. This results in a race condition that affects the number of created threads.
assert spy_thread_start.call_count > 1
def test_http_client_raises_error_when_used_after_close(mocker):
patch_http_client__server_up_and_ready(mocker)
patch_http_client__model_up_and_ready(mocker, ADD_SUB_WITH_BATCHING_MODEL_CONFIG)
with ModelClient(HTTP_LOCALHOST_URL, "dummy") as client:
pass
with pytest.raises(PyTritonClientClosedError):
client.wait_for_model(timeout_s=0.2)
a = np.array([1], dtype=np.float32)
with pytest.raises(PyTritonClientClosedError):
client.infer_sample(a=a)
with pytest.raises(PyTritonClientClosedError):
client.infer_batch(a=[a])
def test_grpc_client_raises_error_when_used_after_close(mocker):
patch_grpc_client__server_up_and_ready(mocker)
patch_grpc_client__model_up_and_ready(mocker, ADD_SUB_WITH_BATCHING_MODEL_CONFIG)
with FuturesModelClient(GRPC_LOCALHOST_URL, "dummy") as client:
pass
with pytest.raises(PyTritonClientClosedError):
client.wait_for_model(timeout_s=0.2).result()
a = np.array([1], dtype=np.float32)
with pytest.raises(PyTritonClientClosedError):
client.infer_sample(a=a).result()
with pytest.raises(PyTritonClientClosedError):
client.infer_batch(a=[a]).result()
|