blob: e9b398a0903b9c91d12671b157f847f275590a31 [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
48#ifndef TENSOR_ARENA_SIZE
49#define TENSOR_ARENA_SIZE 2000000
50#endif
51
52__attribute__((section(".bss.tensor_arena"), aligned(16))) uint8_t TFLuTensorArena[TENSOR_ARENA_SIZE];
53
Kristofer Jonsson5410db12022-01-27 17:39:06 +010054class InferenceProcess inferenceProcess(TFLuTensorArena, TENSOR_ARENA_SIZE);
Anton Moberg456566d2021-03-17 10:19:26 +010055
Jonathan Strandbergd2afc512021-03-19 10:31:18 +010056uint8_t outputData[sizeof(expectedOutputData)] __attribute__((aligned(16), section("output_data_sec")));
Anton Moberg456566d2021-03-17 10:19:26 +010057
Kristofer Jonsson08191ea2021-09-23 13:16:51 +020058#if !defined(ETHOSU_PMU_EVENT_0) || ETHOSU_PMU_EVENT_0 < 0 || ETHOSU_PMU_EVENT_0 >= ETHOSU_PMU_SENTINEL
59#undef ETHOSU_PMU_EVENT_0
Nir Ekhauz4756bf12021-06-27 11:20:24 +030060#define ETHOSU_PMU_EVENT_0 ETHOSU_PMU_CYCLE
61#endif
Kristofer Jonsson08191ea2021-09-23 13:16:51 +020062
63#if !defined(ETHOSU_PMU_EVENT_1) || ETHOSU_PMU_EVENT_1 < 0 || ETHOSU_PMU_EVENT_1 >= ETHOSU_PMU_SENTINEL
64#undef ETHOSU_PMU_EVENT_1
Nir Ekhauz4756bf12021-06-27 11:20:24 +030065#define ETHOSU_PMU_EVENT_1 ETHOSU_PMU_NPU_ACTIVE
66#endif
Kristofer Jonsson08191ea2021-09-23 13:16:51 +020067
68#if !defined(ETHOSU_PMU_EVENT_2) || ETHOSU_PMU_EVENT_2 < 0 || ETHOSU_PMU_EVENT_2 >= ETHOSU_PMU_SENTINEL
69#undef ETHOSU_PMU_EVENT_2
Nir Ekhauz4756bf12021-06-27 11:20:24 +030070#define ETHOSU_PMU_EVENT_2 ETHOSU_PMU_NO_EVENT
71#endif
Kristofer Jonsson08191ea2021-09-23 13:16:51 +020072
73#if !defined(ETHOSU_PMU_EVENT_3) || ETHOSU_PMU_EVENT_3 < 0 || ETHOSU_PMU_EVENT_3 >= ETHOSU_PMU_SENTINEL
74#undef ETHOSU_PMU_EVENT_3
Nir Ekhauz4756bf12021-06-27 11:20:24 +030075#define ETHOSU_PMU_EVENT_3 ETHOSU_PMU_NO_EVENT
76#endif
77
Jens Elofsson74eefce2021-05-25 13:47:13 +020078#ifdef ETHOSU
Jens Elofssonafadfc12021-05-26 19:49:05 +020079constexpr int32_t EventComponentNo = 0x00;
Jens Elofsson74eefce2021-05-25 13:47:13 +020080namespace {
Nir Ekhauz4756bf12021-06-27 11:20:24 +030081std::vector<ethosu_pmu_event_type> pmuEventConfig{ethosu_pmu_event_type(ETHOSU_PMU_EVENT_0),
82 ethosu_pmu_event_type(ETHOSU_PMU_EVENT_1),
83 ethosu_pmu_event_type(ETHOSU_PMU_EVENT_2),
84 ethosu_pmu_event_type(ETHOSU_PMU_EVENT_3)};
85std::vector<int32_t> eventRecMessageIds{EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_EVENT_0),
86 EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_EVENT_1),
87 EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_EVENT_2),
88 EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_EVENT_3)};
Jens Elofssonafadfc12021-05-26 19:49:05 +020089
Jens Elofsson74eefce2021-05-25 13:47:13 +020090const uint32_t delayMs = SystemCoreClock / 60ul;
91struct ethosu_driver *ethosuDrv;
Nir Ekhauz4756bf12021-06-27 11:20:24 +030092EthosUMonitor ethosuMonitor(eventRecMessageIds, EthosUMonitor::Backend::PRINTF);
Jens Elofsson74eefce2021-05-25 13:47:13 +020093} // namespace
94
95extern "C" {
96
97void SysTick_Handler(void) {
98 ethosuMonitor.monitorSample(ethosuDrv);
99}
100
Kristofer Jonsson5410db12022-01-27 17:39:06 +0100101void ethosu_inference_begin(struct ethosu_driver *drv, void *) {
Jens Elofsson74eefce2021-05-25 13:47:13 +0200102 ethosuDrv = drv;
103 ethosuMonitor.configure(drv, pmuEventConfig);
104
105 // Enable polling
106 SysTick_Config(delayMs);
107}
108
Kristofer Jonsson5410db12022-01-27 17:39:06 +0100109void ethosu_inference_end(struct ethosu_driver *drv, void *) {
Jens Elofsson74eefce2021-05-25 13:47:13 +0200110 // Disable polling
111 SysTick->CTRL = 0;
112
113 ethosuDrv = 0;
114 ethosuMonitor.monitorSample(drv);
115 ethosuMonitor.release(drv);
116}
117}
118#endif
119
Anton Moberg456566d2021-03-17 10:19:26 +0100120int runInference() {
Anton Moberg456566d2021-03-17 10:19:26 +0100121 // Create job
Kristofer Jonsson5410db12022-01-27 17:39:06 +0100122 InferenceJob job(string(modelName),
123 DataPtr(networkModelData, sizeof(networkModelData)),
124 {DataPtr(inputData, sizeof(inputData))},
125 {DataPtr(outputData, sizeof(outputData))},
126 {DataPtr(expectedOutputData, sizeof(expectedOutputData))},
127 512);
Anton Moberg456566d2021-03-17 10:19:26 +0100128
129 // Run job
130 bool failed = inferenceProcess.runJob(job);
131 printf("Status of executed job: ");
132 printf(failed ? "Failed\n" : "Success\n");
133
134 return failed;
135}
136
137int main() {
Jens Elofsson74eefce2021-05-25 13:47:13 +0200138#ifdef ETHOSU
139 EventRecorderInitialize(EventRecordAll, 1);
140#endif
Anton Moberg456566d2021-03-17 10:19:26 +0100141 int ret = runInference();
142 return ret;
Jonathan Strandbergd2afc512021-03-19 10:31:18 +0100143}