Skip to content

Commit 74ee780

Browse files
committed
[6/N] Add update in module
Pull Request resolved: #11533 - Expose the update API in module. Inside the module. invoke the method. Update API - Did a bit refactor for the `StubBackend` test code, such that it can shared between `backend_interface_update_test.cpp` and `module_test.cpp` ghstack-source-id: 290372284 @exported-using-ghexport Differential Revision: [D76172680](https://our.internmc.facebook.com/intern/diff/D76172680/)
1 parent 694185b commit 74ee780

File tree

10 files changed

+287
-157
lines changed

10 files changed

+287
-157
lines changed

extension/module/module.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,13 @@ runtime::Error Module::set_output(
309309
output_tensor.mutable_data_ptr(), output_tensor.nbytes(), output_index);
310310
}
311311

312-
} // namespace ET_MODULE_NAMESPACE
312+
runtime::Error Module::update(
313+
const std::string& method_name,
314+
runtime::ArrayRef<runtime::Entry> backend_options) {
315+
ET_CHECK_OK_OR_RETURN_ERROR(load_method(method_name));
316+
auto& method = methods_.at(method_name).method;
317+
return method->update(backend_options);
318+
}
319+
313320
} // namespace extension
314321
} // namespace executorch

extension/module/module.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,32 @@ class Module {
466466
return set_output("forward", std::move(output_value), output_index);
467467
}
468468

469+
/**
470+
* EXPERIMENTAL: Updates backend options for a specific method.
471+
* Loads the program and method before updating if needed.
472+
*
473+
* @param[in] method_name The name of the method to update.
474+
* @param[in] backend_options The backend options to update the method with.
475+
*
476+
* @returns An Error to indicate success or failure.
477+
*/
478+
ET_EXPERIMENTAL ET_NODISCARD runtime::Error update(
479+
const std::string& method_name,
480+
runtime::ArrayRef<runtime::Entry> backend_options);
481+
482+
/**
483+
* EXPERIMENTAL: Updates backend options for the 'forward' method.
484+
* Loads the program and method before updating if needed.
485+
*
486+
* @param[in] backend_options The backend options to update the method with.
487+
*
488+
* @returns An Error to indicate success or failure.
489+
*/
490+
ET_EXPERIMENTAL ET_NODISCARD inline runtime::Error update(
491+
runtime::ArrayRef<runtime::Entry> backend_options) {
492+
return update("forward", backend_options);
493+
}
494+
469495
/**
470496
* Retrieves the EventTracer instance being used by the Module.
471497
* EventTracer is used for tracking and logging events during the execution

extension/module/test/module_test.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,32 @@
1616
#include <executorch/extension/data_loader/file_data_loader.h>
1717
#include <executorch/extension/tensor/tensor.h>
1818
#include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h>
19+
#include <executorch/runtime/backend/backend_options.h>
20+
#include <executorch/runtime/backend/backend_options_map.h>
21+
#include <executorch/runtime/executor/test/stub_backend.h>
1922

2023
using namespace ::executorch::extension;
2124
using namespace ::executorch::runtime;
25+
using executorch::runtime::BackendOptions;
26+
using executorch::runtime::Entry;
27+
using executorch::runtime::IntKey;
2228

2329
class ModuleTest : public ::testing::Test {
2430
protected:
2531
static void SetUpTestSuite() {
2632
model_path_ = std::getenv("ET_MODULE_ADD_PATH");
2733
add_mul_path_ = std::getenv("ET_MODULE_ADD_MUL_PROGRAM_PATH");
2834
add_mul_data_path_ = std::getenv("ET_MODULE_ADD_MUL_DATA_PATH");
35+
stub_model_path_ = std::getenv("ET_MODULE_ADD_MUL_DELEGATED_PATH");
36+
37+
// Register the StubBackend for testing
38+
StubBackend::register_singleton();
2939
}
3040

3141
static inline std::string model_path_;
3242
static inline std::string add_mul_path_;
3343
static inline std::string add_mul_data_path_;
44+
static inline std::string stub_model_path_;
3445
};
3546

3647
TEST_F(ModuleTest, TestLoad) {
@@ -466,3 +477,34 @@ TEST_F(ModuleTest, TestPTD) {
466477
auto tensor = make_tensor_ptr({2, 2}, {2.f, 3.f, 4.f, 2.f});
467478
ASSERT_EQ(module.forward(tensor).error(), Error::Ok);
468479
}
480+
481+
TEST_F(ModuleTest, TestUpdate) {
482+
Module module(stub_model_path_);
483+
484+
BackendOptionsMap<3> map;
485+
BackendOptions<1> backend_options;
486+
int new_num_threads = 4;
487+
backend_options.set_option(IntKey("NumberOfThreads"), new_num_threads);
488+
map.add("StubBackend", backend_options.view());
489+
490+
// Test update method with specific method name
491+
const auto update_result = module.update("forward", map.entries());
492+
EXPECT_EQ(update_result, Error::Ok);
493+
494+
ASSERT_EQ(StubBackend::singleton().num_threads(), new_num_threads);
495+
496+
}
497+
498+
TEST_F(ModuleTest, TestUpdateNonExistentMethod) {
499+
Module module(stub_model_path_);
500+
501+
BackendOptionsMap<3> map;
502+
BackendOptions<1> backend_options;
503+
int new_num_threads = 4;
504+
backend_options.set_option(IntKey("NumberOfThreads"), new_num_threads);
505+
map.add("StubBackend", backend_options.view());
506+
507+
// Test update method with non-existent method name
508+
const auto update_result = module.update("nonexistent", map.entries());
509+
EXPECT_NE(update_result, Error::Ok);
510+
}

extension/module/test/targets.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def define_common_targets(is_fbcode=False):
1919
"ET_MODULE_ADD_PATH": "$(location fbcode//executorch/test/models:exported_programs[ModuleAdd.pte])",
2020
"ET_MODULE_ADD_MUL_PROGRAM_PATH": "$(location fbcode//executorch/test/models:exported_program_and_data[ModuleAddMul.pte])",
2121
"ET_MODULE_ADD_MUL_DATA_PATH": "$(location fbcode//executorch/test/models:exported_program_and_data[ModuleAddMul.ptd])",
22+
"ET_MODULE_ADD_MUL_DELEGATED_PATH": "$(location fbcode//executorch/test/models:exported_delegated_add_mul[ModuleAddMul.pte])",
2223
}
2324

2425
for aten_mode in get_aten_mode_options():
@@ -35,6 +36,7 @@ def define_common_targets(is_fbcode=False):
3536
"//executorch/extension/module:module" + aten_suffix,
3637
"//executorch/extension/tensor:tensor" + aten_suffix,
3738
"//executorch/runtime/core/exec_aten/testing_util:tensor_util" + aten_suffix,
39+
"//executorch/runtime/executor/test:stub_backend",
3840
],
3941
env = modules_env,
4042
platforms = [CXX, ANDROID], # Cannot bundle resources on Apple platform.

runtime/executor/method.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,8 @@ Error Method::experimental_step() {
15131513
return step();
15141514
}
15151515

1516-
Error Method::update(executorch::runtime::ArrayRef<executorch::runtime::Entry> backend_option) {
1516+
Error Method::update(
1517+
executorch::runtime::ArrayRef<executorch::runtime::Entry> backend_option) {
15171518
for (const auto& entry : backend_option) {
15181519
const char* backend_name = entry.backend_name;
15191520
auto backend_options = entry.options;

runtime/executor/method.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,14 @@ class Method final {
241241
/// DEPRECATED: Use `reset_execution()` instead.
242242
ET_DEPRECATED ET_NODISCARD Error experimental_reset_execution();
243243

244-
/**
244+
/**
245245
* EXPERIMENTAL: Update backend options, which will be dispatched to different backends.
246246
*
247247
* @retval Error::Ok step succeeded
248248
* @retval non-Ok Method update fails
249249
*/
250-
ET_EXPERIMENTAL ET_NODISCARD Error update(executorch::runtime::ArrayRef<executorch::runtime::Entry> backend_option);
250+
ET_EXPERIMENTAL ET_NODISCARD Error update(
251+
executorch::runtime::ArrayRef<executorch::runtime::Entry> backend_option);
251252

252253
/**
253254
* Returns the MethodMeta that corresponds to the calling Method.

0 commit comments

Comments
 (0)