blob: 0b5dda75973045b364b26d0d2c1cf6327cd842e7 [file] [log] [blame]
Michael Butler76cd09d2017-10-02 17:49:46 -07001/*
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 "android.hardware.neuralnetworks@1.0-impl-hvx"
18
19#include "Device.h"
Michael Butlera6e68df2017-12-04 12:16:42 -080020#include <android-base/logging.h>
21#include <memory>
22#include <mutex>
23#include <thread>
Michael Butler76cd09d2017-10-02 17:49:46 -070024#include "HexagonModel.h"
25#include "HexagonUtils.h"
26#include "PreparedModel.h"
Jean-Luc Brouilletcf8d4542017-10-16 02:35:33 -070027#include "ValidateHal.h"
Michael Butler76cd09d2017-10-02 17:49:46 -070028
29namespace android {
30namespace hardware {
31namespace neuralnetworks {
32namespace V1_0 {
33namespace implementation {
34
35Device::Device() : mCurrentStatus(DeviceStatus::AVAILABLE) {}
36
37Device::~Device() {}
38
Michael Butler82d31132017-10-04 03:21:58 -070039static std::once_flag configure_nnlib;
40static void configureHexagon() {
Michael Butlera6e68df2017-12-04 12:16:42 -080041 std::call_once(configure_nnlib, []() {
42 hexagon::Controller::getInstance().config();
Michael Butlera6e68df2017-12-04 12:16:42 -080043 });
Michael Butler82d31132017-10-04 03:21:58 -070044}
Michael Butler76cd09d2017-10-02 17:49:46 -070045
46Return<void> Device::getCapabilities(getCapabilities_cb _hidl_cb) {
Michael Butler82d31132017-10-04 03:21:58 -070047 configureHexagon();
Michael Butler76cd09d2017-10-02 17:49:46 -070048
Jean-Luc Brouillet5fa00612017-10-12 23:18:53 -070049 // These numbers are approximations for this release.
50 // TODO Change with the actual number.
Michael Butler76cd09d2017-10-02 17:49:46 -070051 PerformanceInfo float32Performance = {
Michael Butlera6e68df2017-12-04 12:16:42 -080052 .execTime = 30.0f, .powerUsage = 2.0f,
Michael Butler76cd09d2017-10-02 17:49:46 -070053 };
54
55 PerformanceInfo quantized8Performance = {
Michael Butlera6e68df2017-12-04 12:16:42 -080056 .execTime = 0.7f, .powerUsage = 0.7f,
Michael Butler76cd09d2017-10-02 17:49:46 -070057 };
58
59 Capabilities capabilities = {
Michael Butlera6e68df2017-12-04 12:16:42 -080060 .float32Performance = float32Performance, .quantized8Performance = quantized8Performance,
Michael Butler76cd09d2017-10-02 17:49:46 -070061 };
62
Michael Butler82d31132017-10-04 03:21:58 -070063 ErrorStatus status =
Michael Butlera6e68df2017-12-04 12:16:42 -080064 hexagon::isHexagonAvailable() ? ErrorStatus::NONE : ErrorStatus::DEVICE_UNAVAILABLE;
Michael Butler82d31132017-10-04 03:21:58 -070065
66 _hidl_cb(status, capabilities);
Michael Butler76cd09d2017-10-02 17:49:46 -070067 return Void();
68}
69
70Return<void> Device::getSupportedOperations(const Model& model,
71 getSupportedOperations_cb _hidl_cb) {
Michael Butler82d31132017-10-04 03:21:58 -070072 configureHexagon();
Michael Butler76cd09d2017-10-02 17:49:46 -070073
74 if (!nn::validateModel(model)) {
Michael Butler82d31132017-10-04 03:21:58 -070075 _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>{});
Michael Butler76cd09d2017-10-02 17:49:46 -070080 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
Michael Butler25d8e872017-12-19 11:59:54 -080090static void asyncPrepare(const Model& model, const sp<IPreparedModelCallback>& callback) {
Michael Butler20a5bca2017-10-05 15:02:31 -070091 std::shared_ptr<hexagon::Model> hexagonModel = std::make_shared<hexagon::Model>(model);
Michael Butler82d31132017-10-04 03:21:58 -070092
Michael Butler9001cd52017-12-08 11:12:29 -080093 Return<void> ret;
Michael Butler0f4cdac2017-10-25 17:41:34 -070094 if (hexagonModel->prepare()) {
Michael Butler9001cd52017-12-08 11:12:29 -080095 ret = callback->notify(ErrorStatus::NONE, new PreparedModel(model, hexagonModel));
Michael Butlera6e68df2017-12-04 12:16:42 -080096 } else {
Michael Butler9001cd52017-12-08 11:12:29 -080097 ret = callback->notify(ErrorStatus::GENERAL_FAILURE, nullptr);
98 }
99 if (!ret.isOk()) {
100 LOG(ERROR) << "Error in callback's return type: " << ret.description();
Michael Butler82d31132017-10-04 03:21:58 -0700101 }
102}
103
Michael Butler76cd09d2017-10-02 17:49:46 -0700104Return<ErrorStatus> Device::prepareModel(const Model& model,
105 const sp<IPreparedModelCallback>& callback) {
Michael Butler82d31132017-10-04 03:21:58 -0700106 configureHexagon();
107
Michael Butler76cd09d2017-10-02 17:49:46 -0700108 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 }
Michael Butler82d31132017-10-04 03:21:58 -0700116 if (!hexagon::isHexagonAvailable()) {
117 callback->notify(ErrorStatus::DEVICE_UNAVAILABLE, nullptr);
118 return ErrorStatus::DEVICE_UNAVAILABLE;
119 }
Michael Butler76cd09d2017-10-02 17:49:46 -0700120
Michael Butler0f4cdac2017-10-25 17:41:34 -0700121 // TODO: once nnlib hanging issue is resolved, make this function
122 // asynchronous again
123 asyncPrepare(model, callback);
Michael Butler76cd09d2017-10-02 17:49:46 -0700124
125 return ErrorStatus::NONE;
126}
127
128Return<DeviceStatus> Device::getStatus() {
Michael Butler82d31132017-10-04 03:21:58 -0700129 configureHexagon();
130 mCurrentStatus =
Michael Butlera6e68df2017-12-04 12:16:42 -0800131 hexagon::isHexagonAvailable() ? DeviceStatus::AVAILABLE : DeviceStatus::OFFLINE;
Michael Butler76cd09d2017-10-02 17:49:46 -0700132 return mCurrentStatus;
133}
134
135} // namespace implementation
136} // namespace V1_0
137} // namespace neuralnetworks
138} // namespace hardware
139} // namespace android