-
Notifications
You must be signed in to change notification settings - Fork 534
/
bert_QDL.py
138 lines (119 loc) · 4.79 KB
/
bert_QDL.py
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
# Copyright 2023 MLCommons. 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.
# =============================================================================
from time import sleep
import squad_QSL
import mlperf_loadgen as lg
import numpy as np
import threading
import requests
import array
import time
import os
import sys
sys.path.insert(0, os.getcwd())
class bert_QDL:
"""QDL acting as a proxy to the SUT.
This QDL communicates with the SUT via HTTP.
It uses two endpoints to communicate with the SUT:
- /predict/ : Send a query to the SUT and get a response.
- /getname/ : Get the name of the SUT. Send a getname to the SUT and get a response.
"""
def __init__(self, qsl: squad_QSL.SQuAD_v1_QSL, sut_server_addr: list):
"""
Constructor for the QDL.
Args:
qsl: The QSL to use.
sut_server_addr: A list of addresses of the SUT.
"""
self.qsl = qsl
self.quantized = False
# Construct QDL from the python binding
self.qdl = lg.ConstructQDL(
self.issue_query, self.flush_queries, self.client_get_name
)
self.sut_server_addr = sut_server_addr
self.num_nodes = len(sut_server_addr)
# For round robin between the SUTs:
self.next_sut_id = 0
self.lock = threading.Lock()
def issue_query(self, query_samples):
"""Process the query to send to the SUT"""
threading.Thread(
target=self.process_query_async,
args=[query_samples]).start()
def flush_queries(self):
"""Flush the queries. Dummy implementation."""
pass
def process_query_async(self, query_samples):
"""
This function is called by the Loadgen in a separate thread.
It is responsible for
1. Creating a query for the SUT, by reading the features from the QSL.
2. Sending the query to the SUT.
3. Waiting for the response from the SUT.
4. Deserializing the response.
5. Calling mlperf_loadgen.QuerySamplesComplete(query_samples, response)
Args:
query_samples: A list of QuerySample objects.
"""
max_num_threads = int(
os.environ.get(
"CM_MAX_NUM_THREADS",
os.cpu_count()))
for i in range(len(query_samples)):
eval_features = self.qsl.get_features(query_samples[i].index)
encoded_eval_features = {
"input_ids": eval_features.input_ids,
"input_mask": eval_features.input_mask,
"segment_ids": eval_features.segment_ids,
}
n = threading.active_count()
while n >= max_num_threads:
sleep(0.0001)
n = threading.active_count()
threading.Thread(
target=self.client_predict_worker,
args=[encoded_eval_features, query_samples[i].id],
).start()
def get_sut_id_round_robin(self):
"""Get the SUT id in round robin."""
with self.lock:
res = self.next_sut_id
self.next_sut_id = (self.next_sut_id + 1) % self.num_nodes
return res
def client_predict_worker(self, query, query_id):
"""Serialize the query, send it to the SUT in round robin, and return the deserialized response."""
url = "{}/predict/".format(
self.sut_server_addr[self.get_sut_id_round_robin()])
responses = []
response = requests.post(url, json={"query": query})
output = response.json()["result"]
output = np.array(output).astype(np.float32)
response_array = array.array("B", output.tobytes())
bi = response_array.buffer_info()
responses.append(lg.QuerySampleResponse(query_id, bi[0], bi[1]))
lg.QuerySamplesComplete(responses)
def client_get_name(self):
"""Get the name of the SUT from ALL the SUTS."""
if len(self.sut_server_addr) == 1:
return requests.post(
f"{self.sut_server_addr[0]}/getname/").json()["name"]
sut_names = [
requests.post(f"{addr}/getname/").json()["name"]
for addr in self.sut_server_addr
]
return "Multi-node SUT: " + ", ".join(sut_names)
def __del__(self):
lg.DestroyQDL(self.qdl)