Skip to content

Commit 1a9721f

Browse files
committed
Fix comments during code review
- Raise a CMake error if event tracing is enabled without the devtools - Re-factoring of the changes in the portable executor_runner - Minor fix in docs Change-Id: Ia50fef8172f678f9cbe2b33e2178780ff983f335 Signed-off-by: Benjamin Klimczak <[email protected]>
1 parent f3493e4 commit 1a9721f

File tree

4 files changed

+73
-36
lines changed

4 files changed

+73
-36
lines changed

CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
2-
# Copyright 2024 Arm Limited and/or its affiliates.
2+
# Copyright 2024-2025 Arm Limited and/or its affiliates.
33
# All rights reserved.
44
#
55
# This source code is licensed under the BSD-style license found in the
@@ -824,7 +824,11 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER)
824824
endif()
825825

826826
if(EXECUTORCH_ENABLE_EVENT_TRACER)
827-
list(APPEND _executor_runner_libs etdump ${FLATCCRT_LIB})
827+
if(EXECUTORCH_BUILD_DEVTOOLS)
828+
list(APPEND _executor_runner_libs etdump ${FLATCCRT_LIB})
829+
else()
830+
message(SEND_ERROR "Use of 'EXECUTORCH_ENABLE_EVENT_TRACER' requires 'EXECUTORCH_BUILD_DEVTOOLS' to be enabled.")
831+
endif()
828832
endif()
829833

830834
add_executable(executor_runner ${_executor_runner__srcs})

backends/xnnpack/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
2-
# Copyright 2024 Arm Limited and/or its affiliates.
2+
# Copyright 2024-2025 Arm Limited and/or its affiliates.
33
# All rights reserved.
44
#
55
# This source code is licensed under the BSD-style license found in the
@@ -131,7 +131,11 @@ if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$")
131131
add_executable(xnn_executor_runner ${_xnn_executor_runner__srcs})
132132

133133
if(EXECUTORCH_ENABLE_EVENT_TRACER)
134-
list(APPEND xnn_executor_runner_libs etdump)
134+
if(EXECUTORCH_BUILD_DEVTOOLS)
135+
list(APPEND xnn_executor_runner_libs etdump)
136+
else()
137+
message(SEND_ERROR "Use of 'EXECUTORCH_ENABLE_EVENT_TRACER' requires 'EXECUTORCH_BUILD_DEVTOOLS' to be enabled.")
138+
endif()
135139
endif()
136140

137141
target_link_libraries(

docs/source/tutorial-xnnpack-delegate-lowering.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,4 @@ Now you should be able to find the executable built at `./cmake-out/backends/xnn
179179
You can build the XNNPACK backend [CMake target](https://github.com/pytorch/executorch/blob/main/backends/xnnpack/CMakeLists.txt#L83), and link it with your application binary such as an Android or iOS application. For more information on this you may take a look at this [resource](demo-apps-android.md) next.
180180

181181
## Profiling
182-
To enable profiling in the `xnn_executor_runner` pass the flags `-DEXECUTORCH_ENABLE_EVENT_TRACER=ON` and `-DEXECUTORCH_BUILD_SDK=ON` to the build command (add `-DENABLE_XNNPACK_PROFILING=ON` for additional details). This will enable ETDump generation when running the inference and enables command line flags for profiling (see `xnn_executor_runner --help` for details).
182+
To enable profiling in the `xnn_executor_runner` pass the flags `-DEXECUTORCH_ENABLE_EVENT_TRACER=ON` and `-DEXECUTORCH_BUILD_DEVTOOLS=ON` to the build command (add `-DENABLE_XNNPACK_PROFILING=ON` for additional details). This will enable ETDump generation when running the inference and enables command line flags for profiling (see `xnn_executor_runner --help` for details).

examples/portable/executor_runner/executor_runner.cpp

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (c) Meta Platforms, Inc. and affiliates.
3-
* Copyright 2024 Arm Limited and/or its affiliates.
3+
* Copyright 2024-2025 Arm Limited and/or its affiliates.
44
* All rights reserved.
55
*
66
* This source code is licensed under the BSD-style license found in the
@@ -43,10 +43,7 @@ DEFINE_string(
4343
"Model serialized in flatbuffer format.");
4444
DEFINE_uint32(num_executions, 1, "Number of times to run the model.");
4545
#ifdef ET_EVENT_TRACER_ENABLED
46-
DEFINE_string(
47-
etdump_path,
48-
"model.etdump",
49-
"If ETDump generation is enabled an ETDump will be written out to this path.");
46+
DEFINE_string(etdump_path, "model.etdump", "Write ETDump data to this path.");
5047
#endif // ET_EVENT_TRACER_ENABLED
5148

5249
using executorch::extension::FileDataLoader;
@@ -62,6 +59,50 @@ using executorch::runtime::Program;
6259
using executorch::runtime::Result;
6360
using executorch::runtime::Span;
6461

62+
/// Helper to manage resources for ETDump generation
63+
class EventTraceManager {
64+
public:
65+
EventTraceManager() : event_tracer_ptr_(nullptr) {
66+
#ifdef ET_EVENT_TRACER_ENABLED
67+
event_tracer_ptr_ = std::make_shared<torch::executor::ETDumpGen>();
68+
#endif // ET_EVENT_TRACER_ENABLED
69+
}
70+
71+
EventTracer* get_event_tracer() const {
72+
return event_tracer_ptr_.get();
73+
};
74+
75+
Error write_etdump_to_file(const char* filename) const {
76+
torch::executor::ETDumpGen* const etdump_ptr =
77+
static_cast<torch::executor::ETDumpGen*>(get_event_tracer());
78+
if (!etdump_ptr) {
79+
return Error::NotSupported;
80+
}
81+
82+
std::unique_ptr<FILE, decltype(&fclose)> etdump_file(
83+
fopen(filename, "w+"), fclose);
84+
if (!etdump_file) {
85+
ET_LOG(Error, "Failed to open ETDump file at %s.", filename);
86+
return Error::AccessFailed;
87+
}
88+
89+
torch::executor::etdump_result result = etdump_ptr->get_etdump_data();
90+
if (result.buf != nullptr && result.size > 0) {
91+
fwrite((uint8_t*)result.buf, 1, result.size, etdump_file.get());
92+
free(result.buf);
93+
ET_LOG(Info, "ETDump written to file '%s'.", filename);
94+
} else {
95+
ET_LOG(Error, "No ETDump data available!");
96+
return Error::NotFound;
97+
}
98+
99+
return Error::Ok;
100+
}
101+
102+
private:
103+
std::shared_ptr<EventTracer> event_tracer_ptr_;
104+
};
105+
65106
int main(int argc, char** argv) {
66107
executorch::runtime::runtime_init();
67108

@@ -164,20 +205,9 @@ int main(int argc, char** argv) {
164205
// the method can mutate the memory-planned buffers, so the method should only
165206
// be used by a single thread at at time, but it can be reused.
166207
//
167-
EventTracer* event_tracer_ptr = nullptr;
168-
#ifdef ET_EVENT_TRACER_ENABLED
169-
std::unique_ptr<FILE, decltype(&fclose)> etdump_file(
170-
fopen(FLAGS_etdump_path.c_str(), "w+"), fclose);
171-
ET_CHECK_MSG(
172-
etdump_file,
173-
"Failed to open ETDump file at %s.",
174-
FLAGS_etdump_path.c_str());
175-
176-
torch::executor::ETDumpGen etdump_gen = torch::executor::ETDumpGen();
177-
event_tracer_ptr = &etdump_gen;
178-
#endif // ET_EVENT_TRACER_ENABLED
179-
Result<Method> method =
180-
program->load_method(method_name, &memory_manager, event_tracer_ptr);
208+
EventTraceManager tracer;
209+
Result<Method> method = program->load_method(
210+
method_name, &memory_manager, tracer.get_event_tracer());
181211
ET_CHECK_MSG(
182212
method.ok(),
183213
"Loading of method %s failed with status 0x%" PRIx32,
@@ -204,7 +234,10 @@ int main(int argc, char** argv) {
204234
method_name,
205235
(uint32_t)status);
206236
}
207-
ET_LOG(Info, "Model executed successfully %i time(s).", FLAGS_num_executions);
237+
ET_LOG(
238+
Info,
239+
"Model executed successfully %" PRIu32 " time(s).",
240+
FLAGS_num_executions);
208241

209242
// Print the outputs.
210243
std::vector<EValue> outputs(method->outputs_size());
@@ -217,18 +250,14 @@ int main(int argc, char** argv) {
217250
std::cout << "Output " << i << ": " << outputs[i] << std::endl;
218251
}
219252

220-
#ifdef ET_EVENT_TRACER_ENABLED
221-
// Dump the ETDump data containing profiling/debugging data to the specified
222-
// file.
223-
torch::executor::etdump_result result = etdump_gen.get_etdump_data();
224-
if (result.buf != nullptr && result.size > 0) {
225-
fwrite((uint8_t*)result.buf, 1, result.size, etdump_file.get());
226-
free(result.buf);
227-
ET_LOG(Info, "ETDump written to file '%s'.", FLAGS_etdump_path.c_str());
228-
} else {
229-
ET_LOG(Error, "No ETDump data available!");
253+
if (tracer.get_event_tracer()) {
254+
// Dump ETDump data containing profiling/debugging data to specified file.
255+
Error status = tracer.write_etdump_to_file(FLAGS_etdump_path.c_str());
256+
ET_CHECK_MSG(
257+
status == Error::Ok,
258+
"Failed to save ETDump file at %s.",
259+
FLAGS_etdump_path.c_str());
230260
}
231-
#endif // ET_EVENT_TRACER_ENABLED
232261

233262
return 0;
234263
}

0 commit comments

Comments
 (0)