Can I initialize tensor from std::vector in libtorch?

username: crcrpar
cooked: <p>Hi,</p>
<p>I wonder if I can initialize torch::Tensor from std::vector like this</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;vector&gt;

int main()
{
  std::vector&lt;T&gt; initializer;
  ...
  torch::Tensor tensor = torch::from_blob(initializer);
}
</code></pre>

username: tom
cooked: <p>I used <code>torch::tensor(ArrayRef&lt;float&gt;)</code> successfully.<br>
I’m not 100% certain whether that copies already. You might have to take care of ownership or clone the output while the array ref is still alive, I think you have to do that when you use form_blob, too.</p>
<p>Best regards</p>
<p>Thomas</p>

username: crcrpar
cooked: <p>Thank you Tomas!</p>
<p>I’ll try that.</p>

username: goldsborough
cooked: <p><code>torch::tensor</code> makes a copy, <code>from_blob</code> does not (but <code>torch::from_blob(vector).clone()</code> does)</p>

username: leimao
cooked: <p>Could you please point me to the documentation for the <code>torch::tensor</code> API? Thank you.</p>

username: glaringlee
cooked: <p><a class="mention" href="/u/leimao">@leimao</a><br>
What do you want to achieve?</p>
<p>If you need examples on how to create tensor, here，there are examples in the tests:<br>
‘<a href="https://github.com/pytorch/pytorch/blob/455e85a2f181a66e7505d0e7eeb0ad0825bc4a41/aten/src/ATen/test/basic.cpp" rel="noopener nofollow ugc">https://github.com/pytorch/pytorch/blob/455e85a2f181a66e7505d0e7eeb0ad0825bc4a41/aten/src/ATen/test/basic.cpp</a>’</p>
<p>C++ doc is here:<br>
‘<a href="https://pytorch.org/cppdocs/api/classat_1_1_tensor.html?highlight=tensor" rel="noopener nofollow ugc">https://pytorch.org/cppdocs/api/classat_1_1_tensor.html?highlight=tensor</a>’</p>

username: leimao
cooked: <p>Thank you <a class="mention" href="/u/glaringlee">@glaringlee</a>. I want to create a <code>torch::Tensor</code> on device from (preferably const) <code>std::vector</code>.<br>
I got around by calling <code>torch::from_blob</code> and <code>.clone()</code>. Not sure if there is any more convenient way to do so.</p>
<p>The link you provided to GitHub is expired.<br>
The C++ doc describes the usage of <code>torch::Tensor</code> class instead of the <code>torch::tensor</code> function.</p>

username: glaringlee
cooked: <p><a class="mention" href="/u/leimao">@leimao</a><br>
That link works, the quote symbol is problematic, I fixed it.<br>
But if you want to use from_blob, that’s not in tensor examples.<br>
See this ‘<a href="https://github.com/pytorch/pytorch/blob/b3f0297a94977636fd90c0fe6fa9b971ff9f81e2/aten/src/ATen/native/quantized/cpu/conv_serialization.h#L116" rel="noopener nofollow ugc">https://github.com/pytorch/pytorch/blob/b3f0297a94977636fd90c0fe6fa9b971ff9f81e2/aten/src/ATen/native/quantized/cpu/conv_serialization.h#L116</a>’<br>
You can take a look how we use it.</p>
<p>A short example is like this:<br>
Tensor out = torch::from_blob(vec.data_ptr(), {sizes},torch::Device(torch::kCUDA, 0).dtype(torch::kFloat32));</p>

username: leimao
cooked: <p>Thank you <a class="mention" href="/u/glaringlee">@glaringlee</a>.<br>
BTW, I think you mean <code>vec.data()</code>? This returns a const pointer and is not compatible with the <code>from_blob</code> inputs.</p>

username: glaringlee
cooked: <p><a class="mention" href="/u/leimao">@leimao</a><br>
Yes, data(), that’s sudo code, sry.<br>
Did you check the example I send you?<br>
‘<a href="https://github.com/pytorch/pytorch/blob/ac8c7c4e9f45f74652df9d068066b7ef297637de/test/cpp/rpc/test_tensorpipe_serialization.cpp#L125" rel="noopener nofollow ugc">https://github.com/pytorch/pytorch/blob/ac8c7c4e9f45f74652df9d068066b7ef297637de/test/cpp/rpc/test_tensorpipe_serialization.cpp#L125</a>’<br>
You can cast the data() ptr to the type you need.</p>

username: leimao
cooked: <p>Thank you, <a class="mention" href="/u/glaringlee">@glaringlee</a> . I see. You are essentially doing a C++ <code>const_cast</code> equivalent here.</p>