Imported Upstream version 1.12.0
[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     case ANEURALNETWORKS_TENSOR_FLOAT16:
43     case ANEURALNETWORKS_FLOAT16:
44       return DataType::FLOAT16;
45     case ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL:
46       return DataType::QUANT_INT8_SYMM_PER_CHANNEL;
47     case ANEURALNETWORKS_TENSOR_QUANT8_ASYMM_SIGNED:
48       return DataType::QUANT_INT8_ASYMM;
49     default:
50       throw std::runtime_error("Unsupported type");
51   }
52 }
53
54 TypeInfo NNAPIConvert::getTypeInfo(const ANeuralNetworksOperandType *type)
55 {
56   return TypeInfo(getDataType((OperandCode)(type->type)), type->scale, type->zeroPoint);
57 }
58
59 Shape NNAPIConvert::getShape(const ANeuralNetworksOperandType *type)
60 {
61   Shape shape(type->dimensionCount);
62
63   for (uint32_t axis = 0; axis < type->dimensionCount; ++axis)
64   {
65     shape.dim(axis) = type->dimensions[axis];
66   }
67
68   return shape;
69 }
70
71 size_t NNAPIConvert::calculateSizeFromType(const ANeuralNetworksOperandType *type)
72 {
73   auto shape = getShape(type);
74   auto data_type = getDataType((OperandCode)(type->type));
75
76   return shape.num_elements() * sizeOfDataType(data_type);
77 }
78
79 Activation NNAPIConvert::getFusedActivation(FuseCode act)
80 {
81   switch (act)
82   {
83     case ANEURALNETWORKS_FUSED_NONE:
84       return Activation::NONE;
85     case ANEURALNETWORKS_FUSED_RELU:
86       return Activation::RELU;
87     case ANEURALNETWORKS_FUSED_RELU1:
88       return Activation::RELU1;
89     case ANEURALNETWORKS_FUSED_RELU6:
90       return Activation::RELU6;
91     default:
92       throw std::runtime_error("Unsupported activation type");
93   }
94 }
95
96 PaddingType NNAPIConvert::getPaddingType(PaddingCode type)
97 {
98   switch (type)
99   {
100     case ANEURALNETWORKS_PADDING_SAME:
101       return PaddingType::SAME;
102     case ANEURALNETWORKS_PADDING_VALID:
103       return PaddingType::VALID;
104     default:
105       throw std::runtime_error("Unsupported type");
106   }
107 }