Skip to content

Commit ff4a736

Browse files
authored
Hide and simplify backend registry internals
Differential Revision: D61928651 Pull Request resolved: #4947
1 parent 7608ab8 commit ff4a736

File tree

2 files changed

+33
-66
lines changed

2 files changed

+33
-66
lines changed

runtime/backend/interface.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,44 @@ namespace runtime {
1414

1515
PyTorchBackendInterface::~PyTorchBackendInterface() {}
1616

17-
// TODO(T128866626): Remove global static variables.
18-
// We want to be able to run multiple Executor instances
19-
// and having a global registration isn't a viable solution
20-
// in the long term.
21-
BackendRegistry& getBackendRegistry();
22-
BackendRegistry& getBackendRegistry() {
23-
static BackendRegistry backend_reg;
24-
return backend_reg;
25-
}
17+
namespace {
2618

27-
PyTorchBackendInterface* get_backend_class(const char* name) {
28-
return getBackendRegistry().get_backend_class(name);
29-
}
19+
// The max number of backends that can be registered globally.
20+
constexpr size_t kMaxRegisteredBackends = 16;
21+
22+
// TODO(T128866626): Remove global static variables. We want to be able to run
23+
// multiple Executor instances and having a global registration isn't a viable
24+
// solution in the long term.
25+
26+
/// Global table of registered backends.
27+
Backend registered_backends[kMaxRegisteredBackends];
3028

31-
PyTorchBackendInterface* BackendRegistry::get_backend_class(const char* name) {
32-
for (size_t idx = 0; idx < registrationTableSize_; idx++) {
33-
Backend backend = backend_table_[idx];
34-
if (strcmp(backend.name_, name) == 0) {
35-
return backend.interface_ptr_;
29+
/// The number of backends registered in the table.
30+
size_t num_registered_backends = 0;
31+
32+
} // namespace
33+
34+
PyTorchBackendInterface* get_backend_class(const char* name) {
35+
for (size_t i = 0; i < num_registered_backends; i++) {
36+
Backend backend = registered_backends[i];
37+
if (strcmp(backend.name, name) == 0) {
38+
return backend.backend;
3639
}
3740
}
3841
return nullptr;
3942
}
4043

4144
Error register_backend(const Backend& backend) {
42-
return getBackendRegistry().register_backend(backend);
43-
}
44-
45-
Error BackendRegistry::register_backend(const Backend& backend) {
46-
if (registrationTableSize_ >= kRegistrationTableMaxSize) {
45+
if (num_registered_backends >= kMaxRegisteredBackends) {
4746
return Error::Internal;
4847
}
4948

5049
// Check if the name already exists in the table
51-
if (this->get_backend_class(backend.name_) != nullptr) {
50+
if (get_backend_class(backend.name) != nullptr) {
5251
return Error::InvalidArgument;
5352
}
5453

55-
backend_table_[registrationTableSize_++] = backend;
54+
registered_backends[num_registered_backends++] = backend;
5655
return Error::Ok;
5756
}
5857

runtime/backend/interface.h

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -110,46 +110,6 @@ class PyTorchBackendInterface {
110110
virtual void destroy(ET_UNUSED DelegateHandle* handle) const {}
111111
};
112112

113-
struct Backend {
114-
const char* name_;
115-
PyTorchBackendInterface* interface_ptr_;
116-
};
117-
118-
// The max number of backends that can be registered in
119-
// an app. It's hard coded to 16 because it's not estimated
120-
// to have more than 16 backends in a system. Each table
121-
// element has two pointers, represented by Backend struct.
122-
// The memory overhead for this table is minimum (only a few bytes).
123-
constexpr size_t kRegistrationTableMaxSize = 16;
124-
125-
class BackendRegistry {
126-
public:
127-
BackendRegistry() : registrationTableSize_(0) {}
128-
129-
/**
130-
* Registers the Backend object (i.e. string name and PyTorchBackendInterface
131-
* pair) so that it could be called via the name during the runtime.
132-
* @param[in] backend Backend object of the user-defined backend delegate.
133-
* @retval Error code representing whether registration was successful.
134-
*/
135-
ET_NODISCARD Error register_backend(const Backend& backend);
136-
137-
/**
138-
* Returns the corresponding object pointer for a given string name.
139-
* The mapping is populated using register_backend method.
140-
*
141-
* @param[in] name Name of the user-defined backend delegate.
142-
* @retval Pointer to the appropriate object that implements
143-
* PyTorchBackendInterface. Nullptr if it can't find anything
144-
* with the given name.
145-
*/
146-
PyTorchBackendInterface* get_backend_class(const char* name);
147-
148-
private:
149-
Backend backend_table_[kRegistrationTableMaxSize];
150-
size_t registrationTableSize_;
151-
};
152-
153113
/**
154114
* Returns the corresponding object pointer for a given string name.
155115
* The mapping is populated using register_backend method.
@@ -161,6 +121,16 @@ class BackendRegistry {
161121
*/
162122
PyTorchBackendInterface* get_backend_class(const char* name);
163123

124+
/**
125+
* A named instance of a backend.
126+
*/
127+
struct Backend {
128+
/// The name of the backend. Must match the string used in the PTE file.
129+
const char* name;
130+
/// The instance of the backend to use when loading and executing programs.
131+
PyTorchBackendInterface* backend;
132+
};
133+
164134
/**
165135
* Registers the Backend object (i.e. string name and PyTorchBackendInterface
166136
* pair) so that it could be called via the name during the runtime.
@@ -178,11 +148,9 @@ namespace executor {
178148
// TODO(T197294990): Remove these deprecated aliases once all users have moved
179149
// to the new `::executorch` namespaces.
180150
using ::executorch::runtime::Backend;
181-
using ::executorch::runtime::BackendRegistry;
182151
using ::executorch::runtime::CompileSpec;
183152
using ::executorch::runtime::DelegateHandle;
184153
using ::executorch::runtime::get_backend_class;
185-
// using ::executorch::runtime::kRegistrationTableMaxSize;
186154
using ::executorch::runtime::PyTorchBackendInterface;
187155
using ::executorch::runtime::register_backend;
188156
using ::executorch::runtime::SizedBuffer;

0 commit comments

Comments
 (0)