Skip to content

Commit 7ed453f

Browse files
rascaniRJ Ascani
andauthored
Add in_progress() state check to Method::execute() (pytorch#16531)
### Summary Adds in_progress() to detect when execution is mid-way through the instruction sequence. Method::execute() now returns InvalidState if called while in_progress() is true. execute() auto-resets step_state_ on error to allow retry attempts. step() does not auto-reset (requires manual reset_execution() call). ### Test plan ``` ./test/run_oss_cpp_tests.sh ``` Co-authored-by: RJ Ascani <rja@fb.com>
1 parent dbf3c37 commit 7ed453f

3 files changed

Lines changed: 122 additions & 5 deletions

File tree

‎runtime/executor/method.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,11 @@ Error Method::experimental_step() {
15741574
return step();
15751575
}
15761576

1577+
bool Method::in_progress() const {
1578+
return (step_state_.chain_idx != 0 || step_state_.instr_idx != 0) &&
1579+
step_state_.chain_idx < n_chains_;
1580+
}
1581+
15771582
Error Method::execute() {
15781583
internal::event_tracer_create_event_block(event_tracer_, "Execute");
15791584
EventTracerEntry event_tracer_entry =
@@ -1584,6 +1589,8 @@ Error Method::execute() {
15841589
initialized(),
15851590
NotSupported,
15861591
"Cannot execute until method has been initialized.");
1592+
ET_CHECK_OR_RETURN_ERROR(
1593+
!in_progress(), InvalidState, "Method execution is in progress");
15871594
const size_t n_input = inputs_size();
15881595
for (size_t i = 0; i < n_input; ++i) {
15891596
ET_CHECK_OR_RETURN_ERROR(
@@ -1622,6 +1629,7 @@ Error Method::execute() {
16221629
static_cast<DebugHandle>(step_state_.instr_idx));
16231630
auto status = execute_instruction();
16241631
if (status != Error::Ok) {
1632+
step_state_ = StepState{0, 0};
16251633
return status;
16261634
}
16271635
}

‎runtime/executor/method.h‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,12 @@ class Method final {
217217
/**
218218
* Execute the method.
219219
*
220-
* NOTE: Will fail if the method has been partially executed using the
221-
* `step()` api.
220+
* NOTE: Will fail with Error::InvalidState if the method has been partially
221+
* executed using the `step()` api. Call reset_execution() after step()
222+
* reaches EndOfMethod before calling execute().
223+
*
224+
* If execute() encounters an error mid-execution, the state is automatically
225+
* reset to allow retry attempts.
222226
*
223227
* @returns Error::Ok on success, non-Ok on failure.
224228
*/
@@ -236,14 +240,29 @@ class Method final {
236240
/// DEPRECATED: Use `step()` instead.
237241
ET_DEPRECATED ET_NODISCARD Error experimental_step();
238242

243+
/**
244+
* EXPERIMENTAL: Returns true if method execution is in progress.
245+
*
246+
* Execution is considered "in progress" when step() has been called and
247+
* execution is mid-way through the instruction sequence.
248+
*
249+
* @retval true if execution is in progress (not at initial state, not at end)
250+
* @retval false if at initial state, after completion, or after any error
251+
*
252+
* @note execute() automatically resets state on error, so in_progress()
253+
* returns false immediately after execute() fails.
254+
*/
255+
ET_EXPERIMENTAL ET_NODISCARD bool in_progress() const;
256+
239257
/**
240258
* EXPERIMENTAL: Resets execution state to the start of the Method. For use
241259
* with the `step()` API.
242260
*
243-
* @retval Error:Ok on success
261+
* @retval Error::Ok on success
244262
* @retval Error::InvalidState if called before step-based execution reached
245-
* the end of the Method. This means it is not possible to recover a
246-
* Method that failed mid-execution.
263+
* the end of the Method. When using step(), you must step through to
264+
* EndOfMethod before resetting. Note: execute() handles its own error
265+
* recovery and does not require manual reset.
247266
*/
248267
ET_EXPERIMENTAL ET_NODISCARD Error reset_execution();
249268

‎runtime/executor/test/method_test.cpp‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,96 @@ TEST_F(MethodTest, MethodGetAttributeTest) {
430430
EXPECT_EQ(res->const_data_ptr<int32_t>()[0], 1);
431431
}
432432

433+
TEST_F(MethodTest, InProgressInitialState) {
434+
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
435+
Result<Method> method = programs_["add"]->load_method("forward", &mmm.get());
436+
ASSERT_EQ(method.error(), Error::Ok);
437+
438+
EXPECT_FALSE(method->in_progress());
439+
}
440+
441+
TEST_F(MethodTest, InProgressDuringStepExecution) {
442+
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
443+
Result<Method> method =
444+
programs_["add_mul"]->load_method("forward", &mmm.get());
445+
ASSERT_EQ(method.error(), Error::Ok);
446+
447+
auto input_cleanup = prepare_input_tensors(*method);
448+
ASSERT_EQ(input_cleanup.error(), Error::Ok);
449+
450+
Error err = method->step();
451+
ASSERT_EQ(err, Error::Ok);
452+
453+
EXPECT_TRUE(method->in_progress());
454+
455+
while (err != Error::EndOfMethod) {
456+
err = method->step();
457+
ASSERT_TRUE(err == Error::Ok || err == Error::EndOfMethod);
458+
}
459+
460+
EXPECT_FALSE(method->in_progress());
461+
}
462+
463+
TEST_F(MethodTest, ExecuteFailsWhenInProgress) {
464+
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
465+
Result<Method> method =
466+
programs_["add_mul"]->load_method("forward", &mmm.get());
467+
ASSERT_EQ(method.error(), Error::Ok);
468+
469+
auto input_cleanup = prepare_input_tensors(*method);
470+
ASSERT_EQ(input_cleanup.error(), Error::Ok);
471+
472+
Error err = method->step();
473+
ASSERT_EQ(err, Error::Ok);
474+
ASSERT_TRUE(method->in_progress());
475+
476+
err = method->execute();
477+
EXPECT_EQ(err, Error::InvalidState);
478+
}
479+
480+
TEST_F(MethodTest, ExecuteSucceedsAfterReset) {
481+
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
482+
Result<Method> method = programs_["add"]->load_method("forward", &mmm.get());
483+
ASSERT_EQ(method.error(), Error::Ok);
484+
485+
auto input_cleanup = prepare_input_tensors(*method);
486+
ASSERT_EQ(input_cleanup.error(), Error::Ok);
487+
auto input_err = method->set_input(EValue(1.0), 2);
488+
ASSERT_EQ(input_err, Error::Ok);
489+
490+
Error err = Error::Ok;
491+
while (err != Error::EndOfMethod) {
492+
err = method->step();
493+
ASSERT_TRUE(err == Error::Ok || err == Error::EndOfMethod);
494+
}
495+
496+
err = method->reset_execution();
497+
ASSERT_EQ(err, Error::Ok);
498+
EXPECT_FALSE(method->in_progress());
499+
500+
err = method->execute();
501+
EXPECT_EQ(err, Error::Ok);
502+
}
503+
504+
TEST_F(MethodTest, ExecuteResetsOnError) {
505+
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
506+
Result<Method> method = programs_["add"]->load_method("forward", &mmm.get());
507+
ASSERT_EQ(method.error(), Error::Ok);
508+
509+
Error err = method->execute();
510+
EXPECT_NE(err, Error::Ok);
511+
512+
EXPECT_FALSE(method->in_progress());
513+
514+
auto input_cleanup = prepare_input_tensors(*method);
515+
ASSERT_EQ(input_cleanup.error(), Error::Ok);
516+
auto input_err = method->set_input(EValue(1.0), 2);
517+
ASSERT_EQ(input_err, Error::Ok);
518+
519+
err = method->execute();
520+
EXPECT_EQ(err, Error::Ok);
521+
}
522+
433523
/*
434524
* TODO(T161163608): Test is disabled due to a resize bug in tensor_index_out of
435525
* the portable op lib

0 commit comments

Comments
 (0)