tensorflow/core/tpu/graph_rewrite/tpu_embedding_rewrite_pass_utils.cc
// Replaces one TensorFlow graph node with another (specified by a NodeDef),
// moving all the edges.
Replaces one TensorFlow graph node with another (specified by a NodeDef),
moving all the edges.
Replaces one TensorFlow graph node with another ( specified by a NodeDef ) moving all the edges .
Status ReplaceNode(const NodeDef& to_def, Node* from, Node** to, Graph* graph) {
  std::vector<const Edge*> edges;
  VLOG(1) << "Node: " << from->DebugString() << " in_edges";
  for (const Edge* edge : from->in_edges()) {
    VLOG(1) << absl::StrFormat("Edge from [%s:%d] to [%s:%d]",
                               edge->src()->name(), edge->src_output(),
                               edge->dst()->name(), edge->dst_input());
    edges.push_back(edge);
  }
  VLOG(1) << "Node: " << from->DebugString() << " out_edges";
  for (const Edge* edge : from->out_edges()) {
    VLOG(1) << absl::StrFormat("Edge from [%s:%d] to [%s:%d]",
                               edge->src()->name(), edge->src_output(),
                               edge->dst()->name(), edge->dst_input());
    if (edge->dst() != from) {
      edges.push_back(edge);
    }
  }
  TF_RETURN_IF_ERROR(AddNode(to_def, to, graph));
  for (const Edge* edge : edges) {
    Node* new_edge_src = (edge->src() == from ? *to : edge->src());
    Node* new_edge_dst = (edge->dst() == from ? *to : edge->dst());
    VLOG(1) << absl::StrFormat("Adding new edge from [%s:%d] to [%s:%d]",
                               new_edge_src->name(), edge->src_output(),
                               new_edge_dst->name(), edge->dst_input());
    graph->AddEdge(new_edge_src, edge->src_output(), new_edge_dst,
                   edge->dst_input());
  }
  for (const Edge* edge : edges) {
    graph->RemoveEdge(edge);
  }
  graph->RemoveNode(from);
  return Status::OK();
}
Status ReplaceNode ( const NodeDef & to_def , Node * from , Node * * to , Graph * graph ) { std :: vector < const Edge * > edges ; VLOG ( 1 ) << " " << from -> DebugString ( ) << " " ; for ( const Edge * edge : from -> in_edges ( ) ) { VLOG ( 1 ) << absl :: StrFormat ( " " , edge -> src ( ) -> name ( ) , edge -> src_output ( ) , edge -> dst ( ) -> name ( ) , edge -> dst_input ( ) ) ; edges . push_back ( edge ) ; } VLOG ( 1 ) << " " << from -> DebugString ( ) << " " ; for ( const Edge * edge : from -> out_edges ( ) ) { VLOG ( 1 ) << absl :: StrFormat ( " " , edge -> src ( ) -> name ( ) , edge -> src_output ( ) , edge -> dst ( ) -> name ( ) , edge -> dst_input ( ) ) ; if ( edge -> dst ( ) != from ) { edges . push_back ( edge ) ; } } TF_RETURN_IF_ERROR ( AddNode ( to_def , to , graph ) ) ; for ( const Edge * edge : edges ) { Node * new_edge_src = ( edge -> src ( ) == from ? * to : edge -> src ( ) ) ; Node * new_edge_dst = ( edge -> dst ( ) == from ? * to : edge -> dst ( ) ) ; VLOG ( 1 ) << absl :: StrFormat ( " " , new_edge_src -> name ( ) , edge -> src_output ( ) , new_edge_dst -> name ( ) , edge -> dst_input ( ) ) ; graph -> AddEdge ( new_edge_src , edge -> src_output ( ) , new_edge_dst , edge -> dst_input ( ) ) ; } for ( const Edge * edge : edges ) { graph -> RemoveEdge ( edge ) ; } graph -> RemoveNode ( from ) ; return Status :: OK ( ) ; }
Replaces one TensorFlow graph node with another (specified by a NodeDef),
moving all the edges.
Replaces one TensorFlow graph node with another ( specified by a NodeDef ) moving all the edges .
// Don't copy self-loops multiple times. // Add new node. // Add edges for the new node. // Remove edges for the original node. // Remove original node.