Davide Grohmann | ec72e9b | 2022-08-08 17:30:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022 Arm Limited. |
| 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 | /**************************************************************************** |
| 20 | * Includes |
| 21 | ****************************************************************************/ |
| 22 | |
| 23 | #include "FreeRTOS.h" |
| 24 | #include "queue.h" |
| 25 | #include "semphr.h" |
| 26 | #include "task.h" |
| 27 | |
| 28 | #include <inttypes.h> |
| 29 | #include <stdio.h> |
| 30 | |
| 31 | #include "ethosu_core_interface.h" |
| 32 | #include "indexed_networks.hpp" |
| 33 | #include "input.h" |
| 34 | #include "message_client.hpp" |
| 35 | #include "message_handler.hpp" |
| 36 | #include "message_queue.hpp" |
| 37 | #include "networks.hpp" |
| 38 | #include "output.h" |
| 39 | |
| 40 | #include <mailbox.hpp> |
| 41 | #include <mhu_dummy.hpp> |
| 42 | |
| 43 | /* Disable semihosting */ |
| 44 | __asm(".global __use_no_semihosting\n\t"); |
| 45 | |
| 46 | using namespace EthosU; |
| 47 | using namespace MessageHandler; |
| 48 | |
| 49 | /**************************************************************************** |
| 50 | * Defines |
| 51 | ****************************************************************************/ |
| 52 | |
| 53 | #define TEST_ASSERT(v) \ |
| 54 | do { \ |
| 55 | if (!(v)) { \ |
| 56 | fprintf(stderr, "%s:%d ERROR test failed: '%s'\n", __FILE__, __LINE__, #v); \ |
| 57 | exit(1); \ |
| 58 | } \ |
| 59 | } while (0) |
| 60 | |
| 61 | // Nr. of tasks to process inferences with, reserves driver & runs inference (Normally 1 per NPU, but not a must) |
| 62 | #if defined(ETHOSU) && defined(ETHOSU_NPU_COUNT) && ETHOSU_NPU_COUNT > 0 |
| 63 | constexpr size_t NUM_PARALLEL_TASKS = ETHOSU_NPU_COUNT; |
| 64 | #else |
| 65 | constexpr size_t NUM_PARALLEL_TASKS = 1; |
| 66 | #endif |
| 67 | |
| 68 | // TensorArena static initialisation |
| 69 | constexpr size_t arenaSize = TENSOR_ARENA_SIZE; |
| 70 | |
| 71 | __attribute__((section(".bss.tensor_arena"), aligned(16))) uint8_t tensorArena[NUM_PARALLEL_TASKS][arenaSize]; |
| 72 | |
| 73 | // Message queue from remote host |
| 74 | __attribute__((section("ethosu_core_in_queue"))) MessageQueue::Queue<1000> inputMessageQueue; |
| 75 | |
| 76 | // Message queue to remote host |
| 77 | __attribute__((section("ethosu_core_out_queue"))) MessageQueue::Queue<1000> outputMessageQueue; |
| 78 | |
| 79 | namespace { |
| 80 | Mailbox::MHUDummy mailbox; |
| 81 | } // namespace |
| 82 | |
| 83 | /**************************************************************************** |
| 84 | * Application |
| 85 | ****************************************************************************/ |
| 86 | namespace { |
| 87 | |
| 88 | struct TaskParams { |
| 89 | TaskParams() : |
| 90 | messageNotify(xSemaphoreCreateBinary()), |
| 91 | inferenceInputQueue(std::make_shared<Queue<ethosu_core_inference_req>>()), |
| 92 | inferenceOutputQueue(xQueueCreate(10, sizeof(ethosu_core_inference_rsp))), |
| 93 | networks(std::make_shared<WithIndexedNetworks>()) {} |
| 94 | |
| 95 | SemaphoreHandle_t messageNotify; |
| 96 | // Used to pass inference requests to the inference runner task |
| 97 | std::shared_ptr<Queue<ethosu_core_inference_req>> inferenceInputQueue; |
| 98 | // Queue for message responses to the remote host |
| 99 | QueueHandle_t inferenceOutputQueue; |
| 100 | // Networks provider |
| 101 | std::shared_ptr<Networks> networks; |
| 102 | }; |
| 103 | |
| 104 | struct InferenceTaskParams { |
| 105 | TaskParams *taskParams; |
| 106 | uint8_t *arena; |
| 107 | }; |
| 108 | |
| 109 | void inferenceTask(void *pvParameters) { |
| 110 | printf("Starting inference task\n"); |
| 111 | InferenceTaskParams *params = reinterpret_cast<InferenceTaskParams *>(pvParameters); |
| 112 | |
| 113 | InferenceHandler process(params->arena, |
| 114 | arenaSize, |
| 115 | params->taskParams->inferenceInputQueue, |
| 116 | params->taskParams->inferenceOutputQueue, |
| 117 | params->taskParams->messageNotify, |
| 118 | params->taskParams->networks); |
| 119 | |
| 120 | process.run(); |
| 121 | } |
| 122 | |
| 123 | void messageTask(void *pvParameters) { |
| 124 | printf("Starting message task\n"); |
| 125 | TaskParams *params = reinterpret_cast<TaskParams *>(pvParameters); |
| 126 | |
| 127 | IncomingMessageHandler process(*inputMessageQueue.toQueue(), |
| 128 | *outputMessageQueue.toQueue(), |
| 129 | mailbox, |
| 130 | params->inferenceInputQueue, |
| 131 | params->inferenceOutputQueue, |
| 132 | params->messageNotify, |
| 133 | params->networks); |
| 134 | process.run(); |
| 135 | } |
| 136 | |
| 137 | void testPing(MessageClient client) { |
| 138 | TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_PING)); |
| 139 | TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_PONG)); |
| 140 | } |
| 141 | |
| 142 | void testVersion(MessageClient client) { |
| 143 | ethosu_core_msg_version ver; |
| 144 | TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_VERSION_REQ)); |
| 145 | TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_VERSION_RSP, ver)); |
| 146 | |
| 147 | TEST_ASSERT(ver.major == ETHOSU_CORE_MSG_VERSION_MAJOR); |
| 148 | TEST_ASSERT(ver.minor == ETHOSU_CORE_MSG_VERSION_MINOR); |
| 149 | TEST_ASSERT(ver.patch == ETHOSU_CORE_MSG_VERSION_PATCH); |
| 150 | } |
| 151 | |
| 152 | void readCapabilities(ethosu_core_msg_capabilities_rsp &rsp) { |
| 153 | #ifdef ETHOSU |
| 154 | struct ethosu_driver_version version; |
| 155 | ethosu_get_driver_version(&version); |
| 156 | |
| 157 | struct ethosu_hw_info info; |
| 158 | struct ethosu_driver *drv = ethosu_reserve_driver(); |
| 159 | ethosu_get_hw_info(drv, &info); |
| 160 | ethosu_release_driver(drv); |
| 161 | |
| 162 | rsp.version_status = info.version.version_status; |
| 163 | rsp.version_minor = info.version.version_minor; |
| 164 | rsp.version_major = info.version.version_major; |
| 165 | rsp.product_major = info.version.product_major; |
| 166 | rsp.arch_patch_rev = info.version.arch_patch_rev; |
| 167 | rsp.arch_minor_rev = info.version.arch_minor_rev; |
| 168 | rsp.arch_major_rev = info.version.arch_major_rev; |
| 169 | rsp.driver_patch_rev = version.patch; |
| 170 | rsp.driver_minor_rev = version.minor; |
| 171 | rsp.driver_major_rev = version.major; |
| 172 | rsp.macs_per_cc = info.cfg.macs_per_cc; |
| 173 | rsp.cmd_stream_version = info.cfg.cmd_stream_version; |
| 174 | rsp.custom_dma = info.cfg.custom_dma; |
| 175 | #endif |
| 176 | } |
| 177 | |
| 178 | void testCapabilities(MessageClient client) { |
| 179 | const uint64_t fake_user_arg = 42; |
| 180 | ethosu_core_capabilities_req req = {fake_user_arg}; |
| 181 | ethosu_core_msg_capabilities_rsp expected_rsp; |
| 182 | ethosu_core_msg_capabilities_rsp rsp; |
| 183 | |
| 184 | readCapabilities(expected_rsp); |
| 185 | expected_rsp.user_arg = req.user_arg; |
| 186 | |
| 187 | TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_CAPABILITIES_REQ, req)); |
| 188 | TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_CAPABILITIES_RSP, rsp)); |
| 189 | |
| 190 | TEST_ASSERT(expected_rsp.version_status == rsp.version_status); |
| 191 | TEST_ASSERT(expected_rsp.version_minor == rsp.version_minor); |
| 192 | TEST_ASSERT(expected_rsp.version_major == rsp.version_major); |
| 193 | TEST_ASSERT(expected_rsp.product_major == rsp.product_major); |
| 194 | TEST_ASSERT(expected_rsp.arch_patch_rev == rsp.arch_patch_rev); |
| 195 | TEST_ASSERT(expected_rsp.arch_minor_rev == rsp.arch_minor_rev); |
| 196 | TEST_ASSERT(expected_rsp.arch_major_rev == rsp.arch_major_rev); |
| 197 | TEST_ASSERT(expected_rsp.driver_patch_rev == rsp.driver_patch_rev); |
| 198 | TEST_ASSERT(expected_rsp.driver_minor_rev == rsp.driver_minor_rev); |
| 199 | TEST_ASSERT(expected_rsp.driver_major_rev == rsp.driver_major_rev); |
| 200 | TEST_ASSERT(expected_rsp.macs_per_cc == rsp.macs_per_cc); |
| 201 | TEST_ASSERT(expected_rsp.cmd_stream_version == rsp.cmd_stream_version); |
| 202 | TEST_ASSERT(expected_rsp.custom_dma == rsp.custom_dma); |
| 203 | |
| 204 | #ifdef ETHOSU |
Davide Grohmann | fc42c71 | 2022-08-22 14:06:16 +0200 | [diff] [blame^] | 205 | TEST_ASSERT(rsp.version_status > 0); |
Davide Grohmann | ec72e9b | 2022-08-08 17:30:28 +0200 | [diff] [blame] | 206 | TEST_ASSERT(rsp.product_major > 0); |
| 207 | TEST_ASSERT(rsp.arch_major_rev > 0 || rsp.arch_minor_rev > 0 || rsp.arch_patch_rev > 0); |
| 208 | TEST_ASSERT(rsp.driver_major_rev > 0 || rsp.driver_minor_rev > 0 || rsp.driver_patch_rev > 0); |
| 209 | TEST_ASSERT(rsp.macs_per_cc > 0); |
| 210 | #endif |
| 211 | } |
| 212 | |
| 213 | void testNetworkInfo(MessageClient client) { |
| 214 | const uint64_t fake_user_arg = 42; |
| 215 | ethosu_core_network_info_req req = {fake_user_arg, // user_arg |
| 216 | { // network |
| 217 | ETHOSU_CORE_NETWORK_INDEX, // type |
| 218 | {{ |
| 219 | 0, // index |
| 220 | 0 // ignored padding of union |
| 221 | }}}}; |
| 222 | ethosu_core_network_info_rsp rsp; |
| 223 | ethosu_core_network_info_rsp expected_rsp = { |
| 224 | req.user_arg, // user_arg |
| 225 | "Vela Optimised", // description |
| 226 | 1, // ifm_count |
| 227 | {/* not comparable */}, // ifm_sizes |
| 228 | 1, // ofm_count |
| 229 | {/* not comparable */}, // ofm_sizes |
| 230 | 0 // status |
| 231 | }; |
| 232 | |
| 233 | TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_REQ, req)); |
| 234 | TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_RSP, rsp)); |
| 235 | |
| 236 | TEST_ASSERT(expected_rsp.user_arg == rsp.user_arg); |
| 237 | TEST_ASSERT(std::strncmp(expected_rsp.desc, rsp.desc, sizeof(rsp.desc)) == 0); |
| 238 | TEST_ASSERT(expected_rsp.ifm_count == rsp.ifm_count); |
| 239 | TEST_ASSERT(expected_rsp.ofm_count == rsp.ofm_count); |
| 240 | TEST_ASSERT(expected_rsp.status == rsp.status); |
| 241 | } |
| 242 | |
| 243 | void testInferenceRun(MessageClient client) { |
| 244 | uint8_t data[sizeof(expectedOutputData)]; |
| 245 | const uint64_t fake_user_arg = 42; |
| 246 | ethosu_core_inference_req req = { |
| 247 | fake_user_arg, // user_arg |
| 248 | 1, // ifm_count |
| 249 | { // ifm: |
| 250 | { |
| 251 | reinterpret_cast<uint32_t>(&inputData[0]), // ptr |
| 252 | sizeof(inputData) // size |
| 253 | }}, |
| 254 | 1, // ofm_count |
| 255 | { // ofm |
| 256 | { |
| 257 | reinterpret_cast<uint32_t>(&data[0]), // ptr |
| 258 | sizeof(data) // size |
| 259 | }}, |
| 260 | { // network |
| 261 | ETHOSU_CORE_NETWORK_INDEX, // type |
| 262 | {{ |
| 263 | 0, // index |
| 264 | 0 // ignored padding of union |
| 265 | }}}, |
| 266 | {0, 0, 0, 0, 0, 0, 0, 0}, // pmu_event_config |
| 267 | 0 // pmu_cycle_counter_enable |
| 268 | }; |
| 269 | ethosu_core_inference_rsp rsp; |
| 270 | |
| 271 | TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, req)); |
| 272 | TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp)); |
| 273 | |
| 274 | TEST_ASSERT(req.user_arg == rsp.user_arg); |
| 275 | TEST_ASSERT(rsp.ofm_count == 1); |
| 276 | TEST_ASSERT(std::memcmp(expectedOutputData, data, sizeof(expectedOutputData)) == 0); |
| 277 | TEST_ASSERT(rsp.status == ETHOSU_CORE_STATUS_OK); |
| 278 | TEST_ASSERT(rsp.pmu_cycle_counter_enable == req.pmu_cycle_counter_enable); |
| 279 | TEST_ASSERT(std::memcmp(rsp.pmu_event_config, req.pmu_event_config, sizeof(req.pmu_event_config)) == 0); |
| 280 | } |
| 281 | |
| 282 | void clientTask(void *) { |
| 283 | printf("Starting client task\n"); |
| 284 | |
| 285 | MessageClient client(*inputMessageQueue.toQueue(), *outputMessageQueue.toQueue(), mailbox); |
| 286 | |
| 287 | vTaskDelay(10); |
| 288 | |
| 289 | testPing(client); |
| 290 | testVersion(client); |
| 291 | testCapabilities(client); |
| 292 | testNetworkInfo(client); |
| 293 | testInferenceRun(client); |
| 294 | |
| 295 | exit(0); |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Keep task parameters as global data as FreeRTOS resets the stack when the |
| 300 | * scheduler is started. |
| 301 | */ |
| 302 | TaskParams taskParams; |
| 303 | InferenceTaskParams infParams[NUM_PARALLEL_TASKS]; |
| 304 | |
| 305 | } // namespace |
| 306 | |
| 307 | // FreeRTOS application. NOTE: Additional tasks may require increased heap size. |
| 308 | int main() { |
| 309 | BaseType_t ret; |
| 310 | |
| 311 | if (!mailbox.verifyHardware()) { |
| 312 | printf("Failed to verify mailbox hardware\n"); |
| 313 | return 1; |
| 314 | } |
| 315 | |
| 316 | // Task for handling incoming /outgoing messages from the remote host |
| 317 | ret = xTaskCreate(messageTask, "messageTask", 1024, &taskParams, 2, nullptr); |
| 318 | if (ret != pdPASS) { |
| 319 | printf("Failed to create 'messageTask'\n"); |
| 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | // One inference task for each NPU |
| 324 | for (size_t n = 0; n < NUM_PARALLEL_TASKS; n++) { |
| 325 | infParams[n].taskParams = &taskParams; |
| 326 | infParams[n].arena = reinterpret_cast<uint8_t *>(&tensorArena[n]); |
| 327 | ret = xTaskCreate(inferenceTask, "inferenceTask", 8 * 1024, &infParams[n], 3, nullptr); |
| 328 | if (ret != pdPASS) { |
| 329 | printf("Failed to create 'inferenceTask%d'\n", n); |
| 330 | return ret; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // Task for handling incoming /outgoing messages from the remote host |
| 335 | ret = xTaskCreate(clientTask, "clientTask", 512, nullptr, 2, nullptr); |
| 336 | if (ret != pdPASS) { |
| 337 | printf("Failed to create 'messageTask'\n"); |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | // Start Scheduler |
| 342 | vTaskStartScheduler(); |
| 343 | |
| 344 | return 1; |
| 345 | } |