tensorflow为gpu使用做了一个操作 - 演示应用程序不起作用

时间:2016-11-05 20:45:05

标签: python tensorflow gpu

我使用tensorflow,在创建以下文件后,我收到以下错误。我怀疑我提供了错误的输入,但我不知道如何将其更改为正确的表示。

dijkstra.py:

    self.maze = tf.Variable(tf.zeros([64], dtype=tf.int32), name="grid")

    print  self.maze
    if True : 
        self.grid_module = tf.load_op_library('d_grid_gpu.so')
        with tf.Session('') as sess:
            sess.run(tf.initialize_all_variables())
            self.output = self.grid_module.grid_gpu(
                    self.maze

                ).eval()

d_grid_gpu.cc:

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

    using namespace tensorflow;

    REGISTER_OP("GridGpu").Input("grid: int32").Output("prev: int32");    

        void run( int * in);

    class DGridGpuOp : public OpKernel {
      public:
      explicit DGridGpuOp(OpKernelConstruction* context) : OpKernel(context) {


      }

      void Compute(OpKernelContext* context) override {


        Tensor* prev_tensor = NULL;

        Tensor grid_tensor = context->input(0);

        auto grid = grid_tensor.flat<int32>();    


        OP_REQUIRES_OK(context, context->allocate_output(
                                     0, 
                                     TensorShape({64}), &prev_tensor));

        auto prev = prev_tensor->template flat<int32>();


        run(grid.data());//


      }

    };

    REGISTER_KERNEL_BUILDER(Name("GridGpu").Device(DEVICE_GPU), DGridGpuOp);

d_grid_gpu.cu.cc:

    #if GOOGLE_CUDA
    #define EIGEN_USE_GPU
    #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"

    #include <stdio.h>
    #define SIZE    10

        __global__ void VectorAdd(  int *in,  int n)
        {
            int i = threadIdx.x;

            if (i < n)
                in[i] = in[i] + i;
        }


        void run( int * in){

            VectorAdd<<< 1, SIZE >>>(  in,  SIZE);

            /* 
            //these lines cause the segfault
            //for (int i = 0; i < SIZE; i ++) {
            //    printf("%i, " , in[i]);
            //}
            */
        }


    #endif

构建脚本:

    TF_INC=$(python -c 'import tensorflow as tf; print(tf.sysconfig.get_include())')


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

    g++ -std=c++11 -shared -o d_grid_gpu.so d_grid_gpu.cc \
    d_grid_gpu.cu.o -I $TF_INC -fPIC -lcudart -D_GLIBCXX_USE_CXX11_ABI=0 -L /usr/lib/x86_64-linux-gnu/

修改:我删除了旧输出。

我尝试过添加&#39; op(来自TF howto页面)我认为我得到了它的工作。这让我相信我的安装没问题。这个例子编译。我只是无法正确注册 - 或者其他什么。欢迎任何帮助。

编辑:我重新安装了tensorflow,现在错误有点不同

    I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so locally
    I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so locally
    I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so locally
    I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locally
    I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so locally
    simple dijkstra for tensorflow
    <tensorflow.python.ops.variables.Variable object at 0x7fdec57c1b50>
    I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:925] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
    I tensorflow/core/common_runtime/gpu/gpu_device.cc:951] Found device 0 with properties: 
    name: GeForce GTX 850M
    major: 5 minor: 0 memoryClockRate (GHz) 0.9015
    pciBusID 0000:0a:00.0
    Total memory: 3.95GiB
    Free memory: 3.64GiB
    I tensorflow/core/common_runtime/gpu/gpu_device.cc:972] DMA: 0 
    I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] 0:   Y 
    I tensorflow/core/common_runtime/gpu/gpu_device.cc:1041] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 850M, pci bus id: 0000:0a:00.0)
    Traceback (most recent call last):
      File "test_op.py", line 45, in <module>
        d.eval()
      File "/home/dave/workspace/awesome-tf/test_gpu/dijkstra.py", line 57, in eval
        self.maze
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 559, in eval
        return _eval_using_default_session(self, feed_dict, self.graph, session)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 3761, in _eval_using_default_session
        return session.run(tensors, feed_dict)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 717, in run
        run_metadata_ptr)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 915, in _run
        feed_dict_string, options, run_metadata)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 965, in _do_run
        target_list, options, run_metadata)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 985, in _do_call
        raise type(e)(node_def, op, message)
    tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value grid
         [[Node: grid/read = Identity[T=DT_INT32, _class=["loc:@grid"], _device="/job:localhost/replica:0/task:0/cpu:0"](grid)]]

    Caused by op u'grid/read', defined at:
      File "test_op.py", line 45, in <module>
        d.eval()
      File "/home/dave/workspace/awesome-tf/test_gpu/dijkstra.py", line 50, in eval
        self.maze = tf.Variable(tf.zeros([64], dtype=tf.int32), name="grid")
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 215, in __init__
        dtype=dtype)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 327, in _init_from_args
        self._snapshot = array_ops.identity(self._variable, name="read")
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1128, in identity
        result = _op_def_lib.apply_op("Identity", input=input, name=name)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
        op_def=op_def)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
        original_op=self._default_original_op, op_def=op_def)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
        self._traceback = _extract_stack()

    FailedPreconditionError (see above for traceback): Attempting to use uninitialized value grid
         [[Node: grid/read = Identity[T=DT_INT32, _class=["loc:@grid"], _device="/job:localhost/replica:0/task:0/cpu:0"](grid)]]

有时这是我的输出:

    I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so locally
    I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so locally
    I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so locally
    I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locally
    I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so locally
    simple dijkstra for tensorflow
    <tensorflow.python.ops.variables.Variable object at 0x7fba5d0dafd0>
    I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:925] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
    I tensorflow/core/common_runtime/gpu/gpu_device.cc:951] Found device 0 with properties: 
    name: GeForce GTX 850M
    major: 5 minor: 0 memoryClockRate (GHz) 0.9015
    pciBusID 0000:0a:00.0
    Total memory: 3.95GiB
    Free memory: 3.67GiB
    I tensorflow/core/common_runtime/gpu/gpu_device.cc:972] DMA: 0 
    I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] 0:   Y 
    I tensorflow/core/common_runtime/gpu/gpu_device.cc:1041] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 850M, pci bus id: 0000:0a:00.0)
    Segmentation fault (core dumped)

当我使用initialize_all_variables()

时就是这种情况

1 个答案:

答案 0 :(得分:1)

您可能希望使用tf.initialize_all_variables进行初始化,例如: with tf.Session() as sess: sess.run(tf.initialize_all_variables())