aboutsummaryrefslogtreecommitdiff
path: root/lib/layer_by_layer_profiler
diff options
context:
space:
mode:
authorJens Elofsson <jens.elofsson@arm.com>2021-05-23 17:37:07 +0200
committerMåns Nilsson <mans.nilsson@arm.com>2021-05-27 15:50:35 +0200
commit701a63b693bc877fc44abd802a9b2a431d81cfbe (patch)
tree5cfe3fb1c63b083b749c61af25c3b39e6a314907 /lib/layer_by_layer_profiler
parent036d1a89dffa4628f8d5b9a5b44ba9673ca2e3b4 (diff)
downloadethos-u-core-software-701a63b693bc877fc44abd802a9b2a431d81cfbe.tar.gz
Add Event Recorder Profiling
* Add event recorded profiling to ethosu_profiler. * Rename ethosu_profiler to layer_by_layer_profiler. Change-Id: Ie39c29845b2d1eb6d9d72e3fce68bf373e9dc888
Diffstat (limited to 'lib/layer_by_layer_profiler')
-rw-r--r--lib/layer_by_layer_profiler/CMakeLists.txt26
-rw-r--r--lib/layer_by_layer_profiler/include/layer_by_layer_profiler.hpp56
-rw-r--r--lib/layer_by_layer_profiler/src/layer_by_layer_profiler.cpp121
3 files changed, 203 insertions, 0 deletions
diff --git a/lib/layer_by_layer_profiler/CMakeLists.txt b/lib/layer_by_layer_profiler/CMakeLists.txt
new file mode 100644
index 0000000..9774685
--- /dev/null
+++ b/lib/layer_by_layer_profiler/CMakeLists.txt
@@ -0,0 +1,26 @@
+#
+# Copyright (c) 2021 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed under the Apache License, Version 2.0 (the License); you may
+# not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an AS IS BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+if (NOT TARGET ethosu_core_driver)
+ return()
+endif()
+
+add_library(layer_by_layer_profiler INTERFACE)
+target_link_libraries(layer_by_layer_profiler INTERFACE ethosu_core_driver tflu event_recorder)
+target_include_directories(layer_by_layer_profiler INTERFACE include)
+target_sources(layer_by_layer_profiler INTERFACE src/layer_by_layer_profiler.cpp)
diff --git a/lib/layer_by_layer_profiler/include/layer_by_layer_profiler.hpp b/lib/layer_by_layer_profiler/include/layer_by_layer_profiler.hpp
new file mode 100644
index 0000000..8e8dc0f
--- /dev/null
+++ b/lib/layer_by_layer_profiler/include/layer_by_layer_profiler.hpp
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2021 Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the License); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef LAYER_BY_LAYER_PROFILER_H
+#define LAYER_BY_LAYER_PROFILER_H
+
+#include "EventRecorder.h"
+#include "tensorflow/lite/kernels/internal/compatibility.h"
+#include <memory>
+#include <pmu_ethosu.h>
+
+// NOTE: This profiler only works on systems with 1 NPU due to the use of
+// ethosu_reserve_driver().
+namespace tflite {
+class LayerByLayerProfiler : public MicroProfiler {
+public:
+ enum Backend { PRINTF, EVENT_RECORDER };
+ LayerByLayerProfiler(size_t max_events = 200,
+ Backend backend = PRINTF,
+ int32_t event_id = EventID(EventLevelError, EvtStatistics_No, EventRecordNone));
+ uint32_t BeginEvent(const char *tag);
+ void EndEvent(uint32_t event_handle);
+ uint64_t GetTotalTicks() const;
+ void Log() const;
+
+private:
+ size_t max_events_;
+ std::unique_ptr<const char *[]> tags_;
+ std::unique_ptr<uint64_t[]> start_ticks_;
+ std::unique_ptr<uint64_t[]> end_ticks_;
+
+ Backend backend_;
+ int32_t event_id_;
+ size_t num_events_;
+
+ TF_LITE_REMOVE_VIRTUAL_DELETE;
+};
+
+} // namespace tflite
+
+#endif
diff --git a/lib/layer_by_layer_profiler/src/layer_by_layer_profiler.cpp b/lib/layer_by_layer_profiler/src/layer_by_layer_profiler.cpp
new file mode 100644
index 0000000..f7922cb
--- /dev/null
+++ b/lib/layer_by_layer_profiler/src/layer_by_layer_profiler.cpp
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2021 Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the License); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "tensorflow/lite/kernels/internal/compatibility.h"
+#include "tensorflow/lite/micro/micro_error_reporter.h"
+#include "tensorflow/lite/micro/micro_profiler.h"
+#include "tensorflow/lite/micro/micro_time.h"
+
+#include <string.h>
+
+#include "layer_by_layer_profiler.hpp"
+#include <ethosu_driver.h>
+#include <inttypes.h>
+#include <stdio.h>
+
+namespace {
+
+uint64_t GetCurrentEthosuTicks(struct ethosu_driver *drv) {
+ return ETHOSU_PMU_Get_CCNTR(drv);
+}
+
+} // namespace
+
+namespace tflite {
+
+LayerByLayerProfiler::LayerByLayerProfiler(size_t max_events, Backend backend, int32_t event_id) :
+ max_events_(max_events), backend_(backend), event_id_(event_id), num_events_(0) {
+
+ tags_ = std::make_unique<const char *[]>(max_events_);
+ start_ticks_ = std::make_unique<uint64_t[]>(max_events_);
+ end_ticks_ = std::make_unique<uint64_t[]>(max_events_);
+
+ struct ethosu_driver *drv = ethosu_reserve_driver();
+ ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk);
+ ETHOSU_PMU_CYCCNT_Reset(drv);
+ ethosu_release_driver(drv);
+}
+
+// NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU
+uint32_t LayerByLayerProfiler::BeginEvent(const char *tag) {
+ if (num_events_ == max_events_) {
+ tflite::GetMicroErrorReporter()->Report("Profiling event overflow, max: %u events", max_events_);
+ num_events_ = 0;
+ }
+
+ tags_[num_events_] = tag;
+
+ if (strcmp("ethos-u", tag) == 0) {
+ struct ethosu_driver *ethosu_drv = ethosu_reserve_driver();
+ ETHOSU_PMU_CYCCNT_Reset(ethosu_drv);
+ ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event(ethosu_drv, ETHOSU_PMU_NPU_ACTIVE);
+ ETHOSU_PMU_PMCCNTR_CFG_Set_Stop_Event(ethosu_drv, ETHOSU_PMU_NPU_IDLE);
+ start_ticks_[num_events_] = GetCurrentEthosuTicks(ethosu_drv);
+ ethosu_release_driver(ethosu_drv);
+ } else {
+ start_ticks_[num_events_] = GetCurrentTimeTicks();
+ }
+
+ end_ticks_[num_events_] = start_ticks_[num_events_] - 1;
+ return num_events_++;
+}
+
+// NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU
+void LayerByLayerProfiler::EndEvent(uint32_t event_handle) {
+ TFLITE_DCHECK(event_handle < max_events_);
+
+ if (strcmp("ethos-u", tags_[event_handle]) == 0) {
+ struct ethosu_driver *ethosu_drv = ethosu_reserve_driver();
+ end_ticks_[event_handle] = GetCurrentEthosuTicks(ethosu_drv);
+ ethosu_release_driver(ethosu_drv);
+ } else {
+ end_ticks_[event_handle] = GetCurrentTimeTicks();
+ }
+
+ if (backend_ == PRINTF) {
+ printf("%s : cycle_cnt : %" PRIu64 " cycles\n",
+ tags_[event_handle],
+ end_ticks_[event_handle] - start_ticks_[event_handle]);
+ } else {
+ EventRecord2(event_id_, (int32_t)event_handle, end_ticks_[event_handle] - start_ticks_[event_handle]);
+ }
+}
+
+uint64_t LayerByLayerProfiler::GetTotalTicks() const {
+ uint64_t ticks = 0;
+
+ for (size_t i = 0; i < num_events_; ++i) {
+ ticks += end_ticks_[i] - start_ticks_[i];
+ }
+
+ return ticks;
+}
+
+void LayerByLayerProfiler::Log() const {
+
+#if !defined(TF_LITE_STRIP_ERROR_STRINGS)
+ if (backend_ == PRINTF) {
+ for (size_t i = 0; i < num_events_; ++i) {
+ uint64_t ticks = end_ticks_[i] - start_ticks_[i];
+ printf("%s took %" PRIu64 " cycles\n", tags_[i], ticks);
+ }
+ }
+#endif
+}
+
+} // namespace tflite