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 "[email protected]"
18 
19 #include "Device.h"
20 #include <android-base/logging.h>
21 #include <memory>
22 #include <mutex>
23 #include <thread>
24 #include "HexagonModel.h"
25 #include "HexagonUtils.h"
26 #include "PreparedModel.h"
27 #include "ValidateHal.h"
28 
29 namespace android {
30 namespace hardware {
31 namespace neuralnetworks {
32 namespace V1_0 {
33 namespace implementation {
34 
Device()35 Device::Device() : mCurrentStatus(DeviceStatus::AVAILABLE) {}
36 
~Device()37 Device::~Device() {}
38 
39 static std::once_flag configure_nnlib;
configureHexagon()40 static void configureHexagon() {
41     std::call_once(configure_nnlib, []() {
42         hexagon::Controller::getInstance().config();
43     });
44 }
45 
getCapabilities(getCapabilities_cb _hidl_cb)46 Return<void> Device::getCapabilities(getCapabilities_cb _hidl_cb) {
47     configureHexagon();
48 
49     // These numbers are approximations for this release.
50     // TODO Change with the actual number.
51     PerformanceInfo float32Performance = {
52         .execTime = 30.0f, .powerUsage = 2.0f,
53     };
54 
55     PerformanceInfo quantized8Performance = {
56         .execTime = 0.7f, .powerUsage = 0.7f,
57     };
58 
59     Capabilities capabilities = {
60         .float32Performance = float32Performance, .quantized8Performance = quantized8Performance,
61     };
62 
63     ErrorStatus status =
64         hexagon::isHexagonAvailable() ? ErrorStatus::NONE : ErrorStatus::DEVICE_UNAVAILABLE;
65 
66     _hidl_cb(status, capabilities);
67     return Void();
68 }
69 
getSupportedOperations(const Model & model,getSupportedOperations_cb _hidl_cb)70 Return<void> Device::getSupportedOperations(const Model& model,
71                                             getSupportedOperations_cb _hidl_cb) {
72     configureHexagon();
73 
74     if (!nn::validateModel(model)) {
75         _hidl_cb(ErrorStatus::INVALID_ARGUMENT, std::vector<bool>{});
76         return Void();
77     }
78     if (!hexagon::isHexagonAvailable()) {
79         _hidl_cb(ErrorStatus::DEVICE_UNAVAILABLE, std::vector<bool>{});
80         return Void();
81     }
82 
83     hexagon::Model hexagonModel(model);
84     std::vector<bool> supported = hexagonModel.supportedOperations();
85 
86     _hidl_cb(ErrorStatus::NONE, supported);
87     return Void();
88 }
89 
asyncPrepare(const Model & model,const sp<IPreparedModelCallback> & callback)90 static void asyncPrepare(const Model& model, const sp<IPreparedModelCallback>& callback) {
91     std::shared_ptr<hexagon::Model> hexagonModel = std::make_shared<hexagon::Model>(model);
92 
93     Return<void> ret;
94     if (hexagonModel->prepare()) {
95         ret = callback->notify(ErrorStatus::NONE, new PreparedModel(model, hexagonModel));
96     } else {
97         ret = callback->notify(ErrorStatus::GENERAL_FAILURE, nullptr);
98     }
99     if (!ret.isOk()) {
100         LOG(ERROR) << "Error in callback's return type: " << ret.description();
101     }
102 }
103 
prepareModel(const Model & model,const sp<IPreparedModelCallback> & callback)104 Return<ErrorStatus> Device::prepareModel(const Model& model,
105                                          const sp<IPreparedModelCallback>& callback) {
106     configureHexagon();
107 
108     if (callback.get() == nullptr) {
109         LOG(ERROR) << "invalid callback passed to prepareModel";
110         return ErrorStatus::INVALID_ARGUMENT;
111     }
112     if (!nn::validateModel(model)) {
113         callback->notify(ErrorStatus::INVALID_ARGUMENT, nullptr);
114         return ErrorStatus::INVALID_ARGUMENT;
115     }
116     if (!hexagon::isHexagonAvailable()) {
117         callback->notify(ErrorStatus::DEVICE_UNAVAILABLE, nullptr);
118         return ErrorStatus::DEVICE_UNAVAILABLE;
119     }
120 
121     // TODO: once nnlib hanging issue is resolved, make this function
122     // asynchronous again
123     asyncPrepare(model, callback);
124 
125     return ErrorStatus::NONE;
126 }
127 
getStatus()128 Return<DeviceStatus> Device::getStatus() {
129     configureHexagon();
130     mCurrentStatus =
131         hexagon::isHexagonAvailable() ? DeviceStatus::AVAILABLE : DeviceStatus::OFFLINE;
132     return mCurrentStatus;
133 }
134 
135 }  // namespace implementation
136 }  // namespace V1_0
137 }  // namespace neuralnetworks
138 }  // namespace hardware
139 }  // namespace android
140