深度学习性能优化工具及实践 - ucloud · •合并一些小的操作...

32
深度学习性能优化工具及实践 何普江

Upload: others

Post on 21-Jul-2020

33 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

深度学习性能优化工具及实践

何普江

Page 2: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

法律免责声明• 英特尔技术可能需要支持的硬件、特定软件或服务激活。性能会因系统配置的不同而有差异。请咨询您的系统制造商或零

售商。

• 有关性能和基准测试结果的更完整信息,请访问www.intel.com/benchmarks。

• 在性能测试过程中使用的软件及工作负载可能仅针对英特尔微处理器进行了性能优化。

性能测试(如 SYSmark和MobileMark)使用特定的计算机系统、组件、软件、操作和功能进行测量。对这些因素的任何更改可能导致不同的结果。请参考其他信息及性能测试(包括结合其他产品使用时的运行性能)以对目标产品进行全面评估。有关更多完整信息,请访问 http://www.intel.com/performance。

• 所描述的降低成本方案仅用作示例,表明某些基于英特尔的产品在特定环境和配置下会如何影响未来的成本,并节约成本。具体情况复杂多变。英特尔不保证任何成本和成本的节约。

§如欲了解更多信息,请访问 http://www.intel.com/performance。

• 此处提供的所有信息可随时改变而毋需通知。如欲获得英特尔最新的产品规格和发展蓝图,请联系您的英特尔代表。

• 没有任何计算机系统能够做到绝对安全。

• 本文档中涉及的英特尔季度、年以及将来的计划和预期的陈述为前瞻性陈述,存在一些风险和不确定性。英特尔的 SEC 文件(包括 10-K 表格的年度报表)中包含对可能影响英特尔的结果和计划的因素的详细讨论。

• 英特尔、英特尔标志、至强、英特尔博锐、英特尔至强融核、Look Inside. 是英特尔公司或其子公司在美国和/或其他国家(地区)的商标。

• *文中涉及的其他名称及商标属于各自所有者的资产。

• Microsoft、Windows 和Windows 标志是微软公司在美国和/或其他国家(地区)的商标或注册商标。

• © 2017 英特尔公司。

Page 3: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

内容

• 英特尔® MKL及GEMM函数漫谈

• 英特尔® MKL-DNN及其使用

• 英特尔®发行版Caffe*

• 高性能RNN的CPU实现

Page 4: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

英特尔® MKL

• 加速机器学习、科学研究、工程、财务和

设计等领域的数学处理

• 包括稠密及稀疏线性代数 (BLAS,

LAPACK, PARDISO), FFTs, vector math,

summary statistics 等

• 标准API(可以很方便的从其他数学库切

换到Intel® MKL)

• 高度优化(最大限度发挥多核核和 SIMD

指令的优势)

Energy FinancialAnalytics

Engineering Design

Digital Content Creation

Science & Research

Signal Processing

Page 5: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

MKL 2017中的组件

Linear Algebra

• BLAS

• LAPACK

• ScaLAPACK

• Sparse BLAS

• Sparse Solvers

• Iterative

• PARDISO*

• Cluster Sparse

Solver

Fast Fourier

Transforms

• Multidimensional

• FFTW interfaces

• Cluster FFT

Vector Math

• Trigonometric

• Hyperbolic

• Exponential

• Log

• Power

• Root

• Vector RNGs

Summary

Statistics

• Kurtosis

• Variation

coefficient

• Order statistics

• Min/max

• Variance-

covariance

And More…

• Splines

• Interpolation

• Trust Region

• Fast Poisson

Solver

Deep Neural

Networks

• Convolution

• Pooling

• Normalization

• ReLU

• Softmax

New

Page 6: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

cblas_?gemm函数介绍void cblas_sgemm (const CBLAS_LAYOUT Layout, const CBLAS_TRANSPOSE transa, const CBLAS_TRANSPOSE

transb, const MKL_INT m, const MKL_INT n, const MKL_INT k, const float alpha, const float *a, const MKL_INT lda,

const float *b, const MKL_INT ldb, const float beta, float *c, const MKL_INT ldc);

C := alpha*op(A)*op(B) + beta*C,

Layout – 指明二维数据在内存中的存放顺序是行优先(CblasRowMajor) 还是列

优先 (CblasColMajor).

transa – 指明 op(A) 使用的变换:

if transa=CblasNoTrans, then op(A) = A;

if transa=CblasTrans, then op(A) = AT;

if transa=CblasConjTrans, then op(A) = AH.

m – 指明矩阵op(A) 和矩阵C的行数.

n – 指明矩阵 op(B) 和矩阵C的列数.

k – 指明矩阵 op(A) 的列数(也就是矩阵op(B)的行数).

Page 7: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

void cblas_sgemm (const CBLAS_LAYOUT Layout, const CBLAS_TRANSPOSE transa, const CBLAS_TRANSPOSE transb, const MKL_INT m, const MKL_INT n, const MKL_INT k, const float alpha, const float *a, const MKL_INT lda, const float *b, const MKL_INT ldb, const float beta, float *c, const MKL_INT ldc);

C := alpha*op(A)*op(B) + beta*C,

a -lda – 指定 a 的主要维度(leading

dimension)

cblas_?gemm函数介绍

Page 8: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

cblas_sgemm使用技巧

103

130

109

0

20

40

60

80

100

120

140

m,n,k=1008

lda,ldb,ldc=1008

m,n,k=1024

lda,ldb,ldc=1024

m,n,k=1040

lda,ldb,ldc=1040

10

0 l

oo

ps

late

ncy

(m

s)

MKL cblas_sgemm在Intel(R) Xeon(R) Gold

6133上的性能

130

104

0

20

40

60

80

100

120

140

m,n,k=1024

lda,ldb,ldc=1024

m,n,k=1024

lda,ldb,ldc=1040

10

0 l

oo

ps

late

ncy

(m

s)

MKL cblas_sgemm在Intel(R) Xeon(R) Gold

6133上的性能

通过挑选合适的stride参数,我们可以进一步提升GEMM函数的性能

(某些特定情形下)

Page 9: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

Batch GEMM MKL_INT m[GRP_COUNT] = {4, 3};

MKL_INT k[GRP_COUNT] = {4, 6};

MKL_INT n[GRP_COUNT] = {8, 3};

MKL_INT lda[GRP_COUNT] = {4, 6};

MKL_INT ldb[GRP_COUNT] = {4, 6};

MKL_INT ldc[GRP_COUNT] = {8, 3};

CBLAS_TRANSPOSE

transA[GRP_COUNT] = {'N', 'N'};

CBLAS_TRANSPOSE

transB[GRP_COUNT] = {'T', 'T'};

…...

cblas_dgemm_batch (

CblasRowMajor,

transA,

transB,

m,

n,

k,

alpha,

a_array,

lda,

b_array,

ldb,

beta,

c_array,

ldc,

GRP_COUNT,

size_per_group);

m[0]

K[0]

*

https://software.intel.com/en-us/articles/introducing-batch-gemm-operations

K[0]

n[0]n[0]

m[0]=

m[1]

K[1]

* K[1]

n[1]n[1]

m[1]=

Batch

Page 10: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

Packed GEMM

https://software.intel.com/en-us/articles/introducing-the-new-packed-apis-for-gemm

Page 11: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

内容

• 英特尔® MKL及GEMM函数漫谈

• 英特尔® MKL-DNN及其使用

• 英特尔®发行版Caffe*

• 高性能RNN的CPU实现

Page 12: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

关于英特尔® MKL-DNN

高度优化的开源的DNN实现

• Apache License Version 2.0授权

• 开源地址:

https://github.com/01org/mkl-dnn

计算原语及数据操作原语 特性

• Convolution: direct batched

convolution,

• Inner Product,

• Pooling: maximum, average,

• Normalization: local response

normalization (LRN) across

channels and within channel,

batch normalization,

• Activation: rectified linear

unit neuron activation (ReLU),

softmax,

• Data manipulation: reorder

(multi-dimensional

transposition/conversion),

sum, concat, view.

• 多数据类型支持:Float32/INT16/INT8

• Optimized for

bleeding edge image

recognition

topologies, including

Cifar*, AlexNet*,

VGG*, GoogleNet*

and ResNet*

• 支持C和C++接口

英特尔®架构硬件

英特尔®MKL-DNN

深度学习框架(例如caffe*)

Page 13: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

编程模型

• Primitive - 任何操作,例如:

convolution, data format reorder, and

even memory

• Engine - 执行设备,例如CPU。每个原

语都被映射到一个特定的设备。

• Stream - 上下文执行环境. 可以提交一

系列原语到一个stream,并等待它完成

Engine

Primitive

Primitive

Engine Primitive

Stream

……

Page 14: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

如何创建原语?

操作/内存描述Create a logical description of

the operation

Create a primitive descriptor

by attaching the target engine

to the logical description

Create an instance of a

primitive

Create a memory descriptor(dimensions, precision, and format of data

layout)

Create a memory primitive

descriptor

Create a memory primitive

原语描述

原语

操作原语 内存原语

Page 15: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

代码示例

• 创建操作描述和原

语描述

• 创建原语并push

到一个向量

• 提到原语到stream

Page 16: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

使用英特尔® MKL-DNN进行推理

训练好的模型

模型转换器

Inference.cpp

weights.bin

topo.txt 代码生成器

topo.txt: 网络拓扑的中间表示形式

weights.bin : 存储权重信息

Inference.cpp: 基于MKL-DNN的Inference代码,在初始化的时候会加载weights.bin

InferenceFramework.cpp: 轻量级Inference框架,可以解析topo.txt并加载weights.bin

训练好的模型

模型转换器

InferenceFrame

work.cpp

weights.bin

topo.txt

Page 17: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

模型转化器 – 一个Tensorflow的例子

• 公式Y = α * ((X – mean) / (D + δ)^0.5) + β

Y = A * X + BA = α * (D + δ)^0.5B = β - α * (D + δ)^0.5 * mean

• topo.txt

Convolution -> BatchNorm

• weights.bin

conv weights = A * original conv weights

conv bias = B

Page 18: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

batch size

device1 2 4 8 16 32 64 128

Platinum 8170 x 2 + MKLDNN

(OMP_NUM_THREADS=8)24 22.48 41.46 81.94 162.64 320.86 642.73 1274.8

Platinum 8170 x 2 +MKLDNN

(OMP_NUM_THREADS=52)121.73 7.12 12.76 22.45 39.4 73.34 141.2 273.23

Platinum 8170 x 2 + Intel caffe

(OMP_NUM_THREADS=32)7.75 10.7 16.53 28.01 52.5 102.37 207.42 426

Platinum 8170 x 2 + Intel caffe

(OMP_NUM_THREADS=52)9.99 12.84 17.94 28.5 48.56 91.81 180.03 355.26

Platinum 8170 x 2 + TF + MKL + NCHW 26.68 45.49 67.47 98.39 165.93 300.67 580.54 1167.76

Platinum 8170 x 2 + TF + AVX2 + NHWC 99.15 108.49 122.34 140 172.68 245.32 399.9 717.08

Mobilenet 在两路 Intel® Xeon® Platinum 8170上的性能

(延迟: 毫秒)

性能示例

Page 19: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

内容

• 英特尔® MKL及GEMM函数漫谈

• 英特尔® MKL-DNN及其使用

• 英特尔®发行版Caffe*

• 高性能RNN的CPU实现

Page 20: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

英特尔®发行版Caffe*

• 基于BAIR/BVLC Caffe

• 与BAIR/BVLC Caffe兼容

• 添加了流行检测网络的支持(如SSD)

• 支持多节点训练

• 使用Intel® MKL, Intel® MKL-DNN做了深度优化

• 支持3种引擎:CAFFE、MKL2017和MKLDNN

• 源码:https://github.com/intel/caffe

Page 21: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

主要优化

• 代码向量化

• BLAS (基本线性代数子程序) (Intel® MKL)

• JIT Assembler (xbyak)

• 并行化

• openmp

• 多节点训练

• 使用mpi通信

• 数据并行

• 其他优化

Page 22: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

优化示例

Page 23: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

推理 (Inference) 性能

Batch size Images/s Batch size Images/s

Alexnet 256 3309 1024 3530

Googlenet_v1 32 1006 128 1217

Googlenet_v2 32 906 192 1047

Resnet_50 32 483 128 494

Page 24: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

• 推荐使用Centos 7.2 或更新的发行版

• 不要运行其他程序(Intel发行版caffe会用到系统所有核心)

• 清空缓存让测试结果更加稳定:

• sudo sh -c 'sync; echo 3 > /proc/sys/vm/drop_caches'

• 推荐使用gcc 4.8.5 (或更新版本)或Intel编译器来编译 Intel® 发行版Caffe*

• 推荐使用MKLDNN引擎.

• 对于英特尔®至强®可扩展处理器 (Skylake) , 推荐如下配置:

• sudo echo 0 > /proc/sys/kernel/numa_balancing

• For example: numactl -l $TARGET_CAFFE_BUILD_DIR/tools/caffe time -iterations

100 -model <modelfile> -engine=MKLDNN

测试注意事项

Page 25: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

内容

• 英特尔® MKL及GEMM函数漫谈

• 英特尔® MKL-DNN及其使用

• 英特尔®发行版Caffe*

• 高性能RNN的CPU实现

Page 26: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

• 选择高效的GEMM实现

• combine, batch, pack

• 合并一些小的操作

• 并行化元素级(element-wise)操作

• Activation: sigmoid, tanh

• Element-wise add/multiplication

• 合理的数据排布

• 低精度表示 (int16, int8)

GRU: Gated Recurrent Unit

RNN优化要点

Page 27: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

高效的 GEMM 实现 Matrix Combination

Independent matrix multiplicationsGEMM sizes are from Baidu DeepBench project:

Input dim = hidden dim = (256, 512, 1024)

Batch size = 16

Time step = 25

• 合并独立的小矩阵到一个大矩阵

• 多种合并方式

Page 28: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

独立的矩阵乘法 GEMM sizes are from Baidu DeepBench project:

Input dim = hidden dim = (256, 512, 1024)

Batch size = 16

Time step = 25

• 一次性计算多个矩阵乘法

• 不同矩阵大小置入不同组(group)

高效的 GEMM 实现 Matrix Batch Mode

Page 29: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

Same weight U for all GEMMs

• Pack U矩阵之后,再顺序调用矩阵乘法

• 缓存访问更友好: 为所有操作一次性准备好权重U

GEMM sizes are from Baidu DeepBench project:

Input dim = hidden dim = (256, 512, 1024, 2048)

Batch size = 1 for inference

Time step = 128

高效的 GEMM 实现 Matrix Pack Mode

Page 30: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

多层次融合: 探索更多的并行性

• 层与层之间存在独立操作

• 融合多个层次的操作到一个调用

函数以获得更优化的实现

• 打破只能在层次内部进行优化的

局限

• 更大的并行能力Inputs

Fully Connection

Softmax

Page 31: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

优化性能表现

Wix

Wfx

Wcx

Wox

Xt

合并的权重

合并的输入

Page 32: 深度学习性能优化工具及实践 - UCloud · •合并一些小的操作 •并行化元素级(element-wise)操作 •Activation: sigmoid, tanh •Element-wise add/multiplication

更多分享与交流,请关注“UCloud技术公告牌”