blob: 6ed7cbeee17cf6fd6488d4283d98fc496d1c25d1 [file] [log] [blame]
Anton Moberg456566d2021-03-17 10:19:26 +01001/*
Kristofer Jonsson5410db12022-01-27 17:39:06 +01002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
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 Jonssone56b6e42022-09-29 11:52:22 +020081const uint32_t delayMs = SystemCoreClock / 1000ul;
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) {
89 ethosuMonitor.monitorSample(ethosuDrv);
90}
91
Kristofer Jonsson5410db12022-01-27 17:39:06 +010092void ethosu_inference_begin(struct ethosu_driver *drv, void *) {
Jens Elofsson74eefce2021-05-25 13:47:13 +020093 ethosuDrv = drv;
94 ethosuMonitor.configure(drv, pmuEventConfig);
95
96 // Enable polling
97 SysTick_Config(delayMs);
98}
99
Kristofer Jonsson5410db12022-01-27 17:39:06 +0100100void ethosu_inference_end(struct ethosu_driver *drv, void *) {
Jens Elofsson74eefce2021-05-25 13:47:13 +0200101 // Disable polling
102 SysTick->CTRL = 0;
103
104 ethosuDrv = 0;
105 ethosuMonitor.monitorSample(drv);
106 ethosuMonitor.release(drv);
107}
108}
109#endif
110
Anton Moberg456566d2021-03-17 10:19:26 +0100111int runInference() {
Anton Moberg456566d2021-03-17 10:19:26 +0100112 // Create job
Kristofer Jonsson5410db12022-01-27 17:39:06 +0100113 InferenceJob job(string(modelName),
114 DataPtr(networkModelData, sizeof(networkModelData)),
115 {DataPtr(inputData, sizeof(inputData))},
116 {DataPtr(outputData, sizeof(outputData))},
117 {DataPtr(expectedOutputData, sizeof(expectedOutputData))},
118 512);
Anton Moberg456566d2021-03-17 10:19:26 +0100119
120 // Run job
121 bool failed = inferenceProcess.runJob(job);
122 printf("Status of executed job: ");
123 printf(failed ? "Failed\n" : "Success\n");
124
125 return failed;
126}
127
128int main() {
Jens Elofsson74eefce2021-05-25 13:47:13 +0200129#ifdef ETHOSU
130 EventRecorderInitialize(EventRecordAll, 1);
131#endif
Anton Moberg456566d2021-03-17 10:19:26 +0100132 int ret = runInference();
133 return ret;
Jonathan Strandbergd2afc512021-03-19 10:31:18 +0100134}