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 #define LOG_TAG "SampleDriverQuant"
18
19 #include <HalInterfaces.h>
20 #include <Utils.h>
21 #include <android-base/logging.h>
22 #include <hidl/LegacySupport.h>
23
24 #include <thread>
25 #include <vector>
26
27 #include "SampleDriverPartial.h"
28
29 namespace android {
30 namespace nn {
31 namespace sample_driver {
32
33 class SampleDriverQuant : public SampleDriverPartial {
34 public:
SampleDriverQuant()35 SampleDriverQuant() : SampleDriverPartial("nnapi-sample_quant") {}
36 hardware::Return<void> getCapabilities_1_3(getCapabilities_1_3_cb cb) override;
37
38 private:
39 std::vector<bool> getSupportedOperationsImpl(const V1_3::Model& model) const override;
40 };
41
getCapabilities_1_3(getCapabilities_1_3_cb cb)42 hardware::Return<void> SampleDriverQuant::getCapabilities_1_3(getCapabilities_1_3_cb cb) {
43 android::nn::initVLogMask();
44 VLOG(DRIVER) << "getCapabilities()";
45
46 V1_3::Capabilities capabilities = {
47 .relaxedFloat32toFloat16PerformanceScalar = {.execTime = 50.0f, .powerUsage = 1.0f},
48 .relaxedFloat32toFloat16PerformanceTensor = {.execTime = 50.0f, .powerUsage = 1.0f},
49 .operandPerformance = nonExtensionOperandPerformance<HalVersion::V1_3>({50.0f, 1.0f}),
50 .ifPerformance = {.execTime = 50.0f, .powerUsage = 1.0f},
51 .whilePerformance = {.execTime = 50.0f, .powerUsage = 1.0f}};
52
53 cb(V1_3::ErrorStatus::NONE, capabilities);
54 return hardware::Void();
55 }
56
isQuantized(V1_3::OperandType opType)57 static bool isQuantized(V1_3::OperandType opType) {
58 return opType == V1_3::OperandType::TENSOR_QUANT8_ASYMM ||
59 opType == V1_3::OperandType::TENSOR_QUANT8_ASYMM_SIGNED;
60 }
61
getSupportedOperationsImpl(const V1_3::Model & model) const62 std::vector<bool> SampleDriverQuant::getSupportedOperationsImpl(const V1_3::Model& model) const {
63 const size_t count = model.main.operations.size();
64 std::vector<bool> supported(count);
65 for (size_t i = 0; i < count; i++) {
66 const V1_3::Operation& operation = model.main.operations[i];
67 if (!isExtensionOperationType(operation.type) && operation.inputs.size() > 0) {
68 const V1_3::Operand& firstOperand = model.main.operands[operation.inputs[0]];
69 supported[i] = isQuantized(firstOperand.type);
70 if (operation.type == V1_3::OperationType::SELECT) {
71 const V1_3::Operand& secondOperand = model.main.operands[operation.inputs[1]];
72 supported[i] = isQuantized(secondOperand.type);
73 }
74 }
75 }
76 return supported;
77 }
78
79 } // namespace sample_driver
80 } // namespace nn
81 } // namespace android
82
83 using android::sp;
84 using android::nn::sample_driver::SampleDriverQuant;
85
main()86 int main() {
87 sp<SampleDriverQuant> driver(new SampleDriverQuant());
88 return driver->run();
89 }
90