Skip to content

Commit 6c9240b

Browse files
cccclaifacebook-github-bot
authored andcommitted
Allow getting all backend names
Summary: Allow getting all backends name in both python and c++ Differential Revision: D69691354
1 parent 5e4d6b6 commit 6c9240b

File tree

7 files changed

+57
-0
lines changed

7 files changed

+57
-0
lines changed

extension/pybindings/portable_lib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
# Disable "imported but unused" (F401) checks.
3838
_create_profile_block, # noqa: F401
3939
_dump_profile_results, # noqa: F401
40+
_get_backend_names, # noqa: F401
4041
_get_operator_names, # noqa: F401
4142
_load_bundled_program_from_buffer, # noqa: F401
4243
_load_for_executorch, # noqa: F401

extension/pybindings/pybindings.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <executorch/runtime/executor/method.h>
2929
#include <executorch/runtime/executor/program.h>
3030
#include <executorch/runtime/kernel/operator_registry.h>
31+
#include <executorch/runtime/backend/interface.h>
3132
#include <executorch/runtime/platform/assert.h>
3233
#include <executorch/runtime/platform/platform.h>
3334
#include <executorch/runtime/platform/profiler.h>
@@ -87,10 +88,12 @@ using ::executorch::extension::BufferDataLoader;
8788
using ::executorch::extension::MallocMemoryAllocator;
8889
using ::executorch::extension::MmapDataLoader;
8990
using ::executorch::runtime::ArrayRef;
91+
using ::executorch::runtime::Backend;
9092
using ::executorch::runtime::DataLoader;
9193
using ::executorch::runtime::Error;
9294
using ::executorch::runtime::EValue;
9395
using ::executorch::runtime::EventTracerDebugLogLevel;
96+
using ::executorch::runtime::get_registered_backends;
9497
using ::executorch::runtime::get_registered_kernels;
9598
using ::executorch::runtime::HierarchicalAllocator;
9699
using ::executorch::runtime::Kernel;
@@ -975,6 +978,17 @@ py::list get_operator_names() {
975978
return res;
976979
}
977980

981+
py::list get_backend_names() {
982+
Span<const Backend> backends = get_registered_backends();
983+
py::list res;
984+
for (const Backend& backend : backends) {
985+
if (backend.name != nullptr) {
986+
res.append(py::cast(backend.name));
987+
}
988+
}
989+
return res;
990+
}
991+
978992
} // namespace
979993

980994
PYBIND11_MODULE(EXECUTORCH_PYTHON_MODULE_NAME, m) {
@@ -1029,6 +1043,7 @@ PYBIND11_MODULE(EXECUTORCH_PYTHON_MODULE_NAME, m) {
10291043
},
10301044
call_guard);
10311045
m.def("_get_operator_names", &get_operator_names);
1046+
m.def("_get_backend_names", &get_backend_names);
10321047
m.def("_create_profile_block", &create_profile_block, call_guard);
10331048
m.def(
10341049
"_reset_profile_results",

extension/pybindings/pybindings.pyi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,16 @@ def _load_bundled_program_from_buffer(
211211
"""
212212
...
213213

214+
@experimental("This API is experimental and subject to change without notice.")
215+
def _get_backend_names() -> List[str]:
216+
"""
217+
.. warning::
218+
219+
This API is experimental and subject to change without notice.
220+
"""
221+
...
222+
223+
214224
@experimental("This API is experimental and subject to change without notice.")
215225
def _get_operator_names() -> List[str]:
216226
"""

extension/pybindings/test/TARGETS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,11 @@ runtime.python_test(
4747
"//executorch/kernels/quantized:aot_lib",
4848
],
4949
)
50+
51+
runtime.python_test(
52+
name = "test_backend_pybinding",
53+
srcs = ["test_backend_pybinding.py"],
54+
deps = [
55+
"//executorch/extension/pybindings:portable_lib",
56+
],
57+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
from executorch.extension.pybindings.portable_lib import _get_backend_names
3+
4+
class TestBackendsPybinding(unittest.TestCase):
5+
def test_backend_name_list(
6+
self,
7+
) -> None:
8+
all_backend_name = _get_backend_names()
9+
self.assertEqual(len(all_backend_name), 3)
10+
self.assertIn("BackendWithCompilerDemo", all_backend_name)
11+
self.assertIn("QnnpackBackend", all_backend_name)
12+
self.assertIn("XnnpackBackend", all_backend_name)

runtime/backend/interface.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,9 @@ Error register_backend(const Backend& backend) {
5555
return Error::Ok;
5656
}
5757

58+
Span<const Backend> get_registered_backends() {
59+
return {registered_backends, num_registered_backends};
60+
}
61+
5862
} // namespace runtime
5963
} // namespace executorch

runtime/backend/interface.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <executorch/runtime/core/memory_allocator.h>
2020
#include <executorch/runtime/core/result.h>
2121
#include <executorch/runtime/platform/compiler.h>
22+
#include <executorch/runtime/core/span.h>
2223

2324
namespace executorch {
2425
namespace runtime {
@@ -139,9 +140,15 @@ struct Backend {
139140
*/
140141
ET_NODISCARD Error register_backend(const Backend& backend);
141142

143+
/**
144+
* Returns all registered backends.
145+
*/
146+
Span<const Backend> get_registered_backends();
147+
142148
} // namespace runtime
143149
} // namespace executorch
144150

151+
145152
namespace torch {
146153
namespace executor {
147154
// TODO(T197294990): Remove these deprecated aliases once all users have moved

0 commit comments

Comments
 (0)