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 "SampleDriverFloatFast"
18 
19 #include <android-base/logging.h>
20 #include <android/binder_auto_utils.h>
21 #include <hidl/LegacySupport.h>
22 #include <nnapi/hal/aidl/Conversions.h>
23 
24 #include <memory>
25 #include <thread>
26 #include <vector>
27 
28 #include "AidlHalUtils.h"
29 #include "SampleDriverPartial.h"
30 
31 namespace android {
32 namespace nn {
33 namespace sample_driver {
34 
35 class SampleDriverFloatFast : public SampleDriverPartial {
36    public:
SampleDriverFloatFast()37     SampleDriverFloatFast() : SampleDriverPartial("nnapi-sample_float_fast") {}
38     ndk::ScopedAStatus getCapabilities(aidl_hal::Capabilities* capabilities) override;
39 
40    private:
41     std::vector<bool> getSupportedOperationsImpl(const Model& model) const override;
42 };
43 
getCapabilities(aidl_hal::Capabilities * capabilities)44 ndk::ScopedAStatus SampleDriverFloatFast::getCapabilities(aidl_hal::Capabilities* capabilities) {
45     android::nn::initVLogMask();
46     VLOG(DRIVER) << "getCapabilities()";
47 
48     *capabilities = {
49             .relaxedFloat32toFloat16PerformanceScalar = {.execTime = 0.7f, .powerUsage = 1.1f},
50             .relaxedFloat32toFloat16PerformanceTensor = {.execTime = 0.7f, .powerUsage = 1.1f},
51             .operandPerformance = nonExtensionOperandPerformance({1.0f, 1.0f}),
52             .ifPerformance = {.execTime = 1.0f, .powerUsage = 1.0f},
53             .whilePerformance = {.execTime = 1.0f, .powerUsage = 1.0f}};
54     update(&capabilities->operandPerformance, aidl_hal::OperandType::TENSOR_FLOAT32,
55            {.execTime = 0.8f, .powerUsage = 1.2f});
56     update(&capabilities->operandPerformance, aidl_hal::OperandType::FLOAT32,
57            {.execTime = 0.8f, .powerUsage = 1.2f});
58 
59     return ndk::ScopedAStatus::ok();
60 }
61 
getSupportedOperationsImpl(const Model & model) const62 std::vector<bool> SampleDriverFloatFast::getSupportedOperationsImpl(const 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 Operation& operation = model.main.operations[i];
67         if (!isExtensionOperationType(operation.type) && operation.inputs.size() > 0) {
68             const Operand& firstOperand = model.main.operands[operation.inputs[0]];
69             supported[i] = firstOperand.type == OperandType::TENSOR_FLOAT32;
70         }
71     }
72     return supported;
73 }
74 
75 }  // namespace sample_driver
76 }  // namespace nn
77 }  // namespace android
78 
79 using android::nn::sample_driver::SampleDriverFloatFast;
80 
main()81 int main() {
82     std::shared_ptr<SampleDriverFloatFast> driver =
83             ndk::SharedRefBase::make<SampleDriverFloatFast>();
84     return driver->run();
85 }
86