Contents
  1. 1. Software in Ubuntu 18.04
  2. 2. TensorFlow for CPU
  3. 3. TensorFlow for ROCm
    1. 3.1. Install ROCm
    2. 3.2. Install TensorFlow ROCm
  4. 4. TensorFlow for CUDA
    1. 4.1. Install Nvidia CUDA driver
    2. 4.2. Install CUDA Toolkit
    3. 4.3. Install cuDNN (Optional)
    4. 4.4. Install TensorFlow CUDA
  5. 5. Verify TensorFlow by hello example
  6. 6. Issues

How to install TensorFlow. (Verified in Ubuntu 18.04)

Software in Ubuntu 18.04

  • tensorflow 1.12.0
  • ROCm 2.0.89
  • CUDA Toolkit 9.0(required by tensorflow-gpu in 1.12)
  • Python3 3.6.5 is installed by default
    1
    2
    3
    4
    sudo apt install python3-pip

    The following NEW packages will be installed:
    dh-python libexpat1-dev libpython3-dev libpython3.6-dev python-pip-whl python3-dev python3-distutils python3-lib2to3 python3-pip python3-setuptools python3-wheel python3.6-dev

TensorFlow for CPU

1
2
3
4
pip3 install tensorflow

Installing collected packages: astor, six, numpy, keras-preprocessing, h5py, keras-applications, setuptools, protobuf, wheel, markdown, werkzeug, grpcio, tensorboard, gast, termcolor, absl-py, tensorflow
Successfully installed absl-py-0.6.1 astor-0.7.1 gast-0.2.0 grpcio-1.17.1 h5py-2.9.0 keras-applications-1.0.6 keras-preprocessing-1.0.5 markdown-3.0.1 numpy-1.15.4 protobuf-3.6.1 setuptools-40.6.3 six-1.12.0 tensorboard-1.12.1 tensorflow-1.12.0 termcolor-1.1.0 werkzeug-0.14.1 wheel-0.32.3

TensorFlow for ROCm

Install ROCm

Install TensorFlow ROCm

  • Install miopen

    1
    sudo apt install rocm-libs miopen-hip cxlactivitylogger
  • Install tensorflow-rocm whl package from PyPI

    1
    2
    3
    4
    pip3 install --user tensorflow-rocm

    Installing collected packages: wheel, six, setuptools, protobuf, markdown, grpcio, werkzeug, numpy, tensorboard, gast, keras-preprocessing, absl-py, astor, termcolor, h5py, keras-applications, tensorflow-rocm
    Successfully installed absl-py-0.6.1 astor-0.7.1 gast-0.2.0 grpcio-1.17.1 h5py-2.9.0 keras-applications-1.0.6 keras-preprocessing-1.0.5 markdown-3.0.1 numpy-1.15.4 protobuf-3.6.1 setuptools-40.6.3 six-1.12.0 tensorboard-1.12.1 tensorflow-rocm-1.12.0 termcolor-1.1.0 werkzeug-0.14.1 wheel-0.32.3

TensorFlow for CUDA

Install Nvidia CUDA driver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sudo apt install nvidia-cuda-dev


The following additional packages will be installed:
libaccinj64-9.1 libcublas9.1 libcudart9.1 libcufft9.1 libcufftw9.1 libcuinj64-9.1 libcurand9.1 libcusolver9.1 libcusparse9.1 libnppc9.1 libnppial9.1 libnppicc9.1 libnppicom9.1 libnppidei9.1
libnppif9.1 libnppig9.1 libnppim9.1 libnppist9.1 libnppisu9.1 libnppitc9.1 libnpps9.1 libnvblas9.1 libnvgraph9.1 libnvidia-compute-390 libnvrtc9.1 libnvtoolsext1 libnvvm3 libthrust-dev libvdpau-dev
Suggested packages:
nvidia-cuda-toolkit libvdpau-doc
Recommended packages:
libnvcuvid1
The following NEW packages will be installed:
libaccinj64-9.1 libcublas9.1 libcudart9.1 libcufft9.1 libcufftw9.1 libcuinj64-9.1 libcurand9.1 libcusolver9.1 libcusparse9.1 libnppc9.1 libnppial9.1 libnppicc9.1 libnppicom9.1 libnppidei9.1
libnppif9.1 libnppig9.1 libnppim9.1 libnppist9.1 libnppisu9.1 libnppitc9.1 libnpps9.1 libnvblas9.1 libnvgraph9.1 libnvidia-compute-390 libnvrtc9.1 libnvtoolsext1 libnvvm3 libthrust-dev libvdpau-dev
nvidia-cuda-dev

Install CUDA Toolkit

It includes libs required by tensorflow-gpu, e.g. libcublas.
In tensorflow 1.12, it requires libcublas:9.0. By default in Ubuntu18.04 it has libcublas:9.1, not supported.

  • Download the CUDA toolkit

  • Install dependent libs

    1
    sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
  • Install cuda 9.0 toolkit

    1
    2
    3
    4
    5
    6
    sudo chmod 777 cuda_9.0.176_384.81_linux.run
    sudo ./cuda_9.0.176_384.81_linux.run -toolkit -samples -silent -override

    (Installer must be in silent mode to use the toolkit, samples or driver command-line flags. Please also pass -silent to the installer.)

    lrwxrwxrwx 1 root root 19 Dec 25 13:10 /usr/local/cuda -> /usr/local/cuda-9.0
    • Remove cuda 9.0 toolkit
      1
      sudo /usr/local/cuda/bin/uninstall_cuda_9.0.pl
  • Set environment variables

    1
    2
    export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
    export LD_LIBRARY_PATH=/usr/local/cuda/lib64 ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}

Install cuDNN (Optional)

  • Download cuDNN v7.0.5 (Dec 5, 2017), for CUDA 9.0

    • cuDNN Archive
    • cuDNN v7.0.5 Runtime Library for Ubuntu16.04 (Deb)
    • cuDNN v7.0.5 Developer Library for Ubuntu16.04 (Deb)
    • cuDNN v7.0.5 Code Samples and User Guide for Ubuntu16.04 (Deb)
      1
      sudo dpkg -i .deb
  • Install dependent libs for ministCUDNN

    1
    sudo apt-get install libfreeimage3 libfreeimage-dev

Install TensorFlow CUDA

  • Install tensorflow-gpu

    1
    2
    3
    4
    pip3 install tensorflow-gpu

    Installing collected packages: numpy, six, h5py, keras-applications, wheel, werkzeug, grpcio, setuptools, protobuf, markdown, tensorboard, astor, gast, absl-py, keras-preprocessing, termcolor, tensorflow-gpu
    Successfully installed absl-py-0.6.1 astor-0.7.1 gast-0.2.0 grpcio-1.17.1 h5py-2.9.0 keras-applications-1.0.6 keras-preprocessing-1.0.5 markdown-3.0.1 numpy-1.15.4 protobuf-3.6.1 setuptools-40.6.3 six-1.12.0 tensorboard-1.12.1 tensorflow-gpu-1.12.0 termcolor-1.1.0 werkzeug-0.14.1 wheel-0.32.3
  • Install libcupti-dev

    1
    2
    sudo apt-get install libcupti-dev
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/extras/CUPTI/lib64

Verify TensorFlow by hello example

1
2
3
4
5
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow')
sess = tf.Session()
# show gpu support if anyG
sess.run(hello)
  • CPU

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $ python3
    Python 3.6.7 (default, Oct 22 2018, 11:32:17)
    [GCC 8.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import tensorflow as tf
    >>> hello = tf.constant('Hello, TensorFlow')
    >>> sess = tf.Session()
    2018-12-25 10:51:13.340471: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
    >>> sess.run(hello)
    b'Hello, TensorFlow'
    >>>
  • ROCm

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    $ python3
    Python 3.6.7 (default, Oct 22 2018, 11:32:17)
    [GCC 8.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import tensorflow as tf
    >>> hello = tf.constant('Hello, TensorFlow')
    >>> sess = tf.Session()
    2018-12-25 11:10:41.247769: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
    2018-12-25 11:10:41.249605: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1530] Found device 0 with properties:
    name: Vega [Radeon RX Vega]
    AMDGPU ISA: gfx900
    memoryClockRate (GHz) 1.59
    pciBusID 0000:03:00.0
    Total memory: 7.98GiB
    Free memory: 7.73GiB
    2018-12-25 11:10:41.249675: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1641] Adding visible gpu devices: 0
    2018-12-25 11:10:41.249722: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1051] Device interconnect StreamExecutor with strength 1 edge matrix:
    2018-12-25 11:10:41.249749: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1057] 0
    2018-12-25 11:10:41.249766: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1070] 0: N
    2018-12-25 11:10:41.249865: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1189] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 7524 MB memory) -> physical GPU (device: 0, name: Vega [Radeon RX Vega], pci bus id: 0000:03:00.0)
    >>> sess.run(hello)
    b'Hello, TensorFlow'
  • CUDA(Not verified yet)

Issues

error: import tensorflow

1
2
ImportError: libCXLActivityLogger.so: cannot open shared object file: No such file or directory
ImportError: libhiprand.so.1: cannot open shared object file: No such file or directory
  • fix: install ROCm libs
    1
    sudo apt install rocm-libs miopen-hip cxlactivitylogger

Error: import tensorflow

1
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory
  • Fix: install CUDA Toolkit 9.0 when use CUDA stack

Error: Import keras

1
No module named 'tensorflow.python.framework')
  • Fix: re-install any of tensorflow, as it’s a common lib for all tensorflow backend.
    1
    2
    3
    pip3 install tensorflow
    pip3 install tensorflow-gpu
    pip3 install tensorflow-rocm

Error: import tensorflow

1
2
3
python3: Relink '/lib/x86_64-linux-gnu/libudev.so.1' with '/lib/x86_64-linux-gnu/librt.so.1' for IFUNC symbol 'clock_gettime'
Segmentation fault (core dumped)
exit python3
  • Fix: install nvidia cuda driver, including nvidia-cuda-dev
    1
    sudo apt install python3-pycuda

Which tensorflow is used? gpu or rocm

  • install tensorflow-rocm again, switching to tensorflow-rocm by default
  • if tensorflow-gpu is installed along with tensorflow-rocm, we need to install cuda toolkit and nvidia cuda driver, as tensorflow-gpu is called now.
Contents
  1. 1. Software in Ubuntu 18.04
  2. 2. TensorFlow for CPU
  3. 3. TensorFlow for ROCm
    1. 3.1. Install ROCm
    2. 3.2. Install TensorFlow ROCm
  4. 4. TensorFlow for CUDA
    1. 4.1. Install Nvidia CUDA driver
    2. 4.2. Install CUDA Toolkit
    3. 4.3. Install cuDNN (Optional)
    4. 4.4. Install TensorFlow CUDA
  5. 5. Verify TensorFlow by hello example
  6. 6. Issues