• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SampleDriverMinimal"
18 
19 #include <android-base/logging.h>
20 #include <android/binder_auto_utils.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 SampleDriverMinimal : public SampleDriverPartial {
34    public:
SampleDriverMinimal()35     SampleDriverMinimal() : SampleDriverPartial("nnapi-sample_minimal") {}
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 SampleDriverMinimal::getCapabilities(aidl_hal::Capabilities* capabilities) {
43     android::nn::initVLogMask();
44     VLOG(DRIVER) << "getCapabilities()";
45 
46     *capabilities = {
47             .relaxedFloat32toFloat16PerformanceScalar = {.execTime = 0.4f, .powerUsage = 0.5f},
48             .relaxedFloat32toFloat16PerformanceTensor = {.execTime = 0.4f, .powerUsage = 0.5f},
49             .operandPerformance = nonExtensionOperandPerformance({1.0f, 1.0f}),
50             .ifPerformance = {.execTime = 1.0f, .powerUsage = 1.0f},
51             .whilePerformance = {.execTime = 1.0f, .powerUsage = 1.0f}};
52     update(&capabilities->operandPerformance, aidl_hal::OperandType::TENSOR_FLOAT32,
53            {.execTime = 0.4f, .powerUsage = 0.5f});
54     update(&capabilities->operandPerformance, aidl_hal::OperandType::FLOAT32,
55            {.execTime = 0.4f, .powerUsage = 0.5f});
56 
57     return ndk::ScopedAStatus::ok();
58 }
59 
getSupportedOperationsImpl(const Model & model) const60 std::vector<bool> SampleDriverMinimal::getSupportedOperationsImpl(const Model& model) const {
61     const size_t count = model.main.operations.size();
62     std::vector<bool> supported(count);
63     // Simulate supporting just a few ops
64     for (size_t i = 0; i < count; i++) {
65         supported[i] = false;
66         const Operation& operation = model.main.operations[i];
67         switch (operation.type) {
68             case OperationType::ADD:
69             case OperationType::CONCATENATION:
70             case OperationType::CONV_2D: {
71                 const Operand& firstOperand = model.main.operands[operation.inputs[0]];
72                 if (firstOperand.type == OperandType::TENSOR_FLOAT32) {
73                     supported[i] = true;
74                 }
75                 break;
76             }
77             default:
78                 break;
79         }
80     }
81     return supported;
82 }
83 
84 }  // namespace sample_driver
85 }  // namespace nn
86 }  // namespace android
87 
88 using android::nn::sample_driver::SampleDriverMinimal;
89 
main()90 int main() {
91     std::shared_ptr<SampleDriverMinimal> driver = ndk::SharedRefBase::make<SampleDriverMinimal>();
92     return driver->run();
93 }
94