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 "SampleDriverMinimal"
18 
19 #include <android-base/logging.h>
20 #include <hidl/LegacySupport.h>
21 
22 #include <thread>
23 #include <vector>
24 
25 #include "HalInterfaces.h"
26 #include "NeuralNetworksOEM.h"
27 #include "SampleDriverPartial.h"
28 #include "Utils.h"
29 #include "ValidateHal.h"
30 
31 namespace android {
32 namespace nn {
33 namespace sample_driver {
34 
35 using namespace hal;
36 
37 class SampleDriverMinimal : public SampleDriverPartial {
38    public:
SampleDriverMinimal()39     SampleDriverMinimal() : SampleDriverPartial("nnapi-sample_minimal") {}
40     Return<void> getCapabilities_1_3(getCapabilities_1_3_cb cb) override;
41 
42    private:
43     std::vector<bool> getSupportedOperationsImpl(const V1_3::Model& model) const override;
44 };
45 
getCapabilities_1_3(getCapabilities_1_3_cb cb)46 Return<void> SampleDriverMinimal::getCapabilities_1_3(getCapabilities_1_3_cb cb) {
47     android::nn::initVLogMask();
48     VLOG(DRIVER) << "getCapabilities()";
49 
50     Capabilities capabilities = {
51             .relaxedFloat32toFloat16PerformanceScalar = {.execTime = 0.4f, .powerUsage = 0.5f},
52             .relaxedFloat32toFloat16PerformanceTensor = {.execTime = 0.4f, .powerUsage = 0.5f},
53             .operandPerformance = nonExtensionOperandPerformance<HalVersion::V1_3>({1.0f, 1.0f}),
54             .ifPerformance = {.execTime = 1.0f, .powerUsage = 1.0f},
55             .whilePerformance = {.execTime = 1.0f, .powerUsage = 1.0f}};
56     update(&capabilities.operandPerformance, OperandType::TENSOR_FLOAT32,
57            {.execTime = 0.4f, .powerUsage = 0.5f});
58     update(&capabilities.operandPerformance, OperandType::FLOAT32,
59            {.execTime = 0.4f, .powerUsage = 0.5f});
60 
61     cb(ErrorStatus::NONE, capabilities);
62     return Void();
63 }
64 
getSupportedOperationsImpl(const V1_3::Model & model) const65 std::vector<bool> SampleDriverMinimal::getSupportedOperationsImpl(const V1_3::Model& model) const {
66     const size_t count = model.main.operations.size();
67     std::vector<bool> supported(count);
68     // Simulate supporting just a few ops
69     for (size_t i = 0; i < count; i++) {
70         supported[i] = false;
71         const Operation& operation = model.main.operations[i];
72         switch (operation.type) {
73             case OperationType::ADD:
74             case OperationType::CONCATENATION:
75             case OperationType::CONV_2D: {
76                 const Operand& firstOperand = model.main.operands[operation.inputs[0]];
77                 if (firstOperand.type == OperandType::TENSOR_FLOAT32) {
78                     supported[i] = true;
79                 }
80                 break;
81             }
82             default:
83                 break;
84         }
85     }
86     return supported;
87 }
88 
89 }  // namespace sample_driver
90 }  // namespace nn
91 }  // namespace android
92 
93 using android::sp;
94 using android::nn::sample_driver::SampleDriverMinimal;
95 
main()96 int main() {
97     sp<SampleDriverMinimal> driver(new SampleDriverMinimal());
98     return driver->run();
99 }
100