3f3a5d81e3c623038dd24a306b4df51c3c45d4b6
[platform/core/ml/nnfw.git] / runtime / onert / api / src / CustomKernel.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "CustomKernel.h"
18
19 namespace onert
20 {
21 namespace frontend
22 {
23 namespace custom
24 {
25
26 using namespace backend::custom;
27
28 class APIConverter
29 {
30 public:
31   static nnfw_operand convertOperand(void *alloc, const TypeInfo &type)
32   {
33     nnfw_operand api_operand;
34     api_operand.allocation = alloc;
35     api_operand.type = convertType(type);
36     return api_operand;
37   }
38
39   static nnfw_tensorinfo convertType(const TypeInfo &type)
40   {
41     nnfw_tensorinfo api_type;
42     api_type.rank = type.shape.rank();
43     assert(type.shape.rank() <= 6);
44     std::copy(type.shape.dims().begin(), type.shape.dims().end(), std::begin(api_type.dims));
45
46     switch (type.dtype)
47     {
48       case ir::DataType::FLOAT32:
49         api_type.dtype = NNFW_TYPE_TENSOR_FLOAT32;
50         break;
51       case ir::DataType::INT32:
52         api_type.dtype = NNFW_TYPE_TENSOR_INT32;
53         break;
54       case ir::DataType::QUANT_UINT8_ASYMM:
55         api_type.dtype = NNFW_TYPE_TENSOR_QUANT8_ASYMM;
56         break;
57       case ir::DataType::BOOL8:
58         api_type.dtype = NNFW_TYPE_TENSOR_BOOL;
59         break;
60       default:
61         throw std::runtime_error("Unsupported tensor datatype");
62     }
63     return api_type;
64   }
65 };
66
67 Kernel::Kernel(const nnfw_custom_eval evalFunction)
68     : _in_params(), _userdata(nullptr), _userdata_size(0), _evalFunction(evalFunction)
69 {
70 }
71
72 void Kernel::configure(CustomKernelConfigParams &&inParams)
73 {
74   _userdata = inParams.userdata;
75   _userdata_size = inParams.userdata_size;
76
77   _in_params = std::move(inParams);
78 }
79
80 void Kernel::run()
81 {
82   nnfw_custom_kernel_params params;
83
84   // set input tensor buffer and types
85   params.ninputs = _in_params.input_tensors.size();
86   params.inputs = new nnfw_operand[params.ninputs];
87
88   for (size_t i = 0; i < params.ninputs; ++i)
89   {
90     auto *buf = _in_params.input_tensors[i]->buffer();
91     assert(buf);
92     params.inputs[i] = APIConverter::convertOperand(buf, _in_params.input_types[i]);
93   }
94
95   // set output tensor buffer and types
96   params.noutputs = _in_params.output_tensors.size();
97   params.outputs = new nnfw_operand[params.noutputs];
98
99   for (size_t i = 0; i < params.noutputs; ++i)
100   {
101     auto *buf = _in_params.output_tensors[i]->buffer();
102     assert(buf);
103     params.outputs[i] = APIConverter::convertOperand(buf, _in_params.output_types[i]);
104   }
105
106   _evalFunction(&params, _userdata, _userdata_size);
107
108   delete[] params.inputs;
109   delete[] params.outputs;
110 }
111
112 } // namespace custom
113 } // namespace frontend
114 } // namespace onert