pytorch c++ 编译

最近在搞pytorch c++ inference,需要编译pytorch lib库。

折腾了几次,终于搞定,这里记录下来。

clone源码

git clone https://github.com/pytorch/pytorch.git

git checkout tags/v1.3.1

git checkout -b v1.3.1

git submodule sync

git submodule update –init –recursive

准备环境,miniconda、cmake(3.5+)、cuda 9.0+

miniconda安装

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

sh Miniconda3-latest-Linux-x86_64.sh

cmake安装

curl -O https://github.com/Kitware/CMake/releases/download/v3.15.5/cmake-3.15.5.tar.gz

./configure –prefix=install_dir

cuda使用yum安装

安装devtoolset-7,pytorch编译对gcc有要求,不然会出现编译错误,参考https://github.com/pytorch/builder/blob/master/update_compiler.sh

yum install -y -q devtoolset-7-gcc devtoolset-7-gcc-c++ devtoolset-7-gcc-gfortran devtoolset-7-binutils

source /opt/rh/devtoolset-7/enable

源码编译,可以根目录直接make,这里选择手动安装到指定目录

进入torch根目录,

mkdir build

cmake .. -DPYTHON_EXECUTABLE:FILEPATH=/search/privateData/tools/miniconda3/bin/python -DPYTHON_INCLUDE_DIR=/search/privateData/tools/miniconda3/include/python3.7m -DCMAKE_INSTALL_PREFIX=/search/privateData/tools/libtorch-1.3.0

marvin安装与使用

 
cuDNN安装

cp lib* cudnn_dir/lib64/
cp cudnn.h cudnn_dir/include/
cd cudnn_dir
export LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH

如出现error while loading shared libraries: libcudnn.so.4: cannot open shared object file: No such file or directory错误,是文件权限问题,可进行如下操作

cd cudnn_dir
rm -rf libcudnn.so libcudnn.so.4
chmod u=rwx,g=rx,o=rx libcudnn.so.4.0.4
ln -s libcudnn.so.4.0.4 libcudnn.so.4
ln -s libcudnn.so.4 libcudnn.so

 
marvin依赖cuda 7.5和cuDNN 4rc
curl -L https://github.com/PrincetonVision/marvin/tarball/master | tar zx
mv PrincetonVision* marvin && cd marvin
./compile.sh

深度学习环境搭建(以caffe+cudnn为例,无需root权限)

(这里在redhat 6.3上安装,无root权限,全程使用源码编译方式安装)
1、系统环境与权限: GTX780,redhat 6.3, gcc 4.4.6, 无root权限
2、所需依赖库:
(1) cuda 6.5(推荐),6.0, 5.5, 5.0以及cuda6.0对应的驱动,或者cuda 5对应驱动319.*(not 331.*)
注:cuda驱动安装需要root权限,我这里安装cuda 6.5, 至少需要340.*以上驱动(6.0及以下没有测试过)
(2)BLAS(ATLAS, MKL,OpenBLAS)括号中三选一。 我这里选择了系统管理员已经默认安装的Intel MKL
(3)OpenCV(>=2.4)
(4)Boost(>= 1.55)(其实只能安装1.55,后面会提到)
(5) glog, gflags, protobuf, leveldb, snappy, hdf5, lmdb
(6) Python 2.7, numpy(>= 1.7)
(7) MATLAB
(Python和Matlab应该是可选的,我只安装了python)
3、依赖库安装
cuda、python安装比较简单,intel MKL已默认安装,不再介绍

#protobuf
tar zxvf protobuf-2.6.1.tar.gz
cd protobuf-2.6.1
chmod a+x autogen.sh
./autogen.sh
./configure -PREFIX=intstall_dir
make && make install
#leveldb
unzip leveldb-master.zip
cd leveldb-master
chmod a+x build_detect_platform
./build_detect_platform
make
#snappy
unzip snappy-master.zip
cd snappy-master
chmod a+x autogen.sh
./autogen.sh
./configure -PREFIX=install_dir
make && make install
#hdf5
tar zxvf hdf5-1.8.14.tar.gz
cd hdf5-1.8.14
./configure --PREFIX=install_dir
make && make install
# glog
tar zxvf glog-0.3.3.tar.gz
cd glog-0.3.3
./configure --prefix=install_dir
make && make install
# gflags
tar zxvf gflags-2.1.1.tar.gz
cd gflags-2.1.1.ta.gz
mkdir build && cd build
export CXXFLAGS="-fPIC" && cmake -DCMAKE_INSTALL_PREFIX=install_dir .. && make VERBOSE=1
make && make install
# lmdb
git clone git://gitorious.org/mdb/mdb.git
cd mdb/libraries/liblmdb
make && make install
#opencv
unzip opencv-2.4.10.zip
cd opencv-2.4.10
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=install_dir ..
make && make install

4、cudnn安装
首先需要在https://developer.nvidia.com/cuDNN下载cuDNN Deep Neural Network Library。(CUDA Registered Developers 可以自由获得cuDNN library)

tar -zxvf cudnn-6.5-linux-R1.tgz
cd cudnn-6.5-linux-R1
# cp cudnn.h to CUDD_DIR/include, CUDA_DIR is the directory where the CUDA toolkit is installed
cp cudnn.h CUDA_DIR/include
# cp cudnn library to CUDA_DIR/lib64
cp libcudnn* CUDA_DIR/lib64
#这里还需要对cudnn library建立软链接
ln -s libcudnn.so.6.5 libcudnn.so.6.5.18
ln -s libcudnn.so libcudnn.so.6.5

5、caffe
http://caffe.berkeleyvision.org/installation.html上下载caffe软件包
解压后,进入caffe目录,首先复制一份Makefile.config

cp Makefile.config.example Makefile.config

然后修改Makefile.config。有几处需要修改
(1) cudnn
使用cuDNN加速,就需要取消对USE_CUDNN:=1的注释。同时设置cuda 安装路径和cuda architecture(取消对CUDA_ARCH的注释即可)

# cuda_install_dir是cuda安装路径
CUDA_DIR := cuda_install_dir

没有GPU的话,就应该使用CPU_ONLY:=1。
(2) BLAS库
我这里使用默认安装的 Intel MKL

BLAS := mkl

(3)Python
设置python目录

#to find python.h and numpy/arrayobject.h
PYTHON_INCLUDE := python_install_dir
        python_install_dir/dist_packages/numpy/core/include
#to find libpythonX.X.so or .dylib
PYTHON_LIB := python_install_dir/lib

设置到这那就应该行了吧 。高兴早了点!!!上面的依赖库是普通用户权限下使用源码安装的,即使配置了PATH和LD_LIBRARY_PATH,编译还是会出现类似如下的一堆错误

/usr/bin/ld: cannot find -lgflags
collect2: ld returned 1 exit status
make: *** [.build_release/lib/libcaffe.so] Error 1

还需进行如下修改

#=======================================================
PROTO_INCLUDE := /home/usrname/software/protobuf/include
PROTO_LIB := /home/usrname/software/protobuf/lib
GLOG_INCLUDE := /home/usrname/software/glog/include
GLOG_LIB := /home/usrname/software/glog/lib
GFLAGS_INCLUDE := /home/usrname/software/gflags/include
GFLAGS_LIB := /home/usrname/software/gflags/lib
HDF5_INCLUDE := /home/usrname/software/hdf5/include
HDF5_LIB := /home/usrname/software/hdf5/lib
LEVELDB_INCLUDE := /home/usrname/software/leveldb-master/include
LEVELDB_LIB := /home/usrname/software/leveldb-master
LMDB_INCLUDE := /home/usrname/software/lmdb/include
LMDB_LIB := /home/usrname/software/lmdb/lib
OPENCV_INCLUDE := /home/usrname/software/opencv/include
OPENCV_LIB := /home/usrname/software/opencv/lib
SNAPPY_INCLUDE := /home/usrname/software/snappy/include
SNAPPY_LIB := /home/usrname/software/snappy/lib
BOOST_INCLUDE := /home/usrname/boost_1_57_0
BOOST_LIB := /home/usrname/boost_1_57_0/stage/lib
#======================================================================
# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) $(PROTO_INCLUDE) $(GLOG_INCLUDE) $(GFLAGS_INCLUDE) $(HDF5_INCLUDE) $(LEVELDB_INCLUDE) $(LMDB_INCLUDE) $(OPENCV_INCLUDE) $(SNAPPY_INCLUDE) $(BOOST_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) $(PROTO_LIB) $(GLOG_LIB) $(GFLAGS_LIB) $(HDF5_LIB) $(LEVELDB_LIB) $(LMDB_LIB) $(OPENCV_LIB) $(SNAPPY_LIB) $(BOOST_LIB) /usr/local/lib /usr/lib

(这里使我明白了此前一直没注意的一个问题: -L是编译时查找.o或者.so文件所在的目录,用于链接生成可执行文件; LD_LIBRARY_PATH 是环境变量,用于程序执行时, 搜索.so 的路径 。参见http://bbs.csdn.net/topics/330189724
此时,编译应该没什么问题了。

make all
# 或者make all -j12 加快编译速度。j后面数字是并行编译线程数,最好是机器的核数

但是顺利执行make all, make test后,make runtest出现了错误。。

[ PASSED ] 832 tests.
[ FAILED ] 6 tests, listed below:
[ FAILED ] PowerLayerTest/0.TestPowerGradientShiftZero, where TypeParam = caffe::FloatCPU
[ FAILED ] PowerLayerTest/1.TestPowerGradientShiftZero, where TypeParam = caffe::DoubleCPU
[ FAILED ] PowerLayerTest/1.TestPowerGradient, where TypeParam = caffe::DoubleCPU
[ FAILED ] PowerLayerTest/2.TestPowerGradientShiftZero, where TypeParam = caffe::FloatGPU
[ FAILED ] PowerLayerTest/3.TestPowerGradientShiftZero, where TypeParam = caffe::DoubleGPU
[ FAILED ] PowerLayerTest/3.TestPowerGradient, where TypeParam = caffe::DoubleGPU

解决方法就是将boost 1.57换成boost 1.55,然后重新编译。。。(1.56也是不行的,参考http://blog.csdn.net/danieljianfeng/article/details/42836167)

100 Best GitHub: Deep Learning

100 Best GitHub: Deep Learning

ghdeeplearning170714

Deep Learning

Resources:

See also:

100 Best MATLAB VideosDeep Learning & Dialog Systems | MATLAB & Dialog Systems 2010 | MATLAB & Dialog Systems 2011 | MATLAB & Dialog Systems 2012 | MATLAB & Dialog Systems 2013Software Meta Guide


deep-learning [100x Jul 2014]

  • rasmusbergpalm/DeepLearnToolbox .. Matlab/Octave toolbox for deep learning. Includes Deep Belief Nets, Stacked Autoencoders, Convolutional Neural Nets, Convolutional Autoencoders and vanilla Neural Nets. Each method has examples to get you started.
  • karpathy/convnetjs .. Deep Learning in Javascript. Train Convolutional Neural Networks (or ordinary ones) in your browser.
  • nicholas-leonard/dp .. A deep learning library designed for streamlining research and development using the Torch7 distribution.
  • daemonmaker/DMPC .. Repository for experimenting with combining deep learning and model predictive control techniques.
  • woobe/deepr .. An R package to streamline the training, fine-tuning and predicting processes for deep learning based on ‘darch’ and ‘deepnet’.
  • RAMLab/deepbrains .. DeepBrains is a deep learning framework based on GO programming language and common machine learning hierarchical models.
  • colah/DL-Types-FP-posts .. Blog post exploring connection between Deep Learning, Types and Functional Programming
  • Misrab/deeplearning .. A collection of Go packages for Machine Learning, with an emphasis on Deep Learning
  • drshrey/deep_learning .. Playing around with the Alchemy API, and see what’s going on with Natural Language Processing and Deep Learning
  • hycis/pynet .. pynet is meant to be a flexible and modular deep learning framework base on theano.
  • Cysu/dlearn .. A deep learning Python module based on Theano.
  • zhaojunbo/paracel-SDAE .. The repo involves a Deep Learning method, Stacked Denoising Autoencoder. The repo exploits “paracel”, which is a new parallel computing framework.
  • DrDanRyan/ML-Framework .. A modular machine learning framework for Matlab with an emphasis on deep neural network models.
  • junku901/dnn .. Deep learning library for node.js. (Includes Logistic-Regression, MLP, RBM, DBN, CRBM, CDBN)
  • mimosavvy/PairsTradingDL .. Class project regarding a pairs trading strategy with deep learning and social information
  • xqcao/mywebapp .. Deep Learning is a new area of Machine Learning research, I share my deeper learning work overhere,
  • EderSantana/DeepEEG .. Deep learning for EEG analysis. This script was used to generate the results of the paper “Joint Optimization of Algorithmic Suites for EEG analysis” at EMBC 2014
  • samuel1208/UFLDL .. The solution of Andrew Unsupervised Feature Learning and Deep Learning
  • nibroc/SimpleSocket .. A simple non-production, hobby library for socket operations. This is intended as a learning project to deeply familiarize myself with unix sockets. It is not intended for real use.
  • daemonmaker/hedgehog .. Re-implementation of method in Playing Atari with Deep Reinforcement Learning paper.
  • chausler/deep .. some temporal RBM based models we’re playing with. Deep Learning ;-)
  • zewemli/DeepLearning .. An OpenCL implementation of Deep Belief Networks and Restricted Boltzmann Machines