|  | 
|  | 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 <executorch/runtime/backend/backend_options.h> | 
|  | 10 | +#include <executorch/runtime/backend/backend_options_map.h> | 
|  | 11 | +#include <executorch/runtime/platform/runtime.h> | 
|  | 12 | + | 
|  | 13 | +#include <gtest/gtest.h> | 
|  | 14 | + | 
|  | 15 | +using namespace ::testing; | 
|  | 16 | +using executorch::runtime::BackendOption; | 
|  | 17 | +using executorch::runtime::BackendOptions; | 
|  | 18 | +using executorch::runtime::BackendOptionsMap; | 
|  | 19 | +using executorch::runtime::BoolKey; | 
|  | 20 | +using executorch::runtime::Error; | 
|  | 21 | +using executorch::runtime::IntKey; | 
|  | 22 | +using executorch::runtime::OptionKey; | 
|  | 23 | +using executorch::runtime::StrKey; | 
|  | 24 | + | 
|  | 25 | +namespace executorch { | 
|  | 26 | +namespace runtime { | 
|  | 27 | + | 
|  | 28 | +class BackendOptionsMapTest : public ::testing::Test { | 
|  | 29 | + protected: | 
|  | 30 | +  void SetUp() override { | 
|  | 31 | +    // Initialize any necessary runtime components | 
|  | 32 | +    executorch::runtime::runtime_init(); | 
|  | 33 | +  } | 
|  | 34 | +  // Assume 3 backends, each with max 5 options | 
|  | 35 | +  BackendOptionsMap<3> map; | 
|  | 36 | +}; | 
|  | 37 | + | 
|  | 38 | +TEST_F(BackendOptionsMapTest, BasicAddAndRetrieve) { | 
|  | 39 | +  BackendOptions<5> cpu_options; | 
|  | 40 | + | 
|  | 41 | +  cpu_options.set_option(BoolKey("use_fp16"), true); | 
|  | 42 | +  cpu_options.set_option(IntKey("thead"), 4); | 
|  | 43 | +  map.add("CPU", cpu_options.view()); | 
|  | 44 | + | 
|  | 45 | +  auto retrieved = map.get("CPU"); | 
|  | 46 | +  EXPECT_GE(retrieved.size(), 1); | 
|  | 47 | + | 
|  | 48 | +  // bool value; | 
|  | 49 | +  bool found = false; | 
|  | 50 | +  for (auto retrieved_option : retrieved) { | 
|  | 51 | +    if (strcmp(retrieved_option.key, "use_fp16") == 0) { | 
|  | 52 | +      EXPECT_EQ(std::get<bool>(retrieved_option.value), true); | 
|  | 53 | +      found = true; | 
|  | 54 | +    } | 
|  | 55 | +  } | 
|  | 56 | +  EXPECT_TRUE(found); | 
|  | 57 | +} | 
|  | 58 | + | 
|  | 59 | +TEST_F(BackendOptionsMapTest, CapacityLimits) { | 
|  | 60 | +  BackendOptionsMap<2> small_map; // Only 2 backends capacity | 
|  | 61 | + | 
|  | 62 | +  BackendOptions<5> options; | 
|  | 63 | +  ASSERT_EQ(small_map.add("CPU", options.view()), Error::Ok); | 
|  | 64 | +  ASSERT_EQ(small_map.add("GPU", options.view()), Error::Ok); | 
|  | 65 | +  // Return error if it exceeds capacity | 
|  | 66 | +  ASSERT_EQ(small_map.add("NPU", options.view()), Error::InvalidArgument); | 
|  | 67 | +} | 
|  | 68 | + | 
|  | 69 | +TEST_F(BackendOptionsMapTest, EntryIteration) { | 
|  | 70 | +  BackendOptions<2> cpu_options; | 
|  | 71 | +  BackendOptions<3> gpu_options; | 
|  | 72 | + | 
|  | 73 | +  // Add to map using ArrayRef | 
|  | 74 | +  ASSERT_EQ(map.add("CPU", cpu_options.view()), Error::Ok); | 
|  | 75 | +  ASSERT_EQ(map.add("GPU", gpu_options.view()), Error::Ok); | 
|  | 76 | + | 
|  | 77 | +  auto entries = map.entries(); | 
|  | 78 | +  // Should have 2 backends (entries) | 
|  | 79 | +  ASSERT_EQ(entries.size(), 2); | 
|  | 80 | + | 
|  | 81 | +  bool found_cpu = false; | 
|  | 82 | +  bool found_gpu = false; | 
|  | 83 | +  for (const auto& entry : entries) { | 
|  | 84 | +    if (strcmp(entry.backend_name, "CPU") == 0) | 
|  | 85 | +      found_cpu = true; | 
|  | 86 | +    if (strcmp(entry.backend_name, "GPU") == 0) | 
|  | 87 | +      found_gpu = true; | 
|  | 88 | +  } | 
|  | 89 | +  // Should find CPU and GPU in the entries | 
|  | 90 | +  EXPECT_TRUE(found_cpu); | 
|  | 91 | +  EXPECT_TRUE(found_gpu); | 
|  | 92 | +} | 
|  | 93 | + | 
|  | 94 | +TEST_F(BackendOptionsMapTest, ConstCorrectness) { | 
|  | 95 | +  auto cpu_options = BackendOptions<5>(); | 
|  | 96 | +  ASSERT_EQ(map.add("CPU", cpu_options.view()), Error::Ok); | 
|  | 97 | + | 
|  | 98 | +  const auto& const_map = map; | 
|  | 99 | +  auto options_retrived = const_map.get("CPU"); | 
|  | 100 | +  EXPECT_EQ(options_retrived.size(), 0); | 
|  | 101 | + | 
|  | 102 | +  auto entries = const_map.entries(); | 
|  | 103 | +  EXPECT_FALSE(entries.empty()); | 
|  | 104 | +} | 
|  | 105 | + | 
|  | 106 | +TEST_F(BackendOptionsMapTest, EmptyMapBehavior) { | 
|  | 107 | +  EXPECT_EQ(map.get("CPU").size(), 0); | 
|  | 108 | +  EXPECT_TRUE(map.entries().empty()); | 
|  | 109 | +  EXPECT_EQ(map.entries().size(), 0); | 
|  | 110 | +} | 
|  | 111 | + | 
|  | 112 | +TEST_F(BackendOptionsMapTest, OptionIsolation) { | 
|  | 113 | +  BackendOptions<2> cpu_options; | 
|  | 114 | +  cpu_options.set_option(BoolKey("Debug"), true); | 
|  | 115 | +  cpu_options.set_option(IntKey("NumThreads"), 3); | 
|  | 116 | + | 
|  | 117 | +  BackendOptions<3> gpu_options; | 
|  | 118 | +  gpu_options.set_option(BoolKey("Profile"), true); | 
|  | 119 | +  gpu_options.set_option(IntKey("Mem"), 1024); | 
|  | 120 | +  gpu_options.set_option(StrKey("Hardware"), "H100"); | 
|  | 121 | + | 
|  | 122 | +  // Add to map using ArrayRef | 
|  | 123 | +  map.add("CPU", cpu_options.view()); | 
|  | 124 | +  map.add("GPU", gpu_options.view()); | 
|  | 125 | + | 
|  | 126 | +  // Test CPU options | 
|  | 127 | +  auto cpu_opts = map.get("CPU"); | 
|  | 128 | +  ASSERT_FALSE(cpu_opts.empty()); | 
|  | 129 | + | 
|  | 130 | +  // Verify CPU has its own option | 
|  | 131 | +  EXPECT_EQ(cpu_opts.size(), 2); | 
|  | 132 | +  EXPECT_EQ(cpu_opts[0].key, "Debug"); | 
|  | 133 | +  EXPECT_EQ(std::get<bool>(cpu_opts[0].value), true); | 
|  | 134 | +  EXPECT_EQ(cpu_opts[1].key, "NumThreads"); | 
|  | 135 | +  EXPECT_EQ(std::get<int>(cpu_opts[1].value), 3); | 
|  | 136 | + | 
|  | 137 | +  // Test GPU options | 
|  | 138 | +  auto gpu_opts = map.get("GPU"); | 
|  | 139 | +  ASSERT_FALSE(gpu_opts.empty()); | 
|  | 140 | + | 
|  | 141 | +  // Verify GPU has its own option | 
|  | 142 | +  EXPECT_EQ(gpu_opts.size(), 3); | 
|  | 143 | +  EXPECT_EQ(gpu_opts[0].key, "Profile"); | 
|  | 144 | +  EXPECT_EQ(std::get<bool>(gpu_opts[0].value), true); | 
|  | 145 | +  EXPECT_EQ(gpu_opts[1].key, "Mem"); | 
|  | 146 | +  EXPECT_EQ(std::get<int>(gpu_opts[1].value), 1024); | 
|  | 147 | +  EXPECT_EQ(gpu_opts[2].key, "Hardware"); | 
|  | 148 | +  EXPECT_EQ(std::get<const char*>(gpu_opts[2].value), "H100"); | 
|  | 149 | +} | 
|  | 150 | +} // namespace runtime | 
|  | 151 | +} // namespace executorch | 
0 commit comments