blob: 6fc46ab8933127d643376ce25ad14c20ccd5b576 [file] [log] [blame]
Michael Butler8fa53832021-11-01 18:15:48 -07001/*
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 "AidlHalUtils.h"
21
22#include <android-base/logging.h>
23#include <nnapi/hal/aidl/Conversions.h>
24
25#include <algorithm>
26#include <iterator>
27#include <type_traits>
28#include <vector>
29
30#include "AidlHalInterfaces.h"
31#include "nnapi/TypeUtils.h"
32
33namespace android::nn {
34
35std::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
53void 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
64bool isExtensionOperandType(aidl_hal::OperandType type) {
65 return isExtension(convert(type).value());
66}
67
68bool isNonExtensionScalar(aidl_hal::OperandType type) {
69 return isNonExtensionScalar(convert(type).value());
70}
71
72} // namespace android::nn