63d4e3c09c6fe28318f0a1d843b146c89a5bcaa2
[platform/core/ml/nnfw.git] / runtime / onert / frontend / nnapi / wrapper / NNAPIConvert.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 "NNAPIConvert.h"
18
19 #include <numeric>
20
21 using namespace onert::ir;
22
23 DataType NNAPIConvert::getDataType(OperandCode type)
24 {
25   switch (type)
26   {
27     case ANEURALNETWORKS_FLOAT32:
28     case ANEURALNETWORKS_TENSOR_FLOAT32:
29       return DataType::FLOAT32;
30     case ANEURALNETWORKS_INT32:
31     case ANEURALNETWORKS_TENSOR_INT32:
32       return DataType::INT32;
33     case ANEURALNETWORKS_UINT32:
34       return DataType::UINT32;
35     case ANEURALNETWORKS_TENSOR_QUANT8_ASYMM:
36       return DataType::QUANT_UINT8_ASYMM;
37     case ANEURALNETWORKS_TENSOR_QUANT8_SYMM:
38       return DataType::QUANT_INT8_SYMM;
39     case ANEURALNETWORKS_BOOL:
40     case ANEURALNETWORKS_TENSOR_BOOL8:
41       return DataType::BOOL8;
42     default:
43       throw std::runtime_error("Unsupported type");
44   }
45 }
46
47 TypeInfo NNAPIConvert::getTypeInfo(const ANeuralNetworksOperandType *type)
48 {
49   return TypeInfo(getDataType((OperandCode)(type->type)), type->scale, type->zeroPoint);
50 }
51
52 Shape NNAPIConvert::getShape(const ANeuralNetworksOperandType *type)
53 {
54   Shape shape(type->dimensionCount);
55
56   for (uint32_t axis = 0; axis < type->dimensionCount; ++axis)
57   {
58     shape.dim(axis) = type->dimensions[axis];
59   }
60
61   return shape;
62 }
63
64 size_t NNAPIConvert::calculateSizeFromType(const ANeuralNetworksOperandType *type)
65 {
66   auto shape = getShape(type);
67   auto data_type = getDataType((OperandCode)(type->type));
68
69   return shape.num_elements() * sizeOfDataType(data_type);
70 }
71
72 Activation NNAPIConvert::getFusedActivation(FuseCode act)
73 {
74   switch (act)
75   {
76     case ANEURALNETWORKS_FUSED_NONE:
77       return Activation::NONE;
78     case ANEURALNETWORKS_FUSED_RELU:
79       return Activation::RELU;
80     case ANEURALNETWORKS_FUSED_RELU1:
81       return Activation::RELU1;
82     case ANEURALNETWORKS_FUSED_RELU6:
83       return Activation::RELU6;
84     default:
85       throw std::runtime_error("Unsupported activation type");
86   }
87 }
88
89 PaddingType NNAPIConvert::getPaddingType(PaddingCode type)
90 {
91   switch (type)
92   {
93     case ANEURALNETWORKS_PADDING_SAME:
94       return PaddingType::SAME;
95     case ANEURALNETWORKS_PADDING_VALID:
96       return PaddingType::VALID;
97     default:
98       throw std::runtime_error("Unsupported type");
99   }
100 }