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 group = groups_.find(grouping);
38 if (group != groups_.end()) {
39 const uint64 start_us = Env::Default()->NowMicros();
40 for (auto& phase : group->second) {
41 VLOG(1) << "Running optimization phase " << phase.first;
42 for (auto& pass : phase.second) {
43 VLOG(1) << "Running optimization pass: " << pass->name();
44 const uint64 pass_start_us = Env::Default()->NowMicros();
45 Status s = pass->Run(options);
46 const uint64 pass_end_us = Env::Default()->NowMicros();
47 metrics::UpdateGraphOptimizationPassTime(pass->name(),
48 pass_end_us - pass_start_us);
49 if (!s.ok()) return s;
50 if (VLOG_IS_ON(1)) {
51 if (options.graph) {
52 DumpGraphToFile(strings::StrCat("after_group_", grouping, "_phase_",
53 phase.first, "_", pass->name(), "_",
54 reinterpret_cast<uintptr_t>(
55 (*options.graph).get())),
56 **options.graph, options.flib_def);
57 }
58 if (options.partition_graphs) {
59 for (auto& part : *options.partition_graphs) {
60 DumpGraphToFile(
61 strings::StrCat(
62 "after_group_", grouping, "_phase_", phase.first, "_",
63 pass->name(), "_partition_", part.first, "_",
64 reinterpret_cast<uintptr_t>(part.second.get())),
65 *part.second, options.flib_def);
66 }
67 }
68 }
69 }
70 }
71 const uint64 end_us = Env::Default()->NowMicros();
72 metrics::UpdateGraphOptimizationPassTime("*", end_us - start_us);
73 }
74 return Status::OK();
75 }
76
LogGrouping(Grouping grouping,int vlog_level)77 void OptimizationPassRegistry::LogGrouping(Grouping grouping, int vlog_level) {
78 auto group = groups_.find(grouping);
79 if (group != groups_.end()) {
80 for (auto& phase : group->second) {
81 for (auto& pass : phase.second) {
82 VLOG(vlog_level) << "Registered optimization pass grouping " << grouping
83 << " phase " << phase.first << ": " << pass->name();
84 }
85 }
86 }
87 }
88
LogAllGroupings(int vlog_level)89 void OptimizationPassRegistry::LogAllGroupings(int vlog_level) {
90 for (auto group = groups_.begin(); group != groups_.end(); ++group) {
91 LogGrouping(group->first, vlog_level);
92 }
93 }
94
95 } // namespace tensorflow
96