20
20
import pytest
21
21
import requests
22
22
import torch
23
+ from concurrent .futures import ThreadPoolExecutor
24
+ import csv
23
25
24
26
from integration import model_gpu_context_dir
25
27
26
28
BASE_URL = "http://0.0.0.0:8080/"
27
29
PING_URL = BASE_URL + "ping"
30
+ INVOCATION_URL = BASE_URL + "models/model/invoke"
31
+ GPU_COUNT = torch .cuda .device_count ()
32
+ GPU_IDS_EXPECTED = [i for i in range (GPU_COUNT )]
28
33
29
34
30
35
@pytest .fixture (scope = "module" , autouse = True )
@@ -34,7 +39,7 @@ def container(image_uri):
34
39
pytest .skip ("Skipping because tests running on CPU instance" )
35
40
36
41
command = (
37
- "docker run --gpus=all "
42
+ "docker run --gpus=all -p 8080:8080 "
38
43
"--name sagemaker-pytorch-inference-toolkit-context-test "
39
44
"-v {}:/opt/ml/model "
40
45
"{} serve"
@@ -60,16 +65,53 @@ def container(image_uri):
60
65
61
66
62
67
def test_context_all_device_ids ():
63
- gpu_count = torch . cuda . device_count ( )
68
+ time . sleep ( 5 )
64
69
65
- gpu_ids_expected = [i for i in range (gpu_count )]
66
70
gpu_ids_actual = []
67
-
68
- with open (os .path .join (model_gpu_context_dir , 'code' , 'device_info.txt' ), 'r' ) as f :
71
+ with open (os .path .join (model_gpu_context_dir , 'code' , 'model_fn_device_info.txt' ), 'r' ) as f :
69
72
for line in f :
70
73
gpu_ids_actual .append (int (line ))
71
74
72
75
gpu_ids_actual = list (set (gpu_ids_actual ))
73
76
gpu_ids_actual .sort ()
74
77
75
- assert gpu_ids_actual == gpu_ids_expected
78
+ assert gpu_ids_actual == GPU_IDS_EXPECTED
79
+
80
+
81
+ def test_same_pid_threadid ():
82
+ time .sleep (5 )
83
+ headers = {"Content-Type" : "application/json" }
84
+ with ThreadPoolExecutor (max_workers = GPU_COUNT ) as executor :
85
+ for i in range (GPU_COUNT ):
86
+ executor .submit (send_request , b'input' , headers )
87
+
88
+ input_fn_device_info = read_csv ("input_fn_device_info.csv" )
89
+ output_fn_device_info = read_csv ("output_fn_device_info.csv" )
90
+ predict_fn_device_info = read_csv ("predict_fn_device_info.csv" )
91
+
92
+ assert len (input_fn_device_info ) == len (output_fn_device_info ) == len (predict_fn_device_info )
93
+
94
+ for input_fn_row , output_fn_row , predict_fn_row in zip (
95
+ input_fn_device_info , output_fn_device_info , predict_fn_device_info
96
+ ):
97
+
98
+ device_id_input_fn , pid_input_fn , threadid_input_fn = input_fn_row
99
+ device_id_output_fn , pid_output_fn , threadid_output_fn = output_fn_row
100
+ device_id_predict_fn , pid_predict_fn , threadid_predict_fn = predict_fn_row
101
+
102
+ assert device_id_input_fn == device_id_output_fn == device_id_predict_fn
103
+ assert pid_input_fn == pid_output_fn == pid_predict_fn
104
+ assert threadid_input_fn == threadid_output_fn == threadid_predict_fn
105
+
106
+
107
+ def send_request (input_data , headers ):
108
+ requests .post (INVOCATION_URL , data = input_data , headers = headers )
109
+
110
+
111
+ def read_csv (filename ):
112
+ data = []
113
+ with open (os .path .join (model_gpu_context_dir , 'code' , filename ), 'r' ) as csv_file :
114
+ csv_reader = csv .reader (csv_file )
115
+ for row in csv_reader :
116
+ data .append (row )
117
+ return data
0 commit comments