Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 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 "android.hardware.neuralnetworks@1.0-impl-hvx" |
| 18 | |
| 19 | #include "Device.h" |
Michael Butler | a6e68df | 2017-12-04 12:16:42 -0800 | [diff] [blame] | 20 | #include <android-base/logging.h> |
| 21 | #include <memory> |
| 22 | #include <mutex> |
| 23 | #include <thread> |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 24 | #include "HexagonModel.h" |
| 25 | #include "HexagonUtils.h" |
| 26 | #include "PreparedModel.h" |
Jean-Luc Brouillet | cf8d454 | 2017-10-16 02:35:33 -0700 | [diff] [blame] | 27 | #include "ValidateHal.h" |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace hardware { |
| 31 | namespace neuralnetworks { |
| 32 | namespace V1_0 { |
| 33 | namespace implementation { |
| 34 | |
| 35 | Device::Device() : mCurrentStatus(DeviceStatus::AVAILABLE) {} |
| 36 | |
| 37 | Device::~Device() {} |
| 38 | |
Michael Butler | 82d3113 | 2017-10-04 03:21:58 -0700 | [diff] [blame] | 39 | static std::once_flag configure_nnlib; |
| 40 | static void configureHexagon() { |
Michael Butler | a6e68df | 2017-12-04 12:16:42 -0800 | [diff] [blame] | 41 | std::call_once(configure_nnlib, []() { |
| 42 | hexagon::Controller::getInstance().config(); |
Michael Butler | a6e68df | 2017-12-04 12:16:42 -0800 | [diff] [blame] | 43 | }); |
Michael Butler | 82d3113 | 2017-10-04 03:21:58 -0700 | [diff] [blame] | 44 | } |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 45 | |
| 46 | Return<void> Device::getCapabilities(getCapabilities_cb _hidl_cb) { |
Michael Butler | 82d3113 | 2017-10-04 03:21:58 -0700 | [diff] [blame] | 47 | configureHexagon(); |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 48 | |
Jean-Luc Brouillet | 5fa0061 | 2017-10-12 23:18:53 -0700 | [diff] [blame] | 49 | // These numbers are approximations for this release. |
| 50 | // TODO Change with the actual number. |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 51 | PerformanceInfo float32Performance = { |
Michael Butler | a6e68df | 2017-12-04 12:16:42 -0800 | [diff] [blame] | 52 | .execTime = 30.0f, .powerUsage = 2.0f, |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | PerformanceInfo quantized8Performance = { |
Michael Butler | a6e68df | 2017-12-04 12:16:42 -0800 | [diff] [blame] | 56 | .execTime = 0.7f, .powerUsage = 0.7f, |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | Capabilities capabilities = { |
Michael Butler | a6e68df | 2017-12-04 12:16:42 -0800 | [diff] [blame] | 60 | .float32Performance = float32Performance, .quantized8Performance = quantized8Performance, |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Michael Butler | 82d3113 | 2017-10-04 03:21:58 -0700 | [diff] [blame] | 63 | ErrorStatus status = |
Michael Butler | a6e68df | 2017-12-04 12:16:42 -0800 | [diff] [blame] | 64 | hexagon::isHexagonAvailable() ? ErrorStatus::NONE : ErrorStatus::DEVICE_UNAVAILABLE; |
Michael Butler | 82d3113 | 2017-10-04 03:21:58 -0700 | [diff] [blame] | 65 | |
| 66 | _hidl_cb(status, capabilities); |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 67 | return Void(); |
| 68 | } |
| 69 | |
| 70 | Return<void> Device::getSupportedOperations(const Model& model, |
| 71 | getSupportedOperations_cb _hidl_cb) { |
Michael Butler | 82d3113 | 2017-10-04 03:21:58 -0700 | [diff] [blame] | 72 | configureHexagon(); |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 73 | |
| 74 | if (!nn::validateModel(model)) { |
Michael Butler | 82d3113 | 2017-10-04 03:21:58 -0700 | [diff] [blame] | 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>{}); |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 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 | |
Michael Butler | 25d8e87 | 2017-12-19 11:59:54 -0800 | [diff] [blame] | 90 | static void asyncPrepare(const Model& model, const sp<IPreparedModelCallback>& callback) { |
Michael Butler | 20a5bca | 2017-10-05 15:02:31 -0700 | [diff] [blame] | 91 | std::shared_ptr<hexagon::Model> hexagonModel = std::make_shared<hexagon::Model>(model); |
Michael Butler | 82d3113 | 2017-10-04 03:21:58 -0700 | [diff] [blame] | 92 | |
Michael Butler | 9001cd5 | 2017-12-08 11:12:29 -0800 | [diff] [blame] | 93 | Return<void> ret; |
Michael Butler | 0f4cdac | 2017-10-25 17:41:34 -0700 | [diff] [blame] | 94 | if (hexagonModel->prepare()) { |
Michael Butler | 9001cd5 | 2017-12-08 11:12:29 -0800 | [diff] [blame] | 95 | ret = callback->notify(ErrorStatus::NONE, new PreparedModel(model, hexagonModel)); |
Michael Butler | a6e68df | 2017-12-04 12:16:42 -0800 | [diff] [blame] | 96 | } else { |
Michael Butler | 9001cd5 | 2017-12-08 11:12:29 -0800 | [diff] [blame] | 97 | ret = callback->notify(ErrorStatus::GENERAL_FAILURE, nullptr); |
| 98 | } |
| 99 | if (!ret.isOk()) { |
| 100 | LOG(ERROR) << "Error in callback's return type: " << ret.description(); |
Michael Butler | 82d3113 | 2017-10-04 03:21:58 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 104 | Return<ErrorStatus> Device::prepareModel(const Model& model, |
| 105 | const sp<IPreparedModelCallback>& callback) { |
Michael Butler | 82d3113 | 2017-10-04 03:21:58 -0700 | [diff] [blame] | 106 | configureHexagon(); |
| 107 | |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 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 | } |
Michael Butler | 82d3113 | 2017-10-04 03:21:58 -0700 | [diff] [blame] | 116 | if (!hexagon::isHexagonAvailable()) { |
| 117 | callback->notify(ErrorStatus::DEVICE_UNAVAILABLE, nullptr); |
| 118 | return ErrorStatus::DEVICE_UNAVAILABLE; |
| 119 | } |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 120 | |
Michael Butler | 0f4cdac | 2017-10-25 17:41:34 -0700 | [diff] [blame] | 121 | // TODO: once nnlib hanging issue is resolved, make this function |
| 122 | // asynchronous again |
| 123 | asyncPrepare(model, callback); |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 124 | |
| 125 | return ErrorStatus::NONE; |
| 126 | } |
| 127 | |
| 128 | Return<DeviceStatus> Device::getStatus() { |
Michael Butler | 82d3113 | 2017-10-04 03:21:58 -0700 | [diff] [blame] | 129 | configureHexagon(); |
| 130 | mCurrentStatus = |
Michael Butler | a6e68df | 2017-12-04 12:16:42 -0800 | [diff] [blame] | 131 | hexagon::isHexagonAvailable() ? DeviceStatus::AVAILABLE : DeviceStatus::OFFLINE; |
Michael Butler | 76cd09d | 2017-10-02 17:49:46 -0700 | [diff] [blame] | 132 | return mCurrentStatus; |
| 133 | } |
| 134 | |
| 135 | } // namespace implementation |
| 136 | } // namespace V1_0 |
| 137 | } // namespace neuralnetworks |
| 138 | } // namespace hardware |
| 139 | } // namespace android |