TensorFlow Serving C++ API Documentation
loader.h
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 #ifndef TENSORFLOW_SERVING_CORE_LOADER_H_
17 #define TENSORFLOW_SERVING_CORE_LOADER_H_
18 
19 #include <memory>
20 
21 #include "tensorflow/core/lib/core/errors.h"
22 #include "tensorflow/core/lib/core/status.h"
23 #include "tensorflow_serving/core/source.h"
24 #include "tensorflow_serving/resources/resources.pb.h"
25 #include "tensorflow_serving/util/any_ptr.h"
26 
27 namespace tensorflow {
28 namespace serving {
29 
56 class Loader {
57  public:
60  virtual ~Loader() = default;
61 
75  // include: (a) replace conservative estimate with actual measurement
76  // once loaded in memory; (b) load process consumes extra transient
77  // memory that is not used in steady-state after the load completes.
82  virtual Status EstimateResources(ResourceAllocation* estimate) const = 0;
83 
89  virtual Status Load() {
90  return errors::Unimplemented("Load isn't implemented.");
91  }
92 
94  struct Metadata {
95  ServableId servable_id;
96  };
102  virtual Status LoadWithMetadata(const Metadata& metadata) { return Load(); }
103 
108  virtual void Unload() = 0;
109 
138  virtual AnyPtr servable() = 0;
139 };
140 
141 inline bool operator==(const Loader::Metadata& a, const Loader::Metadata& b) {
142  return a.servable_id == b.servable_id;
143 }
144 
145 inline bool operator!=(const Loader::Metadata& a, const Loader::Metadata& b) {
146  return !(a == b);
147 }
148 
155 class ResourceUnsafeLoader : public Loader {
156  public:
157  Status EstimateResources(ResourceAllocation* estimate) const final {
158  estimate->Clear();
159  return Status();
160  }
161 };
162 
163 // A source that emits Loader unique pointers.
164 using LoaderSource = Source<std::unique_ptr<Loader>>;
165 
166 } // namespace serving
167 } // namespace tensorflow
168 
169 #endif // TENSORFLOW_SERVING_CORE_LOADER_H_
virtual AnyPtr servable()=0
virtual Status LoadWithMetadata(const Metadata &metadata)
Definition: loader.h:102
virtual Status Load()
Definition: loader.h:89
virtual ~Loader()=default
virtual Status EstimateResources(ResourceAllocation *estimate) const =0
Status EstimateResources(ResourceAllocation *estimate) const final
Definition: loader.h:157
The metadata consists of the ServableId.
Definition: loader.h:94