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
6 changes: 6 additions & 0 deletions runtime/executor/method_meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,14 @@ Result<etensor::Device> MethodMeta::memory_planned_buffer_device(
bool MethodMeta::uses_backend(const char* backend_name) const {
ET_CHECK_MSG(backend_name, "backend name is null");
const auto delegates = s_plan_->delegates();
if (delegates == nullptr) {
return false;
}
for (size_t i = 0; i < delegates->size(); i++) {
Comment thread
Copilot marked this conversation as resolved.
auto delegate = delegates->Get(i);
if (delegate == nullptr || delegate->id() == nullptr) {
continue;
}
auto backend_name_len = std::strlen(backend_name);
auto delegate_id_len = delegate->id()->size();
if (backend_name_len == delegate_id_len &&
Expand Down
18 changes: 16 additions & 2 deletions runtime/executor/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ add_custom_command(
"${CMAKE_CURRENT_BINARY_DIR}/ModuleMultipleEntry.pte"
"${CMAKE_CURRENT_BINARY_DIR}/ModuleSimpleTrain.pte"
"${CMAKE_CURRENT_BINARY_DIR}/ModuleStateful.pte"
"${CMAKE_CURRENT_BINARY_DIR}/ModuleAddWithDevice.pte"
"${CMAKE_CURRENT_BINARY_DIR}/delegated/ModuleAddMul.pte"
COMMAND ${_export_program_cmd}
COMMAND
Expand All @@ -62,6 +63,9 @@ add_custom_command(
${PYTHON_EXECUTABLE} -m test.models.export_delegated_program --modules
"ModuleAddMul" --backend_id "StubBackend" --outdir
"${CMAKE_CURRENT_BINARY_DIR}/delegated/"
COMMAND
${PYTHON_EXECUTABLE} -m test.models.export_program_with_device_info
--outdir "${CMAKE_CURRENT_BINARY_DIR}"
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
)

Expand All @@ -77,6 +81,7 @@ add_custom_target(
"${CMAKE_CURRENT_BINARY_DIR}/ModuleMultipleEntry.pte"
"${CMAKE_CURRENT_BINARY_DIR}/ModuleSimpleTrain.pte"
"${CMAKE_CURRENT_BINARY_DIR}/ModuleStateful.pte"
"${CMAKE_CURRENT_BINARY_DIR}/ModuleAddWithDevice.pte"
)

set(test_env
Expand All @@ -92,6 +97,7 @@ set(test_env
"ET_MODULE_SIMPLE_TRAIN_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModuleSimpleTrain.pte"
"ET_MODULE_STATEFUL_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModuleStateful.pte"
"ET_MODULE_ADD_MUL_DELEGATED_PATH=${CMAKE_CURRENT_BINARY_DIR}/delegated/ModuleAddMul.pte"
"ET_MODULE_ADD_WITH_DEVICE_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModuleAddWithDevice.pte"
)

et_cxx_test(
Expand Down Expand Up @@ -130,8 +136,16 @@ et_cxx_test(
add_dependencies(method_test generated_pte_files)
set_property(TEST method_test PROPERTY ENVIRONMENT ${test_env})

# TODO(T191569140): Enable this test. et_cxx_test(method_meta_test SOURCES
# method_meta_test.cpp EXTRA_LIBS extension_data_loader)
et_cxx_test(
method_meta_test
SOURCES
method_meta_test.cpp
EXTRA_LIBS
extension_data_loader
program_schema
)
add_dependencies(method_meta_test generated_pte_files)
set_property(TEST method_meta_test PROPERTY ENVIRONMENT ${test_env})

et_cxx_test(
program_test SOURCES program_test.cpp EXTRA_LIBS extension_data_loader
Expand Down
62 changes: 62 additions & 0 deletions runtime/executor/test/method_meta_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
#include <limits>
#include <vector>

#include <executorch/extension/data_loader/buffer_data_loader.h>
#include <executorch/extension/data_loader/file_data_loader.h>
#include <executorch/runtime/core/exec_aten/exec_aten.h>
#include <executorch/runtime/executor/program.h>
#include <executorch/runtime/platform/runtime.h>
#include <executorch/schema/program_generated.h>
Comment on lines +15 to +20

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, added //executorch/extension/data_loader:buffer_data_loader and //executorch/schema:program to the method_meta_test deps in targets.bzl.

#include <executorch/test/utils/DeathTest.h>
#include <gtest/gtest.h>

Expand All @@ -25,6 +28,7 @@ using executorch::runtime::Program;
using executorch::runtime::Result;
using executorch::runtime::Span;
using executorch::runtime::TensorInfo;
using torch::executor::util::BufferDataLoader;
using torch::executor::util::FileDataLoader;

namespace executorch {
Expand Down Expand Up @@ -72,6 +76,9 @@ class MethodMetaTest : public ::testing::Test {
}

void SetUp() override {
// Required to initialize the PAL timer before tests trigger ET_LOG calls in
// et_pal_current_ticks().
executorch::runtime::runtime_init();
load_program(std::getenv("ET_MODULE_ADD_PATH"), "add");
load_program(std::getenv("ET_MODULE_STATEFUL_PATH"), "stateful");
const char* device_path = std::getenv("ET_MODULE_ADD_WITH_DEVICE_PATH");
Expand Down Expand Up @@ -275,3 +282,58 @@ TEST_F(MethodMetaTest, MethodMetaBufferDeviceReturnsCudaForDeviceBuffer) {
method_meta->memory_planned_buffer_device(2).error(),
Error::InvalidArgument);
}

TEST_F(MethodMetaTest, UsesBackendOnUnsetDelegatesReturnsFalse) {
// Construct a minimal schema-valid FlatBuffer program where delegates is
// unset (nullptr).
flatbuffers::FlatBufferBuilder fbb;
Comment on lines +286 to +289

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, method_meta_test was disabled behind TODO(T191569140). Re-enabled it in CMakeLists.txt with the required extension_data_loader/program_schema libs.

std::vector<int32_t> empty_inputs = {};
std::vector<int32_t> empty_outputs = {};
std::vector<int64_t> non_const_buffer_sizes = {0};
auto plan = executorch_flatbuffer::CreateExecutionPlanDirect(
fbb,
/*name=*/"forward",
/*container_meta_type=*/0,
/*values=*/nullptr,
/*inputs=*/&empty_inputs,
/*outputs=*/&empty_outputs,
/*chains=*/nullptr,
/*operators=*/nullptr,
/*delegates=*/nullptr,
/*non_const_buffer_sizes=*/&non_const_buffer_sizes,
/*non_const_buffer_device=*/nullptr);
std::vector<flatbuffers::Offset<executorch_flatbuffer::ExecutionPlan>> plans =
{plan};

auto const_offsets = fbb.CreateVector(std::vector<uint64_t>{0});
auto constant_segment = executorch_flatbuffer::CreateSubsegmentOffsets(
fbb, /*segment_index=*/0, const_offsets);
std::vector<flatbuffers::Offset<executorch_flatbuffer::DataSegment>>
segments = {executorch_flatbuffer::CreateDataSegment(
fbb, /*offset=*/0, /*size=*/0)};

auto program = executorch_flatbuffer::CreateProgramDirect(
fbb,
/*version=*/Program::kMaxSupportedSchemaVersion,
/*execution_plan=*/&plans,
/*constant_buffer=*/nullptr,
/*backend_delegate_data=*/nullptr,
/*segments=*/&segments,
/*constant_segment=*/constant_segment);
executorch_flatbuffer::FinishProgramBuffer(fbb, program);

std::vector<uint8_t> buffer(
fbb.GetBufferPointer(), fbb.GetBufferPointer() + fbb.GetSize());
if (buffer.size() < Program::kMinHeadBytes) {
buffer.resize(Program::kMinHeadBytes, 0);
}

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);

EXPECT_FALSE(method_meta->uses_backend("xnnpack"));
}
2 changes: 2 additions & 0 deletions runtime/executor/test/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ def define_common_targets(is_fbcode = False):
],
deps = [
"//executorch/runtime/executor:program",
"//executorch/extension/data_loader:buffer_data_loader",
"//executorch/extension/data_loader:file_data_loader",
"//executorch/schema:program",
],
env = dict(
modules_env,
Expand Down
Loading