summaryrefslogtreecommitdiff
path: root/compiler/coco/generic/src/IR/Data.cpp
blob: b719472539721b1e6d5ce1bc159c08a11b77f26d (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
/*
 * 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 "coco/IR/Data.h"

#include <nncc/core/ADT/kernel/NCHWLayout.h>
#include <nncc/core/ADT/kernel/Overlay.h>

#include <stdex/Memory.h>

#include <map>

using namespace nncc::core::ADT;

using stdex::make_unique;

namespace
{
class BlobContext
{
public:
  void allocate(const coco::Bag *b, uint32_t elemsize)
  {
    auto buffer = make_unique<std::vector<uint8_t>>();
    buffer->resize(b->size() * elemsize);

    _data[b] = std::move(buffer);
  }

  void release(const coco::Bag *b) { _data.erase(b); }

public:
  uint8_t *at(const coco::Bag *b)
  {
    auto it = _data.find(b);

    if (it != _data.end())
    {
      return it->second->data();
    }

    return nullptr;
  }

public:
  uint32_t size(const coco::Bag *b) const
  {
    auto it = _data.find(b);

    if (it != _data.end())
    {
      return it->second->size();
    }

    return 0;
  }

private:
  std::map<const coco::Bag *, std::unique_ptr<std::vector<uint8_t>>> _data;
};
}

namespace
{

template <typename T> class KernelOverlay : public kernel::Reader<T>, public kernel::Accessor<T>
{
public:
  KernelOverlay(T *base, const coco::KernelObject *object) : _base{base}, _object{object}
  {
    // DO NOTHING
  }

public:
  T at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) const override
  {
    assert(_object->layout() != nullptr);
    auto offset = _object->layout()->at(nth, ch, row, col);
    return *(_base + offset.value());
  }

public:
  T &at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) override
  {
    assert(_object->layout() != nullptr);
    auto offset = _object->layout()->at(nth, ch, row, col);
    return *(_base + offset.value());
  }

private:
  T *_base;
  const coco::KernelObject *_object;
};

} // namespace

namespace
{
template <typename T> class PlainWeightContextImpl final : public coco::PlainWeightContext<T>
{
public:
  PlainWeightContextImpl(BlobContext *blob) : _blob{blob}
  {
    // DO NOTHING
  }

public:
  PlainWeightContextImpl(const PlainWeightContextImpl &) = delete;
  PlainWeightContextImpl(PlainWeightContextImpl &&) = delete;

public:
  coco::Span<T> allocate(const coco::Bag *bag) override
  {
    assert(bag != nullptr);
    _blob->allocate(bag, sizeof(T));
    return weight(bag);
  }

  coco::Span<T> weight(const coco::Bag *b) override
  {
    // TODO Check type later
    if (auto data = _blob->at(b))
    {
      uint32_t byte_size = _blob->size(b);
      assert(byte_size % sizeof(T) == 0);
      uint32_t elem_size = static_cast<uint32_t>(byte_size / sizeof(T));

      return coco::Span<T>{reinterpret_cast<T *>(data), elem_size};
    }

    return coco::Span<T>{nullptr, 0};
  }

public:
  std::unique_ptr<kernel::Accessor<T>> access(const coco::KernelObject *o) override
  {
    auto b = o->bag();
    assert(b != nullptr);

    if (auto base = reinterpret_cast<T *>(_blob->at(b)))
    {
      return make_unique<KernelOverlay<T>>(base, o);
    }

    return nullptr;
  }

public:
  std::unique_ptr<kernel::Reader<T>> read(const coco::KernelObject *o) const override
  {
    auto b = o->bag();
    assert(b != nullptr);

    if (auto base = reinterpret_cast<T *>(_blob->at(b)))
    {
      return make_unique<KernelOverlay<T>>(base, o);
    }

    return nullptr;
  }

private:
  BlobContext *const _blob;
};
} // namespace

namespace
{
struct DataImpl final : public coco::Data
{
  std::unique_ptr<BlobContext> _blob;
  std::unique_ptr<PlainWeightContextImpl<float>> _fp32;

  bool allocated(const coco::Bag *b) const override { return _blob->at(b) != nullptr; }

  void release(const coco::Bag *b) override
  {
    assert(allocated(b));
    _blob->release(b);
  }

  coco::PlainWeightContext<float> *f32(void) override { return _fp32.get(); }
  const coco::PlainWeightContext<float> *f32(void) const override { return _fp32.get(); }
};
} // namespace

namespace coco
{

std::unique_ptr<Data> Data::create(void)
{
  auto blob = make_unique<BlobContext>();
  auto fp32 = make_unique<PlainWeightContextImpl<float>>(blob.get());

  auto data = make_unique<DataImpl>();

  data->_blob = std::move(blob);
  data->_fp32 = std::move(fp32);

  // GCC 4.9 tries to copy data (while GCC 6.X doesn't)
  return std::move(data);
}

} // namespace coco