2017년 6월 18일 일요일

GIT Source로 TensorFlow r1.2 with GPU support 설치하기 (Cuda 8.0 + python 2.7 + cuDNN5.1)

미래에 Tensorflow의 contributor가 되야겠다는 생각을 한다면 GITHUB로 부터 소스를 다운 받아서 설치 할 줄 알아야 한다. 자고로 지식소비자에 머물지 말고 지식생산자가 되야겠다는 마음가짐을 항상 가지는 것이 중요하다고 생각한다.

Tensorflow를 source로 부터 설치하는 과정은 파이썬에서 pip를 이용하여 설치하는 과정 보다 앞에 몇단계가 추가 되는데 그것은 다음과 같다.
  1. GITHUB에서 클론해서 소스코드를 다운받는 단계
  2. 클론 한 소스코드를 자신의 설정에 잘 맞추어 빌드해서 파이썬 package를 생성하는 단계
위의 두 단계를 제외하면 나머지 과정은 pip을 이용하는 설치와 같다.  GIT으로 부터 TensorFlow를 직접 설치하는 경우 아래와 같은 장점을 가질 수 있다.
  • 최신 release를 빠르게 이용할 수 있다.
  • 나의 컴퓨터의 설정에 커스터마이즈 된 빌드를 할 수 있다. (GPU support 설정) 
  • 내가 직접 tensorflow소스를 수정해서 컴파일 해서 테스트 해 볼 수 있다. 
하지만 꼭 소스로 부터 설치하는 것이 좋은 것은 아니며 pip/docker/conda등 다양한 설치 방법을 선택할 수 있다. 이부분은 구글 문서를 참고 하면 좋겠다. 


준비하기 

기본적으로 아래의 상황을 가정한다. 
  • ubuntu 14.04LTS 64bits
  • Nvidia GPU driver 375.66 정상 설치
  • cuda 8.0 정상 설치
  • CuDNN 6.0 정상 설치
  • python 2.7
  • GeForce Titan X 
여기 까지 설치가 안됐다면 다른 사이트를 참고해서 설치해야한다. [1],[5] 그리고 추가적으로 몇가지 파이썬 패키지를 설치할 필요가 있다.

1
sudo apt-get install python-pip python-numpy swig python-dev python-wheel

Bazel의 설치

TensorFlow 설치를 위해서 구글은 Bazel을 사용한다. Bazel이 정확하게 어떤 것인지는 현재 필자도 잘 모르기 때문에 일단 지금은 가져다가 사용하는것으로 넘어간다.  먼저 JDK8을 인스톨 하고 그 뒤에 Bazel을 인스톨하는 것이 순서이다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Installation of  JDK8
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

# Installation of Bazel
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://storage.googleapis.com/bazel-apt/doc/apt-key.pub.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install bazel
sudo apt-get upgrade bazel

GIT에서 TensorFlow 소스 가져오기

GIT에서 클론 기능을 사용하여 소스코드를 가져온다. 원하는 버전의 릴리즈를 가져오면 되는데 필자는 최신 릴리즈인  r1.2를 클론 하였다.


1
2
3
4
git clone https://github.com/tensorflow/tensorflow.git 
cd tensorflow
git init
git checkout origin/r1.2 -t

Bazel로 빌드하고 tensorflow 패키지 설치하기 

Bazel로 빌드 하기전에 TensorFlow 빌드 설정을 해줘야 하는데 "./configure"명령어를 통해서 쉽게 가능하다 [5]. 이 "./configure"설정에서 GPU사용을 위해서 중요하게 확인해야 할 것은
  • CUDA8.0의 사용과 
  • CuDNN 
  • GPU의 computational capability 의 설정이다. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
$ ./configure
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python2.7
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:
Do you wish to use jemalloc as the malloc implementation? [Y/n]
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N]
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N]
No XLA JIT support will be enabled for TensorFlow
Found possible Python library paths:
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python2.7/dist-packages]
Using python library path: /usr/local/lib/python2.7/dist-packages
Do you wish to build TensorFlow with OpenCL support? [y/N] N
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N] Y
CUDA support will be enabled for TensorFlow
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: 8.0
Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify the cuDNN version you want to use. [Leave empty to use system default]: 6
Please specify the location where cuDNN 5 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size.
[Default is: "3.5,5.2"]: 5.2
Setting up Cuda include
Setting up Cuda lib
Setting up Cuda bin
Setting up Cuda nvvm
Setting up CUPTI include
Setting up CUPTI lib64
Configuration finished

설정을 마치고 빌드하기 위해서 아래와 같이 진행한다. 여기서 유의해야할 점은 현재 버전에서는 생성된 패키지 "tensorflow-1.2.0-cp27-cp27mu-linux_x86_64.whl"의 이름을 "tensorflow-1.2.0-cp27-none-linux_x86_64.whl"으로 변경해줘야 pip를 이용해서 인스톨이 가능하다는 것이다.  
1
2
3
4
5
6
7
8
9
# build pip tensorflow package
sudo bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package 

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

# Install the pip package 
cd /tmp/tensorflow_pkg
mv ./tensorflow-1.2.0-cp27-cp27mu-linux_x86_64.whl  ./tensorflow-1.2.0-cp27-none-linux_x86_64.whl 
pip install tensorflow-1.2.0-cp27-none-linux_x86_64.whl 


정상 설치 확인 

우리가 항상 하듯 helloworld로 가볍게 동작을 확인할 수 있다.

hellowTensorFlow.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#-*- coding: utf-8 -*-
#! /usr/bin/env python


import tensorflow as tf

hello   = tf.constant('Hello, TensorFlow!')
sess    = tf.Session()
print '[Lab1] %s' % sess.run(hello)
# Hello, TnesorFlow!
a = tf.constant(10)
b = tf.constant(32)
print '[Lab1] a + b =  %s' % sess.run(a+b)
# 42


실행 결과
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
python helloTensorFlow.py 
2017-06-18 22:37:45.054312: 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-06-18 22:37:45.054852: I tensorflow/core/common_runtime/gpu/gpu_device.cc:940] Found device 0 with properties: 
name: GeForce GTX TITAN X
major: 5 minor: 2 memoryClockRate (GHz) 1.076
pciBusID 0000:01:00.0
Total memory: 11.92GiB
Free memory: 11.81GiB
2017-06-18 22:37:45.054889: I tensorflow/core/common_runtime/gpu/gpu_device.cc:961] DMA: 0 
2017-06-18 22:37:45.054899: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0:   Y 
2017-06-18 22:37:45.054915: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX TITAN X, pci bus id: 0000:01:00.0)
[Lab1] Hello, TensorFlow!
[Lab1] a + b =  42

참고문헌:

[1] https://marcnu.github.io/2016-08-17/Tensorflow-v0.10-installed-from-scratch-Ubuntu-16.04-CUDA8.0RC-cuDNN5.1-1080GTX/
[2] Tensorflow GITHUB: https://github.com/tensorflow/tensorflow/tree/r1.2
[3] 구글 문서 : https://www.tensorflow.org/install/install_linux
[4] 구글 문서: https://www.tensorflow.org/install/install_sources
[5] startupphd 블로그 :  [개발 환경 구축] Ubuntu14.04LTS에 Nvidia GPU driver (375.66) + CUDA 8.0 + CuDNN 6.0 설치하기

댓글 없음:

댓글 쓰기