blob: af14e20b80baa60d19ce4eecd75caf64d6f50cd5 [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
Nir Ekhauz4756bf12021-06-27 11:20:24 +030057#ifndef ETHOSU_PMU_EVENT_0
58#define ETHOSU_PMU_EVENT_0 ETHOSU_PMU_CYCLE
59#endif
60#ifndef ETHOSU_PMU_EVENT_1
61#define ETHOSU_PMU_EVENT_1 ETHOSU_PMU_NPU_ACTIVE
62#endif
63#ifndef ETHOSU_PMU_EVENT_2
64#define ETHOSU_PMU_EVENT_2 ETHOSU_PMU_NO_EVENT
65#endif
66#ifndef ETHOSU_PMU_EVENT_3
67#define ETHOSU_PMU_EVENT_3 ETHOSU_PMU_NO_EVENT
68#endif
69
Jens Elofsson74eefce2021-05-25 13:47:13 +020070#ifdef ETHOSU
Jens Elofssonafadfc12021-05-26 19:49:05 +020071constexpr int32_t EventComponentNo = 0x00;
Jens Elofsson74eefce2021-05-25 13:47:13 +020072namespace {
Nir Ekhauz4756bf12021-06-27 11:20:24 +030073std::vector<ethosu_pmu_event_type> pmuEventConfig{ethosu_pmu_event_type(ETHOSU_PMU_EVENT_0),
74 ethosu_pmu_event_type(ETHOSU_PMU_EVENT_1),
75 ethosu_pmu_event_type(ETHOSU_PMU_EVENT_2),
76 ethosu_pmu_event_type(ETHOSU_PMU_EVENT_3)};
77std::vector<int32_t> eventRecMessageIds{EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_EVENT_0),
78 EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_EVENT_1),
79 EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_EVENT_2),
80 EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_EVENT_3)};
Jens Elofssonafadfc12021-05-26 19:49:05 +020081
Jens Elofsson74eefce2021-05-25 13:47:13 +020082const uint32_t delayMs = SystemCoreClock / 60ul;
83struct ethosu_driver *ethosuDrv;
Nir Ekhauz4756bf12021-06-27 11:20:24 +030084EthosUMonitor ethosuMonitor(eventRecMessageIds, EthosUMonitor::Backend::PRINTF);
Jens Elofsson74eefce2021-05-25 13:47:13 +020085} // namespace
86
87extern "C" {
88
89void SysTick_Handler(void) {
90 ethosuMonitor.monitorSample(ethosuDrv);
91}
92
93void ethosu_inference_begin(struct ethosu_driver *drv, const void *) {
94 ethosuDrv = drv;
95 ethosuMonitor.configure(drv, pmuEventConfig);
96
97 // Enable polling
98 SysTick_Config(delayMs);
99}
100
101void ethosu_inference_end(struct ethosu_driver *drv, const void *) {
102 // Disable polling
103 SysTick->CTRL = 0;
104
105 ethosuDrv = 0;
106 ethosuMonitor.monitorSample(drv);
107 ethosuMonitor.release(drv);
108}
109}
110#endif
111
Anton Moberg456566d2021-03-17 10:19:26 +0100112int runInference() {
113 // Load inference data
114 vector<InferenceProcess::DataPtr> input;
115 input.push_back(InferenceProcess::DataPtr(inputData, sizeof(inputData)));
116
117 vector<InferenceProcess::DataPtr> output;
118 output.push_back(InferenceProcess::DataPtr(outputData, sizeof(outputData)));
119
120 vector<InferenceProcess::DataPtr> expected;
121 expected.push_back(InferenceProcess::DataPtr(expectedOutputData, sizeof(expectedOutputData)));
122
123 // Create job
124 InferenceProcess::InferenceJob job(string(modelName),
125 InferenceProcess::DataPtr(networkModelData, sizeof(networkModelData)),
126 input,
127 output,
128 expected,
129 512,
130 std::vector<uint8_t>(4),
131 false);
132
133 // Run job
134 bool failed = inferenceProcess.runJob(job);
135 printf("Status of executed job: ");
136 printf(failed ? "Failed\n" : "Success\n");
137
138 return failed;
139}
140
141int main() {
Jens Elofsson74eefce2021-05-25 13:47:13 +0200142#ifdef ETHOSU
143 EventRecorderInitialize(EventRecordAll, 1);
144#endif
Anton Moberg456566d2021-03-17 10:19:26 +0100145 int ret = runInference();
146 return ret;
Jonathan Strandbergd2afc512021-03-19 10:31:18 +0100147}