forked from SeldonIO/MLServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
277 lines (250 loc) · 6.99 KB
/
main.py
File metadata and controls
277 lines (250 loc) · 6.99 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
"""
Command-line interface to manage MLServer models.
"""
import click
from functools import wraps
from .init_project import init_cookiecutter_project
from ..server import MLServer
from ..logging import logger, configure_logger
from ..utils import AsyncManager
from .build import generate_dockerfile, build_image, write_dockerfile
from .serve import load_settings
from ..batch_processing import process_batch, CHOICES_TRANSPORT
CTX_ASYNC_MGR_KEY = "async_manager"
def click_async(f):
@wraps(f)
def wrapper(*args, **kwargs):
ctx = click.get_current_context()
async_mgr = ctx.obj[CTX_ASYNC_MGR_KEY]
return async_mgr.run(f(*args, **kwargs))
return wrapper
@click.group()
@click.version_option()
@click.pass_context
def root(ctx):
"""
Command-line interface to manage MLServer models.
"""
ctx.ensure_object(dict)
@root.command("start")
@click.argument("folder", nargs=1)
@click_async
async def start(folder: str):
"""
Start serving a machine learning model with MLServer.
"""
settings, models_settings = await load_settings(folder)
server = MLServer(settings)
await server.start(models_settings)
@root.command("build")
@click.argument("folder", nargs=1)
@click.option("-t", "--tag", type=str)
@click.option("--no-cache", default=False, is_flag=True)
@click_async
async def build(folder: str, tag: str, no_cache: bool = False):
"""
Build a Docker image for a custom MLServer runtime.
"""
dockerfile = generate_dockerfile()
build_image(folder, dockerfile, tag, no_cache=no_cache)
logger.info(f"Successfully built custom Docker image with tag {tag}")
@root.command("init")
# TODO: Update to have template(s) in the SeldonIO org
@click.option("-t", "--template", default="https://github.com/EthicalML/sml-security/")
@click_async
async def init_project(template: str):
"""
Generate a base project template
"""
init_cookiecutter_project(template)
@root.command("dockerfile")
@click.argument("folder", nargs=1)
@click.option("-i", "--include-dockerignore", is_flag=True)
@click_async
async def dockerfile(folder: str, include_dockerignore: bool):
"""
Generate a Dockerfile
"""
dockerfile = generate_dockerfile()
dockerfile_path = write_dockerfile(
folder, dockerfile, include_dockerignore=include_dockerignore
)
logger.info(f"Successfully written Dockerfile in {dockerfile_path}")
@root.command("infer")
@click.option(
"--url",
"-u",
default="localhost:8080",
envvar="MLSERVER_INFER_URL",
help=(
"URL of the MLServer to send inference requests to. "
"Should not contain http or https."
),
)
@click.option(
"--model-name",
"-m",
type=str,
required=True,
envvar="MLSERVER_INFER_MODEL_NAME",
help="Name of the model to send inference requests to.",
)
@click.option(
"--input-data-path",
"-i",
required=True,
type=click.Path(),
envvar="MLSERVER_INFER_INPUT_DATA_PATH",
help="Local path to the input file containing inference requests to be processed.",
)
@click.option(
"--output-data-path",
"-o",
required=True,
type=click.Path(),
envvar="MLSERVER_INFER_OUTPUT_DATA_PATH",
help="Local path to the output file for the inference responses to be written to.",
)
@click.option("--workers", "-w", default=10, envvar="MLSERVER_INFER_WORKERS")
@click.option("--retries", "-r", default=3, envvar="MLSERVER_INFER_RETRIES")
@click.option(
"--batch-size",
"-s",
default=1,
envvar="MLSERVER_INFER_BATCH_SIZE",
help="Send inference requests grouped together as micro-batches.",
)
@click.option(
"--binary-data",
"-b",
is_flag=True,
default=False,
envvar="MLSERVER_INFER_BINARY_DATA",
help="Send inference requests as binary data (not fully supported).",
)
@click.option(
"--verbose",
"-v",
is_flag=True,
default=False,
envvar="MLSERVER_INFER_VERBOSE",
help="Verbose mode.",
)
@click.option(
"--extra-verbose",
"-vv",
is_flag=True,
default=False,
envvar="MLSERVER_INFER_EXTRA_VERBOSE",
help="Extra verbose mode (shows detailed requests and responses).",
)
@click.option(
"--transport",
"-t",
envvar="MLSERVER_INFER_TRANSPORT",
type=click.Choice(CHOICES_TRANSPORT),
default="rest",
help=(
"Transport type to use to send inference requests. "
"Can be 'rest' or 'grpc' (not yet supported)."
),
)
@click.option(
"--request-headers",
"-H",
envvar="MLSERVER_INFER_REQUEST_HEADERS",
type=str,
multiple=True,
help=(
"Headers to be set on each inference request send to the server. "
"Multiple options are allowed as: -H 'Header1: Val1' -H 'Header2: Val2'. "
"When setting up as environmental provide as 'Header1:Val1 Header2:Val2'."
),
)
@click.option(
"--timeout",
default=60,
envvar="MLSERVER_INFER_CONNECTION_TIMEOUT",
help="Connection timeout to be passed to tritonclient.",
)
@click.option(
"--batch-interval",
default=0,
type=float,
envvar="MLSERVER_INFER_BATCH_INTERVAL",
help="Minimum time interval (in seconds) between requests made by each worker.",
)
@click.option(
"--batch-jitter",
default=0,
type=float,
envvar="MLSERVER_INFER_BATCH_JITTER",
help="Maximum random jitter (in seconds) added to batch interval between requests.",
)
@click.option(
"--use-ssl",
is_flag=True,
default=False,
envvar="MLSERVER_INFER_USE_SSL",
help="Use SSL in communications with inference server.",
)
@click.option(
"--insecure",
is_flag=True,
default=False,
envvar="MLSERVER_INFER_INSECURE",
help="Disable SSL verification in communications. Use with caution.",
)
@click_async
# TODO: add flags for SSL --key-file and --cert-file (see Tritonclient examples)
async def infer(
model_name,
url,
workers,
retries,
batch_size,
input_data_path,
output_data_path,
binary_data,
transport,
request_headers,
timeout,
batch_interval,
batch_jitter,
use_ssl,
insecure,
verbose,
extra_verbose,
):
"""
Deprecated: This experimental feature will be removed in future work.
Execute batch inference requests against V2 inference server.
"""
logger.warn(
"This feature has been deprecated and will be removed in a future version"
)
await process_batch(
model_name=model_name,
url=url,
workers=workers,
retries=retries,
batch_size=batch_size,
input_data_path=input_data_path,
output_data_path=output_data_path,
binary_data=binary_data,
transport=transport,
request_headers=request_headers,
timeout=timeout,
batch_interval=batch_interval,
batch_jitter=batch_jitter,
use_ssl=use_ssl,
insecure=insecure,
verbose=verbose,
extra_verbose=extra_verbose,
)
def main():
configure_logger()
async_mgr = AsyncManager()
root(obj={CTX_ASYNC_MGR_KEY: async_mgr})
if __name__ == "__main__":
main()