How to construct a tensorflow::Tensor from raw pointer data in C++
There is no public API for doing this inside the TensorFlow runtime, but it is possible to create a Tensor object from a raw pointer using the C API method TF_NewTensor()
, which has the following signature:
// Return a new tensor that holds the bytes data[0,len-1].
//
// The data will be deallocated by a subsequent call to TF_DeleteTensor via:
// (*deallocator)(data, len, deallocator_arg)
// Clients must provide a custom deallocator function so they can pass in
// memory managed by something like numpy.
extern TF_Tensor* TF_NewTensor(TF_DataType, const int64_t* dims, int num_dims,
void* data, size_t len,
void (*deallocator)(void* data, size_t len,
void* arg),
void* deallocator_arg);
Internally, this creates a reference-counted TensorBuffer
object that takes ownership of the raw pointer. (Unfortunately, only the C API has friend
access to create a tensorflow::Tensor
from a TensorBuffer
directly. This is an open issue.) The deallocator
function is called with the values of data
, len
and dellocator_arg
when the reference count drops to zero.