-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathbase.py
More file actions
342 lines (293 loc) · 11.4 KB
/
base.py
File metadata and controls
342 lines (293 loc) · 11.4 KB
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
# Copyright The Lightning AI team.
#
# 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 asyncio
import inspect
import logging
import pickle
import sys
import time
from abc import ABC
from queue import Empty, Queue
from typing import Any, Dict, List, Optional, Tuple, Union
from starlette.formparsers import MultiPartParser
from litserve import LitAPI
from litserve.callbacks import CallbackRunner
from litserve.specs.base import LitSpec
from litserve.transport.base import MessageTransport
from litserve.utils import LitAPIStatus
logger = logging.getLogger(__name__)
# FastAPI writes form files to disk over 1MB by default, which prevents serialization by multiprocessing
MultiPartParser.max_file_size = sys.maxsize
def _inject_context(context: Union[List[dict], dict], func, *args, **kwargs):
sig = inspect.signature(func)
if "context" in sig.parameters:
return func(*args, **kwargs, context=context)
return func(*args, **kwargs)
def collate_requests(
lit_api: LitAPI, request_queue: Queue, max_batch_size: int, batch_timeout: float
) -> Tuple[List, List]:
payloads = []
timed_out_uids = []
entered_at = time.monotonic()
end_time = entered_at + batch_timeout
apply_timeout = lit_api.request_timeout not in (-1, False)
if batch_timeout == 0:
while len(payloads) < max_batch_size:
try:
response_queue_id, uid, timestamp, x_enc = request_queue.get_nowait()
if apply_timeout and time.monotonic() - timestamp > lit_api.request_timeout:
timed_out_uids.append((response_queue_id, uid))
else:
payloads.append((response_queue_id, uid, x_enc))
except Empty:
break
return payloads, timed_out_uids
while time.monotonic() < end_time and len(payloads) < max_batch_size:
remaining_time = end_time - time.monotonic()
if remaining_time <= 0:
break
try:
response_queue_id, uid, timestamp, x_enc = request_queue.get(timeout=min(remaining_time, 0.001))
if apply_timeout and time.monotonic() - timestamp > lit_api.request_timeout:
timed_out_uids.append((response_queue_id, uid))
else:
payloads.append((response_queue_id, uid, x_enc))
except Empty:
continue
return payloads, timed_out_uids
class _BaseLoop(ABC):
"""Loop runs an inference engine that executes a specific set of hooks, implemented in the LitAPI, in a predefined
order.
For a default loop, LitAPI must implement the following hooks:
- decode_request
- batch
- predict
- unbatch
- encode_response
To implement a custom loop, subclass this class and implement the `run` method. The `run` method should execute the
hooks in the desired order.
`__call__` method is the entry point for the worker process. It calls the `run` method in a loop until the worker is
terminated.
Example:
```python
class TestLoop(_BaseLoop):
def run(
self,
lit_api: LitAPI,
lit_spec: Optional[LitSpec],
device: str,
worker_id: int,
request_queue: Queue,
response_queues: List[Queue],
max_batch_size: int,
batch_timeout: float,
stream: bool,
workers_setup_status: Dict[int, str],
callback_runner: CallbackRunner,
):
item = request_queue.get()
if item is None:
return
response_queue_id, uid, timestamp, x_enc = item
# Expects LitAPI to implement the load_cache method
lit_api.load_cache(x_enc)
x = lit_api.decode_request(x_enc)
response = lit_api.predict(x)
response_enc = lit_api.encode_response(response)
response_queues[response_queue_id].put((uid, (response_enc, LitAPIStatus.OK)))
```
"""
def pre_setup(self, lit_api: LitAPI, spec: Optional[LitSpec]):
pass
async def schedule_task(
self,
lit_api: LitAPI,
lit_spec: Optional[LitSpec],
request_queue: Queue,
max_batch_size: int,
batch_timeout: float,
response_queues: List[Queue],
):
pass
def __call__(
self,
lit_api: LitAPI,
lit_spec: Optional[LitSpec],
device: str,
worker_id: int,
request_queue: Queue,
transport: MessageTransport,
max_batch_size: int,
batch_timeout: float,
stream: bool,
workers_setup_status: Dict[int, str],
callback_runner: CallbackRunner,
):
if asyncio.iscoroutinefunction(self.run):
event_loop = asyncio.new_event_loop()
async def _wrapper():
logger.info("Running LitLoop in a asyncio event loop")
future = self.schedule_task(lit_api, lit_spec, request_queue, max_batch_size, batch_timeout, transport)
_ = event_loop.create_task(future)
while True:
try:
await self.run(
lit_api,
lit_spec,
device,
worker_id,
request_queue,
transport,
max_batch_size,
batch_timeout,
stream,
workers_setup_status,
callback_runner,
)
await asyncio.sleep(0)
except Exception as e:
logger.exception("An error occurred in the loop: %s", e)
event_loop.run_until_complete(_wrapper())
else:
while True:
self.run(
lit_api,
lit_spec,
device,
worker_id,
request_queue,
transport,
max_batch_size,
batch_timeout,
stream,
workers_setup_status,
callback_runner,
)
def run(
self,
lit_api: LitAPI,
lit_spec: Optional[LitSpec],
device: str,
worker_id: int,
request_queue: Queue,
transport: MessageTransport,
max_batch_size: int,
batch_timeout: float,
stream: bool,
workers_setup_status: Dict[int, str],
callback_runner: CallbackRunner,
):
raise NotImplementedError
class LitLoop(_BaseLoop):
def __init__(self):
self._context = {}
def get_batch_requests(self, lit_api: LitAPI, request_queue: Queue, max_batch_size: int, batch_timeout: float):
batches, timed_out_uids = collate_requests(
lit_api,
request_queue,
max_batch_size,
batch_timeout,
)
return batches, timed_out_uids
def get_request(self, request_queue: Queue, block: bool = True, timeout: Optional[float] = None):
try:
return request_queue.get(block=block, timeout=timeout)
except Empty:
return None
def populate_context(self, lit_spec: LitSpec, request: Any):
if lit_spec and hasattr(lit_spec, "populate_context"):
lit_spec.populate_context(self._context, request)
def put_response(
self, transport: MessageTransport, response_queue_id: int, uid: str, response_data: Any, status: LitAPIStatus
) -> None:
transport.send((uid, (response_data, status)), consumer_id=response_queue_id)
def put_error_response(
self, transport: MessageTransport, response_queue_id: int, uid: str, error: Exception
) -> None:
error = pickle.dumps(error)
self.put_response(transport, response_queue_id, uid, error, LitAPIStatus.ERROR)
class DefaultLoop(LitLoop):
def pre_setup(self, lit_api: LitAPI, spec: Optional[LitSpec]):
# we will sanitize regularly if no spec
# in case, we have spec then:
# case 1: spec implements a streaming API
# Case 2: spec implements a non-streaming API
if spec:
# TODO: Implement sanitization
lit_api._spec = spec
return
original = lit_api.unbatch.__code__ is LitAPI.unbatch.__code__
if not lit_api.stream and any([
inspect.isgeneratorfunction(lit_api.predict),
inspect.isgeneratorfunction(lit_api.encode_response),
]):
raise ValueError(
"""When `stream=False`, `lit_api.predict`, `lit_api.encode_response` must not be
generator functions.
Correct usage:
def predict(self, inputs):
...
return {"output": output}
Incorrect usage:
def predict(self, inputs):
...
for i in range(max_token_length):
yield prediction
"""
)
if (
lit_api.stream
and lit_api.max_batch_size > 1
and not all([
inspect.isgeneratorfunction(lit_api.predict),
inspect.isgeneratorfunction(lit_api.encode_response),
(original or inspect.isgeneratorfunction(lit_api.unbatch)),
])
):
raise ValueError(
"""When `stream=True` with max_batch_size > 1, `lit_api.predict`, `lit_api.encode_response` and
`lit_api.unbatch` must generate values using `yield`.
Example:
def predict(self, inputs):
...
for i in range(max_token_length):
yield prediction
def encode_response(self, outputs):
for output in outputs:
encoded_output = ...
yield encoded_output
def unbatch(self, outputs):
for output in outputs:
unbatched_output = ...
yield unbatched_output
"""
)
if lit_api.stream and not all([
inspect.isgeneratorfunction(lit_api.predict),
inspect.isgeneratorfunction(lit_api.encode_response),
]):
raise ValueError(
"""When `stream=True` both `lit_api.predict` and
`lit_api.encode_response` must generate values using `yield`.
Example:
def predict(self, inputs):
...
for i in range(max_token_length):
yield prediction
def encode_response(self, outputs):
for output in outputs:
encoded_output = ...
yield encoded_output
"""
)