TensorFlow Serving C++ API Documentation
predict_impl.cc
1 /* Copyright 2016 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/predict_impl.h"
17 
18 #include <string>
19 #include <utility>
20 
21 #include "absl/strings/substitute.h"
22 #include "tensorflow/cc/saved_model/loader.h"
23 #include "tensorflow/core/lib/core/errors.h"
24 #include "tensorflow/core/platform/threadpool_options.h"
25 #include "tensorflow_serving/core/servable_handle.h"
26 #include "tensorflow_serving/servables/tensorflow/predict_util.h"
27 #include "tensorflow_serving/servables/tensorflow/thread_pool_factory.h"
28 #include "tensorflow_serving/servables/tensorflow/util.h"
29 
30 namespace tensorflow {
31 namespace serving {
32 
33 Status TensorflowPredictor::Predict(const RunOptions& run_options,
34  ServerCore* core,
35  const PredictRequest& request,
36  PredictResponse* response) {
37  if (!request.has_model_spec()) {
38  return tensorflow::Status(
39  static_cast<absl::StatusCode>(absl::StatusCode::kInvalidArgument),
40  "Missing ModelSpec");
41  }
42  return PredictWithModelSpec(run_options, core, request.model_spec(), request,
43  response);
44 }
45 
46 Status TensorflowPredictor::PredictWithModelSpec(const RunOptions& run_options,
47  ServerCore* core,
48  const ModelSpec& model_spec,
49  const PredictRequest& request,
50  PredictResponse* response) {
51  ServableHandle<SavedModelBundle> bundle;
52  TF_RETURN_IF_ERROR(core->GetServableHandle(model_spec, &bundle));
53  return internal::RunPredict(
54  run_options, bundle->meta_graph_def, bundle.id().version,
55  core->predict_response_tensor_serialization_option(),
56  bundle->session.get(), request, response,
57  thread_pool_factory_ == nullptr
58  ? thread::ThreadPoolOptions()
59  : thread_pool_factory_->GetThreadPools().get());
60 }
61 
62 } // namespace serving
63 } // namespace tensorflow