aboutsummaryrefslogtreecommitdiff
path: root/applications/message_handler/test/run_inference_test.cpp
blob: d05224fdb4b40c98bd520b04cbd3e2fcd2aa8bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * Copyright (c) 2022 Arm Limited.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/****************************************************************************
 * Includes
 ****************************************************************************/

#include "FreeRTOS.h"
#include "queue.h"
#include "semphr.h"
#include "task.h"

#include <inttypes.h>
#include <stdio.h>

#include "ethosu_core_interface.h"
#include "indexed_networks.hpp"
#include "input.h"
#include "message_client.hpp"
#include "message_handler.hpp"
#include "message_queue.hpp"
#include "networks.hpp"
#include "output.h"
#include "test_assertions.hpp"
#include "test_helpers.hpp"

#include <mailbox.hpp>
#include <mhu_dummy.hpp>

/* Disable semihosting */
__asm(".global __use_no_semihosting\n\t");

using namespace EthosU;
using namespace MessageHandler;

/****************************************************************************
 * Defines
 ****************************************************************************/

// TensorArena static initialisation
constexpr size_t arenaSize = TENSOR_ARENA_SIZE;

__attribute__((section(".bss.tensor_arena"), aligned(16))) uint8_t tensorArena[arenaSize];

// Message queue from remote host
__attribute__((section("ethosu_core_in_queue"))) MessageQueue::Queue<1000> inputMessageQueue;

// Message queue to remote host
__attribute__((section("ethosu_core_out_queue"))) MessageQueue::Queue<1000> outputMessageQueue;

namespace {
Mailbox::MHUDummy mailbox;
} // namespace

/****************************************************************************
 * Application
 ****************************************************************************/
namespace {

struct TaskParams {
    TaskParams() :
        messageNotify(xSemaphoreCreateBinary()),
        inferenceInputQueue(std::make_shared<Queue<ethosu_core_inference_req>>()),
        inferenceOutputQueue(xQueueCreate(5, sizeof(ethosu_core_inference_rsp))),
        networks(std::make_shared<WithIndexedNetworks>()) {}

    SemaphoreHandle_t messageNotify;
    // Used to pass inference requests to the inference runner task
    std::shared_ptr<Queue<ethosu_core_inference_req>> inferenceInputQueue;
    // Queue for message responses to the remote host
    QueueHandle_t inferenceOutputQueue;
    // Networks provider
    std::shared_ptr<Networks> networks;
};

void inferenceTask(void *pvParameters) {
    printf("Starting inference task\n");
    TaskParams *params = reinterpret_cast<TaskParams *>(pvParameters);

    InferenceHandler process(tensorArena,
                             arenaSize,
                             params->inferenceInputQueue,
                             params->inferenceOutputQueue,
                             params->messageNotify,
                             params->networks);

    process.run();
}

void messageTask(void *pvParameters) {
    printf("Starting message task\n");
    TaskParams *params = reinterpret_cast<TaskParams *>(pvParameters);

    IncomingMessageHandler process(*inputMessageQueue.toQueue(),
                                   *outputMessageQueue.toQueue(),
                                   mailbox,
                                   params->inferenceInputQueue,
                                   params->inferenceOutputQueue,
                                   params->messageNotify,
                                   params->networks);
    process.run();
}

void testPing(MessageClient client) {
    TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_PING));
    TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_PONG));
}

void testVersion(MessageClient client) {
    ethosu_core_msg_version ver;
    TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_VERSION_REQ));
    TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_VERSION_RSP, ver));

    TEST_ASSERT(ver.major == ETHOSU_CORE_MSG_VERSION_MAJOR);
    TEST_ASSERT(ver.minor == ETHOSU_CORE_MSG_VERSION_MINOR);
    TEST_ASSERT(ver.patch == ETHOSU_CORE_MSG_VERSION_PATCH);
}

void readCapabilities(ethosu_core_msg_capabilities_rsp &rsp) {
#ifdef ETHOSU
    struct ethosu_driver_version version;
    ethosu_get_driver_version(&version);

    struct ethosu_hw_info info;
    struct ethosu_driver *drv = ethosu_reserve_driver();
    ethosu_get_hw_info(drv, &info);
    ethosu_release_driver(drv);

    rsp.version_status     = info.version.version_status;
    rsp.version_minor      = info.version.version_minor;
    rsp.version_major      = info.version.version_major;
    rsp.product_major      = info.version.product_major;
    rsp.arch_patch_rev     = info.version.arch_patch_rev;
    rsp.arch_minor_rev     = info.version.arch_minor_rev;
    rsp.arch_major_rev     = info.version.arch_major_rev;
    rsp.driver_patch_rev   = version.patch;
    rsp.driver_minor_rev   = version.minor;
    rsp.driver_major_rev   = version.major;
    rsp.macs_per_cc        = info.cfg.macs_per_cc;
    rsp.cmd_stream_version = info.cfg.cmd_stream_version;
    rsp.custom_dma         = info.cfg.custom_dma;
#endif
}

void testCapabilities(MessageClient client) {
    const uint64_t fake_user_arg     = 42;
    ethosu_core_capabilities_req req = {fake_user_arg};
    ethosu_core_msg_capabilities_rsp expected_rsp;
    ethosu_core_msg_capabilities_rsp rsp;

    readCapabilities(expected_rsp);
    expected_rsp.user_arg = req.user_arg;

    TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_CAPABILITIES_REQ, req));
    TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_CAPABILITIES_RSP, rsp));

    TEST_ASSERT(expected_rsp.version_status == rsp.version_status);
    TEST_ASSERT(expected_rsp.version_minor == rsp.version_minor);
    TEST_ASSERT(expected_rsp.version_major == rsp.version_major);
    TEST_ASSERT(expected_rsp.product_major == rsp.product_major);
    TEST_ASSERT(expected_rsp.arch_patch_rev == rsp.arch_patch_rev);
    TEST_ASSERT(expected_rsp.arch_minor_rev == rsp.arch_minor_rev);
    TEST_ASSERT(expected_rsp.arch_major_rev == rsp.arch_major_rev);
    TEST_ASSERT(expected_rsp.driver_patch_rev == rsp.driver_patch_rev);
    TEST_ASSERT(expected_rsp.driver_minor_rev == rsp.driver_minor_rev);
    TEST_ASSERT(expected_rsp.driver_major_rev == rsp.driver_major_rev);
    TEST_ASSERT(expected_rsp.macs_per_cc == rsp.macs_per_cc);
    TEST_ASSERT(expected_rsp.cmd_stream_version == rsp.cmd_stream_version);
    TEST_ASSERT(expected_rsp.custom_dma == rsp.custom_dma);

#ifdef ETHOSU
    TEST_ASSERT(rsp.version_status > 0);
    TEST_ASSERT(rsp.product_major > 0);
    TEST_ASSERT(rsp.arch_major_rev > 0 || rsp.arch_minor_rev > 0 || rsp.arch_patch_rev > 0);
    TEST_ASSERT(rsp.driver_major_rev > 0 || rsp.driver_minor_rev > 0 || rsp.driver_patch_rev > 0);
    TEST_ASSERT(rsp.macs_per_cc > 0);
#endif
}

void testNetworkInfoIndex(MessageClient client) {
    const uint64_t fake_user_arg     = 42;
    const uint32_t network_index     = 0;
    ethosu_core_network_info_req req = networkInfoIndexedRequest(fake_user_arg, network_index);
    ethosu_core_network_info_rsp rsp;
    ethosu_core_network_info_rsp expected_rsp = networkInfoResponse(fake_user_arg);

    TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_REQ, req));
    TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_RSP, rsp));

    TEST_ASSERT(expected_rsp.user_arg == rsp.user_arg);
    TEST_ASSERT(std::strncmp(expected_rsp.desc, rsp.desc, sizeof(rsp.desc)) == 0);
    TEST_ASSERT(expected_rsp.ifm_count == rsp.ifm_count);
    TEST_ASSERT(expected_rsp.ofm_count == rsp.ofm_count);
    TEST_ASSERT(expected_rsp.status == rsp.status);
}

void testNetworkInfoNonExistantIndex(MessageClient client) {
    const uint64_t fake_user_arg     = 42;
    const uint32_t network_index     = 1;
    ethosu_core_network_info_req req = networkInfoIndexedRequest(fake_user_arg, network_index);
    ethosu_core_network_info_rsp rsp;

    TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_REQ, req));
    TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_RSP, rsp));

    TEST_ASSERT(fake_user_arg == rsp.user_arg);
    TEST_ASSERT(ETHOSU_CORE_STATUS_ERROR == rsp.status);
}

void testNetworkInfoBuffer(MessageClient client) {
    const uint64_t fake_user_arg     = 42;
    uint32_t size                    = sizeof(Model0::networkModelData);
    unsigned char *ptr               = Model0::networkModelData;
    ethosu_core_network_info_req req = networkInfoBufferRequest(fake_user_arg, ptr, size);
    ethosu_core_network_info_rsp rsp;
    ethosu_core_network_info_rsp expected_rsp = networkInfoResponse(fake_user_arg);

    TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_REQ, req));
    TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_RSP, rsp));

    TEST_ASSERT(expected_rsp.user_arg == rsp.user_arg);
    TEST_ASSERT(std::strncmp(expected_rsp.desc, rsp.desc, sizeof(rsp.desc)) == 0);
    TEST_ASSERT(expected_rsp.ifm_count == rsp.ifm_count);
    TEST_ASSERT(expected_rsp.ofm_count == rsp.ofm_count);
    TEST_ASSERT(expected_rsp.status == rsp.status);
}

void testNetworkInfoUnparsableBuffer(MessageClient client) {
    const uint64_t fake_user_arg     = 42;
    uint32_t size                    = sizeof(Model0::networkModelData) / 4;
    unsigned char *ptr               = Model0::networkModelData + size;
    ethosu_core_network_info_req req = networkInfoBufferRequest(fake_user_arg, ptr, size);
    ethosu_core_network_info_rsp rsp;

    TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_REQ, req));
    TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_NETWORK_INFO_RSP, rsp));

    TEST_ASSERT(42 == rsp.user_arg);
    TEST_ASSERT(ETHOSU_CORE_STATUS_ERROR == rsp.status);
}

void testInferenceRunIndex(MessageClient client) {
    const uint64_t fake_user_arg = 42;
    const uint32_t network_index = 0;
    uint8_t data[sizeof(expectedOutputData)];
    ethosu_core_inference_req req =
        inferenceIndexedRequest(fake_user_arg, network_index, inputData, sizeof(inputData), data, sizeof(data));
    ethosu_core_inference_rsp rsp;

    TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, req));
    TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp));

    TEST_ASSERT(req.user_arg == rsp.user_arg);
    TEST_ASSERT(rsp.ofm_count == 1);
    TEST_ASSERT(std::memcmp(expectedOutputData, data, sizeof(expectedOutputData)) == 0);
    TEST_ASSERT(rsp.status == ETHOSU_CORE_STATUS_OK);
    TEST_ASSERT(rsp.pmu_cycle_counter_enable == req.pmu_cycle_counter_enable);
    TEST_ASSERT(std::memcmp(rsp.pmu_event_config, req.pmu_event_config, sizeof(req.pmu_event_config)) == 0);
}

void testInferenceRunNonExistingIndex(MessageClient client) {
    const uint64_t fake_user_arg = 42;
    const uint32_t network_index = 1;
    uint8_t data[sizeof(expectedOutputData)];
    ethosu_core_inference_req req =
        inferenceIndexedRequest(fake_user_arg, network_index, inputData, sizeof(inputData), data, sizeof(data));
    ethosu_core_inference_rsp rsp;

    TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, req));
    TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp));

    TEST_ASSERT(req.user_arg == rsp.user_arg);
    TEST_ASSERT(rsp.status == ETHOSU_CORE_STATUS_ERROR);
}

void testInferenceRunBuffer(MessageClient client) {
    const uint64_t fake_user_arg = 42;
    uint32_t network_size        = sizeof(Model0::networkModelData);
    unsigned char *network_ptr   = Model0::networkModelData;
    uint8_t data[sizeof(expectedOutputData)];
    ethosu_core_inference_req req = inferenceBufferRequest(
        fake_user_arg, network_ptr, network_size, inputData, sizeof(inputData), data, sizeof(data));
    ethosu_core_inference_rsp rsp;

    TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, req));
    TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp));

    TEST_ASSERT(req.user_arg == rsp.user_arg);
    TEST_ASSERT(rsp.ofm_count == 1);
    TEST_ASSERT(std::memcmp(expectedOutputData, data, sizeof(expectedOutputData)) == 0);
    TEST_ASSERT(rsp.status == ETHOSU_CORE_STATUS_OK);
    TEST_ASSERT(rsp.pmu_cycle_counter_enable == req.pmu_cycle_counter_enable);
    TEST_ASSERT(std::memcmp(rsp.pmu_event_config, req.pmu_event_config, sizeof(req.pmu_event_config)) == 0);
}

void testInferenceRunUnparsableBuffer(MessageClient client) {
    const uint64_t fake_user_arg = 42;
    uint32_t network_size        = sizeof(Model0::networkModelData) / 4;
    unsigned char *network_ptr   = Model0::networkModelData + network_size;
    uint8_t data[sizeof(expectedOutputData)];
    ethosu_core_inference_req req = inferenceBufferRequest(
        fake_user_arg, network_ptr, network_size, inputData, sizeof(inputData), data, sizeof(data));
    ethosu_core_inference_rsp rsp;

    TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, req));
    TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp));

    TEST_ASSERT(req.user_arg == rsp.user_arg);
    TEST_ASSERT(rsp.status == ETHOSU_CORE_STATUS_ERROR);
}

void testSequentiallyQueuedInferenceRuns(MessageClient client) {
    int runs = 5;
    uint8_t data[runs][sizeof(expectedOutputData)];
    const uint64_t fake_user_arg = 42;
    const uint32_t network_index = 0;
    ethosu_core_inference_req req;
    ethosu_core_inference_rsp rsp[runs];

    for (int i = 0; i < runs; i++) {
        vTaskDelay(150);

        req = inferenceIndexedRequest(
            fake_user_arg + i, network_index, inputData, sizeof(inputData), data[i], sizeof(data[i]));
        TEST_ASSERT(client.sendInputMessage(ETHOSU_CORE_MSG_INFERENCE_REQ, req));
    }

    for (int i = 0; i < runs; i++) {
        TEST_ASSERT(client.waitAndReadOutputMessage(ETHOSU_CORE_MSG_INFERENCE_RSP, rsp[i]));
        TEST_ASSERT(uint64_t(fake_user_arg + i) == rsp[i].user_arg);
        TEST_ASSERT(rsp[i].ofm_count == 1);
        TEST_ASSERT(std::memcmp(expectedOutputData, data[i], sizeof(expectedOutputData)) == 0);
        TEST_ASSERT(rsp[i].status == ETHOSU_CORE_STATUS_OK);
        TEST_ASSERT(rsp[i].pmu_cycle_counter_enable == req.pmu_cycle_counter_enable);
        TEST_ASSERT(std::memcmp(rsp[i].pmu_event_config, req.pmu_event_config, sizeof(req.pmu_event_config)) == 0);
    }
}

void clientTask(void *) {
    printf("Starting client task\n");

    MessageClient client(*inputMessageQueue.toQueue(), *outputMessageQueue.toQueue(), mailbox);

    vTaskDelay(50);

    testPing(client);
    testVersion(client);
    testCapabilities(client);
    testNetworkInfoIndex(client);
    testNetworkInfoNonExistantIndex(client);
    testNetworkInfoBuffer(client);
    testNetworkInfoUnparsableBuffer(client);
    testInferenceRunIndex(client);
    testInferenceRunNonExistingIndex(client);
    testInferenceRunBuffer(client);
    testInferenceRunUnparsableBuffer(client);
    testSequentiallyQueuedInferenceRuns(client);

    exit(0);
}

/*
 * Keep task parameters as global data as FreeRTOS resets the stack when the
 * scheduler is started.
 */
TaskParams taskParams;

} // namespace

// FreeRTOS application. NOTE: Additional tasks may require increased heap size.
int main() {
    BaseType_t ret;

    if (!mailbox.verifyHardware()) {
        printf("Failed to verify mailbox hardware\n");
        return 1;
    }

    // Task for handling incoming /outgoing messages from the remote host
    ret = xTaskCreate(messageTask, "messageTask", 1024, &taskParams, 2, nullptr);
    if (ret != pdPASS) {
        printf("Failed to create 'messageTask'\n");
        return ret;
    }

    ret = xTaskCreate(inferenceTask, "inferenceTask", 8 * 1024, &taskParams, 3, nullptr);
    if (ret != pdPASS) {
        printf("Failed to create 'inferenceTask'\n");
        return ret;
    }

    // Task for handling incoming /outgoing messages from the remote host
    ret = xTaskCreate(clientTask, "clientTask", 1024, nullptr, 2, nullptr);
    if (ret != pdPASS) {
        printf("Failed to create 'messageTask'\n");
        return ret;
    }

    // Start Scheduler
    vTaskStartScheduler();

    return 1;
}