summaryrefslogtreecommitdiff
path: root/compiler/nnc/backends/soft_backend/code_snippets/cpp_header_types.def
blob: db46a1e975e15c088be64190623bc982ef3fba50 (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
/*
 * 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.
 */

#include <cstring>
#include <initializer_list>
#include <memory>
#include <cassert>
#include <algorithm>

using index_t = long;

const index_t MAX_DIMS = 8;

/** @brief Shape of Tensor object
 *
 * This class represents size of multidimensional table
 */
class Shape
{
public:

  Shape()
  {
    _dims = 0;
  }

  template <class T>
  Shape(std::initializer_list<T> data): _dims(data.size())
  {
    assert(_dims <= MAX_DIMS);
    index_t *dataPtr = _data;
    for (T value: data)
    {
      *dataPtr++ = static_cast<index_t>(value);
    }
  }

  Shape(const Shape &orig): _dims(orig._dims)
  {
    for (index_t i = 0; i < _dims; ++i)
      _data[i] = orig._data[i];
  }

  Shape &operator=(const Shape &orig)
  {
    _dims = orig._dims;
    for (index_t i = 0; i < _dims; ++i)
      _data[i] = orig._data[i];
    return *this;
  }

  /** Returns number of table dimensions*/
  index_t getDims() const
  {
    return _dims;
  }

  /** Sets number of table dimensions*/
  void setDims(index_t dims)
  {
    assert(dims < MAX_DIMS);
    _dims = dims;
  }

  /** Returns size of selected dimension*/
  index_t &operator[](index_t dim)
  {
    assert(dim < _dims);
    return _data[dim];
  }

  /** Returns size of selected dimension, constant version*/
  index_t operator[](index_t dim) const
  {
    assert(dim < _dims);
    return _data[dim];
  }

  /** Returns number of elements in table*/
  index_t getNumElems() const
  {
    index_t volume = 1;
    for (index_t i = 0; i < _dims; ++i)
    {
      volume *= _data[i];
    }
    return volume;
  }

private:
  index_t _data[MAX_DIMS];
  index_t _dims;
};

/** This class points to one cell in table*/
using Index = Shape;

/** @brief Multidimensional table
 *
 * This class represents multidimensional table.
 * It is used to provide NN model interface and intermediate objects in inference sequence.
 */
class Tensor
{
public:
  Tensor(): Tensor(Shape{}){}

  Tensor(Tensor &&orig): _shape(orig._shape), _data(orig._data), _managed(orig._managed)
  {
    orig._managed = false;
  }

  /** Constructs table, that references external data as its content*/
  Tensor(const Shape& shape, float *data): _shape(shape), _data(data){}

  Tensor(const Shape& shape): _shape(shape), _data(new float[shape.getNumElems()]), _managed(true) {}

  ~Tensor()
  {
    if (_managed)
      delete [] _data;
  }

  /** Copies data from external source into table*/
  void fillData(const float *data, const index_t num_elements)
  {
    assert(_managed);
    std::memcpy(_data, data, num_elements * sizeof(float));
  }

  Tensor& operator=(const Tensor& t) {
    if (this == &t)
      return *this;

    if (!t._managed) {
      if (_managed)
        delete [] _data;

      _managed = false;
      _data = t._data;
      _shape = t._shape;
    } else {
      // this tensor is not constant so we can write data into it
      assert(_managed);
      reshape(t._shape);
      fillData(t._data, _shape.getNumElems());
    }

    return *this;
  }

  /** Access element in table by index*/
  float &at(const Index &idx)
  {
    return *(_data + getOffset(idx));
  }

  /** Access element in table by index, constant version*/
  float at(const Index &idx) const
  {
    return *(_data + getOffset(idx));
  }

  void reshape(const Shape &shape)
  {
    index_t oldVolume = _shape.getNumElems();
    _shape = shape;
    if (_managed && oldVolume != shape.getNumElems())
    {
      float* new_data = new float[shape.getNumElems()];
      delete [] _data;
      std::swap(new_data, _data);
    }
  }

  /** Free memory, set empty shape */
  void clean()
  {
    _shape.setDims(0);
    if (_managed)
      delete [] _data;
    _managed = false;
  }

  /** Returns pointer to raw data*/
  float *getData()
  {
    return _data;
  }

  /** Returns pointer to raw data, constant version*/
  const float *getData() const
  {
    return _data;
  }

  /** Returns size object of this table*/
  const Shape &getShape() const
  {
    return _shape;
  }

private:
  index_t getOffset(const Index &idx) const
  {
    assert(idx.getDims() == _shape.getDims());
    index_t offset = 0;
    index_t stride = 1;
    for (index_t i = _shape.getDims() - 1; i >= 0; --i)
    {
      assert(idx[i] < _shape[i]);
      offset += stride * idx[i];
      stride *= _shape[i];
    }
    return offset;
  }

  Shape _shape;
  float *_data;
  bool _managed = false;
};