Trace modules in C++ not in pytorch

joyplenet: <p>hi, I want to train/test and generate jit::script structure totally in C++ code.<br>
simply, I want to trace modules implemented in totally libtorch C++.<br>
Always torch provides save script in python and load script in C++. But, I want to use script in C++ directly from modules same namespace C++. is it possible?</p>
<p>I declared a class for all definitions of Network, and composed a register module and each forward function. Both training and testing work fine.<br>
I would like to convert it into a script form made directly from this class Model, not a .pt file made from Pytorch.<br>
In Python, I can pass the network through the trace function to figure out the structure, but libtorch c++ doesn’t seem to provide such a function.</p>
<p>The Model class I use can be accessed through a function like Model-&gt;named_modules().<br>
However, I couldn’t find a function like ‘trace function’ or a way to convert it to torch::jit:script:Module form.</p>
<p>Any example code that might help?</p>

dfalbel: <aside class="quote no-group" data-username="joyplenet" data-post="1" data-topic="183463">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/joyplenet/48/61886_2.png" class="avatar"> joyplenet:</div>
<blockquote>
<p>In Python, I can pass the network through the trace function to figure out the structure, but libtorch c++ doesn’t seem to provide such a function.</p>
</blockquote>
</aside>
<p>You can do it with <code>torch::jit::tracer::trace</code>.<br>
I don’t have a clean example but here’s an overview of what we do in the R bindings:</p>
<pre><code class="lang-auto">Stack inputs_ = *reinterpret_cast&lt;Stack*&gt;(inputs);
auto module_ = reinterpret_cast&lt;torch::jit::script::Module*&gt;(module);
auto name_ = from_raw::string(name);

std::function&lt;std::string(const torch::autograd::Variable&amp;)&gt; var_fn =
      [](const torch::autograd::Variable&amp; x) { return ""; };

  auto traced =
      torch::jit::tracer::trace(inputs_, fn_, var_fn, strict, false, module_);
</code></pre>

TiStar: <p>Could you give an example in more detail? I’m facing the same problem and can’t get it to work.</p>

SeaShellAtSeaSide: <aside class="quote no-group" data-username="dfalbel" data-post="2" data-topic="183463">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dfalbel/48/8880_2.png" class="avatar"> dfalbel:</div>
<blockquote>
<p><code>torch::jit::tracer::trace</code></p>
</blockquote>
</aside>
<p>On what header file is <code>torch::jit::tracer::trace</code> located?</p>

dfalbel: <p>These are the relevant imports:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/mlverse/torch/blob/ab71bda8de0507770b04835c5c4838e91f3c6261/src/lantern/src/Trace.cpp#L2-L4">
  <header class="source">

      <a href="https://github.com/mlverse/torch/blob/ab71bda8de0507770b04835c5c4838e91f3c6261/src/lantern/src/Trace.cpp#L2-L4" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/mlverse/torch/blob/ab71bda8de0507770b04835c5c4838e91f3c6261/src/lantern/src/Trace.cpp#L2-L4" target="_blank" rel="noopener nofollow ugc">mlverse/torch/blob/ab71bda8de0507770b04835c5c4838e91f3c6261/src/lantern/src/Trace.cpp#L2-L4</a></h4>



    <pre class="onebox"><code class="lang-cpp">
      <ol class="start lines" start="2" style="counter-reset: li-counter 1 ;">
          <li>#include &lt;torch/csrc/jit/frontend/tracer.h&gt;</li>
          <li>#include &lt;torch/script.h&gt;  // One-stop header.</li>
          <li>#include &lt;torch/torch.h&gt;</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>