Repository: pytorch
Issue: Fail to introduce torch::jit::Module as a parameter of a cusomized operator.
Labels: oncall: jit

## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->

## To Reproduce

Steps to reproduce the behavior:

1. Follow the tutorial here: https://pytorch.org/tutorials/advanced/torch_script_custom_ops.html to create a new operator dummy_ops() method. The code is simple like this:

                    torch::Tensor dummy_ops(torch::jit::Module my_module, torch::Tensor testData) {
                        return testData.clone();
                    }

2. Try to execute 'make -j' under the build folder.
3. The error below was thrown out:

In file included from /myrepos/pytorch/torch/include/ATen/core/ivalue.h:1161,
                 from /myrepos/pytorch/torch/include/ATen/record_function.h:3,
                 from /myrepos/pytorch/torch/include/ATen/Dispatch.h:5,
                 from /myrepos/pytorch/torch/include/ATen/ATen.h:13,
                 from /myrepos/pytorch/torch/include/torch/csrc/api/include/torch/types.h:3,
                 from /myrepos/pytorch/torch/include/torch/script.h:3,
                 from /myrepos/pytorch/test_ops/test_ops.cpp:2:
/myrepos/pytorch/torch/include/ATen/core/ivalue_inl.h: In instantiation of ‘T c10::generic_to(c10::IValue, c10::_fake_type<T>) [with T = torch::jit::Module]’:
/myrepos/pytorch/torch/include/ATen/core/ivalue_inl.h:1026:20:   required from ‘T c10::IValue::to() && [with T = torch::jit::Module]’
/myrepos/pytorch/torch/include/ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h:324:33:   required from ‘static decltype(auto) c10::impl::ivalue_to_arg<T, AllowDeprecatedTypes>::call(c10::IValue&) [with T = torch::jit::Module; bool AllowDeprecatedTypes = false]’
/myrepos/pytorch/torch/include/ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h:452:94:   required from ‘std::decay_t<typename c10::guts::infer_function_traits<Functor>::type::return_type> c10::impl::call_functor_with_args_from_stack_(c10::OperatorKernel*, c10::DispatchKeySet, c10::Stack*, std::index_sequence<INDEX ...>, c10::guts::typelist::typelist<Args ...>*) [with Functor = c10::impl::detail::WrapFunctionIntoRuntimeFunctor_<at::Tensor (*)(torch::jit::Module, at::Tensor), at::Tensor, c10::guts::typelist::typelist<torch::jit::Module, at::Tensor> >; bool AllowDeprecatedTypes = false; long unsigned int ...ivalue_arg_indices = {0, 1}; ArgTypes = {torch::jit::Module, at::Tensor}; std::decay_t<typename c10::guts::infer_function_traits<Functor>::type::return_type> = at::Tensor; c10::Stack = std::vector<c10::IValue>; std::index_sequence<INDEX ...> = std::integer_sequence<long unsigned int, 0, 1>]’
/myrepos/pytorch/torch/include/ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h:466:77:   required from ‘std::decay_t<typename c10::guts::infer_function_traits<Functor>::type::return_type> c10::impl::call_functor_with_args_from_stack(c10::OperatorKernel*, c10::DispatchKeySet, c10::Stack*) [with Functor = c10::impl::detail::WrapFunctionIntoRuntimeFunctor_<at::Tensor (*)(torch::jit::Module, at::Tensor), at::Tensor, c10::guts::typelist::typelist<torch::jit::Module, at::Tensor> >; bool AllowDeprecatedTypes = false; std::decay_t<typename c10::guts::infer_function_traits<Functor>::type::return_type> = at::Tensor; c10::Stack = std::vector<c10::IValue>]’
/myrepos/pytorch/torch/include/ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h:536:79:   required from ‘static void c10::impl::make_boxed_from_unboxed_functor<KernelFunctor, AllowDeprecatedTypes>::call(c10::OperatorKernel*, const c10::OperatorHandle&, c10::DispatchKeySet, c10::Stack*) [with KernelFunctor = c10::impl::detail::WrapFunctionIntoRuntimeFunctor_<at::Tensor (*)(torch::jit::Module, at::Tensor), at::Tensor, c10::guts::typelist::typelist<torch::jit::Module, at::Tensor> >; bool AllowDeprecatedTypes = false; c10::Stack = std::vector<c10::IValue>]’
/myrepos/pytorch/torch/include/ATen/core/boxing/KernelFunction_impl.h:136:9:   required from ‘static c10::KernelFunction c10::KernelFunction::makeFromUnboxedFunctor(std::unique_ptr<c10::OperatorKernel>) [with bool AllowLegacyTypes = false; KernelFunctor = c10::impl::detail::WrapFunctionIntoRuntimeFunctor_<at::Tensor (*)(torch::jit::Module, at::Tensor), at::Tensor, c10::guts::typelist::typelist<torch::jit::Module, at::Tensor> >]’
/myrepos/pytorch/torch/include/ATen/core/boxing/KernelFunction_impl.h:165:114:   required from ‘static c10::KernelFunction c10::KernelFunction::makeFromUnboxedRuntimeFunction(FuncType*) [with bool AllowLegacyTypes = false; FuncType = at::Tensor(torch::jit::Module, at::Tensor)]’
/myrepos/pytorch/torch/include/torch/library.h:108:64:   required from ‘torch::CppFunction::CppFunction(Func*, std::enable_if_t<c10::guts::is_function_type<FuncType_>::value, std::nullptr_t>) [with Func = at::Tensor(torch::jit::Module, at::Tensor); std::enable_if_t<c10::guts::is_function_type<FuncType_>::value, std::nullptr_t> = std::nullptr_t]’
/myrepos/pytorch/torch/include/torch/library.h:521:17:   required from ‘torch::Library& torch::Library::def(NameOrSchema&&, Func&&) & [with NameOrSchema = const char (&)[10]; Func = at::Tensor (&)(torch::jit::Module, at::Tensor)]’
/myrepos/pytorch/test_ops/test_ops.cpp:17:31:   required from here
/myrepos/pytorch/torch/include/ATen/core/ivalue_inl.h:899:9: error: no type named ‘element_type’ in ‘std::__remove_pointer_helper<torch::jit::Module, torch::jit::Module>::type’ {aka ‘struct torch::jit::Module’}
  899 |   using ElemType = typename std::remove_pointer<T>::type::element_type;
      |         ^~~~~~~~
/myrepos/pytorch/torch/include/ATen/core/ivalue_inl.h:900:52: error: no matching function for call to ‘c10::IValue::<expression error>()’
  900 |   return std::move(ivalue).toCustomClass<ElemType>();
      |                                                    ^
In file included from /myrepos/pytorch/torch/include/ATen/record_function.h:3,
                 from /myrepos/pytorch/torch/include/ATen/Dispatch.h:5,
                 from /myrepos/pytorch/torch/include/ATen/ATen.h:13,
                 from /myrepos/pytorch/torch/include/torch/csrc/api/include/torch/types.h:3,
                 from /myrepos/pytorch/torch/include/torch/script.h:3,
                 from /myrepos/pytorch/test_ops/test_ops.cpp:2:
/myrepos/pytorch/torch/include/ATen/core/ivalue.h:441:25: note: candidate: ‘template<class T> c10::intrusive_ptr<T> c10::IValue::toCustomClass() &&’
  441 |   c10::intrusive_ptr<T> toCustomClass() &&;
      |                         ^~~~~~~~~~~~~
/myrepos/pytorch/torch/include/ATen/core/ivalue.h:441:25: note:   template argument deduction/substitution failed:
/myrepos/pytorch/torch/include/ATen/core/ivalue.h:443:25: note: candidate: ‘template<class T> c10::intrusive_ptr<T> c10::IValue::toCustomClass() const &’
  443 |   c10::intrusive_ptr<T> toCustomClass() const&;
      |                         ^~~~~~~~~~~~~
/myrepos/pytorch/torch/include/ATen/core/ivalue.h:443:25: note:   template argument deduction/substitution failed:

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->

## Expected behavior

<!-- A clear and concise description of what you expected to happen. -->

## Environment

Please copy and paste the output from our
[environment collection script](https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py)
(or fill out the checklist below manually).

      PyTorch version: 1.9.0a0+git0eba63e
      Is debug build: True
      CUDA used to build PyTorch: None
      ROCM used to build PyTorch: N/A

      OS: Ubuntu 20.04.1 LTS (x86_64)
      GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
      Clang version: Could not collect
      CMake version: version 3.18.2

      Python version: 3.8 (64-bit runtime)
      Is CUDA available: False
      CUDA runtime version: No CUDA
      GPU models and configuration: No CUDA
      Nvidia driver version: No CUDA
      cuDNN version: No CUDA
      HIP runtime version: N/A
      MIOpen runtime version: N/A

## Additional context

<!-- Add any other context about the problem here. -->


cc @gmagogsfm