How to use TF_BitcastFrom to get another "view" on a tensor?


writing a pluggable device, I am adding support for various kernels and sometimes I need a way to just change the tensor metadata like its shape (as in the Reshape operation).

Taking the reshape example, it seems to me that TF_BitcastFrom serves exatly this purpose: giving another view on a tensor, that is same buffer but different metadata - that would be an ideal case for Reshape.

However in practice it doesn’t seem to fit that use case very well… Checkint out the signature:

TF_CAPI_EXPORT extern void TF_TensorBitcastFrom(const TF_Tensor* from,
                                                TF_DataType type, TF_Tensor* to,
                                                const int64_t* new_dims,
                                                int num_new_dims,
                                                TF_Status* status);

to must be initialised. the only way to get a tensor through the C api is to allocate it and its buffer with it, and if we allocate its buffer that makes the bitcast pointless as we need to copy.

The other way I was thinking could be possible was to use the same to and from, but I ended up getting into some weird memory issues doing so - not 100% that it is because of that but almost sure.

So I guess my questions are:

  1. How is TF_BitcastFrom intended to be used and in what context? (examples are welcome)
  2. How can I get different view on a tensor without allocating and copying new tensors?

Any chance to get an answer on this?

I found the bug in my code coming from something else.

To answer, TF_TensorBitcastTo can be called with the same from and to to update the tensor shape without allocating/copying any data.