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 
17 #define LOG_TAG "SampleDriverQuant"
18 
19 #include <android-base/logging.h>
20 #include <nnapi/hal/aidl/Conversions.h>
21 
22 #include <memory>
23 #include <thread>
24 #include <vector>
25 
26 #include "AidlHalUtils.h"
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     ndk::ScopedAStatus getCapabilities(aidl_hal::Capabilities* capabilities) override;
37 
38    private:
39     std::vector<bool> getSupportedOperationsImpl(const Model& model) const override;
40 };
41 
getCapabilities(aidl_hal::Capabilities * capabilities)42 ndk::ScopedAStatus SampleDriverQuant::getCapabilities(aidl_hal::Capabilities* capabilities) {
43     android::nn::initVLogMask();
44     VLOG(DRIVER) << "getCapabilities()";
45 
46     *capabilities = {
47             .relaxedFloat32toFloat16PerformanceScalar = {.execTime = 50.0f, .powerUsage = 1.0f},
48             .relaxedFloat32toFloat16PerformanceTensor = {.execTime = 50.0f, .powerUsage = 1.0f},
49             .operandPerformance = nonExtensionOperandPerformance({50.0f, 1.0f}),
50             .ifPerformance = {.execTime = 50.0f, .powerUsage = 1.0f},
51             .whilePerformance = {.execTime = 50.0f, .powerUsage = 1.0f}};
52 
53     return ndk::ScopedAStatus::ok();
54 }
55 
isQuantized(OperandType opType)56 static bool isQuantized(OperandType opType) {
57     return opType == OperandType::TENSOR_QUANT8_ASYMM ||
58            opType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED;
59 }
60 
getSupportedOperationsImpl(const Model & model) const61 std::vector<bool> SampleDriverQuant::getSupportedOperationsImpl(const Model& model) const {
62     const size_t count = model.main.operations.size();
63     std::vector<bool> supported(count);
64     for (size_t i = 0; i < count; i++) {
65         const Operation& operation = model.main.operations[i];
66         if (!isExtensionOperationType(operation.type) && operation.inputs.size() > 0) {
67             const Operand& firstOperand = model.main.operands[operation.inputs[0]];
68             supported[i] = isQuantized(firstOperand.type);
69             if (operation.type == OperationType::SELECT) {
70                 const Operand& secondOperand = model.main.operands[operation.inputs[1]];
71                 supported[i] = isQuantized(secondOperand.type);
72             }
73         }
74     }
75     return supported;
76 }
77 
78 }  // namespace sample_driver
79 }  // namespace nn
80 }  // namespace android
81 
82 using android::nn::sample_driver::SampleDriverQuant;
83 
main()84 int main() {
85     std::shared_ptr<SampleDriverQuant> driver = ndk::SharedRefBase::make<SampleDriverQuant>();
86     return driver->run();
87 }
88