summaryrefslogtreecommitdiff
path: root/compiler/plier-tf/src/Convert.cpp
blob: c36df53edf66bc55464654e4a2cf0d2646586d3c (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
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 * 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.
 */

#include <plier/tf/Convert.h>

#include <nncc/core/ADT/tensor/Shape.h>

#include <cassert>
#include <stdexcept>

namespace plier
{
namespace tf
{

bool has_attr(const tensorflow::NodeDef &node, const std::string &attr_name)
{
  return node.attr().count(attr_name) > 0;
}

bool has_attrs(const tensorflow::NodeDef &node, const std::vector<std::string> &attr_names)
{
  for (auto &attr : attr_names)
    if (!has_attr(node, attr))
      return false;
  return true;
}

tensorflow::DataType get_datatype_attr(const tensorflow::NodeDef &node,
                                       const std::string &attr_name)
{
  assert(has_attr(node, attr_name));
  const auto &attr = node.attr().at(attr_name);
  assert(attr.value_case() == tensorflow::AttrValue::kType);
  return attr.type();
}

const tensorflow::TensorShapeProto &get_shape_attr(const tensorflow::NodeDef &node,
                                                   const std::string &attr_name)
{
  assert(has_attr(node, attr_name));
  const auto &attr = node.attr().at(attr_name);
  assert(attr.value_case() == tensorflow::AttrValue::kShape);
  return attr.shape();
}

const tensorflow::TensorProto &get_tensor_attr(const tensorflow::NodeDef &node,
                                               const std::string &attr_name)
{
  assert(has_attr(node, attr_name));
  const auto &attr = node.attr().at(attr_name);
  assert(attr.value_case() == tensorflow::AttrValue::kTensor);
  return attr.tensor();
}

const ::tensorflow::AttrValue_ListValue &get_list_attr(const tensorflow::NodeDef &node,
                                                       const std::string &attr_name)
{
  assert(has_attr(node, attr_name));
  const auto &attr = node.attr().at(attr_name);
  assert(attr.value_case() == tensorflow::AttrValue::kList);
  return attr.list();
}

const std::string &get_string_attr(const tensorflow::NodeDef &node, const std::string &attr_name)
{
  assert(has_attr(node, attr_name));
  const auto &attr = node.attr().at(attr_name);
  assert(attr.value_case() == tensorflow::AttrValue::kS);
  return attr.s();
}

int64_t get_int_attr(const tensorflow::NodeDef &node, const std::string &attr_name)
{
  assert(has_attr(node, attr_name));
  const auto &attr = node.attr().at(attr_name);
  assert(attr.value_case() == tensorflow::AttrValue::kI);
  return attr.i();
}

float get_float_attr(const tensorflow::NodeDef &node, const std::string &attr_name)
{
  assert(has_attr(node, attr_name));
  const auto &attr = node.attr().at(attr_name);
  assert(attr.value_case() == tensorflow::AttrValue::kF);
  return attr.f();
}

bool get_bool_attr(const tensorflow::NodeDef &node, const std::string &attr_name)
{
  assert(has_attr(node, attr_name));
  const auto &attr = node.attr().at(attr_name);
  assert(attr.value_case() == tensorflow::AttrValue::kB);
  return attr.b();
}

std::vector<int64_t> as_int64_list(const tensorflow::AttrValue_ListValue &lv)
{
  std::vector<int64_t> vi;
  int isize = lv.i_size();

  vi.resize(isize);
  for (int i = 0; i < isize; ++i)
    vi[i] = lv.i(i);

  return vi;
}

loco::DataType as_loco_datatype(const tensorflow::DataType tf_dtype)
{
  switch (tf_dtype)
  {
    case tensorflow::DT_INT8:
      return loco::DataType::S8;
    case tensorflow::DT_UINT8:
      return loco::DataType::U8;
    case tensorflow::DT_FLOAT:
      return loco::DataType::FLOAT32;
    case tensorflow::DT_INT32:
      return loco::DataType::S32;
    case tensorflow::DT_INT64:
      return loco::DataType::S64;
    case tensorflow::DT_BOOL:
    case tensorflow::DT_STRING:
    case tensorflow::DT_COMPLEX64:
    default:
      break;
  }
  throw std::runtime_error{"Unsupported tensorflow dtype: " + tensorflow::DataType_Name(tf_dtype)};
}

DataLayout as_data_layout(const std::string &tf_layout_str)
{
  if (tf_layout_str == "NHWC")
    return DataLayout::NHWC;
  else if (tf_layout_str == "NCHW")
    return DataLayout::NCHW;
  else
    throw std::runtime_error("unknown data layout");
}

DataLayout get_data_layout(const tensorflow::NodeDef &node, const std::string &attr_name)
{
  auto layout = get_string_attr(node, attr_name);

  if (layout == "NHWC")
    return DataLayout::NHWC;
  else if (layout == "NCHW")
    return DataLayout::NCHW;
  else
    throw std::runtime_error("unknown data layout");
}

void copy_shape(const tensorflow::TensorShapeProto &tf_shape,
                nncc::core::ADT::tensor::Shape &to_shape)
{
  assert(!tf_shape.unknown_rank());

  int64_t tf_rank = tf_shape.dim_size();
  assert(tf_rank < std::numeric_limits<uint32_t>::max());

  int32_t rank = static_cast<int32_t>(tf_rank);
  to_shape.resize(rank);

  for (int32_t d = 0; d < rank; d++)
  {
    int64_t dim_value = tf_shape.dim(d).size();
    assert(dim_value < std::numeric_limits<uint32_t>::max());

    if (dim_value >= 0LL)
    {
      uint32_t dim_value32 = static_cast<uint32_t>(dim_value);
      to_shape.dim(d) = dim_value32;
    }
    else
    {
      throw std::runtime_error("Cannot handle unknown dimension");
      // TODO support unknown dimension
    }
  }
}

} // namespace tf
} // namespace plier