텐서플로우, 직접 소스 빌드하여 설치하기

개요

노트북에서 텐서플로우를 깔아 CPU만으로 사용하는데 아래와 같은 Warning Message가 뜨는 것을 발견했다. 실행해는 문제가 없는데 개발자들이면 들어봤을 SSE 명령어 셋등이 제대로 실행이 안될것을 생각하니 왠지 컴퓨터 성능을 십분 발휘하지 못한다는 생각이 들었다. 따라서 해결책을 검색해보니 텐서프로우 소스를 본인 PC환경해서 직접빌드하면 해결을 할 수 있다고 하여 내용을 살펴보고 정리하여 포스팅 한다.

텐서플로우 바이너리를 통한 설치는 다른 포스팅을 참고하면 좋을 듯 하다.

텐서플로우 실행시 경고 메세지

1
2
3
4
5
6
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
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.
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.
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.
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.
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.

관련 Github 이슈

https://github.com/tensorflow/tensorflow/issues/7693에서 정보를 찾을 수 있었다.

해결방법

텐서플로우 소스를 직접 받아 로컬 환경에서 빌드하면 --config=opt 옵션을 주고 빌드하면 본인의 로컬 환경에 맞는 텐서플로우 바이너리를 생성 할 수 있다.

텐서플로우 소스를 빌드하여 설치하는 동영상

동영상으로 친절하게 설치를 진행하는 동영상을 공유한다. 이 동영상을 참고하는것이 가장 쉽고 필요한 부분은 아래의 코드스니펫을 copy & paste 하자.

소스 빌드 방법 정리

위 동영상과 텐서플로우 소스 빌드를 참고하여 설치를 진행하면 되지만 copy & paste 및 정보접근을 용이하기 위해 내용을 아래에 정리한다.

아래 설치는 파이썬 2.7 + CPU 사용의 가이드다. 파이썬 3.x는 아래에서 configure 명령 실행시 파이썬 위치를 3.x로 바꾸어 주면 된다. GPU 설치는 위의 링크를 참고하자.

텐서플로우 소스 클론

텐서플로우 소스를 직접 클론 받아 원하는 브랜치로 체크아웃 하면 된다. 필요한 사람은 1.1.0이 RC니까 그 버전으로 빌드해도 된다.

1
2
3
$ git clone https://github.com/tensorflow/tensorflow
$ cd tensorflow
$ git checkout r1.0

Bazel 설치

Bazel 설치는 텐서플로우 홈페이지에 나온대로 링크를 따라가 설치하면 된다.

파이썬 의존성 설치

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

텐서플로우 빌드하기

1
2
$ cd tensorflow  # cd to the top-level directory created
$ ./configure

bazel 빌드하기

bazel 빌드를 돌린다.

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

파이썬 whl 만들기

bazel 빌드 후 파이썬 pip설치용 whl을 만들어 /tmp/tensorflow_pkg 경로에 생성한다.

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

아래와 같이 확인해보면 whl이 생성된 것을 확인 할 수 있다.

1
2
$ ls /tmp/tensorflow_pkg/
tensorflow-1.0.1-cp27-cp27mu-linux_x86_64.whl

virtualenv activate

기본 텐서플로우의 권장 사항인 virtualenv환경으로 들어간다. virtualenv를 사용하지 않는다면 넘어가도 된다.

1
$ source ~/tensorflow/bin/activate

pip로 텐서플로우 설치

pip를 통해 빌드된 텐서플로우를 설치한다.

1
$ pip install /tmp/tensorflow_pkg/tensorflow-1.0.1-cp27-cp27mu-linux_x86_64.whl

텐서플로우 설치 확인하기

파이썬을 실행하여 텐서플로우가 제대로 설치되었는지 확인해보자.

1
$ python

아래 구문에 에러가 나지 않으면 제대로 설치가 된 것이다.

1
>>> import tensorflow as tf


Popit은 페이스북 댓글만 사용하고 있습니다. 페이스북 로그인 후 글을 보시면 댓글이 나타납니다.