Generates a MetaGraph which calls `signature_functions`. Args: meta_graph_def: The MetaGraphDef proto to fill. saveable_view: The _SaveableView being exported. signature_functions: A dictionary mapping signature keys to concrete functions containing signatures to add to the Meta
(meta_graph_def, saveable_view, signature_functions)
| 534 | |
| 535 | |
| 536 | def _fill_meta_graph_def(meta_graph_def, saveable_view, signature_functions): |
| 537 | """Generates a MetaGraph which calls `signature_functions`. |
| 538 | |
| 539 | Args: |
| 540 | meta_graph_def: The MetaGraphDef proto to fill. |
| 541 | saveable_view: The _SaveableView being exported. |
| 542 | signature_functions: A dictionary mapping signature keys to concrete |
| 543 | functions containing signatures to add to the MetaGraph. |
| 544 | |
| 545 | Returns: |
| 546 | An _AssetInfo, which contains information to help creating the SavedModel. |
| 547 | """ |
| 548 | # List objects from the eager context to make sure Optimizers give us the |
| 549 | # right Graph-dependent variables. |
| 550 | accessible_objects = saveable_view.nodes |
| 551 | resource_initializer_functions = _trace_resource_initializers( |
| 552 | accessible_objects) |
| 553 | exported_graph = ops.Graph() |
| 554 | resource_initializer_ops = [] |
| 555 | with exported_graph.as_default(): |
| 556 | object_map, resource_map, asset_info = saveable_view.map_resources() |
| 557 | for resource_initializer_function in resource_initializer_functions: |
| 558 | asset_dependencies = [] |
| 559 | for capture in resource_initializer_function.graph.external_captures: |
| 560 | asset_initializer = asset_info.asset_initializers_by_resource.get( |
| 561 | capture, None) |
| 562 | if asset_initializer is not None: |
| 563 | asset_dependencies.append(asset_initializer) |
| 564 | with ops.control_dependencies(asset_dependencies): |
| 565 | resource_initializer_ops.append( |
| 566 | _call_function_with_mapped_captures( |
| 567 | resource_initializer_function, [], resource_map)) |
| 568 | resource_initializer_ops.extend( |
| 569 | asset_info.asset_initializers_by_resource.values()) |
| 570 | with ops.control_dependencies(resource_initializer_ops): |
| 571 | init_op = control_flow_ops.no_op() |
| 572 | # Add the same op to the main_op collection and to the init_op |
| 573 | # signature. The collection is for compatibility with older loader APIs; |
| 574 | # only one will be executed. |
| 575 | meta_graph_def.collection_def[constants.MAIN_OP_KEY].node_list.value.append( |
| 576 | init_op.name) |
| 577 | meta_graph_def.signature_def[constants.INIT_OP_SIGNATURE_KEY].CopyFrom( |
| 578 | signature_def_utils.op_signature_def( |
| 579 | init_op, constants.INIT_OP_SIGNATURE_KEY)) |
| 580 | |
| 581 | # Saving an object-based checkpoint again gathers variables. We need to do the |
| 582 | # gathering from the eager context so Optimizers save the right set of |
| 583 | # variables, but want any operations associated with the save/restore to be in |
| 584 | # the exported graph (thus the `to_graph` argument). |
| 585 | saver = functional_saver.MultiDeviceSaver( |
| 586 | saveable_view.checkpoint_view.frozen_saveable_objects( |
| 587 | object_map=object_map, to_graph=exported_graph)) |
| 588 | |
| 589 | with exported_graph.as_default(): |
| 590 | signatures = _generate_signatures(signature_functions, resource_map) |
| 591 | for concrete_function in saveable_view.concrete_functions: |
| 592 | concrete_function.add_to_graph() |
| 593 | saver_def = saver.to_proto() |
no test coverage detected