简体   繁体   English

如何在C ++中从原始指针数据构造tensorflow :: Tensor

[英]How to construct a tensorflow::Tensor from raw pointer data in C++

I want to change the output tensor's underlying storage during an op. 我想在操作期间更改输出张量的底层存储。

I have a raw pointer(float*) of the new data. 我有一个新数据的原始指针(float *)。 I want to set the output tensor to this new data before launch kernel and return, so that I can hijack this op. 我想在启动内核并返回之前将输出张量设置为这个新数据,这样我就可以劫持这个操作。

However I am confusing with when should I delete the raw pointer, since the the tensor construct seems to be a shallow copy. 但是我很困惑我什么时候应该删除原始指针,因为张量结构似乎是一个浅的副本。 I can only delete the raw pointer after all this tensor's usage finished. 在完成所有张量的使用后,我只能删除原始指针。 But how can I be notified this? 但是我如何通知这个?

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: 在TensorFlow运行时内没有用于执行此操作的公共API,但可以使用C API方法TF_NewTensor()从原始指针创建Tensor对象,该方法具有以下签名:

// 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. 在内部,这会创建一个引用计数的TensorBuffer对象,该对象获取原始指针的所有权。 (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. (不幸的是,只有C API有friend访问才能直接从TensorBuffer创建一个tensorflow::Tensor 。这是一个开放的问题 。)当引用计数降到零时,使用datalendellocator_arg的值调用deallocator函数。 。

Unfortunately, this is too little information to give you a precise answer. 不幸的是,这些信息太少,无法给出准确答案。 Possibly, you're not even allowed to delete the pointer! 可能,你甚至不允许删除指针!

Imagine something like this: 想象一下这样的事情:

float* gf = nullptr; // global pointer (just for illustration)

void calculate()
{
   float f;
   gf = &f;
   doSomething();
   gf = nullptr;
}

Same applies, if your pointer in question points to some class-static or global variable. 如果您的指针指向某个类静态或全局变量,则同样适用。

If you create your variable on the heap, then delete it when you know you don't need it any more, which can be handled quite locally (typically), such as in this example: 如果在堆上创建变量,则在知道不再需要它时将其删除,这可以在本地(通常)处理,例如在此示例中:

class C
{
   std::vector<float>values;

   C(size_t num) : values(num, 0.0f) { }
   ~C() { } // data deleted automatically with vector

   void function()
   {
        for(float& f : values)
        {
            gf = &f;
            doSomething();
        }
        gf = nullptr;
   }
};

Missing the explicit call to operator delete[] ? 缺少显式调用operator delete[] Well, the vector handles this implicitly for me, so I don't have to bother. 好吧,矢量对我来说是隐式处理的,所以我不必费心。 Even if you are forced to use raw pointers, you can avoid explict deletion by use of eg std::unique_ptr ... Attention with the vector, though: the pointer might get invalid, if you add new elements to the vector or remove contained ones from it! 即使您被迫使用原始指针,也可以通过使用例如std::unique_ptr来避免删除...但是注意向量:如果向向量添加新元素或者删除包含,则指针可能无效来自它!

In my examples, I set the gf pointer explicitly to nullptr so that you can inspect when there is no float value in use - you'd have to check for while hijacking, of course... Be aware that in an multithreaded environment, you possibly might protect your pointer against race conditions. 在我的示例中,我将gf指针显式设置为nullptr以便您可以检查何时没有使用浮点值 - 当然,您必须在劫持时检查...请注意,在多线程环境中,您可能会保护你的指针免受竞争条件的影响

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM