1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_GRAPH_OPTIMIZER_H_ 17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_GRAPH_OPTIMIZER_H_ 18 19 #include <string> 20 21 #include "tensorflow/core/framework/graph.pb.h" 22 #include "tensorflow/core/platform/env.h" 23 #include "tensorflow/core/platform/errors.h" 24 #include "tensorflow/core/platform/status.h" 25 #include "tensorflow/core/platform/types.h" 26 27 namespace tensorflow { 28 namespace grappler { 29 30 class Cluster; 31 struct GrapplerItem; 32 33 // An abstract interface for an algorithm for generating a candidate 34 // optimization of a GrapplerItem for running on a cluster. 35 class GraphOptimizer { 36 public: GraphOptimizer()37 GraphOptimizer() : deadline_usec_(0) {} ~GraphOptimizer()38 virtual ~GraphOptimizer() {} 39 40 virtual string name() const = 0; 41 42 // Returns true if the optimizer requires a valid function library to perform 43 // graph optimization. If false, optimized GrapplerItem will have a stub 44 // instead of real function library (all function signatures and attributes 45 // will be valid, but function body will be empty). Most of the optimizers 46 // that do not instantiate functions should return true. 47 virtual bool UsesFunctionLibrary() const = 0; 48 49 // Routine called to allow an algorithm to propose a rewritten graph 50 // for the graph, feeds and fetches in "item" to run more efficiently 51 // on "cluster". If the returned status is Status::OK() then 52 // *optimized_graph contains the rewritten graph. 53 // Returns an error status if it failed to generate a solution. 54 // 55 // A return value of error::Aborted() can be used signal early termination of 56 // the optimizer, e.g. if the optimization turned out to be a no-op. In this 57 // case the content of *optimized_graph is undefined. 58 virtual Status Optimize(Cluster* cluster, const GrapplerItem& item, 59 GraphDef* optimized_graph) = 0; 60 61 // Subclasses may define a version of Optimize that consumes item. Optimize(Cluster * cluster,GrapplerItem && item,GraphDef * optimized_graph)62 virtual Status Optimize(Cluster* cluster, GrapplerItem&& item, 63 GraphDef* optimized_graph) { 64 return Optimize(cluster, item, optimized_graph); 65 } 66 67 // Method invoked by the framework so that it can provide feedback 68 // on how well the "optimized_graph" (produced as *optimized_graph from a 69 // call to Optimize) performed. Lower "result" scores are better. 70 virtual void Feedback(Cluster* cluster, const GrapplerItem& item, 71 const GraphDef& optimized_graph, double result) = 0; 72 73 // Set deadline in microseconds since epoch. A value of zero means no 74 // deadline. set_deadline_usec(uint64 deadline_usec)75 void set_deadline_usec(uint64 deadline_usec) { 76 deadline_usec_ = deadline_usec; 77 } deadline_usec()78 uint64 deadline_usec() const { return deadline_usec_; } DeadlineExceeded()79 bool DeadlineExceeded() const { 80 return deadline_usec_ > 0 && Env::Default()->NowMicros() > deadline_usec_; 81 } 82 83 private: 84 uint64 deadline_usec_; 85 }; 86 87 #define GRAPPLER_RETURN_IF_DEADLINE_EXCEEDED() \ 88 do { \ 89 if (this->DeadlineExceeded()) { \ 90 return errors::DeadlineExceeded(this->name(), " exceeded deadline."); \ 91 } \ 92 } while (0) 93 94 } // end namespace grappler 95 } // end namespace tensorflow 96 97 #endif // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_GRAPH_OPTIMIZER_H_ 98