|
6 | 6 | * LICENSE file in the root directory of this source tree.
|
7 | 7 | */
|
8 | 8 |
|
| 9 | +#include <executorch/runtime/backend/interface.h> |
9 | 10 | #include <executorch/runtime/backend/options.h>
|
10 | 11 | #include <executorch/runtime/backend/options_map.h>
|
11 | 12 | #include <executorch/runtime/platform/runtime.h>
|
12 | 13 |
|
13 | 14 | #include <gtest/gtest.h>
|
14 | 15 |
|
15 | 16 | using namespace ::testing;
|
| 17 | +using executorch::runtime::ArrayRef; |
| 18 | +using executorch::runtime::Backend; |
| 19 | +using executorch::runtime::BackendExecutionContext; |
| 20 | +using executorch::runtime::BackendInitContext; |
| 21 | +using executorch::runtime::BackendInterface; |
16 | 22 | using executorch::runtime::BackendOption;
|
| 23 | +using executorch::runtime::BackendOptionContext; |
17 | 24 | using executorch::runtime::BackendOptions;
|
18 | 25 | using executorch::runtime::BackendOptionsMap;
|
| 26 | +using executorch::runtime::CompileSpec; |
| 27 | +using executorch::runtime::DelegateHandle; |
19 | 28 | using executorch::runtime::Error;
|
| 29 | +using executorch::runtime::EValue; |
| 30 | +using executorch::runtime::FreeableBuffer; |
20 | 31 | using executorch::runtime::OptionKey;
|
| 32 | +using executorch::runtime::register_backend; |
| 33 | +using executorch::runtime::Result; |
21 | 34 |
|
22 | 35 | namespace executorch {
|
23 | 36 | namespace runtime {
|
@@ -144,5 +157,155 @@ TEST_F(BackendOptionsMapTest, OptionIsolation) {
|
144 | 157 | EXPECT_STREQ(gpu_opts[2].key, "Hardware");
|
145 | 158 | EXPECT_STREQ(std::get<const char*>(gpu_opts[2].value), "H100");
|
146 | 159 | }
|
| 160 | + |
| 161 | +// Mock backend for testing |
| 162 | +class StubBackend : public BackendInterface { |
| 163 | + public: |
| 164 | + ~StubBackend() override = default; |
| 165 | + |
| 166 | + bool is_available() const override { |
| 167 | + return true; |
| 168 | + } |
| 169 | + |
| 170 | + Result<DelegateHandle*> init( |
| 171 | + BackendInitContext& context, |
| 172 | + FreeableBuffer* processed, |
| 173 | + ArrayRef<CompileSpec> compile_specs) const override { |
| 174 | + return nullptr; |
| 175 | + } |
| 176 | + |
| 177 | + Error execute( |
| 178 | + BackendExecutionContext& context, |
| 179 | + DelegateHandle* handle, |
| 180 | + EValue** args) const override { |
| 181 | + return Error::Ok; |
| 182 | + } |
| 183 | + |
| 184 | + Error get_option( |
| 185 | + BackendOptionContext& context, |
| 186 | + executorch::runtime::Span<executorch::runtime::BackendOption>& |
| 187 | + backend_options) override { |
| 188 | + // For testing purposes, just record that get_option was called |
| 189 | + // and verify the input parameters |
| 190 | + get_option_called = true; |
| 191 | + get_option_call_count++; |
| 192 | + last_get_option_size = backend_options.size(); |
| 193 | + |
| 194 | + // Verify that the expected option key is present and modify the value |
| 195 | + for (size_t i = 0; i < backend_options.size(); ++i) { |
| 196 | + if (strcmp(backend_options[i].key, "NumberOfThreads") == 0) { |
| 197 | + // Set the value to what was stored by set_option |
| 198 | + backend_options[i].value = last_num_threads; |
| 199 | + found_expected_key = true; |
| 200 | + break; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + return Error::Ok; |
| 205 | + } |
| 206 | + |
| 207 | + Error set_option( |
| 208 | + BackendOptionContext& context, |
| 209 | + const Span<executorch::runtime::BackendOption>& backend_options) |
| 210 | + override { |
| 211 | + // Store the options for verification |
| 212 | + last_options_size = backend_options.size(); |
| 213 | + if (backend_options.size() > 0) { |
| 214 | + for (const auto& option : backend_options) { |
| 215 | + if (strcmp(option.key, "NumberOfThreads") == 0) { |
| 216 | + if (auto* val = std::get_if<int>(&option.value)) { |
| 217 | + last_num_threads = *val; |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + return Error::Ok; |
| 223 | + } |
| 224 | + |
| 225 | + // Mutable for testing verification |
| 226 | + size_t last_options_size = 0; |
| 227 | + int last_num_threads = 0; |
| 228 | + bool get_option_called = false; |
| 229 | + int get_option_call_count = 0; |
| 230 | + size_t last_get_option_size = 0; |
| 231 | + bool found_expected_key = false; |
| 232 | +}; |
| 233 | + |
| 234 | +class BackendUpdateTest : public ::testing::Test { |
| 235 | + protected: |
| 236 | + void SetUp() override { |
| 237 | + // Since these tests cause ET_LOG to be called, the PAL must be initialized |
| 238 | + // first. |
| 239 | + executorch::runtime::runtime_init(); |
| 240 | + |
| 241 | + // Register the stub backend |
| 242 | + stub_backend = std::make_unique<StubBackend>(); |
| 243 | + Backend backend_config{"StubBackend", stub_backend.get()}; |
| 244 | + auto register_result = register_backend(backend_config); |
| 245 | + ASSERT_EQ(register_result, Error::Ok); |
| 246 | + } |
| 247 | + |
| 248 | + std::unique_ptr<StubBackend> stub_backend; |
| 249 | +}; |
| 250 | + |
| 251 | +// Test basic string functionality |
| 252 | +TEST_F(BackendUpdateTest, TestSetOption) { |
| 253 | + BackendOptionsMap<3> map; |
| 254 | + BackendOptions<1> backend_options; |
| 255 | + int new_num_threads = 4; |
| 256 | + backend_options.set_option("NumberOfThreads", new_num_threads); |
| 257 | + map.add("StubBackend", backend_options.view()); |
| 258 | + |
| 259 | + auto status = set_option(map.entries()); |
| 260 | + ASSERT_EQ(status, Error::Ok); |
| 261 | + |
| 262 | + // Verify the map contains the expected data |
| 263 | + ASSERT_EQ(map.size(), 1); |
| 264 | + auto options = map.get("StubBackend"); |
| 265 | + ASSERT_EQ(options.size(), 1); |
| 266 | + |
| 267 | + // Verify that the backend actually received the options |
| 268 | + ASSERT_EQ(stub_backend->last_options_size, 1); |
| 269 | + ASSERT_EQ(stub_backend->last_num_threads, new_num_threads); |
| 270 | +} |
| 271 | + |
| 272 | +// Test get_option functionality |
| 273 | +TEST_F(BackendUpdateTest, TestGetOption) { |
| 274 | + // First, set some options in the backend |
| 275 | + BackendOptionsMap<3> set_map; |
| 276 | + BackendOptions<1> set_backend_options; |
| 277 | + int expected_num_threads = 8; |
| 278 | + set_backend_options.set_option("NumberOfThreads", expected_num_threads); |
| 279 | + set_map.add("StubBackend", set_backend_options.view()); |
| 280 | + |
| 281 | + auto set_status = set_option(set_map.entries()); |
| 282 | + ASSERT_EQ(set_status, Error::Ok); |
| 283 | + ASSERT_EQ(stub_backend->last_num_threads, expected_num_threads); |
| 284 | + |
| 285 | + // Reset get_option tracking variables |
| 286 | + stub_backend->get_option_called = false; |
| 287 | + stub_backend->get_option_call_count = 0; |
| 288 | + stub_backend->found_expected_key = false; |
| 289 | + |
| 290 | + // Now create a map with options for get_option to process |
| 291 | + BackendOptionsMap<3> get_map; |
| 292 | + BackendOptions<1> get_backend_options; |
| 293 | + get_backend_options.set_option("NumberOfThreads", 0); |
| 294 | + get_map.add("StubBackend", get_backend_options.view()); |
| 295 | + |
| 296 | + // Call get_option to test the API |
| 297 | + auto get_status = get_option(get_map.entries()); |
| 298 | + ASSERT_EQ(get_status, Error::Ok); |
| 299 | + |
| 300 | + ASSERT_TRUE( |
| 301 | + std::get<int>(get_map.entries()[0].options[0].value) == |
| 302 | + expected_num_threads); |
| 303 | + |
| 304 | + // Verify that the backend's get_option method was called correctly |
| 305 | + ASSERT_TRUE(stub_backend->get_option_called); |
| 306 | + ASSERT_EQ(stub_backend->get_option_call_count, 1); |
| 307 | + ASSERT_EQ(stub_backend->last_get_option_size, 1); |
| 308 | + ASSERT_TRUE(stub_backend->found_expected_key); |
| 309 | +} |
147 | 310 | } // namespace runtime
|
148 | 311 | } // namespace executorch
|
0 commit comments