Skip to content

Commit a26ada5

Browse files
committed
Update on "[5/N] Add update in method"
Expose the update API in method. This API will take BackendOptionMap as input and call backend_interface->update under the hood. Differential Revision: [D76149467](https://our.internmc.facebook.com/intern/diff/D76149467/) [ghstack-poisoned]
2 parents 142fdbd + ea53a16 commit a26ada5

File tree

2 files changed

+91
-94
lines changed

2 files changed

+91
-94
lines changed

runtime/backend/backend_options.h

Lines changed: 91 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,92 +6,94 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
#pragma once
10-
#include <executorch/runtime/core/array_ref.h>
11-
#include <executorch/runtime/core/error.h>
12-
#include <cstddef>
13-
#include <cstring>
14-
#include <variant>
15-
16-
namespace executorch {
17-
namespace runtime {
18-
19-
// Strongly-typed option key template
20-
template <typename T>
21-
struct OptionKey {
22-
const char* key;
23-
constexpr explicit OptionKey(const char* k) : key(k) {}
24-
};
25-
26-
// Union replaced with std::variant
27-
using OptionValue = std::variant<bool, int, const char*>;
28-
29-
struct BackendOption {
30-
const char* key; // key is the name of the backend option, like num_threads,
31-
// enable_profiling, etc
32-
OptionValue
33-
value; // value is the value of the backend option, like 4, true, etc
34-
};
35-
36-
template <size_t MaxCapacity>
37-
class BackendOptions {
38-
public:
39-
// Initialize with zero options
40-
BackendOptions() : size_(0) {}
41-
42-
// Type-safe setters
43-
template <typename T>
44-
void set_option(OptionKey<T> key, T value) {
45-
const char* k = key.key;
46-
// Update existing if found
47-
for (size_t i = 0; i < size_; ++i) {
48-
if (strcmp(options_[i].key, k) == 0) {
49-
options_[i].value = value;
50-
return;
51-
}
52-
}
53-
// Add new option if space available
54-
if (size_ < MaxCapacity) {
55-
options_[size_++] = BackendOption{k, value};
56-
}
57-
}
58-
59-
// Type-safe getters
60-
template <typename T>
61-
Error get_option(OptionKey<T> key, T& out) const {
62-
const char* k = key.key;
63-
for (size_t i = 0; i < size_; ++i) {
64-
if (strcmp(options_[i].key, k) == 0) {
65-
if (auto* val = std::get_if<T>(&options_[i].value)) {
66-
out = *val;
67-
return Error::Ok;
68-
}
69-
return Error::InvalidArgument;
70-
}
71-
}
72-
return Error::NotFound;
73-
}
74-
executorch::runtime::ArrayRef<BackendOption> view() const {
75-
return executorch::runtime::ArrayRef<BackendOption>(options_, size_);
76-
}
77-
78-
private:
79-
BackendOption options_[MaxCapacity]{}; // Storage for backend options
80-
size_t size_; // Current number of options
81-
};
82-
83-
// Helper functions for creating typed option keys (unchanged)
84-
constexpr OptionKey<bool> BoolKey(const char* k) {
85-
return OptionKey<bool>(k);
86-
}
87-
88-
constexpr OptionKey<int> IntKey(const char* k) {
89-
return OptionKey<int>(k);
90-
}
91-
92-
constexpr OptionKey<const char*> StrKey(const char* k) {
93-
return OptionKey<const char*>(k);
94-
}
95-
96-
} // namespace runtime
97-
} // namespace executorch
9+
#pragma once
10+
#include <executorch/runtime/core/error.h>
11+
#include <cstddef>
12+
#include <cstring>
13+
#include <executorch/runtime/core/array_ref.h>
14+
#include <executorch/runtime/core/error.h>
15+
#include <variant>
16+
17+
namespace executorch {
18+
namespace runtime {
19+
20+
// Strongly-typed option key template
21+
template <typename T>
22+
struct OptionKey {
23+
const char* key;
24+
constexpr explicit OptionKey(const char* k) : key(k) {}
25+
};
26+
27+
// Union replaced with std::variant
28+
using OptionValue = std::variant<bool, int, const char*>;
29+
30+
struct BackendOption {
31+
const char* key; // key is the name of the backend option, like num_threads,
32+
// enable_profiling, etc
33+
OptionValue
34+
value; // value is the value of the backend option, like 4, true, etc
35+
};
36+
37+
template <size_t MaxCapacity>
38+
class BackendOptions {
39+
public:
40+
// Initialize with zero options
41+
BackendOptions() : size_(0) {}
42+
43+
// Type-safe setters
44+
template <typename T>
45+
void set_option(OptionKey<T> key, T value) {
46+
const char* k = key.key;
47+
// Update existing if found
48+
for (size_t i = 0; i < size_; ++i) {
49+
if (strcmp(options_[i].key, k) == 0) {
50+
options_[i].value = value;
51+
return;
52+
}
53+
}
54+
// Add new option if space available
55+
if (size_ < MaxCapacity) {
56+
options_[size_++] = BackendOption{k, value};
57+
}
58+
}
59+
60+
// Type-safe getters
61+
template <typename T>
62+
Error get_option(OptionKey<T> key, T& out) const {
63+
const char* k = key.key;
64+
for (size_t i = 0; i < size_; ++i) {
65+
if (strcmp(options_[i].key, k) == 0) {
66+
if (auto* val = std::get_if<T>(&options_[i].value)) {
67+
out = *val;
68+
return Error::Ok;
69+
}
70+
return Error::InvalidArgument;
71+
}
72+
}
73+
return Error::NotFound;
74+
}
75+
executorch::runtime::ArrayRef<BackendOption> view() const {
76+
return executorch::runtime::ArrayRef<BackendOption>(options_, size_);
77+
}
78+
79+
private:
80+
BackendOption options_[MaxCapacity]{}; // Storage for backend options
81+
size_t size_; // Current number of options
82+
};
83+
84+
// Helper functions for creating typed option keys (unchanged)
85+
constexpr OptionKey<bool> BoolKey(const char* k) {
86+
return OptionKey<bool>(k);
87+
}
88+
89+
constexpr OptionKey<int> IntKey(const char* k) {
90+
return OptionKey<int>(k);
91+
}
92+
93+
constexpr OptionKey<const char*> StrKey(const char* k) {
94+
return OptionKey<const char*>(k);
95+
}
96+
97+
} // namespace runtime
98+
} // namespace executorch
99+

runtime/backend/test/backend_options_map_test.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ TEST_F(BackendOptionsMapTest, BasicAddAndRetrieve) {
5656
EXPECT_TRUE(found);
5757
}
5858

59-
// TEST_F(BackendOptionsMapTest, UnknownBackendHandling) {
60-
// EXPECT_EQ(map.get("NPU"), nullptr)
61-
// << "Should return nullptr for unknown backend";
62-
// }
63-
6459
TEST_F(BackendOptionsMapTest, CapacityLimits) {
6560
BackendOptionsMap<2> small_map; // Only 2 backends capacity
6661

0 commit comments

Comments
 (0)