1 /*
2  * Copyright (C) 2019 The Android Open Source Project
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 "1.3/Utils.h"
18 
19 #include <iostream>
20 #include <numeric>
21 #include "android-base/logging.h"
22 #include "android/hardware/neuralnetworks/1.3/types.h"
23 
24 namespace android::hardware::neuralnetworks {
25 
sizeOfData(V1_3::OperandType type)26 uint32_t sizeOfData(V1_3::OperandType type) {
27     switch (type) {
28         case V1_3::OperandType::FLOAT32:
29         case V1_3::OperandType::INT32:
30         case V1_3::OperandType::UINT32:
31         case V1_3::OperandType::TENSOR_FLOAT32:
32         case V1_3::OperandType::TENSOR_INT32:
33             return 4;
34         case V1_3::OperandType::TENSOR_QUANT16_SYMM:
35         case V1_3::OperandType::TENSOR_FLOAT16:
36         case V1_3::OperandType::FLOAT16:
37         case V1_3::OperandType::TENSOR_QUANT16_ASYMM:
38             return 2;
39         case V1_3::OperandType::TENSOR_QUANT8_ASYMM:
40         case V1_3::OperandType::BOOL:
41         case V1_3::OperandType::TENSOR_BOOL8:
42         case V1_3::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
43         case V1_3::OperandType::TENSOR_QUANT8_SYMM:
44         case V1_3::OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
45             return 1;
46         case V1_3::OperandType::SUBGRAPH:
47             return 0;
48         default:
49             CHECK(false) << "Invalid OperandType " << static_cast<uint32_t>(type);
50             return 0;
51     }
52 }
53 
isTensor(V1_3::OperandType type)54 static bool isTensor(V1_3::OperandType type) {
55     switch (type) {
56         case V1_3::OperandType::FLOAT32:
57         case V1_3::OperandType::INT32:
58         case V1_3::OperandType::UINT32:
59         case V1_3::OperandType::FLOAT16:
60         case V1_3::OperandType::BOOL:
61         case V1_3::OperandType::SUBGRAPH:
62             return false;
63         case V1_3::OperandType::TENSOR_FLOAT32:
64         case V1_3::OperandType::TENSOR_INT32:
65         case V1_3::OperandType::TENSOR_QUANT16_SYMM:
66         case V1_3::OperandType::TENSOR_FLOAT16:
67         case V1_3::OperandType::TENSOR_QUANT16_ASYMM:
68         case V1_3::OperandType::TENSOR_QUANT8_ASYMM:
69         case V1_3::OperandType::TENSOR_BOOL8:
70         case V1_3::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
71         case V1_3::OperandType::TENSOR_QUANT8_SYMM:
72         case V1_3::OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
73             return true;
74         default:
75             CHECK(false) << "Invalid OperandType " << static_cast<uint32_t>(type);
76             return false;
77     }
78 }
79 
sizeOfData(const V1_3::Operand & operand)80 uint32_t sizeOfData(const V1_3::Operand& operand) {
81     const uint32_t dataSize = sizeOfData(operand.type);
82     if (isTensor(operand.type) && operand.dimensions.size() == 0) return 0;
83     return std::accumulate(operand.dimensions.begin(), operand.dimensions.end(), dataSize,
84                            std::multiplies<>{});
85 }
86 
87 namespace V1_3 {
88 
operator <<(::std::ostream & os,ErrorStatus errorStatus)89 ::std::ostream& operator<<(::std::ostream& os, ErrorStatus errorStatus) {
90     return os << toString(errorStatus);
91 }
92 
93 }  // namespace V1_3
94 }  // namespace android::hardware::neuralnetworks
95