For a given `NativeFunction`, find out the corresponding native function and dispatch to it. If zero or more than one native function exists, error out. A simplified version of register_dispatch_key.py Arguments: sig: A CppSignature for this native function we want to use.
(
sig: CppSignature | ExecutorchCppSignature,
f: NativeFunction,
backend_indices: list[BackendIndex],
)
| 96 | |
| 97 | |
| 98 | def static_dispatch( |
| 99 | sig: CppSignature | ExecutorchCppSignature, |
| 100 | f: NativeFunction, |
| 101 | backend_indices: list[BackendIndex], |
| 102 | ) -> str: |
| 103 | """ |
| 104 | For a given `NativeFunction`, find out the corresponding native function and dispatch to it. If zero or more than one |
| 105 | native function exists, error out. A simplified version of register_dispatch_key.py |
| 106 | Arguments: |
| 107 | sig: A CppSignature for this native function we want to use. |
| 108 | f: NativeFunction to generate static dispatch. |
| 109 | backend_indices: All available backends. |
| 110 | Return: |
| 111 | C++ code to call backend-specific functions, e.g., "return at::native::add(self, other, scale);" |
| 112 | """ |
| 113 | if len(backend_indices) == 0 or f.manual_kernel_registration: |
| 114 | return "" |
| 115 | |
| 116 | backends = [b for b in backend_indices if b.has_kernel(f)] |
| 117 | static_block = None |
| 118 | if len(backends) == 1: |
| 119 | backend_metadata = backends[0].get_kernel(f) |
| 120 | if backend_metadata: |
| 121 | args = ", ".join(a.name for a in sig.arguments()) |
| 122 | # Here we are assuming there's no difference between CppSignature and NativeSignature for Executorch. |
| 123 | static_block = f"return ::{backend_metadata.cpp_namespace}::{backend_metadata.kernel}({args});" |
| 124 | else: |
| 125 | static_block = f""" |
| 126 | ET_ASSERT_UNREACHABLE_MSG("The number of native function(s) binding to {f.func.name} is {len(backends)}."); |
| 127 | """ |
| 128 | return f""" |
| 129 | // {f.namespace}::{f.func} |
| 130 | TORCH_API inline {_sig_decl_wrapper(sig)} {{ |
| 131 | {static_block} |
| 132 | }} |
| 133 | """ |
| 134 | |
| 135 | |
| 136 | # Generates Functions.h, which provides the functional public C++ API, |
no test coverage detected