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 | |
Anton Moberg | 07cf70b | 2021-07-07 11:08:17 +0200 | [diff] [blame^] | 26 | #include "ethosu_log.h" |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 27 | #include "layer_by_layer_profiler.hpp" |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 28 | #include <ethosu_driver.h> |
| 29 | #include <inttypes.h> |
| 30 | #include <stdio.h> |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | uint64_t GetCurrentEthosuTicks(struct ethosu_driver *drv) { |
Anton Moberg | 596a466 | 2021-05-24 09:16:07 +0200 | [diff] [blame] | 35 | return ETHOSU_PMU_Get_CCNTR(drv); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 38 | } // namespace |
| 39 | |
| 40 | namespace tflite { |
| 41 | |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 42 | LayerByLayerProfiler::LayerByLayerProfiler(size_t max_events, Backend backend, int32_t event_id) : |
| 43 | max_events_(max_events), backend_(backend), event_id_(event_id), num_events_(0) { |
| 44 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 45 | tags_ = std::make_unique<const char *[]>(max_events_); |
| 46 | start_ticks_ = std::make_unique<uint64_t[]>(max_events_); |
| 47 | end_ticks_ = std::make_unique<uint64_t[]>(max_events_); |
| 48 | |
Jens Elofsson | de044c3 | 2021-05-06 16:21:29 +0200 | [diff] [blame] | 49 | struct ethosu_driver *drv = ethosu_reserve_driver(); |
Anton Moberg | 596a466 | 2021-05-24 09:16:07 +0200 | [diff] [blame] | 50 | ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk); |
| 51 | ETHOSU_PMU_CYCCNT_Reset(drv); |
Jens Elofsson | de044c3 | 2021-05-06 16:21:29 +0200 | [diff] [blame] | 52 | ethosu_release_driver(drv); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 56 | uint32_t LayerByLayerProfiler::BeginEvent(const char *tag) { |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 57 | if (num_events_ == max_events_) { |
| 58 | tflite::GetMicroErrorReporter()->Report("Profiling event overflow, max: %u events", max_events_); |
| 59 | num_events_ = 0; |
| 60 | } |
| 61 | |
| 62 | tags_[num_events_] = tag; |
| 63 | |
| 64 | if (strcmp("ethos-u", tag) == 0) { |
| 65 | struct ethosu_driver *ethosu_drv = ethosu_reserve_driver(); |
Anton Moberg | 596a466 | 2021-05-24 09:16:07 +0200 | [diff] [blame] | 66 | ETHOSU_PMU_CYCCNT_Reset(ethosu_drv); |
| 67 | ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event(ethosu_drv, ETHOSU_PMU_NPU_ACTIVE); |
| 68 | 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] | 69 | start_ticks_[num_events_] = GetCurrentEthosuTicks(ethosu_drv); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 70 | ethosu_release_driver(ethosu_drv); |
| 71 | } else { |
| 72 | start_ticks_[num_events_] = GetCurrentTimeTicks(); |
| 73 | } |
| 74 | |
| 75 | end_ticks_[num_events_] = start_ticks_[num_events_] - 1; |
| 76 | return num_events_++; |
| 77 | } |
| 78 | |
| 79 | // NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 80 | void LayerByLayerProfiler::EndEvent(uint32_t event_handle) { |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 81 | TFLITE_DCHECK(event_handle < max_events_); |
| 82 | |
| 83 | if (strcmp("ethos-u", tags_[event_handle]) == 0) { |
| 84 | struct ethosu_driver *ethosu_drv = ethosu_reserve_driver(); |
| 85 | end_ticks_[event_handle] = GetCurrentEthosuTicks(ethosu_drv); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 86 | ethosu_release_driver(ethosu_drv); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 87 | } else { |
| 88 | end_ticks_[event_handle] = GetCurrentTimeTicks(); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 89 | } |
Jens Elofsson | de044c3 | 2021-05-06 16:21:29 +0200 | [diff] [blame] | 90 | |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 91 | if (backend_ == PRINTF) { |
Anton Moberg | 07cf70b | 2021-07-07 11:08:17 +0200 | [diff] [blame^] | 92 | LOG("%s : cycle_cnt : %" PRIu64 " cycles\n", |
| 93 | tags_[event_handle], |
| 94 | end_ticks_[event_handle] - start_ticks_[event_handle]); |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 95 | } else { |
| 96 | EventRecord2(event_id_, (int32_t)event_handle, end_ticks_[event_handle] - start_ticks_[event_handle]); |
| 97 | } |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 100 | uint64_t LayerByLayerProfiler::GetTotalTicks() const { |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 101 | uint64_t ticks = 0; |
Kristofer Jonsson | 44d6e22 | 2021-05-21 18:59:18 +0200 | [diff] [blame] | 102 | |
| 103 | for (size_t i = 0; i < num_events_; ++i) { |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 104 | ticks += end_ticks_[i] - start_ticks_[i]; |
| 105 | } |
| 106 | |
| 107 | return ticks; |
| 108 | } |
| 109 | |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 110 | void LayerByLayerProfiler::Log() const { |
| 111 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 112 | #if !defined(TF_LITE_STRIP_ERROR_STRINGS) |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 113 | if (backend_ == PRINTF) { |
| 114 | for (size_t i = 0; i < num_events_; ++i) { |
| 115 | uint64_t ticks = end_ticks_[i] - start_ticks_[i]; |
Anton Moberg | 07cf70b | 2021-07-07 11:08:17 +0200 | [diff] [blame^] | 116 | LOG("%s took %" PRIu64 " cycles", tags_[i], ticks); |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 117 | } |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 118 | } |
| 119 | #endif |
| 120 | } |
| 121 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 122 | } // namespace tflite |