Home | History | Annotate | Download | only in service
      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 #include "tensorflow/compiler/xla/service/hlo_pass_pipeline.h"
     17 
     18 #include <functional>
     19 
     20 #include "tensorflow/compiler/xla/service/hlo_graph_dumper.h"
     21 #include "tensorflow/compiler/xla/service/hlo_proto_util.h"
     22 #include "tensorflow/compiler/xla/status_macros.h"
     23 #include "tensorflow/compiler/xla/types.h"
     24 #include "tensorflow/compiler/xla/util.h"
     25 #include "tensorflow/core/lib/gtl/flatset.h"
     26 #include "tensorflow/core/lib/strings/str_util.h"
     27 #include "tensorflow/core/lib/strings/strcat.h"
     28 #include "tensorflow/core/platform/logging.h"
     29 
     30 using ::tensorflow::strings::StrAppend;
     31 using ::tensorflow::strings::StrCat;
     32 
     33 namespace xla {
     34 
     35 namespace {
     36 void DumpModuleGraph(const HloModule& module, const string& message) {
     37   hlo_graph_dumper::MaybeDumpHloModule(module, message);
     38   VLOG(3) << "HLO " << message << ":";
     39   XLA_VLOG_LINES(3, module.ToString());
     40 }
     41 
     42 void DumpModuleProto(const HloModule& module, const string& dump_to,
     43                      const string& pipeline_name, const string& pass_name) {
     44   static tensorflow::mutex mu(tensorflow::LINKER_INITIALIZED);
     45   static auto* const module_id_to_pass_number =
     46       new tensorflow::gtl::FlatMap<int64, int64>();
     47 
     48   tensorflow::mutex_lock lock(mu);
     49   const int64 pass_number = (*module_id_to_pass_number)[module.unique_id()]++;
     50 
     51   const string mod_name = SanitizeFileName(tensorflow::strings::Printf(
     52       "module_%04d.%04lld.%s.after_%s", module.unique_id(), pass_number,
     53       pipeline_name.c_str(), pass_name.c_str()));
     54 
     55   TF_QCHECK_OK(protobuf_util::DumpProtoToDirectory(MakeHloProto(module),
     56                                                    dump_to, mod_name));
     57 }
     58 }  // namespace
     59 
     60 StatusOr<bool> HloPassPipeline::Run(HloModule* module) {
     61   run_called_ = true;
     62 
     63   VLOG(1) << "Running HLO pass pipeline " << name();
     64 
     65   auto repeated_field =
     66       module->config().debug_options().xla_disable_hlo_passes();
     67   tensorflow::gtl::FlatSet<string> disabled_passes(repeated_field.begin(),
     68                                                    repeated_field.end());
     69   if (!disabled_passes.empty()) {
     70     VLOG(1) << "Passes disabled by --xla_disable_hlo_passes: "
     71             << tensorflow::str_util::Join(disabled_passes, ", ");
     72   }
     73 
     74   auto run_invariant_checkers = [this,
     75                                  module](const string& message) -> Status {
     76     for (auto& invariant_checker : invariant_checkers_) {
     77       VLOG(1) << "    Invariant checker " << invariant_checker->name();
     78       StatusOr<bool> changed_status = invariant_checker->Run(module);
     79       VLOG(1) << "    Invariant checker done " << invariant_checker->name();
     80       if (!changed_status.ok()) {
     81         VLOG(2) << "Module failed invariant check:";
     82         XLA_VLOG_LINES(2, module->ToString());
     83         return Status(changed_status.status().code(),
     84                       StrCat(changed_status.status().error_message(),
     85                              "\n\nFailed ", message));
     86       }
     87       TF_RET_CHECK(!changed_status.ValueOrDie())
     88           << "invariant checkers must not change the graph";
     89     }
     90     return Status::OK();
     91   };
     92 
     93   string prefix = name().ToString() + ": pipeline start";
     94   bool changed = false;
     95   string message;
     96   TF_RETURN_IF_ERROR(
     97       run_invariant_checkers(StrCat("before running pipeline: ", name())));
     98   const string xla_dump_per_pass_hlo_proto_to =
     99       module->config().debug_options().xla_dump_per_pass_hlo_proto_to();
    100   if (!xla_dump_per_pass_hlo_proto_to.empty()) {
    101     DumpModuleProto(*module, xla_dump_per_pass_hlo_proto_to, name().ToString(),
    102                     "pipeline_start");
    103   }
    104 
    105   for (auto& pass : passes_) {
    106     if (disabled_passes.count(pass->name().ToString()) > 0) {
    107       VLOG(1) << "  Skipping HLO pass " << pass->name()
    108               << ", disabled by --xla_disable_hlo_passes";
    109       continue;
    110     }
    111 
    112     VLOG(1) << "  HLO pass " << pass->name();
    113 
    114     // Emit label containing: "after foo-pass, before bar-pass".
    115     message.clear();
    116     StrAppend(&message, prefix, ", before ", pass->name());
    117     DumpModuleGraph(*module, message);
    118 
    119     TF_ASSIGN_OR_RETURN(bool changed_this_pass, pass->Run(module));
    120     TF_RETURN_IF_ERROR(
    121         run_invariant_checkers(StrCat("after running pass: ", pass->name())));
    122     if (!xla_dump_per_pass_hlo_proto_to.empty()) {
    123       DumpModuleProto(*module, xla_dump_per_pass_hlo_proto_to,
    124                       name().ToString(), pass->name().ToString());
    125     }
    126 
    127     changed |= changed_this_pass;
    128     prefix.clear();
    129     StrAppend(&prefix, name(), ": after ", pass->name());
    130   }
    131   DumpModuleGraph(*module, prefix + ", pipeline end");
    132   return changed;
    133 }
    134 
    135 }  // namespace xla
    136