Skip to content

Commit b024c7c

Browse files
committed
Fix inputs lifetime issue in executor runner
Also slightly change condition in order to avoid confusing print, 'Loading inputs from input file(s)' when no files where provided. Change-Id: Icc9c1faa50a1c00e0a6ccf12c85b687af736f6b2
1 parent 14e2070 commit b024c7c

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

examples/portable/executor_runner/executor_runner.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <fstream>
2424
#include <iostream>
2525
#include <memory>
26+
#include <optional>
2627

2728
#include <gflags/gflags.h>
2829

@@ -45,8 +46,6 @@
4546
#include <executorch/extension/threadpool/cpuinfo_utils.h>
4647
#include <executorch/extension/threadpool/threadpool.h>
4748
#include <executorch/extension/threadpool/threadpool_guard.h>
48-
49-
#include <optional>
5049
#endif
5150

5251
#ifdef ET_BUNDLE_IO_ENABLED
@@ -288,8 +287,8 @@ int main(int argc, char** argv) {
288287
Info,
289288
"PTD data map created with %" PRIu64 " keys.",
290289
static_cast<uint64_t>(ptd_data_map->get_num_keys().get()));
291-
} else {
292-
ET_LOG(Info, "Load inputs from input file(s).");
290+
} else if (!FLAGS_inputs.empty()) {
291+
ET_LOG(Info, "Loading inputs from input file(s).");
293292
std::stringstream list_of_input_files(FLAGS_inputs);
294293
std::string path;
295294

@@ -453,14 +452,16 @@ int main(int argc, char** argv) {
453452
// Run the model.
454453
for (uint32_t i = 0; i < FLAGS_num_executions; i++) {
455454
// Allocate input tensors and set all of their elements to 1 or to the
456-
// contents of input_buffers if available. The `inputs`
455+
// contents of input_buffers if available. For non bundled IO, the `inputs`
457456
// variable owns the allocated memory and must live past the last call to
458457
// `execute()`.
459458
//
460459
// NOTE: we have to re-prepare input tensors on every execution
461460
// because inputs whose space gets reused by memory planning (if
462461
// any such inputs exist) will not be preserved for the next
463462
// execution.
463+
std::optional<executorch::extension::BufferCleanup> inputs;
464+
464465
#ifdef ET_BUNDLE_IO_ENABLED
465466
if (bundle_io) {
466467
ET_LOG(Debug, "Getting inputs from bundled IO");
@@ -474,12 +475,13 @@ int main(int argc, char** argv) {
474475
#endif
475476
{
476477
ET_LOG(Debug, "Preparing inputs.");
477-
auto inputs = executorch::extension::prepare_input_tensors(
478+
auto res = executorch::extension::prepare_input_tensors(
478479
*method, {}, input_buffers);
479480
ET_CHECK_MSG(
480-
inputs.ok(),
481+
res.ok(),
481482
"Could not prepare inputs: 0x%" PRIx32,
482-
(uint32_t)inputs.error());
483+
(uint32_t)res.error());
484+
inputs.emplace(std::move(res.get()));
483485
ET_LOG(Debug, "Inputs prepared.");
484486
}
485487

0 commit comments

Comments
 (0)