David Gross | 83e24dc | 2017-09-10 14:31:58 -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 "CompilationBuilder" |
| 18 | |
| 19 | #include "CompilationBuilder.h" |
| 20 | |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Michael Butler | 83e406e | 2019-12-16 18:32:45 -0800 | [diff] [blame] | 22 | #include <limits> |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 23 | #include <memory> |
| 24 | #include <string> |
| 25 | #include <utility> |
| 26 | #include <vector> |
Michael Butler | 83e406e | 2019-12-16 18:32:45 -0800 | [diff] [blame] | 27 | |
Michael Butler | 89e99ba | 2019-01-24 02:36:37 -0800 | [diff] [blame] | 28 | #include "BurstBuilder.h" |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 29 | #include "ExecutionBuilder.h" |
Michael Butler | 89e99ba | 2019-01-24 02:36:37 -0800 | [diff] [blame] | 30 | #include "ExecutionBurstController.h" |
David Gross | 0b9453e | 2017-09-22 17:16:51 -0700 | [diff] [blame] | 31 | #include "ExecutionPlan.h" |
David Gross | 1f43815 | 2017-09-28 09:18:51 -0700 | [diff] [blame] | 32 | #include "Manager.h" |
David Gross | 0b9453e | 2017-09-22 17:16:51 -0700 | [diff] [blame] | 33 | #include "ModelBuilder.h" |
| 34 | #include "Utils.h" |
David Gross | 83e24dc | 2017-09-10 14:31:58 -0700 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | namespace nn { |
| 38 | |
Michael Butler | 83e406e | 2019-12-16 18:32:45 -0800 | [diff] [blame] | 39 | using namespace hal; |
| 40 | |
Miao Wang | 8e4df91 | 2018-10-02 13:29:00 -0700 | [diff] [blame] | 41 | CompilationBuilder::CompilationBuilder(const ModelBuilder* model, |
David Gross | 07c3f75 | 2019-03-28 13:38:16 -0700 | [diff] [blame] | 42 | const std::vector<std::shared_ptr<Device>>& devices, |
| 43 | bool explicitDeviceList) |
| 44 | : mModel(model), |
| 45 | mPartitioning(explicitDeviceList ? DeviceManager::kPartitioningWithoutFallback |
| 46 | : DeviceManager::get()->getPartitioning()), |
| 47 | mDevices(devices), |
| 48 | mExplicitDeviceList(explicitDeviceList) { |
Miao Wang | 820215d | 2017-10-04 19:45:45 -0700 | [diff] [blame] | 49 | VLOG(COMPILATION) << "CompilationBuilder::CompilationBuilder"; |
David Gross | 83e24dc | 2017-09-10 14:31:58 -0700 | [diff] [blame] | 50 | } |
| 51 | |
David Gross | 65aa556 | 2017-09-13 19:24:00 -0700 | [diff] [blame] | 52 | int CompilationBuilder::finish() { |
| 53 | if (mFinished) { |
| 54 | LOG(ERROR) << "ANeuralNetworksCompilation_finish called more than once"; |
| 55 | return ANEURALNETWORKS_BAD_STATE; |
| 56 | } |
Jean-Luc Brouillet | e127e49 | 2017-09-27 23:59:20 -0700 | [diff] [blame] | 57 | // TODO validate the rest |
David Gross | 65aa556 | 2017-09-13 19:24:00 -0700 | [diff] [blame] | 58 | |
Michael Butler | f2091af | 2020-02-25 11:39:05 -0800 | [diff] [blame] | 59 | const auto deadline = makeDeadline(mTimeoutDuration); |
Michael Butler | 83e406e | 2019-12-16 18:32:45 -0800 | [diff] [blame] | 60 | |
David Gross | 65aa556 | 2017-09-13 19:24:00 -0700 | [diff] [blame] | 61 | mFinished = true; |
Xusong Wang | d01b6d6 | 2019-01-14 18:53:00 -0800 | [diff] [blame] | 62 | if (mIsCacheInfoProvided) { |
| 63 | mPlan.setCaching(&mCacheDir, mToken); |
| 64 | } |
David Gross | c2f1c11 | 2017-11-13 10:17:31 -0800 | [diff] [blame] | 65 | if (mPartitioning) { |
Michael Butler | f2091af | 2020-02-25 11:39:05 -0800 | [diff] [blame] | 66 | int n = mModel->partitionTheWork(mDevices, mPreference, mPriority, deadline, &mPlan); |
Jean-Luc Brouillet | fc67d17 | 2017-10-12 23:30:10 -0700 | [diff] [blame] | 67 | switch (n) { |
| 68 | case ANEURALNETWORKS_NO_ERROR: |
Xusong Wang | 8e0bbbd | 2018-10-25 18:49:54 -0700 | [diff] [blame] | 69 | return n; |
Jean-Luc Brouillet | fc67d17 | 2017-10-12 23:30:10 -0700 | [diff] [blame] | 70 | case ANEURALNETWORKS_UNEXPECTED_NULL: |
| 71 | case ANEURALNETWORKS_BAD_DATA: |
| 72 | // The two error codes above should only be used for errors in the user's |
| 73 | // request. In case of a user error, we won't try any fallback. |
| 74 | // TODO: Document this in NeuralNetworks.h and in the HAL. Make sure |
| 75 | // driver writers know which code they can return. |
| 76 | return n; |
| 77 | default: |
| 78 | // The error might be recoverable. Return the error only if falling back |
| 79 | // is not allowed. |
| 80 | if (!DeviceManager::partitioningAllowsFallback(mPartitioning)) { |
| 81 | return n; |
| 82 | } |
David Gross | dc52e6c | 2018-08-17 12:37:41 -0700 | [diff] [blame] | 83 | if (mModel->hasOEMOperation()) { |
Slava Shklyaev | e86a07b | 2019-01-23 16:09:51 +0000 | [diff] [blame] | 84 | LOG(ERROR) << "Cannot fall back to CPU because of an OEM operation"; |
| 85 | return n; |
| 86 | } |
| 87 | if (mModel->hasExtensionOperation()) { |
| 88 | LOG(ERROR) << "Cannot fall back to CPU because of an extension operation"; |
David Gross | dc52e6c | 2018-08-17 12:37:41 -0700 | [diff] [blame] | 89 | return n; |
| 90 | } |
Jean-Luc Brouillet | fc67d17 | 2017-10-12 23:30:10 -0700 | [diff] [blame] | 91 | break; |
David Gross | 1f43815 | 2017-09-28 09:18:51 -0700 | [diff] [blame] | 92 | } |
David Gross | 0b9453e | 2017-09-22 17:16:51 -0700 | [diff] [blame] | 93 | } |
David Gross | 0b9453e | 2017-09-22 17:16:51 -0700 | [diff] [blame] | 94 | |
Xusong Wang | 8e0bbbd | 2018-10-25 18:49:54 -0700 | [diff] [blame] | 95 | // Fallback to CPU |
| 96 | VLOG(COMPILATION) << "CompilationBuilder::finish with CPU fallback"; |
| 97 | mPlan.reset(); |
| 98 | mPlan.becomeSingleStep(DeviceManager::getCpuDevice(), mModel); |
Michael Butler | f2091af | 2020-02-25 11:39:05 -0800 | [diff] [blame] | 99 | return mPlan.finish(mPreference, mPriority, deadline); |
David Gross | 65aa556 | 2017-09-13 19:24:00 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | int CompilationBuilder::setPreference(int32_t preference) { |
| 103 | if (mFinished) { |
Michael Butler | 67e41a5 | 2019-07-22 18:59:46 -0700 | [diff] [blame] | 104 | LOG(ERROR) << "ANeuralNetworksCompilation_setPreference can't modify after compilation " |
| 105 | "finished"; |
David Gross | 65aa556 | 2017-09-13 19:24:00 -0700 | [diff] [blame] | 106 | return ANEURALNETWORKS_BAD_STATE; |
| 107 | } |
Jean-Luc Brouillet | e127e49 | 2017-09-27 23:59:20 -0700 | [diff] [blame] | 108 | if (preference >= kNumberOfPreferences) { |
| 109 | LOG(ERROR) << "ANeuralNetworksCompilation_setPreference invalid preference " << preference; |
| 110 | return ANEURALNETWORKS_BAD_DATA; |
| 111 | } |
| 112 | |
David Gross | 65aa556 | 2017-09-13 19:24:00 -0700 | [diff] [blame] | 113 | mPreference = preference; |
David Gross | 83e24dc | 2017-09-10 14:31:58 -0700 | [diff] [blame] | 114 | return ANEURALNETWORKS_NO_ERROR; |
| 115 | } |
| 116 | |
Xusong Wang | 7481a7a | 2019-01-14 16:31:15 -0800 | [diff] [blame] | 117 | int CompilationBuilder::setCaching(const std::string& cacheDir, const uint8_t* token) { |
| 118 | if (mFinished) { |
| 119 | LOG(ERROR) |
| 120 | << "ANeuralNetworksCompilation_setCaching can't modify after compilation finished"; |
| 121 | return ANEURALNETWORKS_BAD_STATE; |
| 122 | } |
| 123 | mCacheDir = cacheDir; |
| 124 | // Make sure the cache dir can concat with the filename. |
| 125 | if (!mCacheDir.empty() && mCacheDir.back() != '/') { |
| 126 | mCacheDir.push_back('/'); |
| 127 | } |
Xusong Wang | f6d7365 | 2019-01-23 21:57:14 -0800 | [diff] [blame] | 128 | std::copy(token, token + ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN, mToken); |
| 129 | mIsCacheInfoProvided = true; |
Xusong Wang | 7481a7a | 2019-01-14 16:31:15 -0800 | [diff] [blame] | 130 | return ANEURALNETWORKS_NO_ERROR; |
| 131 | } |
| 132 | |
Michael Butler | 83e406e | 2019-12-16 18:32:45 -0800 | [diff] [blame] | 133 | int CompilationBuilder::setPriority(int32_t priority) { |
| 134 | if (mFinished) { |
| 135 | LOG(ERROR) << "ANeuralNetworksCompilation_setPriority can't modify after compilation " |
| 136 | "finished"; |
| 137 | return ANEURALNETWORKS_BAD_STATE; |
| 138 | } |
| 139 | if (priority != ANEURALNETWORKS_PRIORITY_LOW && priority != ANEURALNETWORKS_PRIORITY_MEDIUM && |
| 140 | priority != ANEURALNETWORKS_PRIORITY_HIGH) { |
| 141 | LOG(ERROR) << "ANeuralNetworksCompilation_setPriority invalid priority " << priority; |
| 142 | return ANEURALNETWORKS_BAD_DATA; |
| 143 | } |
| 144 | |
| 145 | mPriority = priority; |
| 146 | return ANEURALNETWORKS_NO_ERROR; |
| 147 | } |
| 148 | |
| 149 | int CompilationBuilder::setTimeoutDuration(uint64_t duration) { |
| 150 | if (mFinished) { |
| 151 | LOG(ERROR) << "ANeuralNetworksCompilation_setTimeout can't modify after compilation " |
| 152 | "finished"; |
| 153 | return ANEURALNETWORKS_BAD_STATE; |
| 154 | } |
| 155 | if (!mExplicitDeviceList || (mDevices.size() != 1)) { |
| 156 | LOG(ERROR) << "ANeuralNetworksCompilation_setTimeout called on an " |
| 157 | "ANeuralNetworksCompilation that was not created by " |
| 158 | "ANeuralNetworksCompilation_createForDevices with numDevices = 1"; |
| 159 | return ANEURALNETWORKS_BAD_DATA; |
| 160 | } |
Michael Butler | 5d1916f | 2020-02-10 15:45:28 -0800 | [diff] [blame] | 161 | if (duration > 0) { |
| 162 | mTimeoutDuration = duration; |
| 163 | } else { |
| 164 | mTimeoutDuration.reset(); |
| 165 | } |
Michael Butler | 83e406e | 2019-12-16 18:32:45 -0800 | [diff] [blame] | 166 | return ANEURALNETWORKS_NO_ERROR; |
| 167 | } |
| 168 | |
David Gross | c2f1c11 | 2017-11-13 10:17:31 -0800 | [diff] [blame] | 169 | int CompilationBuilder::setPartitioning(uint32_t partitioning) { |
| 170 | if (mFinished) { |
Michael Butler | 67e41a5 | 2019-07-22 18:59:46 -0700 | [diff] [blame] | 171 | LOG(ERROR) << "ANeuralNetworksCompilation_setPartitioning can't modify after compilation " |
| 172 | "finished"; |
David Gross | c2f1c11 | 2017-11-13 10:17:31 -0800 | [diff] [blame] | 173 | return ANEURALNETWORKS_BAD_STATE; |
| 174 | } |
| 175 | |
| 176 | mPartitioning = partitioning; |
| 177 | return ANEURALNETWORKS_NO_ERROR; |
| 178 | } |
| 179 | |
Michael Butler | 67e41a5 | 2019-07-22 18:59:46 -0700 | [diff] [blame] | 180 | int CompilationBuilder::createExecution(ExecutionBuilder** execution) { |
David Gross | 65aa556 | 2017-09-13 19:24:00 -0700 | [diff] [blame] | 181 | if (!mFinished) { |
| 182 | LOG(ERROR) << "ANeuralNetworksExecution_create passed an unfinished compilation"; |
| 183 | *execution = nullptr; |
| 184 | return ANEURALNETWORKS_BAD_STATE; |
| 185 | } |
Xusong Wang | 8e0bbbd | 2018-10-25 18:49:54 -0700 | [diff] [blame] | 186 | if (!mPlan.isValid()) { |
| 187 | LOG(ERROR) << "ANeuralNetworksExecution_create passed an invalid compilation"; |
| 188 | *execution = nullptr; |
| 189 | return ANEURALNETWORKS_BAD_STATE; |
| 190 | } |
Przemyslaw Szczepaniak | 03d7d98 | 2018-04-20 14:22:10 +0100 | [diff] [blame] | 191 | *execution = new (std::nothrow) ExecutionBuilder(this); |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 192 | return (*execution ? ANEURALNETWORKS_NO_ERROR : ANEURALNETWORKS_OUT_OF_MEMORY); |
David Gross | 83e24dc | 2017-09-10 14:31:58 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Michael Butler | 89e99ba | 2019-01-24 02:36:37 -0800 | [diff] [blame] | 195 | int CompilationBuilder::createBurst(BurstBuilder** burst) { |
| 196 | if (!mFinished) { |
| 197 | LOG(ERROR) << "ANeuralNetworksBurst_create passed an unfinished compilation"; |
| 198 | *burst = nullptr; |
| 199 | return ANEURALNETWORKS_BAD_STATE; |
| 200 | } |
| 201 | if (!mPlan.isValid()) { |
| 202 | LOG(ERROR) << "ANeuralNetworksBurst_create passed an invalid compilation"; |
| 203 | *burst = nullptr; |
| 204 | return ANEURALNETWORKS_BAD_STATE; |
| 205 | } |
Michael Butler | c82044a | 2019-06-24 10:36:20 -0700 | [diff] [blame] | 206 | std::vector<std::shared_ptr<ExecutionBurstController>> burstControllers = |
| 207 | mPlan.makeBursts(mPreference); |
Michael Butler | 89e99ba | 2019-01-24 02:36:37 -0800 | [diff] [blame] | 208 | *burst = new (std::nothrow) BurstBuilder(this, std::move(burstControllers)); |
| 209 | return (*burst ? ANEURALNETWORKS_NO_ERROR : ANEURALNETWORKS_OUT_OF_MEMORY); |
| 210 | } |
| 211 | |
Xusong Wang | c4b4cca | 2019-11-27 11:44:03 -0800 | [diff] [blame] | 212 | int CompilationBuilder::forEachStepRoleOfInput(uint32_t index, |
| 213 | const StepRoleCallback& callback) const { |
| 214 | if (!mFinished) { |
| 215 | LOG(ERROR) << "ANeuralNetworksMemoryDesc_addInputRole passed an unfinished compilation"; |
| 216 | return ANEURALNETWORKS_BAD_STATE; |
| 217 | } |
| 218 | if (!mPlan.isValid()) { |
| 219 | LOG(ERROR) << "ANeuralNetworksMemoryDesc_addInputRole passed an invalid compilation"; |
| 220 | return ANEURALNETWORKS_BAD_STATE; |
| 221 | } |
| 222 | if (index >= mModel->inputCount()) { |
| 223 | LOG(ERROR) << "ANeuralNetworksMemoryDesc_addInputRole passed an invalid input index " |
| 224 | << index; |
| 225 | return ANEURALNETWORKS_BAD_DATA; |
| 226 | } |
| 227 | mPlan.forEachStepRoleOfInput(index, callback); |
| 228 | return ANEURALNETWORKS_NO_ERROR; |
| 229 | } |
| 230 | |
| 231 | int CompilationBuilder::forEachStepRoleOfOutput(uint32_t index, |
| 232 | const StepRoleCallback& callback) const { |
| 233 | if (!mFinished) { |
| 234 | LOG(ERROR) << "ANeuralNetworksMemoryDesc_addOutputRole passed an unfinished compilation"; |
| 235 | return ANEURALNETWORKS_BAD_STATE; |
| 236 | } |
| 237 | if (!mPlan.isValid()) { |
| 238 | LOG(ERROR) << "ANeuralNetworksMemoryDesc_addOutputRole passed an invalid compilation"; |
| 239 | return ANEURALNETWORKS_BAD_STATE; |
| 240 | } |
| 241 | if (index >= mModel->outputCount()) { |
| 242 | LOG(ERROR) << "ANeuralNetworksMemoryDesc_addOutputRole passed an invalid output index " |
| 243 | << index; |
| 244 | return ANEURALNETWORKS_BAD_DATA; |
| 245 | } |
| 246 | mPlan.forEachStepRoleOfOutput(index, callback); |
| 247 | return ANEURALNETWORKS_NO_ERROR; |
| 248 | } |
| 249 | |
David Gross | 83e24dc | 2017-09-10 14:31:58 -0700 | [diff] [blame] | 250 | } // namespace nn |
| 251 | } // namespace android |