summaryrefslogtreecommitdiff
path: root/compiler/tfinfo/src/TensorInfoParser.cpp
blob: 9eb3da2962763273dfda556162cfb7a6ff468eee (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * 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 "nnkit/support/tftestinfo/TensorInfoParser.h"
#include "nnkit/support/tftestinfo/ParsedTensor.h"

// TODO Remove this file after code cleanup
#include "Compat.h"

#include <oops/UserExn.h>
#include <stdex/Memory.h>
#include <nncc/core/ADT/tensor/Shape.h>

#include <cctype>
#include <memory>
#include <vector>
#include <sstream>
#include <fstream>
#include <stdexcept>

/**
 * @file     TensorInfoParser.cpp
 * @brief    This file contains functions to parse test.info files in moco/test/tf
 */

namespace
{

using nnkit::support::tftestinfo::ParsedTensor;

// remove comment
void remove_comment(std::string &line)
{
  int pos = line.find_first_of("#");
  if (pos != std::string::npos)
    line.erase(pos);
}

std::string trim(const std::string &str)
{
  static const std::string whitespace = " \t";
  static const std::string empty = "";

  const auto begin = str.find_first_not_of(whitespace);
  if (begin == std::string::npos)
    return empty;

  const auto end = str.find_last_not_of(whitespace);

  return str.substr(begin, end - begin + 1);
}

ParsedTensor::Kind get_kind(const std::string &tok)
{
  if (tok == "input")
    return ParsedTensor::Kind::Input;
  else if (tok == "output")
    return ParsedTensor::Kind::Output;
  else
    throw oops::UserExn("Unrecognizable token", "token", tok);
}

TF_DataType get_dtype(const std::string &tok)
{
  if (tok == "TF_FLOAT")
    return TF_FLOAT;
  else
    throw oops::UserExn("Unsupported tensor datatype", "data type", tok);
}

bool validate_num(const std::string &num)
{
  for (int i = 0; i < num.length(); i++)
    if (!isdigit(num[i]))
      return false;

  return true;
}

bool validate_name(const std::string &tensor_name)
{
  // Note that Tensorflow tensor name format is
  // operation name ":" index, e.g., "in/placeholder:0"
  int pos = tensor_name.find(":");
  if (pos == std::string::npos)
    return false;

  if (tensor_name.length() == pos + 1) // ':' is  the last char
    return false;

  // 1. Validating operation name.
  // for naming format, refer to https://www.tensorflow.org/api_docs/python/tf/Operation#__init__
  // First char is in the form of "[A-Za-z0-9.]"
  // and the rest chars are  in the form of "[A-Za-z0-9_.\\-/]*"
  std::string op_name = tensor_name.substr(0, pos);

  // first character
  if (!(isalnum(op_name[0]) || op_name[0] == '.'))
    return false;

  // and the rest chars
  for (int i = 1; i < op_name.length(); i++)
    if (!(isalnum(op_name[i]) || std::string("_.\\-/").find(op_name[i]) != std::string::npos))
      return false;

  // 2. validating index after ":"
  std::string index = tensor_name.substr(pos + 1, op_name.length() - pos - 1);

  return validate_num(index);
}

} // namespace

namespace nnkit
{
namespace support
{
namespace tftestinfo
{

#define CHECK_NOT_NULL(x) \
  if (!(x))               \
  oops::UserExn("Cannot find required token")

/**
 * @brief Function to parse a line of test.info file
 * Examples:
 *   - "input, in/placeholder_32:0, TF_INT32, [3, 4, 2, 3]"
 *   - "output, result:0, TF_FLOAT, []"
 */
std::unique_ptr<ParsedTensor> parse_line(std::string &line)
{
  // parsed data
  ParsedTensor::Kind kind;
  std::string name;
  TF_DataType dtype;
  std::vector<int32_t> shape;

  remove_comment(line);

  if (line.length() == 0) // empty line or line with comment
    return nullptr;

  std::string tok, trimmed, dim;

  std::istringstream line_stream(line);

  CHECK_NOT_NULL(std::getline(line_stream, tok, ',')); // kind
  kind = get_kind(trim(tok));

  CHECK_NOT_NULL(std::getline(line_stream, tok, ',')); // tensor name
  trimmed = trim(tok);
  if (!validate_name(trimmed))
    throw oops::UserExn("Tensor name in wrong format", "name", tok);
  name.assign(trimmed);

  CHECK_NOT_NULL(std::getline(line_stream, tok, ',')); // data type
  dtype = get_dtype(trim(tok));

  CHECK_NOT_NULL(std::getline(line_stream, tok, '[')); // start of shape
  trimmed = trim(tok);
  if (trimmed.length())
    throw oops::UserExn("Unknown token between data type and shape", "token", tok);

  CHECK_NOT_NULL(std::getline(line_stream, tok, ']'));

  std::istringstream shape_stream(tok);

  bool first = true;
  while (std::getline(shape_stream, dim, ',')) // each dim
  {
    dim = trim(dim);

    if (first && dim.length() == 0)
      continue; // scalar
    first = false;

    if (dim.length() == 0)
      throw oops::UserExn("Empty dim in shape", "shape", tok);

    if (!validate_num(dim))
      throw oops::UserExn("Dim in shape must be a number", "dim", dim);

    shape.emplace_back(std::stoi(dim));
  }

  return stdex::make_unique<ParsedTensor>(kind, name, dtype, shape);
}

#undef CHECK_NOT_NULL

std::vector<std::unique_ptr<ParsedTensor>> parse(const char *info_path)
{
  std::ifstream infile;
  infile.open(info_path);

  if (infile.fail())
  {
    throw oops::UserExn("Fail to open file", "path", info_path);
  }

  std::vector<std::unique_ptr<ParsedTensor>> tensors;

  std::string line;
  while (std::getline(infile, line))
  {
    auto tensor = parse_line(line);
    if (tensor)
      tensors.emplace_back(std::move(tensor));
  }

  return tensors;
}

} // namespace tftestinfo
} // namespace support
} // namespace nnkit