Skip to content

Commit 5c9c342

Browse files
committed
[3/N] Add update function in backend interface
Pull Request resolved: #11391 Add update function in backend interface class. The update function will receive the backend options from dispatched by the ET runtime. ET runtime's logic: loop over each backend and it's corresponding backend options, dispatch the backend options to the corresponding backend Next step, will add update API in the method and then module ghstack-source-id: 290059231 @exported-using-ghexport Differential Revision: [D75919242](https://our.internmc.facebook.com/intern/diff/D75919242/)
1 parent f2078a2 commit 5c9c342

File tree

4 files changed

+401
-85
lines changed

4 files changed

+401
-85
lines changed

runtime/backend/backend_options.h

Lines changed: 91 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,88 +6,94 @@
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 <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
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/interface.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +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>
16+
#include <executorch/runtime/backend/backend_options.h>
1517
#include <executorch/runtime/core/array_ref.h>
1618
#include <executorch/runtime/core/error.h>
1719
#include <executorch/runtime/core/evalue.h>
@@ -99,6 +101,20 @@ class BackendInterface {
99101
DelegateHandle* handle,
100102
EValue** args) const = 0;
101103

104+
/**
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.
107+
*
108+
* @param[in] context Runtime context if any. Currently it's not used.
109+
* @param[in] args A list of BackendOptions passed in by users.
110+
* @retval Error::Ok if successful.
111+
*/
112+
ET_NODISCARD virtual Error update(
113+
BackendUpdateContext& context,
114+
const executorch::runtime::ArrayRef<BackendOption>& backend_options) const {
115+
return Error::Ok;
116+
};
117+
102118
/**
103119
* Responsible for destroying a handle, if it's required for some backend.
104120
* It may be needed for some backends. For example, resources associated with

0 commit comments

Comments
 (0)