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 | |
| 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 Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 36 | #ifdef ETHOSU |
| 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 | |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 57 | #ifdef ETHOSU |
Jens Elofsson | afadfc1 | 2021-05-26 19:49:05 +0200 | [diff] [blame] | 58 | constexpr int32_t EventComponentNo = 0x00; |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 59 | namespace { |
| 60 | std::vector<ethosu_pmu_event_type> pmuEventConfig{ETHOSU_PMU_CYCLE, ETHOSU_PMU_NPU_ACTIVE}; |
Jens Elofsson | afadfc1 | 2021-05-26 19:49:05 +0200 | [diff] [blame] | 61 | std::vector<int32_t> eventRecMessageIds{EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_CYCLE), |
| 62 | EventID(EventLevelDetail, EventComponentNo, ETHOSU_PMU_NPU_ACTIVE)}; |
| 63 | |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 64 | const uint32_t delayMs = SystemCoreClock / 60ul; |
| 65 | struct ethosu_driver *ethosuDrv; |
Jens Elofsson | afadfc1 | 2021-05-26 19:49:05 +0200 | [diff] [blame] | 66 | EthosUMonitor ethosuMonitor(eventRecMessageIds, EthosUMonitor::Backend::EVENT_RECORDER); |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 67 | } // namespace |
| 68 | |
| 69 | extern "C" { |
| 70 | |
| 71 | void SysTick_Handler(void) { |
| 72 | ethosuMonitor.monitorSample(ethosuDrv); |
| 73 | } |
| 74 | |
| 75 | void ethosu_inference_begin(struct ethosu_driver *drv, const void *) { |
| 76 | ethosuDrv = drv; |
| 77 | ethosuMonitor.configure(drv, pmuEventConfig); |
| 78 | |
| 79 | // Enable polling |
| 80 | SysTick_Config(delayMs); |
| 81 | } |
| 82 | |
| 83 | void ethosu_inference_end(struct ethosu_driver *drv, const void *) { |
| 84 | // Disable polling |
| 85 | SysTick->CTRL = 0; |
| 86 | |
| 87 | ethosuDrv = 0; |
| 88 | ethosuMonitor.monitorSample(drv); |
| 89 | ethosuMonitor.release(drv); |
| 90 | } |
| 91 | } |
| 92 | #endif |
| 93 | |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 94 | int runInference() { |
| 95 | // Load inference data |
| 96 | vector<InferenceProcess::DataPtr> input; |
| 97 | input.push_back(InferenceProcess::DataPtr(inputData, sizeof(inputData))); |
| 98 | |
| 99 | vector<InferenceProcess::DataPtr> output; |
| 100 | output.push_back(InferenceProcess::DataPtr(outputData, sizeof(outputData))); |
| 101 | |
| 102 | vector<InferenceProcess::DataPtr> expected; |
| 103 | expected.push_back(InferenceProcess::DataPtr(expectedOutputData, sizeof(expectedOutputData))); |
| 104 | |
| 105 | // Create job |
| 106 | InferenceProcess::InferenceJob job(string(modelName), |
| 107 | InferenceProcess::DataPtr(networkModelData, sizeof(networkModelData)), |
| 108 | input, |
| 109 | output, |
| 110 | expected, |
| 111 | 512, |
| 112 | std::vector<uint8_t>(4), |
| 113 | false); |
| 114 | |
| 115 | // Run job |
| 116 | bool failed = inferenceProcess.runJob(job); |
| 117 | printf("Status of executed job: "); |
| 118 | printf(failed ? "Failed\n" : "Success\n"); |
| 119 | |
| 120 | return failed; |
| 121 | } |
| 122 | |
| 123 | int main() { |
Jens Elofsson | 74eefce | 2021-05-25 13:47:13 +0200 | [diff] [blame] | 124 | #ifdef ETHOSU |
| 125 | EventRecorderInitialize(EventRecordAll, 1); |
| 126 | #endif |
Anton Moberg | 456566d | 2021-03-17 10:19:26 +0100 | [diff] [blame] | 127 | int ret = runInference(); |
| 128 | return ret; |
Jonathan Strandberg | d2afc51 | 2021-03-19 10:31:18 +0100 | [diff] [blame] | 129 | } |