Skip to content

Instantly share code, notes, and snippets.

@fabriciorsf
Forked from jarutis/ubuntu.sh
Last active December 18, 2017 05:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fabriciorsf/b911963d8b71987a236401c49f1b75d6 to your computer and use it in GitHub Desktop.
Save fabriciorsf/b911963d8b71987a236401c49f1b75d6 to your computer and use it in GitHub Desktop.
Theano and Keras setup on ubuntu with OpenCL on AMD card
## install Catalyst proprietary
sudo ntfsfix /dev/sda2
sudo cp /etc/X11/xorg.conf /etc/X11/xorg.conf.BAK
sudo apt-get remove --purge fglrx*
sudo apt-get install linux-headers-generic
sudo apt-get install fglrx xvba-va-driver libva-glx1 libva-egl1 vainfo
sudo amdconfig --initial
## install build essentials
sudo apt-get install g++ cmake
sudo apt-get update && sudo apt-get install build-essential
## install AMD APP SDK
tar -xvf AMD-APP-SDKInstaller-v3.0.130.136-GA-linux64.tar.bz2
sudo ./AMD-APP-SDK-v3.0.130.136-GA-linux64.sh
## install libgpuarray
sudo apt-get install libssl0.9.8:i386
sudo apt-get install libboost-all-dev
sudo apt-get install libgtest-dev
cd /usr/src/gtest
sudo cmake .
sudo make
sudo mv libg* /usr/lib/
## download AMD acml library
mkdir /opt/programs/acml
tar -xvf acml-6.1.0.31-gfortran64.tgz -C /opt/programs/acml-6.1.0.31-gfortran64
cd /opt/programs/ && ln -s acml-6.1.0.31-gfortran64 acml
echo 'export ACML_ROOT=/opt/programs/acml' >> /etc/profile.d/ACML.sh
echo 'export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:"${ACML_ROOT}/gfortran64/lib"' >> /etc/profile.d/ACML.sh
source /etc/profile.d/AMDAPPSDK.sh
source /etc/profile.d/ACML.sh
## clBlas
sudo apt-get install git
mkdir ~/git && cd ~/git
git clone https://github.com/clMathLibraries/clBLAS.git
## at file clBLAS/src/tests/correctness/blas-lapack.c, comment the lines 658, 673, 688 and 703
cd clBLAS/ && mkdir build && cd build
sudo apt-cache search openblas
sudo apt-get install libopenblas-base libopenblas-dev
sudo apt-get install liblapack3gf liblapack-doc liblapack-dev
cmake ../src
make
sudo make install
## Prepare a separate python environment
## To to this, we recomend you install miniconda (we will use CONDA_ROOT to refer to the conda installation path)
conda create --name env_keras python=3 anaconda-client cython h5py nose numpy pip pytz pyyaml scipy
source activate env_keras
pip install dev
## libgpuarray
cd ~/git
git clone https://github.com/Theano/libgpuarray.git
cd libgpuarray
## here, maybe you must upgrade your cmake installation to version 3
## to do this, run the commands commented bellow
#sudo add-apt-repository ppa:george-edison55/cmake-3.x
#sudo apt-get update && sudo apt-get upgrade cmake
mkdir Build && cd Build
cmake .. -DCMAKE_INSTALL_PREFIX=${CONDA_ROOT}/envs/env_keras/ -DCMAKE_BUILD_TYPE=Release -DOPENCL_INCLUDE_DIRS=${AMDAPPSDKROOT}/include
make
make install
cd ..
python setup.py build
sudo update-alternatives --set liblapack.so.3 /usr/lib/lapack/liblapack.so.3
python setup.py install
## To test and check devices, run this commands
python -c "import pygpu; print(pygpu.init('opencl0:0').devname)"
python -c "import pygpu; print(pygpu.init('opencl0:1').devname)"
python -c "import pygpu; print(pygpu.init('opencl0:2').devname)"
## Theano
## here you also should run in a separate python environment
cd ~/git
pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
## An alternative method to install Theano is to clone the repository from GitHub and use the setup.py script
#git clone https://github.com/Theano/Theano
#cd Theano
#python setup.py install
## In order to get Theano (and Keras) to utilize our GPU rather than the CPU, we need to create a file named .theanorc in our home directory (if it does not already exist).
## This file stores various configurations for the Theano library. You can supply these values via command line argument with the THEANO_FLAGS variable; however, I find it more convenient to store global configurations inside the .theanorc file.
## With variable: THEANO_FLAGS=device=opencl0:0 python test.py
## With .theanorc file.
#nano ~/.theanorc
# [global]
# floatX = float32
# device = opencl0:0
## You can use Theano’s check1.py to check that OpenCL is available:
## http://deeplearning.net/software/theano/tutorial/using_gpu.html#id1
## Only use CPU
THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python check1.py
## CPU over OpenCL
THEANO_FLAGS=mode=FAST_RUN,device=opencl0:1,floatX=float32 python check1.py
## AMD GPU over OpenCL
THEANO_FLAGS=mode=FAST_RUN,device=opencl0:0,floatX=float32 python check1.py
## If you need help, see:
## https://www.robberphex.com/2016/05/521
## http://www.pyimagesearch.com/2016/07/18/installing-keras-for-deep-learning/
## https://github.com/Theano/libgpuarray/issues/203
## https://github.com/Theano/Theano/issues/2936
## Keras
pip install keras
## Maybe you have to change keras backend to use Theano (instead of TensorFlow).
## https://keras.io/backend/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment