aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/hvx/hvx_ops_support_checker/hvx_ops_support_checker_main.cc
blob: 6ae7c4a7420e8d7a58bc0a83e14e792b442f6d5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// bazel build tensorflow/contrib/hvx/hvx_ops_support_checker &&
// bazel-bin/tensorflow/contrib/hvx/hvx_ops_support_checker/hvx_ops_support_checker
// \
// --in_graph=graph_def.pb

#include <unordered_set>

#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/node_def.pb.h"
#include "tensorflow/core/framework/remote_fused_graph_execute_info.pb.h"
#include "tensorflow/core/kernels/hexagon/hexagon_ops_definitions.h"
#include "tensorflow/core/kernels/remote_fused_graph_execute_utils.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/core/platform/init_main.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/util/command_line_flags.h"
#include "tensorflow/tools/graph_transforms/transform_utils.h"

namespace tensorflow {
namespace {
static int ParseFlags(int argc, char* argv[], string* in_graph) {
  std::vector<Flag> flag_list = {
      Flag("in_graph", in_graph, "input graph file name"),
  };
  CHECK(Flags::Parse(&argc, argv, flag_list));
  // We need to call this to set up global state for TensorFlow.
  port::InitMain(argv[0], &argc, &argv);

  string usage = Flags::Usage(argv[0], flag_list);
  CHECK(!in_graph->empty()) << "in_graph graph can't be empty.\n" << usage;

  return 0;
}

static void SummarizeNode(const NodeDef& node_def) {
  LOG(INFO) << "Node(" << node_def.name() << ")";
  LOG(INFO) << "  op: " << node_def.op();
  for (const string& input : node_def.input()) {
    LOG(INFO) << " Input: " << input;
  }
}

static void DumpRemoteFusedGraph(const NodeDef& node_def) {
  LOG(INFO) << "Remote fused graph found.";
  RemoteFusedGraphExecuteInfo info;
  string serialized_proto;
  GetNodeAttr(node_def,
              RemoteFusedGraphExecuteUtils::
                  ATTR_SERIALIZED_REMOTE_FUSED_GRAPH_EXECUTE_INFO,
              &serialized_proto)
      .IgnoreError();
  info.ParseFromString(serialized_proto);
  LOG(INFO) << "Node name: " << node_def.name();
  LOG(INFO) << "Executor name: " << info.executor_name();
  for (const string& input : info.graph_input_node_name()) {
    LOG(INFO) << "Input: " << input;
  }
  for (const RemoteFusedGraphExecuteInfo::TensorShapeTypeProto& shape_type :
       info.default_graph_input_tensor_shape()) {
    LOG(INFO) << "Input shape type: " << shape_type.DebugString();
  }
  for (const string& output : info.graph_output_node_name()) {
    LOG(INFO) << "Output: " << output;
  }
  for (const RemoteFusedGraphExecuteInfo::TensorShapeTypeProto& shape_type :
       info.default_graph_output_tensor_shape()) {
    LOG(INFO) << "Output shape type: " << shape_type.DebugString();
  }
  const int subgraph_node_size = info.remote_graph().node_size();
  LOG(INFO) << "Nodes in the graph: " << subgraph_node_size;
  for (int i = 0; i < subgraph_node_size; ++i) {
    LOG(INFO) << "node(" << i << "): " << info.remote_graph().node(i).name();
  }
}

static void CheckOpsSupport(const GraphDef& graph_def) {
  const IGraphTransferOpsDefinitions& ops_definition =
      HexagonOpsDefinitions::getInstance();
  LOG(INFO) << "Checking " << graph_def.node_size() << " nodes";

  std::unordered_set<string> unsupported_ops;
  bool all_supported = true;
  bool contains_remote_graph = false;
  for (const NodeDef& node : graph_def.node()) {
    if (node.op() == "RemoteFusedGraphExecute") {
      contains_remote_graph = true;
      DumpRemoteFusedGraph(node);
      continue;
    }
    // TODO(satok): Set correct data type if it's given.
    const int op_id = ops_definition.GetOpIdFor(node.op(), {});
    if (op_id == IGraphTransferOpsDefinitions::INVALID_OP_ID) {
      all_supported = false;
      LOG(ERROR) << "OP type: " << node.op() << " is not supported on hvx. "
                 << "Name = " << node.name();
      unsupported_ops.emplace(node.op());
    }
  }

  LOG(INFO) << "\n";
  LOG(INFO) << "Unsupported ops:";
  int count = 0;
  for (const string& op_type : unsupported_ops) {
    LOG(INFO) << "(" << (++count) << ") " << op_type;
  }
  if (count == 0) {
    LOG(INFO) << "All ops supported!";
  } else {
    LOG(INFO) << count << " ops are not supported.";
  }

  if (contains_remote_graph) {
    for (const NodeDef& node : graph_def.node()) {
      SummarizeNode(node);
    }
  }
}

}  // namespace
}  // namespace tensorflow

int main(int argc, char** argv) {
  tensorflow::string in_graph;
  const int ret = tensorflow::ParseFlags(argc, argv, &in_graph);
  if (ret != 0) {
    return ret;
  }

  tensorflow::GraphDef graph_def;
  TF_CHECK_OK(tensorflow::graph_transforms::LoadTextOrBinaryGraphFile(
      in_graph, &graph_def));

  tensorflow::CheckOpsSupport(graph_def);
  return 0;
}