• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. 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 // Helpers for loading the persistent representation of a SavedModelV2.
17 // Please note that this is depended on by code that does not make use of
18 // the full runtime and its dependencies should be restricted.
19 
20 #ifndef TENSORFLOW_CC_SAVED_MODEL_BUNDLE_V2_H_
21 #define TENSORFLOW_CC_SAVED_MODEL_BUNDLE_V2_H_
22 
23 #include <string>
24 
25 #include "absl/container/flat_hash_set.h"
26 #include "tensorflow/core/lib/core/status.h"
27 #include "tensorflow/core/protobuf/graph_debug_info.pb.h"
28 #include "tensorflow/core/protobuf/meta_graph.pb.h"
29 #include "tensorflow/core/protobuf/saved_object_graph.pb.h"
30 #include "tensorflow/core/protobuf/trackable_object_graph.pb.h"
31 #include "tensorflow/core/util/tensor_bundle/tensor_bundle.h"
32 
33 namespace tensorflow {
34 
35 /// Represents a version 2 SavedModel that is loaded from storage (but not yet
36 /// loaded into an executable in-memory representation).
37 class SavedModelV2Bundle {
38  public:
39   using RestoreObjectsCallback =
40       std::function<Status(int, const TrackableObjectGraph::TrackableObject&)>;
41 
42   /// Loads persistent representations for a SavedModelV2 from the specified
43   /// export directory.
44   static Status Load(const std::string& export_dir, SavedModelV2Bundle* bundle);
45 
46   /// MetaGraphDef from the loaded SavedModel.
meta_graph_def()47   MetaGraphDef& meta_graph_def() { return meta_graph_def_; }
48 
49   /// SavedObjectGraph from the MetaGraphDef.
saved_object_graph()50   const SavedObjectGraph& saved_object_graph() {
51     return meta_graph_def().object_graph_def();
52   }
53 
54   /// TrackableObjectGraph loaded from the variable_reader() checkpoint.
trackable_object_graph()55   TrackableObjectGraph& trackable_object_graph() {
56     return trackable_object_graph_;
57   }
58 
59   /// BundleReader for accessing the variables bundle.
variable_reader()60   BundleReader* variable_reader() { return variable_reader_.get(); }
61 
62   /// The GraphDebugInfo (or nullptr if none).
debug_info()63   GraphDebugInfo* debug_info() { return debug_info_.get(); }
64 
65   /// Restores objects, invoking the callback with the node id in the
66   /// saved_object_graph() and the corresponding TrackableObject from the
67   /// trackable_object_graph(). The callback may use the variable_reader() but
68   /// must not modify the underlying saved_object_graph().
69   Status VisitObjectsToRestore(RestoreObjectsCallback callback);
70 
71  private:
72   Status RecurseObjectsToRestore(
73       const SavedObject* saved_object, int saved_object_node_id,
74       const TrackableObjectGraph::TrackableObject* trackable_object,
75       std::string object_name,
76       absl::flat_hash_set<int>* seen_trackable_node_ids,
77       RestoreObjectsCallback callback);
78 
79   MetaGraphDef meta_graph_def_;
80   TrackableObjectGraph trackable_object_graph_;
81   std::unique_ptr<BundleReader> variable_reader_;
82   std::unique_ptr<GraphDebugInfo> debug_info_;
83 };
84 
85 }  // namespace tensorflow
86 
87 #endif  // TENSORFLOW_CC_SAVED_MODEL_BUNDLE_V2_H_
88