blob: e394e3ad3544d616a3300cefe13ebe9eaf64bbc1 [file] [log] [blame]
Anton Moberg456566d2021-03-17 10:19:26 +01001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 *
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// NPU driver
24#include "ethosu_driver.h"
25// Inference process
26#include "inference_process.hpp"
27// System includes
28#include <stdio.h>
29#include <vector>
30
31// Model data
32#include "input.h"
33#include "model.h"
34#include "output.h"
35
Jens Elofsson74eefce2021-05-25 13:47:13 +020036#ifdef ETHOSU
37#include <ethosu_monitor.hpp>
38#include <pmu_ethosu.h>
39#endif
40
Anton Moberg456566d2021-03-17 10:19:26 +010041using namespace std;
42
43/****************************************************************************
44 * InferenceJob
45 ****************************************************************************/
46
47#ifndef TENSOR_ARENA_SIZE
48#define TENSOR_ARENA_SIZE 2000000
49#endif
50
51__attribute__((section(".bss.tensor_arena"), aligned(16))) uint8_t TFLuTensorArena[TENSOR_ARENA_SIZE];
52
53InferenceProcess::InferenceProcess inferenceProcess(TFLuTensorArena, TENSOR_ARENA_SIZE);
54
Jonathan Strandbergd2afc512021-03-19 10:31:18 +010055uint8_t outputData[sizeof(expectedOutputData)] __attribute__((aligned(16), section("output_data_sec")));
Anton Moberg456566d2021-03-17 10:19:26 +010056
Kristofer Jonsson08191ea2021-09-23 13:16:51 +020057#if !defined(ETHOSU_PMU_EVENT_0) || ETHOSU_PMU_EVENT_0 < 0 || ETHOSU_PMU_EVENT_0 >= ETHOSU_PMU_SENTINEL
58#undef ETHOSU_PMU_EVENT_0
Nir Ekhauz4756bf12021-06-27 11:20:24 +030059#define ETHOSU_PMU_EVENT_0 ETHOSU_PMU_CYCLE
60#endif
Kristofer Jonsson08191ea2021-09-23 13:16:51 +020061
62#if !defined(ETHOSU_PMU_EVENT_1) || ETHOSU_PMU_EVENT_1 < 0 || ETHOSU_PMU_EVENT_1 >= ETHOSU_PMU_SENTINEL
63#undef ETHOSU_PMU_EVENT_1
Nir Ekhauz4756bf12021-06-27 11:20:24 +030064#define ETHOSU_PMU_EVENT_1 ETHOSU_PMU_NPU_ACTIVE
65#endif
Kristofer Jonsson08191ea2021-09-23 13:16:51 +020066
67#if !defined(ETHOSU_PMU_EVENT_2) || ETHOSU_PMU_EVENT_2 < 0 || ETHOSU_PMU_EVENT_2 >= ETHOSU_PMU_SENTINEL
68#undef ETHOSU_PMU_EVENT_2
Nir Ekhauz4756bf12021-06-27 11:20:24 +030069#define ETHOSU_PMU_EVENT_2 ETHOSU_PMU_NO_EVENT
70#endif
Kristofer Jonsson08191ea2021-09-23 13:16:51 +020071
72#if !defined(ETHOSU_PMU_EVENT_3) || ETHOSU_PMU_EVENT_3 < 0 || ETHOSU_PMU_EVENT_3 >= ETHOSU_PMU_SENTINEL
73#undef ETHOSU_PMU_EVENT_3
Nir Ekhauz4756bf12021-06-27 11:20:24 +030074#define ETHOSU_PMU_EVENT_3 ETHOSU_PMU_NO_EVENT
75#endif
76
Jens Elofsson74eefce2021-05-25 13:47:13 +020077#ifdef ETHOSU
Jens Elofssonafadfc12021-05-26 19:49:05 +020078constexpr int32_t EventComponentNo = 0x00;
Jens Elofsson74eefce2021-05-25 13:47:13 +020079namespace {
Nir Ekhauz4756bf12021-06-27 11:20:24 +030080std::vector<ethosu_pmu_event_type> pmuEventConfig{ethosu_pmu_event_type(ETHOSU_PMU_EVENT_0),
81 ethosu_pmu_event_type(ETHOSU_PMU_EVENT_1),
82 ethosu_pmu_event_type(ETHOSU_PMU_EVENT_2),
83 ethosu_pmu_event_type(ETHOSU_PMU_EVENT_3)};
84std::vector<int32_t> eventRecMessageIds{EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_EVENT_0),
85 EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_EVENT_1),
86 EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_EVENT_2),
87 EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_EVENT_3)};
Jens Elofssonafadfc12021-05-26 19:49:05 +020088
Jens Elofsson74eefce2021-05-25 13:47:13 +020089const uint32_t delayMs = SystemCoreClock / 60ul;
90struct ethosu_driver *ethosuDrv;
Nir Ekhauz4756bf12021-06-27 11:20:24 +030091EthosUMonitor ethosuMonitor(eventRecMessageIds, EthosUMonitor::Backend::PRINTF);
Jens Elofsson74eefce2021-05-25 13:47:13 +020092} // namespace
93
94extern "C" {
95
96void SysTick_Handler(void) {
97 ethosuMonitor.monitorSample(ethosuDrv);
98}
99
100void ethosu_inference_begin(struct ethosu_driver *drv, const void *) {
101 ethosuDrv = drv;
102 ethosuMonitor.configure(drv, pmuEventConfig);
103
104 // Enable polling
105 SysTick_Config(delayMs);
106}
107
108void ethosu_inference_end(struct ethosu_driver *drv, const void *) {
109 // Disable polling
110 SysTick->CTRL = 0;
111
112 ethosuDrv = 0;
113 ethosuMonitor.monitorSample(drv);
114 ethosuMonitor.release(drv);
115}
116}
117#endif
118
Anton Moberg456566d2021-03-17 10:19:26 +0100119int runInference() {
120 // Load inference data
121 vector<InferenceProcess::DataPtr> input;
122 input.push_back(InferenceProcess::DataPtr(inputData, sizeof(inputData)));
123
124 vector<InferenceProcess::DataPtr> output;
125 output.push_back(InferenceProcess::DataPtr(outputData, sizeof(outputData)));
126
127 vector<InferenceProcess::DataPtr> expected;
128 expected.push_back(InferenceProcess::DataPtr(expectedOutputData, sizeof(expectedOutputData)));
129
130 // Create job
131 InferenceProcess::InferenceJob job(string(modelName),
132 InferenceProcess::DataPtr(networkModelData, sizeof(networkModelData)),
133 input,
134 output,
135 expected,
136 512,
137 std::vector<uint8_t>(4),
138 false);
139
140 // Run job
141 bool failed = inferenceProcess.runJob(job);
142 printf("Status of executed job: ");
143 printf(failed ? "Failed\n" : "Success\n");
144
145 return failed;
146}
147
148int main() {
Jens Elofsson74eefce2021-05-25 13:47:13 +0200149#ifdef ETHOSU
150 EventRecorderInitialize(EventRecordAll, 1);
151#endif
Anton Moberg456566d2021-03-17 10:19:26 +0100152 int ret = runInference();
153 return ret;
Jonathan Strandbergd2afc512021-03-19 10:31:18 +0100154}