A SavedModel may store the name of the initialization op to run in the in the SignatureDef (v2) or a collection (v1). If an init_op collection exists, then the collection must contain exactly one op.
| 244 | // in the SignatureDef (v2) or a collection (v1). If an init_op collection |
| 245 | // exists, then the collection must contain exactly one op. |
| 246 | Status GetInitOp(const string& export_dir, const MetaGraphDef& meta_graph_def, |
| 247 | string* init_op_name) { |
| 248 | const auto& sig_def_map = meta_graph_def.signature_def(); |
| 249 | const auto& init_op_sig_it = |
| 250 | meta_graph_def.signature_def().find(kSavedModelInitOpSignatureKey); |
| 251 | if (init_op_sig_it != sig_def_map.end()) { |
| 252 | const auto& sig_def_outputs = init_op_sig_it->second.outputs(); |
| 253 | const auto& sig_def_outputs_it = |
| 254 | sig_def_outputs.find(kSavedModelInitOpSignatureKey); |
| 255 | if (sig_def_outputs_it == sig_def_outputs.end()) { |
| 256 | return errors::FailedPrecondition("Could not find output ", |
| 257 | kSavedModelInitOpSignatureKey); |
| 258 | } |
| 259 | *init_op_name = sig_def_outputs_it->second.name(); |
| 260 | return Status::OK(); |
| 261 | } |
| 262 | |
| 263 | const auto& collection_def_map = meta_graph_def.collection_def(); |
| 264 | string init_op_collection_key; |
| 265 | if (collection_def_map.find(kSavedModelMainOpKey) != |
| 266 | collection_def_map.end()) { |
| 267 | init_op_collection_key = kSavedModelMainOpKey; |
| 268 | } else { |
| 269 | init_op_collection_key = kSavedModelLegacyInitOpKey; |
| 270 | } |
| 271 | |
| 272 | const auto init_op_it = collection_def_map.find(init_op_collection_key); |
| 273 | if (init_op_it != collection_def_map.end()) { |
| 274 | if (init_op_it->second.node_list().value_size() != 1) { |
| 275 | return errors::FailedPrecondition( |
| 276 | strings::StrCat("Expected exactly one main op in : ", export_dir)); |
| 277 | } |
| 278 | *init_op_name = init_op_it->second.node_list().value(0); |
| 279 | } |
| 280 | return Status::OK(); |
| 281 | } |
| 282 | |
| 283 | Status RunRestore(const RunOptions& run_options, const string& export_dir, |
| 284 | const StringPiece restore_op_name, |
no test coverage detected