Skip to content

Commit 645217a

Browse files
committed
Update on "[3/N] Add get_option/set_option function in backend interface"
Add update function in backend interface class. The update function will receive the backend options from dispatched by the ET runtime. ET runtime's logic: loop over each backend and it's corresponding backend options, dispatch the backend options to the corresponding backend Next step, will add update API in the method and then module exported-using-ghexport Differential Revision: [D75919242](https://our.internmc.facebook.com/intern/diff/D75919242/) Differential Revision: [D75919242](https://our.internmc.facebook.com/intern/diff/D75919242) [ghstack-poisoned]
2 parents 071633b + e86fd5e commit 645217a

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

runtime/backend/options.h

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,12 @@ class BackendOptions {
7474
*/
7575
BackendOptions() : size_(0) {}
7676

77-
/**
78-
* Returns a const view of all stored options as a Span.
79-
*
80-
* @return A const Span containing all BackendOption entries
81-
*/
82-
executorch::runtime::Span<const BackendOption> view() const {
83-
return executorch::runtime::Span<const BackendOption>(options_, size_);
84-
}
85-
8677
/**
8778
* Returns a mutable view of all stored options as a Span.
8879
*
8980
* @return A mutable Span containing all BackendOption entries
9081
*/
91-
executorch::runtime::Span<BackendOption> mutable_view() {
82+
executorch::runtime::Span<BackendOption> view() {
9283
return executorch::runtime::Span<BackendOption>(options_, size_);
9384
}
9485

@@ -101,9 +92,13 @@ class BackendOptions {
10192
* @param value The boolean value to set
10293
* @return Error::Ok on success, Error::InvalidArgument if storage is full
10394
*/
104-
template <size_t N>
105-
Error set_option(const char (&key)[N], bool value) noexcept {
106-
static_assert(N <= kMaxOptionKeyLength, "Option key is too long");
95+
Error set_option(const char* key, bool value) noexcept {
96+
ET_CHECK_MSG(
97+
strlen(key) <= kMaxOptionKeyLength,
98+
"Option key %s (%zu) is too long (max %zu)",
99+
key,
100+
strlen(key),
101+
kMaxOptionKeyLength);
107102
return set_option_impl(key, value);
108103
}
109104

@@ -116,9 +111,13 @@ class BackendOptions {
116111
* @param value The integer value to set
117112
* @return Error::Ok on success, Error::InvalidArgument if storage is full
118113
*/
119-
template <size_t N>
120-
Error set_option(const char (&key)[N], int value) noexcept {
121-
static_assert(N <= kMaxOptionKeyLength, "Option key is too long");
114+
Error set_option(const char* key, int value) noexcept {
115+
ET_CHECK_MSG(
116+
strlen(key) <= kMaxOptionKeyLength,
117+
"Option key %s (%zu) is too long (max %zu)",
118+
key,
119+
strlen(key),
120+
kMaxOptionKeyLength);
122121
return set_option_impl(key, value);
123122
}
124123

@@ -134,9 +133,13 @@ class BackendOptions {
134133
* @param value The string value to set (must have static storage duration)
135134
* @return Error::Ok on success, Error::InvalidArgument if storage is full
136135
*/
137-
template <size_t N>
138-
Error set_option(const char (&key)[N], const char* value) noexcept {
139-
static_assert(N <= kMaxOptionKeyLength, "Option key is too long");
136+
Error set_option(const char* key, const char* value) noexcept {
137+
ET_CHECK_MSG(
138+
strlen(key) <= kMaxOptionKeyLength,
139+
"Option key %s (%zu) is too long (max %zu)",
140+
key,
141+
strlen(key),
142+
kMaxOptionKeyLength);
140143
// Create a fixed-size array and copy the string
141144
std::array<char, kMaxOptionValueLength> arr;
142145
strncpy(arr.data(), value, kMaxOptionValueLength - 1);

runtime/backend/test/backend_interface_update_test.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,28 @@ class MockBackend : public BackendInterface {
5858
const executorch::runtime::Span<BackendOption>& backend_options)
5959
override {
6060
set_option_count++;
61-
int sucess_update = 0;
61+
int success_update = 0;
6262
for (const auto& backend_option : backend_options) {
6363
if (strcmp(backend_option.key, "Backend") == 0) {
64-
if (std::holds_alternative<const char*>(backend_option.value)) {
64+
if (std::holds_alternative<std::array<char, 256>>(backend_option.value)) {
6565
// Store the value in our member variable
66-
target_backend = std::get<const char*>(backend_option.value);
67-
sucess_update++;
66+
const auto& arr = std::get<std::array<char, 256>>(backend_option.value);
67+
target_backend = std::string(arr.data());
68+
success_update++;
6869
}
6970
} else if (strcmp(backend_option.key, "NumberOfThreads") == 0) {
7071
if (std::holds_alternative<int>(backend_option.value)) {
7172
num_threads = std::get<int>(backend_option.value);
72-
sucess_update++;
73+
success_update++;
7374
}
7475
} else if (strcmp(backend_option.key, "Debug") == 0) {
7576
if (std::holds_alternative<bool>(backend_option.value)) {
7677
debug = std::get<bool>(backend_option.value);
77-
sucess_update++;
78+
success_update++;
7879
}
7980
}
8081
}
81-
if (sucess_update == backend_options.size()) {
82+
if (success_update == backend_options.size()) {
8283
return Error::Ok;
8384
}
8485
return Error::InvalidArgument;
@@ -113,9 +114,9 @@ TEST_F(BackendInterfaceUpdateTest, HandlesInvalidOption) {
113114
BackendOptionContext context;
114115

115116
// Test invalid key case
116-
BackendOption invalid_option{"InvalidKey", "None"};
117+
std::array<char, 256> value_array{"None"};
118+
BackendOption invalid_option{"InvalidKey", value_array};
117119

118-
// Create a span from the single option
119120
Error err = mock_backend->set_option(context, invalid_option);
120121
EXPECT_EQ(err, Error::InvalidArgument);
121122
}

runtime/backend/test/backend_options_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ TEST_F(BackendOptionsTest, MutableOption) {
101101
EXPECT_EQ(options.get_option("flag", ival), Error::Ok);
102102
EXPECT_EQ(ival, 0);
103103

104-
options.mutable_view()[0].value = 123; // Overwrites the entry
104+
options.view()[0].value = 123; // Overwrites the entry
105105

106106
// Integer get should succeed with the updated value
107107
EXPECT_EQ(options.get_option("flag", ival), Error::Ok);

0 commit comments

Comments
 (0)