-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmain.cc
552 lines (470 loc) · 16.2 KB
/
main.cc
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
540
541
542
543
544
545
546
547
548
549
550
551
552
// Copyright 2018-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifdef _WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#endif
#ifndef _WIN32
#include <getopt.h>
#include <unistd.h>
#endif
#include <stdint.h>
#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <thread>
#include "triton_signal.h"
#ifdef TRITON_ENABLE_ASAN
#include <sanitizer/lsan_interface.h>
#endif // TRITON_ENABLE_ASAN
#include "command_line_parser.h"
#include "common.h"
#include "shared_memory_manager.h"
#include "tracer.h"
#include "triton/common/logging.h"
#include "triton/core/tritonserver.h"
#if defined(TRITON_ENABLE_HTTP) || defined(TRITON_ENABLE_METRICS)
#include "http_server.h"
#endif // TRITON_ENABLE_HTTP|| TRITON_ENABLE_METRICS
#ifdef TRITON_ENABLE_SAGEMAKER
#include "sagemaker_server.h"
#endif // TRITON_ENABLE_SAGEMAKER
#ifdef TRITON_ENABLE_VERTEX_AI
#include "vertex_ai_server.h"
#endif // TRITON_ENABLE_VERTEX_AI
#ifdef TRITON_ENABLE_GRPC
#include "grpc/grpc_server.h"
#endif // TRITON_ENABLE_GRPC
#ifdef TRITON_ENABLE_GPU
static_assert(
TRITON_MIN_COMPUTE_CAPABILITY >= 1.0,
"Invalid TRITON_MIN_COMPUTE_CAPABILITY specified");
#endif // TRITON_ENABLE_GPU
namespace {
#ifdef TRITON_ENABLE_HTTP
std::unique_ptr<triton::server::HTTPServer> g_http_service;
#endif // TRITON_ENABLE_HTTP
#ifdef TRITON_ENABLE_GRPC
std::unique_ptr<triton::server::grpc::Server> g_grpc_service;
#endif // TRITON_ENABLE_GRPC
#ifdef TRITON_ENABLE_METRICS
std::unique_ptr<triton::server::HTTPServer> g_metrics_service;
#endif // TRITON_ENABLE_METRICS
#ifdef TRITON_ENABLE_SAGEMAKER
std::unique_ptr<triton::server::HTTPServer> g_sagemaker_service;
#endif // TRITON_ENABLE_SAGEMAKER
#ifdef TRITON_ENABLE_VERTEX_AI
std::unique_ptr<triton::server::HTTPServer> g_vertex_ai_service;
#endif // TRITON_ENABLE_VERTEX_AI
triton::server::TritonServerParameters g_triton_params;
#ifdef TRITON_ENABLE_GRPC
TRITONSERVER_Error*
StartGrpcService(
std::unique_ptr<triton::server::grpc::Server>* service,
const std::shared_ptr<TRITONSERVER_Server>& server,
triton::server::TraceManager* trace_manager,
const std::shared_ptr<triton::server::SharedMemoryManager>& shm_manager)
{
TRITONSERVER_Error* err = triton::server::grpc::Server::Create(
server, trace_manager, shm_manager, g_triton_params.grpc_options_,
service);
if (err == nullptr) {
err = (*service)->Start();
}
if (err != nullptr) {
service->reset();
}
return err;
}
#endif // TRITON_ENABLE_GRPC
#ifdef TRITON_ENABLE_HTTP
TRITONSERVER_Error*
StartHttpService(
std::unique_ptr<triton::server::HTTPServer>* service,
const std::shared_ptr<TRITONSERVER_Server>& server,
triton::server::TraceManager* trace_manager,
const std::shared_ptr<triton::server::SharedMemoryManager>& shm_manager)
{
TRITONSERVER_Error* err = triton::server::HTTPAPIServer::Create(
server, trace_manager, shm_manager, g_triton_params.http_port_,
g_triton_params.reuse_http_port_, g_triton_params.http_address_,
g_triton_params.http_forward_header_pattern_,
g_triton_params.http_thread_cnt_, g_triton_params.http_restricted_apis_,
service);
if (err == nullptr) {
err = (*service)->Start();
}
if (err != nullptr) {
service->reset();
}
return err;
}
#endif // TRITON_ENABLE_HTTP
#ifdef TRITON_ENABLE_METRICS
TRITONSERVER_Error*
StartMetricsService(
std::unique_ptr<triton::server::HTTPServer>* service,
const std::shared_ptr<TRITONSERVER_Server>& server)
{
TRITONSERVER_Error* err = triton::server::HTTPMetricsServer::Create(
server, g_triton_params.metrics_port_, g_triton_params.metrics_address_,
1 /* HTTP thread count */, service);
if (err == nullptr) {
err = (*service)->Start();
}
if (err != nullptr) {
service->reset();
}
return err;
}
#endif // TRITON_ENABLE_METRICS
#ifdef TRITON_ENABLE_SAGEMAKER
TRITONSERVER_Error*
StartSagemakerService(
std::unique_ptr<triton::server::HTTPServer>* service,
const std::shared_ptr<TRITONSERVER_Server>& server,
triton::server::TraceManager* trace_manager,
const std::shared_ptr<triton::server::SharedMemoryManager>& shm_manager)
{
TRITONSERVER_Error* err = triton::server::SagemakerAPIServer::Create(
server, trace_manager, shm_manager, g_triton_params.sagemaker_port_,
g_triton_params.sagemaker_address_, g_triton_params.sagemaker_thread_cnt_,
service);
if (err == nullptr) {
err = (*service)->Start();
}
if (err != nullptr) {
service->reset();
}
return err;
}
#endif // TRITON_ENABLE_SAGEMAKER
#ifdef TRITON_ENABLE_VERTEX_AI
TRITONSERVER_Error*
StartVertexAiService(
std::unique_ptr<triton::server::HTTPServer>* service,
const std::shared_ptr<TRITONSERVER_Server>& server,
triton::server::TraceManager* trace_manager,
const std::shared_ptr<triton::server::SharedMemoryManager>& shm_manager)
{
TRITONSERVER_Error* err = triton::server::VertexAiAPIServer::Create(
server, trace_manager, shm_manager, g_triton_params.vertex_ai_port_,
g_triton_params.vertex_ai_address_, g_triton_params.vertex_ai_thread_cnt_,
g_triton_params.vertex_ai_default_model_, service);
if (err == nullptr) {
err = (*service)->Start();
}
if (err != nullptr) {
service->reset();
}
return err;
}
#endif // TRITON_ENABLE_VERTEX_AI
bool
StartEndpoints(
const std::shared_ptr<TRITONSERVER_Server>& server,
triton::server::TraceManager* trace_manager,
const std::shared_ptr<triton::server::SharedMemoryManager>& shm_manager)
{
#ifdef _WIN32
WSADATA wsaData;
int wsa_ret = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (wsa_ret != 0) {
LOG_ERROR << "Error in WSAStartup " << wsa_ret;
return false;
}
#endif
#ifdef TRITON_ENABLE_GRPC
// Enable GRPC endpoints if requested...
if (g_triton_params.allow_grpc_) {
TRITONSERVER_Error* err =
StartGrpcService(&g_grpc_service, server, trace_manager, shm_manager);
if (err != nullptr) {
LOG_TRITONSERVER_ERROR(err, "failed to start GRPC service");
return false;
}
}
#endif // TRITON_ENABLE_GRPC
#ifdef TRITON_ENABLE_HTTP
// Enable HTTP endpoints if requested...
if (g_triton_params.allow_http_) {
TRITONSERVER_Error* err =
StartHttpService(&g_http_service, server, trace_manager, shm_manager);
if (err != nullptr) {
LOG_TRITONSERVER_ERROR(err, "failed to start HTTP service");
return false;
}
}
#endif // TRITON_ENABLE_HTTP
#ifdef TRITON_ENABLE_SAGEMAKER
// Enable Sagemaker endpoints if requested...
if (g_triton_params.allow_sagemaker_) {
TRITONSERVER_Error* err = StartSagemakerService(
&g_sagemaker_service, server, trace_manager, shm_manager);
if (err != nullptr) {
LOG_TRITONSERVER_ERROR(err, "failed to start Sagemaker service");
return false;
}
}
#endif // TRITON_ENABLE_SAGEMAKER
#ifdef TRITON_ENABLE_VERTEX_AI
// Enable Vertex AI endpoints if requested...
if (g_triton_params.allow_vertex_ai_) {
TRITONSERVER_Error* err = StartVertexAiService(
&g_vertex_ai_service, server, trace_manager, shm_manager);
if (err != nullptr) {
LOG_TRITONSERVER_ERROR(err, "failed to start Vertex AI service");
return false;
}
}
#endif // TRITON_ENABLE_VERTEX_AI
#ifdef TRITON_ENABLE_METRICS
// Enable metrics endpoint if requested...
if (g_triton_params.allow_metrics_) {
TRITONSERVER_Error* err = StartMetricsService(&g_metrics_service, server);
if (err != nullptr) {
LOG_TRITONSERVER_ERROR(err, "failed to start Metrics service");
return false;
}
}
#endif // TRITON_ENABLE_METRICS
return true;
}
bool
StopEndpoints(uint32_t* exit_timeout_secs)
{
bool ret = true;
#ifdef TRITON_ENABLE_HTTP
if (g_http_service) {
TRITONSERVER_Error* err = g_http_service->Stop(exit_timeout_secs);
if (err != nullptr) {
LOG_TRITONSERVER_ERROR(err, "failed to stop HTTP service");
ret = false;
}
g_http_service.reset();
}
#endif // TRITON_ENABLE_HTTP
return ret;
}
bool
StopEndpoints()
{
bool ret = true;
// TODO: Add support for 'exit_timeout_secs' to the endpoints below and move
// them to the 'StopEndpoints(uint32_t* exit_timeout_secs)' function above.
#ifdef TRITON_ENABLE_GRPC
if (g_grpc_service) {
TRITONSERVER_Error* err = g_grpc_service->Stop();
if (err != nullptr) {
LOG_TRITONSERVER_ERROR(err, "failed to stop GRPC service");
ret = false;
}
g_grpc_service.reset();
}
#endif // TRITON_ENABLE_GRPC
#ifdef TRITON_ENABLE_METRICS
if (g_metrics_service) {
TRITONSERVER_Error* err = g_metrics_service->Stop();
if (err != nullptr) {
LOG_TRITONSERVER_ERROR(err, "failed to stop Metrics service");
ret = false;
}
g_metrics_service.reset();
}
#endif // TRITON_ENABLE_METRICS
#ifdef TRITON_ENABLE_SAGEMAKER
if (g_sagemaker_service) {
TRITONSERVER_Error* err = g_sagemaker_service->Stop();
if (err != nullptr) {
LOG_TRITONSERVER_ERROR(err, "failed to stop Sagemaker service");
ret = false;
}
g_sagemaker_service.reset();
}
#endif // TRITON_ENABLE_SAGEMAKER
#ifdef TRITON_ENABLE_VERTEX_AI
if (g_vertex_ai_service) {
TRITONSERVER_Error* err = g_vertex_ai_service->Stop();
if (err != nullptr) {
LOG_TRITONSERVER_ERROR(err, "failed to stop Vertex AI service");
ret = false;
}
g_vertex_ai_service.reset();
}
#endif // TRITON_ENABLE_VERTEX_AI
#ifdef _WIN32
int wsa_ret = WSACleanup();
if (wsa_ret != 0) {
LOG_ERROR << "Error in WSACleanup " << wsa_ret;
ret = false;
}
#endif
return ret;
}
bool
StartTracing(triton::server::TraceManager** trace_manager)
{
*trace_manager = nullptr;
#ifdef TRITON_ENABLE_TRACING
TRITONSERVER_Error* err = triton::server::TraceManager::Create(
trace_manager, g_triton_params.trace_level_, g_triton_params.trace_rate_,
g_triton_params.trace_count_, g_triton_params.trace_log_frequency_,
g_triton_params.trace_filepath_, g_triton_params.trace_mode_,
g_triton_params.trace_config_map_);
if (err != nullptr) {
LOG_TRITONSERVER_ERROR(err, "failed to configure tracing");
if (*trace_manager != nullptr) {
delete (*trace_manager);
}
*trace_manager = nullptr;
return false;
}
#endif // TRITON_ENABLE_TRACING
return true;
}
bool
StopTracing(triton::server::TraceManager** trace_manager)
{
#ifdef TRITON_ENABLE_TRACING
// We assume that at this point Triton has been stopped gracefully,
// so can delete the trace manager to finalize the output.
delete (*trace_manager);
*trace_manager = nullptr;
#endif // TRITON_ENABLE_TRACING
return true;
}
} // namespace
int
main(int argc, char** argv)
{
// Parse command-line to create the options for the inference
// server.
triton::server::TritonParser tp;
try {
auto res = tp.Parse(argc, argv);
g_triton_params = res.first;
g_triton_params.CheckPortCollision();
}
catch (const triton::server::ParseException& pe) {
std::cerr << "Usage: tritonserver [options]" << std::endl;
std::cerr << tp.Usage() << std::endl;
// Show error at bottom for immediate visibility
std::cerr << pe.what() << std::endl;
exit(1);
}
triton::server::TritonServerParameters::ManagedTritonServerOptionPtr
triton_options(nullptr, TRITONSERVER_ServerOptionsDelete);
try {
triton_options = g_triton_params.BuildTritonServerOptions();
}
catch (const triton::server::ParseException& pe) {
std::cerr << "Failed to build Triton option:" << std::endl;
std::cerr << pe.what() << std::endl;
exit(1);
}
#ifdef TRITON_ENABLE_LOGGING
// Initialize our own logging instance since it is used by GRPC and
// HTTP endpoints. This logging instance is separate from the one in
// libtritonserver so we must initialize explicitly.
LOG_ENABLE_INFO(g_triton_params.log_info_);
LOG_ENABLE_WARNING(g_triton_params.log_warn_);
LOG_ENABLE_ERROR(g_triton_params.log_error_);
LOG_SET_VERBOSE(g_triton_params.log_verbose_);
LOG_SET_FORMAT(g_triton_params.log_format_);
LOG_SET_OUT_FILE(g_triton_params.log_file_);
#endif // TRITON_ENABLE_LOGGING
// Trace manager.
triton::server::TraceManager* trace_manager;
// Manager for shared memory blocks.
auto shm_manager = std::make_shared<triton::server::SharedMemoryManager>();
// Create the server...
TRITONSERVER_Server* server_ptr = nullptr;
FAIL_IF_ERR(
TRITONSERVER_ServerNew(&server_ptr, triton_options.get()),
"creating server");
std::shared_ptr<TRITONSERVER_Server> server(
server_ptr, TRITONSERVER_ServerDelete);
// Configure and start tracing if specified on the command line.
if (!StartTracing(&trace_manager)) {
exit(1);
}
// Trap SIGINT and SIGTERM to allow server to exit gracefully
TRITONSERVER_Error* signal_err = triton::server::RegisterSignalHandler();
if (signal_err != nullptr) {
LOG_TRITONSERVER_ERROR(signal_err, "failed to register signal handler");
exit(1);
}
// Start the HTTP, GRPC, and metrics endpoints.
if (!StartEndpoints(server, trace_manager, shm_manager)) {
exit(1);
}
// Wait until a signal terminates the server...
while (!triton::server::signal_exiting_) {
// If enabled, poll the model repository to see if there have been
// any changes.
if (g_triton_params.repository_poll_secs_ > 0) {
LOG_TRITONSERVER_ERROR(
TRITONSERVER_ServerPollModelRepository(server_ptr),
"failed to poll model repository");
}
// Wait for the polling interval (or a long time if polling is not
// enabled). Will be woken if the server is exiting.
std::unique_lock<std::mutex> lock(triton::server::signal_exit_mu_);
std::chrono::seconds wait_timeout(
(g_triton_params.repository_poll_secs_ == 0)
? 3600
: g_triton_params.repository_poll_secs_);
triton::server::signal_exit_cv_.wait_for(lock, wait_timeout);
}
// Stop the HTTP[, gRPC, and metrics] endpoints, and update exit timeout.
uint32_t exit_timeout_secs = g_triton_params.exit_timeout_secs_;
StopEndpoints(&exit_timeout_secs);
TRITONSERVER_ServerSetExitTimeout(server_ptr, exit_timeout_secs);
TRITONSERVER_Error* stop_err = TRITONSERVER_ServerStop(server_ptr);
// If unable to gracefully stop the server then Triton threads and
// state are potentially in an invalid state, so just exit
// immediately.
if (stop_err != nullptr) {
LOG_TRITONSERVER_ERROR(stop_err, "failed to stop server");
exit(1);
}
// Stop gRPC and metrics endpoints that do not yet support exit timeout.
StopEndpoints();
// Stop tracing.
StopTracing(&trace_manager);
#ifdef TRITON_ENABLE_ASAN
// Can invoke ASAN before exit though this is typically not very
// useful since there are many objects that are not yet destructed.
// __lsan_do_leak_check();
#endif // TRITON_ENABLE_ASAN
return 0;
}