Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 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 | |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 23 | // Inference process |
| 24 | #include "inference_process.hpp" |
Kristofer Jonsson | 15625f1 | 2021-11-24 14:16:34 +0100 | [diff] [blame] | 25 | |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 26 | // 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 Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 35 | #ifdef ETHOSU |
Kristofer Jonsson | 15625f1 | 2021-11-24 14:16:34 +0100 | [diff] [blame] | 36 | #include <ethosu_driver.h> |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 37 | #include <ethosu_monitor.hpp> |
| 38 | #include <pmu_ethosu.h> |
| 39 | #endif |
| 40 | |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 41 | using namespace std; |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 42 | using namespace InferenceProcess; |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 43 | |
| 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 Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 54 | class InferenceProcess inferenceProcess(TFLuTensorArena, TENSOR_ARENA_SIZE); |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 55 | |
Jonathan Strandberg | d2afc51 | 2021-03-19 10:31:18 +0100 | [diff] [blame] | 56 | uint8_t outputData[sizeof(expectedOutputData)] __attribute__((aligned(16), section("output_data_sec"))); |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 57 | |
Kristofer Jonsson | 08191ea | 2021-09-23 13:16:51 +0200 | [diff] [blame] | 58 | #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 Ekhauz | 4756bf1 | 2021-06-27 11:20:24 +0300 | [diff] [blame] | 60 | #define ETHOSU_PMU_EVENT_0 ETHOSU_PMU_CYCLE |
| 61 | #endif |
Kristofer Jonsson | 08191ea | 2021-09-23 13:16:51 +0200 | [diff] [blame] | 62 | |
| 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 Ekhauz | 4756bf1 | 2021-06-27 11:20:24 +0300 | [diff] [blame] | 65 | #define ETHOSU_PMU_EVENT_1 ETHOSU_PMU_NPU_ACTIVE |
| 66 | #endif |
Kristofer Jonsson | 08191ea | 2021-09-23 13:16:51 +0200 | [diff] [blame] | 67 | |
| 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 Ekhauz | 4756bf1 | 2021-06-27 11:20:24 +0300 | [diff] [blame] | 70 | #define ETHOSU_PMU_EVENT_2 ETHOSU_PMU_NO_EVENT |
| 71 | #endif |
Kristofer Jonsson | 08191ea | 2021-09-23 13:16:51 +0200 | [diff] [blame] | 72 | |
| 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 Ekhauz | 4756bf1 | 2021-06-27 11:20:24 +0300 | [diff] [blame] | 75 | #define ETHOSU_PMU_EVENT_3 ETHOSU_PMU_NO_EVENT |
| 76 | #endif |
| 77 | |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 78 | #ifdef ETHOSU |
Jens Elofsson | afadfc1 | 2021-05-26 19:49:05 +0200 | [diff] [blame] | 79 | constexpr int32_t EventComponentNo = 0x00; |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 80 | namespace { |
Nir Ekhauz | 4756bf1 | 2021-06-27 11:20:24 +0300 | [diff] [blame] | 81 | std::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)}; |
| 85 | std::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 Elofsson | afadfc1 | 2021-05-26 19:49:05 +0200 | [diff] [blame] | 89 | |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 90 | const uint32_t delayMs = SystemCoreClock / 60ul; |
| 91 | struct ethosu_driver *ethosuDrv; |
Nir Ekhauz | 4756bf1 | 2021-06-27 11:20:24 +0300 | [diff] [blame] | 92 | EthosUMonitor ethosuMonitor(eventRecMessageIds, EthosUMonitor::Backend::PRINTF); |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 93 | } // namespace |
| 94 | |
| 95 | extern "C" { |
| 96 | |
| 97 | void SysTick_Handler(void) { |
| 98 | ethosuMonitor.monitorSample(ethosuDrv); |
| 99 | } |
| 100 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 101 | void ethosu_inference_begin(struct ethosu_driver *drv, void *) { |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 102 | ethosuDrv = drv; |
| 103 | ethosuMonitor.configure(drv, pmuEventConfig); |
| 104 | |
| 105 | // Enable polling |
| 106 | SysTick_Config(delayMs); |
| 107 | } |
| 108 | |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 109 | void ethosu_inference_end(struct ethosu_driver *drv, void *) { |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 110 | // Disable polling |
| 111 | SysTick->CTRL = 0; |
| 112 | |
| 113 | ethosuDrv = 0; |
| 114 | ethosuMonitor.monitorSample(drv); |
| 115 | ethosuMonitor.release(drv); |
| 116 | } |
| 117 | } |
| 118 | #endif |
| 119 | |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 120 | int runInference() { |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 121 | // Create job |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 122 | 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 Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 128 | |
| 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 | |
| 137 | int main() { |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 138 | #ifdef ETHOSU |
| 139 | EventRecorderInitialize(EventRecordAll, 1); |
| 140 | #endif |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 141 | int ret = runInference(); |
| 142 | return ret; |
Jonathan Strandberg | d2afc51 | 2021-03-19 10:31:18 +0100 | [diff] [blame] | 143 | } |