Skip to content

Commit 3a628c1

Browse files
[ML] Write actual thread settings after pytorch process starts (#2159)
When the `pytorch_inference` process starts, we validate the given thread settings and adjust them if necessary. This commit changes the process to write the actual thread settings back to ES.
1 parent ecb9c6c commit 3a628c1

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

bin/pytorch_inference/Main.cc

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@
4040
#include <string>
4141

4242
namespace {
43+
const std::string RESULT{"result"};
4344
const std::string INFERENCE{"inference"};
4445
const std::string ERROR{"error"};
4546
const std::string TIME_MS{"time_ms"};
47+
const std::string THREAD_SETTINGS{"thread_settings"};
48+
const std::string INFERENCE_THREADS{"inference_threads"};
49+
const std::string MODEL_THREADS{"model_threads"};
4650
}
4751

4852
torch::Tensor infer(torch::jit::script::Module& module,
@@ -116,17 +120,22 @@ void writeError(const std::string& requestId,
116120
const std::string& message,
117121
ml::core::CRapidJsonConcurrentLineWriter& jsonWriter) {
118122
jsonWriter.StartObject();
123+
jsonWriter.Key(RESULT);
124+
jsonWriter.StartObject();
119125
jsonWriter.Key(ml::torch::CCommandParser::REQUEST_ID);
120126
jsonWriter.String(requestId);
121127
jsonWriter.Key(ERROR);
122128
jsonWriter.String(message);
123129
jsonWriter.EndObject();
130+
jsonWriter.EndObject();
124131
}
125132

126133
void writeDocumentOpening(const std::string& requestId,
127134
std::uint64_t timeMs,
128135
ml::core::CRapidJsonConcurrentLineWriter& jsonWriter) {
129136
jsonWriter.StartObject();
137+
jsonWriter.Key(RESULT);
138+
jsonWriter.StartObject();
130139
jsonWriter.Key(ml::torch::CCommandParser::REQUEST_ID);
131140
jsonWriter.String(requestId);
132141
jsonWriter.Key(TIME_MS);
@@ -135,6 +144,22 @@ void writeDocumentOpening(const std::string& requestId,
135144

136145
void writeDocumentClosing(ml::core::CRapidJsonConcurrentLineWriter& jsonWriter) {
137146
jsonWriter.EndObject();
147+
jsonWriter.EndObject();
148+
}
149+
150+
void writeThreadSettings(ml::core::CJsonOutputStreamWrapper& wrappedOutputStream,
151+
std::int32_t inferenceThreads,
152+
std::int32_t modelThreads) {
153+
ml::core::CRapidJsonConcurrentLineWriter jsonWriter(wrappedOutputStream);
154+
jsonWriter.StartObject();
155+
jsonWriter.Key(THREAD_SETTINGS);
156+
jsonWriter.StartObject();
157+
jsonWriter.Key(INFERENCE_THREADS);
158+
jsonWriter.Uint64(inferenceThreads);
159+
jsonWriter.Key(MODEL_THREADS);
160+
jsonWriter.Uint64(modelThreads);
161+
jsonWriter.EndObject();
162+
jsonWriter.EndObject();
138163
}
139164

140165
template<std::size_t N>
@@ -306,6 +331,10 @@ int main(int argc, char** argv) {
306331
LOG_DEBUG(<< at::get_parallel_info());
307332
LOG_DEBUG(<< "Model threads: " << modelThreads);
308333

334+
ml::core::CJsonOutputStreamWrapper wrappedOutputStream{ioMgr.outputStream()};
335+
336+
writeThreadSettings(wrappedOutputStream, inferenceThreads, modelThreads);
337+
309338
torch::jit::script::Module module;
310339
try {
311340
auto readAdapter = std::make_unique<ml::torch::CBufferedIStreamAdapter>(
@@ -324,8 +353,6 @@ int main(int argc, char** argv) {
324353

325354
ml::torch::CCommandParser commandParser{ioMgr.inputStream()};
326355

327-
ml::core::CJsonOutputStreamWrapper wrappedOutputStream{ioMgr.outputStream()};
328-
329356
// Starting the executor with 1 thread will use an extra thread that isn't necessary
330357
// so we only start it when more than 1 threads are set.
331358
if (modelThreads > 1) {

0 commit comments

Comments
 (0)