Home | History | Annotate | Download | only in common_runtime
      1 /* Copyright 2015 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_COMMON_RUNTIME_GRAPH_OPTIMIZER_H_
     17 #define TENSORFLOW_CORE_COMMON_RUNTIME_GRAPH_OPTIMIZER_H_
     18 
     19 #include "tensorflow/core/framework/function.h"
     20 #include "tensorflow/core/graph/graph.h"
     21 #include "tensorflow/core/lib/core/status.h"
     22 #include "tensorflow/core/platform/env.h"
     23 #include "tensorflow/core/protobuf/config.pb.h"
     24 
     25 namespace tensorflow {
     26 
     27 class GraphOptimizer {
     28  public:
     29   GraphOptimizer(const OptimizerOptions& opts);
     30   ~GraphOptimizer();
     31 
     32   // Applies optimization passes specified in 'opts' to 'graph'.
     33   // Maybe replace *graph with a new graph object.  'device' is device
     34   // on which the 'graph' will execute. It's passed to the optimizers
     35   // so that they can respect constraints if any, that should be
     36   // respected.
     37   //
     38   // If shape_map is not null it maps from nodes in graph to partially-known
     39   // shapes of their outputs, and may be used, e.g., in the constant folding
     40   // pass. The use of shape_map implies that the mapping from node name to the
     41   // vector of partial shapes of its outputs is stable, i.e., no optimization
     42   // pass may replace a node with a different node of the same name that has a
     43   // different number of outputs, or outputs with different known shapes.
     44   // TODO(b/65453533) introduce a unique way to name nodes in a graph.
     45   //
     46   // If cse_consider_fn is not null then only nodes for which cse_consider_fn
     47   // returns true will be considered for CSE.
     48   void Optimize(
     49       FunctionLibraryRuntime* runtime, Env* env, Device* device,
     50       std::unique_ptr<Graph>* graph,
     51       const std::unordered_map<string, std::vector<PartialTensorShape>>*
     52           shape_map,
     53       const std::function<bool(const Node*)>& cse_consider_fn = nullptr);
     54 
     55   const OptimizerOptions& options() { return opts_; }
     56 
     57  private:
     58   OptimizerOptions opts_;
     59 
     60   TF_DISALLOW_COPY_AND_ASSIGN(GraphOptimizer);
     61 };
     62 
     63 }  // end namespace tensorflow
     64 
     65 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_GRAPH_OPTIMIZER_H_
     66