Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -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 | |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 17 | #define LOG_TAG "ExecutionBuilder" |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 18 | |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 19 | #include "ExecutionBuilder.h" |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 20 | |
David Gross | 83e24dc | 2017-09-10 14:31:58 -0700 | [diff] [blame] | 21 | #include "CompilationBuilder.h" |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 22 | #include "CpuExecutor.h" |
| 23 | #include "HalInterfaces.h" |
| 24 | #include "Manager.h" |
| 25 | #include "ModelBuilder.h" |
| 26 | |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 27 | #include <mutex> |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 28 | #include <thread> |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace nn { |
| 33 | |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 34 | int ModelArgumentInfo::setFromPointer(const Operand& operand, |
| 35 | const ANeuralNetworksOperandType* type, void* data, |
| 36 | uint32_t length) { |
| 37 | int n = updateDimensionInfo(operand, type); |
| 38 | if (n != ANEURALNETWORKS_NO_ERROR) { |
| 39 | return n; |
| 40 | } |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 41 | if (data == nullptr) { |
| 42 | if (length) { |
| 43 | LOG(ERROR) << "Setting argument as having no value but non-zero length passed."; |
| 44 | return ANEURALNETWORKS_BAD_DATA; |
| 45 | } |
| 46 | state = ModelArgumentInfo::HAS_NO_VALUE; |
| 47 | } else { |
| 48 | state = ModelArgumentInfo::POINTER; |
| 49 | } |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 50 | buffer = data; |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 51 | locationAndLength = {.poolIndex = 0, .offset = 0, .length = length}; |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 52 | return ANEURALNETWORKS_NO_ERROR; |
| 53 | } |
| 54 | |
| 55 | int ModelArgumentInfo::setFromMemory(const Operand& operand, const ANeuralNetworksOperandType* type, |
| 56 | uint32_t poolIndex, uint32_t offset, uint32_t length) { |
| 57 | int n = updateDimensionInfo(operand, type); |
| 58 | if (n != ANEURALNETWORKS_NO_ERROR) { |
| 59 | return n; |
| 60 | } |
| 61 | state = ModelArgumentInfo::MEMORY; |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 62 | locationAndLength = {.poolIndex = poolIndex, .offset = offset, .length = length}; |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 63 | buffer = nullptr; |
| 64 | return ANEURALNETWORKS_NO_ERROR; |
| 65 | } |
| 66 | |
David Gross | 8fb14e9 | 2017-10-04 15:16:02 -0700 | [diff] [blame] | 67 | int ModelArgumentInfo::setFromTemporaryMemory(const Operand& operand, |
| 68 | uint32_t poolIndex, uint32_t offset) { |
David Gross | 4d83c52 | 2017-10-03 23:20:24 -0700 | [diff] [blame] | 69 | dimensions = operand.dimensions; |
David Gross | 96811e2 | 2017-10-02 14:40:09 -0700 | [diff] [blame] | 70 | state = ModelArgumentInfo::MEMORY; |
David Gross | 4d83c52 | 2017-10-03 23:20:24 -0700 | [diff] [blame] | 71 | locationAndLength = |
David Gross | 8fb14e9 | 2017-10-04 15:16:02 -0700 | [diff] [blame] | 72 | {.poolIndex = poolIndex, .offset = offset, .length = sizeOfData(operand)}; |
David Gross | 96811e2 | 2017-10-02 14:40:09 -0700 | [diff] [blame] | 73 | buffer = nullptr; |
| 74 | return ANEURALNETWORKS_NO_ERROR; |
| 75 | } |
| 76 | |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 77 | int ModelArgumentInfo::updateDimensionInfo(const Operand& operand, |
| 78 | const ANeuralNetworksOperandType* newType) { |
| 79 | if (newType == nullptr) { |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 80 | dimensions = hidl_vec<uint32_t>(); |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 81 | } else { |
Jean-Luc Brouillet | d2d0c03 | 2017-09-12 12:03:41 -0700 | [diff] [blame] | 82 | uint32_t count = newType->dimensionCount; |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 83 | if (static_cast<OperandType>(newType->type) != operand.type || |
| 84 | count != operand.dimensions.size()) { |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 85 | LOG(ERROR) << "ANeuralNetworksExecution_setInput/Output incompatible types"; |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 86 | return ANEURALNETWORKS_BAD_DATA; |
| 87 | } |
| 88 | for (uint32_t i = 0; i < count; i++) { |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 89 | dimensions[i] = newType->dimensions[i]; |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | return ANEURALNETWORKS_NO_ERROR; |
| 93 | } |
| 94 | |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 95 | ExecutionBuilder::ExecutionBuilder(const CompilationBuilder* compilation) : |
David Gross | 83e24dc | 2017-09-10 14:31:58 -0700 | [diff] [blame] | 96 | mModel(compilation->mModel), |
David Gross | 1f43815 | 2017-09-28 09:18:51 -0700 | [diff] [blame] | 97 | mPlan(&compilation->mPlan), |
David Gross | 83e24dc | 2017-09-10 14:31:58 -0700 | [diff] [blame] | 98 | mInputs(mModel->inputCount()), |
Jean-Luc Brouillet | 1da8fed | 2017-10-11 22:34:04 -0700 | [diff] [blame] | 99 | mOutputs(mModel->outputCount()) { |
Miao Wang | 820215d | 2017-10-04 19:45:45 -0700 | [diff] [blame] | 100 | VLOG(EXECUTION) << "ExecutionBuilder::ExecutionBuilder"; |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 101 | } |
| 102 | |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 103 | int ExecutionBuilder::setInput(uint32_t index, const ANeuralNetworksOperandType* type, |
Jean-Luc Brouillet | e127e49 | 2017-09-27 23:59:20 -0700 | [diff] [blame] | 104 | const void* buffer, size_t length) { |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 105 | uint32_t count = static_cast<uint32_t>(mInputs.size()); |
| 106 | if (index >= count) { |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 107 | LOG(ERROR) << "ANeuralNetworksExecution_setInput bad index " << index << " " << count; |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 108 | return ANEURALNETWORKS_BAD_DATA; |
| 109 | } |
Jean-Luc Brouillet | e127e49 | 2017-09-27 23:59:20 -0700 | [diff] [blame] | 110 | if (type != nullptr) { |
| 111 | int n = validateOperandType(*type, "ANeuralNetworksExecution_setInput", false); |
| 112 | if (n != ANEURALNETWORKS_NO_ERROR) { |
| 113 | return n; |
| 114 | } |
| 115 | } |
| 116 | if (length > 0xFFFFFFFF) { |
| 117 | LOG(ERROR) << "ANeuralNetworksExecution_setInput input exceeds max length " << length; |
| 118 | return ANEURALNETWORKS_BAD_DATA; |
| 119 | } |
| 120 | uint32_t l = static_cast<uint32_t>(length); |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 121 | return mInputs[index].setFromPointer(mModel->getInputOperand(index), type, |
Jean-Luc Brouillet | e127e49 | 2017-09-27 23:59:20 -0700 | [diff] [blame] | 122 | const_cast<void*>(buffer), l); |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 123 | } |
| 124 | |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 125 | int ExecutionBuilder::setInputFromMemory(uint32_t index, const ANeuralNetworksOperandType* type, |
Jean-Luc Brouillet | e127e49 | 2017-09-27 23:59:20 -0700 | [diff] [blame] | 126 | const Memory* memory, size_t offset, size_t length) { |
David Gross | 96811e2 | 2017-10-02 14:40:09 -0700 | [diff] [blame] | 127 | // Should be similar to StepExecutor::setInputOrOutputFromTemporaryMemory() |
| 128 | |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 129 | uint32_t count = static_cast<uint32_t>(mInputs.size()); |
| 130 | if (index >= count) { |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 131 | LOG(ERROR) << "ANeuralNetworksExecution_setInputFromMemory bad index " << index << " " |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 132 | << count; |
| 133 | return ANEURALNETWORKS_BAD_DATA; |
| 134 | } |
Miao Wang | 105807d | 2017-09-05 14:41:05 -0700 | [diff] [blame] | 135 | if (!memory->validateSize(offset, length)) { |
| 136 | return ANEURALNETWORKS_BAD_DATA; |
| 137 | } |
Jean-Luc Brouillet | e127e49 | 2017-09-27 23:59:20 -0700 | [diff] [blame] | 138 | // TODO validate the rest |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 139 | uint32_t poolIndex = mMemories.add(memory); |
| 140 | return mInputs[index].setFromMemory(mModel->getInputOperand(index), type, poolIndex, offset, |
| 141 | length); |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 142 | } |
| 143 | |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 144 | int ExecutionBuilder::setOutput(uint32_t index, const ANeuralNetworksOperandType* type, void* buffer, |
Jean-Luc Brouillet | e127e49 | 2017-09-27 23:59:20 -0700 | [diff] [blame] | 145 | size_t length) { |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 146 | uint32_t count = static_cast<uint32_t>(mOutputs.size()); |
| 147 | if (index >= count) { |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 148 | LOG(ERROR) << "ANeuralNetworksExecution_setOutput bad index " << index << " " << count; |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 149 | return ANEURALNETWORKS_BAD_DATA; |
| 150 | } |
Jean-Luc Brouillet | e127e49 | 2017-09-27 23:59:20 -0700 | [diff] [blame] | 151 | if (type != nullptr) { |
| 152 | int n = validateOperandType(*type, "ANeuralNetworksExecution_setOutput", false); |
| 153 | if (n != ANEURALNETWORKS_NO_ERROR) { |
| 154 | return n; |
| 155 | } |
| 156 | } |
| 157 | if (length > 0xFFFFFFFF) { |
| 158 | LOG(ERROR) << "ANeuralNetworksExecution_setOutput input exceeds max length " << length; |
| 159 | return ANEURALNETWORKS_BAD_DATA; |
| 160 | } |
| 161 | uint32_t l = static_cast<uint32_t>(length); |
| 162 | return mOutputs[index].setFromPointer(mModel->getOutputOperand(index), type, buffer, l); |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 163 | } |
| 164 | |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 165 | int ExecutionBuilder::setOutputFromMemory(uint32_t index, const ANeuralNetworksOperandType* type, |
Jean-Luc Brouillet | e127e49 | 2017-09-27 23:59:20 -0700 | [diff] [blame] | 166 | const Memory* memory, size_t offset, size_t length) { |
David Gross | 96811e2 | 2017-10-02 14:40:09 -0700 | [diff] [blame] | 167 | // Should be similar to StepExecutor::setInputOrOutputFromTemporaryMemory() |
| 168 | |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 169 | uint32_t count = static_cast<uint32_t>(mOutputs.size()); |
| 170 | if (index >= count) { |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 171 | LOG(ERROR) << "ANeuralNetworksExecution_setOutputFromMemory bad index " << index << " " |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 172 | << count; |
| 173 | return ANEURALNETWORKS_BAD_DATA; |
| 174 | } |
Miao Wang | 105807d | 2017-09-05 14:41:05 -0700 | [diff] [blame] | 175 | if (!memory->validateSize(offset, length)) { |
| 176 | return ANEURALNETWORKS_BAD_DATA; |
| 177 | } |
Jean-Luc Brouillet | e127e49 | 2017-09-27 23:59:20 -0700 | [diff] [blame] | 178 | // TODO validate the rest |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 179 | uint32_t poolIndex = mMemories.add(memory); |
| 180 | return mOutputs[index].setFromMemory(mModel->getOutputOperand(index), type, poolIndex, offset, |
| 181 | length); |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 182 | } |
| 183 | |
David Gross | 5e8feed | 2017-10-04 23:05:05 -0700 | [diff] [blame] | 184 | // Attempt synchronous execution of full model on CPU. |
| 185 | // Ensure that executionCallback->notify() is called. |
| 186 | static void cpuFallbackFull(const ExecutionBuilder* executionBuilder, |
| 187 | const sp<ExecutionCallback>& executionCallback) { |
Miao Wang | 820215d | 2017-10-04 19:45:45 -0700 | [diff] [blame] | 188 | VLOG(EXECUTION) << "cpuFallbackFull"; |
David Gross | 5e8feed | 2017-10-04 23:05:05 -0700 | [diff] [blame] | 189 | StepExecutor executor(executionBuilder, executionBuilder->getModel(), |
| 190 | nullptr /* no IDevice, so CPU */, |
| 191 | nullptr /* no IPreparedModel */); |
| 192 | executor.mapInputsAndOutputsTrivially(); |
| 193 | sp<ExecutionCallback> fallbackCallback; |
| 194 | if (executor.startCompute(&fallbackCallback) != ANEURALNETWORKS_NO_ERROR) { |
| 195 | executionCallback->notify(ErrorStatus::GENERAL_FAILURE); |
| 196 | return; |
| 197 | } |
| 198 | fallbackCallback->wait(); |
| 199 | executionCallback->notify(fallbackCallback->getStatus()); |
| 200 | } |
| 201 | |
| 202 | // Attempt synchronous execution on CPU. |
| 203 | // (1) First, attempt to execute this step on CPU. If successful, |
| 204 | // return true. (Do not call executionCallback->notify().) |
| 205 | // (2) If unsuccessful, attempt to execute the full model on CPU, |
| 206 | // ensure that executionCallback->notify() is called, and return |
| 207 | // false. |
| 208 | static bool cpuFallbackPartial(const ExecutionBuilder* executionBuilder, |
| 209 | const ExecutionPlan* plan, |
| 210 | std::shared_ptr<ExecutionPlan::Controller> controller, |
| 211 | const sp<ExecutionCallback>& executionCallback) { |
Miao Wang | 820215d | 2017-10-04 19:45:45 -0700 | [diff] [blame] | 212 | VLOG(EXECUTION) << "cpuFallbackPartial"; |
David Gross | 5e8feed | 2017-10-04 23:05:05 -0700 | [diff] [blame] | 213 | std::shared_ptr<StepExecutor> executor; |
| 214 | int n = plan->fallback(controller, &executor); |
| 215 | if (n != ANEURALNETWORKS_NO_ERROR || executor->isCpu()) { |
| 216 | cpuFallbackFull(executionBuilder, executionCallback); |
| 217 | return false; |
| 218 | } |
| 219 | sp<ExecutionCallback> fallbackCallback; |
| 220 | if (executor->startComputeOnCpu(&fallbackCallback) != ANEURALNETWORKS_NO_ERROR) { |
| 221 | cpuFallbackFull(executionBuilder, executionCallback); |
| 222 | return false; |
| 223 | } |
| 224 | fallbackCallback->wait(); |
| 225 | if (fallbackCallback->getStatus() != ErrorStatus::NONE) { |
| 226 | cpuFallbackFull(executionBuilder, executionCallback); |
| 227 | return false; |
| 228 | } |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | static void asyncStartComputePartitioned(const ExecutionBuilder* executionBuilder, |
| 233 | const ExecutionPlan* plan, |
David Gross | e317882 | 2017-10-04 16:05:58 -0700 | [diff] [blame] | 234 | std::shared_ptr<ExecutionPlan::Controller> controller, |
David Gross | 5e8feed | 2017-10-04 23:05:05 -0700 | [diff] [blame] | 235 | bool allowFallback, |
| 236 | const sp<ExecutionCallback>& executionCallback) { |
Miao Wang | 820215d | 2017-10-04 19:45:45 -0700 | [diff] [blame] | 237 | VLOG(EXECUTION) << "ExecutionBuilder::startCompute (from plan, iteratively)"; |
David Gross | e317882 | 2017-10-04 16:05:58 -0700 | [diff] [blame] | 238 | while (true) { |
| 239 | std::shared_ptr<StepExecutor> executor; |
Miao Wang | 820215d | 2017-10-04 19:45:45 -0700 | [diff] [blame] | 240 | VLOG(EXECUTION) << "looking for next StepExecutor"; |
David Gross | e317882 | 2017-10-04 16:05:58 -0700 | [diff] [blame] | 241 | int n = plan->next(controller, &executor); |
David Gross | 5e8feed | 2017-10-04 23:05:05 -0700 | [diff] [blame] | 242 | if (n != ANEURALNETWORKS_NO_ERROR) { |
| 243 | if (allowFallback) { |
| 244 | cpuFallbackFull(executionBuilder, executionCallback); |
| 245 | } else { |
| 246 | executionCallback->notify(ErrorStatus::GENERAL_FAILURE); |
| 247 | } |
| 248 | return; |
| 249 | } |
| 250 | if (executor == nullptr) { |
| 251 | executionCallback->notify(ErrorStatus::NONE); |
David Gross | e317882 | 2017-10-04 16:05:58 -0700 | [diff] [blame] | 252 | return; |
| 253 | } |
| 254 | |
| 255 | sp<ExecutionCallback> stepCallback; |
| 256 | n = executor->startCompute(&stepCallback); |
| 257 | if (n != ANEURALNETWORKS_NO_ERROR) { |
David Gross | 5e8feed | 2017-10-04 23:05:05 -0700 | [diff] [blame] | 258 | if (allowFallback) { |
| 259 | if (cpuFallbackPartial(executionBuilder, plan, controller, executionCallback)) { |
| 260 | // Successfully executed one step on CPU. |
| 261 | continue; |
| 262 | } else { |
| 263 | // Either successfully executed entire plan on |
| 264 | // CPU, or tried and failed to do so. |
| 265 | return; |
| 266 | } |
| 267 | } else { |
| 268 | executionCallback->notify(ErrorStatus::GENERAL_FAILURE); |
| 269 | return; |
| 270 | } |
David Gross | e317882 | 2017-10-04 16:05:58 -0700 | [diff] [blame] | 271 | } |
| 272 | stepCallback->wait(); |
| 273 | ErrorStatus status = stepCallback->getStatus(); |
| 274 | if (status != ErrorStatus::NONE) { |
David Gross | 5e8feed | 2017-10-04 23:05:05 -0700 | [diff] [blame] | 275 | if (allowFallback) { |
| 276 | if (cpuFallbackPartial(executionBuilder, plan, controller, executionCallback)) { |
| 277 | // Successfully executed one step on CPU. |
| 278 | continue; |
| 279 | } else { |
| 280 | // Either successfully executed entire plan on |
| 281 | // CPU, or tried and failed to do so. |
| 282 | return; |
| 283 | } |
| 284 | } else { |
| 285 | executionCallback->notify(status); |
| 286 | return; |
| 287 | } |
David Gross | e317882 | 2017-10-04 16:05:58 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 292 | int ExecutionBuilder::startCompute(sp<ExecutionCallback>* synchronizationCallback) { |
| 293 | *synchronizationCallback = nullptr; |
David Gross | 425b259 | 2017-09-13 19:33:14 -0700 | [diff] [blame] | 294 | |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 295 | // TODO validate that we have full types for all inputs and outputs, |
| 296 | // that the graph is not cyclic, |
Yang Ni | f1817c6 | 2017-08-22 16:18:50 -0700 | [diff] [blame] | 297 | |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 298 | for (auto& p : mInputs) { |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 299 | if (p.state == ModelArgumentInfo::UNSPECIFIED) { |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 300 | LOG(ERROR) << "ANeuralNetworksExecution_startCompute not all inputs specified"; |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 301 | return ANEURALNETWORKS_BAD_DATA; |
| 302 | } |
| 303 | } |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 304 | for (auto& p : mOutputs) { |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 305 | if (p.state == ModelArgumentInfo::UNSPECIFIED) { |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 306 | LOG(ERROR) << "ANeuralNetworksExecution_startCompute not all outputs specified"; |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 307 | return ANEURALNETWORKS_BAD_DATA; |
| 308 | } |
| 309 | } |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 310 | |
David Gross | 5e8feed | 2017-10-04 23:05:05 -0700 | [diff] [blame] | 311 | #ifndef DISABLE_PARTITIONED_EXECUTION |
| 312 | { |
| 313 | // TODO: Remove the non-plan-based path once we've fully integrated ExecutionPlan |
| 314 | // with the compilation and execution phases of the NN API? Or retain that path |
| 315 | // as a fallback in the case of partitioning failure? |
| 316 | // |
| 317 | // TODO: Entire plan-based-path should run in an asynchronous thread -- |
| 318 | // take the asynchronous thread logic out of startComputeOnCpu() and use |
| 319 | // it to wrap the plan-based-path. |
| 320 | const uint32_t partitioning = DeviceManager::get()->getPartitioning(); |
| 321 | if (partitioning > 0) { |
| 322 | const bool allowFallback = DeviceManager::partitioningAllowsFallback(partitioning); |
| 323 | std::shared_ptr<ExecutionPlan::Controller> controller = mPlan->makeController(this); |
| 324 | if (controller == nullptr) { |
| 325 | if (!allowFallback) { |
| 326 | return ANEURALNETWORKS_OP_FAILED; |
| 327 | } |
| 328 | } else { |
| 329 | // TODO: use a thread pool |
| 330 | |
| 331 | // Prepare the callback for asynchronous execution. |
| 332 | // sp<ExecutionCallback> object is returned when the |
| 333 | // execution has been successfully launched, otherwise a |
| 334 | // nullptr is returned. The executionCallback is |
| 335 | // abstracted in the NN API as an "event". |
| 336 | sp<ExecutionCallback> executionCallback = new ExecutionCallback(); |
| 337 | std::thread thread(asyncStartComputePartitioned, this, mPlan, controller, |
| 338 | allowFallback, |
| 339 | executionCallback); |
| 340 | executionCallback->bind_thread(std::move(thread)); |
| 341 | *synchronizationCallback = executionCallback; |
| 342 | return ANEURALNETWORKS_NO_ERROR; |
David Gross | a2a0363 | 2017-10-03 12:49:47 -0700 | [diff] [blame] | 343 | } |
David Gross | 1f43815 | 2017-09-28 09:18:51 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
David Gross | 5e8feed | 2017-10-04 23:05:05 -0700 | [diff] [blame] | 346 | #else |
| 347 | { |
| 348 | // Find a driver that can handle all the operations. |
| 349 | // TODO: Does not handle CPU fallback (which is tricky because |
| 350 | // StepExecutor::startCompute() is designed as |
| 351 | // asynchronous). |
| 352 | // TODO: Does not actually behave asynchronously (because |
| 353 | // StepExecutor::startCompute() isn't actually asynchronous |
| 354 | // on a device as opposed to a CPU). |
| 355 | Model hidlModel; |
| 356 | mModel->setHidlModel(&hidlModel); |
| 357 | const std::vector<std::shared_ptr<Device>>& devices = DeviceManager::get()->getDrivers(); |
| 358 | for (const auto& device : devices) { |
| 359 | hidl_vec<bool> supports; |
Miao Wang | 820215d | 2017-10-04 19:45:45 -0700 | [diff] [blame] | 360 | VLOG(EXECUTION) << "Checking " << device->getName(); |
David Gross | 5e8feed | 2017-10-04 23:05:05 -0700 | [diff] [blame] | 361 | device->getSupportedOperations(hidlModel, &supports); |
| 362 | if (std::find(supports.begin(), supports.end(), false) == supports.end()) { |
Miao Wang | 820215d | 2017-10-04 19:45:45 -0700 | [diff] [blame] | 363 | VLOG(EXECUTION) << "ExecutionBuilder::startCompute (without plan) on " << device->getName(); |
David Gross | 5e8feed | 2017-10-04 23:05:05 -0700 | [diff] [blame] | 364 | StepExecutor executor(this, mModel, device->getInterface(), |
| 365 | nullptr /* no IPreparedModel, so compile */); |
| 366 | executor.mapInputsAndOutputsTrivially(); |
| 367 | return executor.startCompute(synchronizationCallback); |
| 368 | } |
Jean-Luc Brouillet | ef22aa5 | 2017-09-15 22:53:49 -0700 | [diff] [blame] | 369 | } |
| 370 | } |
David Gross | 5e8feed | 2017-10-04 23:05:05 -0700 | [diff] [blame] | 371 | #endif // DISABLE_PARTITIONED_EXECUTION |
| 372 | |
| 373 | // Run on the CPU. |
Miao Wang | 820215d | 2017-10-04 19:45:45 -0700 | [diff] [blame] | 374 | VLOG(EXECUTION) << "ExecutionBuilder::startCompute (without plan) on CPU"; |
David Gross | b260491 | 2017-10-01 15:26:33 -0700 | [diff] [blame] | 375 | StepExecutor executor(this, mModel, |
| 376 | nullptr /* no IDevice, so CPU */, |
| 377 | nullptr /* no IPreparedModel */); |
| 378 | executor.mapInputsAndOutputsTrivially(); |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 379 | return executor.startCompute(synchronizationCallback); |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // Figures out how to place each of the input or outputs in a buffer. This just does the layout, |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 383 | // it does not copy data. Aligns each input a bit. |
David Gross | b260491 | 2017-10-01 15:26:33 -0700 | [diff] [blame] | 384 | int StepExecutor::allocatePointerArgumentsToPool(std::vector<ModelArgumentInfo>* args, |
| 385 | Memory* memory) { |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 386 | uint32_t nextPoolIndex = mMemories.size(); |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 387 | int64_t total = 0; |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 388 | for (auto& info : *args) { |
| 389 | if (info.state == ModelArgumentInfo::POINTER) { |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 390 | DataLocation& loc = info.locationAndLength; |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 391 | // TODO Good enough alignment? |
| 392 | total += alignBytesNeeded(static_cast<uint32_t>(total), loc.length); |
| 393 | loc.poolIndex = nextPoolIndex; |
| 394 | loc.offset = static_cast<uint32_t>(total); |
| 395 | total += loc.length; |
| 396 | } |
| 397 | }; |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 398 | if (total > 0xFFFFFFFF) { |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 399 | LOG(ERROR) << "ANeuralNetworksExecution_startCompute Size of all inputs or outputs exceeds " |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 400 | "2^32."; |
| 401 | return ANEURALNETWORKS_BAD_DATA; |
| 402 | } |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 403 | hidl_memory hidlMemory; |
| 404 | if (total > 0) { |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 405 | memory->create(total); // TODO check error |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 406 | mMemories.add(memory); |
| 407 | } |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 408 | return ANEURALNETWORKS_NO_ERROR; |
| 409 | } |
| 410 | |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 411 | static void setRequestArgumentArray(const std::vector<ModelArgumentInfo>& argumentInfos, |
Jean-Luc Brouillet | a4e2ee8 | 2017-09-09 19:27:25 -0700 | [diff] [blame] | 412 | hidl_vec<RequestArgument>* ioInfos) { |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 413 | size_t count = argumentInfos.size(); |
| 414 | ioInfos->resize(count); |
| 415 | for (size_t i = 0; i < count; i++) { |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 416 | const auto& info = argumentInfos[i]; |
| 417 | (*ioInfos)[i] = { .hasNoValue = info.state == ModelArgumentInfo::HAS_NO_VALUE, |
| 418 | .location = info.locationAndLength, |
| 419 | .dimensions = info.dimensions, |
| 420 | }; |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
David Gross | b260491 | 2017-10-01 15:26:33 -0700 | [diff] [blame] | 424 | StepExecutor::StepExecutor(const ExecutionBuilder* executionBuilder, |
| 425 | const ModelBuilder* model, |
| 426 | sp<IDevice> driver, sp<IPreparedModel> preparedModel) : |
| 427 | mExecutionBuilder(executionBuilder), mModel(model), |
David Gross | 891b10f | 2017-10-01 20:48:10 -0700 | [diff] [blame] | 428 | mDriver(driver), mPreparedModel(preparedModel), |
| 429 | mInputs(model->inputCount()), mOutputs(model->outputCount()) {} |
David Gross | b260491 | 2017-10-01 15:26:33 -0700 | [diff] [blame] | 430 | |
| 431 | void StepExecutor::mapInputsAndOutputsTrivially() { |
| 432 | mInputs = mExecutionBuilder->mInputs; |
| 433 | mOutputs = mExecutionBuilder->mOutputs; |
| 434 | mMemories = mExecutionBuilder->mMemories; |
| 435 | } |
| 436 | |
David Gross | 891b10f | 2017-10-01 20:48:10 -0700 | [diff] [blame] | 437 | void StepExecutor::mapInputOrOutput(const ModelArgumentInfo& builderInputOrOutput, |
| 438 | ModelArgumentInfo* executorInputOrOutput) { |
| 439 | *executorInputOrOutput = builderInputOrOutput; |
| 440 | switch (executorInputOrOutput->state) { |
| 441 | default: |
| 442 | nnAssert(!"unexpected ModelArgumentInfo::state"); |
| 443 | case ModelArgumentInfo::POINTER: |
| 444 | case ModelArgumentInfo::UNSPECIFIED: |
| 445 | break; |
| 446 | case ModelArgumentInfo::MEMORY: { |
| 447 | const uint32_t builderPoolIndex = |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 448 | builderInputOrOutput.locationAndLength.poolIndex; |
David Gross | 891b10f | 2017-10-01 20:48:10 -0700 | [diff] [blame] | 449 | const Memory* memory = mExecutionBuilder->mMemories[builderPoolIndex]; |
| 450 | const uint32_t executorPoolIndex = mMemories.add(memory); |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 451 | executorInputOrOutput->locationAndLength.poolIndex = |
David Gross | 891b10f | 2017-10-01 20:48:10 -0700 | [diff] [blame] | 452 | executorPoolIndex; |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
David Gross | 96811e2 | 2017-10-02 14:40:09 -0700 | [diff] [blame] | 458 | int StepExecutor::setInputOrOutputFromTemporaryMemory(const Operand& inputOrOutputOperand, |
David Gross | 8fb14e9 | 2017-10-04 15:16:02 -0700 | [diff] [blame] | 459 | const Memory* memory, uint32_t offset, |
David Gross | 96811e2 | 2017-10-02 14:40:09 -0700 | [diff] [blame] | 460 | ModelArgumentInfo* inputOrOutputInfo) { |
| 461 | // Should be similar to |
| 462 | // ExecutionBuilder::setInputFromMemory() |
| 463 | // ExecutionBuilder::setOutputFromMemory() |
| 464 | |
| 465 | uint32_t poolIndex = mMemories.add(memory); |
David Gross | 8fb14e9 | 2017-10-04 15:16:02 -0700 | [diff] [blame] | 466 | return inputOrOutputInfo->setFromTemporaryMemory(inputOrOutputOperand, poolIndex, offset); |
David Gross | 96811e2 | 2017-10-02 14:40:09 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 469 | int StepExecutor::startCompute(sp<ExecutionCallback>* synchronizationCallback) { |
David Gross | b260491 | 2017-10-01 15:26:33 -0700 | [diff] [blame] | 470 | if (mDriver == nullptr) { |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 471 | return startComputeOnCpu(synchronizationCallback); |
David Gross | b260491 | 2017-10-01 15:26:33 -0700 | [diff] [blame] | 472 | } else { |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 473 | return startComputeOnDevice(synchronizationCallback); |
David Gross | b260491 | 2017-10-01 15:26:33 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 477 | int StepExecutor::startComputeOnDevice(sp<ExecutionCallback>* synchronizationCallback) { |
David Gross | b260491 | 2017-10-01 15:26:33 -0700 | [diff] [blame] | 478 | nnAssert(mDriver != nullptr); |
| 479 | |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 480 | *synchronizationCallback = nullptr; |
David Gross | 425b259 | 2017-09-13 19:33:14 -0700 | [diff] [blame] | 481 | |
David Gross | b260491 | 2017-10-01 15:26:33 -0700 | [diff] [blame] | 482 | // TODO: Remove the mPreparedModel == nullptr case once we've fully integrated |
David Gross | 1f43815 | 2017-09-28 09:18:51 -0700 | [diff] [blame] | 483 | // ExecutionPlan with the compilation and execution phases of the NN API |
David Gross | b260491 | 2017-10-01 15:26:33 -0700 | [diff] [blame] | 484 | if (mPreparedModel == nullptr) { |
David Gross | 1f43815 | 2017-09-28 09:18:51 -0700 | [diff] [blame] | 485 | Model model; |
| 486 | mModel->setHidlModel(&model); |
Michael Butler | 5f916fc | 2017-09-11 21:10:36 -0700 | [diff] [blame] | 487 | |
David Gross | 1f43815 | 2017-09-28 09:18:51 -0700 | [diff] [blame] | 488 | // TODO Dangerous! In async, the model will outlive it here. Safe for now |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 489 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 490 | Return<ErrorStatus> prepareLaunchStatus = |
| 491 | mDriver->prepareModel(model, preparedModelCallback); |
| 492 | if (!prepareLaunchStatus.isOk() || prepareLaunchStatus != ErrorStatus::NONE) { |
| 493 | return ANEURALNETWORKS_OP_FAILED; |
| 494 | } |
Michael Butler | 5f916fc | 2017-09-11 21:10:36 -0700 | [diff] [blame] | 495 | |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 496 | // Immediately synchronize with callback object for now |
David Gross | 1f43815 | 2017-09-28 09:18:51 -0700 | [diff] [blame] | 497 | // TODO: change to asynchronous later |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 498 | preparedModelCallback->wait(); |
| 499 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
| 500 | mPreparedModel = preparedModelCallback->getPreparedModel(); |
| 501 | if (prepareReturnStatus != ErrorStatus::NONE || mPreparedModel == nullptr) { |
David Gross | 1f43815 | 2017-09-28 09:18:51 -0700 | [diff] [blame] | 502 | return ANEURALNETWORKS_OP_FAILED; |
| 503 | } |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 504 | } |
| 505 | |
David Gross | e413eef | 2017-09-29 11:43:32 -0700 | [diff] [blame] | 506 | // We separate the input & output pools so that we reduce the copying done if we |
| 507 | // do an eventual remoting (hidl_memory->update()). We could also use it to set |
| 508 | // protection on read only memory but that's not currently done. |
| 509 | Memory inputPointerArguments; |
| 510 | Memory outputPointerArguments; |
| 511 | |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 512 | // Layout the input and output data |
David Gross | e413eef | 2017-09-29 11:43:32 -0700 | [diff] [blame] | 513 | int n = allocatePointerArgumentsToPool(&mInputs, &inputPointerArguments); |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 514 | if (n != ANEURALNETWORKS_NO_ERROR) { |
| 515 | return n; |
| 516 | } |
David Gross | e413eef | 2017-09-29 11:43:32 -0700 | [diff] [blame] | 517 | n = allocatePointerArgumentsToPool(&mOutputs, &outputPointerArguments); |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 518 | if (n != ANEURALNETWORKS_NO_ERROR) { |
| 519 | return n; |
| 520 | } |
| 521 | |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 522 | // Copy the input data that was specified via a pointer. |
David Gross | e413eef | 2017-09-29 11:43:32 -0700 | [diff] [blame] | 523 | // inputPointerArguments.update(); |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 524 | for (auto& info : mInputs) { |
| 525 | if (info.state == ModelArgumentInfo::POINTER) { |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 526 | DataLocation& loc = info.locationAndLength; |
Jean-Luc Brouillet | 2150f1d | 2017-09-01 13:29:08 -0700 | [diff] [blame] | 527 | uint8_t* data = nullptr; |
David Gross | e413eef | 2017-09-29 11:43:32 -0700 | [diff] [blame] | 528 | int n = inputPointerArguments.getPointer(&data); |
Jean-Luc Brouillet | 2150f1d | 2017-09-01 13:29:08 -0700 | [diff] [blame] | 529 | if (n != ANEURALNETWORKS_NO_ERROR) { |
| 530 | return n; |
| 531 | } |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 532 | memcpy(data + loc.offset, info.buffer, loc.length); |
| 533 | } |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 534 | } |
David Gross | e413eef | 2017-09-29 11:43:32 -0700 | [diff] [blame] | 535 | // TODO: Add inputPointerArguments.commit() and .update() at all the right places |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 536 | |
| 537 | Request request; |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 538 | setRequestArgumentArray(mInputs, &request.inputs); |
| 539 | setRequestArgumentArray(mOutputs, &request.outputs); |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 540 | uint32_t count = mMemories.size(); |
| 541 | request.pools.resize(count); |
| 542 | for (uint32_t i = 0; i < count; i++) { |
| 543 | request.pools[i] = mMemories[i]->getHidlMemory(); |
| 544 | } |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 545 | |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 546 | // Prepare the callback for asynchronous execution. sp<ExecutionCallback> |
| 547 | // object is returned when the execution has been successfully launched, |
| 548 | // otherwise a nullptr is returned. The executionCallback is abstracted in |
| 549 | // the NN API as an "event". |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 550 | // |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 551 | // The sp is used for ref-counting purposes. Without it, the HIDL service |
| 552 | // could attempt to communicate with a dead callback object. |
| 553 | // |
| 554 | // TODO: Explain the "dead callback" problem further, either here or |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 555 | // in the design document. |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 556 | sp<ExecutionCallback> executionCallback = new ExecutionCallback(); |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 557 | |
Miao Wang | 820215d | 2017-10-04 19:45:45 -0700 | [diff] [blame] | 558 | VLOG(EXECUTION) << "Before mPreparedModel->execute() " << toString(request); |
David Gross | 3ced3cf | 2017-09-13 10:45:21 -0700 | [diff] [blame] | 559 | // Execute. |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 560 | // TODO: What happens to the Callback if the service dies abnormally |
| 561 | // -- won't that keep the Callback live forever, because the service |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 562 | // never has the opportunity to bump the reference count down? Or |
| 563 | // maybe the HIDL infrastructure handles this magically? At worst, |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 564 | // it seems like this is a small memory leak, if the Callback stays |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 565 | // alive forever. |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 566 | if (mPreparedModel->execute(request, executionCallback) != ErrorStatus::NONE) { |
Miao Wang | 820215d | 2017-10-04 19:45:45 -0700 | [diff] [blame] | 567 | VLOG(EXECUTION) << "**Execute failed**"; |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 568 | return ANEURALNETWORKS_OP_FAILED; |
| 569 | } |
| 570 | |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 571 | // TODO: Remove this synchronization point when the block of code below is |
| 572 | // removed. |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 573 | executionCallback->wait(); |
| 574 | Return<ErrorStatus> executionStatus = executionCallback->getStatus(); |
| 575 | if (!executionStatus.isOk() || executionStatus != ErrorStatus::NONE) { |
Miao Wang | 820215d | 2017-10-04 19:45:45 -0700 | [diff] [blame] | 576 | VLOG(EXECUTION) << "**Execute async failed**"; |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 577 | return ANEURALNETWORKS_OP_FAILED; |
| 578 | } |
| 579 | |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 580 | // Copy the output data from shared memory to the output buffers. |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 581 | // TODO: Move this block of code somewhere else. It should not be in the |
| 582 | // startCompute function. |
| 583 | // TODO: outputMemory->update(); outputMemory->commit() |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 584 | for (auto& info : mOutputs) { |
| 585 | if (info.state == ModelArgumentInfo::POINTER) { |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 586 | DataLocation& loc = info.locationAndLength; |
Jean-Luc Brouillet | 2150f1d | 2017-09-01 13:29:08 -0700 | [diff] [blame] | 587 | uint8_t* data = nullptr; |
David Gross | e413eef | 2017-09-29 11:43:32 -0700 | [diff] [blame] | 588 | int n = outputPointerArguments.getPointer(&data); |
Jean-Luc Brouillet | 2150f1d | 2017-09-01 13:29:08 -0700 | [diff] [blame] | 589 | if (n != ANEURALNETWORKS_NO_ERROR) { |
| 590 | return n; |
| 591 | } |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 592 | memcpy(info.buffer, data + loc.offset, loc.length); |
| 593 | } |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 594 | } |
Miao Wang | 820215d | 2017-10-04 19:45:45 -0700 | [diff] [blame] | 595 | VLOG(EXECUTION) << "StepExecutor::startComputeOnDevice completed"; |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 596 | |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 597 | *synchronizationCallback = executionCallback; |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 598 | return ANEURALNETWORKS_NO_ERROR; |
| 599 | } |
| 600 | |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 601 | static void asyncStartComputeOnCpu(const Model& model, const Request& request, |
Jean-Luc Brouillet | 1da8fed | 2017-10-11 22:34:04 -0700 | [diff] [blame] | 602 | const std::vector<RunTimePoolInfo>& modelPoolInfos, |
| 603 | const std::vector<RunTimePoolInfo>& requestPoolInfos, |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 604 | const sp<IExecutionCallback>& executionCallback) { |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 605 | CpuExecutor executor; |
Jean-Luc Brouillet | 1da8fed | 2017-10-11 22:34:04 -0700 | [diff] [blame] | 606 | int err = executor.run(model, request, modelPoolInfos, requestPoolInfos); |
Michael Butler | 5f916fc | 2017-09-11 21:10:36 -0700 | [diff] [blame] | 607 | ErrorStatus status = err == ANEURALNETWORKS_NO_ERROR ? |
| 608 | ErrorStatus::NONE : ErrorStatus::GENERAL_FAILURE; |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 609 | executionCallback->notify(status); |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 612 | int StepExecutor::startComputeOnCpu(sp<ExecutionCallback>* synchronizationCallback) { |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 613 | // TODO: use a thread pool |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 614 | |
David Gross | 1f43815 | 2017-09-28 09:18:51 -0700 | [diff] [blame] | 615 | Model model; |
| 616 | mModel->setHidlModel(&model); |
| 617 | |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 618 | // Prepare the callback for asynchronous execution. sp<ExecutionCallback> |
| 619 | // object is returned when the execution has been successfully launched, |
| 620 | // otherwise a nullptr is returned. The executionCallback is abstracted in |
| 621 | // the NN API as an "event". |
| 622 | sp<ExecutionCallback> executionCallback = new ExecutionCallback(); |
| 623 | *synchronizationCallback = nullptr; |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 624 | |
Jean-Luc Brouillet | 1da8fed | 2017-10-11 22:34:04 -0700 | [diff] [blame] | 625 | std::vector<RunTimePoolInfo> modelPoolInfos; |
| 626 | if (!setRunTimePoolInfosFromHidlMemories(&modelPoolInfos, model.pools)) { |
| 627 | return ANEURALNETWORKS_UNMAPPABLE; |
| 628 | } |
| 629 | |
| 630 | std::vector<RunTimePoolInfo> requestPoolInfos; |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 631 | uint32_t count = mMemories.size(); |
Jean-Luc Brouillet | 1da8fed | 2017-10-11 22:34:04 -0700 | [diff] [blame] | 632 | requestPoolInfos.resize(count); |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 633 | for (uint32_t i = 0; i < count; i++) { |
| 634 | const Memory* mem = mMemories[i]; |
Jean-Luc Brouillet | 1da8fed | 2017-10-11 22:34:04 -0700 | [diff] [blame] | 635 | if (!requestPoolInfos[i].set(mem->getHidlMemory())) { |
| 636 | return ANEURALNETWORKS_UNMAPPABLE; |
| 637 | } |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 638 | } |
| 639 | // Create as many pools as there are input / output. |
Jean-Luc Brouillet | 1da8fed | 2017-10-11 22:34:04 -0700 | [diff] [blame] | 640 | auto fixPointerArguments = [&requestPoolInfos](std::vector<ModelArgumentInfo>& argumentInfos) { |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 641 | for (ModelArgumentInfo& argumentInfo : argumentInfos) { |
| 642 | if (argumentInfo.state == ModelArgumentInfo::POINTER) { |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 643 | RunTimePoolInfo runTimeInfo = { |
| 644 | .buffer = static_cast<uint8_t*>(argumentInfo.buffer)}; |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 645 | argumentInfo.locationAndLength.poolIndex = |
Jean-Luc Brouillet | 1da8fed | 2017-10-11 22:34:04 -0700 | [diff] [blame] | 646 | static_cast<uint32_t>(requestPoolInfos.size()); |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 647 | argumentInfo.locationAndLength.offset = 0; |
Jean-Luc Brouillet | 1da8fed | 2017-10-11 22:34:04 -0700 | [diff] [blame] | 648 | requestPoolInfos.push_back(runTimeInfo); |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | }; |
| 652 | fixPointerArguments(mInputs); |
| 653 | fixPointerArguments(mOutputs); |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 654 | |
Jean-Luc Brouillet | 8b99bb1 | 2017-08-20 18:16:36 -0700 | [diff] [blame] | 655 | Request request; |
Jean-Luc Brouillet | 62cc275 | 2017-09-27 19:18:51 -0700 | [diff] [blame] | 656 | setRequestArgumentArray(mInputs, &request.inputs); |
| 657 | setRequestArgumentArray(mOutputs, &request.outputs); |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 658 | |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 659 | // TODO: should model be moved with a std::cref? |
| 660 | std::thread thread(asyncStartComputeOnCpu, model, std::move(request), |
Jean-Luc Brouillet | 1da8fed | 2017-10-11 22:34:04 -0700 | [diff] [blame] | 661 | std::move(modelPoolInfos), std::move(requestPoolInfos), |
| 662 | executionCallback); |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 663 | executionCallback->bind_thread(std::move(thread)); |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 664 | |
Michael Butler | 033b8a6 | 2017-09-22 18:21:59 -0700 | [diff] [blame] | 665 | *synchronizationCallback = executionCallback; |
Michael Butler | 689d892 | 2017-09-01 10:58:46 -0700 | [diff] [blame] | 666 | return ANEURALNETWORKS_NO_ERROR; |
Jean-Luc Brouillet | 707dbd2 | 2017-07-25 00:17:50 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Jean-Luc Brouillet | 389f26c | 2017-09-02 23:05:37 -0700 | [diff] [blame] | 669 | } // namespace nn |
| 670 | } // namespace android |