blob: 20bc756f9a6a3475971fcda07110a752e8745091 [file] [log] [blame]
Anton Moberg456566d2021-03-17 10:19:26 +01001/*
Kristofer Jonsson00b8e7b2023-02-16 11:34:52 +01002 * SPDX-FileCopyrightText: Copyright 2021-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
Anton Moberg456566d2021-03-17 10:19:26 +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
Anton Moberg456566d2021-03-17 10:19:26 +010023// Inference process
24#include "inference_process.hpp"
Kristofer Jonsson15625f12021-11-24 14:16:34 +010025
Anton Moberg456566d2021-03-17 10:19:26 +010026// System includes
27#include <stdio.h>
28#include <vector>
29
30// Model data
31#include "input.h"
32#include "model.h"
33#include "output.h"
34
Jens Elofsson74eefce2021-05-25 13:47:13 +020035#ifdef ETHOSU
Kristofer Jonsson15625f12021-11-24 14:16:34 +010036#include <ethosu_driver.h>
Jens Elofsson74eefce2021-05-25 13:47:13 +020037#include <ethosu_monitor.hpp>
38#include <pmu_ethosu.h>
39#endif
40
Anton Moberg456566d2021-03-17 10:19:26 +010041using namespace std;
Kristofer Jonsson5410db12022-01-27 17:39:06 +010042using namespace InferenceProcess;
Anton Moberg456566d2021-03-17 10:19:26 +010043
44/****************************************************************************
45 * InferenceJob
46 ****************************************************************************/
47
Davide Grohmann5508acb2022-08-08 17:12:08 +020048__attribute__((section(".bss.tensor_arena"), aligned(16))) uint8_t TFLuTensorArena[tensorArenaSize];
Anton Moberg456566d2021-03-17 10:19:26 +010049
Davide Grohmann5508acb2022-08-08 17:12:08 +020050class InferenceProcess inferenceProcess(TFLuTensorArena, tensorArenaSize);
Anton Moberg456566d2021-03-17 10:19:26 +010051
Jonathan Strandbergd2afc512021-03-19 10:31:18 +010052uint8_t outputData[sizeof(expectedOutputData)] __attribute__((aligned(16), section("output_data_sec")));
Anton Moberg456566d2021-03-17 10:19:26 +010053
Kristofer Jonsson08191ea2021-09-23 13:16:51 +020054#if !defined(ETHOSU_PMU_EVENT_0) || ETHOSU_PMU_EVENT_0 < 0 || ETHOSU_PMU_EVENT_0 >= ETHOSU_PMU_SENTINEL
55#undef ETHOSU_PMU_EVENT_0
Nir Ekhauz4756bf12021-06-27 11:20:24 +030056#define ETHOSU_PMU_EVENT_0 ETHOSU_PMU_CYCLE
57#endif
Kristofer Jonsson08191ea2021-09-23 13:16:51 +020058
59#if !defined(ETHOSU_PMU_EVENT_1) || ETHOSU_PMU_EVENT_1 < 0 || ETHOSU_PMU_EVENT_1 >= ETHOSU_PMU_SENTINEL
60#undef ETHOSU_PMU_EVENT_1
Nir Ekhauz4756bf12021-06-27 11:20:24 +030061#define ETHOSU_PMU_EVENT_1 ETHOSU_PMU_NPU_ACTIVE
62#endif
Kristofer Jonsson08191ea2021-09-23 13:16:51 +020063
64#if !defined(ETHOSU_PMU_EVENT_2) || ETHOSU_PMU_EVENT_2 < 0 || ETHOSU_PMU_EVENT_2 >= ETHOSU_PMU_SENTINEL
65#undef ETHOSU_PMU_EVENT_2
Nir Ekhauz4756bf12021-06-27 11:20:24 +030066#define ETHOSU_PMU_EVENT_2 ETHOSU_PMU_NO_EVENT
67#endif
Kristofer Jonsson08191ea2021-09-23 13:16:51 +020068
69#if !defined(ETHOSU_PMU_EVENT_3) || ETHOSU_PMU_EVENT_3 < 0 || ETHOSU_PMU_EVENT_3 >= ETHOSU_PMU_SENTINEL
70#undef ETHOSU_PMU_EVENT_3
Nir Ekhauz4756bf12021-06-27 11:20:24 +030071#define ETHOSU_PMU_EVENT_3 ETHOSU_PMU_NO_EVENT
72#endif
73
Jens Elofsson74eefce2021-05-25 13:47:13 +020074#ifdef ETHOSU
75namespace {
Nir Ekhauz4756bf12021-06-27 11:20:24 +030076std::vector<ethosu_pmu_event_type> pmuEventConfig{ethosu_pmu_event_type(ETHOSU_PMU_EVENT_0),
77 ethosu_pmu_event_type(ETHOSU_PMU_EVENT_1),
78 ethosu_pmu_event_type(ETHOSU_PMU_EVENT_2),
79 ethosu_pmu_event_type(ETHOSU_PMU_EVENT_3)};
Jens Elofssonafadfc12021-05-26 19:49:05 +020080
Kristofer Jonsson4646ed12022-12-05 14:41:13 +010081const uint32_t delay = 250ul;
Jens Elofsson74eefce2021-05-25 13:47:13 +020082struct ethosu_driver *ethosuDrv;
Kristofer Jonssone56b6e42022-09-29 11:52:22 +020083EthosUMonitor ethosuMonitor(EthosUMonitor::Backend::EVENT_RECORDER);
Jens Elofsson74eefce2021-05-25 13:47:13 +020084} // namespace
85
86extern "C" {
87
88void SysTick_Handler(void) {
Kristofer Jonsson4646ed12022-12-05 14:41:13 +010089 // Disable systick, preventing new systick interrupt to fire while we call the Ethos-U monitor
90 SysTick->CTRL = 0;
91
Kristofer Jonsson00b8e7b2023-02-16 11:34:52 +010092 if (ethosuDrv != nullptr) {
93 ethosuMonitor.monitorSample(ethosuDrv);
Kristofer Jonsson4646ed12022-12-05 14:41:13 +010094
Kristofer Jonsson00b8e7b2023-02-16 11:34:52 +010095 // Restart the systick timer
96 SysTick_Config(delay);
97 }
Jens Elofsson74eefce2021-05-25 13:47:13 +020098}
99
Kristofer Jonsson5410db12022-01-27 17:39:06 +0100100void ethosu_inference_begin(struct ethosu_driver *drv, void *) {
Jens Elofsson74eefce2021-05-25 13:47:13 +0200101 ethosuDrv = drv;
102 ethosuMonitor.configure(drv, pmuEventConfig);
Kristofer Jonsson4646ed12022-12-05 14:41:13 +0100103 SysTick_Config(delay);
Jens Elofsson74eefce2021-05-25 13:47:13 +0200104}
105
Kristofer Jonsson5410db12022-01-27 17:39:06 +0100106void ethosu_inference_end(struct ethosu_driver *drv, void *) {
Jens Elofsson74eefce2021-05-25 13:47:13 +0200107 // Disable polling
108 SysTick->CTRL = 0;
109
Kristofer Jonsson00b8e7b2023-02-16 11:34:52 +0100110 ethosuDrv = nullptr;
Jens Elofsson74eefce2021-05-25 13:47:13 +0200111 ethosuMonitor.monitorSample(drv);
112 ethosuMonitor.release(drv);
113}
114}
115#endif
116
Anton Moberg456566d2021-03-17 10:19:26 +0100117int runInference() {
Anton Moberg456566d2021-03-17 10:19:26 +0100118 // Create job
Kristofer Jonsson5410db12022-01-27 17:39:06 +0100119 InferenceJob job(string(modelName),
120 DataPtr(networkModelData, sizeof(networkModelData)),
121 {DataPtr(inputData, sizeof(inputData))},
122 {DataPtr(outputData, sizeof(outputData))},
123 {DataPtr(expectedOutputData, sizeof(expectedOutputData))},
124 512);
Anton Moberg456566d2021-03-17 10:19:26 +0100125
126 // Run job
127 bool failed = inferenceProcess.runJob(job);
128 printf("Status of executed job: ");
129 printf(failed ? "Failed\n" : "Success\n");
Kristofer Jonsson4646ed12022-12-05 14:41:13 +0100130 printf("Performance monitor merge count %zu\n", ethosuMonitor.getMergeCount());
Anton Moberg456566d2021-03-17 10:19:26 +0100131
132 return failed;
133}
134
135int main() {
Jens Elofsson74eefce2021-05-25 13:47:13 +0200136#ifdef ETHOSU
137 EventRecorderInitialize(EventRecordAll, 1);
138#endif
Anton Moberg456566d2021-03-17 10:19:26 +0100139 int ret = runInference();
140 return ret;
Jonathan Strandbergd2afc512021-03-19 10:31:18 +0100141}