Skip to content

Commit bc58034

Browse files
committed
Leave a method ready to run again after a kernel throws
Method::execute keeps its progress in the method, and resets it when an instruction returns an error. When a kernel or a backend throws a C++ exception instead, the exception leaves execute() halfway with the progress still set. Every later execute() then fails with InvalidState ("Method execution is in progress") until the method is loaded again. So a caller that catches the exception, such as a Python binding, cannot run the method again. The same exception also skips the reset of the temporary allocator that follows every instruction. Temporary memory the kernel took before it threw stays allocated. With the default allocator, which uses malloc, that memory leaks if the caller destroys the method instead of running it again. execute() now resets its progress however it ends, and each instruction resets the temporary allocator however it ends, also when an exception passes through. Runs that return normally behave as before. Builds without exception support are not affected.
1 parent ba0d826 commit bc58034

2 files changed

Lines changed: 70 additions & 9 deletions

File tree

‎runtime/executor/method.cpp‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,16 @@ Error Method::execute_instruction() {
17161716
size_t next_instr_idx = step_state_.instr_idx + 1;
17171717
Error err = Error::Ok;
17181718

1719+
// Reset the temp allocator for every instruction, even if a kernel throws.
1720+
struct TempMemoryReset {
1721+
MemoryAllocator* allocator;
1722+
~TempMemoryReset() {
1723+
if (allocator != nullptr) {
1724+
allocator->reset();
1725+
}
1726+
}
1727+
} temp_memory_reset{temp_allocator_};
1728+
17191729
switch (instruction->instr_args_type()) {
17201730
case executorch_flatbuffer::InstructionArguments::KernelCall: {
17211731
EXECUTORCH_SCOPE_PROF("OPERATOR_CALL");
@@ -1853,10 +1863,6 @@ Error Method::execute_instruction() {
18531863
static_cast<uint8_t>(instruction->instr_args_type()));
18541864
err = Error::InvalidProgram;
18551865
}
1856-
// Reset the temp allocator for every instruction.
1857-
if (temp_allocator_ != nullptr) {
1858-
temp_allocator_->reset();
1859-
}
18601866
if (err == Error::Ok) {
18611867
step_state_.instr_idx = next_instr_idx;
18621868
}
@@ -1972,6 +1978,13 @@ Error Method::execute() {
19721978
temp_allocator_->reset();
19731979
}
19741980

1981+
struct ExecutionGuard {
1982+
StepState& state;
1983+
~ExecutionGuard() {
1984+
state = StepState{0, 0};
1985+
}
1986+
} guard{step_state_};
1987+
19751988
// Chains are executed sequentially today, but future async designs may
19761989
// branch and run many in parallel or out of order.
19771990
size_t instruction_count = 0;
@@ -1999,7 +2012,6 @@ Error Method::execute() {
19992012
kMaxInstructions,
20002013
step_state_.chain_idx,
20012014
step_state_.instr_idx);
2002-
step_state_ = StepState{0, 0};
20032015
return Error::InvalidProgram;
20042016
}
20052017
++instruction_count;
@@ -2013,17 +2025,14 @@ Error Method::execute() {
20132025
static_cast<DebugHandle>(step_state_.instr_idx));
20142026
auto status = execute_instruction();
20152027
if (status != Error::Ok) {
2016-
step_state_ = StepState{0, 0};
20172028
return status;
20182029
}
20192030
}
20202031
}
20212032
internal::event_tracer_end_profiling_event(event_tracer_, event_tracer_entry);
20222033
log_outputs();
20232034

2024-
// TODO(jakeszwe, dbort): Decide on calling execute back to back without
2025-
// going through the reset api first.
2026-
return reset_execution(); // @lint-ignore CLANGTIDY facebook-hte-Deprecated
2035+
return Error::Ok;
20272036
}
20282037

20292038
MethodMeta Method::method_meta() const {

‎runtime/executor/test/kernel_integration_test.cpp‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <cstring>
1313
#include <memory>
14+
#include <stdexcept>
1415

1516
#include <executorch/extension/data_loader/file_data_loader.h>
1617
#include <executorch/extension/runner_util/inputs.h>
@@ -71,8 +72,12 @@ struct KernelControl {
7172
// The total size of all allocations.
7273
int total_allocated_size = 0;
7374

75+
// If true, the kernel throws after any temporary allocation.
76+
bool throw_exception = false;
77+
7478
void reset() {
7579
call_count = 0;
80+
throw_exception = false;
7681
call_context_fail = false;
7782
fail_value = Error::Ok;
7883
allocate_temp_memory = false;
@@ -160,6 +165,12 @@ struct KernelControl {
160165
control->total_allocated_size += control->temp_memory_size;
161166
EXPECT_EQ(temp_mem_res.error(), Error::Ok);
162167
}
168+
169+
#if ET_HAS_EXCEPTIONS
170+
if (control->throw_exception) {
171+
throw std::runtime_error("kernel throws");
172+
}
173+
#endif
163174
}
164175

165176
static bool registered_;
@@ -298,6 +309,35 @@ TEST_F(KernelIntegrationTest, KernelHookIsCalled) {
298309
EXPECT_EQ(control_->call_count, 2);
299310
}
300311

312+
#if ET_HAS_EXCEPTIONS
313+
TEST_F(KernelIntegrationTest, ThrowingKernelLeavesMethodReadyToRunAgain) {
314+
// ModuleAddMul runs mul before add, so add throws past the first instruction.
315+
static const Error mul_registered =
316+
executorch::runtime::register_kernel(Kernel(
317+
"aten::mul.out",
318+
KernelKey("v1/6;0,1|6;0,1|6;0,1|6;0,1"),
319+
[](KernelRuntimeContext&, Span<EValue*>) {}));
320+
ASSERT_EQ(mul_registered, Error::Ok);
321+
Result<FileDataLoader> loader =
322+
FileDataLoader::from(std::getenv("ET_MODULE_ADD_MUL_PATH"));
323+
ASSERT_EQ(loader.error(), Error::Ok);
324+
Result<Program> program = Program::load(&loader.get());
325+
ASSERT_EQ(program.error(), Error::Ok);
326+
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
327+
Result<Method> method = program->load_method("forward", &mmm.get());
328+
ASSERT_EQ(method.error(), Error::Ok);
329+
auto inputs = executorch::extension::prepare_input_tensors(*method);
330+
ASSERT_EQ(inputs.error(), Error::Ok);
331+
332+
control_->throw_exception = true;
333+
EXPECT_THROW((void)method->execute(), std::runtime_error);
334+
EXPECT_FALSE(method->in_progress());
335+
336+
control_->throw_exception = false;
337+
EXPECT_EQ(method->execute(), Error::Ok);
338+
}
339+
#endif
340+
301341
TEST_F(KernelIntegrationTest, FailurePropagates) {
302342
// Tell the kernel to fail.
303343
control_->call_context_fail = true;
@@ -343,6 +383,18 @@ TEST_F(KernelIntegrationTest, DefaultPlatformMemoryAllocator) {
343383
EXPECT_EQ(control_->total_allocated_size, 12);
344384
}
345385

386+
#if ET_HAS_EXCEPTIONS
387+
TEST_F(KernelTempMemoryAllocatorIntegrationTest, ThrowResetsTempMemory) {
388+
control_->simulate_temp_memory_allocation = true;
389+
control_->temp_memory_size = 4;
390+
control_->throw_exception = true;
391+
392+
EXPECT_THROW((void)method_->execute(), std::runtime_error);
393+
EXPECT_EQ(temp_allocator_->number_of_allocations, 1);
394+
EXPECT_EQ(temp_allocator_->currently_allocated_size, 0);
395+
}
396+
#endif
397+
346398
TEST_F(KernelTempMemoryAllocatorIntegrationTest, UsingTempMemoryAllocator) {
347399
// In this test we provide a temp allocator to the method, and tell the kernel
348400
// to allocate memory using it. We want to make sure that the kernel uses the

0 commit comments

Comments
 (0)