TensorFlow Serving C++ API Documentation
server_init.h
1 /* Copyright 2022 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 THIRD_PARTY_TENSORFLOW_SERVING_MODEL_SERVERS_SERVER_INIT_H_
17 #define THIRD_PARTY_TENSORFLOW_SERVING_MODEL_SERVERS_SERVER_INIT_H_
18 
19 #include <string>
20 
21 #include "google/protobuf/any.pb.h"
22 #include "tensorflow/core/lib/core/status.h"
23 #include "tensorflow_serving/apis/prediction_service.grpc.pb.h"
24 #include "tensorflow_serving/model_servers/http_rest_api_handler_base.h"
25 #include "tensorflow_serving/model_servers/prediction_service_util.h"
26 #include "tensorflow_serving/model_servers/server_core.h"
27 #include "tensorflow_serving/servables/tensorflow/session_bundle_config.pb.h"
28 
29 namespace tensorflow {
30 namespace serving {
31 namespace init {
32 
33 using SetupPlatformConfigMapForTensorFlowFnType =
34  Status (*)(const SessionBundleConfig&, PlatformConfigMap&);
35 using UpdatePlatformConfigMapForTensorFlowFnType =
36  Status (*)(PlatformConfigMap&);
37 using CreateHttpRestApiHandlerFnType =
38  std::unique_ptr<HttpRestApiHandlerBase> (*)(int, ServerCore*);
39 using CreatePredictionServiceFnType =
40  std::unique_ptr<PredictionService::Service> (*)(
41  const PredictionServiceOptions&);
42 
43 Status SetupPlatformConfigMapForTensorFlowImpl(const SessionBundleConfig&,
44  PlatformConfigMap&);
45 Status UpdatePlatformConfigMapForTensorFlowImpl(PlatformConfigMap&);
46 std::unique_ptr<HttpRestApiHandlerBase> CreateHttpRestApiHandlerImpl(
47  int, ServerCore*);
48 std::unique_ptr<PredictionService::Service> CreatePredictionServiceImpl(
49  const PredictionServiceOptions&);
50 
51 // Register the tensorflow serving functions.
53  public:
54  virtual ~TensorflowServingFunctionRegistration() = default;
55 
56  // Get the registry singleton.
57  static TensorflowServingFunctionRegistration* GetRegistry() {
58  static auto* registration = new TensorflowServingFunctionRegistration();
59  return registration;
60  }
61 
62  // The tensorflow serving function registration. For TFRT, the TFRT
63  // registration will overwrite the Tensorflow registration.
64  void Register(
65  absl::string_view type,
66  SetupPlatformConfigMapForTensorFlowFnType setup_platform_config_map_func,
67  UpdatePlatformConfigMapForTensorFlowFnType
68  update_platform_config_map_func,
69  CreateHttpRestApiHandlerFnType create_http_rest_api_handler_func,
70  CreatePredictionServiceFnType create_prediction_service_func);
71 
72  bool IsRegistered() const { return !registration_type_.empty(); }
73 
74  SetupPlatformConfigMapForTensorFlowFnType GetSetupPlatformConfigMap() const {
75  return setup_platform_config_map_;
76  }
77 
78  UpdatePlatformConfigMapForTensorFlowFnType GetUpdatePlatformConfigMap()
79  const {
80  return update_platform_config_map_;
81  }
82 
83  CreateHttpRestApiHandlerFnType GetCreateHttpRestApiHandler() const {
84  return create_http_rest_api_handler_;
85  }
86 
87  CreatePredictionServiceFnType GetCreatePredictionService() const {
88  return create_prediction_service_;
89  }
90 
91  private:
93  Register("tensorflow", init::SetupPlatformConfigMapForTensorFlowImpl,
94  init::UpdatePlatformConfigMapForTensorFlowImpl,
95  init::CreateHttpRestApiHandlerImpl,
96  init::CreatePredictionServiceImpl);
97  }
98 
99  // The registration type, indicating the platform, e.g. tensorflow, tfrt.
100  std::string registration_type_ = "";
101 
102  // Setup the 'TensorFlow' PlatformConfigMap from the specified
103  // SessionBundleConfig.
104  SetupPlatformConfigMapForTensorFlowFnType setup_platform_config_map_;
105  // If the PlatformConfigMap contains the config for the 'TensorFlow'
106  // platform, update the PlatformConfigMap when necessary.
107  UpdatePlatformConfigMapForTensorFlowFnType update_platform_config_map_;
108  // Create an HttpRestApiHandler object that handles HTTP/REST request APIs
109  // for serving.
110  CreateHttpRestApiHandlerFnType create_http_rest_api_handler_;
111  // Create a PredictionService object that handles gRPC request APIs for
112  // serving.
113  CreatePredictionServiceFnType create_prediction_service_;
114 };
115 
116 } // namespace init
117 } // namespace serving
118 } // namespace tensorflow
119 
120 #endif // THIRD_PARTY_TENSORFLOW_SERVING_MODEL_SERVERS_SERVER_INIT_H_