tensorflow gpu版本运行时怎么知道有没有调用gpu以及cuda加速

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tensorflow gpu版本运行时怎么知道有没有调用gpu以及cuda加速相关的知识,希望对你有一定的参考价值。

参考技术A 首先需要看你的PC配置是否够,TF的GPU模式只支持N卡,然后计算能力高于3.0,具体可以查:安装教程可以参考:Ubuntu16.04上gtx1080的cuda安装July172016目前tensorflow是一个非常流行的深度学习计算框架,常规硬件及系统的安装方法官方的doc已经说的很清楚了,但是因为系统是ubuntu16.04,显卡是GTX1080,所以不可避免的要折腾起来。在上一篇已经在16.04上安装好了驱动。接下来其实重点安装的是CUDA和cuDNN.首先说为什么要安装CUDA和cuDNN,关于采用GPU计算比CPU有速度有多少提升的benchmark找找就有,这次重点是怎么让tensorflow充分用的上GTX1080能力。具体的就是如何把支持GTX1080的CUDA和cuDNN装起来,然后让tensorflow认识我们新装的CUDA和cuDNN。首先总体说下安装步骤:1首先要注册NVIDIAdeveloper的帐号,分别下载CUDA和cuDNN2确认准备gcc版本,安装依赖库sudoapt-getinstallfreegl3安装CUDA4解压cuDNN5clonetensorflow源码,configure配置6编译安装7最后一哆嗦,测试!准备工作在正式开始前,需要做几个准备工作,主要是大概先看下文档cudaFAQtensorflow的安装文档cuda-gpu的支持列表/计算能力/FAQcudnn5.1有多牛cudatookit下载页面CUDA_Installation_Guide_Linux.pdfcudnnUserGuide文档看过之后接下来就是实际动手的过程:1注册NVIDIAdeveloper的帐号,分别下载CUDA和cuDNN1.1下载CUDA打开cudatoolkit下载页面,GTX1080要用的是CUDA8。先点击JOIN,注册帐号。完了后,再回到cudatoolkit下载页面。选择linux,x86-64,ubuntu,16.04,runfile(local)1.2下载cuDNN进入cudnn的下载页,一堆调查,日志写时下载的是[DownloadcuDNNv5(May27,2016),forCUDA8.0RC],点开选linux,不出意外的话这个就是下载地址.2确认GCC版本,安装依赖库确认本机gcc版本,16.04默认的是gcc5,这里安装需要的最高是gcc4.9。接下来就安装配置gcc4.9.2.1安装gcc4.9,并修改系统默认为4.9sudoapt-getinstallgcc-4.9gcc-4.9g++-4.9g++-4.9gcc--versionsudoupdate-alternatives--install/usr/bin/g++g++/usr/bin/g++-4.910sudoupdate-alternatives--install/usr/bin/cccc/usr/bin/gcc30sudoupdate-alternatives--setcc/usr/bin/gccsudoupdate-alternatives--install/usr/bin/c++c++/usr/bin/g++30sudoupdate-alternatives--setc++/usr/bin/g++gcc--version2.2一个小依赖sudoapt-getinstallfreegl3安装CUDA需要注意的是这个地方有个选择安装低版本驱动的地方,选n大致的安装流程如下:3.1安装CUDAchmod+x/cuda_8.0.27_linux.run./cuda_8.0.27_linux.run.DoyouacceptthepreviouslyreadEULA?accept/decline/quit:acceptInstallNVIDIAAcceleratedGraphicsDriverforLinux-x86_64361.62?(y)es/(n)o/(q)uit:nInstalltheCUDA8.0Toolkit?(y)es/(n)o/(q)uit:yEnterToolkitLocation[defaultis/usr/local/cuda-8.0]:Doyouwanttoinstallasymboliclinkat/usr/local/cuda?(y)es/(n)o/(q)uit:yInstalltheCUDA8.0Samples?(y)es/(n)o/(q)uit:yEnterCUDASamplesLocation[defaultis/home/h]:/home/h/Documents/cuda_samples.3.2写入环境变量vim~/.bashrc#添加下面变量exportPATH=/usr/local/cuda-8.0/bin$PATH:+:$PATHexportLD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64$LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH3.3安装好后简单验证a.进入刚配置时指定的cudasample所在文件夹,NVIDIA_CUDA-8.0_Samples/b.cd0_Simple/asyncAPI;sudomakec.NVIDIA_CUDA-8.0_Samples/0_Simple/asyncAPI$./asyncAPI[./asyncAPI]-Starting…GPUDevice0:逗GeForceGTX1080地withcomputecapability6.1CUDAdevice[GeForceGTX1080]timespentexecutingbytheGPU:10.94timespentbyCPUinCUDAcalls:0.19CPUexecuted50591iterationswhilewaitingforGPUtofinish4安装cuDNNh@h:~/Downloads$tarxvzfcudnn-8.0-linux-x64-v5.0-ga.tgzcuda/include/cudnn.hcuda/lib64/libcudnn.socuda/lib64/libcudnn.so.5cuda/lib64/libcudnn.so.5.0.5cuda/lib64/libcudnn_static.ah@h:~/Downloads$sudocp-Rcuda/lib64/usr/local/cuda/lib64h@h:~/Downloads$sudomkdir-p/usr/local/cuda/includeh@h:~/Downloads/cuda$sudocpinclude/cudnn.h/usr/local/cuda/include/sudochmoda+r/usr/local/cuda/include/cudnn.h/usr/local/cuda/lib64/libcudnn*5clone,configuretensorflow5.1clone源码$gitclone5.2configure配置整个配置流程应该跟下面的基本一样的h@h:~/Downloads/tensorflow$cd./tensorflow/h@h:~/Downloads/tensorflow$./configurePleasespecifythelocationofpython.[Defaultis/usr/bin/python]:***DoyouwishtobuildTensorFlowwithGoogleCloudPlatformsupport?[y/N]N***NoGoogleCloudPlatformsupportwillbeenabledforTensorFlow***DoyouwishtobuildTensorFlowwithGPUsupport?[y/N]y***GPUsupportwillbeenabledforTensorFlowPleasespecifywhichgccnvccshoulduseasthehostcompiler.[Defaultis/usr/bin/gcc]:**PleasespecifythelocationwhereCUDAtoolkitisinstalled.RefertoREADME.mdformoredetails.[Defaultis/usr/local/cuda]:/usr/local/cuda-8.0****PleasespecifytheCudnnversionyouwanttouse.[Leaveemptytousesystemdefault]:5.0.5****PleasespecifythelocationwherecuDNN5.0.5libraryisinstalled.RefertoREADME.mdformoredetails.[Defaultis/usr/local/cuda-8.0]:/usr/local/cuda**Pleasespecifyalistofcomma-separatedCudacomputecapabilitiesyouwanttobuildwith.Youcanfindthecomputecapabilityofyourdeviceat:**Pleasenotethateachadditionalcomputecapabilitysignificantlyincreasesyourbuildtimeandbinarysize.[Defaultis:"3.5,5.2"]:6.1**SettingupCudaincludeSettingupCudalib64SettingupCudabinSettingupCudanvvmSettingupCUPTIincludeSettingupCUPTIlib64Configurationfinished6编译安装6.1编译工具Bazel安装配置先看一眼文档然后就执行下面的流程:#安装java1.8sudoadd-apt-repositoryppa:webupd8team/javasudoapt-getupdatesudoapt-getinstalloracle-java8-installer#安装好后车参考下java-version#添加源echo"deb[arch=amd64]stablejdk1.8"|sudotee/etc/apt/sources.list.d/bazel.listcurl|sudoapt-keyadd-#下载sudoapt-getupdate&&sudoapt-getinstallbazel#升级sudoapt-getupgradebazel6.2编译tensorflow的pip版本并安装$bazelbuild-copt//tensorflow/tools/pip_package:build_pip_package#TobuildwithGPUsupport:$bazelbuild-copt--config=cuda//tensorflow/tools/pip_package:build_pip_package$bazel-bin/tensorflow/tools/pip_package/build_pip_package/tmp/tensorflow_pkg#Thenameofthe.whlfilewilldependonyourplatform.#注意编译完成后生成的文件名字和官方doc里面的是不一定一致的$sudopipinstall/tmp/tensorflow_pkg/tensorflow-0.*-linux_x86_64.whli6700k32g编译时间:只编译代码不带pipINFO:Elapsedtime:967.271s,CriticalPath:538.38sbazel-bin/tensorflow/tools/pip_package/build_pip_packageINFO:Elapsedtime:65.183s,CriticalPath:48.587最后测试前面都整完了,现在该测试了,注意前面有两个动态链接库的位置,cuDNN在/usr/local/cuda/lib64,而cuda在/usr/local/cuda-8.0/lib64,所以这个时候的bashrc应该这么写:exportPATH=/usr/local/cuda-8.0/bin$PATH:+:$PATHexportLD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda-8.0/lib64$LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH写完后,source~/.bashrccdtensorflow/tensorflow/models/image/mnistpythonconvolutional.py成功的话会出现流畅的跑动:h@h:~/Downloads/tensorflow/tensorflow/models/image/mnist$pythonconvolutional.pyItensorflow/stream_executor/dso_loader.cc:108]successfullyopenedCUDAlibrarylibcublas.solocallyItensorflow/stream_executor/dso_loader.cc:108]successfullyopenedCUDAlibrarylibcudnn.so.5.0.5locallyItensorflow/stream_executor/dso_loader.cc:108]successfullyopenedCUDAlibrarylibcufft.solocallyItensorflow/stream_executor/dso_loader.cc:108]successfullyopenedCUDAlibrarylibcuda.so.1locallyItensorflow/stream_executor/dso_loader.cc:108]successfullyopenedCUDAlibrarylibcurand.solocallyExtractingdata/train-images-idx3-ubyte.gzExtractingdata/train-labels-idx1-ubyte.gzExtractingdata/t10k-images-idx3-ubyte.gzExtractingdata/t10k-labels-idx1-ubyte.gzItensorflow/stream_executor/cuda/cuda_gpu_executor.cc:925]successfulNUMAnodereadfromSysFShadnegativevalue(-1),buttheremustbeatleastoneNUMAnode,soreturningNUMAnodezeroItensorflow/core/common_runtime/gpu/gpu_init.cc:102]Founddevice0withproperties:name:GeForceGTX1080major:6minor:1memoryClockRate(GHz)1.8475pciBusID0000:01:00.0Totalmemory:7.92GiBFreememory:7.41GiBItensorflow/core/common_runtime/gpu/gpu_init.cc:126]DMA:0Itensorflow/core/common_runtime/gpu/gpu_init.cc:136]0:YItensorflow/core/common_runtime/gpu/gpu_device.cc:838]CreatingTensorFlowdevice(/gpu:0)->(device:0,name:GeForceGTX1080,pcibusid:0000:01:00.0)Initialized!Step0(epoch0.00),8.4msMinibatchloss:12.054,learningrate:0.010000Minibatcherror:90.6%Validationerror:84.6%Minibatcherror:0.0%Validationerror:0.7%Step8500(epoch9.89),4.7msMinibatchloss:1.601,learningrate:0.006302Minibatcherror:0.0%Validationerror:0.9%Testerror:0.8%

以上是关于tensorflow gpu版本运行时怎么知道有没有调用gpu以及cuda加速的主要内容,如果未能解决你的问题,请参考以下文章

Compute Capability 3.0 卡可以运行 Tensorflow 1.8 tensorflow-gpu 运行时吗?

tensorflow只能在装有gpu的机器上运行

Ubuntu下TensorFlow GPU版本的运行

TensorFlow: Use GPU 使用GPU运行TensorFlow

tensorflow怎么删除gpu版本,下载cpu版本

tensorflow 怎么设置成cpu运行