Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [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 | #include "tensorflow/lite/kernels/internal/compatibility.h" |
| 20 | #include "tensorflow/lite/micro/micro_error_reporter.h" |
| 21 | #include "tensorflow/lite/micro/micro_profiler.h" |
| 22 | #include "tensorflow/lite/micro/micro_time.h" |
| 23 | |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "ethosu_profiler.hpp" |
| 27 | #include <ethosu_driver.h> |
| 28 | #include <inttypes.h> |
| 29 | #include <stdio.h> |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | uint64_t GetCurrentEthosuTicks(struct ethosu_driver *drv) { |
| 34 | return ETHOSU_PMU_Get_CCNTR_v2(drv); |
| 35 | } |
| 36 | |
| 37 | void InitEthosuPMUCounters(struct ethosu_driver *drv, ethosu_pmu_event_type *ethosu_pmu_cntrs) { |
| 38 | ETHOSU_PMU_Enable_v2(drv); |
| 39 | |
| 40 | ETHOSU_PMU_CNTR_Enable_v2(drv, |
| 41 | ETHOSU_PMU_CNT1_Msk | ETHOSU_PMU_CNT2_Msk | ETHOSU_PMU_CNT3_Msk | ETHOSU_PMU_CNT4_Msk | |
| 42 | ETHOSU_PMU_CCNT_Msk); |
| 43 | |
| 44 | for (int i = 0; i < ETHOSU_PMU_NCOUNTERS; i++) { |
| 45 | ETHOSU_PMU_Set_EVTYPER_v2(drv, i, ethosu_pmu_cntrs[i]); |
| 46 | } |
| 47 | |
| 48 | ETHOSU_PMU_EVCNTR_ALL_Reset_v2(drv); |
| 49 | } |
| 50 | |
| 51 | uint32_t GetEthosuPMUEventCounter(struct ethosu_driver *drv, int counter) { |
| 52 | return ETHOSU_PMU_Get_EVCNTR_v2(drv, counter); |
| 53 | } |
| 54 | } // namespace |
| 55 | |
| 56 | namespace tflite { |
| 57 | |
| 58 | EthosUProfiler::EthosUProfiler(ethosu_pmu_event_type event0, |
| 59 | ethosu_pmu_event_type event1, |
| 60 | ethosu_pmu_event_type event2, |
| 61 | ethosu_pmu_event_type event3, |
| 62 | size_t max_events) : |
| 63 | max_events_(max_events) { |
| 64 | tags_ = std::make_unique<const char *[]>(max_events_); |
| 65 | start_ticks_ = std::make_unique<uint64_t[]>(max_events_); |
| 66 | end_ticks_ = std::make_unique<uint64_t[]>(max_events_); |
| 67 | |
| 68 | for (size_t i = 0; i < ETHOSU_PMU_NCOUNTERS; i++) { |
| 69 | event_counters[i] = 0; |
| 70 | } |
| 71 | |
| 72 | MonitorEthosuPMUEvents(event0, event1, event2, event3); |
| 73 | } |
| 74 | |
| 75 | // NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU |
| 76 | uint32_t EthosUProfiler::BeginEvent(const char *tag) { |
| 77 | if (num_events_ == max_events_) { |
| 78 | tflite::GetMicroErrorReporter()->Report("Profiling event overflow, max: %u events", max_events_); |
| 79 | num_events_ = 0; |
| 80 | } |
| 81 | |
| 82 | tags_[num_events_] = tag; |
| 83 | |
| 84 | if (strcmp("ethos-u", tag) == 0) { |
| 85 | struct ethosu_driver *ethosu_drv = ethosu_reserve_driver(); |
| 86 | ETHOSU_PMU_CYCCNT_Reset_v2(ethosu_drv); |
| 87 | ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event_v2(ethosu_drv, ETHOSU_PMU_NPU_ACTIVE); |
| 88 | ETHOSU_PMU_PMCCNTR_CFG_Set_Stop_Event_v2(ethosu_drv, ETHOSU_PMU_NPU_IDLE); |
| 89 | start_ticks_[num_events_] = GetCurrentEthosuTicks(ethosu_drv); |
| 90 | InitEthosuPMUCounters(ethosu_drv, ethosu_pmu_cntrs); |
| 91 | ethosu_release_driver(ethosu_drv); |
| 92 | } else { |
| 93 | start_ticks_[num_events_] = GetCurrentTimeTicks(); |
| 94 | } |
| 95 | |
| 96 | end_ticks_[num_events_] = start_ticks_[num_events_] - 1; |
| 97 | return num_events_++; |
| 98 | } |
| 99 | |
| 100 | // NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU |
| 101 | void EthosUProfiler::EndEvent(uint32_t event_handle) { |
| 102 | TFLITE_DCHECK(event_handle < max_events_); |
| 103 | |
| 104 | if (strcmp("ethos-u", tags_[event_handle]) == 0) { |
| 105 | struct ethosu_driver *ethosu_drv = ethosu_reserve_driver(); |
| 106 | end_ticks_[event_handle] = GetCurrentEthosuTicks(ethosu_drv); |
| 107 | uint32_t ethosu_pmu_counter_end[ETHOSU_PMU_NCOUNTERS]; |
| 108 | ETHOSU_PMU_Disable_v2(ethosu_drv); |
| 109 | for (size_t i = 0; i < ETHOSU_PMU_NCOUNTERS; i++) { |
| 110 | ethosu_pmu_counter_end[i] = GetEthosuPMUEventCounter(ethosu_drv, i); |
| 111 | tflite::GetMicroErrorReporter()->Report( |
| 112 | "%s : ethosu_pmu_cntr%d : %u", tags_[event_handle], i, ethosu_pmu_counter_end[i]); |
| 113 | |
| 114 | event_counters[i] += ethosu_pmu_counter_end[i]; |
| 115 | } |
| 116 | ethosu_release_driver(ethosu_drv); |
| 117 | printf("%s : cycle_cnt : %" PRIu64 " cycles\n", |
| 118 | tags_[event_handle], |
| 119 | end_ticks_[event_handle] - start_ticks_[event_handle]); |
| 120 | |
| 121 | } else { |
| 122 | end_ticks_[event_handle] = GetCurrentTimeTicks(); |
| 123 | printf("%s : cycle_cnt : %" PRIu64 " cycles\n", |
| 124 | tags_[event_handle], |
| 125 | end_ticks_[event_handle] - start_ticks_[event_handle]); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | uint64_t EthosUProfiler::GetTotalTicks() const { |
| 130 | uint64_t ticks = 0; |
| 131 | for (int i = 0; i < num_events_; ++i) { |
| 132 | ticks += end_ticks_[i] - start_ticks_[i]; |
| 133 | } |
| 134 | |
| 135 | return ticks; |
| 136 | } |
| 137 | |
| 138 | void EthosUProfiler::Log() const { |
| 139 | #if !defined(TF_LITE_STRIP_ERROR_STRINGS) |
| 140 | for (int i = 0; i < num_events_; ++i) { |
| 141 | uint64_t ticks = end_ticks_[i] - start_ticks_[i]; |
| 142 | printf("%s took %" PRIu64 " cycles\n", tags_[i], ticks); |
| 143 | } |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | void EthosUProfiler::MonitorEthosuPMUEvents(ethosu_pmu_event_type event0, |
| 148 | ethosu_pmu_event_type event1, |
| 149 | ethosu_pmu_event_type event2, |
| 150 | ethosu_pmu_event_type event3) { |
| 151 | ethosu_pmu_cntrs[0] = event0; |
| 152 | ethosu_pmu_cntrs[1] = event1; |
| 153 | ethosu_pmu_cntrs[2] = event2; |
| 154 | ethosu_pmu_cntrs[3] = event3; |
| 155 | } |
| 156 | |
| 157 | uint32_t EthosUProfiler::GetEthosuPMUCounter(int counter) { |
| 158 | return event_counters[counter]; |
| 159 | } |
| 160 | |
| 161 | } // namespace tflite |