summaryrefslogtreecommitdiff
path: root/runtimes/neurun/src/frontend/execution.cc
blob: 5f1729b30019f3f2b95f84585c977798ea6ce2b8 (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
320
321
322
323
324
325
326
327
328
/*
 * 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 <NeuralNetworks.h>

#include <new>

#include "frontend/wrapper/compilation.h"
#include "frontend/wrapper/execution.h"
#include "frontend/wrapper/event.h"

#include "model/operand/DataType.h"
#include "model/operand/Index.h"
#include "graph/operand/Layout.h"
#include "backend/BackendManager.h"
#include "backend/interface/IConfig.h"
#include "compiler/BackendResolver.h"
#include "compiler/TensorInfo.h"
#include "backend/interface/operand/ITensor.h"

inline void source(ANeuralNetworksExecution *execution,
                   const ::neurun::model::operand::DataType &type, int32_t index,
                   const void *buffer, size_t length)
{
  const auto &operands = execution->plan().model().operands();
  neurun::model::operand::IO::Index input_index{index};

  const auto operand_index = execution->plan().model().getInputs().at(input_index);
  auto operand = &operands.at(operand_index);
  auto operand_li = operand->lower_info();
  const auto output_backend = operand_li->def_backends().getOnlyElement();
  const auto output_layout = output_backend->config()->getOperandLayout();
  auto input_layout = execution->plan()
                          .model()
                          .backend_resolver()
                          ->getDefaultBackend()
                          ->config()
                          ->getOperandLayout();
  if (input_layout == neurun::graph::operand::Layout::NHWC &&
      output_layout == neurun::graph::operand::Layout::NCHW)
  {
    const auto tensor_info = neurun::compiler::TensorInfo(operand->shape(), operand->typeInfo());

    execution->source<::neurun::exec::PermutateSource>(index, buffer, tensor_info.total_size(),
                                                       operand->shape());
    return;
  }
  using ::neurun::model::operand::DataType;
  switch (type)
  {
    case DataType::SCALAR_FLOAT32:
    case DataType::TENSOR_FLOAT32:
      execution->source<::neurun::exec::Source<float>>(
          index, reinterpret_cast<const float *>(buffer), length);
      break;
    case DataType::SCALAR_INT32:
    case DataType::TENSOR_INT32:
      execution->source<::neurun::exec::Source<int32_t>>(
          index, reinterpret_cast<const int32_t *>(buffer), length);
      break;
    case DataType::SCALAR_UINT32:
      execution->source<::neurun::exec::Source<uint32_t>>(
          index, reinterpret_cast<const uint32_t *>(buffer), length);
      break;
    case DataType::TENSOR_QUANT8_ASYMM:
      execution->source<::neurun::exec::Source<uint8_t>>(
          index, reinterpret_cast<const uint8_t *>(buffer), length);
      break;
    default:
      throw std::runtime_error("Not supported, yet");
      break;
  }
}

inline void sink(ANeuralNetworksExecution *execution,
                 const ::neurun::model::operand::DataType &type, int32_t index, void *buffer,
                 size_t length)
{
  const auto &operands = execution->plan().model().operands();
  neurun::model::operand::IO::Index input_index{index};

  const auto operand_index = execution->plan().model().getOutputs().at(input_index);
  auto operand = &operands.at(operand_index);
  auto operand_li = operand->lower_info();
  const auto input_backend = operand_li->def_backends().getOnlyElement();
  const auto input_layout = input_backend->config()->getOperandLayout();
  auto output_layout = execution->plan()
                           .model()
                           .backend_resolver()
                           ->getDefaultBackend()
                           ->config()
                           ->getOperandLayout();
  if (input_layout == neurun::graph::operand::Layout::NCHW &&
      output_layout == neurun::graph::operand::Layout::NHWC)
  {
    const auto tensor_info = neurun::compiler::TensorInfo(operand->shape(), operand->typeInfo());

    execution->sink<::neurun::exec::PermutateSink>(index, buffer, tensor_info.total_size(),
                                                   operand->shape());
    return;
  }
  using ::neurun::model::operand::DataType;
  switch (type)
  {
    case DataType::SCALAR_FLOAT32:
    case DataType::TENSOR_FLOAT32:
      execution->sink<::neurun::exec::Sink<float>>(index, reinterpret_cast<float *>(buffer),
                                                   length);
      break;
    case DataType::SCALAR_INT32:
    case DataType::TENSOR_INT32:
      execution->sink<::neurun::exec::Sink<int32_t>>(index, reinterpret_cast<int32_t *>(buffer),
                                                     length);
      break;
    case DataType::SCALAR_UINT32:
      execution->sink<::neurun::exec::Sink<uint32_t>>(index, reinterpret_cast<uint32_t *>(buffer),
                                                      length);
      break;
    case DataType::TENSOR_QUANT8_ASYMM:
      execution->sink<::neurun::exec::Sink<uint8_t>>(index, reinterpret_cast<uint8_t *>(buffer),
                                                     length);
      break;
    default:
      throw std::runtime_error("Not supported, yet");
      break;
  }
}

//
// NNAPI Implementation
//
int ANeuralNetworksExecution_create(ANeuralNetworksCompilation *compilation,
                                    ANeuralNetworksExecution **execution)
{
  if ((compilation == nullptr) || (execution == nullptr))
  {
    return ANEURALNETWORKS_UNEXPECTED_NULL;
  }

  // Can handle compiled state only
  if (compilation->plan().state() != neurun::compiler::State::COMPILED)
  {
    return ANEURALNETWORKS_BAD_STATE;
  }

  std::shared_ptr<const neurun::compiler::Plan> plan;

  compilation->publish(plan);

  *execution = new (std::nothrow) ANeuralNetworksExecution{plan};
  if (*execution == nullptr)
  {
    return ANEURALNETWORKS_OUT_OF_MEMORY;
  }

  return ANEURALNETWORKS_NO_ERROR;
}

int ANeuralNetworksExecution_setInput(ANeuralNetworksExecution *execution, int32_t index,
                                      const ANeuralNetworksOperandType * /* type */,
                                      const void *buffer, size_t length)
{
  // Don't check type
  // Comment about ANeuralNetworksOperandType in NeuralNetworks.h:
  //  If the input or output is optional and omitted then it need not have a fully specified tensor
  //  operand type
  if ((execution == nullptr) || ((buffer == nullptr) && (length != 0)))
  {
    return ANEURALNETWORKS_UNEXPECTED_NULL;
  }

  // TODO Handle optional input
  if (buffer == nullptr)
  {
    throw std::runtime_error("Not supported optional input, yet");
  }

  const auto &operands = execution->plan().model().operands();

  // TODO Check type conflicts

  neurun::model::operand::IO::Index input_index{index};

  const auto operand_index = execution->plan().model().getInputs().at(input_index);
  const auto data_type = operands.at(operand_index).typeInfo().type();
  const auto operand_shape = operands.at(operand_index).shape();

  source(execution, data_type, index, buffer, length);

  return ANEURALNETWORKS_NO_ERROR;
}

int ANeuralNetworksExecution_setOutput(ANeuralNetworksExecution *execution, int32_t index,
                                       const ANeuralNetworksOperandType * /* type */, void *buffer,
                                       size_t length)
{
  // Don't check type
  // Comment about ANeuralNetworksOperandType in NeuralNetworks.h:
  //  If the input or output is optional and omitted then it need not have a fully specified tensor
  //  operand type
  if ((execution == nullptr) || ((buffer == nullptr) && (length != 0)))
  {
    return ANEURALNETWORKS_UNEXPECTED_NULL;
  }

  // Handle optional output
  if (buffer == nullptr)
  {
    return ANEURALNETWORKS_NO_ERROR;
  }

  const auto &operands = execution->plan().model().operands();

  // TODO Check type conflicts

  neurun::model::operand::IO::Index output_index{index};

  const auto operand_index = execution->plan().model().getOutputs().at(output_index);
  const auto data_type = operands.at(operand_index).typeInfo().type();
  const auto operand_shape = operands.at(operand_index).shape();

  sink(execution, data_type, index, buffer, length);

  return ANEURALNETWORKS_NO_ERROR;
}

int ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution *execution,
                                          ANeuralNetworksEvent **event)
{
  if ((execution == nullptr) || (event == nullptr))
  {
    return ANEURALNETWORKS_UNEXPECTED_NULL;
  }

  // TODO: Handle event
  *event = new (std::nothrow) ANeuralNetworksEvent{};
  if (*event == nullptr)
  {
    return ANEURALNETWORKS_OUT_OF_MEMORY;
  }

  const auto &plan = execution->plan();
  const auto &model = plan.model();

  // Set input(s)
  for (uint32_t n = 0; n < model.getInputs().size(); ++n)
  {
    auto setter = [&](::neurun::backend::operand::ITensor &tensor) {
      execution->source(n).push(tensor);
    };

    neurun::model::operand::IO::Index input_index{n};

    ::neurun::model::operand::Index index{model.getInputs().at(input_index)};
    auto object = plan.operands().at(index);

    object->access(setter);
  }

  const auto &operations = execution->plan().operations();

  for (uint32_t n = 0; n < operations.size(); ++n)
  {
    operations.at(n).run();
  }

  // Get output(s)
  for (uint32_t n = 0; n < model.getOutputs().size(); ++n)
  {
    auto getter = [&](::neurun::backend::operand::ITensor &tensor) {
      execution->sink(n).pull(tensor);
    };

    neurun::model::operand::IO::Index output_index{n};

    ::neurun::model::operand::Index index{model.getOutputs().at(output_index)};
    auto object = plan.operands().at(index);

    object->access(getter);
  }

  return ANEURALNETWORKS_NO_ERROR;
}

void ANeuralNetworksExecution_free(ANeuralNetworksExecution * /* execution */) {}

int ANeuralNetworksExecution_setInputFromMemory(ANeuralNetworksExecution *execution,
                                                int32_t /* index */,
                                                const ANeuralNetworksOperandType * /* type */,
                                                const ANeuralNetworksMemory *memory,
                                                size_t /* offset */, size_t /* length */)
{
  if ((execution == nullptr) || (memory == nullptr))
  {
    return ANEURALNETWORKS_UNEXPECTED_NULL;
  }

  // NYI
  return ANEURALNETWORKS_NO_ERROR;
}

int ANeuralNetworksExecution_setOutputFromMemory(ANeuralNetworksExecution *execution,
                                                 int32_t /* index */,
                                                 const ANeuralNetworksOperandType * /* type */,
                                                 const ANeuralNetworksMemory *memory,
                                                 size_t /* offset */, size_t /* length */)
{
  if ((execution == nullptr) || (memory == nullptr))
  {
    return ANEURALNETWORKS_UNEXPECTED_NULL;
  }

  // NYI
  return ANEURALNETWORKS_NO_ERROR;
}