blob: a76ac41e295c644e32be62b9ad3235a11b1959f4 [file] [log] [blame]
Jens Elofsson955288a2021-04-22 20:57:15 +02001/*
Måns Nilsson45a8a132022-09-26 08:42:10 +02002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
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
Jens Elofsson701a63b2021-05-23 17:37:07 +020019#ifndef LAYER_BY_LAYER_PROFILER_H
20#define LAYER_BY_LAYER_PROFILER_H
Jens Elofsson955288a2021-04-22 20:57:15 +020021
Jens Elofsson701a63b2021-05-23 17:37:07 +020022#include "EventRecorder.h"
Kristofer Jonsson1fed1d52022-11-21 13:39:45 +010023#include "tensorflow/lite/micro/compatibility.h"
Måns Nilsson45a8a132022-09-26 08:42:10 +020024#include "tensorflow/lite/micro/micro_profiler_interface.h"
Jens Elofsson955288a2021-04-22 20:57:15 +020025#include <memory>
Jonny Svärd4c11a482021-12-17 17:04:08 +010026#include <vector>
Jens Elofsson955288a2021-04-22 20:57:15 +020027
28// NOTE: This profiler only works on systems with 1 NPU due to the use of
29// ethosu_reserve_driver().
30namespace tflite {
Måns Nilsson45a8a132022-09-26 08:42:10 +020031class LayerByLayerProfiler : public MicroProfilerInterface {
Jens Elofsson955288a2021-04-22 20:57:15 +020032public:
Jens Elofsson701a63b2021-05-23 17:37:07 +020033 enum Backend { PRINTF, EVENT_RECORDER };
Kristofer Jonsson5a15bf42022-01-27 17:36:55 +010034
35 LayerByLayerProfiler(size_t max_events = 200,
36 Backend backend = PRINTF,
37 int32_t event_id = EventID(EventLevelError, EvtStatistics_No, EventRecordNone));
38
Jens Elofsson955288a2021-04-22 20:57:15 +020039 uint32_t BeginEvent(const char *tag);
40 void EndEvent(uint32_t event_handle);
Kristofer Jonssondcc1ce02021-12-21 16:25:19 +010041 int32_t GetTotalTicks() const;
Jens Elofsson955288a2021-04-22 20:57:15 +020042 void Log() const;
Jens Elofsson955288a2021-04-22 20:57:15 +020043
44private:
Jens Elofsson955288a2021-04-22 20:57:15 +020045 std::unique_ptr<const char *[]> tags_;
46 std::unique_ptr<uint64_t[]> start_ticks_;
47 std::unique_ptr<uint64_t[]> end_ticks_;
Jens Elofsson701a63b2021-05-23 17:37:07 +020048
Jonny Svärd4c11a482021-12-17 17:04:08 +010049 size_t max_events_;
50 Backend backend;
51 int32_t event_id;
Kristofer Jonsson44d6e222021-05-21 18:59:18 +020052 size_t num_events_;
Jens Elofsson955288a2021-04-22 20:57:15 +020053
Jens Elofsson955288a2021-04-22 20:57:15 +020054 TF_LITE_REMOVE_VIRTUAL_DELETE;
55};
56
57} // namespace tflite
58
59#endif