-
Notifications
You must be signed in to change notification settings - Fork 58
/
ModelServer.java
539 lines (490 loc) · 21 KB
/
ModelServer.java
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. 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. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file 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.
*/
package ai.djl.serving;
import ai.djl.Device;
import ai.djl.engine.Engine;
import ai.djl.engine.EngineException;
import ai.djl.metric.Dimension;
import ai.djl.metric.Metric;
import ai.djl.metric.Unit;
import ai.djl.modality.Input;
import ai.djl.modality.Output;
import ai.djl.repository.FilenameUtils;
import ai.djl.serving.http.ServerStartupException;
import ai.djl.serving.models.ModelManager;
import ai.djl.serving.plugins.DependencyManager;
import ai.djl.serving.plugins.FolderScanPluginManager;
import ai.djl.serving.util.ConfigManager;
import ai.djl.serving.util.Connector;
import ai.djl.serving.util.ServerGroups;
import ai.djl.serving.wlm.ModelInfo;
import ai.djl.serving.workflow.BadWorkflowException;
import ai.djl.serving.workflow.Workflow;
import ai.djl.serving.workflow.WorkflowDefinition;
import ai.djl.util.RandomUtils;
import ai.djl.util.Utils;
import ai.djl.util.cuda.CudaUtils;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.handler.ssl.SslContext;
import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.util.internal.logging.Slf4JLoggerFactory;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedWriter;
import java.io.IOException;
import java.lang.management.MemoryUsage;
import java.net.MalformedURLException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** The main entry point for model server. */
public class ModelServer {
private static final Logger logger = LoggerFactory.getLogger(ModelServer.class);
private static final Logger SERVER_METRIC = LoggerFactory.getLogger("server_metric");
private static final Pattern MODEL_STORE_PATTERN = Pattern.compile("(\\[?([^?]+?)]?=)?(.+)");
private ServerGroups serverGroups;
private List<ChannelFuture> futures = new ArrayList<>(2);
private AtomicBoolean stopped = new AtomicBoolean(false);
private ConfigManager configManager;
private FolderScanPluginManager pluginManager;
/**
* Creates a new {@code ModelServer} instance.
*
* @param configManager the model server configuration
*/
public ModelServer(ConfigManager configManager) {
this.configManager = configManager;
this.pluginManager = new FolderScanPluginManager(configManager);
serverGroups = new ServerGroups(configManager);
}
/**
* The entry point for the model server.
*
* @param args the command line arguments
*/
public static void main(String[] args) {
Options options = Arguments.getOptions();
try {
DefaultParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args, null, false);
Arguments arguments = new Arguments(cmd);
if (arguments.hasHelp()) {
printHelp("djl-serving [OPTIONS]", options);
return;
} else if (cmd.hasOption("install")) {
String[] dependencies = cmd.getOptionValues("install");
DependencyManager dm = DependencyManager.getInstance();
for (String dep : dependencies) {
try {
dm.installDependency(dep);
} catch (Throwable t) {
logger.error("Failed to install dependency: {}", dep, t);
System.exit(1); // NOPMD
}
}
return;
}
DependencyManager.getInstance().initialize();
ConfigManager.init(arguments);
ConfigManager configManager = ConfigManager.getInstance();
InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);
new ModelServer(configManager).startAndWait();
} catch (IllegalArgumentException e) {
logger.error("Invalid configuration", e);
SERVER_METRIC.info("{}", new Metric("ConfigurationError", 1));
System.exit(1); // NOPMD
} catch (ParseException e) {
printHelp(e.getMessage(), options);
SERVER_METRIC.info("{}", new Metric("CmdError", 1));
System.exit(1); // NOPMD
} catch (Throwable t) {
logger.error("Unexpected error", t);
SERVER_METRIC.info("{}", new Metric("StartupFailed", 1));
System.exit(1); // NOPMD
}
}
/**
* Starts the model server and block until server stops.
*
* @throws InterruptedException if interrupted
* @throws IOException if failed to start socket listener
* @throws GeneralSecurityException if failed to read SSL certificate
* @throws ServerStartupException if failed to startup server
*/
public void startAndWait()
throws InterruptedException,
IOException,
GeneralSecurityException,
ServerStartupException {
try {
logger.info("Starting model server ...");
List<ChannelFuture> channelFutures = start();
channelFutures.get(0).sync();
} finally {
serverGroups.shutdown(true);
logger.info("Model server stopped.");
}
}
/**
* Main Method that prepares the future for the channel and sets up the ServerBootstrap.
*
* @return a list of ChannelFuture object
* @throws InterruptedException if interrupted
* @throws IOException if failed to start socket listener
* @throws GeneralSecurityException if failed to read SSL certificate
* @throws ServerStartupException if failed to startup server
*/
public List<ChannelFuture> start()
throws InterruptedException,
IOException,
GeneralSecurityException,
ServerStartupException {
long begin = System.nanoTime();
stopped.set(false);
String version = Engine.getDjlVersion();
logger.info("Starting djl-serving: {} ...", version);
logger.info(configManager.dumpConfigurations());
Dimension dim = new Dimension("Version", version);
SERVER_METRIC.info("{}", new Metric("DJLServingStart", 1, Unit.COUNT, dim));
pluginManager.loadPlugins(true);
try {
initModelStore();
} catch (BadWorkflowException | CompletionException e) {
throw new ServerStartupException(
"Failed to initialize startup models and workflows", e);
}
Connector inferenceConnector =
configManager.getConnector(Connector.ConnectorType.INFERENCE);
Connector managementConnector =
configManager.getConnector(Connector.ConnectorType.MANAGEMENT);
inferenceConnector.clean();
managementConnector.clean();
EventLoopGroup serverGroup = serverGroups.getServerGroup();
EventLoopGroup workerGroup = serverGroups.getChildGroup();
futures.clear();
if (inferenceConnector.equals(managementConnector)) {
Connector both = configManager.getConnector(Connector.ConnectorType.BOTH);
futures.add(initializeServer(both, serverGroup, workerGroup));
} else {
futures.add(initializeServer(inferenceConnector, serverGroup, workerGroup));
futures.add(initializeServer(managementConnector, serverGroup, workerGroup));
}
long duration = (System.nanoTime() - begin) / 1000;
Metric metric = new Metric("StartupLatency", duration, Unit.MICROSECONDS);
SERVER_METRIC.info("{}", metric);
for (int i = 0; i < CudaUtils.getGpuCount(); ++i) {
try {
Device device = Device.gpu(i);
MemoryUsage mem = CudaUtils.getGpuMemory(device);
SERVER_METRIC.info(
"{}", new Metric("GPUMemory_" + i, mem.getCommitted(), Unit.BYTES));
} catch (IllegalArgumentException | EngineException e) {
logger.warn("Failed get GPU memory", e);
break;
}
}
if (stopped.get()) {
// check if model load failed in wait loading model case
stop();
}
return futures;
}
/**
* Return if the server is running.
*
* @return {@code true} if the server is running
*/
public boolean isRunning() {
return !stopped.get();
}
/** Stops the model server. */
public void stop() {
logger.info("Stopping model server.");
stopped.set(true);
for (ChannelFuture future : futures) {
future.channel().close();
}
serverGroups.shutdown(true);
serverGroups.reset();
}
private ChannelFuture initializeServer(
Connector connector, EventLoopGroup serverGroup, EventLoopGroup workerGroup)
throws InterruptedException, IOException, GeneralSecurityException {
Class<? extends ServerChannel> channelClass = connector.getServerChannel();
logger.info(
"Initialize {} server with: {}.",
connector.getType(),
channelClass.getSimpleName());
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024)
.channel(channelClass)
.childOption(ChannelOption.SO_LINGER, 0)
.childOption(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true);
b.group(serverGroup, workerGroup);
SslContext sslCtx = null;
if (connector.isSsl()) {
sslCtx = configManager.getSslContext();
}
b.childHandler(new ServerInitializer(sslCtx, connector.getType(), pluginManager));
ChannelFuture future;
try {
future = b.bind(connector.getSocketAddress()).sync();
} catch (Exception e) {
// https://github.com/netty/netty/issues/2597
if (e instanceof IOException) {
throw new IOException("Failed to bind to address: " + connector, e);
}
throw e;
}
future.addListener(
(ChannelFutureListener)
f -> {
if (!f.isSuccess()) {
try {
f.get();
} catch (InterruptedException | ExecutionException e) {
logger.error("", e);
}
System.exit(2); // NOPMD
}
serverGroups.registerChannel(f.channel());
});
future.sync();
ChannelFuture f = future.channel().closeFuture();
f.addListener(
(ChannelFutureListener)
listener -> logger.info("{} listener stopped.", connector.getType()));
logger.info("{} API bind to: {}", connector.getType(), connector);
return f;
}
private void initModelStore() throws IOException, BadWorkflowException {
Set<String> startupModels = ModelManager.getInstance().getStartupWorkflows();
String loadModels = configManager.getLoadModels();
Path modelStore = configManager.getModelStore();
if (loadModels == null || loadModels.isEmpty()) {
loadModels = "ALL";
}
ModelManager modelManager = ModelManager.getInstance();
Set<String> urls = new HashSet<>();
if ("NONE".equalsIgnoreCase(loadModels)) {
// to disable load all models from model store
return;
} else if ("ALL".equalsIgnoreCase(loadModels)) {
if (modelStore == null) {
logger.warn("Model store is not configured.");
return;
}
if (Files.isDirectory(modelStore)) {
// contains only directory or archive files
boolean isMultiModelsDirectory =
Files.list(modelStore)
.filter(p -> !p.getFileName().toString().startsWith("."))
.allMatch(
p ->
Files.isDirectory(p)
|| FilenameUtils.isArchiveFile(
p.toString()));
if (isMultiModelsDirectory) {
// Check folders to see if they can be models as well
try (Stream<Path> stream = Files.list(modelStore)) {
urls.addAll(
stream.map(this::mapModelUrl)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
}
} else {
// Check if root model store folder contains a model
String url = mapModelUrl(modelStore);
if (url != null) {
urls.add(url);
}
}
} else {
logger.warn("Model store path is not found: {}", modelStore);
}
} else {
String[] modelsUrls = loadModels.split("[, ]+");
urls.addAll(Arrays.asList(modelsUrls));
}
String huggingFaceModelId = Utils.getEnvOrSystemProperty("HF_MODEL_ID");
if (huggingFaceModelId != null) {
urls.add(createHuggingFaceModel(huggingFaceModelId));
}
for (String url : urls) {
logger.info("Initializing model: {}", url);
Matcher matcher = MODEL_STORE_PATTERN.matcher(url);
if (!matcher.matches()) {
throw new AssertionError("Invalid model store url: " + url);
}
String endpoint = matcher.group(2);
String modelUrl = matcher.group(3);
String version = null;
String engineName = null;
String deviceMapping = null;
String modelName = null;
if (endpoint != null) {
String[] tokens = endpoint.split(":", -1);
modelName = tokens[0];
if (tokens.length > 1) {
version = tokens[1].isEmpty() ? null : tokens[1];
}
if (tokens.length > 2) {
engineName = tokens[2].isEmpty() ? null : tokens[2];
}
if (tokens.length > 3) {
deviceMapping = tokens[3];
}
}
Workflow workflow;
URI uri = WorkflowDefinition.toWorkflowUri(modelUrl);
if (uri != null) {
workflow = WorkflowDefinition.parse(modelName, uri).toWorkflow();
} else {
if (modelName == null) {
modelName = ModelInfo.inferModelNameFromUrl(modelUrl);
}
ModelInfo<Input, Output> modelInfo =
new ModelInfo<>(
modelName,
modelUrl,
version,
engineName,
deviceMapping,
Input.class,
Output.class,
-1,
-1,
-1,
-1,
-1,
-1);
workflow = new Workflow(modelInfo);
}
CompletableFuture<Void> f = modelManager.registerWorkflow(workflow);
f.exceptionally(
t -> {
logger.error("Failed register workflow", t);
Dimension dim = new Dimension("Model", workflow.getName());
SERVER_METRIC.info(
"{}", new Metric("ModelLoadingError", 1, Unit.COUNT, dim));
// delay 3 seconds, allows REST API to send PING
// response (health check)
try {
Thread.sleep(3000);
} catch (InterruptedException ignore) {
// ignore
}
stop();
return null;
});
if (configManager.waitModelLoading()) {
f.join();
}
startupModels.add(modelName);
}
}
String mapModelUrl(Path path) {
try {
if (!Files.exists(path)
|| Files.isHidden(path)
|| (!Files.isDirectory(path)
&& !FilenameUtils.isArchiveFile(path.toString()))) {
return null;
}
if (Files.list(path).findFirst().isEmpty()) {
return null;
}
path = Utils.getNestedModelDir(path);
String url = path.toUri().toURL().toString();
String modelName = ModelInfo.inferModelNameFromUrl(url);
logger.info("Found model {}={}", modelName, url);
return modelName + '=' + url;
} catch (MalformedURLException e) {
throw new AssertionError("Invalid path: " + path, e);
} catch (IOException e) {
logger.warn("Failed to access file: " + path, e);
return null;
}
}
private static void printHelp(String msg, Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.setLeftPadding(1);
formatter.setWidth(120);
formatter.printHelp(msg, options);
}
private String createHuggingFaceModel(String modelId) throws IOException {
if (modelId.startsWith("djl://") || modelId.startsWith("s3://")) {
return modelId;
}
Path path = Paths.get(modelId);
if (Files.exists(path)) {
// modelId point to a local file
return modelId;
}
// TODO: Download the full model from HF
String hash = Utils.hash(modelId);
String downloadDir = Utils.getenv("SERVING_DOWNLOAD_DIR", null);
Path parent = downloadDir == null ? Utils.getCacheDir() : Paths.get(downloadDir);
Path huggingFaceModelDir = parent.resolve(hash);
String modelName = modelId.replaceAll("(\\W|^_)", "_");
if (Files.exists(huggingFaceModelDir)) {
logger.warn("HuggingFace Model {} already exists, use random model name", modelId);
return modelName + '_' + RandomUtils.nextInt() + '=' + huggingFaceModelDir;
}
String huggingFaceModelRevision = Utils.getEnvOrSystemProperty("HF_REVISION");
Properties huggingFaceProperties = new Properties();
huggingFaceProperties.put("option.model_id", modelId);
if (huggingFaceModelRevision != null) {
huggingFaceProperties.put("option.revision", huggingFaceModelRevision);
}
String task = Utils.getEnvOrSystemProperty("HF_TASK");
if (task != null) {
huggingFaceProperties.put("option.task", task);
}
Files.createDirectories(huggingFaceModelDir);
Path propertiesFile = huggingFaceModelDir.resolve("serving.properties");
try (BufferedWriter writer = Files.newBufferedWriter(propertiesFile)) {
huggingFaceProperties.store(writer, null);
}
logger.debug("Created serving.properties for model at path {}", propertiesFile);
return modelName + '=' + huggingFaceModelDir;
}
}