Skip to content

Commit ba6f9f9

Browse files
committed
Update on "[4/N] Add backend options map"
This is to manage the backend <-> BackendOptions map. Users will create the bakcend options map, and ET runtime will read the backend name, and dispatch the list of backend options to each backend. Differential Revision: [D76149466](https://our.internmc.facebook.com/intern/diff/D76149466/) [ghstack-poisoned]
2 parents 5685bf2 + bf88011 commit ba6f9f9

File tree

6 files changed

+153
-127
lines changed

6 files changed

+153
-127
lines changed

runtime/backend/backend_options.h

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

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

runtime/backend/backend_options_map.h

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9+
#pragma once
10+
911
#include <executorch/runtime/backend/backend_options.h>
1012
#include <executorch/runtime/core/error.h>
13+
#include <executorch/runtime/core/span.h>
1114
#include <cstring>
12-
13-
#pragma once
1415
namespace executorch {
1516
namespace runtime {
1617

1718
struct Entry {
1819
const char* backend_name;
19-
ArrayRef<BackendOption> options;
20+
Span<BackendOption> options;
2021
};
2122

2223
template <size_t MaxBackends>
@@ -28,7 +29,7 @@ class BackendOptionsMap {
2829
// Add a new backend configuration
2930
Error add(
3031
const char* backend_name,
31-
::executorch::runtime::ArrayRef<BackendOption> options) {
32+
::executorch::runtime::Span<BackendOption> options) {
3233
if (size_ < MaxBackends) {
3334
entries_[size_] = {backend_name, options};
3435
++size_;
@@ -39,25 +40,35 @@ class BackendOptionsMap {
3940
return Error::InvalidArgument;
4041
}
4142

42-
// Get options for a specific backend
43-
::executorch::runtime::ArrayRef<BackendOption> get(
43+
// Get options for a specific backend (const version)
44+
::executorch::runtime::Span<const BackendOption> get(
4445
const char* backend_name) const {
4546
for (size_t i = 0; i < size_; ++i) {
4647
if (std::strcmp(entries_[i].backend_name, backend_name) == 0) {
4748
return entries_[i].options;
4849
}
4950
}
50-
return {}; // Return empty ArrayRef if not found
51+
return {}; // Return empty Span if not found
52+
}
53+
54+
// Get options for a specific backend (non-const version)
55+
::executorch::runtime::Span<BackendOption> get(const char* backend_name) {
56+
for (size_t i = 0; i < size_; ++i) {
57+
if (std::strcmp(entries_[i].backend_name, backend_name) == 0) {
58+
return entries_[i].options;
59+
}
60+
}
61+
return {}; // Return empty Span if not found
5162
}
5263

5364
// Get a view of the entries (const version)
54-
::executorch::runtime::ArrayRef<const Entry> entries() const {
55-
return ::executorch::runtime::ArrayRef<const Entry>(entries_, size_);
65+
::executorch::runtime::Span<const Entry> entries() const {
66+
return ::executorch::runtime::Span<const Entry>(entries_, size_);
5667
}
5768

5869
// Get a view of the entries (non-const version)
59-
::executorch::runtime::ArrayRef<Entry> entries() {
60-
return ::executorch::runtime::ArrayRef<Entry>(entries_, size_);
70+
::executorch::runtime::Span<Entry> entries() {
71+
return ::executorch::runtime::Span<Entry>(entries_, size_);
6172
}
6273

6374
// Get number of entries

runtime/backend/interface.h

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
#include <executorch/runtime/backend/backend_execution_context.h>
1414
#include <executorch/runtime/backend/backend_init_context.h>
15-
#include <executorch/runtime/backend/backend_update_context.h>
1615
#include <executorch/runtime/backend/backend_options.h>
16+
#include <executorch/runtime/backend/backend_update_context.h>
1717
#include <executorch/runtime/core/array_ref.h>
1818
#include <executorch/runtime/core/error.h>
1919
#include <executorch/runtime/core/evalue.h>
@@ -102,18 +102,35 @@ class BackendInterface {
102102
EValue** args) const = 0;
103103

104104
/**
105-
* Responsible update the backend status, if any. The backend options are passed in
106-
* by users, and the backend can update its internal status based on the options.
105+
* Responsible update the backend status, if any. The backend options are
106+
* passed in by users, and the backend can update its internal status based on
107+
* the options.
107108
*
108109
* @param[in] context Runtime context if any. Currently it's not used.
109110
* @param[in] args A list of BackendOptions passed in by users.
110111
* @retval Error::Ok if successful.
111112
*/
112-
ET_NODISCARD virtual Error update(
113-
BackendUpdateContext& context,
114-
const executorch::runtime::ArrayRef<BackendOption>& backend_options) const {
115-
return Error::Ok;
116-
};
113+
ET_NODISCARD virtual Error set_option(
114+
BackendUpdateContext& context,
115+
const executorch::runtime::Span<BackendOption>& backend_options) {
116+
return Error::Ok;
117+
};
118+
119+
/**
120+
* Responsible update the backend status, if any. The backend options are
121+
* passed in by users, and the backend can update its internal status based on
122+
* the options.
123+
*
124+
* @param[in] context Runtime context if any. Currently it's not used.
125+
* @param[in] args A list of BackendOptions passed in by users, that will be
126+
* filled by the backend
127+
* @retval Error::Ok if successful.
128+
*/
129+
ET_NODISCARD virtual Error get_option(
130+
BackendUpdateContext& context,
131+
executorch::runtime::Span<BackendOption>& backend_options) {
132+
return Error::Ok;
133+
};
117134

118135
/**
119136
* Responsible for destroying a handle, if it's required for some backend.

0 commit comments

Comments
 (0)