• 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 
18 #include "tensorflow/core/framework/metrics.h"
19 #include "tensorflow/core/util/dump_graph.h"
20 
21 namespace tensorflow {
22 
23 // static
Global()24 OptimizationPassRegistry* OptimizationPassRegistry::Global() {
25   static OptimizationPassRegistry* global_optimization_registry =
26       new OptimizationPassRegistry;
27   return global_optimization_registry;
28 }
29 
Register(Grouping grouping,int phase,std::unique_ptr<GraphOptimizationPass> pass)30 void OptimizationPassRegistry::Register(
31     Grouping grouping, int phase, std::unique_ptr<GraphOptimizationPass> pass) {
32   groups_[grouping][phase].push_back(std::move(pass));
33 }
34 
RunGrouping(Grouping grouping,const GraphOptimizationPassOptions & options)35 Status OptimizationPassRegistry::RunGrouping(
36     Grouping grouping, const GraphOptimizationPassOptions& options) {
37   auto dump_graph = [&](std::string& prefix) {
38     if (options.graph) {
39       DumpGraphToFile(
40           strings::StrCat(prefix, "_",
41                           reinterpret_cast<uintptr_t>((*options.graph).get())),
42           **options.graph, options.flib_def);
43     }
44     if (options.partition_graphs) {
45       for (auto& part : *options.partition_graphs) {
46         DumpGraphToFile(
47             strings::StrCat(prefix, "_partition_", part.first, "_",
48                             reinterpret_cast<uintptr_t>(part.second.get())),
49             *part.second, options.flib_def);
50       }
51     }
52   };
53 
54   VLOG(1) << "Starting optimization of a group " << grouping;
55   if (VLOG_IS_ON(2)) {
56     std::string prefix = strings::StrCat("before_grouping_", grouping);
57     dump_graph(prefix);
58   }
59   auto group = groups_.find(grouping);
60   if (group != groups_.end()) {
61     const uint64 start_us = Env::Default()->NowMicros();
62     for (auto& phase : group->second) {
63       VLOG(1) << "Running optimization phase " << phase.first;
64       for (auto& pass : phase.second) {
65         VLOG(1) << "Running optimization pass: " << pass->name();
66         const uint64 pass_start_us = Env::Default()->NowMicros();
67         Status s = pass->Run(options);
68         const uint64 pass_end_us = Env::Default()->NowMicros();
69         metrics::UpdateGraphOptimizationPassTime(pass->name(),
70                                                  pass_end_us - pass_start_us);
71         if (!s.ok()) return s;
72         if (VLOG_IS_ON(5)) {
73           std::string prefix =
74               strings::StrCat("after_group_", grouping, "_phase_", phase.first,
75                               "_", pass->name());
76           dump_graph(prefix);
77         }
78       }
79     }
80     const uint64 end_us = Env::Default()->NowMicros();
81     metrics::UpdateGraphOptimizationPassTime("*", end_us - start_us);
82   }
83   VLOG(1) << "Finished optimization of a group " << grouping;
84   if (VLOG_IS_ON(2)) {
85     std::string prefix = strings::StrCat("after_grouping_", grouping);
86     dump_graph(prefix);
87   }
88   return Status::OK();
89 }
90 
LogGrouping(Grouping grouping,int vlog_level)91 void OptimizationPassRegistry::LogGrouping(Grouping grouping, int vlog_level) {
92   auto group = groups_.find(grouping);
93   if (group != groups_.end()) {
94     for (auto& phase : group->second) {
95       for (auto& pass : phase.second) {
96         VLOG(vlog_level) << "Registered optimization pass grouping " << grouping
97                          << " phase " << phase.first << ": " << pass->name();
98       }
99     }
100   }
101 }
102 
LogAllGroupings(int vlog_level)103 void OptimizationPassRegistry::LogAllGroupings(int vlog_level) {
104   for (auto group = groups_.begin(); group != groups_.end(); ++group) {
105     LogGrouping(group->first, vlog_level);
106   }
107 }
108 
109 }  // namespace tensorflow
110