Skip to content

Commit 60dce2a

Browse files
authored
Don't let a later Ok discard an earlier resolve_operator error (pytorch#22357)
Summary: `Method::init()` resolves every `KernelCall`, recording failures in `delayed_error`, which is read once after the loop. The assignment ran unconditionally, including for `Error::Ok`, so a failure on any non-final instruction was overwritten by the next instruction's `Ok` and `init()` returned `Ok`. Only a failure on the last KernelCall survived. `OperatorMissing` and `MemoryAllocationFailed` have their own branches, so what was lost is the rest -- e.g. the `InvalidProgram` from the "Op index out of range" check. `err != Error::Ok` is the fix; `delayed_error == Error::Ok` keeps the first failure rather than the last. This unmasks errors that were previously swallowed, so a method that appeared to load may now correctly fail. Reviewed By: rascani Differential Revision: D118141579 Pull Request resolved: pytorch#22357
1 parent 2f964f8 commit 60dce2a

3 files changed

Lines changed: 76 additions & 8 deletions

File tree

‎runtime/executor/method.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,8 @@ Error Method::init(
10641064
num_instructions_missing_op++;
10651065
} else if (err == Error::MemoryAllocationFailed) {
10661066
return err;
1067-
} else {
1067+
} else if (err != Error::Ok && delayed_error == Error::Ok) {
1068+
// delayed_error is read after the loop; keep the first error.
10681069
delayed_error = err;
10691070
}
10701071
} break;

‎runtime/executor/test/program_validation_test.cpp‎

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
#include <executorch/extension/data_loader/buffer_data_loader.h>
1313
#include <executorch/extension/data_loader/file_data_loader.h>
1414
#include <executorch/runtime/core/error.h>
15+
#include <executorch/runtime/executor/method.h>
1516
#include <executorch/runtime/executor/program.h>
17+
#include <executorch/runtime/executor/test/managed_memory_manager.h>
18+
#include <executorch/runtime/kernel/operator_registry.h>
1619
#include <executorch/runtime/platform/runtime.h>
1720
#include <executorch/schema/program_generated.h>
1821

@@ -72,7 +75,10 @@ struct EValueConfig {
7275
// specifies which value indices appear in the execution plan's inputs list.
7376
std::vector<uint8_t> CreateTestProgram(
7477
const std::vector<EValueConfig>& configs,
75-
const std::vector<int32_t>& input_indices = {}) {
78+
const std::vector<int32_t>& input_indices = {},
79+
// If true, emit a chain of two KernelCalls: the first's op_index is out of
80+
// range so resolve_operator() fails on a non-final instruction.
81+
bool with_out_of_range_kernel_call = false) {
7682
flatbuffers::FlatBufferBuilder builder(1024);
7783

7884
std::vector<flatbuffers::Offset<executorch_flatbuffer::EValue>> evalues;
@@ -132,10 +138,37 @@ std::vector<uint8_t> CreateTestProgram(
132138
auto inputs_vec = builder.CreateVector(input_indices);
133139
auto empty_int_vec = builder.CreateVector(std::vector<int32_t>{});
134140
auto empty_int64_vec = builder.CreateVector(std::vector<int64_t>{0});
135-
auto empty_chain_vec = builder.CreateVector(
136-
std::vector<flatbuffers::Offset<executorch_flatbuffer::Chain>>{});
137-
auto empty_operators_vec = builder.CreateVector(
138-
std::vector<flatbuffers::Offset<executorch_flatbuffer::Operator>>{});
141+
std::vector<flatbuffers::Offset<executorch_flatbuffer::Chain>> chains;
142+
if (with_out_of_range_kernel_call) {
143+
auto kernel_call = executorch_flatbuffer::CreateKernelCall(
144+
builder, /*op_index=*/5, builder.CreateVector(std::vector<int32_t>{}));
145+
auto good_call = executorch_flatbuffer::CreateKernelCall(
146+
builder, /*op_index=*/0, builder.CreateVector(std::vector<int32_t>{}));
147+
std::vector<flatbuffers::Offset<executorch_flatbuffer::Instruction>>
148+
instructions{
149+
executorch_flatbuffer::CreateInstruction(
150+
builder,
151+
executorch_flatbuffer::InstructionArguments::KernelCall,
152+
kernel_call.Union()),
153+
// Resolves, so letting a later Ok overwrite the failure returns Ok.
154+
executorch_flatbuffer::CreateInstruction(
155+
builder,
156+
executorch_flatbuffer::InstructionArguments::KernelCall,
157+
good_call.Union())};
158+
chains.push_back(executorch_flatbuffer::CreateChain(
159+
builder,
160+
/*inputs=*/builder.CreateVector(std::vector<int32_t>{}),
161+
/*outputs=*/builder.CreateVector(std::vector<int32_t>{}),
162+
builder.CreateVector(instructions),
163+
/*stacktrace=*/0));
164+
}
165+
auto chain_vec = builder.CreateVector(chains);
166+
std::vector<flatbuffers::Offset<executorch_flatbuffer::Operator>> operators;
167+
if (with_out_of_range_kernel_call) {
168+
operators.push_back(executorch_flatbuffer::CreateOperator(
169+
builder, builder.CreateString("test::noop"), builder.CreateString("")));
170+
}
171+
auto operators_vec = builder.CreateVector(operators);
139172
auto empty_delegates_vec = builder.CreateVector(
140173
std::vector<
141174
flatbuffers::Offset<executorch_flatbuffer::BackendDelegate>>{});
@@ -147,8 +180,8 @@ std::vector<uint8_t> CreateTestProgram(
147180
values_vec,
148181
/*inputs=*/inputs_vec,
149182
/*outputs=*/empty_int_vec,
150-
empty_chain_vec,
151-
empty_operators_vec,
183+
chain_vec,
184+
operators_vec,
152185
empty_delegates_vec,
153186
empty_int64_vec);
154187

@@ -339,3 +372,35 @@ TEST_F(ProgramValidationTest, TensorListWithOutOfBoundsIndexDetected) {
339372
Program::load(&loader, Program::Verification::InternalConsistency);
340373
EXPECT_EQ(program.error(), Error::InvalidProgram);
341374
}
375+
376+
// A resolve_operator() failure is surfaced after the loop, so a later
377+
// instruction must not discard it.
378+
TEST_F(ProgramValidationTest, ResolveOperatorFailureSurfacesFromInit) {
379+
std::vector<EValueConfig> configs = {
380+
{EValueType::Tensor, {2, 2}, {}, /*is_dynamic=*/false}};
381+
382+
AlignedBuffer buf(CreateTestProgram(
383+
configs,
384+
/*input_indices=*/{},
385+
/*with_out_of_range_kernel_call=*/true));
386+
auto loader = buf.loader();
387+
388+
// Minimal, so validation doesn't reject the bad op_index before init() runs.
389+
Result<Program> program =
390+
Program::load(&loader, Program::Verification::Minimal);
391+
ASSERT_EQ(program.error(), Error::Ok);
392+
393+
// A keyless kernel matches any meta, so instruction 1 resolves.
394+
static const auto noop = executorch::runtime::Kernel(
395+
"test::noop",
396+
[](executorch::runtime::KernelRuntimeContext&,
397+
executorch::runtime::Span<executorch::runtime::EValue*>) {});
398+
ASSERT_EQ(executorch::runtime::register_kernel(noop), Error::Ok);
399+
400+
executorch::runtime::testing::ManagedMemoryManager mmm(
401+
/*planned_memory_bytes=*/32 * 1024U,
402+
/*method_allocator_bytes=*/32 * 1024U);
403+
Result<executorch::runtime::Method> method =
404+
program->load_method("forward", &mmm.get());
405+
EXPECT_EQ(method.error(), Error::InvalidProgram);
406+
}

‎runtime/executor/test/targets.bzl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ def define_common_targets(is_fbcode = False):
209209
"program_validation_test.cpp",
210210
],
211211
deps = [
212+
":managed_memory_manager",
212213
"//executorch/runtime/executor:program",
214+
"//executorch/runtime/kernel:operator_registry",
213215
"//executorch/extension/data_loader:buffer_data_loader",
214216
"//executorch/extension/data_loader:file_data_loader",
215217
"//executorch/schema:program",

0 commit comments

Comments
 (0)