blob: 021141444ce716d3742a73fa1037a56593fa901e [file] [log] [blame]
Jens Elofsson955288a2021-04-22 20:57:15 +02001/*
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 Moberg07cf70b2021-07-07 11:08:17 +020026#include "ethosu_log.h"
Jens Elofsson701a63b2021-05-23 17:37:07 +020027#include "layer_by_layer_profiler.hpp"
Jens Elofsson955288a2021-04-22 20:57:15 +020028#include <ethosu_driver.h>
29#include <inttypes.h>
30#include <stdio.h>
31
32namespace {
33
34uint64_t GetCurrentEthosuTicks(struct ethosu_driver *drv) {
Anton Moberg596a4662021-05-24 09:16:07 +020035 return ETHOSU_PMU_Get_CCNTR(drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020036}
37
Jens Elofsson955288a2021-04-22 20:57:15 +020038} // namespace
39
40namespace tflite {
41
Jens Elofsson701a63b2021-05-23 17:37:07 +020042LayerByLayerProfiler::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 Elofsson955288a2021-04-22 20:57:15 +020045 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 Elofssonde044c32021-05-06 16:21:29 +020049 struct ethosu_driver *drv = ethosu_reserve_driver();
Anton Moberg596a4662021-05-24 09:16:07 +020050 ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk);
51 ETHOSU_PMU_CYCCNT_Reset(drv);
Jens Elofssonde044c32021-05-06 16:21:29 +020052 ethosu_release_driver(drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020053}
54
55// NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU
Jens Elofsson701a63b2021-05-23 17:37:07 +020056uint32_t LayerByLayerProfiler::BeginEvent(const char *tag) {
Jens Elofsson955288a2021-04-22 20:57:15 +020057 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 Moberg596a4662021-05-24 09:16:07 +020066 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 Elofsson955288a2021-04-22 20:57:15 +020069 start_ticks_[num_events_] = GetCurrentEthosuTicks(ethosu_drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020070 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 Elofsson701a63b2021-05-23 17:37:07 +020080void LayerByLayerProfiler::EndEvent(uint32_t event_handle) {
Jens Elofsson955288a2021-04-22 20:57:15 +020081 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 Elofsson955288a2021-04-22 20:57:15 +020086 ethosu_release_driver(ethosu_drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020087 } else {
88 end_ticks_[event_handle] = GetCurrentTimeTicks();
Jens Elofsson955288a2021-04-22 20:57:15 +020089 }
Jens Elofssonde044c32021-05-06 16:21:29 +020090
Jens Elofsson701a63b2021-05-23 17:37:07 +020091 if (backend_ == PRINTF) {
Anton Moberg07cf70b2021-07-07 11:08:17 +020092 LOG("%s : cycle_cnt : %" PRIu64 " cycles\n",
93 tags_[event_handle],
94 end_ticks_[event_handle] - start_ticks_[event_handle]);
Jens Elofsson701a63b2021-05-23 17:37:07 +020095 } else {
96 EventRecord2(event_id_, (int32_t)event_handle, end_ticks_[event_handle] - start_ticks_[event_handle]);
97 }
Jens Elofsson955288a2021-04-22 20:57:15 +020098}
99
Jens Elofsson701a63b2021-05-23 17:37:07 +0200100uint64_t LayerByLayerProfiler::GetTotalTicks() const {
Jens Elofsson955288a2021-04-22 20:57:15 +0200101 uint64_t ticks = 0;
Kristofer Jonsson44d6e222021-05-21 18:59:18 +0200102
103 for (size_t i = 0; i < num_events_; ++i) {
Jens Elofsson955288a2021-04-22 20:57:15 +0200104 ticks += end_ticks_[i] - start_ticks_[i];
105 }
106
107 return ticks;
108}
109
Jens Elofsson701a63b2021-05-23 17:37:07 +0200110void LayerByLayerProfiler::Log() const {
111
Jens Elofsson955288a2021-04-22 20:57:15 +0200112#if !defined(TF_LITE_STRIP_ERROR_STRINGS)
Jens Elofsson701a63b2021-05-23 17:37:07 +0200113 if (backend_ == PRINTF) {
114 for (size_t i = 0; i < num_events_; ++i) {
115 uint64_t ticks = end_ticks_[i] - start_ticks_[i];
Anton Moberg07cf70b2021-07-07 11:08:17 +0200116 LOG("%s took %" PRIu64 " cycles", tags_[i], ticks);
Jens Elofsson701a63b2021-05-23 17:37:07 +0200117 }
Jens Elofsson955288a2021-04-22 20:57:15 +0200118 }
119#endif
120}
121
Jens Elofsson955288a2021-04-22 20:57:15 +0200122} // namespace tflite