• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 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 #include "tensorflow/core/common_runtime/optimization_registry.h"
17 #include "tensorflow/core/util/dump_graph.h"
18 
19 namespace tensorflow {
20 
21 // static
Global()22 OptimizationPassRegistry* OptimizationPassRegistry::Global() {
23   static OptimizationPassRegistry* global_optimization_registry =
24       new OptimizationPassRegistry;
25   return global_optimization_registry;
26 }
27 
Register(Grouping grouping,int phase,std::unique_ptr<GraphOptimizationPass> pass)28 void OptimizationPassRegistry::Register(
29     Grouping grouping, int phase, std::unique_ptr<GraphOptimizationPass> pass) {
30   groups_[grouping][phase].push_back(std::move(pass));
31 }
32 
RunGrouping(Grouping grouping,const GraphOptimizationPassOptions & options)33 Status OptimizationPassRegistry::RunGrouping(
34     Grouping grouping, const GraphOptimizationPassOptions& options) {
35   auto group = groups_.find(grouping);
36   if (group != groups_.end()) {
37     for (auto& phase : group->second) {
38       VLOG(1) << "Running optimization phase " << phase.first;
39       for (auto& pass : phase.second) {
40         VLOG(1) << "Running optimization pass: " << pass->name();
41         Status s = pass->Run(options);
42         if (!s.ok()) return s;
43         if (VLOG_IS_ON(1)) {
44           if (options.graph) {
45             DumpGraphToFile(
46                 strings::StrCat(
47                     "after_phase_", phase.first, "_", pass->name(), "_",
48                     reinterpret_cast<uintptr_t>((*options.graph).get())),
49                 **options.graph, options.flib_def);
50           }
51           if (options.partition_graphs) {
52             for (auto& part : *options.partition_graphs) {
53               DumpGraphToFile(
54                   strings::StrCat(
55                       "after_phase_", phase.first, "_", pass->name(),
56                       "_partition_", part.first, "_",
57                       reinterpret_cast<uintptr_t>(part.second.get())),
58                   *part.second, options.flib_def);
59             }
60           }
61         }
62       }
63     }
64   }
65   return Status::OK();
66 }
67 
LogGrouping(Grouping grouping,int vlog_level)68 void OptimizationPassRegistry::LogGrouping(Grouping grouping, int vlog_level) {
69   auto group = groups_.find(grouping);
70   if (group != groups_.end()) {
71     for (auto& phase : group->second) {
72       for (auto& pass : phase.second) {
73         VLOG(vlog_level) << "Registered optimization pass grouping " << grouping
74                          << " phase " << phase.first << ": " << pass->name();
75       }
76     }
77   }
78 }
79 
LogAllGroupings(int vlog_level)80 void OptimizationPassRegistry::LogAllGroupings(int vlog_level) {
81   for (auto group = groups_.begin(); group != groups_.end(); ++group) {
82     LogGrouping(group->first, vlog_level);
83   }
84 }
85 
86 }  // namespace tensorflow
87