summaryrefslogtreecommitdiff
path: root/runtimes/neurun/core/src/exec/Sink.h
blob: 7ec3efa22ccfb108b3bb03cecbff2bb51b2f9cfb (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
/*
 * 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 __NEURUN_EXEC_SINK_H__
#define __NEURUN_EXEC_SINK_H__

#include <cassert>

#include "cpp14/memory.h"
#include "util/feature/nhwc/View.h"
#include "util/feature/nchw/View.h"
#include "util/Utils.h"
#include <misc/feature/IndexIterator.h>

namespace neurun
{
namespace exec
{
struct ISink
{
  virtual ~ISink() = default;

  virtual void pull(::neurun::backend::operand::ITensor &tensor) const = 0;
};

// Create second lever inheritance: the first lever is used as a reference type in use-case places
template <typename T> class ITemplSink : public ISink
{
public:
  ITemplSink(void *output_buffer, const size_t &output_size, const model::Shape &shape,
             const bool copy)
      : _output_buffer{reinterpret_cast<T *>(output_buffer)}, _output_size{output_size},
        _shape{shape}, _copy{copy}
  {
  }

protected:
  void pullUnif(neurun::backend::operand::ITensor &tensor) const
  {
    auto input_buffer = tensor.buffer();
    auto rank = _shape.rank();

    if (!tensor.has_padding() && rank < 4 + _copy)
    {
      memcpy(_output_buffer, input_buffer, _output_size);
      return;
    }

    switch (rank)
    {
      case 0:
      case 1:
      {
        memcpy(_output_buffer, input_buffer, _output_size);
        break;
      }
      case 2:
      {
        const int32_t copy_len = _shape.dim(1);

        for (auto i = 0; i < _shape.dim(0); ++i)
        {
          neurun::util::Coordinates coords{i, 0};
          memcpy(_output_buffer + i * copy_len, input_buffer + tensor.calcOffset(coords),
                 copy_len * sizeof(T));
        }
        break;
      }
      case 3:
      {
        const int32_t dim1 = _shape.dim(1);
        const int32_t dim2 = _shape.dim(2);

        for (auto i = 0; i < _shape.dim(0); ++i)
        {
          for (auto j = 0; j < _shape.dim(1); ++j)
          {
            neurun::util::Coordinates coords{i, j, 0};
            memcpy(_output_buffer + i * dim1 * dim2 + j * dim2,
                   input_buffer + tensor.calcOffset(coords), dim2 * sizeof(T));
          }
        }
        break;
      }
      case 4:
      {
        if (_copy)
        {
          const int32_t dim1 = _shape.dim(1);
          const int32_t dim2 = _shape.dim(2);
          const int32_t dim3 = _shape.dim(3);

          for (auto i = 0; i < _shape.dim(0); ++i)
          {
            for (auto j = 0; j < _shape.dim(1); ++j)
            {
              for (auto k = 0; k < _shape.dim(2); ++k)
              {
                neurun::util::Coordinates coords{i, j, k, 0};
                memcpy(_output_buffer + i * dim1 * dim2 * dim3 + j * dim2 * dim3 + k * dim3,
                       input_buffer + tensor.calcOffset(coords), dim3 * sizeof(T));
              }
            }
          }
        }
        else
        {
          // TODO Support from nhwc to nchw
          auto feature = _shape.asFeature(model::Layout::NHWC);

          const util::feature::nchw::View<T> from{&tensor};
          util::feature::nhwc::View<T> into{feature, _output_buffer, _output_size};

          ::nnfw::misc::feature::iterate(feature)
              << [&](uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) {
                   const auto value = from.at(batch, ch, row, col);
                   into.at(batch, ch, row, col) = value;
                 };
        }
        break;
      }
      default:
        throw std::runtime_error("NYI");
        break;
    }
  }

private:
  T *_output_buffer;
  const size_t _output_size;
  const model::Shape _shape;
  const bool _copy;
};

template <typename T> class PermutateSink final : public ITemplSink<T>
{
public:
  PermutateSink(void *output_buffer, const size_t &output_size, const model::Shape &shape)
      : ITemplSink<T>(output_buffer, output_size, shape, false)
  {
  }

public:
  void pull(neurun::backend::operand::ITensor &tensor) const override
  {
    ITemplSink<T>::pullUnif(tensor);
  }
};

// Only supports NHWC format front-end(NNAPI) now
template <typename T> class CopySink final : public ITemplSink<T>
{
public:
  CopySink(void *output_buffer, const size_t &output_size, const model::Shape &shape)
      : ITemplSink<T>(output_buffer, output_size, shape, true)
  {
  }

public:
  void pull(neurun::backend::operand::ITensor &tensor) const override
  {
    ITemplSink<T>::pullUnif(tensor);
  }
};

} // namespace exec
} // namespace neurun

#endif // __NEURUN_EXEC_SINK_H__