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 "SampleDriverFloatFast"
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 SampleDriverFloatFast : public SampleDriverPartial {
34    public:
SampleDriverFloatFast()35     SampleDriverFloatFast() : SampleDriverPartial("nnapi-sample_float_fast") {}
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> SampleDriverFloatFast::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 = 0.7f, .powerUsage = 1.1f},
48             .relaxedFloat32toFloat16PerformanceTensor = {.execTime = 0.7f, .powerUsage = 1.1f},
49             .operandPerformance = nonExtensionOperandPerformance<HalVersion::V1_3>({1.0f, 1.0f}),
50             .ifPerformance = {.execTime = 1.0f, .powerUsage = 1.0f},
51             .whilePerformance = {.execTime = 1.0f, .powerUsage = 1.0f}};
52     update(&capabilities.operandPerformance, V1_3::OperandType::TENSOR_FLOAT32,
53            {.execTime = 0.8f, .powerUsage = 1.2f});
54     update(&capabilities.operandPerformance, V1_3::OperandType::FLOAT32,
55            {.execTime = 0.8f, .powerUsage = 1.2f});
56 
57     cb(V1_3::ErrorStatus::NONE, capabilities);
58     return hardware::Void();
59 }
60 
getSupportedOperationsImpl(const V1_3::Model & model) const61 std::vector<bool> SampleDriverFloatFast::getSupportedOperationsImpl(
62         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] = firstOperand.type == V1_3::OperandType::TENSOR_FLOAT32;
70         }
71     }
72     return supported;
73 }
74 
75 }  // namespace sample_driver
76 }  // namespace nn
77 }  // namespace android
78 
79 using android::sp;
80 using android::nn::sample_driver::SampleDriverFloatFast;
81 
main()82 int main() {
83     sp<SampleDriverFloatFast> driver(new SampleDriverFloatFast());
84     return driver->run();
85 }
86