Skip to content

MethodMeta API to query of backends number and names #9440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 20, 2025
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
13 changes: 13 additions & 0 deletions runtime/executor/method_meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,19 @@ bool MethodMeta::uses_backend(const char* backend_name) const {
return false;
}

size_t MethodMeta::num_backends() const {
const auto delegates = s_plan_->delegates();
return delegates ? delegates->size() : 0;
}

const char* MethodMeta::get_backend_name(size_t index) const {
const auto delegates = s_plan_->delegates();
if (delegates && index < delegates->size()) {
return delegates->Get(index)->id()->c_str();
}
return nullptr;
}

size_t MethodMeta::num_instructions() const {
const auto chains = s_plan_->chains();
if (chains == nullptr) {
Expand Down
16 changes: 16 additions & 0 deletions runtime/executor/method_meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ class MethodMeta final {
*/
bool uses_backend(const char* backend_name) const;

/**
* Get the number of backends used in this method.
*
* @returns The total number of backend names.
*/
size_t num_backends() const;

/**
* Get the backend name at the given index.
*
* @param[in] index The index of the backend name.
* @returns The backend name as a C-style string, or nullptr if the index is
* invalid.
*/
const char* get_backend_name(size_t index) const;

/**
* Get the number of instructions in this method.
*
Expand Down
29 changes: 29 additions & 0 deletions runtime/executor/test/backend_integration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,35 @@ TEST_P(BackendIntegrationTest, BasicInitSucceeds) {
EXPECT_EQ(method_res.error(), Error::Ok);
}

TEST_P(BackendIntegrationTest, GetBackendNamesSuccess) {
// Load the program from file.
Result<FileDataLoader> loader = FileDataLoader::from(program_path());
ASSERT_EQ(loader.error(), Error::Ok);

Result<Program> program = Program::load(&loader.get());
ASSERT_EQ(program.error(), Error::Ok);

// Get method metadata for the "forward" method.
auto method_meta = program->method_meta("forward");

// Ensure the StubBackend is used.
EXPECT_TRUE(method_meta->uses_backend(StubBackend::kName));

// Retrieve the number of backends.
size_t num_backends = method_meta->num_backends();
EXPECT_GT(num_backends, 0u);

// Iterate through each backend and verify its name.
for (size_t i = 0; i < num_backends; ++i) {
const char* name = method_meta->get_backend_name(i);
EXPECT_NE(name, nullptr);
// For this test, we expect that the only backend is StubBackend.
EXPECT_STREQ(name, StubBackend::kName);
}
// Check that an out-of-range index returns nullptr.
EXPECT_EQ(method_meta->get_backend_name(num_backends), nullptr);
}

TEST_P(BackendIntegrationTest, FreeingProcessedBufferSucceeds) {
// Install an init() implementation that frees its processed buffer, and lets
// us know that it was actually called by setting init_called.
Expand Down
Loading