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