Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 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" |
Kristofer Jonsson | 1fed1d5 | 2022-11-21 13:39:45 +0100 | [diff] [blame^] | 20 | #include "tensorflow/lite/micro/micro_log.h" |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 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 <inttypes.h> |
| 29 | #include <stdio.h> |
| 30 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 31 | namespace tflite { |
| 32 | |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 33 | LayerByLayerProfiler::LayerByLayerProfiler(size_t max_events, Backend _backend, int32_t _event_id) : |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 34 | max_events_(max_events), backend(_backend), event_id(_event_id), num_events_(0) { |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 35 | |
Jonny Svärd | 4c11a48 | 2021-12-17 17:04:08 +0100 | [diff] [blame] | 36 | 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 Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 42 | uint32_t LayerByLayerProfiler::BeginEvent(const char *tag) { |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 43 | if (num_events_ == max_events_) { |
Kristofer Jonsson | 1fed1d5 | 2022-11-21 13:39:45 +0100 | [diff] [blame^] | 44 | MicroPrintf("Profiling event overflow, max: %u events", max_events_); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 45 | num_events_ = 0; |
| 46 | } |
| 47 | |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 48 | tags_[num_events_] = tag; |
| 49 | start_ticks_[num_events_] = GetCurrentTimeTicks(); |
Jonny Svärd | 4c11a48 | 2021-12-17 17:04:08 +0100 | [diff] [blame] | 50 | end_ticks_[num_events_] = |
| 51 | start_ticks_[num_events_]; // NOTE: In case an EndEvent() doesn't trigger, cycles reports as 0 |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 52 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 53 | return num_events_++; |
| 54 | } |
| 55 | |
| 56 | // NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 57 | void LayerByLayerProfiler::EndEvent(uint32_t event_handle) { |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 58 | TFLITE_DCHECK(event_handle < max_events_); |
| 59 | |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 60 | end_ticks_[event_handle] = GetCurrentTimeTicks(); |
Jens Elofsson | de044c3 | 2021-05-06 16:21:29 +0200 | [diff] [blame] | 61 | |
Jonny Svärd | 4c11a48 | 2021-12-17 17:04:08 +0100 | [diff] [blame] | 62 | if (backend == PRINTF) { |
Kristofer Jonsson | 5a15bf4 | 2022-01-27 17:36:55 +0100 | [diff] [blame] | 63 | LOG("%s : cycle_cnt : %" PRIu64 " cycles\n", |
| 64 | tags_[event_handle], |
| 65 | end_ticks_[event_handle] - start_ticks_[event_handle]); |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 66 | } else { |
Jonny Svärd | 4c11a48 | 2021-12-17 17:04:08 +0100 | [diff] [blame] | 67 | EventRecord2(event_id, (int32_t)event_handle, end_ticks_[event_handle] - start_ticks_[event_handle]); |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 68 | } |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 71 | int32_t LayerByLayerProfiler::GetTotalTicks() const { |
| 72 | int32_t ticks = 0; |
Kristofer Jonsson | 44d6e22 | 2021-05-21 18:59:18 +0200 | [diff] [blame] | 73 | |
| 74 | for (size_t i = 0; i < num_events_; ++i) { |
Kristofer Jonsson | dcc1ce0 | 2021-12-21 16:25:19 +0100 | [diff] [blame] | 75 | ticks += static_cast<int32_t>(end_ticks_[i] - start_ticks_[i]); |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | return ticks; |
| 79 | } |
| 80 | |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 81 | void LayerByLayerProfiler::Log() const { |
| 82 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 83 | #if !defined(TF_LITE_STRIP_ERROR_STRINGS) |
Jonny Svärd | 4c11a48 | 2021-12-17 17:04:08 +0100 | [diff] [blame] | 84 | if (backend == PRINTF) { |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 85 | for (size_t i = 0; i < num_events_; ++i) { |
| 86 | uint64_t ticks = end_ticks_[i] - start_ticks_[i]; |
Anton Moberg | 07cf70b | 2021-07-07 11:08:17 +0200 | [diff] [blame] | 87 | LOG("%s took %" PRIu64 " cycles", tags_[i], ticks); |
Jens Elofsson | 701a63b | 2021-05-23 17:37:07 +0200 | [diff] [blame] | 88 | } |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 89 | } |
| 90 | #endif |
| 91 | } |
| 92 | |
Jens Elofsson | 955288a | 2021-04-22 20:57:15 +0200 | [diff] [blame] | 93 | } // namespace tflite |