blob: 37bd86818c48e8c614b5692cbac86b376def13e9 [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
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"
Jens Elofsson955288a2021-04-22 20:57:15 +020023#include "tensorflow/lite/kernels/internal/compatibility.h"
24#include <memory>
25#include <pmu_ethosu.h>
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 {
Jens Elofsson701a63b2021-05-23 17:37:07 +020031class LayerByLayerProfiler : public MicroProfiler {
Jens Elofsson955288a2021-04-22 20:57:15 +020032public:
Jens Elofsson701a63b2021-05-23 17:37:07 +020033 enum Backend { PRINTF, EVENT_RECORDER };
Jonny Svärd4c11a482021-12-17 17:04:08 +010034 LayerByLayerProfiler(const std::vector<uint8_t> &event_config = {},
35 bool pmu_cycle_counter_enable = true,
36 size_t max_events = 200,
37 Backend backend = PRINTF,
38 int32_t event_id = EventID(EventLevelError, EvtStatistics_No, EventRecordNone));
Jens Elofsson955288a2021-04-22 20:57:15 +020039 uint32_t BeginEvent(const char *tag);
40 void EndEvent(uint32_t event_handle);
41 uint64_t GetTotalTicks() const;
42 void Log() const;
Jens Elofsson955288a2021-04-22 20:57:15 +020043
Jonny Svärd4c11a482021-12-17 17:04:08 +010044 uint64_t GetPmuCycleCounterCount() const;
45 const std::vector<uint32_t> &GetPmuEventCount() const;
46
Jens Elofsson955288a2021-04-22 20:57:15 +020047private:
Jens Elofsson955288a2021-04-22 20:57:15 +020048 std::unique_ptr<const char *[]> tags_;
49 std::unique_ptr<uint64_t[]> start_ticks_;
50 std::unique_ptr<uint64_t[]> end_ticks_;
Jens Elofsson701a63b2021-05-23 17:37:07 +020051
Jonny Svärd4c11a482021-12-17 17:04:08 +010052 std::vector<uint8_t> pmu_event_config;
53 std::vector<uint32_t> pmu_event_count;
54 bool pmu_cycle_counter_enable;
55 uint64_t pmu_cycle_counter_count;
56
57 size_t max_events_;
58 Backend backend;
59 int32_t event_id;
Kristofer Jonsson44d6e222021-05-21 18:59:18 +020060 size_t num_events_;
Jens Elofsson955288a2021-04-22 20:57:15 +020061
Jens Elofsson955288a2021-04-22 20:57:15 +020062 TF_LITE_REMOVE_VIRTUAL_DELETE;
63};
64
65} // namespace tflite
66
67#endif