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 // Dumps out the operations that are present in a serialized computation.
     17 
     18 #include <iostream>
     19 #include <memory>
     20 #include <string>
     21 
     22 #include "tensorflow/compiler/xla/client/client.h"
     23 #include "tensorflow/compiler/xla/client/client_library.h"
     24 #include "tensorflow/compiler/xla/client/computation.h"
     25 #include "tensorflow/compiler/xla/client/local_client.h"
     26 #include "tensorflow/compiler/xla/service/dfs_hlo_visitor_with_default.h"
     27 #include "tensorflow/compiler/xla/service/service.h"
     28 #include "tensorflow/compiler/xla/service/session.pb.h"
     29 #include "tensorflow/compiler/xla/statusor.h"
     30 #include "tensorflow/compiler/xla/types.h"
     31 #include "tensorflow/compiler/xla/xla_data.pb.h"
     32 #include "tensorflow/core/lib/gtl/array_slice.h"
     33 #include "tensorflow/core/lib/strings/str_util.h"
     34 #include "tensorflow/core/lib/strings/stringprintf.h"
     35 #include "tensorflow/core/platform/env.h"
     36 #include "tensorflow/core/platform/init_main.h"
     37 #include "tensorflow/core/platform/logging.h"
     38 
     39 namespace xla {
     40 namespace tools {
     41 
     42 class OperationDumper : public DfsHloVisitorWithDefault {
     43  public:
     44   explicit OperationDumper(const string& path) : path_(path) {}
     45 
     46   Status DefaultAction(HloInstruction* hlo) override {
     47     string params = tensorflow::str_util::Join(
     48         hlo->operands(), ", ", [](string* out, const HloInstruction* operand) {
     49           tensorflow::strings::StrAppend(
     50               out, ShapeUtil::HumanString(operand->shape()));
     51         });
     52     // Spit `op_name(params...) -> result_type :: path` to stdout.
     53     std::cout << tensorflow::strings::Printf(
     54         "%s :: (%s) -> %s :: %s\n", HloOpcodeString(hlo->opcode()).c_str(),
     55         params.c_str(), ShapeUtil::HumanString(hlo->shape()).c_str(),
     56         path_.c_str());
     57     return Status::OK();
     58   }
     59 
     60  private:
     61   string path_;
     62 };
     63 
     64 void RealMain(tensorflow::gtl::ArraySlice<char*> args) {
     65   LocalClient* client = ClientLibrary::LocalClientOrDie();
     66   LocalService* local_service =
     67       ClientLibrary::GetXlaService(client->platform());
     68   for (char* arg : args) {
     69     SessionModule session_module;
     70     TF_CHECK_OK(tensorflow::ReadBinaryProto(tensorflow::Env::Default(), arg,
     71                                             &session_module));
     72     auto computation_status = client->LoadSnapshot(session_module);
     73     if (!computation_status.ok()) {
     74       fprintf(stderr, "could not load snapshot for %s: %s\n", arg,
     75               computation_status.status().ToString().c_str());
     76       continue;
     77     }
     78     Computation computation = computation_status.ConsumeValueOrDie();
     79 
     80     std::unique_ptr<ProgramShape> program_shape =
     81         client->GetComputationShape(computation).ConsumeValueOrDie();
     82 
     83     std::vector<const Shape*> layouts;
     84     layouts.reserve(program_shape->parameters_size());
     85     for (int i = 0; i < program_shape->parameters_size(); ++i) {
     86       layouts.push_back(&program_shape->parameters(i));
     87     }
     88     ExecutableBuildOptions build_options;
     89     build_options.set_device_ordinal(0);
     90     build_options.set_result_layout(program_shape->result());
     91     StatusOr<std::unique_ptr<Executable>> executable =
     92         local_service->CompileExecutable(computation.handle(), layouts,
     93                                          build_options);
     94 
     95     const HloModule& module = executable.ValueOrDie()->module();
     96 
     97     OperationDumper dumper(arg);
     98     for (auto* computation : module.computations()) {
     99       TF_CHECK_OK(computation->Accept(&dumper));
    100     }
    101   }
    102 }
    103 
    104 }  // namespace tools
    105 }  // namespace xla
    106 
    107 int main(int argc, char** argv) {
    108   tensorflow::port::InitMain(argv[0], &argc, &argv);
    109 
    110   tensorflow::gtl::ArraySlice<char*> args(argv, argc);
    111   args.pop_front();  // Pop off the binary name, argv[0]
    112   xla::tools::RealMain(args);
    113   return 0;
    114 }
    115