|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <c10/util/Metaprogramming.h> |
| 4 | +#include <c10/util/TypeList.h> |
| 5 | +#include <c10/core/TensorOptions.h> |
| 6 | +#include <c10/core/CompileTimeFunctionPointer.h> |
| 7 | + |
| 8 | +// This file defines hacky_wrapper_for_legacy_signatures, which takes a kernel written in a legacy way |
| 9 | +// (e.g. with TensorOptions packed) and wraps it into a kernel with the signature expected by |
| 10 | +// the PyTorch operator library. The intention is to ultimately rewrite kernels to take the new signature |
| 11 | +// and then delete this file. This transition process can happen kernel-by-kernel, since this wrapper |
| 12 | +// is a no-op for kernels that already have a non-legacy signature. |
| 13 | + |
| 14 | +namespace c10 { |
| 15 | +namespace impl { |
| 16 | + |
| 17 | +inline c10::optional<MemoryFormat> process_memory_format(const TensorOptions& options, c10::optional<MemoryFormat> memory_format) { |
| 18 | + TORCH_CHECK( |
| 19 | + !(options.has_memory_format() && memory_format.has_value()), |
| 20 | + "Cannot set memory_format both in TensorOptions and explicit argument; please delete " |
| 21 | + "the redundant setter."); |
| 22 | + if (memory_format.has_value()) { |
| 23 | + return memory_format; |
| 24 | + } else { |
| 25 | + return options.memory_format_opt(); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +namespace detail { |
| 30 | + |
| 31 | +// with_scattered_tensor_options takes a function pointer that potentially takes a TensorOptions argument. |
| 32 | +// If it does, then it creates a new function pointer that takes scattered arguments, internally |
| 33 | +// gathers those arguments, and then calls the underlying function pointer. If the underlying |
| 34 | +// function pointer does not take a TensorOptions argument, it is passed through unmodified. |
| 35 | + |
| 36 | +template<class Type, class Enable = void> struct is_tensoroptions_arg : std::false_type {}; |
| 37 | +template<class Type> struct is_tensoroptions_arg<Type, std::enable_if_t<std::is_same<TensorOptions, std::decay_t<Type>>::value>> : std::true_type {}; |
| 38 | +template<class Type> |
| 39 | +using is_tensoroptions_arg_t = typename is_tensoroptions_arg<Type>::type; |
| 40 | + |
| 41 | +template<class FuncType> |
| 42 | +inline constexpr bool has_tensoroptions_arg() { |
| 43 | + using parameter_types = typename guts::infer_function_traits_t<FuncType>::parameter_types; |
| 44 | + constexpr size_t num_tensoroptions_args = guts::typelist::count_if<is_tensoroptions_arg_t, parameter_types>::value; |
| 45 | + static_assert(num_tensoroptions_args <= 1, "Function has multiple TensorOptions parameters. We support at most one."); |
| 46 | + return num_tensoroptions_args > 0; |
| 47 | +} |
| 48 | + |
| 49 | +// sanity checks |
| 50 | +static_assert(has_tensoroptions_arg<int (int64_t, const TensorOptions&)>(), ""); |
| 51 | +static_assert(has_tensoroptions_arg<int (int64_t, TensorOptions)>(), ""); |
| 52 | +static_assert(!has_tensoroptions_arg<int (int64_t, std::string)>(), ""); |
| 53 | + |
| 54 | +template<class FuncPtr, class ParametersBeforeTensorOptions, class ParametersAfterTensorOptions> struct with_scattered_tensor_options_; |
| 55 | + |
| 56 | +template<class FuncPtr, class Enable = void> |
| 57 | +struct with_scattered_tensor_options final {}; |
| 58 | + |
| 59 | +template<class UnderlyingFuncPtr> |
| 60 | +struct with_scattered_tensor_options<UnderlyingFuncPtr, std::enable_if_t<!has_tensoroptions_arg<typename UnderlyingFuncPtr::FuncType>()>> final { |
| 61 | + // FuncType does not have TensorOptions arguments. |
| 62 | + // Don't wrap anything but just return the base pointer. |
| 63 | + using FuncPtr = UnderlyingFuncPtr; |
| 64 | +}; |
| 65 | + |
| 66 | +template<class UnderlyingFuncPtr> |
| 67 | +struct with_scattered_tensor_options<UnderlyingFuncPtr, std::enable_if_t<has_tensoroptions_arg<typename UnderlyingFuncPtr::FuncType>()>> final { |
| 68 | +private: |
| 69 | + // FuncType has TensorOptions arguments. |
| 70 | + // Return a function pointer to a wrapper function that replaces those with expanded arguments. |
| 71 | + using gathered_parameter_types = typename guts::infer_function_traits_t<typename UnderlyingFuncPtr::FuncType>::parameter_types; |
| 72 | + static constexpr size_t tensoroptions_arg_index = |
| 73 | + guts::typelist::find_if< |
| 74 | + gathered_parameter_types, |
| 75 | + is_tensoroptions_arg_t |
| 76 | + >::value; |
| 77 | + |
| 78 | + using parameters_before_tensoroptions = |
| 79 | + guts::typelist::take_t<gathered_parameter_types, tensoroptions_arg_index>; |
| 80 | + using parameters_after_tensoroptions = |
| 81 | + guts::typelist::drop_t<gathered_parameter_types, tensoroptions_arg_index + 1>; |
| 82 | + |
| 83 | + using wrapper = with_scattered_tensor_options_<UnderlyingFuncPtr, parameters_before_tensoroptions, parameters_after_tensoroptions>; |
| 84 | +public: |
| 85 | + using FuncPtr = TORCH_FN_TYPE(&wrapper::wrapper); |
| 86 | +}; |
| 87 | + |
| 88 | +template<class FuncPtr, class... ParametersBeforeTensorOptions, class... ParametersAfterTensorOptions> |
| 89 | +struct with_scattered_tensor_options_<FuncPtr, guts::typelist::typelist<ParametersBeforeTensorOptions...>, guts::typelist::typelist<ParametersAfterTensorOptions...>> final { |
| 90 | + static decltype(auto) wrapper( |
| 91 | + ParametersBeforeTensorOptions... parameters_before, |
| 92 | + optional<ScalarType> scalar_type, |
| 93 | + optional<Layout> layout, |
| 94 | + optional<Device> device, |
| 95 | + optional<bool> pin_memory, |
| 96 | + ParametersAfterTensorOptions... parameters_after) { |
| 97 | + return (*FuncPtr::func_ptr())( |
| 98 | + std::forward<ParametersBeforeTensorOptions>(parameters_before)..., |
| 99 | + TensorOptions().dtype(scalar_type).device(device).layout(layout).pinned_memory(pin_memory), |
| 100 | + std::forward<ParametersAfterTensorOptions>(parameters_after)... |
| 101 | + ); |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +template<class FuncPtr> |
| 108 | +constexpr auto hacky_wrapper_for_legacy_signatures(FuncPtr) { |
| 109 | + return typename detail::with_scattered_tensor_options<FuncPtr>::FuncPtr(); |
| 110 | +}; |
| 111 | + |
| 112 | +} |
| 113 | +} |
0 commit comments