在Tensorflow中添加GPU Op

时间:2017-06-07 03:46:32

标签: python c++ tensorflow cuda tensorflow-gpu

我正在尝试在this文档之后松散地向TensorFlow添加新操作。不同之处在于我正在尝试实现基于GPU的操作。我试图添加的是来自here的cuda op(cuda_op.py,cuda_op_kernel.cc,cuda_op_kernel.cu.cc)。我正在尝试在tensorflow之外编译这些并使用tf.load_op_library来引入它们。我做了一些更改,所以这里是我的文件:

cuda_op_kernel.cc

#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/shape_inference.h"
#include "tensorflow/core/framework/op_kernel.h"

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

REGISTER_OP("AddOne")
    .Input("input: int32")
    .Output("output: int32")
    .SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) {
      c->set_output(0, c->input(0));
      return Status::OK();
    });

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 = NULL;
    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
    AddOneKernelLauncher(input.data(), N, output.data());

  }
};

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

cuda_op_kernel.cu

#define EIGEN_USE_GPU
#include <cuda.h>
#include <stdio.h>

__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);

  cudaError_t cudaerr = cudaDeviceSynchronize();
  if (cudaerr != cudaSuccess)
    printf("kernel launch failed with error \"%s\".\n", cudaGetErrorString(cudaerr));
}

的CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

#found from running python -c 'import tensorflow as tf; print(tf.sysconfig.get_include())'
include_directories(/usr/local/lib/python3.5/dist-packages/tensorflow/include)

find_package(CUDA)

#set flags based on tutorial
set (CMAKE_CXX_FLAGS "--std=c++11 -fPIC -O2 -D_GLIBCXX_USE_CXX11_ABI=0")

#pass flags to c++ compiler
SET(CUDA_PROPAGATE_HOST_FLAGS ON)

#create library
cuda_add_library(
    cuda_op SHARED
    src/cuda_op_kernel.cu
    src/cuda_op_kernel.cc
    OPTIONS -gencode=arch=compute_20,code=sm_20)

#copy test file to build folder
configure_file(src/test.py test.py COPYONLY)

test.py

import tensorflow as tf
mod = tf.load_op_library('./libcuda_op.so')
with tf.Session() as sess:
    start = [5,4,3,2,1]
    print(start)
    print(mod.add_one(start).eval())

我能够成功编译并运行test.py,但输出始终为[0 0 0 0 0]。如果我将AddOneKernel<<<32, 256>>>(in, N, out);替换为for (int i = 0; i < N; i++) out[i] = in[i] + 1;而将DEVICE_GPU替换为DEVICE_CPU,则op会输出正确的值[6 5 4 3 2](完全相同的CMakeList.txt)。< / p>

知道如何获取正确的值吗?

3 个答案:

答案 0 :(得分:2)

我不完全记得我在哪里找到了CUDA的cmake内容,但选项却以某种方式弄乱了编译。将cuda_add_library中的CMakeLists.txt替换为以下内容可解决此问题。

#no options needed
cuda_add_library(
    cuda_op SHARED
    src/cuda_op_kernel.cu
    src/cuda_op_kernel.cc)

答案 1 :(得分:0)

ubuntu @cubuntu:〜/ Desktop / src / src / build $ cmake ..

- 配置完成

- 生成完成

- 构建文件已写入:/ home / ubuntu / Desktop / src / src / build

ubuntu @cabuntu:〜/ Desktop / src / src / build $ make

[33%]构建NVCC(设备)对象CMakeFiles / cuda_op.d / cuda_op_generated_cuda_op_kernel.cu.o

nvcc警告:&#39; compute_20&#39;,&#39; sm_20&#39;和&#39; sm_21&#39;架构已弃用,可能会在将来的版本中删除(使用-Wno-deprecated-gpu-targets来禁止警告)。

nvcc警告:&#39; compute_20&#39;,&#39; sm_20&#39;和&#39; sm_21&#39;架构已弃用,可能会在将来的版本中删除(使用-Wno-deprecated-gpu-targets来禁止警告)。

扫描目标cuda_op的依赖关系

[66%]构建CXX对象CMakeFiles / cuda_op.dir / cuda_op_kernel.cc.o /home/ubuntu/Desktop/src/src/cuda_op_kernel.cc:1:17:错误:'tensorflow'不是名称空间名称 使用namespace tensorflow; // NOLINT(构建/命名空间)

答案 2 :(得分:0)

查看Tensorflow adding GPU op support上当前的官方GPU操作系统构建说明

nvcc -std=c++11 -c -o cuda_op_kernel.cu.o cuda_op_kernel.cu.cc \
  ${TF_CFLAGS[@]} -D GOOGLE_CUDA=1 -x cu -Xcompiler -fPIC

g++ -std=c++11 -shared -o cuda_op_kernel.so cuda_op_kernel.cc \
  cuda_op_kernel.cu.o ${TF_CFLAGS[@]} -fPIC -lcudart ${TF_LFLAGS[@]}

如前所述,请注意,如果您的CUDA库未安装在/usr/local/lib64中,则需要在上面的第二个(g ++)命令中显式指定路径。例如,如果您的CUDA已安装在-L /usr/local/cuda-8.0/lib64/中,则添加/usr/local/cuda-8.0

此外,请注意,在某些Linux设置中,需要nvcc编译步骤的其他选项。将-D_MWAITXINTRIN_H_INCLUDED添加到nvcc命令行中,以避免来自mwaitxintrin.h的错误。