【问题标题】:Why does the floatX's flag impact whether GPU is used in Theano?为什么 floatX 的标志会影响 Theano 中是否使用 GPU?
【发布时间】:2016-06-30 03:20:34
【问题描述】:

我正在使用 script provided in the tutorial for that purpose 使用 GPU 测试 Theano:

# Start gpu_test.py
# From http://deeplearning.net/software/theano/tutorial/using_gpu.html#using-gpu
from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in xrange(iters):
    r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')
# End gpu_test.py

如果我指定floatX=float32,它会在 GPU 上运行:

francky@here:/fun$ THEANO_FLAGS='mode=FAST_RUN,device=gpu2,floatX=float32' python gpu_test.py
Using gpu device 2: GeForce GTX TITAN X (CNMeM is disabled)
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>), HostFromGpu(Gp
Looping 1000 times took 1.458473 seconds
Result is [ 1.23178029  1.61879349  1.52278066 ...,  2.20771813  2.29967761
  1.62323296]
Used the gpu

如果我不指定floatX=float32,它在CPU上运行:

francky@here:/fun$ THEANO_FLAGS='mode=FAST_RUN,device=gpu2'
Using gpu device 2: GeForce GTX TITAN X (CNMeM is disabled)
[Elemwise{exp,no_inplace}(<TensorType(float64, vector)>)]
Looping 1000 times took 3.086261 seconds
Result is [ 1.23178032  1.61879341  1.52278065 ...,  2.20771815  2.29967753
  1.62323285]
Used the cpu

如果我指定floatX=float64,它在CPU上运行:

francky@here:/fun$ THEANO_FLAGS='mode=FAST_RUN,device=gpu2,floatX=float64' python gpu_test.py
Using gpu device 2: GeForce GTX TITAN X (CNMeM is disabled)
[Elemwise{exp,no_inplace}(<TensorType(float64, vector)>)]
Looping 1000 times took 3.148040 seconds
Result is [ 1.23178032  1.61879341  1.52278065 ...,  2.20771815  2.29967753
  1.62323285]
Used the cpu

为什么floatX 标志会影响 Theano 中是否使用 GPU?

我用:

  • Theano 0.7.0(根据pip freeze),
  • Python 2.7.6 64 位(根据import platform; platform.architecture()),
  • Nvidia-smi 361.28(根据nvidia-smi),
  • CUDA 7.5.17(根据nvcc --version),
  • GeForce GTX Titan X(根据nvidia-smi),
  • Ubuntu 14.04.4 LTS x64(根据lsb_release -auname -i)。

我阅读了floatX 上的文档,但没有帮助。它只是说:

config.floatX
字符串值:“float64”或“float32”
默认值:‘float64’

这设置了 tensor.matrix() 返回的默认 dtype, tensor.vector() 和类似的函数。它还设置了默认值 作为 Python 浮点数传递的参数的 theano 位宽 数字。

【问题讨论】:

    标签: python gpu theano


    【解决方案1】:

    来自http://deeplearning.net/software/theano/tutorial/using_gpu.html#gpuarray-backend我读到可以在GPU上执行float64计算,但你必须从源代码安装libgpuarray

    我设法安装了它,见this script,我用过virtualenv,你甚至不必有sudo

    安装后,您可以使用config flag device=gpu 的旧后端和device=cuda 的新后端。

    新的后端可以执行 64 位计算,但对我来说工作方式不同。一些操作停止工作。 ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law :)

    【讨论】:

    【解决方案2】:

    据我所知,这是因为他们还没有为 GPU 实现 float64。

    http://deeplearning.net/software/theano/tutorial/using_gpu.html

    只能加速具有 float32 数据类型的计算。预计即将推出的硬件会更好地支持 float64,但 float64 计算仍然相对较慢(2010 年 1 月)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-07
      • 2014-05-16
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多