为 CUDA 编译自定义张量流操作

Compile custom tensorflow op for CUDA

我正在按照 the tensorflow documentation. While tracing the errors in my own code I went back to the example from the documentation and tried to compile the referenced code example:

中的指南开发需要 GPU 支持的 tensorflow 自定义 OP
#if GOOGLE_CUDA
#define EIGEN_USE_GPU
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"

__global__ void AddOneKernel(const int* in, const int N, int* out) {
  for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < N;
       i += blockDim.x * gridDim.x) {
    out[i] = in[i] + 1;
  }
}

void AddOneKernelLauncher(const int* in, const int N, int* out) {
  AddOneKernel<<<32, 256>>>(in, N, out);
}

#endif

使用文档中建议的命令:

nvcc -std=c++11 -c -o cuda_op_kernel.cu.o cuda_op_kernel.cu.cc \
-I $TF_INC -D GOOGLE_CUDA=1 -x cu -Xcompiler -fPIC

with $TF_INC 正确替换为 tensorflow 包含路径。不幸的是,这会产生很多错误:

/usr/lib/gcc/x86_64-linux-gnu/5/include/emmintrin.h(1294): error: expression must have arithmetic, unscoped enum, or pointer type
/usr/lib/gcc/x86_64-linux-gnu/5/include/emmintrin.h(1300): error: expression must have arithmetic, unscoped enum, or pointer type
/usr/lib/gcc/x86_64-linux-gnu/5/include/emmintrin.h(1306): error: expression must have arithmetic, unscoped enum, or pointer type
/usr/lib/gcc/x86_64-linux-gnu/5/include/emmintrin.h(1312): error: expression must have arithmetic, unscoped enum, or pointer type
/usr/lib/gcc/x86_64-linux-gnu/5/include/emmintrin.h(1318): error: expression must have arithmetic, unscoped enum, or pointer type
/usr/lib/gcc/x86_64-linux-gnu/5/include/emmintrin.h(1324): error: expression must have arithmetic, unscoped enum, or pointer type
/usr/lib/gcc/x86_64-linux-gnu/5/include/emmintrin.h(1330): error: expression must have arithmetic, unscoped enum, or pointer type
/usr/lib/gcc/x86_64-linux-gnu/5/include/emmintrin.h(1336): error: expression must have arithmetic, unscoped enum, or pointer type

还有很多类似的东西。

我发现这可能与不受支持的 nvcc / gcc / os 组合有关。我没有自己设置机器(实际上没有 sudo 权限)。我在 Ubuntu 16.04.2 上有 nvcc 7.5.17 版,gcc 4.9.3 版。 Ubuntu 16.04.2 未在 CUDA 7.5 支持的系统中列出。这可能是个问题,但我发现很多人声称它适用于 16.04。另外我在这台机器上成功编译了支持GPU的Tensorflow..

此外,这些错误与代码中的 the Tensor #include 有关,并且代码在没有此行的情况下编译成功。如果演示 OP 在没有这个包含的情况下工作,我还没有尝试过,但是我自己的 OP 失败了

2017-06-01 09:36:14.679685: E tensorflow/stream_executor/cuda/cuda_driver.cc:1067] could not synchronize on CUDA context: CUDA_ERROR_LAUNCH_FAILED :: No stack trace available
2017-06-01 09:36:14.679777: F tensorflow/core/common_runtime/gpu/gpu_util.cc:370] GPU sync failed

两个问题:

  1. 为什么我需要包含这个 Eigen Tensor header,尽管演示 OP 实际上没有使用 Eigen Tensor?
  2. 错误从何而来,如何解决?您认为这与不受支持的系统配置有关吗?

好的,对于那些遇到相同问题的人:您可以使用 -ccbin 参数为 nvcc 设置主机编译器,如 this 答案中所指出的。只需将其设置为 gcc-4.9.