TensorFlow Serving C++ API Documentation
multi_inference.h
1 /* Copyright 2017 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 #ifndef TENSORFLOW_SERVING_SERVABLES_TENSORFLOW_MULTI_INFERENCE_H_
17 #define TENSORFLOW_SERVING_SERVABLES_TENSORFLOW_MULTI_INFERENCE_H_
18 
19 #include "absl/types/optional.h"
20 #include "tensorflow/core/lib/core/status.h"
21 #include "tensorflow/core/platform/threadpool_options.h"
22 #include "tensorflow/core/protobuf/meta_graph.pb.h"
23 #include "tensorflow/core/public/session.h"
24 #include "tensorflow_serving/apis/inference.pb.h"
25 
26 namespace tensorflow {
27 namespace serving {
28 
29 // TensorFlow implementation of the MultiInference.
30 // Only supports Models in the SavedModel format.
32  public:
33  TensorFlowMultiInferenceRunner(Session* session,
34  const MetaGraphDef* meta_graph_def)
35  : TensorFlowMultiInferenceRunner(session, meta_graph_def,
36  /*servable_version=*/{}) {}
37 
39  Session* session, const MetaGraphDef* meta_graph_def,
40  absl::optional<int64_t> servable_version,
41  const thread::ThreadPoolOptions& thread_pool_options =
42  thread::ThreadPoolOptions())
43  : session_(session),
44  meta_graph_def_(meta_graph_def),
45  servable_version_(servable_version),
46  thread_pool_options_(thread_pool_options) {}
47 
48  // Run inference and return the inference results in the same order as the
49  // InferenceTasks in the request.
50  Status Infer(const RunOptions& run_options,
51  const MultiInferenceRequest& request,
52  MultiInferenceResponse* response);
53 
54  virtual ~TensorFlowMultiInferenceRunner() = default;
55 
56  private:
57  Session* const session_;
58  const MetaGraphDef* const meta_graph_def_;
59  // If available, servable_version is used to set the ModelSpec version in the
60  // InferenceResults of the MultiInferenceResponse.
61  const absl::optional<int64_t> servable_version_;
62  const tensorflow::thread::ThreadPoolOptions thread_pool_options_;
63 };
64 
65 // Creates TensorFlowMultiInferenceRunner and calls Infer on it.
66 Status RunMultiInference(
67  const RunOptions& run_options, const MetaGraphDef& meta_graph_def,
68  const absl::optional<int64_t>& servable_version, Session* session,
69  const MultiInferenceRequest& request, MultiInferenceResponse* response,
70  const tensorflow::thread::ThreadPoolOptions& thread_pool_options =
71  tensorflow::thread::ThreadPoolOptions());
72 
73 } // namespace serving
74 } // namespace tensorflow
75 
76 #endif // TENSORFLOW_SERVING_SERVABLES_TENSORFLOW_MULTI_INFERENCE_H_