summaryrefslogtreecommitdiff
path: root/compiler/luci/export/src/CircleExporterUtils.cpp
blob: 1272facb270724153f4e12c81a5375f769d09f40 (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
/*
 * Copyright (c) 2020 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 "CircleExporterUtils.h"

#include <oops/InternalExn.h>

#include <cassert>
#include <memory>

namespace luci
{

circle::ActivationFunctionType to_circle_actfunc(luci::FusedActFunc func)
{
  switch (func)
  {
    case luci::FusedActFunc::NONE:
      return circle::ActivationFunctionType_NONE;
    case luci::FusedActFunc::RELU:
      return circle::ActivationFunctionType_RELU;
    case luci::FusedActFunc::RELU_N1_TO_1:
      return circle::ActivationFunctionType_RELU_N1_TO_1;
    case luci::FusedActFunc::RELU6:
      return circle::ActivationFunctionType_RELU6;
    default:
      INTERNAL_EXN_V("trying to convert unsupported luci::FusedActFunc", oops::to_uint32(func));
  }
}

circle::TensorType to_circle_tensortype(loco::DataType type)
{
  switch (type)
  {
    case loco::DataType::U8:
      return circle::TensorType_UINT8;

    case loco::DataType::S8:
      return circle::TensorType_INT8;
    case loco::DataType::S16:
      return circle::TensorType_INT16;
    case loco::DataType::S32:
      return circle::TensorType_INT32;
    case loco::DataType::S64:
      return circle::TensorType_INT64;

    case loco::DataType::FLOAT16:
      return circle::TensorType_FLOAT16;
    case loco::DataType::FLOAT32:
      return circle::TensorType_FLOAT32;

    case loco::DataType::BOOL:
      return circle::TensorType_BOOL;

    default:
      INTERNAL_EXN_V("failed to convert unsupported loco::DataType", oops::to_uint32(type));
  }
}

} // namespace luci

namespace luci
{

uint32_t SerializedModelData::registerBuiltinOpcode(circle::BuiltinOperator builtin_code)
{
  auto it = _operator_codes.find(OpCode{builtin_code});
  if (it != _operator_codes.end())
  {
    return it->second;
  }
  auto idx = static_cast<uint32_t>(_operator_codes.size());
  _operator_codes.emplace(OpCode{builtin_code}, idx);
  return idx;
}

uint32_t SerializedModelData::registerCustomOpcode(const std::string &custom_op)
{
  circle::BuiltinOperator custom_code = circle::BuiltinOperator_CUSTOM;
  auto idx = registerBuiltinOpcode(custom_code);
  _custom_operator_codes.emplace(OpCode{custom_code}, custom_op);
  return idx;
}

circle::Padding getOpPadding(const loco::Padding2D *pad, const loco::Stride<2> *stride,
                             const ShapeDescription &ifm, const ShapeDescription &ofm)
{
  // VALID padding
  if (pad->top() == 0 && pad->bottom() == 0 && pad->left() == 0 && pad->right() == 0)
    return circle::Padding_VALID;

  // SAME padding
  //
  // For same padding, by definition, following equation should hold:
  //   O = floor((I - 1) / S) + 1
  //   where input size I, output size O, stride S
  //
  // NOTE input and output 'feature' map are shape of NHWC
  bool same_padding_criterion_1 =
      (static_cast<uint32_t>(ofm._dims[1]) == (ifm._dims[1] - 1) / stride->vertical() + 1) &&
      (static_cast<uint32_t>(ofm._dims[2]) == (ifm._dims[2] - 1) / stride->horizontal() + 1);

  // For same padding, rear padding is same or bigger than front padding by at most 1
  bool same_padding_criterion_2 =
      (pad->top() <= pad->bottom()) && (pad->bottom() <= pad->top() + 1) &&
      (pad->left() <= pad->right()) && (pad->right() <= pad->left() + 1);

  if (same_padding_criterion_1 && same_padding_criterion_2)
    return circle::Padding_SAME;

  INTERNAL_EXN("Unsupported padding criteria");
}

circle::Padding getOpPadding(const luci::Padding pad)
{
  if (pad == luci::Padding::VALID)
    return circle::Padding_VALID;
  if (pad == luci::Padding::SAME)
    return circle::Padding_SAME;

  INTERNAL_EXN_V("Unsupported luci::Padding", oops::to_uint32(pad));
}

namespace
{

class CircleTensorIndexAnnotation final : public loco::NodeAnnotation
{
public:
  CircleTensorIndexAnnotation(const CircleTensorIndex &index) : _index{index}
  {
    // DO NOTHING
  }

public:
  const CircleTensorIndex &index(void) const { return _index; }

private:
  CircleTensorIndex _index;
};

} // namespace

void set_tensor_index(loco::Node *node, const CircleTensorIndex &tensor_id)
{
  assert(node->annot<CircleTensorIndexAnnotation>() == nullptr);
  node->annot(std::make_unique<CircleTensorIndexAnnotation>(tensor_id));
}

CircleTensorIndex get_tensor_index(loco::Node *node)
{
  assert(node->annot<CircleTensorIndexAnnotation>() != nullptr);
  return node->annot<CircleTensorIndexAnnotation>()->index();
}

} // namespace luci