| 446 | } |
| 447 | |
| 448 | Status MutableGraphView::AddSubgraph(GraphDef&& subgraph) { |
| 449 | // 1. Add all new functions and check that functions with the same name |
| 450 | // have identical definition. |
| 451 | const int function_size = subgraph.library().function_size(); |
| 452 | if (function_size > 0) { |
| 453 | absl::flat_hash_map<absl::string_view, const FunctionDef*> graph_fdefs; |
| 454 | for (const FunctionDef& fdef : graph()->library().function()) { |
| 455 | graph_fdefs.emplace(fdef.signature().name(), &fdef); |
| 456 | } |
| 457 | |
| 458 | for (FunctionDef& fdef : *subgraph.mutable_library()->mutable_function()) { |
| 459 | const auto graph_fdef = graph_fdefs.find(fdef.signature().name()); |
| 460 | |
| 461 | if (graph_fdef == graph_fdefs.end()) { |
| 462 | VLOG(3) << "Add new function definition: " << fdef.signature().name(); |
| 463 | graph()->mutable_library()->add_function()->Swap(&fdef); |
| 464 | } else { |
| 465 | if (!FunctionDefsEqual(fdef, *graph_fdef->second)) { |
| 466 | return MutationError( |
| 467 | "AddSubgraph", |
| 468 | absl::Substitute("function_size=$0", function_size), |
| 469 | absl::StrCat( |
| 470 | "Found different function definition with the same name: ", |
| 471 | fdef.signature().name())); |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | // 2. Add all nodes to the underlying graph. |
| 478 | int node_size_before = graph()->node_size(); |
| 479 | |
| 480 | for (NodeDef& node : *subgraph.mutable_node()) { |
| 481 | auto* node_in_graph = graph()->add_node(); |
| 482 | node_in_graph->Swap(&node); |
| 483 | TF_RETURN_IF_ERROR(AddUniqueNode(node_in_graph)); |
| 484 | } |
| 485 | |
| 486 | // TODO(ezhulenev, lyandy): Right now AddAndDedupFanouts do not check that |
| 487 | // fanins actually exists in the graph, and there is already TODO for that. |
| 488 | |
| 489 | for (int i = node_size_before; i < graph()->node_size(); ++i) { |
| 490 | NodeDef* node = graph()->mutable_node(i); |
| 491 | AddAndDedupFanouts(node); |
| 492 | } |
| 493 | |
| 494 | return Status::OK(); |
| 495 | } |
| 496 | |
| 497 | Status MutableGraphView::UpdateNode( |
| 498 | absl::string_view node_name, absl::string_view op, absl::string_view device, |