diff --git a/runtime/executor/method.cpp b/runtime/executor/method.cpp index e512081d6ff..2d36110913b 100644 --- a/runtime/executor/method.cpp +++ b/runtime/executor/method.cpp @@ -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"); @@ -1853,10 +1863,6 @@ Error Method::execute_instruction() { static_cast(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; } @@ -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; @@ -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; @@ -2013,7 +2025,6 @@ Error Method::execute() { static_cast(step_state_.instr_idx)); auto status = execute_instruction(); if (status != Error::Ok) { - step_state_ = StepState{0, 0}; return status; } } @@ -2021,9 +2032,7 @@ Error Method::execute() { 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 { diff --git a/runtime/executor/test/kernel_integration_test.cpp b/runtime/executor/test/kernel_integration_test.cpp index 61d17f9be1e..33a9cca11a4 100644 --- a/runtime/executor/test/kernel_integration_test.cpp +++ b/runtime/executor/test/kernel_integration_test.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -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; @@ -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_; @@ -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) {})); + ASSERT_EQ(mul_registered, Error::Ok); + Result loader = + FileDataLoader::from(std::getenv("ET_MODULE_ADD_MUL_PATH")); + ASSERT_EQ(loader.error(), Error::Ok); + Result program = Program::load(&loader.get()); + ASSERT_EQ(program.error(), Error::Ok); + ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes); + Result 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; @@ -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