TensorFlow Serving C++ API Documentation
simple_servers.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/simple_servers.h"
17 
18 #include <algorithm>
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "tensorflow/core/lib/core/errors.h"
24 #include "tensorflow/core/lib/core/status.h"
25 #include "tensorflow_serving/config/file_system_storage_path_source.pb.h"
26 #include "tensorflow_serving/core/aspired_versions_manager_builder.h"
27 #include "tensorflow_serving/core/availability_preserving_policy.h"
28 #include "tensorflow_serving/core/loader.h"
29 #include "tensorflow_serving/core/source.h"
30 #include "tensorflow_serving/core/source_adapter.h"
31 #include "tensorflow_serving/core/storage_path.h"
32 #include "tensorflow_serving/core/target.h"
33 #include "tensorflow_serving/servables/tensorflow/saved_model_bundle_source_adapter.h"
34 #include "tensorflow_serving/servables/tensorflow/saved_model_bundle_source_adapter.pb.h"
35 #include "tensorflow_serving/sources/storage_path/file_system_storage_path_source.h"
36 
37 namespace tensorflow {
38 namespace serving {
39 namespace simple_servers {
40 
41 namespace {
42 
43 // Creates a Source<StoragePath> that monitors a filesystem's base_path for new
44 // directories. Upon finding these, it provides the target with the new version
45 // (a directory). The servable_name param simply allows this source to create
46 // all AspiredVersions for the target with the same servable_name.
47 Status CreateStoragePathSource(
48  const string& base_path, const string& servable_name,
49  std::unique_ptr<Source<StoragePath>>* path_source) {
50  FileSystemStoragePathSourceConfig config;
51  config.set_file_system_poll_wait_seconds(1);
52  auto* servable = config.add_servables();
53  servable->set_servable_name(servable_name);
54  servable->set_base_path(base_path);
55 
56  std::unique_ptr<FileSystemStoragePathSource> file_system_source;
57  TF_RETURN_IF_ERROR(
58  FileSystemStoragePathSource::Create(config, &file_system_source));
59 
60  *path_source = std::move(file_system_source);
61  return absl::OkStatus();
62 }
63 
64 // Creates a SavedModelBundle Source by adapting the underlying
65 // FileSystemStoragePathSource. These two are connected in the
66 // 'CreateSingleTFModelManagerFromBasePath' method, with the
67 // FileSystemStoragePathSource as the Source and the SavedModelBundleSource as
68 // the Target.
69 Status CreateSavedModelBundleSource(
70  std::unique_ptr<SavedModelBundleSourceAdapter>* source) {
71  SavedModelBundleSourceAdapterConfig config;
72  TF_RETURN_IF_ERROR(SavedModelBundleSourceAdapter::Create(config, source));
73 
74  return absl::OkStatus();
75 }
76 
77 } // namespace
78 
79 Status CreateSingleTFModelManagerFromBasePath(
80  const string& base_path, std::unique_ptr<Manager>* const manager) {
81  std::unique_ptr<SavedModelBundleSourceAdapter> bundle_source;
82  TF_RETURN_IF_ERROR(CreateSavedModelBundleSource(&bundle_source));
83  std::unique_ptr<Source<StoragePath>> path_source;
84  TF_RETURN_IF_ERROR(
85  CreateStoragePathSource(base_path, "default", &path_source));
86 
87  AspiredVersionsManagerBuilder::Options manager_options;
88  manager_options.aspired_version_policy.reset(
89  new AvailabilityPreservingPolicy);
90  std::unique_ptr<AspiredVersionsManagerBuilder> builder;
91  TF_CHECK_OK(AspiredVersionsManagerBuilder::Create(std::move(manager_options),
92  &builder));
93  builder->AddSourceChain(std::move(path_source), std::move(bundle_source));
94  *manager = builder->Build();
95 
96  return absl::OkStatus();
97 }
98 
99 } // namespace simple_servers
100 } // namespace serving
101 } // namespace tensorflow