diff --git a/runtime/backend/backend_options.h b/runtime/backend/backend_options.h index f7b0a000d4..588b572882 100644 --- a/runtime/backend/backend_options.h +++ b/runtime/backend/backend_options.h @@ -6,94 +6,92 @@ * LICENSE file in the root directory of this source tree. */ - #pragma once - #include - #include - #include - #include - #include - #include - - namespace executorch { - namespace runtime { - - // Strongly-typed option key template - template - struct OptionKey { - const char* key; - constexpr explicit OptionKey(const char* k) : key(k) {} - }; - - // Union replaced with std::variant - using OptionValue = std::variant; - - struct BackendOption { - const char* key; // key is the name of the backend option, like num_threads, - // enable_profiling, etc - OptionValue - value; // value is the value of the backend option, like 4, true, etc - }; - - template - class BackendOptions { - public: - // Initialize with zero options - BackendOptions() : size_(0) {} - - // Type-safe setters - template - void set_option(OptionKey key, T value) { - const char* k = key.key; - // Update existing if found - for (size_t i = 0; i < size_; ++i) { - if (strcmp(options_[i].key, k) == 0) { - options_[i].value = value; - return; - } - } - // Add new option if space available - if (size_ < MaxCapacity) { - options_[size_++] = BackendOption{k, value}; - } - } - - // Type-safe getters - template - Error get_option(OptionKey key, T& out) const { - const char* k = key.key; - for (size_t i = 0; i < size_; ++i) { - if (strcmp(options_[i].key, k) == 0) { - if (auto* val = std::get_if(&options_[i].value)) { - out = *val; - return Error::Ok; - } - return Error::InvalidArgument; - } - } - return Error::NotFound; - } - executorch::runtime::ArrayRef view() const { - return executorch::runtime::ArrayRef(options_, size_); - } - - private: - BackendOption options_[MaxCapacity]{}; // Storage for backend options - size_t size_; // Current number of options - }; - - // Helper functions for creating typed option keys (unchanged) - constexpr OptionKey BoolKey(const char* k) { - return OptionKey(k); - } - - constexpr OptionKey IntKey(const char* k) { - return OptionKey(k); - } - - constexpr OptionKey StrKey(const char* k) { - return OptionKey(k); - } - - } // namespace runtime - } // namespace executorch - \ No newline at end of file +#pragma once +#include +#include +#include +#include +#include + +namespace executorch { +namespace runtime { + +// Strongly-typed option key template +template +struct OptionKey { + const char* key; + constexpr explicit OptionKey(const char* k) : key(k) {} +}; + +// Union replaced with std::variant +using OptionValue = std::variant; + +struct BackendOption { + const char* key; // key is the name of the backend option, like num_threads, + // enable_profiling, etc + OptionValue + value; // value is the value of the backend option, like 4, true, etc +}; + +template +class BackendOptions { + public: + // Initialize with zero options + BackendOptions() : size_(0) {} + + // Type-safe setters + template + void set_option(OptionKey key, T value) { + const char* k = key.key; + // Update existing if found + for (size_t i = 0; i < size_; ++i) { + if (strcmp(options_[i].key, k) == 0) { + options_[i].value = value; + return; + } + } + // Add new option if space available + if (size_ < MaxCapacity) { + options_[size_++] = BackendOption{k, value}; + } + } + + // Type-safe getters + template + Error get_option(OptionKey key, T& out) const { + const char* k = key.key; + for (size_t i = 0; i < size_; ++i) { + if (strcmp(options_[i].key, k) == 0) { + if (auto* val = std::get_if(&options_[i].value)) { + out = *val; + return Error::Ok; + } + return Error::InvalidArgument; + } + } + return Error::NotFound; + } + executorch::runtime::ArrayRef view() const { + return executorch::runtime::ArrayRef(options_, size_); + } + + private: + BackendOption options_[MaxCapacity]{}; // Storage for backend options + size_t size_; // Current number of options +}; + +// Helper functions for creating typed option keys (unchanged) +constexpr OptionKey BoolKey(const char* k) { + return OptionKey(k); +} + +constexpr OptionKey IntKey(const char* k) { + return OptionKey(k); +} + +constexpr OptionKey StrKey(const char* k) { + return OptionKey(k); +} + +} // namespace runtime +} // namespace executorch diff --git a/runtime/backend/backend_options_map.h b/runtime/backend/backend_options_map.h new file mode 100644 index 0000000000..142cd019d1 --- /dev/null +++ b/runtime/backend/backend_options_map.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include +#include + +#pragma once +namespace executorch { +namespace runtime { + +struct Entry { + const char* backend_name; + ArrayRef options; +}; + +template +class BackendOptionsMap { + public: + // Default constructor + BackendOptionsMap() : size_(0) {} + + // Add a new backend configuration + Error add( + const char* backend_name, + ::executorch::runtime::ArrayRef options) { + if (size_ < MaxBackends) { + entries_[size_] = {backend_name, options}; + ++size_; + return Error::Ok; + } else { + ET_LOG(Error, "Maximum number of backends %lu reached", MaxBackends); + } + return Error::InvalidArgument; + } + + // Get options for a specific backend + ::executorch::runtime::ArrayRef get( + const char* backend_name) const { + for (size_t i = 0; i < size_; ++i) { + if (std::strcmp(entries_[i].backend_name, backend_name) == 0) { + return entries_[i].options; + } + } + return {}; // Return empty ArrayRef if not found + } + + // Get a view of the entries (const version) + ::executorch::runtime::ArrayRef entries() const { + return ::executorch::runtime::ArrayRef(entries_, size_); + } + + // Get a view of the entries (non-const version) + ::executorch::runtime::ArrayRef entries() { + return ::executorch::runtime::ArrayRef(entries_, size_); + } + + // Get number of entries + size_t size() const { + return size_; + } + + private: + Entry entries_[MaxBackends]; // Storage for backend entries + size_t size_ = 0; // Current number of entries +}; + +} // namespace runtime +} // namespace executorch diff --git a/runtime/backend/targets.bzl b/runtime/backend/targets.bzl index c72cf76981..c58913e2bb 100644 --- a/runtime/backend/targets.bzl +++ b/runtime/backend/targets.bzl @@ -9,6 +9,25 @@ def define_common_targets(): for aten_mode in get_aten_mode_options(): aten_suffix = ("_aten" if aten_mode else "") + runtime.cxx_library( + name = "backend_options" + aten_suffix, + exported_headers = [ + "backend_options.h", + ], + preprocessor_flags = ["-DUSE_ATEN_LIB"] if aten_mode else [], + visibility = [ + "//executorch/...", + "@EXECUTORCH_CLIENTS", + ], + exported_deps = [ + "//executorch/runtime/core:core", + "//executorch/runtime/core:evalue" + aten_suffix, + "//executorch/runtime/core:event_tracer" + aten_suffix, + "//executorch/runtime/core:memory_allocator", + "//executorch/runtime/core:named_data_map", + ], + ) + runtime.cxx_library( name = "interface" + aten_suffix, srcs = [ @@ -18,7 +37,6 @@ def define_common_targets(): "backend_execution_context.h", "backend_init_context.h", "backend_update_context.h", - "backend_options.h", "interface.h", ], preprocessor_flags = ["-DUSE_ATEN_LIB"] if aten_mode else [], @@ -32,5 +50,22 @@ def define_common_targets(): "//executorch/runtime/core:event_tracer" + aten_suffix, "//executorch/runtime/core:memory_allocator", "//executorch/runtime/core:named_data_map", + "//executorch/runtime/backend:backend_options" + aten_suffix, + ], + ) + + runtime.cxx_library( + name = "backend_options_map" + aten_suffix, + exported_headers = [ + "backend_options_map.h", + ], + preprocessor_flags = ["-DUSE_ATEN_LIB"] if aten_mode else [], + visibility = [ + "//executorch/...", + "@EXECUTORCH_CLIENTS", + ], + exported_deps = [ + "//executorch/runtime/core:core", + ":backend_options" + aten_suffix, ], ) diff --git a/runtime/backend/test/backend_options_map_test.cpp b/runtime/backend/test/backend_options_map_test.cpp new file mode 100644 index 0000000000..afdd6df2c2 --- /dev/null +++ b/runtime/backend/test/backend_options_map_test.cpp @@ -0,0 +1,156 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include +#include + +#include + +using namespace ::testing; +using executorch::runtime::BackendOption; +using executorch::runtime::BackendOptions; +using executorch::runtime::BackendOptionsMap; +using executorch::runtime::BoolKey; +using executorch::runtime::Error; +using executorch::runtime::IntKey; +using executorch::runtime::OptionKey; +using executorch::runtime::StrKey; + +namespace executorch { +namespace runtime { + +class BackendOptionsMapTest : public ::testing::Test { + protected: + void SetUp() override { + // Initialize any necessary runtime components + executorch::runtime::runtime_init(); + } + // Assume 3 backends, each with max 5 options + BackendOptionsMap<3> map; +}; + +TEST_F(BackendOptionsMapTest, BasicAddAndRetrieve) { + BackendOptions<5> cpu_options; + + cpu_options.set_option(BoolKey("use_fp16"), true); + cpu_options.set_option(IntKey("thead"), 4); + map.add("CPU", cpu_options.view()); + + auto retrieved = map.get("CPU"); + EXPECT_GE(retrieved.size(), 1); + + // bool value; + bool found = false; + for (auto retrieved_option : retrieved) { + if (strcmp(retrieved_option.key, "use_fp16") == 0) { + EXPECT_EQ(std::get(retrieved_option.value), true); + found = true; + } + } + EXPECT_TRUE(found); +} + +// TEST_F(BackendOptionsMapTest, UnknownBackendHandling) { +// EXPECT_EQ(map.get("NPU"), nullptr) +// << "Should return nullptr for unknown backend"; +// } + +TEST_F(BackendOptionsMapTest, CapacityLimits) { + BackendOptionsMap<2> small_map; // Only 2 backends capacity + + BackendOptions<5> options; + ASSERT_EQ(small_map.add("CPU", options.view()), Error::Ok); + ASSERT_EQ(small_map.add("GPU", options.view()), Error::Ok); + // Return error if it exceeds capacity + ASSERT_EQ(small_map.add("NPU", options.view()), Error::InvalidArgument); +} + +TEST_F(BackendOptionsMapTest, EntryIteration) { + BackendOptions<2> cpu_options; + BackendOptions<3> gpu_options; + + // Add to map using ArrayRef + ASSERT_EQ(map.add("CPU", cpu_options.view()), Error::Ok); + ASSERT_EQ(map.add("GPU", gpu_options.view()), Error::Ok); + + auto entries = map.entries(); + // Should have 2 backends (entries) + ASSERT_EQ(entries.size(), 2); + + bool found_cpu = false; + bool found_gpu = false; + for (const auto& entry : entries) { + if (strcmp(entry.backend_name, "CPU") == 0) + found_cpu = true; + if (strcmp(entry.backend_name, "GPU") == 0) + found_gpu = true; + } + // Should find CPU and GPU in the entries + EXPECT_TRUE(found_cpu); + EXPECT_TRUE(found_gpu); +} + +TEST_F(BackendOptionsMapTest, ConstCorrectness) { + auto cpu_options = BackendOptions<5>(); + ASSERT_EQ(map.add("CPU", cpu_options.view()), Error::Ok); + + const auto& const_map = map; + auto options_retrived = const_map.get("CPU"); + EXPECT_EQ(options_retrived.size(), 0); + + auto entries = const_map.entries(); + EXPECT_FALSE(entries.empty()); +} + +TEST_F(BackendOptionsMapTest, EmptyMapBehavior) { + EXPECT_EQ(map.get("CPU").size(), 0); + EXPECT_TRUE(map.entries().empty()); + EXPECT_EQ(map.entries().size(), 0); +} + +TEST_F(BackendOptionsMapTest, OptionIsolation) { + BackendOptions<2> cpu_options; + cpu_options.set_option(BoolKey("Debug"), true); + cpu_options.set_option(IntKey("NumThreads"), 3); + + BackendOptions<3> gpu_options; + gpu_options.set_option(BoolKey("Profile"), true); + gpu_options.set_option(IntKey("Mem"), 1024); + gpu_options.set_option(StrKey("Hardware"), "H100"); + + // Add to map using ArrayRef + map.add("CPU", cpu_options.view()); + map.add("GPU", gpu_options.view()); + + // Test CPU options + auto cpu_opts = map.get("CPU"); + ASSERT_FALSE(cpu_opts.empty()); + + // Verify CPU has its own option + EXPECT_EQ(cpu_opts.size(), 2); + EXPECT_EQ(cpu_opts[0].key, "Debug"); + EXPECT_EQ(std::get(cpu_opts[0].value), true); + EXPECT_EQ(cpu_opts[1].key, "NumThreads"); + EXPECT_EQ(std::get(cpu_opts[1].value), 3); + + // Test GPU options + auto gpu_opts = map.get("GPU"); + ASSERT_FALSE(gpu_opts.empty()); + + // Verify GPU has its own option + EXPECT_EQ(gpu_opts.size(), 3); + EXPECT_EQ(gpu_opts[0].key, "Profile"); + EXPECT_EQ(std::get(gpu_opts[0].value), true); + EXPECT_EQ(gpu_opts[1].key, "Mem"); + EXPECT_EQ(std::get(gpu_opts[1].value), 1024); + EXPECT_EQ(gpu_opts[2].key, "Hardware"); + EXPECT_EQ(std::get(gpu_opts[2].value), "H100"); +} +} // namespace runtime +} // namespace executorch diff --git a/runtime/backend/test/targets.bzl b/runtime/backend/test/targets.bzl index 5430cb17cc..5d1ef2d5c8 100644 --- a/runtime/backend/test/targets.bzl +++ b/runtime/backend/test/targets.bzl @@ -15,6 +15,15 @@ def define_common_targets(): ], ) + runtime.cxx_test( + name = "backend_options_map_test", + srcs = ["backend_options_map_test.cpp"], + deps = [ + "//executorch/runtime/core:core", + "//executorch/runtime/backend:backend_options_map", + ], + ) + runtime.cxx_test( name = "backend_interface_update_test", srcs = ["backend_interface_update_test.cpp"],