blob: f7922cb8ae8a85c791a653bf67da4a9000860bb6 [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
Jens Elofsson701a63b2021-05-23 17:37:07 +020026#include "layer_by_layer_profiler.hpp"
Jens Elofsson955288a2021-04-22 20:57:15 +020027#include <ethosu_driver.h>
28#include <inttypes.h>
29#include <stdio.h>
30
31namespace {
32
33uint64_t GetCurrentEthosuTicks(struct ethosu_driver *drv) {
Anton Moberg596a4662021-05-24 09:16:07 +020034 return ETHOSU_PMU_Get_CCNTR(drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020035}
36
Jens Elofsson955288a2021-04-22 20:57:15 +020037} // namespace
38
39namespace tflite {
40
Jens Elofsson701a63b2021-05-23 17:37:07 +020041LayerByLayerProfiler::LayerByLayerProfiler(size_t max_events, Backend backend, int32_t event_id) :
42 max_events_(max_events), backend_(backend), event_id_(event_id), num_events_(0) {
43
Jens Elofsson955288a2021-04-22 20:57:15 +020044 tags_ = std::make_unique<const char *[]>(max_events_);
45 start_ticks_ = std::make_unique<uint64_t[]>(max_events_);
46 end_ticks_ = std::make_unique<uint64_t[]>(max_events_);
47
Jens Elofssonde044c32021-05-06 16:21:29 +020048 struct ethosu_driver *drv = ethosu_reserve_driver();
Anton Moberg596a4662021-05-24 09:16:07 +020049 ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk);
50 ETHOSU_PMU_CYCCNT_Reset(drv);
Jens Elofssonde044c32021-05-06 16:21:29 +020051 ethosu_release_driver(drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020052}
53
54// NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU
Jens Elofsson701a63b2021-05-23 17:37:07 +020055uint32_t LayerByLayerProfiler::BeginEvent(const char *tag) {
Jens Elofsson955288a2021-04-22 20:57:15 +020056 if (num_events_ == max_events_) {
57 tflite::GetMicroErrorReporter()->Report("Profiling event overflow, max: %u events", max_events_);
58 num_events_ = 0;
59 }
60
61 tags_[num_events_] = tag;
62
63 if (strcmp("ethos-u", tag) == 0) {
64 struct ethosu_driver *ethosu_drv = ethosu_reserve_driver();
Anton Moberg596a4662021-05-24 09:16:07 +020065 ETHOSU_PMU_CYCCNT_Reset(ethosu_drv);
66 ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event(ethosu_drv, ETHOSU_PMU_NPU_ACTIVE);
67 ETHOSU_PMU_PMCCNTR_CFG_Set_Stop_Event(ethosu_drv, ETHOSU_PMU_NPU_IDLE);
Jens Elofsson955288a2021-04-22 20:57:15 +020068 start_ticks_[num_events_] = GetCurrentEthosuTicks(ethosu_drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020069 ethosu_release_driver(ethosu_drv);
70 } else {
71 start_ticks_[num_events_] = GetCurrentTimeTicks();
72 }
73
74 end_ticks_[num_events_] = start_ticks_[num_events_] - 1;
75 return num_events_++;
76}
77
78// NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU
Jens Elofsson701a63b2021-05-23 17:37:07 +020079void LayerByLayerProfiler::EndEvent(uint32_t event_handle) {
Jens Elofsson955288a2021-04-22 20:57:15 +020080 TFLITE_DCHECK(event_handle < max_events_);
81
82 if (strcmp("ethos-u", tags_[event_handle]) == 0) {
83 struct ethosu_driver *ethosu_drv = ethosu_reserve_driver();
84 end_ticks_[event_handle] = GetCurrentEthosuTicks(ethosu_drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020085 ethosu_release_driver(ethosu_drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020086 } else {
87 end_ticks_[event_handle] = GetCurrentTimeTicks();
Jens Elofsson955288a2021-04-22 20:57:15 +020088 }
Jens Elofssonde044c32021-05-06 16:21:29 +020089
Jens Elofsson701a63b2021-05-23 17:37:07 +020090 if (backend_ == PRINTF) {
91 printf("%s : cycle_cnt : %" PRIu64 " cycles\n",
92 tags_[event_handle],
93 end_ticks_[event_handle] - start_ticks_[event_handle]);
94 } else {
95 EventRecord2(event_id_, (int32_t)event_handle, end_ticks_[event_handle] - start_ticks_[event_handle]);
96 }
Jens Elofsson955288a2021-04-22 20:57:15 +020097}
98
Jens Elofsson701a63b2021-05-23 17:37:07 +020099uint64_t LayerByLayerProfiler::GetTotalTicks() const {
Jens Elofsson955288a2021-04-22 20:57:15 +0200100 uint64_t ticks = 0;
Kristofer Jonsson44d6e222021-05-21 18:59:18 +0200101
102 for (size_t i = 0; i < num_events_; ++i) {
Jens Elofsson955288a2021-04-22 20:57:15 +0200103 ticks += end_ticks_[i] - start_ticks_[i];
104 }
105
106 return ticks;
107}
108
Jens Elofsson701a63b2021-05-23 17:37:07 +0200109void LayerByLayerProfiler::Log() const {
110
Jens Elofsson955288a2021-04-22 20:57:15 +0200111#if !defined(TF_LITE_STRIP_ERROR_STRINGS)
Jens Elofsson701a63b2021-05-23 17:37:07 +0200112 if (backend_ == PRINTF) {
113 for (size_t i = 0; i < num_events_; ++i) {
114 uint64_t ticks = end_ticks_[i] - start_ticks_[i];
115 printf("%s took %" PRIu64 " cycles\n", tags_[i], ticks);
116 }
Jens Elofsson955288a2021-04-22 20:57:15 +0200117 }
118#endif
119}
120
Jens Elofsson955288a2021-04-22 20:57:15 +0200121} // namespace tflite