summaryrefslogtreecommitdiff
path: root/compiler/tfl-inspect/src/Dump.cpp
blob: 8d879a84e80b59a6b38161da44622d0b4fe8a1b5 (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
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. 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.
 */

#include "Dump.h"
#include "Reader.h"

#include <string>
#include <ostream>
#include <stdexcept>

namespace tflinspect
{

void DumpOperators::run(std::ostream &os, const tflite::Model *model)
{
  tflinspect::Reader reader(model);

  assert(reader.num_subgraph() == 1);
  reader.select_subgraph(0);

  auto ops = reader.operators();

  // dump operators
  for (uint32_t i = 0; i < ops->Length(); ++i)
  {
    const auto op = ops->Get(i);

    auto op_name = reader.opcode_name(op);

    os << op_name << std::endl;
  }
}

} // namespace tflinspect

namespace
{

const tflite::Operator *operator_match_output(tflinspect::Reader &reader, const int32_t tensor)
{
  auto ops = reader.operators();

  for (uint32_t i = 0; i < ops->Length(); ++i)
  {
    const auto op = ops->Get(i);

    const std::vector<int32_t> &outputs = tflinspect::as_index_vector(op->outputs());

    for (auto output : outputs)
    {
      if (output == tensor)
        return op;
    }
  }
  return nullptr;
}

size_t tensor_buffer_size(tflinspect::Reader &reader, const int32_t tensor_id)
{
  auto tensors = reader.tensors();

  if (tensor_id < 0 || tensor_id >= tensors->Length())
  {
    throw std::runtime_error("Invalid Tensor ID");
  }

  auto tensor = tensors->Get(tensor_id);
  auto buffer_id = tensor->buffer();

  size_t size = reader.buffer_info(buffer_id, nullptr);

  return size;
}

} // namespace

namespace tflinspect
{

void DumpConv2DWeight::run(std::ostream &os, const tflite::Model *model)
{
  tflinspect::Reader reader(model);

  assert(reader.num_subgraph() == 1);
  reader.select_subgraph(0);

  auto ops = reader.operators();

  // dump Conv2D, DepthwiseConv2D and its weight input operator
  for (uint32_t i = 0; i < ops->Length(); ++i)
  {
    const auto op = ops->Get(i);
    auto bc = reader.builtin_code(op);

    if (bc == tflite::BuiltinOperator_CONV_2D || bc == tflite::BuiltinOperator_DEPTHWISE_CONV_2D)
    {
      const std::vector<int32_t> &inputs = tflinspect::as_index_vector(op->inputs());
      if (inputs.size() < 2)
      {
        throw std::runtime_error("Operator has invalid input");
      }
      auto weight_input = inputs[1]; // Tensor ID of weight input

      const auto op_weight = operator_match_output(reader, weight_input);
      const auto buffer_size = tensor_buffer_size(reader, weight_input);

      std::string weight_op_name = "?";

      if (op_weight == nullptr && buffer_size > 0)
      {
        weight_op_name = "CONST";
      }
      else if (op_weight != nullptr)
      {
        weight_op_name = reader.opcode_name(op_weight);
      }

      auto op_name = reader.opcode_name(op);
      os << op_name << "," << weight_op_name << std::endl;
    }
  }
}

} // namespace tflinspect