Can't forward on torch::nn::Sequential

username: Fitanium
cooked: <p>Hello there, I am using LibTorch 2.1.0 on Windows.</p>
<p>And I use <code>torch::nn::Sequential</code> build a network and implement <code>forward</code> method. Then I compile code and run, and see the message that the application crashed. Do I miss something? I wonder why and how to solve it. Thanks in advance!</p>
<p>this the net:</p>
<pre data-code-wrap="C++"><code class="lang-C++">class Net : public torch::nn::Module
{
private:
    torch::nn::Sequential layers{nullptr};

public:
    Net()
    {
        layers = torch::nn::Sequential();
        layers-&gt;push_back(torch::nn::Linear(1, 4));
        layers-&gt;push_back(torch::nn::Linear(4, 1));
    }

    at::Tensor forward(at::Tensor x)
    {
        std::cout &lt;&lt; "CHECK ... 000" &lt;&lt; std::endl;
        x = layers-&gt;forward(x);
        std::cout &lt;&lt; "CHECK ... 001" &lt;&lt; std::endl;
        return x;
    }
};
</code></pre>
<p>this is the error message:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/3/83dff1b62bc0d89092d70f0f6f17c74c0f898bf4.png" data-download-href="https://discuss.pytorch.org/uploads/default/83dff1b62bc0d89092d70f0f6f17c74c0f898bf4" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/8/3/83dff1b62bc0d89092d70f0f6f17c74c0f898bf4.png" alt="image" data-base62-sha1="iOClvem8xyP5ZGuM33Ffr6r3Byc" width="690" height="78" data-dominant-color="2E3536"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1874×214 10 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Shivang_Patel1
cooked: <p>You need to register your submodules (Linear Layers in your case). Refer to this for more details <a href="https://pytorch.org/tutorials/advanced/cpp_frontend.html" class="inline-onebox" rel="noopener nofollow ugc">Using the PyTorch C++ Frontend — PyTorch Tutorials 2.1.0+cu121 documentation</a></p>

username: Fitanium
cooked: <p>Thanks for reply. But I use <code>torch::nn::Sequential</code> to hold submodule layers. and I find some implementations in class <code>SequentialImpl</code>:<br>
In mycode, I call <code>push_back</code> to add submodule,</p>
<pre data-code-wrap="C++"><code class="lang-plaintext">layers-&gt;push_back(module);
</code></pre>
<p>the difinition of this function is:</p>
<pre data-code-wrap="C++"><code class="lang-plaintext">/// Adds a type-erased `AnyModule` to the `Sequential`.
void push_back(AnyModule any_module) {
  push_back(c10::to_string(modules_.size()), std::move(any_module));
}
</code></pre>
<p>this will call the following function:</p>
<pre data-code-wrap="C++"><code class="lang-plaintext">void push_back(std::string name, AnyModule any_module) {
  modules_.push_back(std::move(any_module));
  const auto index = modules_.size() - 1;
  register_module(std::move(name), modules_[index].ptr());
}
</code></pre>
<p>so, I call <code>push_back</code> which means that submodule has been registered. but cann’t forward.</p>

username: Fitanium
cooked: <p>It is seems that we can’t define a variable with type <code>torch::nn::Sequential</code> and call <code>forward</code> in a class, which inherits <code>torch::nn::Module</code> class. I am not sure about this, but I move <code>torch::nn::Sequential</code> outside and build a network directly, it works and no error.</p>

username: Fitanium
cooked: <p>For this problem, I forgot to call <code>register_module</code> to register this <code>torch::nn::Sequential</code> variable, which I should have done.</p>
<p>Sorry if anyone was misled.</p>