|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | +/// \file extension/kernel_util/make_boxed_from_unboxed_functor.h |
| 11 | +/// Defines a template that can be used to create a boxed version of an unboxed |
| 12 | +/// functor. |
| 13 | +/// Example usage: |
| 14 | +/// ``` |
| 15 | +/// Tensor& |
| 16 | +/// my_op(RuntimeContext& ctx, const Tensor& self, const Tensor& other, Tensor& |
| 17 | +/// out) { |
| 18 | +/// // ... |
| 19 | +/// return out; |
| 20 | +/// } |
| 21 | +/// |
| 22 | +/// Kernel my_kernel = Kernel::make_boxed_kernel("my_ns::my_op", |
| 23 | +/// EXECUTORCH_FN(my_op)); |
| 24 | +/// static auto res = register_kernels({my_kernel}); |
| 25 | +/// ``` |
| 26 | +/// Or simply: |
| 27 | +/// ``` |
| 28 | +/// EXECUTORCH_LIBRARY(my_ns, "my_op", my_op); |
| 29 | +/// ``` |
| 30 | +/// |
| 31 | +/// The trick here is to convert each EValue to inferred argument type. This |
| 32 | +/// uses a lot of C++17 features. |
| 33 | +//===----------------------------------------------------------------------===// |
| 34 | + |
| 35 | +#pragma once |
| 36 | +#if __cplusplus < 201703L |
| 37 | +#error "This header requires C++17" |
| 38 | +#endif |
| 39 | + |
| 40 | +#include <executorch/extension/kernel_util/meta_programming.h> |
| 41 | +#include <executorch/extension/kernel_util/type_list.h> |
| 42 | +#include <executorch/runtime/core/evalue.h> |
| 43 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 44 | +#include <executorch/runtime/kernel/operator_registry.h> |
| 45 | +#include <cstdlib> |
| 46 | +#include <memory> |
| 47 | +#include <type_traits> |
| 48 | +#include <typeinfo> |
| 49 | + |
| 50 | +namespace torch { |
| 51 | +namespace executor { |
| 52 | + |
| 53 | +class KernelRuntimeContext; // Forward declaration |
| 54 | +using RuntimeContext = KernelRuntimeContext; // TODO(T147221312): Remove |
| 55 | + |
| 56 | +// evalue_to_arg |
| 57 | +template <class T> |
| 58 | +struct decay_if_not_tensor final { |
| 59 | + using type = std::decay_t<T>; |
| 60 | +}; |
| 61 | +template <> |
| 62 | +struct decay_if_not_tensor<exec_aten::Tensor&> final { |
| 63 | + using type = exec_aten::Tensor&; |
| 64 | +}; |
| 65 | +template <> |
| 66 | +struct decay_if_not_tensor<const exec_aten::Tensor&> final { |
| 67 | + using type = const exec_aten::Tensor&; |
| 68 | +}; |
| 69 | + |
| 70 | +template <class T> |
| 71 | +struct evalue_to_arg final { |
| 72 | + static T call(EValue& v) { |
| 73 | + return std::move(v).to<T>(); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +template <> |
| 78 | +struct evalue_to_arg<exec_aten::Tensor&> final { |
| 79 | + static exec_aten::Tensor& call(EValue& v) { |
| 80 | + return v.toTensor(); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +template <> |
| 85 | +struct evalue_to_arg<const exec_aten::Tensor&> final { |
| 86 | + static const exec_aten::Tensor& call(EValue& v) { |
| 87 | + return v.toTensor(); |
| 88 | + } |
| 89 | +}; |
| 90 | +// Call functor with args from stack |
| 91 | + |
| 92 | +template <class Functor, size_t... evalue_arg_indices, typename... ArgTypes> |
| 93 | +void call_functor_with_args_from_stack_( |
| 94 | + RuntimeContext& ctx, |
| 95 | + EValue** stack, |
| 96 | + std::index_sequence<evalue_arg_indices...>, |
| 97 | + typelist<ArgTypes...>*) { |
| 98 | + (*Functor::func_ptr())( |
| 99 | + ctx, |
| 100 | + evalue_to_arg<typename decay_if_not_tensor<ArgTypes>::type>::call( |
| 101 | + *stack[evalue_arg_indices])...); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * WrapUnboxedIntoFunctor: Given a function pointer, wrap it into a functor that |
| 106 | + * takes EValues as input and returns void. The wrapped functor will unbox all |
| 107 | + * inputs and forward them to unboxed kernel. |
| 108 | + */ |
| 109 | +template <class FuncType> |
| 110 | +struct WrapUnboxedIntoFunctor { |
| 111 | + static_assert( |
| 112 | + is_compile_time_function_pointer<FuncType>::value, |
| 113 | + "Can't handle function other than EXECUTORCH_FN"); |
| 114 | + using TrueType = typename FuncType::FuncType; |
| 115 | + using ReturnType = typename infer_function_traits_t<TrueType>::return_type; |
| 116 | + using ArgsType = typename infer_function_traits_t<TrueType>::parameter_types; |
| 117 | + // check if the first argument is RuntimeContext, if so, remove it |
| 118 | + static constexpr bool first_arg_is_context = std::is_same< |
| 119 | + RuntimeContext, |
| 120 | + std::remove_reference_t<head_with_default_t<void, ArgsType>>>::value; |
| 121 | + using ContextRemovedArgsType = std::conditional_t< |
| 122 | + first_arg_is_context, |
| 123 | + drop_if_nonempty_t<ArgsType, 1>, |
| 124 | + ArgsType>; |
| 125 | + |
| 126 | + static void call(RuntimeContext& ctx, EValue** stack) { |
| 127 | + constexpr size_t num_inputs = size<ContextRemovedArgsType>::value; |
| 128 | + return call_functor_with_args_from_stack_<FuncType>( |
| 129 | + ctx, |
| 130 | + stack, |
| 131 | + std::make_index_sequence<num_inputs>(), |
| 132 | + static_cast<ContextRemovedArgsType*>(nullptr)); |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | +template <typename FuncType> |
| 137 | +static Kernel make_boxed_kernel(const char* name, FuncType) { |
| 138 | + return Kernel(name, WrapUnboxedIntoFunctor<FuncType>::call); |
| 139 | +} |
| 140 | + |
| 141 | +#define EXECUTORCH_LIBRARY(ns, op_name, func) \ |
| 142 | + static auto res_##ns = register_kernels( \ |
| 143 | + make_boxed_kernel(#ns "::" op_name, EXECUTORCH_FN(func))) |
| 144 | +} // namespace executor |
| 145 | +} // namespace torch |
0 commit comments