๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ก EE's DEV/๋จธ์‹ ๋Ÿฌ๋‹

[๋จธ์‹ ๋Ÿฌ๋‹] Ubuntu16.04์—์„œ CUDA, cuDNN, Anaconda, Tensorflow, Pytorch ์„ค์น˜ํ•˜๊ธฐ

by Danna 2017. 12. 20.
728x90
728x90


1. Nvidia ๊ทธ๋ž˜ํ”ฝ ๋“œ๋ผ์ด๋ฒ„ ์„ค์น˜


ํ•ด๋‹นํ•˜๋Š” ๊ทธ๋ž˜ํ”ฝ์นด๋“œ์™€ ํ˜ธํ™˜์ด ๋˜๋Š” 367.4x๋ฒ„์ „ ์ด์ƒ์˜ ์ตœ์‹  ๋ฒ„์ „์˜ ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์„ค์น˜ํ•œ๋‹ค.


$ sudo add-apt-repository ppa:graphics-drivers/ppa

$ sudo apt-get update

$ sudo apt-get install nvidia-375


์„ค์น˜๊ฐ€ ๋๋‚œ ํ›„ ์žฌ๋ถ€ํŒ…ํ•œ๋‹ค.


$ sudo reboot


๋“œ๋ผ์ด๋ฒ„ ๋ฒ„์ „๊ณผ ๊ทธ๋ž˜ํ”ฝ์นด๋“œ์˜ ์ •๋ณด๊ฐ€ ๋‚˜์˜ค๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.


$ nvidia-smi
Wed Dec 20 15:40:51 2017      
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.98                 Driver Version: 384.98                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 960     Off  | 00000000:01:00.0  On |                  N/A |
| 22%   44C    P5    13W / 160W |    251MiB /  1993MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                              
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1039      G   /usr/lib/xorg/Xorg                           169MiB |
|    0      1778      G   compiz                                        76MiB |
|    0      2043      G   /usr/lib/firefox/firefox                       1MiB |
+-----------------------------------------------------------------------------+





2. CUDA Toolkit v8.0 ์„ค์น˜


NVIDIA ์‚ฌ์ดํŠธ์—์„œ ํ˜„์žฌ ์ตœ์‹ ๋ฒ„์ „์€ v9.1์ด๋ฏ€๋กœ developer.nvidia.com/cuda-80-ga2-download-archive ์—์„œ 8.0 ๋ฒ„์ „์„ ์„ค์น˜ํ•œ๋‹ค. opengl์„ ์„ค์น˜ํ•˜์ง€ ์•Š์•„์•ผ ๋ฌดํ•œ๋ถ€ํŒ… ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š๋Š”๋‹ค. Ctrl+Alt+F1 ์œผ๋กœ ์ปค๋งจ๋“œ ๋ชจ๋“œ์— ์ ‘์†ํ•ด ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. (lightdm stop ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜๋ฉด GUI๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ) ์ปค๋งจ๋“œ ๋ชจ๋“œ์— ์ ‘์†ํ•˜๊ธฐ ์ „์— run ํŒŒ์ผ์„ /home/์œ ์ €๋ช…/ ํด๋”๋กœ ์˜ฎ๊ฒจ๋‘๋Š” ๊ฒƒ์ด ํŽธํ•˜๋‹ค.


$ chmod 777 cuda_8.0.61_375.26_linux.run

$ sudo service lightdm stop

$ sudo ./cuda_8.0.61_375.26_linux.run --no-opengl-libs

์ด๋ฏธ ๊ทธ๋ž˜ํ”ฝ ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์„ค์น˜ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— NVIDIA Driver๋ฅผ ์„ค์น˜ํ•œ๋‹ค๋Š” ์งˆ๋ฌธ์— no๋ผ๊ณ  ์„ ํƒํ•œ๋‹ค. ์„ค์น˜๊ฐ€ ๋‹ค ๋œ ํ›„์—๋Š” ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •์„ ํ•œ๋‹ค.


$ vi ~/.bashrc


๋งจ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์— ์•„๋ž˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•œ๋‹ค. {PATH}์—๋Š” ๊ธฐ์กด์— ์žˆ๋Š” ๋‚ด์šฉ์„ ์œ ์ง€ํ•˜๋ฉด ๋œ๋‹ค.


export PATH = /usr/local/cuda/bin:${PATH}

export LD_LIBRARY_PATH = /usr/local/cuda/lib64:${LD_LIBRARY_PATH}


๋ณ€๊ฒฝ๋œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์ ์šฉํ•˜๊ณ  cuda ์„ค์น˜์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•œ๋‹ค.


$ source ~/.bashrc

$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Tue_Jan_10_13:22:03_CST_2017
Cuda compilation tools, release 8.0, V8.0.61


์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด cuda๊ฐ€ ์–ด๋Š ํด๋” ์ €์žฅ๋˜์–ด ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.


$ which nvcc
/usr/local/cuda/bin/nvcc




3. cuDNN v6.0 ์„ค์น˜


NVIDIA ์‚ฌ์ดํŠธ์—์„œ cuDNN v6.0 for CUDA 8.0 ์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๋Š”๋‹ค. cuDNN v6.0 Library for Linux๋ฅผ ๋‹ค์šด๋ฐ›์œผ๋ฉด ๋œ๋‹ค.


$ tar xzvf cudnn-8.0-linux-x64-v6.0.tgz

$ sudo cp cuda/lib64/* /usr/local/cuda/lib64/

$ sudo cp cuda/include/* /usr/local/cuda/include/
$ sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
$ sudo chmod a+r /usr/local/cuda/include/cudnn.h



๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์˜ค๋ฉด ์„ค์น˜๊ฐ€ ์ž˜ ๋œ ๊ฒƒ์ด๋‹ค.


$ cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
#define CUDNN_MAJOR      6
#define CUDNN_MINOR      0
#define CUDNN_PATCHLEVEL 21
--
#define CUDNN_VERSION    (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

#include "driver_types.h"




+ NVIDIA CUDA Profiler Tools Interface ์„ค์น˜


$ sudo apt-get install libcupti-dev



+ Bazel ์„ค์น˜


$ sudo add-apt-repository ppa:webupd8team/java

$ sudo apt-get update

$ sudo apt-get install oracle-java8-installer


$ echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list

$ sudo apt install curl

$ curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -


$ sudo apt-get update && sudo apt-get install bazel

$ bazel version
Build label: 0.9.0
Build target: bazel-out/k8-fastbuild/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue Dec 19 09:31:58 2017 (1513675918)
Build timestamp: 1513675918
Build timestamp as int: 1513675918




4. Anaconda Python 3.6 ์„ค์น˜


Anaconda ์‚ฌ์ดํŠธ์—์„œ ํŒŒ์ด์ฌ 3.6๋ฒ„์ „์˜ 64bit ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๋Š”๋‹ค. ์„ค์น˜๋ฅผ ํ•˜๋ฉด์„œ ๋งˆ์ง€๋ง‰์— ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์ ์šฉํ•œ๋‹ค๋Š” ์งˆ๋ฌธ์— yes ๋กœ ๋‹ตํ•ด์•ผ ํ•œ๋‹ค. ์„ค์น˜ ํ›„์—๋Š” ๋ณ€๊ฒฝ๋œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์ ์šฉํ•œ๋‹ค.


$ bash Anaconda3-5.0.1-Linux-x86_64.sh

$ source ~/.bashrc



ํŒŒ์ด์ฌ์„ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด Anaconda๊ฐ€ ๋œฌ๋‹ค.


$ python
Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>





5. Tensorflow ์„ค์น˜


Anaconda์— ์ด๋ฏธ tensorflow๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ GPU๋ฒ„์ „๋งŒ ๋‹ค์‹œ ์„ค์น˜ํ•ด์ค€๋‹ค.


// pip install ๋กœ ์„ค์น˜ํ–ˆ์„ ๋•Œ์—๋Š” RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6return f(*args, **kwds) ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๊ธฐ ๋•Œ๋ฌธ์— uninstall ํ•˜๊ณ  ๋‹ค์‹œ ์„ค์น˜ํ•ด์ฃผ์—ˆ์Œ.


$ conda search tensorflow

$ conda install tensorflow-gpu

$ python
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.Session()
2017-12-20 17:34:58.514172: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-12-20 17:34:58.514223: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-12-20 17:34:58.514241: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-12-20 17:34:58.514256: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-12-20 17:34:58.514283: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-12-20 17:34:58.763001: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:893] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2017-12-20 17:34:58.763244: I tensorflow/core/common_runtime/gpu/gpu_device.cc:955] Found device 0 with properties:
name: GeForce GTX 960
major: 5 minor: 2 memoryClockRate (GHz) 1.304
pciBusID 0000:01:00.0
Total memory: 1.95GiB
Free memory: 1.65GiB
2017-12-20 17:34:58.763256: I tensorflow/core/common_runtime/gpu/gpu_device.cc:976] DMA: 0
2017-12-20 17:34:58.763260: I tensorflow/core/common_runtime/gpu/gpu_device.cc:986] 0:   Y
2017-12-20 17:34:58.763266: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 960, pci bus id: 0000:01:00.0)
<tensorflow.python.client.session.Session object at 0x7f63ba279828>




6. Pytorch ์„ค์น˜


$ conda install pytorch torchvision -c pytorch

$ python
Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> A = torch.rand(3, 4)
>>> print(A)

 0.7512  0.0381  0.1706  0.3747
 0.5672  0.5980  0.3960  0.6589
 0.6865  0.3870  0.0666  0.7997
[torch.FloatTensor of size 3x4]



17.12.20 ์ž‘์„ฑ.


์„ค์น˜ํ•˜๋ฉด์„œ ์ฐธ๊ณ ํ•œ ์‚ฌ์ดํŠธ

1 ejklike.github.io/2017/03/06/install-tensorflow1.0-on-ubuntu16.04-1.html

2 newsight.tistory.com/92

3 tensorflow.blog/tag/pytorch/


728x90
728x90

'๐Ÿ’ก EE's DEV > ๋จธ์‹ ๋Ÿฌ๋‹' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Tensorflow] MLP(Multi Layer Perceptron) classification with MNIST data  (0) 2018.09.24