| 3821 | } |
| 3822 | |
| 3823 | Status ArithmeticOptimizer::Optimize(Cluster* /*cluster*/, |
| 3824 | const GrapplerItem& item, |
| 3825 | GraphDef* optimized_graph) { |
| 3826 | // Set up helper data structures. |
| 3827 | nodes_to_preserve_ = item.NodesToPreserve(); |
| 3828 | fetch_nodes_known_ = !item.fetch.empty(); |
| 3829 | GrapplerItem optimized_item(item); |
| 3830 | optimized_graph_ = &optimized_item.graph; |
| 3831 | |
| 3832 | node_map_.reset(new NodeMap(optimized_graph_)); |
| 3833 | for (const auto& feed : item.feed) { |
| 3834 | feed_nodes_.insert(NodeName(feed.first)); |
| 3835 | } |
| 3836 | |
| 3837 | // // Disable restricted graph rewrites. |
| 3838 | options_.unary_ops_composition &= |
| 3839 | item.optimization_options().allow_non_differentiable_rewrites; |
| 3840 | |
| 3841 | // Perform topological sort on the graph in order to help DedupComputations |
| 3842 | // and AddOpsRewrite to optimize larger subgraphs starting from the roots |
| 3843 | // with more inputs. |
| 3844 | TF_RETURN_IF_ERROR(TopologicalSort(optimized_graph_)); |
| 3845 | GRAPPLER_RETURN_IF_DEADLINE_EXCEEDED(); |
| 3846 | |
| 3847 | if (options_.dedup_computations) { |
| 3848 | DedupComputations(); |
| 3849 | GRAPPLER_RETURN_IF_DEADLINE_EXCEEDED(); |
| 3850 | } |
| 3851 | |
| 3852 | graph_properties_.reset(new GraphProperties(optimized_item)); |
| 3853 | const bool assume_valid_feeds = opt_level_ == RewriterConfig::AGGRESSIVE; |
| 3854 | const Status status = |
| 3855 | graph_properties_->InferStatically(assume_valid_feeds, |
| 3856 | /*aggressive_shape_inference=*/false, |
| 3857 | /*include_tensor_values=*/false); |
| 3858 | const bool can_use_shapes = status.ok(); |
| 3859 | if (!can_use_shapes) { |
| 3860 | VLOG(1) << "Shape inference failed." << status.error_message(); |
| 3861 | } |
| 3862 | |
| 3863 | // Perform the optimizations. |
| 3864 | TF_RETURN_IF_ERROR(SimplifyArithmeticOps(can_use_shapes)); |
| 3865 | |
| 3866 | optimized_graph->Swap(optimized_graph_); |
| 3867 | return Status::OK(); |
| 3868 | } |
| 3869 | |
| 3870 | void ArithmeticOptimizer::Feedback(Cluster* /*cluster*/, |
| 3871 | const GrapplerItem& /*item*/, |
nothing calls this directly
no test coverage detected