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