Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions runtime/executor/method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,16 @@ Error Method::execute_instruction() {
size_t next_instr_idx = step_state_.instr_idx + 1;
Error err = Error::Ok;

// Reset the temp allocator for every instruction, even if a kernel throws.
struct TempMemoryReset {
MemoryAllocator* allocator;
~TempMemoryReset() {
if (allocator != nullptr) {
allocator->reset();
}
}
} temp_memory_reset{temp_allocator_};

switch (instruction->instr_args_type()) {
case executorch_flatbuffer::InstructionArguments::KernelCall: {
EXECUTORCH_SCOPE_PROF("OPERATOR_CALL");
Expand Down Expand Up @@ -1853,10 +1863,6 @@ Error Method::execute_instruction() {
static_cast<uint8_t>(instruction->instr_args_type()));
err = Error::InvalidProgram;
}
// Reset the temp allocator for every instruction.
if (temp_allocator_ != nullptr) {
temp_allocator_->reset();
}
if (err == Error::Ok) {
step_state_.instr_idx = next_instr_idx;
}
Expand Down Expand Up @@ -1972,6 +1978,13 @@ Error Method::execute() {
temp_allocator_->reset();
}

struct ExecutionGuard {
StepState& state;
~ExecutionGuard() {
state = StepState{0, 0};
}
} guard{step_state_};

// Chains are executed sequentially today, but future async designs may
// branch and run many in parallel or out of order.
size_t instruction_count = 0;
Expand Down Expand Up @@ -1999,7 +2012,6 @@ Error Method::execute() {
kMaxInstructions,
step_state_.chain_idx,
step_state_.instr_idx);
step_state_ = StepState{0, 0};
return Error::InvalidProgram;
}
++instruction_count;
Expand All @@ -2013,17 +2025,14 @@ Error Method::execute() {
static_cast<DebugHandle>(step_state_.instr_idx));
auto status = execute_instruction();
if (status != Error::Ok) {
step_state_ = StepState{0, 0};
return status;
}
}
}
internal::event_tracer_end_profiling_event(event_tracer_, event_tracer_entry);
log_outputs();

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

MethodMeta Method::method_meta() const {
Expand Down
52 changes: 52 additions & 0 deletions runtime/executor/test/kernel_integration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <cstring>
#include <memory>
#include <stdexcept>

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

// If true, the kernel throws after any temporary allocation.
bool throw_exception = false;

void reset() {
call_count = 0;
throw_exception = false;
call_context_fail = false;
fail_value = Error::Ok;
allocate_temp_memory = false;
Expand Down Expand Up @@ -160,6 +165,12 @@ struct KernelControl {
control->total_allocated_size += control->temp_memory_size;
EXPECT_EQ(temp_mem_res.error(), Error::Ok);
}

#if ET_HAS_EXCEPTIONS
if (control->throw_exception) {
throw std::runtime_error("kernel throws");
}
#endif
}

static bool registered_;
Expand Down Expand Up @@ -298,6 +309,35 @@ TEST_F(KernelIntegrationTest, KernelHookIsCalled) {
EXPECT_EQ(control_->call_count, 2);
}

#if ET_HAS_EXCEPTIONS
TEST_F(KernelIntegrationTest, ThrowingKernelLeavesMethodReadyToRunAgain) {
// ModuleAddMul runs mul before add, so add throws past the first instruction.
static const Error mul_registered =
executorch::runtime::register_kernel(Kernel(
"aten::mul.out",
KernelKey("v1/6;0,1|6;0,1|6;0,1|6;0,1"),
[](KernelRuntimeContext&, Span<EValue*>) {}));
ASSERT_EQ(mul_registered, Error::Ok);
Result<FileDataLoader> loader =
FileDataLoader::from(std::getenv("ET_MODULE_ADD_MUL_PATH"));
ASSERT_EQ(loader.error(), Error::Ok);
Result<Program> program = Program::load(&loader.get());
ASSERT_EQ(program.error(), Error::Ok);
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
Result<Method> method = program->load_method("forward", &mmm.get());
ASSERT_EQ(method.error(), Error::Ok);
auto inputs = executorch::extension::prepare_input_tensors(*method);
ASSERT_EQ(inputs.error(), Error::Ok);

control_->throw_exception = true;
EXPECT_THROW((void)method->execute(), std::runtime_error);
EXPECT_FALSE(method->in_progress());

control_->throw_exception = false;
EXPECT_EQ(method->execute(), Error::Ok);
}
#endif

TEST_F(KernelIntegrationTest, FailurePropagates) {
// Tell the kernel to fail.
control_->call_context_fail = true;
Expand Down Expand Up @@ -343,6 +383,18 @@ TEST_F(KernelIntegrationTest, DefaultPlatformMemoryAllocator) {
EXPECT_EQ(control_->total_allocated_size, 12);
}

#if ET_HAS_EXCEPTIONS
TEST_F(KernelTempMemoryAllocatorIntegrationTest, ThrowResetsTempMemory) {
control_->simulate_temp_memory_allocation = true;
control_->temp_memory_size = 4;
control_->throw_exception = true;

EXPECT_THROW((void)method_->execute(), std::runtime_error);
EXPECT_EQ(temp_allocator_->number_of_allocations, 1);
EXPECT_EQ(temp_allocator_->currently_allocated_size, 0);
}
#endif

TEST_F(KernelTempMemoryAllocatorIntegrationTest, UsingTempMemoryAllocator) {
// In this test we provide a temp allocator to the method, and tell the kernel
// to allocate memory using it. We want to make sure that the kernel uses the
Expand Down
Loading