blob: 4e586468fe225550c7e358bff5481eca615afb58 [file] [log] [blame]
Kristofer Jonsson43ce4912020-11-20 09:42:53 +01001/*
Per Åstrand81e01af2021-02-19 13:45:26 +01002 * Copyright (c) 2020-2021 Arm Limited. All rights reserved.
Kristofer Jonsson43ce4912020-11-20 09:42:53 +01003 *
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// FreeRTOS
24#include "FreeRTOS.h"
25#include "queue.h"
26#include "task.h"
27
28// Ethos-U
29#include "ethosu_driver.h"
30#include "inference_process.hpp"
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010031
32// System includes
33#include <stdio.h>
34
35using namespace std;
36using namespace InferenceProcess;
37
38/****************************************************************************
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010039 * InferenceJob
40 ****************************************************************************/
41
Per Åstrand81e01af2021-02-19 13:45:26 +010042#define TENSOR_ARENA_SIZE 0xa0000
Anton Moberg1a679c42021-02-10 08:45:39 +010043
44__attribute__((section(".bss.NoInit"), aligned(16))) uint8_t inferenceProcessTensorArena[TENSOR_ARENA_SIZE];
45
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010046namespace {
47
48struct xInferenceJob : public InferenceJob {
49 QueueHandle_t queue;
50 bool status;
51
52 xInferenceJob();
53 xInferenceJob(const string &name,
54 const DataPtr &networkModel,
55 const vector<DataPtr> &input,
56 const vector<DataPtr> &output,
57 const vector<DataPtr> &expectedOutput,
58 size_t numBytesToPrint,
Bhavik Patel66636302020-12-08 12:08:11 +010059 const vector<uint8_t> &pmuEventConfig,
60 const uint32_t pmuCycleCounterEnable,
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010061 QueueHandle_t queue);
62};
63
64xInferenceJob::xInferenceJob() : InferenceJob(), queue(nullptr), status(false) {}
65
66xInferenceJob::xInferenceJob(const std::string &_name,
67 const DataPtr &_networkModel,
68 const std::vector<DataPtr> &_input,
69 const std::vector<DataPtr> &_output,
70 const std::vector<DataPtr> &_expectedOutput,
71 size_t _numBytesToPrint,
Bhavik Patel66636302020-12-08 12:08:11 +010072 const vector<uint8_t> &_pmuEventConfig,
73 const uint32_t _pmuCycleCounterEnable,
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010074 QueueHandle_t _queue) :
Bhavik Patel66636302020-12-08 12:08:11 +010075 InferenceJob(_name,
76 _networkModel,
77 _input,
78 _output,
79 _expectedOutput,
80 _numBytesToPrint,
81 _pmuEventConfig,
82 _pmuCycleCounterEnable),
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010083 queue(_queue), status(false) {}
84
85} // namespace
86
87/****************************************************************************
88 * Functions
89 ****************************************************************************/
90
91namespace {
92
Per Åstrand81e01af2021-02-19 13:45:26 +010093#include "model.h"
94#include "input.h"
95#include "output.h"
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010096
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010097void inferenceProcessTask(void *pvParameters) {
98 QueueHandle_t queue = reinterpret_cast<QueueHandle_t>(pvParameters);
99
Anton Moberg1a679c42021-02-10 08:45:39 +0100100 class InferenceProcess inferenceProcess(inferenceProcessTensorArena, TENSOR_ARENA_SIZE);
Kristofer Jonsson43ce4912020-11-20 09:42:53 +0100101
102 while (true) {
103 xInferenceJob *job;
104
105 // Wait for inference job
106 xQueueReceive(queue, &job, portMAX_DELAY);
107 printf("Received inference job. job=%p, name=%s\n", job, job->name.c_str());
108
109 bool status = inferenceProcess.runJob(*job);
110 job->status = status;
111
112 // Return inference job response
113 xQueueSend(job->queue, &job, portMAX_DELAY);
114 }
115
116 vTaskDelete(NULL);
117}
118
119void inferenceJobTask(void *pvParameters) {
120 QueueHandle_t inferenceProcessQueue = reinterpret_cast<QueueHandle_t>(pvParameters);
121
122 // Create queue for response messages
123 QueueHandle_t senderQueue = xQueueCreate(10, sizeof(xInferenceJob *));
124
125 // Inference job
126 DataPtr networkModel(networkModelData, sizeof(networkModelData));
127 DataPtr input(inputData, sizeof(inputData));
128 DataPtr expected(expectedData, sizeof(expectedData));
129
130 xInferenceJob job;
131 xInferenceJob *j = &job;
Per Åstrand81e01af2021-02-19 13:45:26 +0100132 job.name = "mobilenet_v2";
Kristofer Jonsson43ce4912020-11-20 09:42:53 +0100133 job.networkModel = networkModel;
134 job.input.push_back(input);
135 job.expectedOutput.push_back(expected);
136 job.queue = senderQueue;
137
138 // Send job
139 printf("Sending inference job\n");
140 xQueueSend(inferenceProcessQueue, &j, portMAX_DELAY);
141
142 // Wait for response
143 xQueueReceive(senderQueue, &j, portMAX_DELAY);
144 printf("Received inference job response. status=%u\n", j->status);
145
Kristofer Jonsson715c07b2021-02-25 09:49:34 +0100146 exit(j->status);
Kristofer Jonsson43ce4912020-11-20 09:42:53 +0100147}
148
149} // namespace
150
Per Åstrand4d612752021-02-22 09:50:54 +0100151/* Keep the queue ouf of the stack sinde freertos resets it when the scheduler starts.*/
152QueueHandle_t inferenceProcessQueue;
153
Kristofer Jonsson43ce4912020-11-20 09:42:53 +0100154int main() {
Kristofer Jonsson43ce4912020-11-20 09:42:53 +0100155 // Inference process
Per Åstrand4d612752021-02-22 09:50:54 +0100156 inferenceProcessQueue = xQueueCreate(10, sizeof(xInferenceJob *));
Kristofer Jonsson43ce4912020-11-20 09:42:53 +0100157 xTaskCreate(inferenceProcessTask, "inferenceProcess", 2 * 1024, inferenceProcessQueue, 1, nullptr);
158
159 // Inference job task
160 xTaskCreate(inferenceJobTask, "inferenceJob", 2 * 1024, inferenceProcessQueue, 2, nullptr);
161
162 // Run the scheduler
163 vTaskStartScheduler();
164
165 return 0;
166}