summaryrefslogtreecommitdiff
path: root/runtimes/pure_arm_compute/src/internal/Model.h
blob: 33ba3a8fd124dd7704de08f243ab46a7df859258 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (c) 2018 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.
 */

#ifndef __INTERNAL_MODEL_H__
#define __INTERNAL_MODEL_H__

namespace internal
{
namespace tflite
{
namespace operand
{

class Index
{
public:
  explicit Index(int value) : _value{value}
  {
    // DO NOTHING
  }

public:
  int asInt(void) const { return _value; }

private:
  int _value;
};

} // namespace operand
} // namespace tflite
} // namespace internal

#include <vector>
#include <cstdint>

#include "util/feature/Shape.h"
#include "util/matrix/Shape.h"
#include "util/kernel/Shape.h"
#include "util/tensor/Shape.h"

namespace internal
{
namespace tflite
{
namespace operand
{

struct Shape : public nnfw::util::tensor::Shape
{
public:
  Shape(uint32_t rank);

public:
  int32_t asVector(void) const;
  nnfw::util::feature::Shape asFeature(void) const;
  nnfw::util::matrix::Shape asMatrix(void) const;
  nnfw::util::kernel::Shape asKernel(void) const;
  nnfw::util::tensor::Shape asTensor(void) const;

public:
  void extendRank(size_t);
};

} // namespace operand
} // namespace tflite
} // namespace internal

#include <algorithm>

namespace internal
{
namespace tflite
{
namespace operand
{

struct Data
{
  virtual ~Data() = default;

  virtual size_t size(void) const = 0;
  virtual const uint8_t *base(void) const = 0;
};

class CachedData final : public Data
{
public:
  CachedData(const uint8_t *base, size_t size) : _base{new uint8_t[size]}, _size{size}
  {
    std::copy(base, base + size, _base);
  }

public:
  ~CachedData() { delete[] _base; }

public:
  size_t size(void) const override { return _size; }
  const uint8_t *base(void) const override { return _base; }

private:
  uint8_t *_base;
  size_t _size;
};

class ExternalData final : public Data
{
public:
  ExternalData(const uint8_t *base, size_t size) : _base{base}, _size{size}
  {
    // DO NOTHING
  }

public:
  size_t size(void) const override { return _size; }
  const uint8_t *base(void) const override { return _base; }

private:
  const uint8_t *_base;
  const size_t _size;
};

} // namespace operand
} // namespace tflite
} // namespace internal

#include <memory>
#include <cassert>
#include <functional>
#include "internal/Swizzle.h"

namespace internal
{
namespace tflite
{
namespace operand
{

class Object
{
public:
  explicit Object(const Shape &shape, const int32_t type, const float scale,
                  const int32_t zeroPoint)
      : _shape{shape}, _type{type}, _scale{scale}, _zeroPoint{zeroPoint}
  {
    // DO NOTHING
  }

public:
  const Shape &shape(void) const { return _shape; }
  const int32_t type(void) const { return _type; }
  const float scale(void) const { return _scale; }
  const int32_t zeroPoint(void) const { return _zeroPoint; }

private:
  void data(std::unique_ptr<Data> &&data) { _data = std::move(data); }

public:
  const Data &data(void) const { return *_data; }
  bool hasData(void) const { return _data != nullptr; }

public:
  template <typename T, typename... Args> void data(Args &&... args)
  {
    data(std::unique_ptr<T>(new T{std::forward<Args>(args)...}));
  }

public:
  template <typename T> T asScalar(void) const
  {
    assert((_shape.rank() == 0) || ((_shape.rank() == 1) && (_shape.dim(0) == 1)));
    assert(_data != nullptr);
    assert((_data->base() != nullptr) && (_data->size() == sizeof(T)));

    return *(reinterpret_cast<const T *>(_data->base()));
  }

public:
  template <typename T> T asReorderBits(size_t numOfBits) const
  {
    assert((_shape.rank() == 0) || ((_shape.rank() == 1) && (_shape.dim(0) == 1)));
    assert(_data != nullptr);
    assert((_data->base() != nullptr) && (_data->size() == sizeof(T)));

    return ReorderBits<T>(asScalar<T>(), numOfBits);
  }

private:
  const Shape _shape;
  const int32_t _type;
  const float _scale;
  const int32_t _zeroPoint;
  std::unique_ptr<Data> _data;
};

} // namespace operand
} // namespace tflite
} // namespace internal

#include <memory>

namespace internal
{
namespace tflite
{
namespace operand
{

class Set
{
public:
  void iterate(const std::function<void(const Index &)> &fn)
  {
    for (uint32_t n = 0; n < _objects.size(); ++n)
    {
      const Index operand_index{static_cast<int>(n)};
      fn(operand_index);
    }
  }

public:
  Index append(const Shape &, int32_t type, float scale, int32_t zeroPoint);

public:
  const Object &at(const Index &) const;
  Object &at(const Index &);
  size_t size(void) const { return _objects.size(); }

private:
  std::vector<std::unique_ptr<Object>> _objects;
};

} // namespace operand
} // namespace tflite
} // namespace internal

#include "internal/op/NodeVisitor.h"

namespace internal
{
namespace tflite
{
namespace op
{

class Sequence
{
public:
  Sequence() = default;

public:
  uint32_t size(void) const { return _ops.size(); }

public:
  op::Node &at(uint32_t nth) { return *(_ops.at(nth)); }
  const op::Node &at(uint32_t nth) const { return *(_ops.at(nth)); }

private:
  Sequence &emplace_back(std::unique_ptr<op::Node> &&node)
  {
    _ops.emplace_back(std::move(node));
    return (*this);
  }

public:
  template <typename T, typename... Args> Sequence &emplace_back(Args &&... args)
  {
    return emplace_back(std::unique_ptr<T>(new T{std::forward<Args>(args)...}));
  }

private:
  std::vector<std::unique_ptr<op::Node>> _ops;
};

} // namespace op
} // namespace tflite
} // namespace internal

namespace internal
{
namespace tflite
{

class Model
{
public:
  operand::Set &operands(void) { return _operands; }
  const operand::Set &operands(void) const { return _operands; }

public:
  op::Sequence &operations(void) { return _operations; }
  const op::Sequence &operations(void) const { return _operations; }

private:
  operand::Set _operands;
  op::Sequence _operations;

public:
  // TODO Hide these fields
  std::vector<operand::Index> inputs;
  std::vector<operand::Index> outputs;
};

} // namespace tflite
} // namespace internal

#endif // __INTERNAL_MODEL_H__