2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "CustomKernel.h"
26 using namespace backend::custom;
31 static nnfw_operand convertOperand(void *alloc, const TypeInfo &type)
33 nnfw_operand api_operand;
34 api_operand.allocation = alloc;
35 api_operand.type = convertType(type);
39 static nnfw_tensorinfo convertType(const TypeInfo &type)
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));
48 case ir::DataType::FLOAT32:
49 api_type.dtype = NNFW_TYPE_TENSOR_FLOAT32;
51 case ir::DataType::INT32:
52 api_type.dtype = NNFW_TYPE_TENSOR_INT32;
54 case ir::DataType::QUANT_UINT8_ASYMM:
55 api_type.dtype = NNFW_TYPE_TENSOR_QUANT8_ASYMM;
57 case ir::DataType::BOOL8:
58 api_type.dtype = NNFW_TYPE_TENSOR_BOOL;
61 throw std::runtime_error("Unsupported tensor datatype");
67 Kernel::Kernel(const nnfw_custom_eval evalFunction)
68 : _in_params(), _userdata(nullptr), _userdata_size(0), _evalFunction(evalFunction)
72 void Kernel::configure(CustomKernelConfigParams &&inParams)
74 _userdata = inParams.userdata;
75 _userdata_size = inParams.userdata_size;
77 _in_params = std::move(inParams);
82 nnfw_custom_kernel_params params;
84 // set input tensor buffer and types
85 params.ninputs = _in_params.input_tensors.size();
86 params.inputs = new nnfw_operand[params.ninputs];
88 for (size_t i = 0; i < params.ninputs; ++i)
90 auto *buf = _in_params.input_tensors[i]->buffer();
92 params.inputs[i] = APIConverter::convertOperand(buf, _in_params.input_types[i]);
95 // set output tensor buffer and types
96 params.noutputs = _in_params.output_tensors.size();
97 params.outputs = new nnfw_operand[params.noutputs];
99 for (size_t i = 0; i < params.noutputs; ++i)
101 auto *buf = _in_params.output_tensors[i]->buffer();
103 params.outputs[i] = APIConverter::convertOperand(buf, _in_params.output_types[i]);
106 _evalFunction(¶ms, _userdata, _userdata_size);
108 delete[] params.inputs;
109 delete[] params.outputs;
112 } // namespace custom
113 } // namespace frontend