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" |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 22 | #include "tensorflow/lite/schema/schema_generated.h" |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 23 | |
| 24 | #ifdef ETHOSU |
| 25 | #include <ethosu_driver.h> |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 26 | #include <pmu_ethosu.h> |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 27 | #endif |
| 28 | |
| 29 | #include "FreeRTOS.h" |
| 30 | #include "queue.h" |
| 31 | #include "semphr.h" |
| 32 | |
| 33 | #include <cstring> |
| 34 | #include <inttypes.h> |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 35 | #include <vector> |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 36 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 37 | #define XSTRINGIFY(src) #src |
| 38 | #define STRINGIFY(src) XSTRINGIFY(src) |
| 39 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 40 | using namespace EthosU; |
| 41 | using namespace MessageQueue; |
| 42 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 43 | /**************************************************************************** |
| 44 | * Models |
| 45 | ****************************************************************************/ |
| 46 | |
| 47 | namespace { |
| 48 | #if defined(__has_include) |
| 49 | |
| 50 | #if __has_include(STRINGIFY(MODEL_0)) |
| 51 | namespace Model0 { |
| 52 | #include STRINGIFY(MODEL_0) |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | #if __has_include(STRINGIFY(MODEL_1)) |
| 57 | namespace Model1 { |
| 58 | #include STRINGIFY(MODEL_1) |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | #if __has_include(STRINGIFY(MODEL_2)) |
| 63 | namespace Model2 { |
| 64 | #include STRINGIFY(MODEL_2) |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | #if __has_include(STRINGIFY(MODEL_3)) |
| 69 | namespace Model3 { |
| 70 | #include STRINGIFY(MODEL_3) |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | #endif |
| 75 | } // namespace |
| 76 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 77 | namespace MessageHandler { |
| 78 | |
| 79 | /**************************************************************************** |
| 80 | * IncomingMessageHandler |
| 81 | ****************************************************************************/ |
| 82 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 83 | namespace { |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 84 | |
| 85 | template <typename T, typename U> |
| 86 | class Array { |
| 87 | public: |
| 88 | Array() = delete; |
| 89 | Array(T *const data, U &size, size_t capacity) : _data{data}, _size{size}, _capacity{capacity} {} |
| 90 | |
| 91 | auto size() const { |
| 92 | return _size; |
| 93 | } |
| 94 | |
| 95 | auto capacity() const { |
| 96 | return _capacity; |
| 97 | } |
| 98 | |
| 99 | void push_back(const T &data) { |
| 100 | _data[_size++] = data; |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | T *const _data; |
| 105 | U &_size; |
| 106 | const size_t _capacity{}; |
| 107 | }; |
| 108 | |
| 109 | template <typename T, typename U> |
| 110 | Array<T, U> makeArray(T *const data, U &size, size_t capacity) { |
| 111 | return Array<T, U>{data, size, capacity}; |
| 112 | } |
| 113 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 114 | bool getNetwork(const ethosu_core_buffer &buffer, void *&data, size_t &size) { |
| 115 | data = reinterpret_cast<void *>(buffer.ptr); |
| 116 | size = buffer.size; |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | bool getNetwork(const uint32_t index, void *&data, size_t &size) { |
| 121 | switch (index) { |
| 122 | #if __has_include(STRINGIFY(MODEL_0)) |
| 123 | case 0: |
| 124 | data = reinterpret_cast<void *>(Model0::networkModel); |
| 125 | size = sizeof(Model0::networkModel); |
| 126 | break; |
| 127 | #endif |
| 128 | |
| 129 | #if __has_include(STRINGIFY(MODEL_1)) |
| 130 | case 1: |
| 131 | data = reinterpret_cast<void *>(Model1::networkModel); |
| 132 | size = sizeof(Model1::networkModel); |
| 133 | break; |
| 134 | #endif |
| 135 | |
| 136 | #if __has_include(STRINGIFY(MODEL_2)) |
| 137 | case 2: |
| 138 | data = reinterpret_cast<void *>(Model2::networkModel); |
| 139 | size = sizeof(Model2::networkModel); |
| 140 | break; |
| 141 | #endif |
| 142 | |
| 143 | #if __has_include(STRINGIFY(MODEL_3)) |
| 144 | case 3: |
| 145 | data = reinterpret_cast<void *>(Model3::networkModel); |
| 146 | size = sizeof(Model3::networkModel); |
| 147 | break; |
| 148 | #endif |
| 149 | |
| 150 | default: |
| 151 | printf("Error: Network model index out of range. index=%u\n", index); |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | bool getNetwork(const ethosu_core_network_buffer &buffer, void *&data, size_t &size) { |
| 159 | switch (buffer.type) { |
| 160 | case ETHOSU_CORE_NETWORK_BUFFER: |
| 161 | return getNetwork(buffer.buffer, data, size); |
| 162 | case ETHOSU_CORE_NETWORK_INDEX: |
| 163 | return getNetwork(buffer.index, data, size); |
| 164 | default: |
| 165 | printf("Error: Unsupported network model type. type=%u\n", buffer.type); |
| 166 | return true; |
| 167 | } |
| 168 | } |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 169 | |
| 170 | bool getShapeSize(const flatbuffers::Vector<int32_t> *shape, size_t &size) { |
| 171 | size = 1; |
| 172 | |
| 173 | if (shape == nullptr) { |
| 174 | printf("Warning: nullptr shape size.\n"); |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | if (shape->Length() == 0) { |
| 179 | printf("Warning: shape zero length.\n"); |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | for (auto it = shape->begin(); it != shape->end(); ++it) { |
| 184 | size *= *it; |
| 185 | } |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | bool getTensorTypeSize(const enum tflite::TensorType type, size_t &size) { |
| 191 | switch (type) { |
| 192 | case tflite::TensorType::TensorType_UINT8: |
| 193 | case tflite::TensorType::TensorType_INT8: |
| 194 | size = 1; |
| 195 | break; |
| 196 | case tflite::TensorType::TensorType_INT16: |
| 197 | size = 2; |
| 198 | break; |
| 199 | case tflite::TensorType::TensorType_INT32: |
| 200 | case tflite::TensorType::TensorType_FLOAT32: |
| 201 | size = 4; |
| 202 | break; |
| 203 | default: |
| 204 | printf("Warning: Unsupported tensor type\n"); |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | template <typename T> |
| 212 | bool getSubGraphDims(const tflite::SubGraph *subgraph, const flatbuffers::Vector<int32_t> *tensorMap, T &dims) { |
| 213 | if (subgraph == nullptr || tensorMap == nullptr) { |
| 214 | printf("Warning: nullptr subgraph or tensormap.\n"); |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | if ((dims.capacity() - dims.size()) < tensorMap->size()) { |
| 219 | printf("Warning: tensormap size is larger than dimension capacity.\n"); |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | for (auto index = tensorMap->begin(); index != tensorMap->end(); ++index) { |
| 224 | auto tensor = subgraph->tensors()->Get(*index); |
| 225 | size_t size; |
| 226 | size_t tensorSize; |
| 227 | |
| 228 | bool failed = getShapeSize(tensor->shape(), size); |
| 229 | if (failed) { |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | failed = getTensorTypeSize(tensor->type(), tensorSize); |
| 234 | if (failed) { |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | size *= tensorSize; |
| 239 | |
| 240 | if (size > 0) { |
| 241 | dims.push_back(size); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | template <typename T, typename U, size_t S> |
| 249 | bool parseModel(const ethosu_core_network_buffer &buffer, char (&description)[S], T &&ifmDims, U &&ofmDims) { |
| 250 | void *data; |
| 251 | size_t size; |
| 252 | bool failed = getNetwork(buffer, data, size); |
| 253 | if (failed) { |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | // Create model handle |
| 258 | const tflite::Model *model = tflite::GetModel(reinterpret_cast<const void *>(data)); |
| 259 | if (model->subgraphs() == nullptr) { |
| 260 | printf("Warning: nullptr subgraph\n"); |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | strncpy(description, model->description()->c_str(), sizeof(description)); |
| 265 | |
| 266 | // Get input dimensions for first subgraph |
| 267 | auto *subgraph = *model->subgraphs()->begin(); |
| 268 | failed = getSubGraphDims(subgraph, subgraph->inputs(), ifmDims); |
| 269 | if (failed) { |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | // Get output dimensions for last subgraph |
| 274 | subgraph = *model->subgraphs()->rbegin(); |
| 275 | failed = getSubGraphDims(subgraph, subgraph->outputs(), ofmDims); |
| 276 | if (failed) { |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | return false; |
| 281 | } |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 282 | }; // namespace |
| 283 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 284 | IncomingMessageHandler::IncomingMessageHandler(ethosu_core_queue &_messageQueue, |
| 285 | Mailbox::Mailbox &_mailbox, |
| 286 | QueueHandle_t _inferenceQueue, |
| 287 | QueueHandle_t _outputQueue) : |
| 288 | messageQueue(_messageQueue), |
| 289 | mailbox(_mailbox), inferenceQueue(_inferenceQueue), outputQueue(_outputQueue) { |
| 290 | mailbox.registerCallback(handleIrq, reinterpret_cast<void *>(this)); |
| 291 | semaphore = xSemaphoreCreateBinary(); |
| 292 | } |
| 293 | |
| 294 | void IncomingMessageHandler::run() { |
| 295 | while (true) { |
| 296 | // Wait for event |
| 297 | xSemaphoreTake(semaphore, portMAX_DELAY); |
| 298 | |
| 299 | // Handle all messages in queue |
| 300 | while (handleMessage()) {} |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | void IncomingMessageHandler::handleIrq(void *userArg) { |
| 305 | IncomingMessageHandler *_this = reinterpret_cast<IncomingMessageHandler *>(userArg); |
| 306 | xSemaphoreGive(_this->semaphore); |
| 307 | } |
| 308 | |
| 309 | void IncomingMessageHandler::queueErrorAndResetQueue(ethosu_core_msg_err_type type, const char *message) { |
| 310 | OutputMessage msg(ETHOSU_CORE_MSG_ERR); |
| 311 | msg.data.error.type = type; |
| 312 | |
| 313 | for (size_t i = 0; i < sizeof(msg.data.error.msg) && message[i]; i++) { |
| 314 | msg.data.error.msg[i] = message[i]; |
| 315 | } |
| 316 | |
| 317 | xQueueSend(outputQueue, &msg, portMAX_DELAY); |
| 318 | messageQueue.reset(); |
| 319 | } |
| 320 | |
| 321 | bool IncomingMessageHandler::handleMessage() { |
| 322 | struct ethosu_core_msg msg; |
| 323 | |
| 324 | if (messageQueue.available() == 0) { |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | // Read msg header |
| 329 | // Only process a complete message header, else send error message |
| 330 | // and reset queue |
| 331 | if (!messageQueue.read(msg)) { |
| 332 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_SIZE, "Failed to read a complete header"); |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | printf("Msg: header magic=%" PRIX32 ", type=%" PRIu32 ", length=%" PRIu32 "\n", msg.magic, msg.type, msg.length); |
| 337 | |
| 338 | if (msg.magic != ETHOSU_CORE_MSG_MAGIC) { |
| 339 | printf("Msg: Invalid Magic\n"); |
| 340 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_MAGIC, "Invalid magic"); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | switch (msg.type) { |
| 345 | case ETHOSU_CORE_MSG_PING: { |
| 346 | printf("Msg: Ping\n"); |
| 347 | |
| 348 | OutputMessage message(ETHOSU_CORE_MSG_PONG); |
| 349 | xQueueSend(outputQueue, &message, portMAX_DELAY); |
| 350 | break; |
| 351 | } |
| 352 | case ETHOSU_CORE_MSG_ERR: { |
| 353 | ethosu_core_msg_err error; |
| 354 | |
| 355 | if (!messageQueue.read(error)) { |
| 356 | printf("ERROR: Msg: Failed to receive error message\n"); |
| 357 | } else { |
| 358 | printf("Msg: Received an error response, type=%" PRIu32 ", msg=\"%s\"\n", error.type, error.msg); |
| 359 | } |
| 360 | |
| 361 | messageQueue.reset(); |
| 362 | return false; |
| 363 | } |
| 364 | case ETHOSU_CORE_MSG_VERSION_REQ: { |
| 365 | printf("Msg: Version request\n"); |
| 366 | |
| 367 | OutputMessage message(ETHOSU_CORE_MSG_VERSION_RSP); |
| 368 | xQueueSend(outputQueue, &message, portMAX_DELAY); |
| 369 | break; |
| 370 | } |
| 371 | case ETHOSU_CORE_MSG_CAPABILITIES_REQ: { |
| 372 | ethosu_core_capabilities_req capabilities; |
| 373 | |
| 374 | if (!messageQueue.read(capabilities)) { |
| 375 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "CapabilitiesReq. Failed to read payload"); |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | printf("Msg: Capabilities request.user_arg=0x%" PRIx64 "\n", capabilities.user_arg); |
| 380 | |
| 381 | OutputMessage message(ETHOSU_CORE_MSG_CAPABILITIES_RSP); |
| 382 | message.data.userArg = capabilities.user_arg; |
| 383 | xQueueSend(outputQueue, &message, portMAX_DELAY); |
| 384 | break; |
| 385 | } |
| 386 | case ETHOSU_CORE_MSG_INFERENCE_REQ: { |
| 387 | ethosu_core_inference_req inference; |
| 388 | |
| 389 | if (!messageQueue.read(inference)) { |
| 390 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "InferenceReq. Failed to read payload"); |
| 391 | break; |
| 392 | } |
| 393 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 394 | printf("Msg: InferenceReq. user_arg=0x%" PRIx64 ", network_type=%" PRIu32 ", ", |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 395 | inference.user_arg, |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 396 | inference.network.type); |
| 397 | |
| 398 | if (inference.network.type == ETHOSU_CORE_NETWORK_BUFFER) { |
| 399 | printf("network.buffer={0x%" PRIx32 ", %" PRIu32 "},\n", |
| 400 | inference.network.buffer.ptr, |
| 401 | inference.network.buffer.size); |
| 402 | } else { |
| 403 | printf("network.index=%" PRIu32 ",\n", inference.network.index); |
| 404 | } |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 405 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 406 | printf("ifm_count=%" PRIu32 ", ifm=[", inference.ifm_count); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 407 | for (uint32_t i = 0; i < inference.ifm_count; ++i) { |
| 408 | if (i > 0) { |
| 409 | printf(", "); |
| 410 | } |
| 411 | |
| 412 | printf("{0x%" PRIx32 ", %" PRIu32 "}", inference.ifm[i].ptr, inference.ifm[i].size); |
| 413 | } |
| 414 | printf("]"); |
| 415 | |
| 416 | printf(", ofm_count=%" PRIu32 ", ofm=[", inference.ofm_count); |
| 417 | for (uint32_t i = 0; i < inference.ofm_count; ++i) { |
| 418 | if (i > 0) { |
| 419 | printf(", "); |
| 420 | } |
| 421 | |
| 422 | printf("{0x%" PRIx32 ", %" PRIu32 "}", inference.ofm[i].ptr, inference.ofm[i].size); |
| 423 | } |
| 424 | printf("]\n"); |
| 425 | |
| 426 | xQueueSend(inferenceQueue, &inference, portMAX_DELAY); |
| 427 | break; |
| 428 | } |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 429 | case ETHOSU_CORE_MSG_NETWORK_INFO_REQ: { |
| 430 | ethosu_core_network_info_req req; |
| 431 | |
| 432 | if (!messageQueue.read(req)) { |
| 433 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_INVALID_PAYLOAD, "NetworkInfoReq. Failed to read payload"); |
| 434 | break; |
| 435 | } |
| 436 | |
| 437 | printf("Msg: NetworkInfoReq. user_arg=0x%" PRIx64 "\n", req.user_arg); |
| 438 | |
| 439 | OutputMessage message(ETHOSU_CORE_MSG_NETWORK_INFO_RSP); |
| 440 | ethosu_core_network_info_rsp &rsp = message.data.networkInfo; |
| 441 | rsp.user_arg = req.user_arg; |
| 442 | rsp.ifm_count = 0; |
| 443 | rsp.ofm_count = 0; |
| 444 | |
| 445 | bool failed = parseModel(req.network, |
| 446 | rsp.desc, |
| 447 | makeArray(rsp.ifm_size, rsp.ifm_count, ETHOSU_CORE_BUFFER_MAX), |
| 448 | makeArray(rsp.ofm_size, rsp.ofm_count, ETHOSU_CORE_BUFFER_MAX)); |
| 449 | |
| 450 | rsp.status = failed ? ETHOSU_CORE_STATUS_ERROR : ETHOSU_CORE_STATUS_OK; |
| 451 | |
| 452 | xQueueSend(outputQueue, &message, portMAX_DELAY); |
| 453 | break; |
| 454 | } |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 455 | default: { |
| 456 | char errMsg[128]; |
| 457 | |
| 458 | snprintf(&errMsg[0], |
| 459 | sizeof(errMsg), |
| 460 | "Msg: Unknown type: %" PRIu32 " with payload length %" PRIu32 " bytes\n", |
| 461 | msg.type, |
| 462 | msg.length); |
| 463 | |
| 464 | queueErrorAndResetQueue(ETHOSU_CORE_MSG_ERR_UNSUPPORTED_TYPE, errMsg); |
| 465 | |
| 466 | return false; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return true; |
| 471 | } |
| 472 | |
| 473 | /**************************************************************************** |
| 474 | * InferenceHandler |
| 475 | ****************************************************************************/ |
| 476 | |
| 477 | InferenceHandler::InferenceHandler(uint8_t *tensorArena, |
| 478 | size_t arenaSize, |
| 479 | QueueHandle_t _inferenceQueue, |
| 480 | QueueHandle_t _outputQueue) : |
| 481 | inferenceQueue(_inferenceQueue), |
| 482 | outputQueue(_outputQueue), inference(tensorArena, arenaSize) {} |
| 483 | |
| 484 | void InferenceHandler::run() { |
| 485 | while (true) { |
| 486 | ethosu_core_inference_req req; |
| 487 | |
| 488 | if (pdTRUE != xQueueReceive(inferenceQueue, &req, portMAX_DELAY)) { |
| 489 | continue; |
| 490 | } |
| 491 | |
| 492 | OutputMessage msg(ETHOSU_CORE_MSG_INFERENCE_RSP); |
| 493 | runInference(req, msg.data.inference); |
| 494 | |
| 495 | xQueueSend(outputQueue, &msg, portMAX_DELAY); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | 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] | 500 | currentReq = &req; |
| 501 | currentRsp = &rsp; |
| 502 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 503 | /* |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 504 | * Run inference |
| 505 | */ |
| 506 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 507 | InferenceProcess::InferenceJob job; |
| 508 | bool failed = getInferenceJob(req, job); |
| 509 | |
| 510 | if (!failed) { |
| 511 | job.invalidate(); |
| 512 | failed = inference.runJob(job); |
| 513 | job.clean(); |
| 514 | } |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 515 | |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 516 | #if defined(ETHOSU) |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 517 | /* |
Kristofer Jonsson | 47f556a | 2022-02-02 12:11:22 +0100 | [diff] [blame] | 518 | * Print PMU counters |
| 519 | */ |
| 520 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 521 | if (!failed) { |
| 522 | 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] | 523 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 524 | for (int i = 0; i < numEvents; i++) { |
| 525 | printf("ethosu_pmu_cntr%d : %" PRIu32 "\n", i, rsp.pmu_event_count[i]); |
| 526 | } |
Kristofer Jonsson | 47f556a | 2022-02-02 12:11:22 +0100 | [diff] [blame] | 527 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 528 | if (rsp.pmu_cycle_counter_enable) { |
| 529 | printf("ethosu_pmu_cycle_cntr : %" PRIu64 " cycles\n", rsp.pmu_cycle_counter_count); |
| 530 | } |
Kristofer Jonsson | 47f556a | 2022-02-02 12:11:22 +0100 | [diff] [blame] | 531 | } |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 532 | #endif |
Kristofer Jonsson | 47f556a | 2022-02-02 12:11:22 +0100 | [diff] [blame] | 533 | |
| 534 | /* |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 535 | * Send inference response |
| 536 | */ |
| 537 | |
| 538 | rsp.user_arg = req.user_arg; |
| 539 | rsp.ofm_count = job.output.size(); |
| 540 | rsp.status = failed ? ETHOSU_CORE_STATUS_ERROR : ETHOSU_CORE_STATUS_OK; |
| 541 | |
| 542 | for (size_t i = 0; i < job.output.size(); ++i) { |
| 543 | rsp.ofm_size[i] = job.output[i].size; |
| 544 | } |
| 545 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 546 | currentReq = nullptr; |
| 547 | currentRsp = nullptr; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 548 | } |
| 549 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 550 | bool InferenceHandler::getInferenceJob(const ethosu_core_inference_req &req, InferenceProcess::InferenceJob &job) { |
| 551 | bool failed = getNetwork(req.network, job.networkModel.data, job.networkModel.size); |
| 552 | if (failed) { |
| 553 | return true; |
| 554 | } |
| 555 | |
| 556 | for (uint32_t i = 0; i < req.ifm_count; ++i) { |
| 557 | job.input.push_back(InferenceProcess::DataPtr(reinterpret_cast<void *>(req.ifm[i].ptr), req.ifm[i].size)); |
| 558 | } |
| 559 | |
| 560 | for (uint32_t i = 0; i < req.ofm_count; ++i) { |
| 561 | job.output.push_back(InferenceProcess::DataPtr(reinterpret_cast<void *>(req.ofm[i].ptr), req.ofm[i].size)); |
| 562 | } |
| 563 | |
| 564 | job.externalContext = this; |
| 565 | |
| 566 | return false; |
| 567 | } |
| 568 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 569 | /**************************************************************************** |
| 570 | * OutgoingMessageHandler |
| 571 | ****************************************************************************/ |
| 572 | |
| 573 | OutgoingMessageHandler::OutgoingMessageHandler(ethosu_core_queue &_messageQueue, |
| 574 | Mailbox::Mailbox &_mailbox, |
| 575 | QueueHandle_t _outputQueue) : |
| 576 | messageQueue(_messageQueue), |
| 577 | mailbox(_mailbox), outputQueue(_outputQueue) { |
| 578 | readCapabilties(capabilities); |
| 579 | } |
| 580 | |
| 581 | void OutgoingMessageHandler::run() { |
| 582 | while (true) { |
| 583 | OutputMessage message; |
| 584 | if (pdTRUE != xQueueReceive(outputQueue, &message, portMAX_DELAY)) { |
| 585 | continue; |
| 586 | } |
| 587 | |
| 588 | switch (message.type) { |
| 589 | case ETHOSU_CORE_MSG_INFERENCE_RSP: |
| 590 | sendInferenceRsp(message.data.inference); |
| 591 | break; |
| 592 | case ETHOSU_CORE_MSG_CAPABILITIES_RSP: |
| 593 | sendCapabilitiesRsp(message.data.userArg); |
| 594 | break; |
| 595 | case ETHOSU_CORE_MSG_VERSION_RSP: |
| 596 | sendVersionRsp(); |
| 597 | break; |
| 598 | case ETHOSU_CORE_MSG_PONG: |
| 599 | sendPong(); |
| 600 | break; |
| 601 | case ETHOSU_CORE_MSG_ERR: |
| 602 | sendErrorRsp(message.data.error); |
| 603 | break; |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 604 | case ETHOSU_CORE_MSG_NETWORK_INFO_RSP: |
| 605 | sendNetworkInfoRsp(message.data.networkInfo); |
| 606 | break; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 607 | default: |
| 608 | printf("Dropping unknown outcome of type %d\n", message.type); |
| 609 | break; |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | void OutgoingMessageHandler::sendPong() { |
| 615 | if (!messageQueue.write(ETHOSU_CORE_MSG_PONG)) { |
| 616 | printf("ERROR: Msg: Failed to write pong response. No mailbox message sent\n"); |
| 617 | } else { |
| 618 | mailbox.sendMessage(); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | void OutgoingMessageHandler::sendVersionRsp() { |
| 623 | ethosu_core_msg_version version = { |
| 624 | ETHOSU_CORE_MSG_VERSION_MAJOR, |
| 625 | ETHOSU_CORE_MSG_VERSION_MINOR, |
| 626 | ETHOSU_CORE_MSG_VERSION_PATCH, |
| 627 | 0, |
| 628 | }; |
| 629 | |
| 630 | if (!messageQueue.write(ETHOSU_CORE_MSG_VERSION_RSP, version)) { |
| 631 | printf("ERROR: Failed to write version response. No mailbox message sent\n"); |
| 632 | } else { |
| 633 | mailbox.sendMessage(); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | void OutgoingMessageHandler::sendCapabilitiesRsp(uint64_t userArg) { |
| 638 | capabilities.user_arg = userArg; |
| 639 | |
| 640 | if (!messageQueue.write(ETHOSU_CORE_MSG_CAPABILITIES_RSP, capabilities)) { |
| 641 | printf("ERROR: Failed to write capabilities response. No mailbox message sent\n"); |
| 642 | } else { |
| 643 | mailbox.sendMessage(); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | void OutgoingMessageHandler::sendInferenceRsp(ethosu_core_inference_rsp &inference) { |
| 648 | if (!messageQueue.write(ETHOSU_CORE_MSG_INFERENCE_RSP, inference)) { |
| 649 | printf("ERROR: Msg: Failed to write inference response. No mailbox message sent\n"); |
| 650 | } else { |
| 651 | mailbox.sendMessage(); |
| 652 | } |
| 653 | } |
| 654 | |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 655 | void OutgoingMessageHandler::sendNetworkInfoRsp(EthosU::ethosu_core_network_info_rsp &networkInfo) { |
| 656 | if (!messageQueue.write(ETHOSU_CORE_MSG_NETWORK_INFO_RSP, networkInfo)) { |
| 657 | printf("ERROR: Msg: Failed to write network info response. No mailbox message sent\n"); |
| 658 | } else { |
| 659 | mailbox.sendMessage(); |
| 660 | } |
| 661 | } |
| 662 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 663 | void OutgoingMessageHandler::sendErrorRsp(ethosu_core_msg_err &error) { |
| 664 | printf("ERROR: Msg: \"%s\"\n", error.msg); |
| 665 | |
| 666 | if (!messageQueue.write(ETHOSU_CORE_MSG_ERR, error)) { |
| 667 | printf("ERROR: Msg: Failed to write error response. No mailbox message sent\n"); |
| 668 | } else { |
| 669 | mailbox.sendMessage(); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | void OutgoingMessageHandler::readCapabilties(ethosu_core_msg_capabilities_rsp &rsp) { |
Kristofer Jonsson | 29467e0 | 2021-11-26 16:10:43 +0100 | [diff] [blame] | 674 | rsp = {}; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 675 | |
| 676 | #ifdef ETHOSU |
| 677 | struct ethosu_driver_version version; |
| 678 | ethosu_get_driver_version(&version); |
| 679 | |
| 680 | struct ethosu_hw_info info; |
| 681 | struct ethosu_driver *drv = ethosu_reserve_driver(); |
| 682 | ethosu_get_hw_info(drv, &info); |
| 683 | ethosu_release_driver(drv); |
| 684 | |
| 685 | rsp.user_arg = 0; |
| 686 | rsp.version_status = info.version.version_status; |
| 687 | rsp.version_minor = info.version.version_minor; |
| 688 | rsp.version_major = info.version.version_major; |
| 689 | rsp.product_major = info.version.product_major; |
| 690 | rsp.arch_patch_rev = info.version.arch_patch_rev; |
| 691 | rsp.arch_minor_rev = info.version.arch_minor_rev; |
| 692 | rsp.arch_major_rev = info.version.arch_major_rev; |
| 693 | rsp.driver_patch_rev = version.patch; |
| 694 | rsp.driver_minor_rev = version.minor; |
| 695 | rsp.driver_major_rev = version.major; |
| 696 | rsp.macs_per_cc = info.cfg.macs_per_cc; |
| 697 | rsp.cmd_stream_version = info.cfg.cmd_stream_version; |
| 698 | rsp.custom_dma = info.cfg.custom_dma; |
| 699 | #endif |
| 700 | } |
| 701 | |
| 702 | } // namespace MessageHandler |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 703 | |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 704 | #if defined(ETHOSU) |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 705 | extern "C" void ethosu_inference_begin(struct ethosu_driver *drv, void *userArg) { |
| 706 | MessageHandler::InferenceHandler *self = static_cast<MessageHandler::InferenceHandler *>(userArg); |
| 707 | |
| 708 | // Calculate maximum number of events |
| 709 | const int numEvents = std::min(static_cast<int>(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); |
| 710 | |
| 711 | // Enable PMU |
| 712 | ETHOSU_PMU_Enable(drv); |
| 713 | |
| 714 | // Configure and enable events |
| 715 | for (int i = 0; i < numEvents; i++) { |
| 716 | ETHOSU_PMU_Set_EVTYPER(drv, i, static_cast<ethosu_pmu_event_type>(self->currentReq->pmu_event_config[i])); |
| 717 | ETHOSU_PMU_CNTR_Enable(drv, 1 << i); |
| 718 | } |
| 719 | |
| 720 | // Enable cycle counter |
| 721 | if (self->currentReq->pmu_cycle_counter_enable) { |
Kristofer Jonsson | b9d7ded | 2022-02-04 13:10:04 +0100 | [diff] [blame] | 722 | ETHOSU_PMU_PMCCNTR_CFG_Set_Stop_Event(drv, ETHOSU_PMU_NPU_IDLE); |
| 723 | ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event(drv, ETHOSU_PMU_NPU_ACTIVE); |
| 724 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 725 | ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk); |
| 726 | ETHOSU_PMU_CYCCNT_Reset(drv); |
| 727 | } |
| 728 | |
| 729 | // Reset all counters |
| 730 | ETHOSU_PMU_EVCNTR_ALL_Reset(drv); |
| 731 | } |
| 732 | |
| 733 | extern "C" void ethosu_inference_end(struct ethosu_driver *drv, void *userArg) { |
| 734 | MessageHandler::InferenceHandler *self = static_cast<MessageHandler::InferenceHandler *>(userArg); |
| 735 | |
| 736 | // Get cycle counter |
| 737 | self->currentRsp->pmu_cycle_counter_enable = self->currentReq->pmu_cycle_counter_enable; |
| 738 | if (self->currentReq->pmu_cycle_counter_enable) { |
| 739 | self->currentRsp->pmu_cycle_counter_count = ETHOSU_PMU_Get_CCNTR(drv); |
| 740 | } |
| 741 | |
| 742 | // Calculate maximum number of events |
| 743 | const int numEvents = std::min(static_cast<int>(ETHOSU_PMU_Get_NumEventCounters()), ETHOSU_CORE_PMU_MAX); |
| 744 | |
| 745 | // Get event counters |
| 746 | int i; |
| 747 | for (i = 0; i < numEvents; i++) { |
| 748 | self->currentRsp->pmu_event_config[i] = self->currentReq->pmu_event_config[i]; |
| 749 | self->currentRsp->pmu_event_count[i] = ETHOSU_PMU_Get_EVCNTR(drv, i); |
| 750 | } |
| 751 | |
| 752 | for (; i < ETHOSU_CORE_PMU_MAX; i++) { |
| 753 | self->currentRsp->pmu_event_config[i] = 0; |
| 754 | self->currentRsp->pmu_event_count[i] = 0; |
| 755 | } |
| 756 | |
| 757 | // Disable PMU |
| 758 | ETHOSU_PMU_Disable(drv); |
| 759 | } |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 760 | #endif |