Repository navigation
Expand file tree
/
Copy pathinception_inference.cc
More file actions
393 lines (340 loc) · 14.3 KB
/
Copy pathinception_inference.cc
File metadata and controls
393 lines (340 loc) · 14.3 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
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
/* Copyright 2016 Google Inc. 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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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.
==============================================================================*/
// A gRPC server that serves inception model exported by inception_export.py.
// Given each request with an image as JPEG encoded byte stream, the server
// responds with kNumTopClasses integer values as indexes of top k matched
// categories and kNumTopClasses float values as the corresponding
// probabilities.
#include <stddef.h>
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include "grpc++/completion_queue.h"
#include "grpc++/security/server_credentials.h"
#include "grpc++/server.h"
#include "grpc++/server_builder.h"
#include "grpc++/server_context.h"
#include "grpc++/support/async_unary_call.h"
#include "grpc++/support/status.h"
#include "grpc++/support/status_code_enum.h"
#include "grpc/grpc.h"
#include "tensorflow/contrib/session_bundle/manifest.pb.h"
#include "tensorflow/contrib/session_bundle/session_bundle.h"
#include "tensorflow/contrib/session_bundle/signature.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/framework/tensor_types.h"
#include "tensorflow/core/framework/types.pb.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/init_main.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow/core/util/command_line_flags.h"
#include "tensorflow_serving/batching/basic_batch_scheduler.h"
#include "tensorflow_serving/batching/batch_scheduler.h"
#include "tensorflow_serving/core/manager.h"
#include "tensorflow_serving/core/servable_handle.h"
#include "tensorflow_serving/core/servable_id.h"
#include "tensorflow_serving/example/inception_inference.grpc.pb.h"
#include "tensorflow_serving/example/inception_inference.pb.h"
#include "tensorflow_serving/servables/tensorflow/simple_servers.h"
using grpc::InsecureServerCredentials;
using grpc::Server;
using grpc::ServerAsyncResponseWriter;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::ServerCompletionQueue;
using grpc::Status;
using grpc::StatusCode;
using tensorflow::serving::InceptionRequest;
using tensorflow::serving::InceptionResponse;
using tensorflow::serving::InceptionService;
using tensorflow::string;
using tensorflow::Tensor;
using tensorflow::serving::ClassificationSignature;
const int kNumTopClasses = 5;
namespace {
class InceptionServiceImpl;
// Class encompassing the state and logic needed to serve a request.
class CallData {
public:
CallData(InceptionServiceImpl* service_impl,
InceptionService::AsyncService* service,
ServerCompletionQueue* cq);
void Proceed();
void Finish(Status status);
const InceptionRequest& request() { return request_; }
InceptionResponse* mutable_response() { return &response_; }
private:
// Service implementation.
InceptionServiceImpl* service_impl_;
// The means of communication with the gRPC runtime for an asynchronous
// server.
InceptionService::AsyncService* service_;
// The producer-consumer queue where for asynchronous server notifications.
ServerCompletionQueue* cq_;
// Context for the rpc, allowing to tweak aspects of it such as the use
// of compression, authentication, as well as to send metadata back to the
// client.
ServerContext ctx_;
// What we get from the client.
InceptionRequest request_;
// What we send back to the client.
InceptionResponse response_;
// The means to get back to the client.
ServerAsyncResponseWriter<InceptionResponse> responder_;
// Let's implement a tiny state machine with the following states.
enum CallStatus { CREATE, PROCESS, FINISH };
CallStatus status_; // The current serving state.
};
// A Task holds all of the information for a single inference request.
struct Task : public tensorflow::serving::BatchTask {
~Task() override = default;
size_t size() const override { return 1; }
Task(CallData* calldata_arg)
: calldata(calldata_arg) {}
CallData* calldata;
};
class InceptionServiceImpl final {
public:
InceptionServiceImpl(const string& servable_name,
std::unique_ptr<tensorflow::serving::Manager> manager);
void Classify(CallData* call_data);
// Produces classifications for a batch of requests and associated responses.
void DoClassifyInBatch(
std::unique_ptr<tensorflow::serving::Batch<Task>> batch);
// Name of the servable to use for inference.
const string servable_name_;
// Manager in charge of loading and unloading servables.
std::unique_ptr<tensorflow::serving::Manager> manager_;
// A scheduler for batching multiple request calls into single calls to
// Session->Run().
std::unique_ptr<tensorflow::serving::BasicBatchScheduler<Task>>
batch_scheduler_;
};
// Take in the "service" instance (in this case representing an asynchronous
// server) and the completion queue "cq" used for asynchronous communication
// with the gRPC runtime.
CallData::CallData(InceptionServiceImpl* service_impl,
InceptionService::AsyncService* service,
ServerCompletionQueue* cq)
: service_impl_(service_impl),
service_(service), cq_(cq), responder_(&ctx_), status_(CREATE) {
// Invoke the serving logic right away.
Proceed();
}
void CallData::Proceed() {
if (status_ == CREATE) {
// As part of the initial CREATE state, we *request* that the system
// start processing Classify requests. In this request, "this" acts are
// the tag uniquely identifying the request (so that different CallData
// instances can serve different requests concurrently), in this case
// the memory address of this CallData instance.
service_->RequestClassify(&ctx_, &request_, &responder_, cq_, cq_, this);
// Make this instance progress to the PROCESS state.
status_ = PROCESS;
} else if (status_ == PROCESS) {
// Spawn a new CallData instance to serve new clients while we process
// the one for this CallData. The instance will deallocate itself as
// part of its FINISH state.
new CallData(service_impl_, service_, cq_);
// Start processing.
service_impl_->Classify(this);
} else {
GPR_ASSERT(status_ == FINISH);
// Once in the FINISH state, deallocate ourselves (CallData).
delete this;
}
}
void CallData::Finish(Status status) {
status_ = FINISH;
responder_.Finish(response_, status, this);
}
InceptionServiceImpl::InceptionServiceImpl(
const string& servable_name,
std::unique_ptr<tensorflow::serving::Manager> manager)
: servable_name_(servable_name), manager_(std::move(manager)) {
// Setup a batcher used to combine multiple requests (tasks) into a single
// graph run for efficiency.
// The batcher queues tasks until,
// (a) the next task would cause the batch to exceed the size target;
// (b) waiting for more tasks to be added would exceed the timeout.
// at which point it processes the entire batch.
//
// Use the default batch-size, timeout and thread options. In general
// the numbers are extremely performance critical and should be tuned based
// specific graph structure and usage.
tensorflow::serving::BasicBatchScheduler<Task>::Options scheduler_options;
scheduler_options.thread_pool_name = "inception_service_batch_threads";
// Use a very large queue, to avoid rejecting requests. (Note: a production
// server with load balancing may want to use the default, much smaller,
// value.)
scheduler_options.max_enqueued_batches = 1000;
scheduler_options.max_batch_size = 10;
TF_CHECK_OK(tensorflow::serving::BasicBatchScheduler<Task>::Create(
scheduler_options,
[this](std::unique_ptr<tensorflow::serving::Batch<Task>> batch) {
this->DoClassifyInBatch(std::move(batch));
},
&batch_scheduler_));
}
// Creates a gRPC Status from a TensorFlow Status.
Status ToGRPCStatus(const tensorflow::Status& status) {
return Status(static_cast<grpc::StatusCode>(status.code()),
status.error_message());
}
void InceptionServiceImpl::Classify(CallData* calldata) {
// Create and submit a task to the batch scheduler.
std::unique_ptr<Task> task(new Task(calldata));
tensorflow::Status status = batch_scheduler_->Schedule(&task);
if (!status.ok()) {
calldata->Finish(ToGRPCStatus(status));
return;
}
}
// Produces classifications for a batch of requests and associated responses.
void InceptionServiceImpl::DoClassifyInBatch(
std::unique_ptr<tensorflow::serving::Batch<Task>> batch) {
batch->WaitUntilClosed();
if (batch->empty()) {
return;
}
const int batch_size = batch->num_tasks();
// Replies to each task with the given error status.
auto complete_with_error = [&batch](StatusCode code, const string& msg) {
Status status(code, msg);
for (int i = 0; i < batch->num_tasks(); i++) {
Task* task = batch->mutable_task(i);
task->calldata->Finish(status);
}
};
// Get a handle to the SessionBundle. The handle ensures the Manager does
// not reload this while it is in use.
auto handle_request =
tensorflow::serving::ServableRequest::Latest(servable_name_);
tensorflow::serving::ServableHandle<tensorflow::serving::SessionBundle>
bundle;
const tensorflow::Status lookup_status =
manager_->GetServableHandle(handle_request, &bundle);
if (!lookup_status.ok()) {
complete_with_error(StatusCode::INTERNAL,
lookup_status.error_message());
return;
}
// Get the default signature of the graph. Expected to be a
// classification signature.
tensorflow::serving::ClassificationSignature signature;
const tensorflow::Status signature_status =
GetClassificationSignature(bundle->meta_graph_def, &signature);
if (!signature_status.ok()) {
complete_with_error(StatusCode::INTERNAL,
signature_status.error_message());
return;
}
// Transform protobuf input to inference input tensor.
tensorflow::Tensor batched_input(tensorflow::DT_STRING, {batch_size});
for (int i = 0; i < batch_size; ++i) {
batched_input.vec<string>()(i) =
batch->mutable_task(i)->calldata->request().jpeg_encoded();
}
// Run classification.
tensorflow::Tensor batched_classes;
tensorflow::Tensor batched_scores;
const tensorflow::Status run_status =
RunClassification(signature, batched_input, bundle->session.get(),
&batched_classes, &batched_scores);
if (!run_status.ok()) {
complete_with_error(StatusCode::INTERNAL, run_status.error_message());
return;
}
// Transform inference output tensor to protobuf output.
for (int i = 0; i < batch_size; ++i) {
auto calldata = batch->mutable_task(i)->calldata;
auto classes = calldata->mutable_response()->mutable_classes();
auto scores = calldata->mutable_response()->mutable_scores();
for (int j = 0; j < kNumTopClasses; j++) {
*classes->Add() = batched_classes.matrix<string>()(i, j);
scores->Add(batched_scores.matrix<float>()(i, j));
}
calldata->Finish(Status::OK);
}
}
void HandleRpcs(InceptionServiceImpl* service_impl,
InceptionService::AsyncService* service,
ServerCompletionQueue* cq) {
// Spawn a new CallData instance to serve new clients.
new CallData(service_impl, service, cq);
void* tag; // uniquely identifies a request.
bool ok = false;
while (true) {
// Block waiting to read the next event from the completion queue. The
// event is uniquely identified by its tag, which in this case is the
// memory address of a CallData instance.
if (!cq->Next(&tag, &ok)) {
break; // server shutting down
}
if (!ok) {
continue; // irregular event
}
static_cast<CallData*>(tag)->Proceed();
}
}
// Runs InceptionService server until shutdown.
void RunServer(const int port, const string& servable_name,
std::unique_ptr<tensorflow::serving::Manager> manager) {
// "0.0.0.0" is the way to listen on localhost in gRPC.
const string server_address = "0.0.0.0:" + std::to_string(port);
InceptionService::AsyncService service;
ServerBuilder builder;
std::shared_ptr<grpc::ServerCredentials> creds = InsecureServerCredentials();
builder.AddListeningPort(server_address, creds);
builder.RegisterService(&service);
std::unique_ptr<ServerCompletionQueue> cq = builder.AddCompletionQueue();
std::unique_ptr<Server> server(builder.BuildAndStart());
LOG(INFO) << "Running...";
InceptionServiceImpl service_impl(servable_name, std::move(manager));
HandleRpcs(&service_impl, &service, cq.get());
}
} // namespace
int main(int argc, char** argv) {
// Parse command-line options.
tensorflow::int32 port = 0;
const bool parse_result =
tensorflow::ParseFlags(&argc, argv, {tensorflow::Flag("port", &port)});
if (!parse_result) {
LOG(FATAL) << "Error parsing command line flags.";
}
if (argc != 2) {
LOG(ERROR) << "Usage: inception_inference --port=9000 /path/to/exports";
return -1;
}
const string export_base_path(argv[1]);
tensorflow::port::InitMain(argv[0], &argc, &argv);
std::unique_ptr<tensorflow::serving::Manager> manager;
tensorflow::Status status = tensorflow::serving::simple_servers::
CreateSingleTFModelManagerFromBasePath(export_base_path, &manager);
TF_CHECK_OK(status) << "Error creating manager";
// Wait until at least one model is loaded.
std::vector<tensorflow::serving::ServableId> ready_ids;
// TODO(b/25545573): Create a more streamlined startup mechanism than polling.
do {
LOG(INFO) << "Waiting for models to be loaded...";
tensorflow::Env::Default()->SleepForMicroseconds(1 * 1000 * 1000 /*1 sec*/);
ready_ids = manager->ListAvailableServableIds();
} while (ready_ids.empty());
// Run the service.
RunServer(port, ready_ids[0].name, std::move(manager));
return 0;
}