|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | + #include <cstdlib> |
| 10 | + #include <filesystem> |
| 11 | + |
| 12 | + #include <executorch/extension/data_loader/file_data_loader.h> |
| 13 | + #include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 14 | + #include <executorch/runtime/executor/method.h> |
| 15 | + #include <executorch/runtime/executor/program.h> |
| 16 | + #include <executorch/runtime/executor/test/managed_memory_manager.h> |
| 17 | + #include <executorch/runtime/platform/runtime.h> |
| 18 | + #include <executorch/test/utils/DeathTest.h> |
| 19 | + #include <gtest/gtest.h> |
| 20 | + #include <executorch/runtime/backend/interface.h> |
| 21 | + #include <executorch/runtime/backend/backend_update_context.h> |
| 22 | + #include <executorch/runtime/backend/backend_options.h> |
| 23 | + #include <executorch/runtime/backend/backend_options_map.h> |
| 24 | + #include <executorch/runtime/core/error.h> |
| 25 | + #include <executorch/runtime/core/result.h> |
| 26 | + |
| 27 | + |
| 28 | + using namespace ::testing; |
| 29 | + using executorch::aten::ArrayRef; |
| 30 | + using executorch::runtime::Error; |
| 31 | + using executorch::runtime::EValue; |
| 32 | + using executorch::runtime::Method; |
| 33 | + using executorch::runtime::Program; |
| 34 | + using executorch::runtime::Result; |
| 35 | + using executorch::runtime::testing::ManagedMemoryManager; |
| 36 | + using torch::executor::util::FileDataLoader; |
| 37 | + using executorch::runtime::BackendExecutionContext; |
| 38 | +using executorch::runtime::BackendInitContext; |
| 39 | +using executorch::runtime::BackendInterface; |
| 40 | +using executorch::runtime::BackendUpdateContext; |
| 41 | +using executorch::runtime::BackendOption; |
| 42 | +using executorch::runtime::BackendOptions; |
| 43 | +using executorch::runtime::BackendOptionsMap; |
| 44 | +using executorch::runtime::BoolKey; |
| 45 | +using executorch::runtime::IntKey; |
| 46 | +using executorch::runtime::Entry; |
| 47 | +using executorch::runtime::OptionType; |
| 48 | +using executorch::runtime::CompileSpec; |
| 49 | +using executorch::runtime::DataLoader; |
| 50 | +using executorch::runtime::DelegateHandle; |
| 51 | +using executorch::runtime::FreeableBuffer; |
| 52 | + |
| 53 | + constexpr size_t kDefaultNonConstMemBytes = 32 * 1024U; |
| 54 | + constexpr size_t kDefaultRuntimeMemBytes = 32 * 1024U; |
| 55 | + |
| 56 | +/** |
| 57 | + * A backend class whose methods can be overridden individually. |
| 58 | + */ |
| 59 | +class StubBackend final : public BackendInterface { |
| 60 | + public: |
| 61 | + |
| 62 | + // Default name that this backend is registered as. |
| 63 | + static constexpr char kName[] = "StubBackend"; |
| 64 | + |
| 65 | + bool is_available() const override { |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + Result<DelegateHandle*> init( |
| 70 | + BackendInitContext& context, |
| 71 | + FreeableBuffer* processed, |
| 72 | + ArrayRef<CompileSpec> compile_specs) const override { |
| 73 | + return nullptr; |
| 74 | + } |
| 75 | + |
| 76 | + Error execute( |
| 77 | + BackendExecutionContext& context, |
| 78 | + DelegateHandle* handle, |
| 79 | + EValue** args) const override { |
| 80 | + return Error::Ok; |
| 81 | + } |
| 82 | + |
| 83 | + int num_threads() const { |
| 84 | + return num_threads_; |
| 85 | + } |
| 86 | + |
| 87 | + Error update( |
| 88 | + BackendUpdateContext& context, |
| 89 | + const executorch::runtime::ArrayRef<BackendOption>& backend_options) const override { |
| 90 | + int success_update = 0; |
| 91 | + for (const auto& backend_option : backend_options) { |
| 92 | + if (strcmp(backend_option.key, "NumberOfThreads") == 0) { |
| 93 | + if (backend_option.type == OptionType::INT) { |
| 94 | + num_threads_ = backend_option.value.int_value; |
| 95 | + success_update++; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + if (success_update == backend_options.size()) { |
| 100 | + return Error::Ok; |
| 101 | + } |
| 102 | + return Error::InvalidArgument; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Registers the singleton instance if not already registered. |
| 107 | + * |
| 108 | + * Note that this can be used to install the stub as the implementation for |
| 109 | + * any export-time backend by passing in the right name, as long as no other |
| 110 | + * backend with that name has been registered yet. |
| 111 | + */ |
| 112 | + static Error register_singleton(const char* name = kName) { |
| 113 | + if (!registered_) { |
| 114 | + registered_ = true; |
| 115 | + return executorch::runtime::register_backend({name, &singleton_}); |
| 116 | + } |
| 117 | + return Error::Ok; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Returns the instance that was added to the backend registry. |
| 122 | + */ |
| 123 | + static StubBackend& singleton() { |
| 124 | + return singleton_; |
| 125 | + } |
| 126 | + |
| 127 | + private: |
| 128 | + static bool registered_; |
| 129 | + static StubBackend singleton_; |
| 130 | + mutable int num_threads_ = 1; |
| 131 | + }; |
| 132 | + |
| 133 | + bool StubBackend::registered_ = false; |
| 134 | + StubBackend StubBackend::singleton_; |
| 135 | + |
| 136 | + class MethodUpdateTest : public ::testing::Test { |
| 137 | + protected: |
| 138 | + void load_program() { |
| 139 | + // Since these tests cause ET_LOG to be called, the PAL must be initialized |
| 140 | + // first. |
| 141 | + executorch::runtime::runtime_init(); |
| 142 | + |
| 143 | + // Create a loader for the serialized program. |
| 144 | + ASSERT_EQ(StubBackend::register_singleton(), Error::Ok); |
| 145 | + |
| 146 | + auto loader_res = FileDataLoader::from(std::getenv("ET_MODULE_ADD_MUL_DELEGATED_PATH")); |
| 147 | + ASSERT_EQ(loader_res.error(), Error::Ok); |
| 148 | + loader_ = std::make_unique<FileDataLoader>(std::move(loader_res.get())); |
| 149 | + |
| 150 | + // Use it to load the program. |
| 151 | + auto program_res = Program::load(loader_.get()); |
| 152 | + ASSERT_EQ(program_res.error(), Error::Ok); |
| 153 | + program_ = std::make_unique<Program>(std::move(program_res.get())); |
| 154 | + } |
| 155 | + |
| 156 | + void SetUp() override { |
| 157 | + executorch::runtime::runtime_init(); |
| 158 | + |
| 159 | + load_program(); |
| 160 | + } |
| 161 | + |
| 162 | + private: |
| 163 | + std::unique_ptr<FileDataLoader> loader_; |
| 164 | + |
| 165 | + protected: |
| 166 | + std::unique_ptr<Program> program_; |
| 167 | + }; |
| 168 | + |
| 169 | + TEST_F(MethodUpdateTest, MoveTest) { |
| 170 | + BackendInterface* backend = |
| 171 | + executorch::runtime::get_backend_class(StubBackend::kName); |
| 172 | + ASSERT_EQ(backend, &StubBackend::singleton()); |
| 173 | + |
| 174 | + ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes); |
| 175 | + Result<Method> method = program_->load_method("forward", &mmm.get()); |
| 176 | + // Check that the default number of threads is 1. |
| 177 | + ASSERT_EQ(StubBackend::singleton().num_threads(), 1); |
| 178 | + ASSERT_EQ(method.error(), Error::Ok); |
| 179 | + |
| 180 | + BackendOptionsMap<3> map; |
| 181 | + BackendOptions<1> backend_options; |
| 182 | + int new_num_threads = 4; |
| 183 | + backend_options.set_option(IntKey("NumberOfThreads"), new_num_threads); |
| 184 | + map.add("StubBackend", backend_options.view()); |
| 185 | + Error update_result = method->update(map.entries()); |
| 186 | + ASSERT_EQ(update_result, Error::Ok); |
| 187 | + ASSERT_EQ(StubBackend::singleton().num_threads(), new_num_threads); |
| 188 | +} |
0 commit comments