Does JIT makes model faster?

analvikingur: <p>Is there any JIT performance measurements? Does it makes a model any faster or the only benefit of involving JIT is ability to save model and perform inference in any other environment except python?</p>

tom: <p>Yes, we do monitor the performance of certain bits. For example the recent <a href="https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/" rel="nofollow noopener">PyTorch blog on RNN speedups</a> discusses benchmarks we’ve been monitoring quite closely and continue to work against. ResNet performance it also regularly checked.</p>
<p>That said, whether any given model sees significant speedups, depends.</p>
<ul>
<li>I always give the ballpark figure of 10% speedup for moving from Python to C++ - I got this number from a couple of specific models, e.g. when you do a “1-1” translation into C++ of the LLTM model used in the C+±Extension tutorial. Your model will see different numbers. A similar speedup probably is there for the JIT.</li>
<li>Where the JIT really get large speedups is when one of the optimizations can fully come into play. E.g. if you have chains of elementwise operations, they will be fused into a single kernel. As those are typically memory-bound, fusing two elementwise ops will be ~2x as fast as doing them separately.</li>
</ul>
<p>Best regards</p>
<p>Thomas</p>

sukuya: <p>I traced the <a href="https://huggingface.co/pytorch-transformers/examples.html" rel="nofollow noopener">BERT</a> model from HuggingFace PyTorchTransformers library and getting following results for 10 iterations.<br>
a) Using Python runtime for running the forward: 979292 µs</p>
<pre><code class="lang-auto">import time
model = torch.jit.load('models_backup/2_2.pt')
x = torch.randint(2000, (1, 14), dtype=torch.long, device='cpu')
start = time.time()
for i in range(10):
    model(x)
end = time.time()
print((end - start)*1000000, "µs")
</code></pre>
<p>b) Using C++ runtime for running the forward: 3333758 µs which is almost 3x of what Python</p>
<pre><code class="lang-auto">  torch::Tensor x = torch::randint(index_max, {1, inputsize}, torch::dtype(torch::kInt64).device(torch::kCPU));
  input.push_back(x);
  #endif
  // Execute the model and turn its output into a tensor.
  auto outputs = module-&gt;forward(input).toTuple();
  auto start = chrono::steady_clock::now();
  for (int16_t i = 0; i&lt;10; ++i)
  {
    outputs = module-&gt;forward(input).toTuple();
  }
  auto end = chrono::steady_clock::now();
  cout &lt;&lt; "Elapsed time in microseconds : " 
		&lt;&lt; chrono::duration_cast&lt;chrono::microseconds&gt;(end - start).count()
		&lt;&lt; " µs" &lt;&lt; endl;
</code></pre>
<p><a class="mention" href="/u/tom">@tom</a> any suggestions on what am I missing ?</p>

tom: <p>You are not even doing the comparison I had in mind. - If the C++/uses the JIT, you compare JIT called from Python vs JIT called from C++, and that should really have the same speed modulo constant overhead (which is not 6s).<br>
Are you using the same inputs, libtorch, environment,…?</p>
<p>Best regards</p>
<p>Thomas</p>

Edwardmark: <p>Hi, Tom<br>
Which layers can be seen as chains of elementwise ops? Any detailed benchmarks? Thank you very much in advance.</p>
<p>Best regards,</p>
<p>Edward</p>

tom: <p>I teach that in my PyTorch internals training, if you’re near Munich and want to book a seat… <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
But so the theory answer is any sequence of elementwise ops and the practical answer is anything that you see merged into fusion groups in <code>myfn.graph_for(*my_inputs) </code>. (Only is done on GPU by default.)<br>
In addition to the <a href="https://pytorch.org/blog/optimizing-cuda-rnn-with-torchscript/" rel="nofollow noopener">blog post linked above</a>, there is <a href="https://lernapparat.de/more-jit-optimizations/" rel="nofollow noopener">my blog post on optimizing LSTM backwards</a> and an old talk from me <a href="https://lernapparat.de/pytorch-jit-android/" rel="nofollow noopener">using this on a simple example IoU in detail</a>.<br>
Obviously a lot more is to be had from extending what can be fused <a href="https://github.com/pytorch/tvm/" rel="nofollow noopener">TorchTVM</a> is a (highly experimental) approach to hook into TVM which is a (also experimental in my experience) framework that can optimize also reductions, which the JIT cannot .<br>
Personally, I think a lot more could be had, but I’m not sure who makes that a top priority at the moment.</p>
<p>Best regards</p>
<p>Thomas</p>