Helper function that given a GrapplerItem generates a mutated graph def with the batch size changed. The GrapplerItem could be generated from the main graph or could be a function graph.
| 831 | // with the batch size changed. The GrapplerItem could be generated from the |
| 832 | // main graph or could be a function graph. |
| 833 | Status OptimizeGraph(const GrapplerItem& item, int64 num_replicas, |
| 834 | bool use_fallback, GraphDef* output) { |
| 835 | *output = item.graph; |
| 836 | MutableGraphView graph(output); |
| 837 | |
| 838 | FunctionLibraryDefinition flib(OpRegistry::Global(), item.graph.library()); |
| 839 | |
| 840 | NodeDef* sink_node; |
| 841 | TF_RETURN_IF_ERROR(graph_utils::GetFetchNode(graph, item, &sink_node)); |
| 842 | |
| 843 | Status s = RecursivelyHandleOp(*sink_node, num_replicas, use_fallback, &flib, |
| 844 | &graph); |
| 845 | if (!s.ok()) { |
| 846 | if (use_fallback) { |
| 847 | VLOG(1) << "Failed to rebatch by rewriting the batch transformation (" |
| 848 | << s << "). Using a fallback method instead."; |
| 849 | // If RecursivelyHandleOp fails, we reset `graph` to use the original, |
| 850 | // graph, since that function may have mutated `graph`. |
| 851 | *output = item.graph; |
| 852 | graph = MutableGraphView(output); |
| 853 | TF_RETURN_IF_ERROR( |
| 854 | RebatchWithFallback(sink_node, num_replicas, &flib, &graph)); |
| 855 | } else { |
| 856 | // Return the error |
| 857 | return s; |
| 858 | } |
| 859 | } |
| 860 | *output->mutable_library() = flib.ToProto(); |
| 861 | return Status::OK(); |
| 862 | } |
| 863 | |
| 864 | } // anonymous namespace |
| 865 |
no test coverage detected