如何在张量流中在 gpu 和 cpu 之间拉/推数据 [英] How can I pull/push data between gpu and cpu in tensorflow

查看:46
本文介绍了如何在张量流中在 gpu 和 cpu 之间拉/推数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用临时张量将数据存储在我定制的基于 gpu 的操作中.出于调试目的,我想通过 C++ 中的传统 printf 打印此张量的数据.如何将这个基于 gpu 的张量拉到 cpu,然后打印其内容.非常感谢.

I used a temporary tensor to store data in my customized gpu-based op. For debug purpose, I want to print the data of this tensor by traditional printf inside C++. How can I pull this gpu-based tensor to cpu and then print its contents. Thank you very much.

推荐答案

如果 temporary 你的意思是 allocate_temp 而不是 allocate_output,有无法在python端获取数据.

If by temporary you mean allocate_temp instead of allocate_output, there is no way of fetching the data on the python side.

我通常在调试期间返回张量本身,以便一个简单的 sess.run 获取结果.否则,显示数据的唯一方法是传统的printf inside C++.鉴于您的张量是您自定义操作的输出,tf.Print 可以简化进一步的调试.

I usually return the tensor itself during debugging so that a simple sess.run fetches the result. Otherwise, the only way to display the data is the traditional printf inside C++. Given your tensor is an output of your custom operation a tf.Print eases further debugging.

示例:

Tensor temp_tensor;
OP_REQUIRES_OK(ctx, ctx->allocate_temp(DT_FLOAT, some.shape(), &temp_tensor));

float* host_memory = new float[some.NumElements()];
cudaMemcpy(host_memory, temp_tensor.flat<Dtype>().data(), some.NumElements() * sizeof(float), cudaMemcpyDeviceToHost);
std::cout << host_memory[0] << std::endl;
std::cout << host_memory[1] << std::endl;
std::cout << host_memory[2] << std::endl;
delete[] host_memory;

这篇关于如何在张量流中在 gpu 和 cpu 之间拉/推数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆