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 <HalInterfaces.h>
20 #include <Utils.h>
21 #include <android-base/logging.h>
22
23 #include <thread>
24 #include <vector>
25
26 #include "NeuralNetworksOEM.h"
27 #include "SampleDriverPartial.h"
28
29 namespace android {
30 namespace nn {
31 namespace sample_driver {
32
33 class SampleDriverMinimal : public SampleDriverPartial {
34 public:
SampleDriverMinimal()35 SampleDriverMinimal() : SampleDriverPartial("nnapi-sample_minimal") {}
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> SampleDriverMinimal::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.4f, .powerUsage = 0.5f},
48 .relaxedFloat32toFloat16PerformanceTensor = {.execTime = 0.4f, .powerUsage = 0.5f},
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.4f, .powerUsage = 0.5f});
54 update(&capabilities.operandPerformance, V1_3::OperandType::FLOAT32,
55 {.execTime = 0.4f, .powerUsage = 0.5f});
56
57 cb(V1_3::ErrorStatus::NONE, capabilities);
58 return hardware::Void();
59 }
60
getSupportedOperationsImpl(const V1_3::Model & model) const61 std::vector<bool> SampleDriverMinimal::getSupportedOperationsImpl(const V1_3::Model& model) const {
62 const size_t count = model.main.operations.size();
63 std::vector<bool> supported(count);
64 // Simulate supporting just a few ops
65 for (size_t i = 0; i < count; i++) {
66 supported[i] = false;
67 const V1_3::Operation& operation = model.main.operations[i];
68 switch (operation.type) {
69 case V1_3::OperationType::ADD:
70 case V1_3::OperationType::CONCATENATION:
71 case V1_3::OperationType::CONV_2D: {
72 const V1_3::Operand& firstOperand = model.main.operands[operation.inputs[0]];
73 if (firstOperand.type == V1_3::OperandType::TENSOR_FLOAT32) {
74 supported[i] = true;
75 }
76 break;
77 }
78 default:
79 break;
80 }
81 }
82 return supported;
83 }
84
85 } // namespace sample_driver
86 } // namespace nn
87 } // namespace android
88
89 using android::sp;
90 using android::nn::sample_driver::SampleDriverMinimal;
91
main()92 int main() {
93 sp<SampleDriverMinimal> driver(new SampleDriverMinimal());
94 return driver->run();
95 }
96