GPU 加速的 Caffe2

现在,开始阅读此《GPU Ready Apps Guide》(GPU Ready 应用程序指南)。

Caffe2

Caffe2 是一个深度学习框架,可以实现简单、灵活的深度学习。Caffe2 基于原始 Caffe 构建,设计时着重考虑了表达式、速度和模块化,允许以更灵活的方式组织计算。

Caffe2 旨在利用社区对新模型和算法的贡献,来为您提供一种简单、直观的深度学习实验方法。Caffe2 随附了可交互的原生 Python 和 C++ API,因此您现在可快速生成原型,并随后轻松进行优化。

Caffe2 使用最新的 NVIDIA Pascal™ GPU 进行加速,并可以在单个节点内的多 GPU 间扩展。如今,您训练模型的时间可以从几天缩短到几小时。

NVIDIA 和 Facebook 合作构建 Caffe2 深度学习框架。

安装

下载和安装说明

安装和运行 CAFFE2

Caffe2 是一个深度学习框架,同时支持卷积和递归网络。

框架依赖项

Caffe2 需要 protobuf、atlas、glog、gtest、limdb、leveldb、snappy、OpenMP、OpenCV、pthread-stubs、cmake、python-protobuf 以及 numpy。

对于 GPU 加速,CUDA 和 cuDNN 是必须的。支持最新版本的 cuDNN (5.1.10)。

下面列出了在 Ubuntu 16.04 系统上安装依赖项的示例过程。

sudo apt-get install libprotobuf-dev protobuf-compiler libatlas-base-dev libgoogle-glog-dev libgtest-dev liblmdb-dev libleveldb-dev libsnappy-dev python-dev python-pip libiomp-dev libopencv-dev libpthread-stubs0-dev cmake python-protobuf git
sudo pip install numpy [matplotlib ipython (如果还想使用 ipython notebook)]

安装 GPU 库

wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.61-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1604_8.0.61-1_amd64.deb
wget http://developer.download.nvidia.com/compute/redist/cudnn/v5.1/libcudnn5_5.1.10-1+cuda8.0_amd64.deb
wget http://developer.download.nvidia.com/compute/redist/cudnn/v5.1/libcudnn5-dev_5.1.10-1+cuda8.0_amd64.deb
sudo dpkg -i libcudnn5*

构建 CAFFE2

git clone --recursive https://github.com/caffe2/caffe2.git
cd caffe2
mkdir build && cd build
cmake ..
make

测试 CAFFE2

有多种验证 Caffe2 构建是否正常工作的方法,其中一种是使用位于 $CAFFE2_ROOT/build/caffe2/binaries 中的示例二进制文件,而另一种是使用位于 $CAFFE2_ROOT/build/caffe2/python 中的 python 脚本。以“_test”结尾的二进制文件全部可用于验证 Caffe2 构建是否正常工作。以下是来自 elementwise_op_gpu_test 的示例输出。

user@gpu-platform:~/framework/caffe2/build/caffe2/binaries$ ./elementwise_op_gpu_test
Running main() from gtest_main.cc
[==========] Running 4 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from ElementwiseGPUTest
[ RUN ] ElementwiseGPUTest.And
[ OK ] ElementwiseGPUTest.And (118 ms)
[ RUN ] ElementwiseGPUTest.Or
[ OK ] ElementwiseGPUTest.Or (2 ms)
[ RUN ] ElementwiseGPUTest.Xor
[ OK ] ElementwiseGPUTest.Xor (1 ms)
[ RUN ] ElementwiseGPUTest.Not
[ OK ] ElementwiseGPUTest.Not (1 ms)
[----------] 4 tests from ElementwiseGPUTest (122 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 1 test case ran. (122 ms total)
[ PASSED ] 4 tests.
user@gpu-platform:~/framework/caffe2/build/caffe2/binaries$

提供了多个 python 脚本,可以让用户轻松地使用不同的神经网络架构测试 Caffe2。以下显示了来自 convnet_benchmarks.py 的示例结果。

user@gpu-platform:~/frameworks/caffe2/build/caffe2/python$ python convnet_benchmarks.py --batch_size 1 --model AlexNet --cudnn_ws 500 --iterations 50
AlexNet: running forward-backward.
I0313 09:52:50.316722 9201 net_dag.cc:112] Operator graph pruning prior to chain compute took: 3.1274e-05 secs
I0313 09:52:50.316797 9201 net_dag.cc:363] Number of parallel execution chains 34 Number of operators = 61
I0313 09:52:50.316933 9201 net_dag.cc:525] Starting benchmark.
I0313 09:52:50.316938 9201 net_dag.cc:526] Running warmup runs.
I0313 09:52:51.110822 9201 net_dag.cc:536] Main runs.
I0313 09:52:52.975484 9201 net_dag.cc:547] Main run finished. Milliseconds per iter: 37.2928. Iters per second: 26.8149

如果尝试运行任何 python 脚本时收到了重大错误提示,请将 caffe2 添加到 PYTHONPATH 中或在脚本中手动添加。终端的示例行如下

export PYTHONPATH=$PYTHONPATH:$CAFFE2_ROOT/build

或者,在 Python 脚本内使用 Python sys 模块添加该路径。

CAFFE_ROOT=path/to/caffe2
import sys
sys.path.insert(0, CAFFE_ROOT+'/build')

除对 AlexNet 进行测试外,此脚本还可用于测试不同的批尺寸和网络。此命令将为两个使用 Inception 网络配置且批量大小不同(16 和 32)的网络提供结果。下文中提供了使用 K80 上单个 GPU 的典型输出。

for BS in 16 32; do python convnet_benchmarks.py --batch_size $BS --model Inception --cudnn_ws 1000 ; done

user@sas03:~/frameworks/caffe2/build/caffe2/python$ for BS in 16 32; do python convnet_benchmarks.py --batch_size $BS --model Inception --cudnn_ws 500 ; done
Inception: running forward-backward.
I0317 00:54:50.650635 13008 net_dag.cc:123] Operator graph pruning prior to chain compute took: 0.000371156 secs
I0317 00:54:50.651175 13008 net_dag.cc:374] Number of parallel execution chains 381 Number of operators = 410
I0317 00:54:50.652022 13008 net_dag.cc:536] Starting benchmark.
I0317 00:54:50.652041 13008 net_dag.cc:537] Running warmup runs.
I0317 00:54:53.824313 13008 net_dag.cc:547] Main runs.
I0317 00:54:55.567314 13008 net_dag.cc:558] Main run finished. Milliseconds per iter: 174.29. Iters per second: 5.73757
Inception: running forward-backward.
I0317 00:54:59.600677 13051 net_dag.cc:123] Operator graph pruning prior to chain compute took: 0.000389294 secs
I0317 00:54:59.601260 13051 net_dag.cc:374] Number of parallel execution chains 381 Number of operators = 410
I0317 00:54:59.602026 13051 net_dag.cc:536] Starting benchmark.
I0317 00:54:59.602046 13051 net_dag.cc:537] Running warmup runs.
I0317 00:55:05.079577 13051 net_dag.cc:547] Main runs.
I0317 00:55:08.394855 13051 net_dag.cc:558] Main run finished. Milliseconds per iter: 331.52. Iters per second: 3.01641

表 1.使用 P100e 获得的示例结果。

Batch Size Network (milliseconds/iteration)
  AlexNet Overfeat Inception VGGA
16 24.6273 71.0308 65.605 166.251
32 40.0542 118.455 115.41 301.975
64 70.194 214.297 212.805 584.622
128 128.4 411.872 410.167 1149.21

Batch Size Network (image/s == iteration/millisecond*Batch Size/iteration*1,000ms/1s)
  AlexNet Overfeat Inception VGGA
16 650 225 244 96
32 799 270 277 106
64 912 299 301 109
128 997 311 312 111

Note, the benchmarks results shown above are for synthetic data only, and does not use images from the ImageNet dataset.

基准

GoogLeNet 的图像训练性能

GoogLeNet 是较新的深度学习模型,可以利用更深度、更广泛的网络提供更高准确度的图像分类。

AlexNet 在 Pascal GPU 上的训练性能
Pascal GPU 上的 Inception 训练性能
Pascal GPU 上的 OverFeat 训练性能
VGG-A 在 Pascal GPU 上的训练性能

推荐的系统配置

硬件配置

PC

参数
规格

CPU 架构

x86

系统内存

16GB

CPU

2(8 核以上,2 GHz 以上)

GPU 型号

NVIDIA TITAN Xp

GPU

1-4

服务器

参数
规格

CPU 架构

x86

系统内存

高达 256GB

CPU 数/节点

2(8 核以上,2 GHz 以上)

GPU 型号

NVIDIA ® Tesla ® P100

GPU 数/节点

1-8

软件配置

软件堆栈

参数
版本

操作系统

CentOS 6.2

GPU 驱动程序

375.26 或更高版本

CUDA 工具包

8.0 或更高版本

立即构建理想的 GPU 解决方案。