-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
TritonService.cc
393 lines (350 loc) · 15.2 KB
/
TritonService.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
#include "HeterogeneousCore/SonicTriton/interface/TritonService.h"
#include "HeterogeneousCore/SonicTriton/interface/triton_utils.h"
#include "DataFormats/Provenance/interface/ModuleDescription.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
#include "FWCore/ServiceRegistry/interface/SystemBounds.h"
#include "FWCore/ServiceRegistry/interface/ProcessContext.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "grpc_client.h"
#include "grpc_service.pb.h"
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <utility>
#include <tuple>
#include <unistd.h>
namespace tc = triton::client;
const std::string TritonService::Server::fallbackName{"fallback"};
const std::string TritonService::Server::fallbackAddress{"0.0.0.0"};
namespace {
std::pair<std::string, int> execSys(const std::string& cmd) {
//redirect stderr to stdout
auto pipe = popen((cmd + " 2>&1").c_str(), "r");
int thisErrno = errno;
if (!pipe)
throw cms::Exception("SystemError")
<< "TritonService: popen() failed with errno " << thisErrno << " for command: " << cmd;
//extract output
constexpr static unsigned buffSize = 128;
std::array<char, buffSize> buffer;
std::string result;
while (!feof(pipe)) {
if (fgets(buffer.data(), buffSize, pipe))
result += buffer.data();
else {
thisErrno = ferror(pipe);
if (thisErrno)
throw cms::Exception("SystemError")
<< "TritonService: failed reading command output with errno " << thisErrno;
}
}
int rv = pclose(pipe);
return std::make_pair(result, rv);
}
} // namespace
TritonService::TritonService(const edm::ParameterSet& pset, edm::ActivityRegistry& areg)
: verbose_(pset.getUntrackedParameter<bool>("verbose")),
fallbackOpts_(pset.getParameterSet("fallback")),
currentModuleId_(0),
allowAddModel_(false),
startedFallback_(false),
callFails_(0),
pid_(std::to_string(::getpid())) {
//module construction is assumed to be serial (correct at the time this code was written)
areg.watchPreallocate(this, &TritonService::preallocate);
areg.watchPreModuleConstruction(this, &TritonService::preModuleConstruction);
areg.watchPostModuleConstruction(this, &TritonService::postModuleConstruction);
areg.watchPreModuleDestruction(this, &TritonService::preModuleDestruction);
//fallback server will be launched (if needed) before beginJob
areg.watchPreBeginJob(this, &TritonService::preBeginJob);
areg.watchPostEndJob(this, &TritonService::postEndJob);
//include fallback server in set if enabled
if (fallbackOpts_.enable) {
auto serverType = TritonServerType::Remote;
if (!fallbackOpts_.useGPU)
serverType = TritonServerType::LocalCPU;
#ifdef TRITON_ENABLE_GPU
else
serverType = TritonServerType::LocalGPU;
#endif
servers_.emplace(std::piecewise_construct,
std::forward_as_tuple(Server::fallbackName),
std::forward_as_tuple(Server::fallbackName, Server::fallbackAddress, serverType));
}
//loop over input servers: check which models they have
std::string msg;
if (verbose_)
msg = "List of models for each server:\n";
for (const auto& serverPset : pset.getUntrackedParameterSetVector("servers")) {
const std::string& serverName(serverPset.getUntrackedParameter<std::string>("name"));
//ensure uniqueness
auto [sit, unique] = servers_.emplace(serverName, serverPset);
if (!unique)
throw cms::Exception("DuplicateServer")
<< "TritonService: Not allowed to specify more than one server with same name (" << serverName << ")";
auto& server(sit->second);
std::unique_ptr<tc::InferenceServerGrpcClient> client;
TRITON_THROW_IF_ERROR(
tc::InferenceServerGrpcClient::Create(&client, server.url, false, server.useSsl, server.sslOptions),
"TritonService(): unable to create inference context for " + serverName + " (" + server.url + ")",
false);
if (verbose_) {
inference::ServerMetadataResponse serverMetaResponse;
TRITON_THROW_IF_ERROR(client->ServerMetadata(&serverMetaResponse),
"TritonService(): unable to get metadata for " + serverName + " (" + server.url + ")",
false);
edm::LogInfo("TritonService") << "Server " << serverName << ": url = " << server.url
<< ", version = " << serverMetaResponse.version();
}
inference::RepositoryIndexResponse repoIndexResponse;
TRITON_THROW_IF_ERROR(client->ModelRepositoryIndex(&repoIndexResponse),
"TritonService(): unable to get repository index for " + serverName + " (" + server.url + ")",
false);
//servers keep track of models and vice versa
if (verbose_)
msg += serverName + ": ";
for (const auto& modelIndex : repoIndexResponse.models()) {
const auto& modelName = modelIndex.name();
auto mit = models_.find(modelName);
if (mit == models_.end())
mit = models_.emplace(modelName, "").first;
auto& modelInfo(mit->second);
modelInfo.servers.insert(serverName);
server.models.insert(modelName);
if (verbose_)
msg += modelName + ", ";
}
if (verbose_)
msg += "\n";
}
if (verbose_)
edm::LogInfo("TritonService") << msg;
}
void TritonService::preallocate(edm::service::SystemBounds const& bounds) {
numberOfThreads_ = bounds.maxNumberOfThreads();
}
void TritonService::preModuleConstruction(edm::ModuleDescription const& desc) {
currentModuleId_ = desc.id();
allowAddModel_ = true;
}
void TritonService::addModel(const std::string& modelName, const std::string& path) {
//should only be called in module constructors
if (!allowAddModel_)
throw cms::Exception("DisallowedAddModel")
<< "TritonService: Attempt to call addModel() outside of module constructors";
//if model is not in the list, then no specified server provides it
auto mit = models_.find(modelName);
if (mit == models_.end()) {
auto& modelInfo(unservedModels_.emplace(modelName, path).first->second);
modelInfo.modules.insert(currentModuleId_);
//only keep track of modules that need unserved models
modules_.emplace(currentModuleId_, modelName);
}
}
void TritonService::postModuleConstruction(edm::ModuleDescription const& desc) { allowAddModel_ = false; }
void TritonService::preModuleDestruction(edm::ModuleDescription const& desc) {
//remove destructed modules from unserved list
if (unservedModels_.empty())
return;
auto id = desc.id();
auto oit = modules_.find(id);
if (oit != modules_.end()) {
const auto& moduleInfo(oit->second);
auto mit = unservedModels_.find(moduleInfo.model);
if (mit != unservedModels_.end()) {
auto& modelInfo(mit->second);
modelInfo.modules.erase(id);
//remove a model if it is no longer needed by any modules
if (modelInfo.modules.empty())
unservedModels_.erase(mit);
}
modules_.erase(oit);
}
}
//second return value is only true if fallback CPU server is being used
TritonService::Server TritonService::serverInfo(const std::string& model, const std::string& preferred) const {
auto mit = models_.find(model);
if (mit == models_.end())
throw cms::Exception("MissingModel") << "TritonService: There are no servers that provide model " << model;
const auto& modelInfo(mit->second);
const auto& modelServers = modelInfo.servers;
auto msit = modelServers.end();
if (!preferred.empty()) {
msit = modelServers.find(preferred);
//todo: add a "strict" parameter to stop execution if preferred server isn't found?
if (msit == modelServers.end())
edm::LogWarning("PreferredServer") << "Preferred server " << preferred << " for model " << model
<< " not available, will choose another server";
}
const auto& serverName(msit == modelServers.end() ? *modelServers.begin() : preferred);
//todo: use some algorithm to select server rather than just picking arbitrarily
const auto& server(servers_.find(serverName)->second);
return server;
}
void TritonService::preBeginJob(edm::PathsAndConsumesOfModulesBase const&, edm::ProcessContext const&) {
//only need fallback if there are unserved models
if (!fallbackOpts_.enable or unservedModels_.empty())
return;
std::string msg;
if (verbose_)
msg = "List of models for fallback server: ";
//all unserved models are provided by fallback server
auto& server(servers_.find(Server::fallbackName)->second);
for (const auto& [modelName, model] : unservedModels_) {
auto& modelInfo(models_.emplace(modelName, model).first->second);
modelInfo.servers.insert(Server::fallbackName);
server.models.insert(modelName);
if (verbose_)
msg += modelName + ", ";
}
if (verbose_)
edm::LogInfo("TritonService") << msg;
//assemble server start command
fallbackOpts_.command = "cmsTriton -P -1 -p " + pid_;
if (fallbackOpts_.debug)
fallbackOpts_.command += " -c";
if (fallbackOpts_.verbose)
fallbackOpts_.command += " -v";
if (fallbackOpts_.useDocker)
fallbackOpts_.command += " -d";
if (fallbackOpts_.useGPU)
fallbackOpts_.command += " -g";
if (!fallbackOpts_.instanceName.empty())
fallbackOpts_.command += " -n " + fallbackOpts_.instanceName;
if (fallbackOpts_.retries >= 0)
fallbackOpts_.command += " -r " + std::to_string(fallbackOpts_.retries);
if (fallbackOpts_.wait >= 0)
fallbackOpts_.command += " -w " + std::to_string(fallbackOpts_.wait);
for (const auto& [modelName, model] : unservedModels_) {
fallbackOpts_.command += " -m " + model.path;
}
std::string thread_string = " -I " + std::to_string(numberOfThreads_);
fallbackOpts_.command += thread_string;
if (!fallbackOpts_.imageName.empty())
fallbackOpts_.command += " -i " + fallbackOpts_.imageName;
if (!fallbackOpts_.sandboxName.empty())
fallbackOpts_.command += " -s " + fallbackOpts_.sandboxName;
//don't need this anymore
unservedModels_.clear();
//get a random temporary directory if none specified
if (fallbackOpts_.tempDir.empty()) {
auto tmp_dir_path{std::filesystem::temp_directory_path() /= edm::createGlobalIdentifier()};
fallbackOpts_.tempDir = tmp_dir_path.string();
}
//special case ".": use script default (temp dir = .$instanceName)
if (fallbackOpts_.tempDir != ".")
fallbackOpts_.command += " -t " + fallbackOpts_.tempDir;
std::string command = fallbackOpts_.command + " start";
if (fallbackOpts_.debug)
edm::LogInfo("TritonService") << "Fallback server temporary directory: " << fallbackOpts_.tempDir;
if (verbose_)
edm::LogInfo("TritonService") << command;
//mark as started before executing in case of ctrl+c while command is running
startedFallback_ = true;
const auto& [output, rv] = execSys(command);
if (rv != 0) {
edm::LogError("TritonService") << output;
printFallbackServerLog<edm::LogError>();
throw cms::Exception("FallbackFailed")
<< "TritonService: Starting the fallback server failed with exit code " << rv;
} else if (verbose_)
edm::LogInfo("TritonService") << output;
//get the port
const std::string& portIndicator("CMS_TRITON_GRPC_PORT: ");
//find last instance in log in case multiple ports were tried
auto pos = output.rfind(portIndicator);
if (pos != std::string::npos) {
auto pos2 = pos + portIndicator.size();
auto pos3 = output.find('\n', pos2);
const auto& portNum = output.substr(pos2, pos3 - pos2);
server.url += ":" + portNum;
} else
throw cms::Exception("FallbackFailed") << "TritonService: Unknown port for fallback server, log follows:\n"
<< output;
}
void TritonService::notifyCallStatus(bool status) const {
if (status)
--callFails_;
else
++callFails_;
}
void TritonService::postEndJob() {
if (!startedFallback_)
return;
std::string command = fallbackOpts_.command;
//prevent log cleanup during server stop
if (callFails_ > 0)
command += " -c";
command += " stop";
if (verbose_)
edm::LogInfo("TritonService") << command;
const auto& [output, rv] = execSys(command);
if (rv != 0 or callFails_ > 0) {
//print logs if cmsRun is currently exiting because of a TritonException
edm::LogError("TritonService") << output;
printFallbackServerLog<edm::LogError>();
if (rv != 0) {
std::string stopCat("FallbackFailed");
std::string stopMsg = fmt::format("TritonService: Stopping the fallback server failed with exit code {}", rv);
//avoid throwing if the stack is already unwinding
if (callFails_ > 0)
edm::LogWarning(stopCat) << stopMsg;
else
throw cms::Exception(stopCat) << stopMsg;
}
} else if (verbose_) {
edm::LogInfo("TritonService") << output;
printFallbackServerLog<edm::LogInfo>();
}
}
template <typename LOG>
void TritonService::printFallbackServerLog() const {
std::vector<std::string> logNames{"log_" + fallbackOpts_.instanceName + ".log"};
//cmsTriton script moves log from temp to current dir in verbose mode or in some cases when auto_stop is called
// -> check both places
logNames.push_back(fallbackOpts_.tempDir + "/" + logNames[0]);
bool foundLog = false;
for (const auto& logName : logNames) {
std::ifstream infile(logName);
if (infile.is_open()) {
LOG("TritonService") << "TritonService: server log " << logName << "\n" << infile.rdbuf();
foundLog = true;
break;
}
}
if (!foundLog)
LOG("TritonService") << "TritonService: could not find server log " << logNames[0] << " in current directory or "
<< fallbackOpts_.tempDir;
}
void TritonService::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.addUntracked<bool>("verbose", false);
edm::ParameterSetDescription validator;
validator.addUntracked<std::string>("name");
validator.addUntracked<std::string>("address");
validator.addUntracked<unsigned>("port");
validator.addUntracked<bool>("useSsl", false);
validator.addUntracked<std::string>("rootCertificates", "");
validator.addUntracked<std::string>("privateKey", "");
validator.addUntracked<std::string>("certificateChain", "");
desc.addVPSetUntracked("servers", validator, {});
edm::ParameterSetDescription fallbackDesc;
fallbackDesc.addUntracked<bool>("enable", false);
fallbackDesc.addUntracked<bool>("debug", false);
fallbackDesc.addUntracked<bool>("verbose", false);
fallbackDesc.addUntracked<bool>("useDocker", false);
fallbackDesc.addUntracked<bool>("useGPU", false);
fallbackDesc.addUntracked<int>("retries", -1);
fallbackDesc.addUntracked<int>("wait", -1);
fallbackDesc.addUntracked<std::string>("instanceBaseName", "triton_server_instance");
fallbackDesc.addUntracked<std::string>("instanceName", "");
fallbackDesc.addUntracked<std::string>("tempDir", "");
fallbackDesc.addUntracked<std::string>("imageName", "");
fallbackDesc.addUntracked<std::string>("sandboxName", "");
desc.add<edm::ParameterSetDescription>("fallback", fallbackDesc);
descriptions.addWithDefaultLabel(desc);
}