Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 2 | * Copyright (c) 2020-2022 Arm Limited. |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | * not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include "message_handler.hpp" |
| 20 | |
| 21 | #include "cmsis_compiler.h" |
| 22 | |
| 23 | #ifdef ETHOSU |
| 24 | #include <ethosu_driver.h> |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 25 | #include <pmu_ethosu.h> |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | #include "FreeRTOS.h" |
| 29 | #include "queue.h" |
| 30 | #include "semphr.h" |
| 31 | |
| 32 | #include <cstring> |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 33 | #include <vector> |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 34 | |
| 35 | using namespace EthosU; |
| 36 | using namespace MessageQueue; |
| 37 | |
| 38 | namespace MessageHandler { |
| 39 | |
| 40 | /**************************************************************************** |
| 41 | * IncomingMessageHandler |
| 42 | ****************************************************************************/ |
| 43 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 44 | IncomingMessageHandler::IncomingMessageHandler( |
| 45 | EthosU::ethosu_core_queue &_inputMessageQueue, |
| 46 | EthosU::ethosu_core_queue &_outputMessageQueue, |
| 47 | Mailbox::Mailbox &_mailbox, |
| 48 | std::shared_ptr<Queue<EthosU::ethosu_core_inference_req>> _inferenceInputQueue, |
| 49 | QueueHandle_t _inferenceOutputQueue, |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 50 | SemaphoreHandle_t _messageNotify, |
| 51 | std::shared_ptr<Networks> _networks) : |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 52 | inputMessageQueue(_inputMessageQueue), |
| 53 | outputMessageQueue(_outputMessageQueue), mailbox(_mailbox), inferenceInputQueue(_inferenceInputQueue), |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 54 | inferenceOutputQueue(_inferenceOutputQueue), messageNotify(_messageNotify), networks(_networks) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 55 | mailbox.registerCallback(handleIrq, reinterpret_cast<void *>(this)); |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 56 | readCapabilties(capabilities); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void IncomingMessageHandler::run() { |
| 60 | while (true) { |
| 61 | // Wait for event |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 62 | xSemaphoreTake(messageNotify, portMAX_DELAY); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 63 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 64 | // Handle all inference outputs and all messages in queue |
| 65 | while (handleInferenceOutput() || handleMessage()) {} |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | void IncomingMessageHandler::handleIrq(void *userArg) { |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 70 | if (userArg == nullptr) { |
| 71 | return; |
| 72 | } |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 73 | IncomingMessageHandler *_this = reinterpret_cast<IncomingMessageHandler *>(userArg); |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 74 | xSemaphoreGiveFromISR(_this->messageNotify, nullptr); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 77 | void IncomingMessageHandler::sendErrorAndResetQueue(ethosu_core_msg_err_type type, const char *message) { |
| 78 | ethosu_core_msg_err error; |
| 79 | error.type = type; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 80 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 81 | for (size_t i = 0; i < sizeof(error.msg) && message[i]; i++) { |
| 82 | error.msg[i] = message[i]; |
| 83 | } |
| 84 | printf("ERROR: Msg: \"%s\"\n", error.msg); |
| 85 | |
| 86 | if (!outputMessageQueue.write(ETHOSU_CORE_MSG_ERR, error)) { |
| 87 | printf("ERROR: Msg: Failed to write error response. No mailbox message sent\n"); |
| 88 | } else { |
| 89 | mailbox.sendMessage(); |
| 90 | } |
| 91 | inputMessageQueue.reset(); |
| 92 | } |
| 93 | |
| 94 | bool IncomingMessageHandler::handleInferenceOutput() { |
| 95 | struct ethosu_core_inference_rsp rsp; |
| 96 | if (pdTRUE != xQueueReceive(inferenceOutputQueue, &rsp, 0)) { |
| 97 | return false; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 100 | sendInferenceRsp(rsp); |
| 101 | return true; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | bool IncomingMessageHandler::handleMessage() { |
| 105 | struct ethosu_core_msg msg; |
| 106 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 107 | if (inputMessageQueue.available() == 0) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 108 | return false; |
| 109 | } |
| 110 | |
| 111 | // Read msg header |
| 112 | // Only process a complete message header, else send error message |
| 113 | // and reset queue |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 114 | if (!inputMessageQueue.read(msg)) { |
| 115 | sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_SIZE, "Failed to read a complete header"); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | |
| 119 | printf("Msg: header magic=%" PRIX32 ", type=%" PRIu32 ", length=%" PRIu32 "\n", msg.magic, msg.type, msg.length); |
| 120 | |
| 121 | if (msg.magic != ETHOSU_CORE_MSG_MAGIC) { |
| 122 | printf("Msg: Invalid Magic\n"); |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 123 | sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_MAGIC, "Invalid magic"); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
| 127 | switch (msg.type) { |
| 128 | case ETHOSU_CORE_MSG_PING: { |
| 129 | printf("Msg: Ping\n"); |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 130 | sendPong(); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 131 | break; |
| 132 | } |
| 133 | case ETHOSU_CORE_MSG_ERR: { |
| 134 | ethosu_core_msg_err error; |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 135 | if (!inputMessageQueue.read(error)) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 136 | printf("ERROR: Msg: Failed to receive error message\n"); |
| 137 | } else { |
| 138 | printf("Msg: Received an error response, type=%" PRIu32 ", msg=\"%s\"\n", error.type, error.msg); |
| 139 | } |
| 140 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 141 | inputMessageQueue.reset(); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 142 | return false; |
| 143 | } |
| 144 | case ETHOSU_CORE_MSG_VERSION_REQ: { |
| 145 | printf("Msg: Version request\n"); |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 146 | sendVersionRsp(); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 147 | break; |
| 148 | } |
| 149 | case ETHOSU_CORE_MSG_CAPABILITIES_REQ: { |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 150 | ethosu_core_capabilities_req req; |
| 151 | if (!inputMessageQueue.read(req)) { |
| 152 | sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "CapabilitiesReq. Failed to read payload"); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 153 | break; |
| 154 | } |
| 155 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 156 | printf("Msg: Capabilities request.user_arg=0x%" PRIx64 "\n", req.user_arg); |
| 157 | sendCapabilitiesRsp(req.user_arg); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 158 | break; |
| 159 | } |
| 160 | case ETHOSU_CORE_MSG_INFERENCE_REQ: { |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 161 | ethosu_core_inference_req req; |
| 162 | if (!inputMessageQueue.read(req)) { |
| 163 | sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "InferenceReq. Failed to read payload"); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 167 | printf("Msg: InferenceReq. user_arg=0x%" PRIx64 ", network_type=%" PRIu32 ", ", req.user_arg, req.network.type); |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 168 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 169 | if (req.network.type == ETHOSU_CORE_NETWORK_BUFFER) { |
| 170 | printf("network.buffer={0x%" PRIx32 ", %" PRIu32 "},\n", req.network.buffer.ptr, req.network.buffer.size); |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 171 | } else { |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 172 | printf("network.index=%" PRIu32 ",\n", req.network.index); |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 173 | } |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 174 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 175 | printf("ifm_count=%" PRIu32 ", ifm=[", req.ifm_count); |
| 176 | for (uint32_t i = 0; i < req.ifm_count; ++i) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 177 | if (i > 0) { |
| 178 | printf(", "); |
| 179 | } |
| 180 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 181 | printf("{0x%" PRIx32 ", %" PRIu32 "}", req.ifm[i].ptr, req.ifm[i].size); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 182 | } |
| 183 | printf("]"); |
| 184 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 185 | printf(", ofm_count=%" PRIu32 ", ofm=[", req.ofm_count); |
| 186 | for (uint32_t i = 0; i < req.ofm_count; ++i) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 187 | if (i > 0) { |
| 188 | printf(", "); |
| 189 | } |
| 190 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 191 | printf("{0x%" PRIx32 ", %" PRIu32 "}", req.ofm[i].ptr, req.ofm[i].size); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 192 | } |
| 193 | printf("]\n"); |
| 194 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 195 | if (!inferenceInputQueue->push(req)) { |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 196 | printf("Msg: Inference queue full. Rejecting inference user_arg=0x%" PRIx64 "\n", req.user_arg); |
| 197 | sendFailedInferenceRsp(req.user_arg, ETHOSU_CORE_STATUS_REJECTED); |
| 198 | } |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 199 | break; |
| 200 | } |
Davide Grohmann | 00de9ee | 2022-03-23 14:59:56 +0100 | [diff] [blame] | 201 | case ETHOSU_CORE_MSG_CANCEL_INFERENCE_REQ: { |
| 202 | ethosu_core_cancel_inference_req req; |
| 203 | if (!inputMessageQueue.read(req)) { |
| 204 | sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "CancelInferenceReq. Failed to read payload"); |
| 205 | break; |
| 206 | } |
| 207 | printf("Msg: CancelInferenceReq. user_arg=0x%" PRIx64 ", inference_handle=0x%" PRIx64 "\n", |
| 208 | req.user_arg, |
| 209 | req.inference_handle); |
| 210 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 211 | bool found = |
| 212 | inferenceInputQueue->erase([req](auto &inf_req) { return inf_req.user_arg == req.inference_handle; }); |
| 213 | |
| 214 | // NOTE: send an inference response with status ABORTED if the inference has been droped from the queue |
| 215 | if (found) { |
| 216 | sendFailedInferenceRsp(req.inference_handle, ETHOSU_CORE_STATUS_ABORTED); |
| 217 | } |
| 218 | |
| 219 | sendCancelInferenceRsp(req.user_arg, found ? ETHOSU_CORE_STATUS_OK : ETHOSU_CORE_STATUS_ERROR); |
Davide Grohmann | 00de9ee | 2022-03-23 14:59:56 +0100 | [diff] [blame] | 220 | break; |
| 221 | } |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 222 | case ETHOSU_CORE_MSG_NETWORK_INFO_REQ: { |
| 223 | ethosu_core_network_info_req req; |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 224 | if (!inputMessageQueue.read(req)) { |
| 225 | sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "NetworkInfoReq. Failed to read payload"); |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 226 | break; |
| 227 | } |
| 228 | |
| 229 | printf("Msg: NetworkInfoReq. user_arg=0x%" PRIx64 "\n", req.user_arg); |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 230 | sendNetworkInfoRsp(req.user_arg, req.network); |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 231 | break; |
| 232 | } |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 233 | default: { |
| 234 | char errMsg[128]; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 235 | snprintf(&errMsg[0], |
| 236 | sizeof(errMsg), |
| 237 | "Msg: Unknown type: %" PRIu32 " with payload length %" PRIu32 " bytes\n", |
| 238 | msg.type, |
| 239 | msg.length); |
| 240 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 241 | sendErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_UNSUPPORTED_TYPE, errMsg); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return true; |
| 247 | } |
| 248 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 249 | void IncomingMessageHandler::sendPong() { |
| 250 | if (!outputMessageQueue.write(ETHOSU_CORE_MSG_PONG)) { |
| 251 | printf("ERROR: Msg: Failed to write pong response. No mailbox message sent\n"); |
| 252 | } else { |
| 253 | mailbox.sendMessage(); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void IncomingMessageHandler::sendVersionRsp() { |
| 258 | ethosu_core_msg_version version = { |
| 259 | ETHOSU_CORE_MSG_VERSION_MAJOR, |
| 260 | ETHOSU_CORE_MSG_VERSION_MINOR, |
| 261 | ETHOSU_CORE_MSG_VERSION_PATCH, |
| 262 | 0, |
| 263 | }; |
| 264 | |
| 265 | if (!outputMessageQueue.write(ETHOSU_CORE_MSG_VERSION_RSP, version)) { |
| 266 | printf("ERROR: Failed to write version response. No mailbox message sent\n"); |
| 267 | } else { |
| 268 | mailbox.sendMessage(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void IncomingMessageHandler::sendCapabilitiesRsp(uint64_t userArg) { |
| 273 | capabilities.user_arg = userArg; |
| 274 | |
| 275 | if (!outputMessageQueue.write(ETHOSU_CORE_MSG_CAPABILITIES_RSP, capabilities)) { |
| 276 | printf("ERROR: Failed to write capabilities response. No mailbox message sent\n"); |
| 277 | } else { |
| 278 | mailbox.sendMessage(); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | void IncomingMessageHandler::sendNetworkInfoRsp(uint64_t userArg, ethosu_core_network_buffer &network) { |
| 283 | ethosu_core_network_info_rsp rsp; |
| 284 | rsp.user_arg = userArg; |
| 285 | rsp.ifm_count = 0; |
| 286 | rsp.ofm_count = 0; |
| 287 | |
| 288 | void *buffer; |
| 289 | size_t size; |
Per Ã…strand | 4199935 | 2022-05-18 09:09:01 +0200 | [diff] [blame] | 290 | |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 291 | bool failed = networks->getNetwork(network, buffer, size); |
Per Ã…strand | 4199935 | 2022-05-18 09:09:01 +0200 | [diff] [blame] | 292 | |
| 293 | if (!failed) { |
| 294 | failed = parser.parseModel(buffer, |
Davide Grohmann | f4379e9 | 2022-06-15 11:20:41 +0200 | [diff] [blame] | 295 | size, |
Per Ã…strand | 4199935 | 2022-05-18 09:09:01 +0200 | [diff] [blame] | 296 | rsp.desc, |
| 297 | InferenceProcess::makeArray(rsp.ifm_size, rsp.ifm_count, ETHOSU_CORE_BUFFER_MAX), |
| 298 | InferenceProcess::makeArray(rsp.ofm_size, rsp.ofm_count, ETHOSU_CORE_BUFFER_MAX)); |
| 299 | } |
| 300 | rsp.status = failed ? ETHOSU_CORE_STATUS_ERROR : ETHOSU_CORE_STATUS_OK; |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 301 | |
| 302 | if (!outputMessageQueue.write(ETHOSU_CORE_MSG_NETWORK_INFO_RSP, rsp)) { |
| 303 | printf("ERROR: Msg: Failed to write network info response. No mailbox message sent\n"); |
| 304 | } else { |
| 305 | mailbox.sendMessage(); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void IncomingMessageHandler::sendInferenceRsp(ethosu_core_inference_rsp &rsp) { |
| 310 | if (!outputMessageQueue.write(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp)) { |
| 311 | printf("ERROR: Msg: Failed to write inference response. No mailbox message sent\n"); |
| 312 | } else { |
| 313 | mailbox.sendMessage(); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void IncomingMessageHandler::sendFailedInferenceRsp(uint64_t userArg, uint32_t status) { |
| 318 | ethosu_core_inference_rsp rsp; |
| 319 | rsp.user_arg = userArg; |
| 320 | rsp.status = status; |
| 321 | if (!outputMessageQueue.write(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp)) { |
| 322 | printf("ERROR: Msg: Failed to write inference response. No mailbox message sent\n"); |
| 323 | } else { |
| 324 | mailbox.sendMessage(); |
| 325 | } |
| 326 | } |
Davide Grohmann | 00de9ee | 2022-03-23 14:59:56 +0100 | [diff] [blame] | 327 | void IncomingMessageHandler::sendCancelInferenceRsp(uint64_t userArg, uint32_t status) { |
| 328 | ethosu_core_cancel_inference_rsp cancellation; |
| 329 | cancellation.user_arg = userArg; |
| 330 | cancellation.status = status; |
| 331 | if (!outputMessageQueue.write(ETHOSU_CORE_MSG_CANCEL_INFERENCE_RSP, cancellation)) { |
| 332 | printf("ERROR: Msg: Failed to write cancel inference response. No mailbox message sent\n"); |
| 333 | } else { |
| 334 | mailbox.sendMessage(); |
| 335 | } |
| 336 | } |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 337 | |
| 338 | void IncomingMessageHandler::readCapabilties(ethosu_core_msg_capabilities_rsp &rsp) { |
| 339 | rsp = {}; |
| 340 | |
| 341 | #ifdef ETHOSU |
| 342 | struct ethosu_driver_version version; |
| 343 | ethosu_get_driver_version(&version); |
| 344 | |
| 345 | struct ethosu_hw_info info; |
| 346 | struct ethosu_driver *drv = ethosu_reserve_driver(); |
| 347 | ethosu_get_hw_info(drv, &info); |
| 348 | ethosu_release_driver(drv); |
| 349 | |
| 350 | rsp.user_arg = 0; |
| 351 | rsp.version_status = info.version.version_status; |
| 352 | rsp.version_minor = info.version.version_minor; |
| 353 | rsp.version_major = info.version.version_major; |
| 354 | rsp.product_major = info.version.product_major; |
| 355 | rsp.arch_patch_rev = info.version.arch_patch_rev; |
| 356 | rsp.arch_minor_rev = info.version.arch_minor_rev; |
| 357 | rsp.arch_major_rev = info.version.arch_major_rev; |
| 358 | rsp.driver_patch_rev = version.patch; |
| 359 | rsp.driver_minor_rev = version.minor; |
| 360 | rsp.driver_major_rev = version.major; |
| 361 | rsp.macs_per_cc = info.cfg.macs_per_cc; |
| 362 | rsp.cmd_stream_version = info.cfg.cmd_stream_version; |
| 363 | rsp.custom_dma = info.cfg.custom_dma; |
| 364 | #endif |
| 365 | } |
| 366 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 367 | /**************************************************************************** |
| 368 | * InferenceHandler |
| 369 | ****************************************************************************/ |
| 370 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 371 | InferenceHandler::InferenceHandler(uint8_t *tensorArena, |
| 372 | size_t arenaSize, |
| 373 | std::shared_ptr<Queue<EthosU::ethosu_core_inference_req>> _inferenceInputQueue, |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 374 | QueueHandle_t _inferenceOutputQueue, |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 375 | SemaphoreHandle_t _messageNotify, |
| 376 | std::shared_ptr<Networks> _networks) : |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 377 | inferenceInputQueue(_inferenceInputQueue), |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 378 | inferenceOutputQueue(_inferenceOutputQueue), messageNotify(_messageNotify), inference(tensorArena, arenaSize), |
| 379 | networks(_networks) {} |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 380 | |
| 381 | void InferenceHandler::run() { |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 382 | ethosu_core_inference_req req; |
| 383 | ethosu_core_inference_rsp rsp; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 384 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 385 | while (true) { |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 386 | inferenceInputQueue->pop(req); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 387 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 388 | runInference(req, rsp); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 389 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 390 | xQueueSend(inferenceOutputQueue, &rsp, portMAX_DELAY); |
| 391 | xSemaphoreGive(messageNotify); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | void InferenceHandler::runInference(ethosu_core_inference_req &req, ethosu_core_inference_rsp &rsp) { |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 396 | currentReq = &req; |
| 397 | currentRsp = &rsp; |
| 398 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 399 | /* |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 400 | * Run inference |
| 401 | */ |
| 402 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 403 | InferenceProcess::InferenceJob job; |
| 404 | bool failed = getInferenceJob(req, job); |
| 405 | |
| 406 | if (!failed) { |
| 407 | job.invalidate(); |
| 408 | failed = inference.runJob(job); |
| 409 | job.clean(); |
| 410 | } |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 411 | |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 412 | #if defined(ETHOSU) |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 413 | /* |
Kristofer Jonsson | 47f556a | 2022-02-02 12:11:22 +0100 | [diff] [blame] | 414 | * Print PMU counters |
| 415 | */ |
| 416 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 417 | if (!failed) { |
| 418 | const int numEvents = std::min(static_cast<int>(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); |
Kristofer Jonsson | 47f556a | 2022-02-02 12:11:22 +0100 | [diff] [blame] | 419 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 420 | for (int i = 0; i < numEvents; i++) { |
| 421 | printf("ethosu_pmu_cntr%d : %" PRIu32 "\n", i, rsp.pmu_event_count[i]); |
| 422 | } |
Kristofer Jonsson | 47f556a | 2022-02-02 12:11:22 +0100 | [diff] [blame] | 423 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 424 | if (rsp.pmu_cycle_counter_enable) { |
| 425 | printf("ethosu_pmu_cycle_cntr : %" PRIu64 " cycles\n", rsp.pmu_cycle_counter_count); |
| 426 | } |
Kristofer Jonsson | 47f556a | 2022-02-02 12:11:22 +0100 | [diff] [blame] | 427 | } |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 428 | #endif |
Kristofer Jonsson | 47f556a | 2022-02-02 12:11:22 +0100 | [diff] [blame] | 429 | |
| 430 | /* |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 431 | * Send inference response |
| 432 | */ |
| 433 | |
| 434 | rsp.user_arg = req.user_arg; |
| 435 | rsp.ofm_count = job.output.size(); |
| 436 | rsp.status = failed ? ETHOSU_CORE_STATUS_ERROR : ETHOSU_CORE_STATUS_OK; |
| 437 | |
| 438 | for (size_t i = 0; i < job.output.size(); ++i) { |
| 439 | rsp.ofm_size[i] = job.output[i].size; |
| 440 | } |
| 441 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 442 | currentReq = nullptr; |
| 443 | currentRsp = nullptr; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 446 | bool InferenceHandler::getInferenceJob(const ethosu_core_inference_req &req, InferenceProcess::InferenceJob &job) { |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 447 | bool failed = networks->getNetwork(req.network, job.networkModel.data, job.networkModel.size); |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 448 | if (failed) { |
| 449 | return true; |
| 450 | } |
| 451 | |
| 452 | for (uint32_t i = 0; i < req.ifm_count; ++i) { |
| 453 | job.input.push_back(InferenceProcess::DataPtr(reinterpret_cast<void *>(req.ifm[i].ptr), req.ifm[i].size)); |
| 454 | } |
| 455 | |
| 456 | for (uint32_t i = 0; i < req.ofm_count; ++i) { |
| 457 | job.output.push_back(InferenceProcess::DataPtr(reinterpret_cast<void *>(req.ofm[i].ptr), req.ofm[i].size)); |
| 458 | } |
| 459 | |
| 460 | job.externalContext = this; |
| 461 | |
| 462 | return false; |
| 463 | } |
| 464 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 465 | } // namespace MessageHandler |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 466 | |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 467 | #if defined(ETHOSU) |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 468 | extern "C" void ethosu_inference_begin(struct ethosu_driver *drv, void *userArg) { |
| 469 | MessageHandler::InferenceHandler *self = static_cast<MessageHandler::InferenceHandler *>(userArg); |
| 470 | |
| 471 | // Calculate maximum number of events |
| 472 | const int numEvents = std::min(static_cast<int>(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); |
| 473 | |
| 474 | // Enable PMU |
| 475 | ETHOSU_PMU_Enable(drv); |
| 476 | |
| 477 | // Configure and enable events |
| 478 | for (int i = 0; i < numEvents; i++) { |
| 479 | ETHOSU_PMU_Set_EVTYPER(drv, i, static_cast<ethosu_pmu_event_type>(self->currentReq->pmu_event_config[i])); |
| 480 | ETHOSU_PMU_CNTR_Enable(drv, 1 << i); |
| 481 | } |
| 482 | |
| 483 | // Enable cycle counter |
| 484 | if (self->currentReq->pmu_cycle_counter_enable) { |
Kristofer Jonsson | b9d7ded | 2022-02-04 13:10:04 +0100 | [diff] [blame] | 485 | ETHOSU_PMU_PMCCNTR_CFG_Set_Stop_Event(drv, ETHOSU_PMU_NPU_IDLE); |
| 486 | ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event(drv, ETHOSU_PMU_NPU_ACTIVE); |
| 487 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 488 | ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk); |
| 489 | ETHOSU_PMU_CYCCNT_Reset(drv); |
| 490 | } |
| 491 | |
| 492 | // Reset all counters |
| 493 | ETHOSU_PMU_EVCNTR_ALL_Reset(drv); |
| 494 | } |
| 495 | |
| 496 | extern "C" void ethosu_inference_end(struct ethosu_driver *drv, void *userArg) { |
| 497 | MessageHandler::InferenceHandler *self = static_cast<MessageHandler::InferenceHandler *>(userArg); |
| 498 | |
| 499 | // Get cycle counter |
| 500 | self->currentRsp->pmu_cycle_counter_enable = self->currentReq->pmu_cycle_counter_enable; |
| 501 | if (self->currentReq->pmu_cycle_counter_enable) { |
| 502 | self->currentRsp->pmu_cycle_counter_count = ETHOSU_PMU_Get_CCNTR(drv); |
| 503 | } |
| 504 | |
| 505 | // Calculate maximum number of events |
| 506 | const int numEvents = std::min(static_cast<int>(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); |
| 507 | |
| 508 | // Get event counters |
| 509 | int i; |
| 510 | for (i = 0; i < numEvents; i++) { |
| 511 | self->currentRsp->pmu_event_config[i] = self->currentReq->pmu_event_config[i]; |
| 512 | self->currentRsp->pmu_event_count[i] = ETHOSU_PMU_Get_EVCNTR(drv, i); |
| 513 | } |
| 514 | |
| 515 | for (; i < ETHOSU_CORE_PMU_MAX; i++) { |
| 516 | self->currentRsp->pmu_event_config[i] = 0; |
| 517 | self->currentRsp->pmu_event_count[i] = 0; |
| 518 | } |
| 519 | |
| 520 | // Disable PMU |
| 521 | ETHOSU_PMU_Disable(drv); |
| 522 | } |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 523 | #endif |