Skip to content
Merged
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
8 changes: 7 additions & 1 deletion runtime/executor/method_meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,13 @@ Result<const char*> MethodMeta::get_backend_name(size_t index) const {
"Index %zu out of range. num_backends: %zu",
index,
count);
return s_plan_->delegates()->Get(index)->id()->c_str();
const auto delegate = s_plan_->delegates()->Get(index);
ET_CHECK_OR_RETURN_ERROR(
delegate != nullptr && delegate->id() != nullptr,
InvalidProgram,
"Missing backend id at index %zu",
index);
return delegate->id()->c_str();
}

size_t MethodMeta::num_instructions() const {
Expand Down
37 changes: 32 additions & 5 deletions runtime/executor/test/method_meta_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,13 @@ TEST_F(MethodMetaTest, MethodMetaBufferDeviceReturnsCudaForDeviceBuffer) {
Error::InvalidArgument);
}

TEST_F(MethodMetaTest, UsesBackendOnUnsetDelegatesReturnsFalse) {
// Construct a minimal schema-valid FlatBuffer program where delegates is
// unset (nullptr).
flatbuffers::FlatBufferBuilder fbb;
// Builds a minimal schema-valid FlatBuffer program whose "forward" plan has
// `delegates`, which may be null to leave them unset.
std::vector<uint8_t> program_with_delegates(
flatbuffers::FlatBufferBuilder& fbb,
const std::vector<
flatbuffers::Offset<executorch_flatbuffer::BackendDelegate>>*
delegates) {
std::vector<int32_t> empty_inputs = {};
std::vector<int32_t> empty_outputs = {};
std::vector<int64_t> non_const_buffer_sizes = {0};
Expand All @@ -300,7 +303,7 @@ TEST_F(MethodMetaTest, UsesBackendOnUnsetDelegatesReturnsFalse) {
/*outputs=*/&empty_outputs,
/*chains=*/nullptr,
/*operators=*/nullptr,
/*delegates=*/nullptr,
/*delegates=*/delegates,
/*non_const_buffer_sizes=*/&non_const_buffer_sizes,
/*non_const_buffer_device=*/nullptr);
std::vector<flatbuffers::Offset<executorch_flatbuffer::ExecutionPlan>> plans =
Expand Down Expand Up @@ -328,6 +331,13 @@ TEST_F(MethodMetaTest, UsesBackendOnUnsetDelegatesReturnsFalse) {
if (buffer.size() < Program::kMinHeadBytes) {
buffer.resize(Program::kMinHeadBytes, 0);
}
return buffer;
}

TEST_F(MethodMetaTest, UsesBackendOnUnsetDelegatesReturnsFalse) {
flatbuffers::FlatBufferBuilder fbb;
std::vector<uint8_t> buffer =
program_with_delegates(fbb, /*delegates=*/nullptr);

BufferDataLoader loader(buffer.data(), buffer.size());
Result<Program> prog = Program::load(&loader, Program::Verification::Minimal);
Expand All @@ -338,3 +348,20 @@ TEST_F(MethodMetaTest, UsesBackendOnUnsetDelegatesReturnsFalse) {

EXPECT_FALSE(method_meta->uses_backend("xnnpack"));
}

TEST_F(MethodMetaTest, GetBackendNameOfDelegateWithoutIdReturnsError) {
flatbuffers::FlatBufferBuilder fbb;
std::vector<flatbuffers::Offset<executorch_flatbuffer::BackendDelegate>>
delegates = {executorch_flatbuffer::CreateBackendDelegate(fbb)};
std::vector<uint8_t> buffer = program_with_delegates(fbb, &delegates);

BufferDataLoader loader(buffer.data(), buffer.size());
Result<Program> prog = Program::load(&loader, Program::Verification::Minimal);
ASSERT_EQ(prog.error(), Error::Ok);

Result<MethodMeta> method_meta = prog->method_meta("forward");
ASSERT_EQ(method_meta.error(), Error::Ok);

ASSERT_EQ(method_meta->num_backends(), 1);
EXPECT_EQ(method_meta->get_backend_name(0).error(), Error::InvalidProgram);
}