Transfer CUDA memory to libtorch tensor

Terbe_Daniel: <p>Hi there,</p>
<p>My preprocessing happens on CUDA memory (but not with libtorch). Currently, I transfer data to CPU in an OpenCV matrix and create a libtortch tensor from it, and finally, I transfer data back to GPU which is really inefficient. I am wondering whether it is possible to initialize libtorch tensor from CUDA memory. For example create an opencv GpuMat and glob from that.</p>

ptrblck: <p>I would think <code>from_blob</code> should work (and I think we had similar discussions here before, but I can’t find it quickly).</p>

AlexWilkinsonnn: <p>See this issue my dude:</p><aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/19786">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/19786" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/19786" target="_blank" rel="noopener nofollow ugc">Could Pytorch C++ API load tensor from gpu memory directly?</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2019-04-25" data-time="23:29:42" data-timezone="UTC">11:29PM - 25 Apr 19 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2019-04-26" data-time="08:45:23" data-timezone="UTC">08:45AM - 26 Apr 19 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/xin-xinhanggao" target="_blank" rel="noopener nofollow ugc">
          <img alt="xin-xinhanggao" src="https://avatars.githubusercontent.com/u/20569510?v=4" class="onebox-avatar-inline" width="20" height="20">
          xin-xinhanggao
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: cpp
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triaged
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">## ❓ Questions and Help
Sorry to post it here, I try to post it on Pytorch Foru<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">m but get no response.
I want to load OpenGL framebuffer object into pytorch C++ tensor.

In https://pytorch.org/cppdocs/notes/tensor_basics.html, I find that torch::from_blob can be used to view cuda memory as a tensor in Aten but seems can not work for me.

Could anyone can help, thanks!</span></p>
  </div>

  </article>

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

  <div style="clear: both"></div>
</aside>
<p>
Just be careful with the CUDA device. If you specify a device ID explicitly it needs to be the same as the one the data is on. If you want it on a different device on it needs to go back to CPU first.</p>