| 1624 | } |
| 1625 | |
| 1626 | Status MutableGraphView::ApplyMutationInternal() { |
| 1627 | // Node name -> node index mapping. If a node index is -1, the associated node |
| 1628 | // with key node name exists. Otherwise the node index is the node's index in |
| 1629 | // the graph. |
| 1630 | absl::flat_hash_map<absl::string_view, int> node_names; |
| 1631 | // Indices of MutableNodeViewDiff in Mutation::updated_nodes_ where nodes are |
| 1632 | // renamed (and possibly have other fields mutated). |
| 1633 | std::vector<RenamedOrOverwrittenNode> renamed_nodes; |
| 1634 | // Indices of MutableNodeViewDiff in Mutation::updated_nodes_ where nodes are |
| 1635 | // not renamed but have fields mutated. |
| 1636 | std::vector<int> inplace_nodes; |
| 1637 | // Indices of nodes in graph where MutableNodeViewDiff are empty. |
| 1638 | // `update_index_` of nodes associated to empty MutableNodeViewDiff should be |
| 1639 | // cleared after validation success. |
| 1640 | std::vector<int> empty_diff_node_indices; |
| 1641 | |
| 1642 | // Check if this mutation is valid before applying, and partition |
| 1643 | // updated_nodes_ into inplace mutated nodes and renamed nodes. |
| 1644 | TF_RETURN_IF_ERROR(ValidateInternal( |
| 1645 | &node_names, &renamed_nodes, &inplace_nodes, &empty_diff_node_indices)); |
| 1646 | |
| 1647 | // Clear `update_index_` of MutableNodeView with empty associated |
| 1648 | // MutableNodeViewDiff. |
| 1649 | for (const int empty_diff_node_index : empty_diff_node_indices) { |
| 1650 | nodes_[empty_diff_node_index].update_index_ = internal::kMissingIndex; |
| 1651 | } |
| 1652 | |
| 1653 | // Node name and associated fanouts. |
| 1654 | absl::flat_hash_map<string, NodeViewFanouts> renamed_fanouts; |
| 1655 | // Removed nodes where name was overwritten by a renamed node. |
| 1656 | std::vector<bool> overwritten_name_removed_nodes(nodes_.size()); |
| 1657 | // Fix renaming of existing nodes by swapping fanouts and rehashing names. |
| 1658 | // This will also overwrite removed or unmodified nodes. |
| 1659 | FixRenamedNodes(&renamed_nodes, &renamed_fanouts, |
| 1660 | &overwritten_name_removed_nodes); |
| 1661 | |
| 1662 | // Indices of nodes in graph where new nodes were inserted/appended. These |
| 1663 | // will be corresponding to `new_nodes_` in order. |
| 1664 | std::vector<int> new_node_indices; |
| 1665 | // Add new nodes, overwriting removed or unmodified nodes. |
| 1666 | AddNewNodes(&renamed_fanouts, &new_node_indices); |
| 1667 | |
| 1668 | // For abandoned fanouts, mark their respective fanins so the original node |
| 1669 | // associated will not have their fanouts removed and be left in an |
| 1670 | // inconsistent state. |
| 1671 | FixRenamedFanouts(renamed_fanouts); |
| 1672 | |
| 1673 | // Apply mutations to updated nodes (renamed nodes are treated as inplace |
| 1674 | // nodes as they have already been renamed). Removed nodes are ignored. |
| 1675 | ApplyNodeUpdates(); |
| 1676 | |
| 1677 | // Set fanins of new nodes. |
| 1678 | SetNewNodesFanins(new_node_indices); |
| 1679 | |
| 1680 | // Remove overwritten nodes and updated nodes set to be removed. |
| 1681 | RemoveNodesInternal(renamed_nodes, overwritten_name_removed_nodes); |
| 1682 | |
| 1683 | mutation_.ResetInternal(); |
no test coverage detected