2429876612f6978a0aa1c2a959867bcee4985a97
[platform/core/ml/nnfw.git] / compiler / tflchef / tflite / src / Convert.cpp
1 /*
2  * Copyright (c) 2018 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 "Convert.h"
18
19 namespace tflchef
20 {
21
22 tflchef::TensorType as_tflchef_type(const tflite::TensorType type)
23 {
24   switch (type)
25   {
26     case tflite::TensorType_FLOAT32:
27       return tflchef::FLOAT32;
28     case tflite::TensorType_INT32:
29       return tflchef::INT32;
30     case tflite::TensorType_INT64:
31       return tflchef::INT64;
32     case tflite::TensorType_UINT8:
33       return tflchef::UINT8;
34     case tflite::TensorType_BOOL:
35       return tflchef::BOOL;
36     case tflite::TensorType_INT16:
37       return tflchef::INT16;
38     case tflite::TensorType_FLOAT16:
39       return tflchef::FLOAT16;
40     // TODO handle other types
41     // TensorType_STRING
42     // TensorType_COMPLEX64
43     default:
44       throw std::runtime_error{"unsupported tensor type"};
45   }
46 }
47
48 tflchef::Activation as_tflchef_activation(const tflite::ActivationFunctionType type)
49 {
50   switch (type)
51   {
52     case tflite::ActivationFunctionType_NONE:
53       return tflchef::NONE;
54     case tflite::ActivationFunctionType_RELU:
55       return tflchef::RELU;
56     case tflite::ActivationFunctionType_RELU_N1_TO_1:
57       return tflchef::RELU_N1_TO_1;
58     case tflite::ActivationFunctionType_RELU6:
59       return tflchef::RELU6;
60     case tflite::ActivationFunctionType_TANH:
61       return tflchef::TANH;
62     case tflite::ActivationFunctionType_SIGN_BIT:
63       return tflchef::SIGN_BIT;
64     default:
65       throw std::runtime_error{"unsupported activation type"};
66   }
67 }
68
69 tflchef::Padding as_tflchef_padding(const tflite::Padding padding)
70 {
71   switch (padding)
72   {
73     case tflite::Padding_SAME:
74       return tflchef::SAME;
75     case tflite::Padding_VALID:
76       return tflchef::VALID;
77     default:
78       throw std::runtime_error{"unsupported padding"};
79   }
80 }
81
82 tflchef::MirrorPadMode as_tflchef_mirrorpadmode(const tflite::MirrorPadMode mode)
83 {
84   switch (mode)
85   {
86     case tflite::MirrorPadMode_REFLECT:
87       return tflchef::REFLECT;
88     case tflite::MirrorPadMode_SYMMETRIC:
89       return tflchef::SYMMETRIC;
90     default:
91       throw std::runtime_error{"Unknown mirrorpad mode"};
92   }
93 }
94
95 tflchef::DimensionType as_tflchef_sparse_dim_type(const tflite::DimensionType type)
96 {
97   switch (type)
98   {
99     case tflite::DimensionType_DENSE:
100       return tflchef::DimensionType::DENSE;
101     case tflite::DimensionType_SPARSE_CSR:
102       return tflchef::DimensionType::SPARSE_CSR;
103     default:
104       throw std::runtime_error("unsupported sparse dimension type");
105   }
106 }
107
108 tflchef::SparseIndexVecType as_tflchef_sparse_idx_vec_type(const tflite::SparseIndexVector type)
109 {
110   switch (type)
111   {
112     case tflite::SparseIndexVector_NONE:
113       return tflchef::SparseIndexVecType::SparseIdxVecType_NONE;
114     case tflite::SparseIndexVector_Int32Vector:
115       return tflchef::SparseIndexVecType::INT32VEC;
116     case tflite::SparseIndexVector_Uint16Vector:
117       return tflchef::SparseIndexVecType::UINT16VEC;
118     case tflite::SparseIndexVector_Uint8Vector:
119       return tflchef::SparseIndexVecType::UINT8VEC;
120     default:
121       throw std::runtime_error("unsupported sparse index vector type");
122   }
123 }
124
125 } // namespace tflchef