1 /*
2  * Copyright (C) 2017 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 #ifndef ANDROID_HARDWARE_V1_0_UTILS_H
18 #define ANDROID_HARDWARE_V1_0_UTILS_H
19 
20 #include <android-base/logging.h>
21 #include <android/hardware/neuralnetworks/1.0/types.h>
22 #include <string>
23 #include <unordered_set>
24 #include <vector>
25 #include "CpuExecutor.h"
26 #include "HexagonController.h"
27 #include "OperationsUtils.h"
28 #include "hexagon_nn_controller/hexagon_nn_controller.h"
29 
30 #define HEXAGON_SOFT_ASSERT(condition, message)                          \
31     if (!(condition)) {                                                  \
32         LOG(DEBUG) << __FILE__ << "::" << __LINE__ << " -- " << message; \
33         return {};                                                       \
34     }
35 
36 #define HEXAGON_SOFT_ASSERT_CMP(cmp, lhs, rhs, message)                        \
37     HEXAGON_SOFT_ASSERT(((lhs)cmp(rhs)), "failed " #lhs " " #cmp " " #rhs " (" \
38                                              << (lhs) << " " #cmp " " << (rhs) << "): " message)
39 
40 #define HEXAGON_SOFT_ASSERT_EQ(lhs, rhs, message) HEXAGON_SOFT_ASSERT_CMP(==, lhs, rhs, message)
41 #define HEXAGON_SOFT_ASSERT_NE(lhs, rhs, message) HEXAGON_SOFT_ASSERT_CMP(!=, lhs, rhs, message)
42 #define HEXAGON_SOFT_ASSERT_LT(lhs, rhs, message) HEXAGON_SOFT_ASSERT_CMP(<, lhs, rhs, message)
43 #define HEXAGON_SOFT_ASSERT_LE(lhs, rhs, message) HEXAGON_SOFT_ASSERT_CMP(<=, lhs, rhs, message)
44 #define HEXAGON_SOFT_ASSERT_GT(lhs, rhs, message) HEXAGON_SOFT_ASSERT_CMP(>, lhs, rhs, message)
45 #define HEXAGON_SOFT_ASSERT_GE(lhs, rhs, message) HEXAGON_SOFT_ASSERT_CMP(>=, lhs, rhs, message)
46 
47 namespace android {
48 namespace hardware {
49 namespace neuralnetworks {
50 namespace V1_0 {
51 namespace implementation {
52 namespace hexagon {
53 
54 using ::android::sp;
55 using ::android::hardware::hidl_memory;
56 using ::android::hardware::hidl_vec;
57 using ::android::hardware::neuralnetworks::V1_0::FusedActivationFunc;
58 using ::android::hardware::neuralnetworks::V1_0::Operand;
59 using ::android::nn::RunTimePoolInfo;
60 
61 bool isHexagonAvailable();
62 
63 hexagon_nn_padding_type getPadding(uint32_t pad);
64 hexagon_nn_padding_type getPadding(int32_t inWidth, int32_t inHeight, int32_t strideWidth,
65                                    int32_t strideHeight, int32_t filterWidth, int32_t filterHeight,
66                                    int32_t paddingLeft, int32_t paddingRight, int32_t paddingTop,
67                                    int32_t paddingBottom);
68 op_type getFloatActivationFunction(FusedActivationFunc act);
69 op_type getQuantizedActivationFunction(FusedActivationFunc act);
70 
71 uint32_t getSize(OperandType type);
72 std::vector<uint32_t> getAlignedDimensions(const std::vector<uint32_t>& dims, uint32_t N);
73 
74 std::vector<RunTimePoolInfo> mapPools(const hidl_vec<hidl_memory>& pools);
75 
76 std::unordered_set<uint32_t> getPoolIndexes(const std::vector<RequestArgument>& inputsOutputs);
77 
78 const uint8_t* getData(const Operand& operand, const hidl_vec<uint8_t>& block,
79                        const std::vector<RunTimePoolInfo>& pools);
80 
81 template <typename Type>
transpose(uint32_t height,uint32_t width,const Type * input)82 std::vector<Type> transpose(uint32_t height, uint32_t width, const Type* input) {
83     std::vector<Type> output(height * width);
84     for (uint32_t i = 0; i < height; ++i) {
85         for (uint32_t j = 0; j < width; ++j) {
86             output[j * height + i] = input[i * width + j];
87         }
88     }
89     return output;
90 }
91 
92 hexagon_nn_output make_hexagon_nn_output(const std::vector<uint32_t>& dims, uint32_t size);
93 
94 bool operator==(const hexagon_nn_input& lhs, const hexagon_nn_input& rhs);
95 bool operator!=(const hexagon_nn_input& lhs, const hexagon_nn_input& rhs);
96 bool operator==(const hexagon_nn_output& lhs, const hexagon_nn_output& rhs);
97 bool operator!=(const hexagon_nn_output& lhs, const hexagon_nn_output& rhs);
98 
99 // printers
100 std::string toString(uint32_t val);
101 std::string toString(float val);
102 std::string toString(hexagon_nn_nn_id id);
103 std::string toString(op_type op);
104 std::string toString(hexagon_nn_padding_type padding);
105 std::string toString(const hexagon_nn_input& input);
106 std::string toString(const hexagon_nn_output& output);
107 std::string toString(const hexagon_nn_tensordef& tensordef);
108 std::string toString(const hexagon_nn_perfinfo& perfinfo);
109 std::string toString(const ::android::nn::Shape& input);
110 
111 template <typename Type>
toString(const Type * buffer,uint32_t count)112 std::string toString(const Type* buffer, uint32_t count) {
113     std::string os = "[";
114     for (uint32_t i = 0; i < count; ++i) {
115         os += (i == 0 ? "" : ", ") + toString(buffer[i]);
116     }
117     return os += "]";
118 }
119 
120 template <typename CharT, typename Traits>
121 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
122                                               const hexagon_nn_input& obj) {
123     return os << toString(obj);
124 }
125 
126 template <typename CharT, typename Traits>
127 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
128                                               const hexagon_nn_output& obj) {
129     return os << toString(obj);
130 }
131 
132 template <typename CharT, typename Traits>
133 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
134                                               const hexagon_nn_tensordef& obj) {
135     return os << toString(obj);
136 }
137 
138 template <typename CharT, typename Traits>
139 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
140                                               const hexagon_nn_perfinfo& obj) {
141     return os << toString(obj);
142 }
143 
144 template <typename CharT, typename Traits>
145 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
146                                               const ::android::nn::Shape& obj) {
147     return os << toString(obj);
148 }
149 
150 template <typename CharT, typename Traits>
151 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
152                                               ErrorStatus status) {
153     return os << toString(status);
154 }
155 
156 }  // namespace hexagon
157 }  // namespace implementation
158 }  // namespace V1_0
159 }  // namespace neuralnetworks
160 }  // namespace hardware
161 }  // namespace android
162 
163 #endif  // ANDROID_HARDWARE_V1_0_UTILS_H
164