【问题标题】:How can I pull/push data between gpu and cpu in tensorflow如何在张量流中的 gpu 和 cpu 之间拉/推数据
【发布时间】:2018-12-23 12:23:13
【问题描述】:

我使用一个临时张量将数据存储在我定制的基于 gpu 的操作中。出于调试目的,我想通过 C++ 中的传统 printf 打印此张量的数据。我怎样才能把这个基于 gpu 的张量拉到 cpu 上,然后打印它的内容。 非常感谢。

【问题讨论】:

    标签: c++ tensorflow cuda


    【解决方案1】:

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

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

    例子:

    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;
    

    【讨论】:

    • 感谢您的帮助。是的,我想通过 C++ 中的传统 printf 输出数据。既然是基于gpu的张量,我想知道如何将数据从gpu拉到cpu?
    • cumemcpy 或 if(!threadIdx.x) printf 就像在每个 cuda 实现中一样。
    猜你喜欢
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    相关资源
    最近更新 更多