forked from microsoft/DeepSpeed-MII
-
Notifications
You must be signed in to change notification settings - Fork 0
/
method_table.py
277 lines (213 loc) · 9.73 KB
/
method_table.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
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
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import uuid
from abc import ABC, abstractmethod
from transformers import Conversation
from mii.legacy.constants import TaskType
from mii.legacy.grpc_related.proto import legacymodelresponse_pb2 as modelresponse_pb2
from mii.legacy.utils import kwarg_dict_to_proto, unpack_proto_query_kwargs
from mii.legacy.models.utils import ImageResponse
def single_string_request_to_proto(self, request_dict, **query_kwargs):
return modelresponse_pb2.SingleStringRequest(
request=request_dict["query"],
query_kwargs=kwarg_dict_to_proto(query_kwargs))
def single_string_response_to_proto(self, response, time_taken, model_time_taken):
return modelresponse_pb2.SingleStringReply(response=f"{response}",
time_taken=time_taken,
model_time_taken=model_time_taken)
def multi_string_request_to_proto(self, request_dict, **query_kwargs):
return modelresponse_pb2.MultiStringRequest(
request=request_dict["query"] if isinstance(request_dict["query"],
list) else [request_dict["query"]],
query_kwargs=kwarg_dict_to_proto(query_kwargs),
)
def proto_request_to_single_input(self, request):
args = (request.request, )
kwargs = unpack_proto_query_kwargs(request.query_kwargs)
return args, kwargs
def proto_request_to_list(self, request):
args = ([r for r in request.request], )
kwargs = unpack_proto_query_kwargs(request.query_kwargs)
return args, kwargs
class TaskMethods(ABC):
@property
@abstractmethod
def method(self):
...
def pack_request_to_proto(self, request_dict, **query_kwargs):
return request_dict, query_kwargs
def unpack_request_from_proto(self, request):
return request
def run_inference(self, inference_pipeline, args, kwargs):
return inference_pipeline(*args, **kwargs)
def pack_response_to_proto(self, response, time_taken, model_time_taken):
return response, time_taken, model_time_taken
def unpack_response_from_proto(self, response):
return response
class TextGenerationMethods(TaskMethods):
session_context = {}
@property
def method(self):
return "GeneratorReply"
pack_request_to_proto = multi_string_request_to_proto
unpack_request_from_proto = proto_request_to_list
def create_session(self, session_id):
if session_id in self.session_context:
raise ValueError(f"session {session_id} already exists")
self.session_context[session_id] = None
def destroy_session(self, session_id):
if session_id not in self.session_context:
raise ValueError(f"session {session_id} does not exist")
del self.session_context[session_id]
def preprocess_session(self, session_id, args):
if session_id not in self.session_context:
raise ValueError(f"session {session_id} does not exist")
if self.session_context[session_id] is None:
self.session_context[session_id] = ""
if len(args[0]) != 1:
raise ValueError(f"You can pass only one prompt with a session_id")
args = ([self.session_context[session_id] + args[0][0]], )
return args
def run_inference(self, inference_pipeline, args, kwargs):
session_id = kwargs.pop("session_id", None)
if session_id:
args = self.preprocess_session(session_id, args)
response = inference_pipeline(*args, **kwargs)
if session_id:
response = self.postprocess_session(session_id, args, response)
return response
def postprocess_session(self, session_id, args, response):
generated_text = response[0][0]["generated_text"]
self.session_context[session_id] = generated_text
response[0][0]["generated_text"] = generated_text[len(args[0][0]):]
return response
def pack_response_to_proto(self, response, time_taken, model_time_taken):
text_responses = []
for response in response:
text = response[0]["generated_text"]
text_responses.append(text)
return modelresponse_pb2.MultiStringReply(
response=text_responses,
time_taken=time_taken,
model_time_taken=model_time_taken,
)
class TextClassificationMethods(TaskMethods):
@property
def method(self):
return "ClassificationReply"
pack_request_to_proto = single_string_request_to_proto
unpack_request_from_proto = proto_request_to_single_input
pack_response_to_proto = single_string_response_to_proto
class QuestionAnsweringMethods(TaskMethods):
@property
def method(self):
return "QuestionAndAnswerReply"
pack_response_to_proto = single_string_response_to_proto
def pack_request_to_proto(self, request_dict, **query_kwargs):
return modelresponse_pb2.QARequest(
question=request_dict["question"],
context=request_dict["context"],
query_kwargs=kwarg_dict_to_proto(query_kwargs),
)
def unpack_request_from_proto(self, request):
kwargs = unpack_proto_query_kwargs(request.query_kwargs)
kwargs["question"] = request.question
kwargs["context"] = request.context
args = ()
return args, kwargs
class FillMaskMethods(TaskMethods):
@property
def method(self):
return "FillMaskReply"
pack_request_to_proto = single_string_request_to_proto
unpack_request_from_proto = proto_request_to_single_input
pack_response_to_proto = single_string_response_to_proto
class TokenClassificationMethods(TaskMethods):
@property
def method(self):
return "TokenClassificationReply"
pack_request_to_proto = single_string_request_to_proto
unpack_request_from_proto = proto_request_to_single_input
pack_response_to_proto = single_string_response_to_proto
class ConversationalMethods(TaskMethods):
@property
def method(self):
return "ConversationalReply"
def create_conversation(self, request):
if isinstance(request, dict):
assert 'text' in request and 'past_user_inputs' in request and 'generated_responses' in request, "Conversation requires 'text', 'past_user_inputs', and 'generated_responses' keys"
text = request['text']
conversation_id = request[
'conversation_id'] if 'conversation_id' in request else ""
past_user_inputs = request['past_user_inputs']
generated_responses = request['generated_responses']
else:
text = getattr(request, 'text')
conversation_id = getattr(request, 'conversation_id')
past_user_inputs = getattr(request, 'past_user_inputs')
generated_responses = getattr(request, 'generated_responses')
# Create UUID from conversation ID
conversation_id = uuid.uuid5(uuid.NAMESPACE_DNS, str(conversation_id))
conv = Conversation(text=text,
conversation_id=conversation_id,
past_user_inputs=past_user_inputs,
generated_responses=generated_responses)
return conv
def pack_response_to_proto(self, conv, time_taken, model_time_taken):
return modelresponse_pb2.ConversationReply(
conversation_id=str(conv.uuid),
past_user_inputs=conv.past_user_inputs,
generated_responses=conv.generated_responses,
time_taken=time_taken,
model_time_taken=model_time_taken,
)
def unpack_request_from_proto(self, request):
kwargs = unpack_proto_query_kwargs(request.query_kwargs)
conv = self.create_conversation(request)
args = (conv, )
return args, kwargs
def pack_request_to_proto(self, request_dict, **query_kwargs):
return modelresponse_pb2.ConversationRequest(
text=request_dict['text'],
conversation_id=str(request_dict['conversation_id'])
if 'conversation_id' in request_dict else "",
past_user_inputs=request_dict['past_user_inputs'],
generated_responses=request_dict['generated_responses'],
query_kwargs=kwarg_dict_to_proto(query_kwargs))
class Text2ImgMethods(TaskMethods):
@property
def method(self):
return "Txt2ImgReply"
pack_request_to_proto = multi_string_request_to_proto
unpack_request_from_proto = proto_request_to_list
def pack_response_to_proto(self, response, time_taken, model_time_taken):
images_bytes = []
nsfw_content_detected = []
response_count = len(response.images)
for i in range(response_count):
img = response.images[i]
img_bytes = img.tobytes()
images_bytes.append(img_bytes)
nsfw_content_detected.append(response.nsfw_content_detected[i])
img_mode = response.images[0].mode
img_size_w, img_size_h = response.images[0].size
return modelresponse_pb2.ImageReply(
images=images_bytes,
nsfw_content_detected=nsfw_content_detected,
mode=img_mode,
size_w=img_size_w,
size_h=img_size_h,
time_taken=time_taken,
)
def unpack_response_from_proto(self, response):
return ImageResponse(response)
GRPC_METHOD_TABLE = {
TaskType.TEXT_GENERATION: TextGenerationMethods(),
TaskType.TEXT_CLASSIFICATION: TextClassificationMethods(),
TaskType.QUESTION_ANSWERING: QuestionAnsweringMethods(),
TaskType.FILL_MASK: FillMaskMethods(),
TaskType.TOKEN_CLASSIFICATION: TokenClassificationMethods(),
TaskType.CONVERSATIONAL: ConversationalMethods(),
TaskType.TEXT2IMG: Text2ImgMethods(),
}