Custom_class nn::module issue

pya: <p>Inspired by the example at <a href="https://docs.pytorch.org/tutorials/advanced/custom_class_pt2.html" class="inline-onebox" rel="noopener nofollow ugc">Supporting Custom C++ Classes in torch.compile/torch.export — PyTorch Tutorials 2.8.0+cu128 documentation</a> , I tried to do the following:</p>
<pre><code class="lang-auto">struct NetImpl : nn::Module {
  NetImpl(int64_t in_sz, int64_t out_sz) :
    lin1(nn::LinearOptions(in_sz, out_sz))
  {
    register_module("lin1", lin1);
  }

  torch::Tensor forward(torch::Tensor x) {
    return lin1(x);
  }
  private:
    nn::Linear lin1;
};

TORCH_MODULE(Net);

struct NetTypeCustomClass : torch::CustomClassHolder {

  NetTypeCustomClass(int64_t in_sz, int64_t out_sz) : _net(Net(in_sz, out_sz)){
  }

  Net&amp; mod() { return _net; }

  private:
    Net _net = nullptr;
};

TORCH_LIBRARY(MyCustomLib, m) {
  auto mc_ = m.class_&lt;NetTypeCustomClass&gt;(TORCH_SELECTIVE_CLASS("NetTypeCustomClass"))
    .def(torch::init&lt;int64_t, int64_t&gt;())
    .def("mod", &amp;NetTypeCustomClass::mod);

}
</code></pre>
<p>When I try to compile it, it produces the following error:</p>
<pre><code class="lang-auto">./.venv/lib/python3.13/site-packages/torch/include/ATen/core/ivalue_inl.h: In instantiation of ‘c10::IValue c10::ivalue::detail:from_(T&amp;&amp;, std::false_type) [with T = Net; std::false_type = std::integral_constant&lt;bool, false&gt;]’:
./.venv/lib/python3.13/site-packages/torch/include/ATen/core/ivalue_inl.h:2503:23:   required from ‘c10::IValue c10::ivalue::fro(T&amp;&amp;) [with T = Net]’
./.venv/lib/python3.13/site-packages/torch/include/torch/custom_class_detail.h:181:41:   required from ‘void torch::detail::BoxedProxy&lt;RetType, Func&gt;::operator()(torch::jit::Stack&amp;, Func&amp;) [with RetType = Net&amp;; Func = torch::detail::WrapMethod&lt;Net&amp; (NetTypeCustomClass::*)()&gt;; torch::jit::Stack = std::vector&lt;c10::IValue&gt;]’
./.venv/lib/python3.13/site-packages/torch/include/torch/custom_class.h:410:42:   required from ‘torch::jit::Function* torch::class_&lt;CurClass&gt;::defineMethod(std::string, Func, std::string, std::initializer_list&lt;torch::arg&gt;) [with Func = torch::detail::WrapMethod&lt;Net&amp; (NetTypeCustomClass::*)()&gt;; CurClass = NetTypeCustomClass; std::string = std::__cxx11::basic_string&lt;char&gt;]’
./.venv/lib/python3.13/site-packages/torch/include/torch/custom_class.h:161:17:   required from ‘torch::class_&lt;CurClass&gt;&amp; torch::class_&lt;CurClass&gt;::def(std::string, Func, std::string, std::initializer_list&lt;torch::arg&gt;) [with Func = Net&amp; (NetTypeCustomClass::*)(); CurClass = NetTypeCustomClass; std::string = std::__cxx11::basic_string&lt;char&gt;]’
dcgan_generator.cpp:325:9:   required from here
./.venv/lib/python3.13/site-packages/torch/include/ATen/core/ivalue_inl.h:2495:25: error: static assertion failed: You are calling from with a type that it doesn't support, and isn't a potential custom class (ie: is an intrusive_ptr)
 2495 |       guts::false_t&lt;T&gt;::value,
      |                         ^~~~~
./.venv/lib/python3.13/site-packages/torch/include/ATen/core/ivalue_inl.h:2495:25: note: ‘std::integral_constant&lt;bool, false&gt;::value’ evaluates to false
</code></pre>
<p>I understand that nn::Module derives from ModuleHolder which is a wrapper around std::shared_ptr. It seems you don’t want to mix std::shared_ptr and intrusive_ptr, though both use reference counting. I am trying to understand the reason behind this choice.</p>
<p>Is there no way to create a custom_class that wraps a nn:Module so that it can be accessed in Python (other than using Pybind11)?</p>