blob: 4f525eeb70ac5248dafa1fac5e113c75861d9dd5 [file] [log] [blame]
Jens Elofsson955288a2021-04-22 20:57:15 +02001/*
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +01002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
Jens Elofsson955288a2021-04-22 20:57:15 +02003 *
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 <inttypes.h>
29#include <stdio.h>
30
Jens Elofsson955288a2021-04-22 20:57:15 +020031namespace tflite {
32
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010033LayerByLayerProfiler::LayerByLayerProfiler(size_t max_events, Backend _backend, int32_t _event_id) :
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010034 max_events_(max_events), backend(_backend), event_id(_event_id), num_events_(0) {
Jens Elofsson701a63b2021-05-23 17:37:07 +020035
Jonny Svärd4c11a482021-12-17 17:04:08 +010036 tags_ = std::make_unique<const char *[]>(max_events);
37 start_ticks_ = std::make_unique<uint64_t[]>(max_events);
38 end_ticks_ = std::make_unique<uint64_t[]>(max_events);
Jens Elofsson955288a2021-04-22 20:57:15 +020039}
40
41// NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU
Jens Elofsson701a63b2021-05-23 17:37:07 +020042uint32_t LayerByLayerProfiler::BeginEvent(const char *tag) {
Jens Elofsson955288a2021-04-22 20:57:15 +020043 if (num_events_ == max_events_) {
44 tflite::GetMicroErrorReporter()->Report("Profiling event overflow, max: %u events", max_events_);
45 num_events_ = 0;
46 }
47
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010048 tags_[num_events_] = tag;
49 start_ticks_[num_events_] = GetCurrentTimeTicks();
Jonny Svärd4c11a482021-12-17 17:04:08 +010050 end_ticks_[num_events_] =
51 start_ticks_[num_events_]; // NOTE: In case an EndEvent() doesn't trigger, cycles reports as 0
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010052
Jens Elofsson955288a2021-04-22 20:57:15 +020053 return num_events_++;
54}
55
56// NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU
Jens Elofsson701a63b2021-05-23 17:37:07 +020057void LayerByLayerProfiler::EndEvent(uint32_t event_handle) {
Jens Elofsson955288a2021-04-22 20:57:15 +020058 TFLITE_DCHECK(event_handle < max_events_);
59
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010060 end_ticks_[event_handle] = GetCurrentTimeTicks();
Jens Elofssonde044c32021-05-06 16:21:29 +020061
Jonny Svärd4c11a482021-12-17 17:04:08 +010062 if (backend == PRINTF) {
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010063 LOG("%s : cycle_cnt : %" PRIu64 " cycles\n",
64 tags_[event_handle],
65 end_ticks_[event_handle] - start_ticks_[event_handle]);
Jens Elofsson701a63b2021-05-23 17:37:07 +020066 } else {
Jonny Svärd4c11a482021-12-17 17:04:08 +010067 EventRecord2(event_id, (int32_t)event_handle, end_ticks_[event_handle] - start_ticks_[event_handle]);
Jens Elofsson701a63b2021-05-23 17:37:07 +020068 }
Jens Elofsson955288a2021-04-22 20:57:15 +020069}
70
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010071int32_t LayerByLayerProfiler::GetTotalTicks() const {
72 int32_t ticks = 0;
Kristofer Jonsson44d6e222021-05-21 18:59:18 +020073
74 for (size_t i = 0; i < num_events_; ++i) {
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010075 ticks += static_cast<int32_t>(end_ticks_[i] - start_ticks_[i]);
Jens Elofsson955288a2021-04-22 20:57:15 +020076 }
77
78 return ticks;
79}
80
Jens Elofsson701a63b2021-05-23 17:37:07 +020081void LayerByLayerProfiler::Log() const {
82
Jens Elofsson955288a2021-04-22 20:57:15 +020083#if !defined(TF_LITE_STRIP_ERROR_STRINGS)
Jonny Svärd4c11a482021-12-17 17:04:08 +010084 if (backend == PRINTF) {
Jens Elofsson701a63b2021-05-23 17:37:07 +020085 for (size_t i = 0; i < num_events_; ++i) {
86 uint64_t ticks = end_ticks_[i] - start_ticks_[i];
Anton Moberg07cf70b2021-07-07 11:08:17 +020087 LOG("%s took %" PRIu64 " cycles", tags_[i], ticks);
Jens Elofsson701a63b2021-05-23 17:37:07 +020088 }
Jens Elofsson955288a2021-04-22 20:57:15 +020089 }
90#endif
91}
92
Jens Elofsson955288a2021-04-22 20:57:15 +020093} // namespace tflite