| 583 | } |
| 584 | |
| 585 | Status MetaOptimizer::RunOptimizer( |
| 586 | GraphOptimizer* optimizer, Cluster* cluster, GrapplerItem* optimized_item, |
| 587 | GraphDef* optimized_graph, GraphOptimizationResult* optimization_result) { |
| 588 | const uint64 start_us = Env::Default()->NowMicros(); |
| 589 | |
| 590 | // If optimizer doesn't need a function library, we will replace it with a |
| 591 | // stub before running optimization, and will put it back at the end. |
| 592 | FunctionDefLibrary optimized_graph_function_library; |
| 593 | const bool is_function_library_aware = optimizer->UsesFunctionLibrary(); |
| 594 | |
| 595 | // Replace function library in optimized graph with a stub. |
| 596 | if (!is_function_library_aware) { |
| 597 | VLOG(3) << "Replace function library with a stub for " << optimizer->name(); |
| 598 | optimized_graph_function_library.Swap(optimized_graph->mutable_library()); |
| 599 | *optimized_graph->mutable_library() = |
| 600 | GetFunctionDefLibraryStub(optimized_graph_function_library); |
| 601 | } |
| 602 | |
| 603 | // This swaps the current optimized_graph into optimized item and |
| 604 | // resets optimized_graph to an empty graph. |
| 605 | optimized_graph->Swap(&optimized_item->graph); |
| 606 | *optimized_graph = GraphDef(); |
| 607 | optimizer->set_deadline_usec(this->deadline_usec()); |
| 608 | Status status = |
| 609 | optimizer->Optimize(cluster, *optimized_item, optimized_graph); |
| 610 | const uint64 end_us = Env::Default()->NowMicros(); |
| 611 | const float duration_ms = (end_us - start_us) / 1000.0f; |
| 612 | |
| 613 | string message; |
| 614 | if (!status.ok()) { |
| 615 | optimized_graph->Swap(&optimized_item->graph); |
| 616 | if (errors::IsAborted(status)) { |
| 617 | // By convention we (ab-)use the Aborted error code to signal that the |
| 618 | // optimizer returned without performing any changes to the graph. |
| 619 | message = strings::StrCat(optimizer->name(), |
| 620 | " did nothing. time = ", duration_ms, "ms."); |
| 621 | // Swallow the non-critical error. |
| 622 | status = Status::OK(); |
| 623 | } else if (errors::IsDeadlineExceeded(status)) { |
| 624 | message = |
| 625 | strings::StrCat(status.ToString(), ", time = ", duration_ms, "ms."); |
| 626 | LOG(WARNING) << optimizer->name() << " failed: " << message; |
| 627 | } else { |
| 628 | message = status.ToString(); |
| 629 | LOG(ERROR) << optimizer->name() << " failed: " << message; |
| 630 | } |
| 631 | } else { |
| 632 | message = strings::StrCat( |
| 633 | PrintSizesBeforeAfter(optimized_item->graph, *optimized_graph), |
| 634 | ", time = ", duration_ms, "ms."); |
| 635 | VLOG(1) << optimizer->name() << ": " << message; |
| 636 | } |
| 637 | |
| 638 | // Swap function library back into the main graph. |
| 639 | if (!is_function_library_aware) { |
| 640 | optimized_graph->mutable_library()->Swap(&optimized_graph_function_library); |
| 641 | } |
| 642 |
nothing calls this directly
no test coverage detected