【问题标题】:add an op tensorflow debugging添加一个 op tensorflow 调试
【发布时间】:2018-08-09 04:17:29
【问题描述】:

我想知道在向 tensorflow 添加新操作以进行调试时如何打印输入张量的值。我一直在学习 cuda_op_kernel.cc 的教程,如下所示:

#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include <typeinfo>

using namespace tensorflow;  // NOLINT(build/namespaces)

REGISTER_OP("AddOne")
    .Input("input: int32")
    .Output("output: int32")
    .Doc(R"doc(
Adds 1 to all elements of the tensor.

output: A Tensor.
  output = input + 1
)doc");

void AddOneKernelLauncher(const int* in, const int N, int* out);

class AddOneOp : public OpKernel {
 public:
  explicit AddOneOp(OpKernelConstruction* context) : OpKernel(context) {}

  void Compute(OpKernelContext* context) override {
    // Grab the input tensor
    const Tensor& input_tensor = context->input(0);
    auto input = input_tensor.flat<int32>();

    // Create an output tensor
    Tensor* output_tensor = nullptr;
    OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),
                                                     &output_tensor));
    auto output = output_tensor->template flat<int32>();

    // Set all but the first element of the output tensor to 0.
    const int N = input.size();
    // Call the cuda kernel launcher
    std::cout << input.data() << std::endl;
    std::cout << input.data()[0] << std::endl;
    AddOneKernelLauncher(input.data(), N, output.data());
  }
};

REGISTER_KERNEL_BUILDER(Name("AddOne").Device(DEVICE_GPU), AddOneOp);

第二个 std::cout 导致段错误。我的理解是 input.data() 应该是一个 const int 数组,所以我可以打印出它的值。怎么了?

【问题讨论】:

  • 请注意,存在一个tf.Print 操作,主要用于调试目的,您可以在传递操作输入之前将其应用于操作。
  • 至于报错,会不会是tensor实际上是空的?在任何情况下,打印操作(see here)的实现都是基于tensorflow::Tensor.SummarizeValue,所以你可以研究一下......

标签: c++ tensorflow


【解决方案1】:

我知道为什么了。 Input.data() 是指向 GPU 中地址的指针。流执行器显然已经把它放在那里了。尝试取消对 CPU 的引用是不好的。

【讨论】:

    猜你喜欢
    • 2021-04-12
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    相关资源
    最近更新 更多