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) { |
Anton Moberg | 596a466 | 2021-05-24 09:16:07 +0200 | [diff] [blame^] | 34 | return ETHOSU_PMU_Get_CCNTR(drv); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 37 | } // namespace |
| 38 | |
| 39 | namespace tflite { |
| 40 | |
Jens Elofsson | de044c3 | 2021-05-06 16:21:29 +0200 | [diff] [blame] | 41 | EthosUProfiler::EthosUProfiler(size_t max_events) : max_events_(max_events) { |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 42 | tags_ = std::make_unique<const char *[]>(max_events_); |
| 43 | start_ticks_ = std::make_unique<uint64_t[]>(max_events_); |
| 44 | end_ticks_ = std::make_unique<uint64_t[]>(max_events_); |
| 45 | |
Jens Elofsson | de044c3 | 2021-05-06 16:21:29 +0200 | [diff] [blame] | 46 | struct ethosu_driver *drv = ethosu_reserve_driver(); |
Anton Moberg | 596a466 | 2021-05-24 09:16:07 +0200 | [diff] [blame^] | 47 | ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk); |
| 48 | ETHOSU_PMU_CYCCNT_Reset(drv); |
Jens Elofsson | de044c3 | 2021-05-06 16:21:29 +0200 | [diff] [blame] | 49 | ethosu_release_driver(drv); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU |
| 53 | uint32_t EthosUProfiler::BeginEvent(const char *tag) { |
| 54 | if (num_events_ == max_events_) { |
| 55 | tflite::GetMicroErrorReporter()->Report("Profiling event overflow, max: %u events", max_events_); |
| 56 | num_events_ = 0; |
| 57 | } |
| 58 | |
| 59 | tags_[num_events_] = tag; |
| 60 | |
| 61 | if (strcmp("ethos-u", tag) == 0) { |
| 62 | struct ethosu_driver *ethosu_drv = ethosu_reserve_driver(); |
Anton Moberg | 596a466 | 2021-05-24 09:16:07 +0200 | [diff] [blame^] | 63 | ETHOSU_PMU_CYCCNT_Reset(ethosu_drv); |
| 64 | ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event(ethosu_drv, ETHOSU_PMU_NPU_ACTIVE); |
| 65 | ETHOSU_PMU_PMCCNTR_CFG_Set_Stop_Event(ethosu_drv, ETHOSU_PMU_NPU_IDLE); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 66 | start_ticks_[num_events_] = GetCurrentEthosuTicks(ethosu_drv); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 67 | ethosu_release_driver(ethosu_drv); |
| 68 | } else { |
| 69 | start_ticks_[num_events_] = GetCurrentTimeTicks(); |
| 70 | } |
| 71 | |
| 72 | end_ticks_[num_events_] = start_ticks_[num_events_] - 1; |
| 73 | return num_events_++; |
| 74 | } |
| 75 | |
| 76 | // NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU |
| 77 | void EthosUProfiler::EndEvent(uint32_t event_handle) { |
| 78 | TFLITE_DCHECK(event_handle < max_events_); |
| 79 | |
| 80 | if (strcmp("ethos-u", tags_[event_handle]) == 0) { |
| 81 | struct ethosu_driver *ethosu_drv = ethosu_reserve_driver(); |
| 82 | end_ticks_[event_handle] = GetCurrentEthosuTicks(ethosu_drv); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 83 | ethosu_release_driver(ethosu_drv); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 84 | } else { |
| 85 | end_ticks_[event_handle] = GetCurrentTimeTicks(); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 86 | } |
Jens Elofsson | de044c3 | 2021-05-06 16:21:29 +0200 | [diff] [blame] | 87 | |
| 88 | printf("%s : cycle_cnt : %" PRIu64 " cycles\n", |
| 89 | tags_[event_handle], |
| 90 | end_ticks_[event_handle] - start_ticks_[event_handle]); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | uint64_t EthosUProfiler::GetTotalTicks() const { |
| 94 | uint64_t ticks = 0; |
| 95 | for (int i = 0; i < num_events_; ++i) { |
| 96 | ticks += end_ticks_[i] - start_ticks_[i]; |
| 97 | } |
| 98 | |
| 99 | return ticks; |
| 100 | } |
| 101 | |
| 102 | void EthosUProfiler::Log() const { |
| 103 | #if !defined(TF_LITE_STRIP_ERROR_STRINGS) |
| 104 | for (int i = 0; i < num_events_; ++i) { |
| 105 | uint64_t ticks = end_ticks_[i] - start_ticks_[i]; |
| 106 | printf("%s took %" PRIu64 " cycles\n", tags_[i], ticks); |
| 107 | } |
| 108 | #endif |
| 109 | } |
| 110 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 111 | } // namespace tflite |