简体   繁体   English

Tensorflow C++ 快速 Tensor 深拷贝

[英]Tensorflow C++ fast Tensor deep copy

I am writing a custom tensorflow op in C++ and I want to know how I might perform a deep copy of one tensor into another.我正在用 C++ 编写一个自定义的张量流操作,我想知道如何将一个张量的深度复制到另一个张量中。 In other words, I want an element-wise copy from one tensor to another such that they are not made to share an underlying memory buffer.换句话说,我想要从一个张量到另一个张量的逐元素复制,这样它们就不会共享底层内存缓冲区。

The closest thing I've found is DeepCopy (defined in tensor_util.h ).我发现的最接近的是 DeepCopy(在tensor_util.h 中定义)。 The problem is I need this operation to be fast and the documentation states clearly that this function has not been optimized for speed:问题是我需要这个操作很快,并且文档清楚地说明这个功能没有针对速度进行优化:

// DeepCopy returns a tensor whose contents are a deep copy of the
// contents of 'other'.  This function is intended only for
// convenience, not speed.

A bit more detail: I am using Tensorflow on the GPU, so basically what I want to do is to have TF initiate a cudaMemcpy where both source and target are device pointers.更详细一点:我在 GPU 上使用 Tensorflow,所以基本上我想做的是让 TF 启动一个 cudaMemcpy,其中源和目标都是设备指针。 TF does give access to tensor pointers (tensor.tensor_data().data()), but you run into problems pretty quick if you try to cudaMemcpy with them (they have to do with overlapping cuda contexts; not nice). TF 确实可以访问张量指针(tensor.tensor_data().data()),但是如果您尝试使用它们进行 cudaMemcpy,就会很快遇到问题(它们与重叠的 cuda 上下文有关;不好)。

Thanks in advance!提前致谢!

Currently for "fast" deep copies, TensorFlow uses Eigen to implement CPU and GPU versions.目前对于“快速”深度拷贝,TensorFlow 使用 Eigen 来实现 CPU 和 GPU 版本。 Being Eigen, the code is deeply templated, but the relevant code is here (copied from dense_update_ops.h ):作为 Eigen,代码是深度模板化的,但相关代码在这里(从dense_update_ops.h复制):

template <typename Device, typename T>
struct DenseUpdate<Device, T, ASSIGN> {
  void operator()(const Device& d, typename TTypes<T>::Flat params,
                  typename TTypes<T>::ConstFlat update) {
    params.device(d) = update;
  }
};

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

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