• 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_COMPILER_XLA_SERVICE_HLO_PASS_FIX_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PASS_FIX_H_
18 
19 #include <algorithm>
20 
21 #include "tensorflow/compiler/xla/service/hlo_module.h"
22 #include "tensorflow/compiler/xla/service/hlo_module_group.h"
23 #include "tensorflow/compiler/xla/status_macros.h"
24 #include "tensorflow/compiler/xla/statusor.h"
25 #include "tensorflow/compiler/xla/types.h"
26 #include "tensorflow/core/platform/macros.h"
27 
28 namespace xla {
29 
30 // Do an HLO pass to a fix point.
31 template <typename Pass>
32 class HloPassFix : public Pass {
33  public:
34   template <typename... Args>
HloPassFix(Args &&...args)35   explicit HloPassFix(Args&&... args) : Pass(args...) {}
36 
Run(HloModule * module)37   StatusOr<bool> Run(HloModule* module) override {
38     bool changed = false;
39     bool changed_this_iteration = true;
40     int64 iteration_count = 0;
41     const int64 kLimit = 25;
42     VLOG(3) << "Running HloPassFix on " << Pass::name();
43     while (changed_this_iteration) {
44       TF_ASSIGN_OR_RETURN(changed_this_iteration, Pass::Run(module));
45       changed |= changed_this_iteration;
46       VLOG(3) << Pass::name() << " iteration " << iteration_count
47               << " changed_this_iteration: " << changed_this_iteration;
48       ++iteration_count;
49       if (iteration_count == kLimit) {
50         VLOG(1) << "Unexpectedly high number of iterations in HLO passes '"
51                 << Pass::name() << "' for module '" << module->name()
52                 << "'. Exiting fixed point loop.";
53         // Return false in case this is fixed point is nested.
54         return false;
55       }
56     }
57     return changed;
58   }
59 
RunOnModuleGroup(HloModuleGroup * module_group)60   StatusOr<bool> RunOnModuleGroup(HloModuleGroup* module_group) override {
61     bool changed = false;
62     bool changed_this_iteration = true;
63     int64 iteration_count = 0;
64     const int64 kLimit = 25;
65     VLOG(3) << "Running HloPassFix.";
66     while (changed_this_iteration) {
67       TF_ASSIGN_OR_RETURN(changed_this_iteration,
68                           Pass::RunOnModuleGroup(module_group));
69       changed |= changed_this_iteration;
70       VLOG(3) << "changed_this_iteration: " << changed_this_iteration;
71       ++iteration_count;
72       if (iteration_count == kLimit) {
73         VLOG(1) << "Unexpectedly high number of iterations in HLO passes, "
74                    "exiting fixed point loop.";
75         // Return false in case this is fixed point is nested.
76         return false;
77       }
78     }
79     return changed;
80   }
81 };
82 
83 }  // namespace xla
84 
85 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PASS_FIX_H_
86