What is the forward function is doing at the beginning

JeremyGong: <p><strong>A. ENV</strong></p>
<pre><code>1. libtorch 1.8.1 CPU version
2. centos 7
3. Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
4. gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)
5. x86_64 4 CPU 
</code></pre>
<p><strong>B. TEST CODE</strong></p>
<pre><code>torch::jit::script::Module module;
module = torch::jit::load(argv[1]);
std::vectortorch::jit::IValue inputs;
at::Tensor input = torch::ones({1, 300, 40});
inputs.push_back(input);

torch::NoGradGuard guard;
at::set_num_interop_threads(1);
at::set_num_threads(1);

// record profile
torch::autograd::profiler::RecordProfile perf_guard(argv[2]);

for (int i=0; i &lt; num; i++) {
    b = time_get_ms();
    std::cout &lt;&lt; "module forward begin" &lt;&lt; std::endl;
    at::Tensor output = module.forward(inputs).toTensor();
    e = time_get_ms();
    d = e - b;
    std::cout &lt;&lt; "forward output size : " &lt;&lt; output.numel() &lt;&lt; ", No." &lt;&lt; i &lt;&lt;" forward time : " &lt;&lt; d &lt;&lt; std::endl;
    total_duration = total_duration + d;
}

avg_duration = total_duration / num ;
std::cout &lt;&lt; "AVG forward time : " &lt;&lt; avg_duration &lt;&lt; std::endl;
</code></pre>
<p><strong>C. LATENCY</strong></p>
<p>See the comments for specific data .</p>
<ol>
<li>No.0 forward duration : 188.196 ms;</li>
<li>No.1 forward duration : 106.588 ms;</li>
<li>No.2 ~ No.9 forward duration : about 70 ms；</li>
</ol>
<p><strong>D. PERFORMANCE</strong></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/0/303fe6ff5781af32458c32b82662881c7834513a.png" data-download-href="https://discuss.pytorch.org/uploads/default/303fe6ff5781af32458c32b82662881c7834513a" title="g_perf"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/0/303fe6ff5781af32458c32b82662881c7834513a_2_690x192.png" alt="g_perf" data-base62-sha1="6SPPnwr6GcwFVaB0lInCdMDNzHs" width="690" height="192" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/0/303fe6ff5781af32458c32b82662881c7834513a_2_690x192.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/0/303fe6ff5781af32458c32b82662881c7834513a_2_1035x288.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/0/303fe6ff5781af32458c32b82662881c7834513a_2_1380x384.png 2x" data-dominant-color="F0E6F2"><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">g_perf</span><span class="informations">1912×534 39.8 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>
<p><strong>E. QUESTION</strong></p>
<ol>
<li>what is the ‘module.forward’ function is doing at the beginning ?</li>
<li>I guess when run ‘module.forward’ function in the first time, the ‘module.forward’ function may do something about warming up or else ?</li>
<li>If preheating is needed during the first ‘module.forward’ running? Why does the function spend so much time at the beginning of the second run? But in the subsequent run, it did not take the same time ？</li>
</ol>

JeremyGong: <p>Time of each run</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/5/15d3925259bf8f93df7c4a4b52e2f643ceb61a93.png" alt="good_perf" data-base62-sha1="375jE4kvDE7P3Iq6vMYLCXUlOKL" width="556" height="397"></p>

ptrblck: <p>The JIT is optimizing the model and its graph in the first iterations, which would add some overhead to them. Generally, the first iteration would also see an overhead from the cuda memory allocation (which would then be cached) etc.</p>

JeremyGong: <aside class="quote no-group" data-username="ptrblck" data-post="3" data-topic="118163">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>overhead</p>
</blockquote>
</aside>
<p>Thank you very much, but I still have two question ：</p>
<ol>
<li>I use the libtorch  with the option ‘USE_CUDA = OFF’. I don’t know whether the libtorch will allocate cuda memory or not ?</li>
<li>If JIT is optimizing the model and its graph in the first iterations , Why the libtorch take some time at the beginning in the second iterations ?  you can see the consume time in the performance analysis chart .</li>
</ol>
<p>Have a good day, thanks.</p>

ptrblck: <ol>
<li>
<p>I would assume that the GPU is not usable, if CUDA is deactivated.</p>
</li>
<li>
<p>I think the first 3(?) iterations are used for the optimizations.</p>
</li>
</ol>

JeremyGong: <aside class="quote no-group" data-username="ptrblck" data-post="5" data-topic="118163">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<ol>
<li>I think the first 3(?) iterations are used for the optimizations.</li>
</ol>
</blockquote>
</aside>
<ol>
<li>
<p>As the performance analysis chart show, the third iterations are used for the optimizations . I am confused about this phenomenon, what are the second iterations doing at the beginning ?</p>
</li>
<li>
<p>Generally speaking, the first iterations will take some time to do the optimizations, and then there is no need to do the same thing later, except for other optimized contents. I don’t know if this assumption is correct ?</p>
</li>
<li>
<p>I did an experiment a moment ago, I run the code ‘torch::jit::GraphOptimizerEnabledGuard _optimize_guard(true);’ before the ‘module.forward’ function. I want to make the module do the optimizations actively, But It didn’t seem to work.</p>
</li>
</ol>

ptrblck: <p>We would have to wait for a dev working on the JIT as they know in detail which optimizations are performed. However, to the best of my knowledge, multiple passes are needed for different stages of optimizations so a single forward pass won’t be enough.</p>

JeremyGong: <p>Thank u, I have done an experiment again. I set the parameter of the GraphOptimizerEnabledGuard to false instead of true. and it works.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/0/4099fc20a57d83aace7f5042a739555c5ff99104.png" alt="image" data-base62-sha1="9dut5weA8x6aL8tEjN0x75CewoA" width="562" height="402"></p>
<p>code ：</p>
<pre><code>torch::jit::GraphOptimizerEnabledGuard optimize_guard(false);
</code></pre>
<p>I’m looking forward to getting the details of the optimization .</p>