1 /*
2 * Copyright (C) 2021 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 // This file contains pre-canonical-types utility code and includes HAL
17 // utilities. LegacyUtils.h is the subset of these utilities that do not touch
18 // HAL.
19
20 #include "HalUtils.h"
21
22 #include "HalInterfaces.h"
23
24 #include <android-base/logging.h>
25 #include <nnapi/TypeUtils.h>
26 #include <nnapi/hal/aidl/Conversions.h>
27
28 #include <algorithm>
29 #include <iterator>
30 #include <type_traits>
31 #include <vector>
32
33 namespace android::nn {
34
nonExtensionOperandPerformance(aidl_hal::PerformanceInfo perf)35 std::vector<aidl_hal::OperandPerformance> nonExtensionOperandPerformance(
36 aidl_hal::PerformanceInfo perf) {
37 static constexpr ndk::enum_range<aidl_hal::OperandType> kOperandTypeRange;
38 std::vector<aidl_hal::OperandPerformance> ret;
39 ret.reserve(std::distance(kOperandTypeRange.begin(), kOperandTypeRange.end()));
40 for (aidl_hal::OperandType type : kOperandTypeRange) {
41 if (type != aidl_hal::OperandType::SUBGRAPH) {
42 ret.push_back(aidl_hal::OperandPerformance{type, perf});
43 }
44 }
45 std::sort(ret.begin(), ret.end(),
46 [](const aidl_hal::OperandPerformance& a, const aidl_hal::OperandPerformance& b) {
47 return a.type < b.type;
48 });
49
50 return ret;
51 }
52
update(std::vector<aidl_hal::OperandPerformance> * operandPerformance,aidl_hal::OperandType type,aidl_hal::PerformanceInfo perf)53 void update(std::vector<aidl_hal::OperandPerformance>* operandPerformance,
54 aidl_hal::OperandType type, aidl_hal::PerformanceInfo perf) {
55 CHECK(operandPerformance != nullptr);
56 const auto it = std::lower_bound(operandPerformance->begin(), operandPerformance->end(), type,
57 [](const aidl_hal::OperandPerformance& perf,
58 aidl_hal::OperandType type) { return perf.type < type; });
59 CHECK(it != operandPerformance->end())
60 << toString(type) << " not in operand performance vector";
61 it->info = perf;
62 }
63
isExtensionOperandType(aidl_hal::OperandType type)64 bool isExtensionOperandType(aidl_hal::OperandType type) {
65 return isExtension(convert(type).value());
66 }
67
isNonExtensionScalar(aidl_hal::OperandType type)68 bool isNonExtensionScalar(aidl_hal::OperandType type) {
69 return isNonExtensionScalar(convert(type).value());
70 }
71
72 } // namespace android::nn
73