728x90
반응형

 

Python 공식 홈페이지에서 제공하는 우분투 환경에서 파이썬을 사용하기 위해 

CUDA 10.0 버전을 기반으로 TensorFlow 를 소스빌드 하는 과정에 대한 포스팅이다. 

 

참고로 윈도우 환경에서 소스빌드 하고 싶다면 아래 링크 참고

https://www.pytorials.com/install-tensorflow-gpu-windows/

 

참고로 공식적으로 빌드된 pip 패키지를 통해서 다운 받고 싶다면 아래 명령어 실행

$ pip install tensorflow

 

 

 

 

TensorFlow 를 직접 소스 빌드하는 방법은 아래와 같다.

 

 

 

 

 

구성환경

TensorFlow 1.12 GPU version

CUDA 10.0 

cuDNN 7.3.1

Computer Capability > 3.0

64 bit Python (32 bit x)

NCCL 2.3.5

 

Nvidia Driver 와 CUDA 10.0 및 cuDNN 7.3.1 버전이 이미 설치된 상태여야한다.

 

 

 

 

 

 

 

 

1. 시스템 업데이트

$ sudo apt-get update 
$ sudo apt-get upgrade

 

2. Dependency 설치

$ sudo apt-get install build-essential 

$ sudo apt-get install cmake git unzip zip

$ sudo apt-get install python-dev python3-dev python-pip python3-pip

 

3. CUDA 및 Nvidia Driver 설치 확인

$ nvidia-smi 

 

 

4. NCCL 2.3.5

 

NCCL 2.3.5 버전 다운로드 (설문조사 완료 후, 다운로드)

https://developer.nvidia.com/nccl/nccl-download

 

NCCL 2.3.5 버전 설치 

$ tar -xf nccl_2.3.5-2+cuda10.0_x86_64.txz

$ cd nccl_2.3.5-2+cuda10.0_x86_64

$ sudo cp -R * /usr/local/cuda-10.0/targets/x86_64-linux/

$ sudo ldconfig

 

 

5. Dependency 설치

$ pip install -U --user pip six numpy wheel mock

$ pip3 install -U --user pip six numpy wheel mock

$ pip install -U --user keras_applications==1.0.5 --no-deps

$ pip3 install -U --user keras_applications==1.0.5 --no-deps

$ pip install -U --user keras_preprocessing==1.0.3 --no-deps

$ pip3 install -U --user keras_preprocessing==1.0.3 --no-deps

 

로컬 환경이 아닌 가상 환경이라면 아래와 같이 설치

$ pip install -U pip six numpy wheel mock

$ pip install -U keras_applications==1.0.5 --no-deps

$ pip install -U keras_preprocessing==1.0.3 --no-deps

 

 

 

6. Bazel 을 이용한 소스 빌드 

 

6.1 Bazel 다운로드

$ cd ~/

$ wget https://github.com/bazelbuild/bazel/releases/download/0.17.2/bazel-0.17.2-installer-linux-x86_64.sh

$ chmod +x bazel-0.17.2-installer-linux-x86_64.sh

$ ./bazel-0.17.2-installer-linux-x86_64.sh --user

$ echo 'export PATH="$PATH:$HOME/bin"' >> ~/.bashrc

 

위에서 지정한 환경 변수 Reload

$ source ~/.bashrc
$ sudo ldconfig

 

6.2 빌드 설정

$ cd ~/

$ git clone https://github.com/tensorflow/tensorflow.git

$ cd tensorflow

$ git checkout r1.12

$ ./configure

r1.12 는 1.12 버전이라는 뜻임

텐서플로우 릴리즈 노트 참고

https://github.com/tensorflow/tensorflow/releases

 

tensorflow/tensorflow

An Open Source Machine Learning Framework for Everyone - tensorflow/tensorflow

github.com

 

 

여기서부터 이제 오타내지 말고 잘 입력해야함

Please specify the location of python. [Default is /usr/bin/python]: 
/usr/bin/python3
Do you wish to build TensorFlow with Apache Ignite support? [Y/n]: Y

Do you wish to build TensorFlow with XLA JIT support? [Y/n]: Y

Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: N

Do you wish to build TensorFlow with ROCm support? [y/N]: N

Do you wish to build TensorFlow with CUDA support? [y/N]: Y
Please specify the CUDA SDK version you want to use. 
[Leave empty to default to CUDA 9.0]:
10.0
Please specify the location where CUDA 10.0 toolkit is installed. 
Refer to README.md for more details. 
[Default is /usr/local/cuda]: 
/usr/local/cuda-10.0
Please specify the cuDNN version you want to use. 
[Leave empty to default to cuDNN 7]: 
7.3.1
Please specify the location where cuDNN 7 library is installed. 
Refer to README.md for more details. 
[Default is /usr/local/cuda-10.0]: 
/usr/local/cuda-10.0
Do you wish to build TensorFlow with TensorRT support? [y/N]: N
Please specify the NCCL version you want to use. 
If NCCL 2.2 is not installed, then you can use version 1.3 
that can be fetched automatically 
but it may have worse performance with multiple GPUs. 
[Default is 2.2]: 
2.3.5
Please note that each additional compute capability significantly increases your build time and binary size. 
[Default is: 5.0] 5.0
Do you want to use clang as CUDA compiler? [y/N]: N

Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: /usr/bin/gcc

Do you wish to build TensorFlow with MPI support? [y/N]: N

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: -march=native

Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]:N

 

설정 완료!

 

 

6.3 빌드 시작

$ bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

여기에 덧붙일 수 있는 옵션이 있다.

 add "--config=mkl" if you want Intel MKL support for newer intel cpu for faster training on cpu

 add "--config=monolithic" if you want static monolithic build (try this if build failed)

 add "--local_resources 2048,.5,1.0" if your PC has low ram causing Segmentation fault or other related errors

빌드는 약 3~4 시간 소요될 수 있다.

Segmentation Fault 에러가 날 경우 다시 빌드를 시작해 볼 것

 

 

 

6.4 whl 파일 생성하기

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

 

위 명령을 수행하면 tensorflow_pkg 폴더에 whl 파일이 생성된다.

$ cd tensorflow_pkg

 

 

 

6.5 whl 파일과 pip 를 이용하여 TensorFlow 패키지 설치

 

6.5.1 가상 환경에서의 설치

$ pip install tensorflow*.whl

6.5.2 가상환경 생성 후 설치

$ sudo apt-get install virtualenv
$ virtualenv tf_1.12_cuda10.0 -p /usr/bin/python3
$ source tf_1.12_cuda10.0/bin/activate
$ pip install tensorflow*.whl

6.5.3 로컬 및 루트 환경에서 Python2 에 설치

$ pip2 install tensorflow*.whl

6.5.4 로컬 및 루트 환경에서 Python2 에 설치

$ pip3 install tensorflow*.whl

 

 

 

 

7. TensorFlow 설치 확인

$ python

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

 

 

 

 

 

 

참고자료 1

https://www.pytorials.com/how-to-install-tensorflow-gpu-with-cuda-10-0-for-python-on-ubuntu/1

 

How to install Tensorflow with CUDA 10 | pytorials.com

This is going to be a tutorial on how to install tensorflow 1.12 GPU version. We will also be installing CUDA 10 and cuDNN 7.3.1 ...

www.pytorials.com

참고자료 2

https://www.pytorials.com/how-to-install-tensorflow-gpu-with-cuda-10-0-for-python-on-ubuntu/2

 

How to install Tensorflow with CUDA 10 | pytorials.com

This is going to be a tutorial on how to install tensorflow 1.12 GPU version. We will also be installing CUDA 10 and cuDNN 7.3.1 ...

www.pytorials.com

참고자료 3

https://github.com/tensorflow/tensorflow.git

 

tensorflow/tensorflow

An Open Source Machine Learning Framework for Everyone - tensorflow/tensorflow

github.com

 

 

728x90
반응형