summaryrefslogtreecommitdiff
path: root/runtimes/neurun/frontend/api/wrapper/nnfw_api.cc
blob: 80a768e4ec4e28dea8773fd4e080bf3d3719583c (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
329
330
331
332
333
334
335
336
337
/*
 * Copyright (c) 2019 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 "nnfw_api.hpp"
#include "backend/CustomKernelRegistry.h"
#include "compiler/Compiler.h"
#include "exec/Execution.h"
#include "loader.h"
#include "json/json.h"
#include <iostream>
#include <string>
#include <dirent.h>
#include <limits.h>
#include <stdint.h>
#include <util/ConfigSource.h>

nnfw_session::nnfw_session()
    : _graph{nullptr}, _execution{nullptr},
      _kernel_registry{std::make_shared<neurun::backend::custom::KernelRegistry>()},
      _source{nnfw::cpp14::make_unique<neurun::util::GeneralConfigSource>()}
{
  // DO NOTHING
}

NNFW_STATUS nnfw_session::load_model_from_file(const char *package_dir)
{
  // TODO : add support for zipped package file load
  DIR *dir;
  if (!(dir = opendir(package_dir)))
  {
    std::cerr << "invalid nnpackge directory: " << package_dir << std::endl;
    return NNFW_STATUS_ERROR;
  }
  closedir(dir);

  try
  {
    std::string manifest_file_name(package_dir);
    manifest_file_name += "/metadata/MANIFEST";
    std::ifstream mfs(manifest_file_name);

    // extract the filename of the first(index 0) model
    // e.g. In MANIFEST file, { "models" : [ "firstmodel.tflite", "2nd.tflite" ] }
    Json::Value root;
    mfs >> root;
    Json::Value models = root["models"];

    auto model = nnfw::cpp14::make_unique<neurun::model::Model>();
    _graph = std::make_shared<neurun::graph::Graph>(std::move(model));
    _graph->bindKernelRegistry(_kernel_registry);
    tflite_loader::Loader loader(*_graph);
    auto model_file_path = package_dir + std::string("/") + models[0].asString(); // first model
    loader.loadFromFile(model_file_path.c_str());
  }
  catch (...)
  {
    std::cerr << "Error during model loading" << std::endl;
    return NNFW_STATUS_ERROR;
  }

  return NNFW_STATUS_NO_ERROR;
}

NNFW_STATUS nnfw_session::prepare()
{
  // TODO : add additional setting routine(executor type, backend)
  // Note that we assume acl_cl backend

  try
  {
    // config_source setting
    using neurun::util::config_source;
    config_source(std::move(_source));

    auto compiler = nnfw::cpp14::make_unique<neurun::compiler::Compiler>(_graph);
    compiler->compile();
    std::shared_ptr<neurun::exec::IExecutor> executor;
    compiler->release(executor);
    _execution = std::make_shared<neurun::exec::Execution>(executor);
  }
  catch (...)
  {
    std::cerr << "Error during model prepare" << std::endl;
    return NNFW_STATUS_ERROR;
  }
  return NNFW_STATUS_NO_ERROR;
}

NNFW_STATUS nnfw_session::run()
{
  try
  {
    _execution->execute();
  }
  catch (...)
  {
    std::cerr << "Error during nnfw_session::run" << std::endl;
    return NNFW_STATUS_ERROR;
  }

  return NNFW_STATUS_NO_ERROR;
}

NNFW_STATUS nnfw_session::set_input(uint32_t index, NNFW_TYPE /*type*/, const void *buffer,
                                    size_t length)
{
  try
  {
    _execution->setInput(neurun::model::IOIndex(index), buffer, length);
  }
  catch (...)
  {
    std::cerr << "Error during nnfw_session::set_input" << std::endl;
    return NNFW_STATUS_ERROR;
  }
  return NNFW_STATUS_NO_ERROR;
}

NNFW_STATUS nnfw_session::set_output(uint32_t index, NNFW_TYPE /*type*/, void *buffer,
                                     size_t length)
{
  try
  {
    _execution->setOutput(neurun::model::IOIndex(index), buffer, length);
  }
  catch (...)
  {
    std::cerr << "Error during nnfw_session::set_output" << std::endl;
    return NNFW_STATUS_ERROR;
  }
  return NNFW_STATUS_NO_ERROR;
}

NNFW_STATUS nnfw_session::input_size(uint32_t *number)
{
  try
  {
    if (number == nullptr)
    {
      std::cerr << "Error during nnfw_session::input_size, number is null pointer." << std::endl;
      return NNFW_STATUS_ERROR;
    }
    *number = _graph->getInputs().size();
  }
  catch (...)
  {
    std::cerr << "Error during nnfw_session::input_size" << std::endl;
    return NNFW_STATUS_ERROR;
  }
  return NNFW_STATUS_NO_ERROR;
}

NNFW_STATUS nnfw_session::output_size(uint32_t *number)
{
  try
  {
    if (number == nullptr)
    {
      std::cerr << "Error during nnfw_session::output_size, number is null pointer." << std::endl;
      return NNFW_STATUS_ERROR;
    }
    *number = _graph->getOutputs().size();
  }
  catch (...)
  {
    std::cerr << "Error during nnfw_session::output_size" << std::endl;
    return NNFW_STATUS_ERROR;
  }
  return NNFW_STATUS_NO_ERROR;
}

static NNFW_TYPE datatype_to_nnfw_dtype(neurun::model::DataType dt)
{
  using neurun::model::DataType;
  switch (dt)
  {
    case DataType::FLOAT32:
      return NNFW_TYPE_TENSOR_FLOAT32;
    case DataType::INT32:
      return NNFW_TYPE_TENSOR_INT32;
    case DataType::QUANT8_ASYMM:
      return NNFW_TYPE_TENSOR_QUANT8_ASYMM;
    case DataType::BOOL8:
      return NNFW_TYPE_TENSOR_BOOL;
    case DataType::UINT32:
    default:
      std::cerr << "Error: Model has type that runtime API does not support." << std::endl;
      exit(-1);
  }
}

NNFW_STATUS nnfw_session::input_tensorinfo(uint32_t index, nnfw_tensorinfo *ti)
{
  try
  {
    if (ti == nullptr)
    {
      std::cerr << "Error during nnfw_session::input_tensorinfo, tensorinfo is null pointer."
                << std::endl;
      return NNFW_STATUS_ERROR;
    }
    if (index >= _graph->getInputs().size())
    {
      std::cerr << "Error during nnfw_session::input_tensorinfo, index is out of range."
                << std::endl;
      return NNFW_STATUS_ERROR;
    }
    auto opidx = _graph->getInputs().at(index);
    auto shape = _graph->operands().at(opidx).shape();
    ti->rank = shape.rank();
    for (int j = 0; j < ti->rank; ++j)
    {
      ti->dims[j] = shape.dim(j);
    }
    ti->dtype = datatype_to_nnfw_dtype(_graph->operands().at(opidx).typeInfo().type());
  }
  catch (...)
  {
    std::cerr << "Error during nnfw_session::input_tensorinfo." << std::endl;
    return NNFW_STATUS_ERROR;
  }
  return NNFW_STATUS_NO_ERROR;
}

NNFW_STATUS nnfw_session::output_tensorinfo(uint32_t index, nnfw_tensorinfo *ti)
{
  try
  {
    if (ti == nullptr)
    {
      std::cerr << "Error during nnfw_session::output_tensorinfo, tensorinfo is null pointer."
                << std::endl;
      return NNFW_STATUS_ERROR;
    }
    if (index >= _graph->getOutputs().size())
    {
      std::cerr << "Error during nnfw_session::output_tensorinfo, index is out of range."
                << std::endl;
      return NNFW_STATUS_ERROR;
    }
    auto opidx = _graph->getOutputs().at(index);
    auto shape = _graph->operands().at(opidx).shape();
    ti->rank = shape.rank();
    for (int j = 0; j < ti->rank; ++j)
    {
      ti->dims[j] = shape.dim(j);
    }
    ti->dtype = datatype_to_nnfw_dtype(_graph->operands().at(opidx).typeInfo().type());
  }
  catch (...)
  {
    std::cerr << "Error during nnfw_session::output_tensorinfo." << std::endl;
    return NNFW_STATUS_ERROR;
  }
  return NNFW_STATUS_NO_ERROR;
}

NNFW_STATUS nnfw_session::register_custom_operation(const std::string &id,
                                                    nnfw_custom_eval eval_func)
{
  _kernel_registry->registerKernel(id, eval_func);
  return NNFW_STATUS_NO_ERROR;
}

static std::string get_op_backend_string(std::string op)
{
  // TODO: Provide complete set of operations
  static std::unordered_map<std::string, std::string> operation_map = {
      {"TRANSPOSE_CONV", "OP_BACKEND_TransposeConvNode"},
      {"CONV_2D", "OP_BACKEND_Conv2DNode"},
      {"DEPTHWISE_CONV_2D", "OP_BACKEND_DepthwiseConv2DNode"},
      {"MEAN", "OP_BACKEND_MeanNode"},
      {"AVERAGE_POOL_2D", "OP_BACKEND_AvgPool2DNode"},
      {"MAX_POOL_2D", "OP_BACKEND_MaxPool2DNode"},
  };

  auto n = operation_map.find(op);

  if (n == operation_map.end())
  {
    // this return value is handled by a caller to return error code
    return std::string("");
  }
  else
  {
    return n->second;
  }
}

NNFW_STATUS nnfw_session::set_default_backend(std::string backend)
{
  try
  {
    _source->set("OP_BACKEND_ALLOPS", backend);
  }
  catch (...)
  {
    std::cerr << "Error during nnfw_session::set_default_backend" << std::endl;
    return NNFW_STATUS_ERROR;
  }
  return NNFW_STATUS_NO_ERROR;
}

NNFW_STATUS nnfw_session::set_op_backend(std::string op, std::string backend)
{

  try
  {
    auto key = get_op_backend_string(op);

    if (key.empty())
    {
      return NNFW_STATUS_ERROR;
    }

    _source->set(key, backend);
  }
  catch (...)
  {
    std::cerr << "Error during nnfw_session::set_op_backend." << std::endl;
    return NNFW_STATUS_ERROR;
  }
  return NNFW_STATUS_NO_ERROR;
}