我想在操作期间更改输出张量的基础存储。
我有新数据的原始指针(float *)。我想在启动内核并返回之前将输出张量设置为此新数据,以便可以劫持此操作。
但是我对何时删除原始指针感到困惑,因为张量构造似乎是一个浅表副本。我只能在所有此张量的使用完成后删除原始指针。但是如何通知我呢?
在 TensorFlow 运行时内没有用于执行此操作的公共 API,但可以使用 C API 方法从原始指针创建 Tensor 对象,该方法TF_NewTensor()具有以下签名:
// 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);
Run Code Online (Sandbox Code Playgroud)
在内部,这会创建一个引用计数TensorBuffer对象,该对象拥有原始指针的所有权。(遗憾的是,只有C API具有friend访问权限创建tensorflow::Tensor从一个TensorBuffer直接,这是一个悬而未决的问题。)的deallocator函数调用的值data,len并dellocator_arg在该引用计数下降到零。
| 归档时间: |
|
| 查看次数: |
1967 次 |
| 最近记录: |