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

Hi,

I wonder if I can initialize torch::Tensor from std::vector like this

#include <torch/torch.h>
#include <vector>

int main()
{
  std::vector<T> initializer;
  ...
  torch::Tensor tensor = torch::from_blob(initializer);
}
3 Likes

I used torch::tensor(ArrayRef<float>) successfully.
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.

Best regards

Thomas

3 Likes

Thank you Tomas!

I’ll try that.

torch::tensor makes a copy, from_blob does not (but torch::from_blob(vector).clone() does)

10 Likes

Could you please point me to the documentation for the torch::tensor API? Thank you.

@leimao
What do you want to achieve?

If you need examples on how to create tensor, here,there are examples in the tests:
https://github.com/pytorch/pytorch/blob/455e85a2f181a66e7505d0e7eeb0ad0825bc4a41/aten/src/ATen/test/basic.cpp

C++ doc is here:
https://pytorch.org/cppdocs/api/classat_1_1_tensor.html?highlight=tensor

Thank you @glaringlee. I want to create a torch::Tensor on device from (preferably const) std::vector.
I got around by calling torch::from_blob and .clone(). Not sure if there is any more convenient way to do so.

The link you provided to GitHub is expired.
The C++ doc describes the usage of torch::Tensor class instead of the torch::tensor function.

@leimao
That link works, the quote symbol is problematic, I fixed it.
But if you want to use from_blob, that’s not in tensor examples.
See this ‘https://github.com/pytorch/pytorch/blob/b3f0297a94977636fd90c0fe6fa9b971ff9f81e2/aten/src/ATen/native/quantized/cpu/conv_serialization.h#L116
You can take a look how we use it.

A short example is like this:
Tensor out = torch::from_blob(vec.data_ptr(), {sizes},torch::Device(torch::kCUDA, 0).dtype(torch::kFloat32));

1 Like

Thank you @glaringlee.
BTW, I think you mean vec.data()? This returns a const pointer and is not compatible with the from_blob inputs.

@leimao
Yes, data(), that’s sudo code, sry.
Did you check the example I send you?
https://github.com/pytorch/pytorch/blob/ac8c7c4e9f45f74652df9d068066b7ef297637de/test/cpp/rpc/test_tensorpipe_serialization.cpp#L125
You can cast the data() ptr to the type you need.

Thank you, @glaringlee . I see. You are essentially doing a C++ const_cast equivalent here.