代码之家  ›  专栏  ›  技术社区  ›  Srinath Ganesh

TensorFlow GPU:HelloWorld代码没有性能提升

  •  0
  • Srinath Ganesh  · 技术社区  · 7 年前

    背景

    我是TensorFlow的新Python开发人员。

    系统规格:

    • GeForce 940MX 4GB
    • Ubuntu 18

    我在Docker上运行TensorFlow(发现安装cuda的东西太复杂、太长,可能是我搞糟了)


    基本上,我在GPU和CPU上运行一种HelloWorld代码,并检查它会有什么不同,让我惊讶的是,几乎没有什么不同!

    version: '2.3'
    
    services:
      tensorflow:
        # image: tensorflow/tensorflow:latest-gpu-py3
        image: tensorflow/tensorflow:latest-py3
        runtime: nvidia
        volumes:
          - ./:/notebooks/TensorTest1
        ports:
          - 8888:8888
    

    image: tensorflow/tensorflow:latest-py3 我有大约5秒钟的时间。

    root@e7dc71acfa59:/notebooks/TensorTest1# python3 hello1.py 
    2018-11-18 14:37:24.288321: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
    TIME: 4.900559186935425
    result:  [3. 3. 3. ... 3. 3. 3.]
    

    当我和你一起跑的时候 image: tensorflow/tensorflow:latest-gpu-py3

    root@baf68fc71921:/notebooks/TensorTest1# python3 hello1.py 
    2018-11-18 14:39:39.811575: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
    2018-11-18 14:39:39.877483: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:964] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
    2018-11-18 14:39:39.878122: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties: 
    name: GeForce 940MX major: 5 minor: 0 memoryClockRate(GHz): 1.189
    pciBusID: 0000:01:00.0
    totalMemory: 3.95GiB freeMemory: 3.56GiB
    2018-11-18 14:39:39.878148: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
     2018-11-18 14:44:17.101263: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
    2018-11-18 14:44:17.101303: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988]      0 
    2018-11-18 14:44:17.101313: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0:   N 
    2018-11-18 14:44:17.101540: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3259 MB memory) -> physical GPU (device: 0, name: GeForce 940MX, pci bus id: 0000:01:00.0, compute capability: 5.0)
    TIME: 5.82940673828125
    result:  [3. 3. 3. ... 3. 3. 3.]
    

    我的代码

    import tensorflow as tf
    import time
    
    with tf.Session():
        start_time = time.time()
    
        input1 = tf.constant([1.0, 1.0, 1.0, 1.0] * 100 * 100 * 100)
        input2 = tf.constant([2.0, 2.0, 2.0, 2.0] * 100 * 100 * 100)
        output = tf.add(input1, input2)
        result = output.eval()
    
        duration = time.time() - start_time
        print("TIME:", duration)
    
        print("result: ", result)
    

    我做错什么了吗?基于打印,它似乎正确地使用了GPU


    Can I measure the execution time of individual operations with TensorFlow? 我得到了这个 enter image description here

    1 回复  |  直到 7 年前
        1
  •  2
  •   hobbs    7 年前

    GPU是一个“外部”处理器,为它编译程序、运行程序、发送数据和检索结果都会带来开销。GPU也有不同于CPU的性能权衡。虽然GPU对于大型和复杂的数字运算任务通常更快,但您的“hello world”太简单了。在加载和保存数据项之间,它对每个数据项都没有多大作用(只是两两相加),而且在任何时候都没有多大作用 一百万次的行动算不了什么。这使得任何设置/拆卸开销都相对更加明显。因此,虽然GPU的速度较慢

    推荐文章