Skip to content

Commit cb2ff0f

Browse files
tarun292facebook-github-bot
authored andcommitted
SDK example runner changes to suppport intermediate tensor logging (#1307)
Summary: Pull Request resolved: #1307 This diff adds support to the `sdk_example_runner` to take in command line parameters to enable intermediate/program output logging to etdump. Along with that we take in additional parameters for debug_buffer path and debug_buffer size. The debug_buffer is where the tensor and scalar data is stored (just the raw data not the metadata). Reviewed By: Jack-Khuu Differential Revision: D51599690 fbshipit-source-id: 3c4a00edd4db62000cc14990427f0144a83c3278
1 parent 23df70b commit cb2ff0f

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

examples/sdk/sdk_example_runner/sdk_example_runner.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ DEFINE_bool(
6161
false,
6262
"Print the output of the ET model to stdout, if needs.");
6363

64+
DEFINE_bool(dump_outputs, false, "Dump outputs to etdump file");
65+
66+
DEFINE_bool(
67+
dump_intermediate_outputs,
68+
false,
69+
"Dump intermediate outputs to etdump file.");
70+
71+
DEFINE_string(
72+
debug_output_path,
73+
"debug_output.bin",
74+
"Path to dump debug outputs to.");
75+
76+
DEFINE_int32(
77+
debug_buffer_size,
78+
262144, // 256 KB
79+
"Size of the debug buffer in bytes to allocate for intermediate outputs and program outputs logging.");
80+
6481
using namespace torch::executor;
6582
using torch::executor::util::FileDataLoader;
6683

@@ -199,6 +216,18 @@ int main(int argc, char** argv) {
199216
method.error());
200217
ET_LOG(Info, "Method loaded.");
201218

219+
void* debug_buffer = malloc(FLAGS_debug_buffer_size);
220+
if (FLAGS_dump_intermediate_outputs) {
221+
Span<uint8_t> buffer((uint8_t*)debug_buffer, FLAGS_debug_buffer_size);
222+
etdump_gen.set_debug_buffer(buffer);
223+
etdump_gen.set_event_tracer_debug_level(
224+
EventTracerDebugLogLevel::kIntermediateOutputs);
225+
} else if (FLAGS_dump_outputs) {
226+
Span<uint8_t> buffer((uint8_t*)debug_buffer, FLAGS_debug_buffer_size);
227+
etdump_gen.set_debug_buffer(buffer);
228+
etdump_gen.set_event_tracer_debug_level(
229+
EventTracerDebugLogLevel::kProgramOutputs);
230+
}
202231
// Prepare the inputs.
203232
exec_aten::ArrayRef<void*> inputs;
204233
// Use the inputs embedded in the bundled program.
@@ -264,5 +293,12 @@ int main(int argc, char** argv) {
264293
ET_LOG(Info, "Model verified successfully.");
265294
}
266295

296+
if (FLAGS_dump_outputs || FLAGS_dump_intermediate_outputs) {
297+
FILE* f = fopen(FLAGS_debug_output_path.c_str(), "w+");
298+
fwrite((uint8_t*)debug_buffer, 1, FLAGS_debug_buffer_size, f);
299+
fclose(f);
300+
}
301+
free(debug_buffer);
302+
267303
return 0;
268304
}

runtime/executor/method.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ Error Method::experimental_reset_execution() {
10351035
void Method::log_outputs() {
10361036
#ifdef ET_EVENT_TRACER_ENABLED
10371037
if (event_tracer_ != nullptr) {
1038-
if (event_tracer->event_tracer_debug_level() >=
1038+
if (event_tracer_->event_tracer_debug_level() >=
10391039
EventTracerDebugLogLevel::kProgramOutputs) {
10401040
for (size_t i = 0; i < outputs_size(); i++) {
10411041
internal::event_tracer_log_evalue_output(event_tracer_, get_output(i));

sdk/bundled_program/bundled_program.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <ATen/ATen.h>
1717
#endif // USE_ATEN_LIB
1818

19+
#include <executorch/runtime/core/event_tracer_hooks.h>
1920
#include <executorch/runtime/core/exec_aten/util/dim_order_util.h>
2021
#include <executorch/runtime/core/memory_allocator.h>
2122
#include <executorch/runtime/executor/method.h>
@@ -293,6 +294,9 @@ __ET_NODISCARD Error LoadBundledInput(
293294
status);
294295
}
295296

297+
internal::event_tracer_set_bundled_input_index(
298+
method.get_event_tracer(), testset_idx);
299+
296300
return Error::Ok;
297301
}
298302

0 commit comments

Comments
 (0)