|
| 1 | +# Copyright 2019-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +from __future__ import absolute_import |
| 14 | + |
| 15 | +import os |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | +import time |
| 19 | + |
| 20 | +import pytest |
| 21 | +import requests |
| 22 | +import torch |
| 23 | +from concurrent.futures import ThreadPoolExecutor |
| 24 | +import csv |
| 25 | + |
| 26 | +from integration import model_gpu_context_dir |
| 27 | + |
| 28 | +BASE_URL = "http://0.0.0.0:8080/" |
| 29 | +PING_URL = BASE_URL + "ping" |
| 30 | +INVOCATION_URL = BASE_URL + "models/model/invoke" |
| 31 | +GPU_COUNT = torch.cuda.device_count() |
| 32 | +DEVICE_IDS_EXPECTED = [i for i in range(GPU_COUNT)] |
| 33 | + |
| 34 | + |
| 35 | +def send_request(input_data, headers): |
| 36 | + requests.post(INVOCATION_URL, data=input_data, headers=headers) |
| 37 | + |
| 38 | + |
| 39 | +def read_csv(filename): |
| 40 | + data = {} |
| 41 | + with open(os.path.join(model_gpu_context_dir, 'code', filename), 'r') as csv_file: |
| 42 | + csv_reader = csv.reader(csv_file) |
| 43 | + for row in csv_reader: |
| 44 | + device_id, pid, threadid = row |
| 45 | + if device_id in data: |
| 46 | + continue |
| 47 | + data[int(device_id)] = {'pid': pid, 'threadid': threadid} |
| 48 | + return data |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture(scope="module", autouse=True) |
| 52 | +def container(image_uri): |
| 53 | + try: |
| 54 | + if 'cpu' in image_uri: |
| 55 | + pytest.skip("Skipping because tests running on CPU instance") |
| 56 | + |
| 57 | + command = ( |
| 58 | + "docker run --gpus=all -p 8080:8080 " |
| 59 | + "--name sagemaker-pytorch-inference-toolkit-context-test " |
| 60 | + "-v {}:/opt/ml/model " |
| 61 | + "{} serve" |
| 62 | + ).format(model_gpu_context_dir, image_uri) |
| 63 | + |
| 64 | + proc = subprocess.Popen(command.split(), stdout=sys.stdout, stderr=subprocess.STDOUT) |
| 65 | + |
| 66 | + attempts = 0 |
| 67 | + while attempts < 10: |
| 68 | + time.sleep(3) |
| 69 | + try: |
| 70 | + requests.get(PING_URL) |
| 71 | + break |
| 72 | + except Exception: |
| 73 | + attempts += 1 |
| 74 | + pass |
| 75 | + time.sleep(60) |
| 76 | + yield proc.pid |
| 77 | + |
| 78 | + finally: |
| 79 | + if 'cpu' in image_uri: |
| 80 | + pytest.skip("Skipping because tests running on CPU instance") |
| 81 | + subprocess.check_call("docker rm -f sagemaker-pytorch-inference-toolkit-context-test".split()) |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture(scope="module", autouse=True) |
| 85 | +def inference_requests(): |
| 86 | + headers = {"Content-Type": "application/json"} |
| 87 | + with ThreadPoolExecutor(max_workers=GPU_COUNT) as executor: |
| 88 | + for i in range(32): |
| 89 | + executor.submit(send_request, b'input', headers) |
| 90 | + time.sleep(60) |
| 91 | + yield |
| 92 | + |
| 93 | + |
| 94 | +@pytest.fixture(scope="module", name="model_fn_device_info") |
| 95 | +def model_fn_device_info(): |
| 96 | + return read_csv("model_fn_device_info.csv") |
| 97 | + |
| 98 | + |
| 99 | +@pytest.fixture(scope="module", name="input_fn_device_info") |
| 100 | +def input_fn_device_info(): |
| 101 | + return read_csv("input_fn_device_info.csv") |
| 102 | + |
| 103 | + |
| 104 | +@pytest.fixture(scope="module", name="predict_fn_device_info") |
| 105 | +def predict_fn_device_info(): |
| 106 | + return read_csv("predict_fn_device_info.csv") |
| 107 | + |
| 108 | + |
| 109 | +@pytest.fixture(scope="module", name="output_fn_device_info") |
| 110 | +def output_fn_device_info(): |
| 111 | + return read_csv("output_fn_device_info.csv") |
| 112 | + |
| 113 | + |
| 114 | +def test_context_all_device_ids( |
| 115 | + model_fn_device_info, input_fn_device_info, predict_fn_device_info, output_fn_device_info |
| 116 | +): |
| 117 | + for device_id in DEVICE_IDS_EXPECTED: |
| 118 | + assert device_id in model_fn_device_info |
| 119 | + assert device_id in input_fn_device_info |
| 120 | + assert device_id in predict_fn_device_info |
| 121 | + assert device_id in output_fn_device_info |
| 122 | + |
| 123 | + |
| 124 | +def test_same_pid_threadid( |
| 125 | + model_fn_device_info, input_fn_device_info, predict_fn_device_info, output_fn_device_info |
| 126 | +): |
| 127 | + for device_id in DEVICE_IDS_EXPECTED: |
| 128 | + pid_model_fn = model_fn_device_info[device_id]['pid'] |
| 129 | + threadid_model_fn = model_fn_device_info[device_id]['threadid'] |
| 130 | + |
| 131 | + pid_input_fn = input_fn_device_info[device_id]['pid'] |
| 132 | + threadid_input_fn = input_fn_device_info[device_id]['threadid'] |
| 133 | + |
| 134 | + pid_predict_fn = predict_fn_device_info[device_id]['pid'] |
| 135 | + threadid_predict_fn = predict_fn_device_info[device_id]['threadid'] |
| 136 | + |
| 137 | + pid_output_fn = output_fn_device_info[device_id]['pid'] |
| 138 | + threadid_output_fn = output_fn_device_info[device_id]['threadid'] |
| 139 | + |
| 140 | + assert pid_model_fn == pid_input_fn == pid_output_fn == pid_predict_fn |
| 141 | + assert threadid_model_fn == threadid_input_fn == threadid_output_fn == threadid_predict_fn |
0 commit comments