Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
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; |
| 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 | |
| 53 | InferenceProcess::InferenceProcess inferenceProcess(TFLuTensorArena, TENSOR_ARENA_SIZE); |
| 54 | |
Jonathan Strandberg | d2afc51 | 2021-03-19 10:31:18 +0100 | [diff] [blame] | 55 | uint8_t outputData[sizeof(expectedOutputData)] __attribute__((aligned(16), section("output_data_sec"))); |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 56 | |
Kristofer Jonsson | 08191ea | 2021-09-23 13:16:51 +0200 | [diff] [blame] | 57 | #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 Ekhauz | 4756bf1 | 2021-06-27 11:20:24 +0300 | [diff] [blame] | 59 | #define ETHOSU_PMU_EVENT_0 ETHOSU_PMU_CYCLE |
| 60 | #endif |
Kristofer Jonsson | 08191ea | 2021-09-23 13:16:51 +0200 | [diff] [blame] | 61 | |
| 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 Ekhauz | 4756bf1 | 2021-06-27 11:20:24 +0300 | [diff] [blame] | 64 | #define ETHOSU_PMU_EVENT_1 ETHOSU_PMU_NPU_ACTIVE |
| 65 | #endif |
Kristofer Jonsson | 08191ea | 2021-09-23 13:16:51 +0200 | [diff] [blame] | 66 | |
| 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 Ekhauz | 4756bf1 | 2021-06-27 11:20:24 +0300 | [diff] [blame] | 69 | #define ETHOSU_PMU_EVENT_2 ETHOSU_PMU_NO_EVENT |
| 70 | #endif |
Kristofer Jonsson | 08191ea | 2021-09-23 13:16:51 +0200 | [diff] [blame] | 71 | |
| 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 Ekhauz | 4756bf1 | 2021-06-27 11:20:24 +0300 | [diff] [blame] | 74 | #define ETHOSU_PMU_EVENT_3 ETHOSU_PMU_NO_EVENT |
| 75 | #endif |
| 76 | |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 77 | #ifdef ETHOSU |
Jens Elofsson | afadfc1 | 2021-05-26 19:49:05 +0200 | [diff] [blame] | 78 | constexpr int32_t EventComponentNo = 0x00; |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 79 | namespace { |
Nir Ekhauz | 4756bf1 | 2021-06-27 11:20:24 +0300 | [diff] [blame] | 80 | std::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)}; |
| 84 | std::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 Elofsson | afadfc1 | 2021-05-26 19:49:05 +0200 | [diff] [blame] | 88 | |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 89 | const uint32_t delayMs = SystemCoreClock / 60ul; |
| 90 | struct ethosu_driver *ethosuDrv; |
Nir Ekhauz | 4756bf1 | 2021-06-27 11:20:24 +0300 | [diff] [blame] | 91 | EthosUMonitor ethosuMonitor(eventRecMessageIds, EthosUMonitor::Backend::PRINTF); |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 92 | } // namespace |
| 93 | |
| 94 | extern "C" { |
| 95 | |
| 96 | void SysTick_Handler(void) { |
| 97 | ethosuMonitor.monitorSample(ethosuDrv); |
| 98 | } |
| 99 | |
| 100 | void 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 | |
| 108 | void 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 Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 119 | int 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 | |
| 148 | int main() { |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 149 | #ifdef ETHOSU |
| 150 | EventRecorderInitialize(EventRecordAll, 1); |
| 151 | #endif |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 152 | int ret = runInference(); |
| 153 | return ret; |
Jonathan Strandberg | d2afc51 | 2021-03-19 10:31:18 +0100 | [diff] [blame] | 154 | } |