• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tensorflow/core/framework/graph.pb.h"
21 #include "tensorflow/core/lib/core/status.h"
22 #include "tensorflow/core/platform/env.h"
23 
24 namespace tensorflow {
25 namespace grappler {
26 
27 class Cluster;
28 struct GrapplerItem;
29 
30 // An abstract interface for an algorithm for generating a candidate
31 // optimization of a GrapplerItem for running on a cluster.
32 class GraphOptimizer {
33  public:
GraphOptimizer()34   GraphOptimizer() : deadline_usec_(0) {}
~GraphOptimizer()35   virtual ~GraphOptimizer() {}
36 
37   virtual string name() const = 0;
38 
39   // Routine called to allow an algorithm to propose a rewritten graph
40   // for the graph, feeds and fetches in "item" to run more efficiently
41   // on "cluster".
42   // Returns an error status if it failed to generate a solution.
43   virtual Status Optimize(Cluster* cluster, const GrapplerItem& item,
44                           GraphDef* optimized_graph) = 0;
45 
46   // Method invoked by the framework so that it can provide feedback
47   // on how well the "optimized_graph" (produced as *optimized_graph from a
48   // call to Optimize) performed.  Lower "result" scores are better.
49   virtual void Feedback(Cluster* cluster, const GrapplerItem& item,
50                         const GraphDef& optimized_graph, double result) = 0;
51 
52   // Set deadline in microseconds since epoch. A value of zero means no
53   // deadline.
set_deadline_usec(uint64 deadline_usec)54   void set_deadline_usec(uint64 deadline_usec) {
55     deadline_usec_ = deadline_usec;
56   }
deadline_usec()57   uint64 deadline_usec() const { return deadline_usec_; }
DeadlineExceeded()58   bool DeadlineExceeded() const {
59     return deadline_usec_ > 0 && Env::Default()->NowMicros() > deadline_usec_;
60   }
61 
62  private:
63   uint64 deadline_usec_;
64 };
65 
66 #define GRAPPLER_RETURN_IF_DEADLINE_EXCEEDED()                              \
67   do {                                                                      \
68     if (this->DeadlineExceeded()) {                                         \
69       return errors::DeadlineExceeded(this->name(), " exceeded deadline."); \
70     }                                                                       \
71   } while (0)
72 
73 }  // end namespace grappler
74 }  // end namespace tensorflow
75 
76 #endif  // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_GRAPH_OPTIMIZER_H_
77