Home | History | Annotate | Download | only in tools
      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 <stdio.h>
     17 #include <memory>
     18 #include <string>
     19 
     20 #include "absl/types/span.h"
     21 #include "tensorflow/compiler/xla/client/client.h"
     22 #include "tensorflow/compiler/xla/client/client_library.h"
     23 #include "tensorflow/compiler/xla/client/local_client.h"
     24 #include "tensorflow/compiler/xla/client/xla_computation.h"
     25 #include "tensorflow/compiler/xla/service/hlo.pb.h"
     26 #include "tensorflow/compiler/xla/service/service.h"
     27 #include "tensorflow/compiler/xla/statusor.h"
     28 #include "tensorflow/compiler/xla/types.h"
     29 #include "tensorflow/core/platform/env.h"
     30 #include "tensorflow/core/platform/init_main.h"
     31 #include "tensorflow/core/platform/logging.h"
     32 
     33 namespace xla {
     34 namespace tools {
     35 
     36 void RealMain(absl::Span<char* const> args, bool compile) {
     37   LocalClient* client = ClientLibrary::LocalClientOrDie();
     38   LocalService* local_service =
     39       ClientLibrary::GetXlaService(client->platform());
     40   for (char* arg : args) {
     41     HloSnapshot snapshot;
     42     TF_CHECK_OK(tensorflow::ReadBinaryProto(tensorflow::Env::Default(), arg,
     43                                             &snapshot));
     44     auto computation_status = client->LoadSnapshot(snapshot);
     45     if (!computation_status.ok()) {
     46       fprintf(stderr, "could not load snapshot for %s: %s\n", arg,
     47               computation_status.status().ToString().c_str());
     48       continue;
     49     }
     50     XlaComputation computation = computation_status.ConsumeValueOrDie();
     51 
     52     if (compile) {
     53       std::unique_ptr<ProgramShape> program_shape =
     54           client->GetComputationShape(computation).ConsumeValueOrDie();
     55 
     56       std::vector<const Shape*> layouts;
     57       layouts.reserve(program_shape->parameters_size());
     58       for (int i = 0; i < program_shape->parameters_size(); ++i) {
     59         layouts.push_back(&program_shape->parameters(i));
     60       }
     61 
     62       ExecutableBuildOptions build_options;
     63       build_options.set_device_ordinal(0);
     64       build_options.set_result_layout(program_shape->result());
     65       StatusOr<std::unique_ptr<Executable>> executable =
     66           local_service->CompileExecutable(computation, layouts, build_options);
     67 
     68       const HloModule& module = executable.ValueOrDie()->module();
     69 
     70       fprintf(stdout, "HLO compiled for %s backend:\n%s\n",
     71               local_service->backend().platform()->Name().c_str(),
     72               module.ToString(HloPrintOptions::ShortParsable()).c_str());
     73     } else {
     74       auto config = HloModule::CreateModuleConfigFromProto(computation.proto(),
     75                                                            DebugOptions())
     76                         .ConsumeValueOrDie();
     77       std::unique_ptr<HloModule> module =
     78           HloModule::CreateFromProto(computation.proto(), config)
     79               .ConsumeValueOrDie();
     80 
     81       fprintf(stdout, "%s\n",
     82               module->ToString(HloPrintOptions::ShortParsable()).c_str());
     83     }
     84   }
     85 }
     86 
     87 }  // namespace tools
     88 }  // namespace xla
     89 
     90 int main(int argc, char** argv) {
     91   bool compile = false;
     92   std::vector<tensorflow::Flag> flag_list = {
     93       {"compile", &compile,
     94        "If true, compile the computation using the default client before "
     95        "dumping the HLO. Otherwise dump the raw (uncompiled) HLO."},
     96   };
     97   const xla::string usage = tensorflow::Flags::Usage(argv[0], flag_list);
     98   bool parsed_flags_ok = tensorflow::Flags::Parse(&argc, argv, flag_list);
     99   QCHECK(parsed_flags_ok) << "\n" << usage;
    100 
    101   tensorflow::port::InitMain(usage.c_str(), &argc, &argv);
    102   QCHECK(argc > 1) << "\nERROR: must specify at least one module\n" << usage;
    103 
    104   absl::Span<char* const> args(argv, argc);
    105   args.remove_prefix(1);  // Pop off the binary name, argv[0]
    106   xla::tools::RealMain(args, compile);
    107   return 0;
    108 }
    109