1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
template <typename Name, typename Func> Library& impl( Name name, Func&& raw_f, _RegisterOrVerify rv = _RegisterOrVerify::REGISTER) & { CppFunction f(std::forward<Func>(raw_f)); return _impl(name, std::move(f), rv); }
template <class T> struct is_compile_time_function_pointer : std::false_type {}; template <class FuncType, FuncType* func_ptr> struct is_compile_time_function_pointer< CompileTimeFunctionPointer<FuncType, func_ptr>> : std::true_type {};
template <typename FuncPtr> explicit CppFunction( FuncPtr f, std::enable_if_t< c10::is_compile_time_function_pointer<FuncPtr>::value, std::nullptr_t> = nullptr) : func_(c10::KernelFunction::makeFromUnboxedFunction(f)), cpp_signature_( c10::impl::CppSignature::make<typename FuncPtr::FuncType>()), schema_(c10::detail::inferFunctionSchemaFromFunctor< typename FuncPtr::FuncType>()), debug_() {}
template<class FuncPtr, bool AllowLegacyTypes = false> inline KernelFunction KernelFunction::makeFromUnboxedFunction(FuncPtr func_ptr) { (void)func_ptr; return makeFromUnboxedFunctor<AllowLegacyTypes, typename impl::WrapFunctionIntoFunctor<FuncPtr>::type>( guts::make_unique_base<OperatorKernel, typename impl::WrapFunctionIntoFunctor<FuncPtr>::type>() ); }
template<class FuncPtr> struct WrapFunctionIntoFunctor final { using type = detail::WrapFunctionIntoFunctor_< FuncPtr, typename guts::function_traits<typename FuncPtr::FuncType>::return_type, typename guts::function_traits<typename FuncPtr::FuncType>::parameter_types >; };
template <class Func> struct function_traits { static_assert( !std::is_same<Func, Func>::value, "In function_traits<Func>, Func must be a plain function type."); };
template <class Result, class... Args> struct function_traits<Result(Args...)> { using func_type = Result(Args...); using return_type = Result; using parameter_types = typelist::typelist<Args...>; static constexpr auto number_of_parameters = sizeof...(Args); };
template<class FuncPtr, class ReturnType, class ParameterList> class WrapFunctionIntoFunctor_ {};
template<class FuncPtr, class ReturnType, class... Parameters> class WrapFunctionIntoFunctor_<FuncPtr, ReturnType, guts::typelist::typelist<Parameters...>> final : public c10::OperatorKernel { public: C10_ALWAYS_INLINE decltype(auto) operator()(Parameters... args) { return (*FuncPtr::func_ptr())(std::forward<Parameters>(args)...); } };
template <typename Base, typename Child, typename... Args> typename std::enable_if< !std::is_array<Base>::value && !std::is_array<Child>::value && std::is_base_of<Base, Child>::value, std::unique_ptr<Base>>::type make_unique_base(Args&&... args) { return std::unique_ptr<Base>(new Child(std::forward<Args>(args)...)); }
template<bool AllowLegacyTypes, class KernelFunctor> inline KernelFunction KernelFunction::makeFromUnboxedFunctor(std::unique_ptr<OperatorKernel> kernelFunctor) { auto* unboxed_fn = &impl::wrap_kernel_functor_unboxed<KernelFunctor>::call; void* void_unboxed_fn = reinterpret_cast<void*>(unboxed_fn); bool is_symint = fn_has_symint<decltype(unboxed_fn)>::value; return KernelFunction( std::move(kernelFunctor), &impl::make_boxed_from_unboxed_functor<KernelFunctor, AllowLegacyTypes>::call, is_symint ? nullptr : void_unboxed_fn, is_symint ? void_unboxed_fn : nullptr ); }
template<class KernelFunctor> using wrap_kernel_functor_unboxed = wrap_kernel_functor_unboxed_<KernelFunctor, typename guts::infer_function_traits_t<KernelFunctor>::func_type>;
template <typename T> using infer_function_traits_t = typename infer_function_traits<T>::type;
template <typename Functor> struct infer_function_traits { using type = function_traits< c10::guts::detail::strip_class_t<decltype(&Functor::operator())>>; };
template <typename Result, typename... Args> struct infer_function_traits<Result (*)(Args...)> { using type = function_traits<Result(Args...)>; };
template <typename Result, typename... Args> struct infer_function_traits<Result(Args...)> { using type = function_traits<Result(Args...)>; };
template<class KernelFunctor, class OpSignature> struct wrap_kernel_functor_unboxed_ final {};
template<class KernelFunctor, class ReturnType, class... ParameterTypes> struct wrap_kernel_functor_unboxed_<KernelFunctor, ReturnType(ParameterTypes...)> final { static ReturnType call(OperatorKernel* functor, DispatchKeySet, ParameterTypes... args) { KernelFunctor* functor_ = static_cast<KernelFunctor*>(functor); return (*functor_)(std::forward<ParameterTypes>(args)...); } };
template<class KernelFunctor, bool AllowDeprecatedTypes> struct make_boxed_from_unboxed_functor final { static void call(OperatorKernel* functor, const OperatorHandle&, DispatchKeySet dispatchKeySet, Stack* stack) { using ReturnType = typename guts::infer_function_traits_t<KernelFunctor>::return_type; using ArgTypes = typename c10::remove_DispatchKeySet_arg_from_func<KernelFunctor>::parameter_types; constexpr bool has_outputs = !std::is_same<void, ReturnType>::value; constexpr size_t num_inputs = guts::typelist::size<ArgTypes>::value; if constexpr (has_outputs) { using ReturnType_ = ::std::decay_t<ReturnType>; ReturnType_ output = call_functor_with_args_from_stack<KernelFunctor, AllowDeprecatedTypes>(functor, dispatchKeySet, stack); torch::jit::drop(*stack, num_inputs); push_outputs<ReturnType_, AllowDeprecatedTypes>::call(::std::move(output), stack); } else { call_functor_with_args_from_stack<KernelFunctor, AllowDeprecatedTypes>(functor, dispatchKeySet, stack); torch::jit::drop(*stack, num_inputs); } } };
template<class Functor, bool AllowDeprecatedTypes, size_t... ivalue_arg_indices, typename... ArgTypes> std::decay_t<typename guts::infer_function_traits_t<Functor>::return_type> call_functor_with_args_from_stack_(OperatorKernel* functor, DispatchKeySet dispatchKeySet, Stack* stack, std::index_sequence<ivalue_arg_indices...>, guts::typelist::typelist<ArgTypes...>*) { (void)(stack); return wrap_kernel_functor_unboxed<Functor>::call(functor, dispatchKeySet, ivalue_to_arg<typename decay_if_not_tensor<ArgTypes>::type, AllowDeprecatedTypes>::call( torch::jit::peek(*stack, ivalue_arg_indices, sizeof...(ivalue_arg_indices)) )...); }
template<class Functor, bool AllowDeprecatedTypes> std::decay_t<typename guts::infer_function_traits_t<Functor>::return_type> call_functor_with_args_from_stack(OperatorKernel* functor, DispatchKeySet dispatchKeySet, Stack* stack) { using ArgTypes = typename c10::remove_DispatchKeySet_arg_from_func<Functor>::parameter_types; constexpr size_t num_ivalue_args = guts::typelist::size<ArgTypes>::value; return call_functor_with_args_from_stack_<Functor, AllowDeprecatedTypes>(functor, dispatchKeySet, stack, std::make_index_sequence<num_ivalue_args>(), static_cast<ArgTypes*>(nullptr)); }
template <class FuncType> using remove_DispatchKeySet_arg_from_func = guts::make_function_traits_t< typename guts::infer_function_traits_t<FuncType>::return_type, typename std::conditional_t< std::is_same< DispatchKeySet, typename guts::typelist::head_with_default_t< void, typename guts::infer_function_traits_t< FuncType>::parameter_types>>::value, guts::typelist::drop_if_nonempty_t< typename guts::infer_function_traits_t<FuncType>::parameter_types, 1>, typename guts::infer_function_traits_t<FuncType>::parameter_types>>;
inline KernelFunction::KernelFunction(std::unique_ptr<OperatorKernel> functor, InternalBoxedKernelFunction* boxed_kernel_func, void* unboxed_kernel_func, void* sym_unboxed_kernel_func = nullptr) : boxed_kernel_func_(std::move(functor), boxed_kernel_func) , unboxed_kernel_func_(unboxed_kernel_func) , sym_unboxed_kernel_func_(sym_unboxed_kernel_func) {}
inline BoxedKernel::BoxedKernel(std::unique_ptr<OperatorKernel> functor, InternalBoxedKernelFunction* boxed_kernel_func) : functor_(std::move(functor)) , boxed_kernel_func_(boxed_kernel_func) {}
template <typename Func> Library& fallback(Func&& raw_f) & { CppFunction f((std::forward<Func>(raw_f))); return _fallback(std::move(f)); }
Library& Library::_fallback(CppFunction&& f) & { auto dispatch_key = f.dispatch_key_.has_value() ? f.dispatch_key_ : dispatch_key_; for (auto k : c10::getRuntimeDispatchKeySet(*dispatch_key)) { auto idx = getDispatchTableIndexForDispatchKey(k); if (idx < 0) continue; registrars_.emplace_back( c10::Dispatcher::singleton().registerFallback( k, std::move(f.func_), debugString(std::move(f.debug_), file_, line_) ) ); } return *this; }
Library& Library::_impl(const char* name_str, CppFunction&& f, _RegisterOrVerify rv) & { at::OperatorName name = _parseNameForLib(name_str); auto dispatch_key = f.dispatch_key_.has_value() ? f.dispatch_key_ : dispatch_key_; switch (rv) { case _RegisterOrVerify::REGISTER: registrars_.emplace_back( c10::Dispatcher::singleton().registerImpl( std::move(name), dispatch_key, std::move(f.func_), std::move(f.cpp_signature_), std::move(f.schema_), debugString(std::move(f.debug_), file_, line_) ) ); break; case _RegisterOrVerify::VERIFY: c10::Dispatcher::singleton().waitForImpl(name, dispatch_key); break; } return *this;
RegistrationHandleRAII Dispatcher::registerImpl( OperatorName op_name, c10::optional<DispatchKey> dispatch_key, KernelFunction kernel, c10::optional<impl::CppSignature> cpp_signature, std::unique_ptr<FunctionSchema> inferred_function_schema, std::string debug ) { std::lock_guard<std::mutex> lock(mutex_);
auto op = findOrRegisterName_(op_name);
auto handle = op.operatorDef_->op.registerKernel( *this, dispatch_key, std::move(kernel), std::move(cpp_signature), std::move(inferred_function_schema), std::move(debug) );
++op.operatorDef_->def_and_impl_count;
cond_var_.notify_all();
return RegistrationHandleRAII([this, op, op_name, dispatch_key, handle] { deregisterImpl_(op, op_name, dispatch_key, handle); }); }
|