TensorFlow Serving C++ API Documentation
multi_inference_helper.cc
1 /* Copyright 2018 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow_serving/servables/tensorflow/multi_inference_helper.h"
17 
18 #include "tensorflow/cc/saved_model/loader.h"
19 #include "tensorflow_serving/apis/input.pb.h"
20 #include "tensorflow_serving/apis/model.pb.h"
21 #include "tensorflow_serving/servables/tensorflow/multi_inference.h"
22 
23 namespace tensorflow {
24 namespace serving {
25 
26 namespace {
27 
28 const ModelSpec& GetModelSpecFromRequest(const MultiInferenceRequest& request) {
29  if (request.tasks_size() > 0 && request.tasks(0).has_model_spec()) {
30  return request.tasks(0).model_spec();
31  }
32  return ModelSpec::default_instance();
33 }
34 
35 } // namespace
36 
37 Status RunMultiInferenceWithServerCore(
38  const RunOptions& run_options, ServerCore* core,
39  const tensorflow::thread::ThreadPoolOptions& thread_pool_options,
40  const MultiInferenceRequest& request, MultiInferenceResponse* response) {
41  return RunMultiInferenceWithServerCoreWithModelSpec(
42  run_options, core, thread_pool_options, GetModelSpecFromRequest(request),
43  request, response);
44 }
45 
46 Status RunMultiInferenceWithServerCoreWithModelSpec(
47  const RunOptions& run_options, ServerCore* core,
48  const tensorflow::thread::ThreadPoolOptions& thread_pool_options,
49  const ModelSpec& model_spec, const MultiInferenceRequest& request,
50  MultiInferenceResponse* response) {
51  ServableHandle<SavedModelBundle> bundle;
52  TF_RETURN_IF_ERROR(core->GetServableHandle(model_spec, &bundle));
53 
54  return RunMultiInference(run_options, bundle->meta_graph_def,
55  bundle.id().version, bundle->session.get(), request,
56  response, thread_pool_options);
57 }
58 
59 } // namespace serving
60 } // namespace tensorflow