summaryrefslogtreecommitdiff
path: root/runtimes/neurun/src/frontend/execution.cc
blob: ff34921b736d810709edb3609c94b369cb2ff67b (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
/*
 * 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 "graph/operand/Index.h"

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

  std::shared_ptr<const neurun::codegen::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;
  }

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

  // TODO Check type conflicts

  // NOTE The current implemenation assumes that every input is a feature map.
  // TODO Remove this assumption
  neurun::graph::operand::IO::Index input_index{index};

  const auto operand_index = execution->plan().model().getInputs().at(input_index);

  if (operands.at(operand_index).shape().rank() == 2)
  {
    assert(operands.at(operand_index).shape().dim(0) == 1);

    const auto len = operands.at(operand_index).shape().dim(1);

    execution->source<neurun::exec::VectorSource>(
        index, len, reinterpret_cast<const uint8_t *>(buffer), length);
  }
  else if (operands.at(operand_index).shape().rank() == 4)
  {
    const auto &operand_shape = operands.at(operand_index).shape().asFeature();

    execution->source<neurun::exec::FeatureSource>(
        index, operand_shape, reinterpret_cast<const uint8_t *>(buffer), length);
  }
  else
  {
    throw std::runtime_error{"Not supported, yet"};
  }

  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;
  }

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

  // TODO Check type conflicts

  // NOTE The current implemenation assumes that every output is a feature map.
  // TODO Remove this assumption
  neurun::graph::operand::IO::Index output_index{index};

  const auto operand_index = execution->plan().model().getOutputs().at(output_index);

  if (operands.at(operand_index).shape().rank() == 2)
  {
    assert(operands.at(operand_index).shape().dim(0) == 1);

    const auto len = operands.at(operand_index).shape().dim(1);

    execution->sink<neurun::exec::VectorSink>(index, len, reinterpret_cast<uint8_t *>(buffer),
                                              length);
  }
  else if (operands.at(operand_index).shape().rank() == 4)
  {
    const auto &operand_shape = operands.at(operand_index).shape().asFeature();

    execution->sink<neurun::exec::FeatureSink>(index, operand_shape,
                                               reinterpret_cast<uint8_t *>(buffer), length);
  }
  else
  {
    throw std::runtime_error{"Not supported, yet"};
  }

  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 = [&](::arm_compute::ITensor &tensor) { execution->source(n).push(tensor); };

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

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

    for (auto object : objects)
    {
      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 = [&](::arm_compute::ITensor &tensor) { execution->sink(n).pull(tensor); };

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

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

    for (auto object : objects)
    {
      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;
}