Skip to content

Commit 5ba13cc

Browse files
committed
Update on "[kernel] Add template based unboxing"
Adding a new feature to allow users to bypass codegen and register their kernels directly. This is very useful for custom kernels for custom ops. Example usage: ``` Tensor& my_op(RuntimeContext& ctx, const Tensor& self, const Tensor& other, Tensor& out) { // ... return out; } Kernel my_kernel = Kernel.make_boxed_kernel("my_ns::my_op",EXECUTORCH_FN(my_op)); register_kernels({my_kernel}); ``` Differential Revision: [D51553099](https://our.internmc.facebook.com/intern/diff/D51553099) [ghstack-poisoned]
2 parents 96adfcf + b5ab718 commit 5ba13cc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

extension/kernel_util/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
This header file `make_boxed_from_unboxed_functor.h` defines a template that can be used to create a boxed version of an unboxed functor. It is part of the executorch extension in the torch namespace.
2+
## Requirements
3+
This header requires C++17 or later.
4+
## Usage
5+
The template takes an unboxed function pointer and wraps it into a functor that takes `RuntimeContext` and `EValues` as inputs and returns void. The wrapped functor will unbox all inputs and forward them to the unboxed kernel.
6+
Here is an example of how to use the template:
7+
```C++
8+
Tensor& my_op(RuntimeContext& ctx, const Tensor& self, const Tensor& other, Tensor& out) {
9+
// ...
10+
return out;
11+
}
12+
Kernel my_kernel = Kernel::make_boxed_kernel("my_ns::my_op", EXECUTORCH_FN(my_op));
13+
static auto res = register_kernels({my_kernel});
14+
```
15+
Alternatively, you can use the EXECUTORCH_LIBRARY macro to simplify the process:
16+
```C++
17+
EXECUTORCH_LIBRARY(my_ns, "my_op", my_op);
18+
```
19+
## Details
20+
The template uses a lot of C++17 features to convert each EValue to the inferred argument type. It checks if the first argument is `RuntimeContext`, and if so, it removes it. The call method of the `WrapUnboxedIntoFunctor` struct calls the unboxed function with the corresponding arguments.
21+
The `EXECUTORCH_LIBRARY` macro registers the kernel for the operation and stores the result in a static variable.
22+
## Note
23+
The `RuntimeContext` is a placeholder for a context that will be passed to kernels. It is currently empty, but it is planned to be used for kernel temp memory allocation and error handling in the future.

0 commit comments

Comments
 (0)