Issue
I have the (initialized) tensor input
and want to access the data directly using a pointer. I am using
float *input_ptr = input.flat<float>().data();
When I try to access the data, for example by using:
input_ptr[0]
it causes a segmentation fault but I don't understand why.
According to the C++ API:
flat()
Return the tensor data as an Eigen::Tensor of the data type and a specified shape.
These methods allow you to access the data with the dimensions and sizes of your choice. You do not need to know the number of dimensions of the Tensor to call them. However, they CHECK that the type matches and the dimensions requested creates an Eigen::Tensor with the same number of elements as the tensor.`
This gets me the eigen Tensor. Now, according to the Eigen documentation:
data()
Returns a pointer to the storage for the tensor. The pointer is const if the tensor was const. This allows direct access to the data. The layout of the data depends on the tensor layout: RowMajor or ColMajor.
Example:
Eigen::Tensor<float, 2> a(3, 4);
float* a_data = a.data();
>a_data[0] = 123.45f;
>cout << "a(0, 0): " << a(0, 0);
>=> a(0, 0): 123.45
So, as far as I can tell, I should be able to use input_ptr
without problems.
(It's worth noting that the pointer does indeed point to a valid location. I can, for example, use cudaMemcpy
to successfully copy its contents to a device array. So the problem has to be that I'm not allowed to access the data location, I just cannot figure out why.)
Any ideas would be appreciated.
Solution
It looks like you're trying to access a GPU tensor from the CPU. That does not work.
To manipulate a GPU tensor, use eigen operations directly or write an op which is placed on the CPU so that the tensor will get copied, and then you can access it.
Answered By - Alexandre Passos
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.