1*523fa7a6SAndroid Build Coastguard WorkerThis 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*523fa7a6SAndroid Build Coastguard Worker## Requirements 3*523fa7a6SAndroid Build Coastguard WorkerThis header requires C++17 or later. 4*523fa7a6SAndroid Build Coastguard Worker## Usage 5*523fa7a6SAndroid Build Coastguard WorkerThe template takes an unboxed function pointer and wraps it into a functor that takes `KernelRuntimeContext` and `EValues` as inputs and returns void. The wrapped functor will unbox all inputs and forward them to the unboxed kernel. 6*523fa7a6SAndroid Build Coastguard WorkerHere is an example of how to use the template: 7*523fa7a6SAndroid Build Coastguard Worker```C++ 8*523fa7a6SAndroid Build Coastguard WorkerTensor& my_op(KernelRuntimeContext& ctx, const Tensor& self, const Tensor& other, Tensor& out) { 9*523fa7a6SAndroid Build Coastguard Worker // ... 10*523fa7a6SAndroid Build Coastguard Worker return out; 11*523fa7a6SAndroid Build Coastguard Worker} 12*523fa7a6SAndroid Build Coastguard WorkerKernel my_kernel = Kernel::make_boxed_kernel("my_ns::my_op", EXECUTORCH_FN(my_op)); 13*523fa7a6SAndroid Build Coastguard Workerstatic auto res = register_kernels({my_kernel}); 14*523fa7a6SAndroid Build Coastguard Worker``` 15*523fa7a6SAndroid Build Coastguard WorkerAlternatively, you can use the EXECUTORCH_LIBRARY macro to simplify the process: 16*523fa7a6SAndroid Build Coastguard Worker```C++ 17*523fa7a6SAndroid Build Coastguard WorkerEXECUTORCH_LIBRARY(my_ns, "my_op", my_op); 18*523fa7a6SAndroid Build Coastguard Worker``` 19*523fa7a6SAndroid Build Coastguard Worker## Details 20*523fa7a6SAndroid Build Coastguard WorkerThe template uses a lot of C++17 features to convert each EValue to the inferred argument type. It checks if the first argument is `KernelRuntimeContext`, and if so, it removes it. The call method of the `WrapUnboxedIntoFunctor` struct calls the unboxed function with the corresponding arguments. 21*523fa7a6SAndroid Build Coastguard WorkerThe `EXECUTORCH_LIBRARY` macro registers the kernel for the operation and stores the result in a static variable. 22*523fa7a6SAndroid Build Coastguard Worker## Note 23*523fa7a6SAndroid Build Coastguard WorkerThe `KernelRuntimeContext` is a context object that lets kernels handle errors and allocate temp memory. It can be used to add support for other actions in the future. 24