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 #include "tensorflow/cc/saved_model/loader_util.h"
17
18 #include <vector>
19
20 #include "tensorflow/cc/saved_model/constants.h"
21 #include "tensorflow/core/lib/strings/strcat.h"
22 #include "tensorflow/core/platform/errors.h"
23 #include "tensorflow/core/platform/protobuf_internal.h"
24
25 namespace tensorflow {
26 namespace internal {
27
28 // A SavedModel may store the name of the initialization op to run in the
29 // in the SignatureDef (v2) or a collection (v1). If an init_op collection
30 // exists, then the collection must contain exactly one op.
GetInitOp(const string & export_dir,const MetaGraphDef & meta_graph_def,string * init_op_name)31 Status GetInitOp(const string& export_dir, const MetaGraphDef& meta_graph_def,
32 string* init_op_name) {
33 const auto& sig_def_map = meta_graph_def.signature_def();
34 const auto& init_op_sig_it =
35 meta_graph_def.signature_def().find(kSavedModelInitOpSignatureKey);
36 if (init_op_sig_it != sig_def_map.end()) {
37 *init_op_name = init_op_sig_it->second.outputs()
38 .find(kSavedModelInitOpSignatureKey)
39 ->second.name();
40 return Status::OK();
41 }
42
43 const auto& collection_def_map = meta_graph_def.collection_def();
44 string init_op_collection_key;
45 if (collection_def_map.find(kSavedModelMainOpKey) !=
46 collection_def_map.end()) {
47 init_op_collection_key = kSavedModelMainOpKey;
48 } else {
49 init_op_collection_key = kSavedModelLegacyInitOpKey;
50 }
51
52 const auto init_op_it = collection_def_map.find(init_op_collection_key);
53 if (init_op_it != collection_def_map.end()) {
54 if (init_op_it->second.node_list().value_size() != 1) {
55 return errors::FailedPrecondition(
56 strings::StrCat("Expected exactly one main op in : ", export_dir));
57 }
58 *init_op_name = init_op_it->second.node_list().value(0);
59 }
60 return Status::OK();
61 }
62
GetAssetFileDefs(const MetaGraphDef & meta_graph_def,std::vector<AssetFileDef> * asset_file_defs)63 Status GetAssetFileDefs(const MetaGraphDef& meta_graph_def,
64 std::vector<AssetFileDef>* asset_file_defs) {
65 // With SavedModel v2, we write asset file def into metagraph instead of
66 // collection, so read from metagraph first.
67 if (meta_graph_def.asset_file_def_size() > 0) {
68 for (const auto& asset : meta_graph_def.asset_file_def()) {
69 asset_file_defs->push_back(asset);
70 }
71 return Status::OK();
72 }
73 // Fall back to read from collection to be backward compatible with v1.
74 const auto& collection_def_map = meta_graph_def.collection_def();
75 const auto assets_it = collection_def_map.find(kSavedModelAssetsKey);
76 if (assets_it == collection_def_map.end()) {
77 return Status::OK();
78 }
79 const auto& any_assets = assets_it->second.any_list().value();
80 for (const auto& any_asset : any_assets) {
81 AssetFileDef asset_file_def;
82 TF_RETURN_IF_ERROR(
83 ParseAny(any_asset, &asset_file_def, "tensorflow.AssetFileDef"));
84 asset_file_defs->push_back(asset_file_def);
85 }
86 return Status::OK();
87 }
88
89 } // namespace internal
90 } // namespace tensorflow
91