torch.Tensor.cuda

patfla: <p>I converted a <a href="http://colab.research.google.com" rel="noopener nofollow ugc">colab.research.google.com</a> Jupyter notebook to run on my local machine.  Unsurprisingly it takes much longer.  However, when cProfiling the python code I find that 40% of that time is taken in torch.Tensor.cuda() - which is basically copying your objects from RAM to memory on the GPU.  Any way of speeding this up short of “buy a new faster machine”?  Buy a new faster machine that is, with a different and faster interconnect between RAM and the GPU.  Discrete (I assume) GPU card - GeForce GTX 1660.  New machine - haven’t opened it up and looked around much.</p>
<p>Interestingly, I have a routine that measures TFOPS and the 1660 is faster by some 1/3 than whatever the default GPU is that I’m getting in an unpaid Colab acct.  Wonder if paying the $9.99/mth results in faster GPUs or interconnects?  Although of course that’s not where my current bottleneck is.  The two training steps of the code in question take 1.5 hrs on Colab - and much longer locally.</p>

ptrblck: <p>I’m not sure how you are profiling your code, but note that CUDA operations are executed asynchronously, so you would need to synchronize the code via <code>torch.cuda.synchronize()</code> before starting and stopping the timer.<br>
If your device is busy with other workload (e.g. the model training), the tensor copy operation might accumulate the previous timings, and your profiling would be invalid.</p>

patfla: <p>Thanks for responding.</p>
<p>I’ve modified (hacked) the following so that it will run locally:</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://colab.research.google.com/drive/1mlOPvPd59HkB0_APsbzoFrPtpu5Rtzyx">
  <header class="source">
      <img src="https://ssl.gstatic.com/colaboratory-static/common/2580b6a0a5b3a23f1efbba8a2eebb5a3/img/favicon.ico" class="site-icon" width="128" height="128">

      <a href="https://colab.research.google.com/drive/1mlOPvPd59HkB0_APsbzoFrPtpu5Rtzyx" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>

  <article class="onebox-body">
    <img width="260" height="260" src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar">

<h3><a href="https://colab.research.google.com/drive/1mlOPvPd59HkB0_APsbzoFrPtpu5Rtzyx" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>

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

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

<p>If need be, I could post my version somewhere.</p>
<p>Then I run locally with:</p>
<p>ipython -m cProfile FlowerML-2.py</p>
<p>As I was googling on the problem I noticed your id reply to someone somewhere to the effect that asynchronous operations were being added to pytorch (probably long ago) but that the person asking would need to build pytorch for him or herself.</p>
<p>… ah, found it (your earlier conversation):</p>
<aside class="quote" data-post="1" data-topic="20100">
  <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/nlgranger/48/3607_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/does-torch-tensor-to-supports-async-gpu-transfer-solved-yes/20100">Does torch.Tensor.to supports async GPU transfer? [SOLVED: yes]</a> 
  </div>
  <blockquote>
    It all in the title: 
torch.Tensor.cuda has an async parameter that allows asynchronous transfer from pinned memory to GPU. Does torch.Tensor.to automatically uses async when possible? 
I wasn’t able to find the relevant piece of source code.
  </blockquote>
</aside>

<p>June 2018.</p>
<p>I’m a little confused - immediately after the call to .cuda() the same objects are operated upon, presumably on the GPU and happening under-the-hood within pytorch, and so I was assuming this would need to happen synchronously.</p>
<p>Not all of which (the above) necessarily addresses how you’ve asked your question.  I’m not at all new to programming but relatively new to ML and pytorch.</p>

Arun_Kumar4: <p>using this code torch.cuda.synchronize  how can we reduce time</p>

ptrblck: <p>You cannot reduce time using <code>torch.cuda.syncrhonize()</code>, as it’s used to synchronize the code in order to get valid profiling times. As described before, CUDA operations are executed asynchronously, so profiling them without a synchronization yields invalid results.</p>

Arun_Kumar4: <p>give any tips for time reduction techniques in pytorch</p>

ptrblck: <p>Profile the code properly, narrow down the bottlenecks, and check which operations are slow first.<br>
There is no silver bullet for “time reduction” without knowing which part is slow.</p>

Arun_Kumar4: <p>i used cprofile and bottlenecks both are showed {method cpu tensor_cobjects} take more time}</p>