Skip to content

Conversation

@tensor-tang
Copy link
Contributor

fix #5790

这里可能方便review。

@tensor-tang tensor-tang requested a review from luotao1 November 25, 2017 10:54
Copy link
Contributor

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

图的部分,先提一点意见:

  • 图的字体大小要全文统一,现在有的地方很小,有的地方太大。配色也尽量全文统一。
  • 缺少一张介绍MKL/MKLML/MKLDNN的图(表格),和相应的文字。
所以,我们会在`cmake/external`目录新建`mkldnn.cmake``mklml.cmake`文件,它们会在编译PaddlePaddle的时候下载对应的软件包,并放到PaddlePaddle的third party目录中。
<div align="center">
<img src="image/matrix.png" width=400 height=250><br/>
Figure 3. MKLDNNMatrix
Copy link
Contributor

@luotao1 luotao1 Nov 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

关于图片有几个建议:

  • MKLDNNMatrix能挪到上面么?因为它和paddle:Matrix是并级的
  • 图片里面的Paddle是不是都可以去掉,如果保留的话,paddle:MKLDNNMatirx也要加上。
  • 为什么只有MKLDNNMatrix是红色的?mkldnn:memory和其他的都是蓝色的?mkldnn:memory的颜色应该是MKLDNNMatrix一样吧。
  • 图片里的字体太小了。字体要放大,基本要和正文字体一样。目前长方形框太高,可以矮一点。
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 是可以挪到上面,但是我原先的考虑的它是继承CpuMatrix的,所以放在了下面。我后面还是用UML来表示吧。
  • 嗯,是的,忘记在MKLDNNMatrix前面加了,thx。
  • 颜色的话,本来是突出新加的模块,mkldnn的因为本来就有,也不算新的所以也用了蓝色,如果觉得没有必要也可以统一为红色。
  • 好的,没问题。
<img src="image/overview.png" width=350><br/>
Figure 1. PaddlePaddle on IA.
<img src="image/engine.png" width=300><br/>
Figure 2. PaddlePaddle with MKL Engines
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个图里的字体,每个框都不一样大,请调整成统一的大小。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,是因为有resize了所以字体没能统一。字体都统一了话,有的图片内容多,框的大小可能会不一样,不过会尽量统一。


<div align="center">
<img src="image/layers.png" width=430><br/>
Figure 4. MKLDNNLayer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 这个图里的省略号是什么?要么补充完整,要么forward, backward,resetFwd, resetBwd全部删掉。
  • MKLDNNMatrix只要出现一次就够了
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的, 没问题。

同时,由于MKL-DNN在训练时使用的参数layout可能与PaddlePaddle默认的`nchw`不一致,我们会在网络训练的开始和结束时分别转换这个layout,使得最终保存的参数格式与PaddlePaddle一致。
<div align="center">
<img src="image/gradients.png" width=600 height=300><br/>
Figure 5. Merge Gradients
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

  • Merge部分的线没有箭头,同时线太乱,合并的三条线应该可以画成1条。
  • 这张图过大,MKLDNNLayer的字体也大。
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,没问题。

- `WITH_MKLML` 控制是否使用MKLML库。
当打开`WITH_MKL`时,会自动使用MKLML库作为PaddlePaddle的CBLAS和LAPACK库,同时会开启Intel OpenMP用于提高MKLML的性能。
- `WITH_MKLDNN` 控制是否使用MKL-DNN。
当开启`WITH_MKL`时,会自动根据硬件配置[[2](#references)]选择是否编译MKL-DNN。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里可以对应说一下,三个动态库么?包括动态库的大小,以及安装的地方?

  • WITH_MKL时,多了libiomp5.so和libmklml_intel.so
  • WITH_MKLDNN时,多了libmkldnn.so
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,没问题。

每个`MKLDNNlayer`都会有`inVal_`,`inGrad_`,`outVal_``outGrad_``MKLDNNMatrix`
分别代表input value, input gradient,output value和output gradient。
它们会存放MKL-DNN用到的internal memory,同时还会定义以*ext*开头的`MKLDNNMatrix`(表示external的memory)。
他们主要是当数据格式与PaddlePaddle默认的`nchw`格式不匹配时,用于转换内存的工作。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

他们-》它们

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx

`MKLDNNLayer`中会提供一些必要的接口和函数,并且会写好`forward``backward`的基本逻辑。部分函数定义为纯虚函数,子类只需要实现这些函数即可。
一般来说,每个`MKLDNNLayer`中的`extOutVal_``extOutGrad_`必须分别与`output_.value``output_.grad`共享内存,
因为PaddlePaddle的activation会直接使用`output_.value``output_.grad`,
如果不需要external的buffer用于转换,那么internal的buffer也会与它们共享内存。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

109-120行可以重新组织下,使得逻辑更加清晰(下面语句可以再组织下):

每个MKLDNNLayer都包含用于内部存储和外部存储的一系列MKLDNNMatrix:

  • 内部存储(internel memory):inVal_,inGrad_,outVal_outGrad_,分别代表输入数据,输入梯度,输出数据和输出梯度。
  • 外部存储(external memory):以ext开头的,如extOutVal_extOutGrad_。它们必须分别与output_.valueoutput_.grad共享内存。原因是PaddlePaddle的activation会直接使用output_.valueoutput_.grad
  • 转换函数(resetXXX):对输入数据,输入梯度,输出数据和输出梯度的转换(可以列出对应函数名)。这些函数会根据输入参数重新设置内部和外部存储。当然这两者也可以相等,即表示不需要转换。

注意:每个MKLDNNlayer的子类只需要使用内部存储就可以了,所有外部的转换工作都会在reset系列函数中都准备好。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,thx。我会按照你的这个来改


### Activations
由于在PaddlePaddle中,激活函数是独立于layer概念的,所以会在`paddle/gserver/activations`目录下添加`MKLDNNActivation.h``MKLDNNActivation.cpp`文件用于定义和使用MKL-DNN的接口。
在重构前的PaddlePaddle中,激活函数是独立于`Layer`的概念,并且输入输出都是公用一块内存,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

公用-》共用?
下同

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,thx

会在`paddle/gserver/test`目录下添加`test_MKLDNN.cpp``MKLDNNTester.*`用于MKL-DNN的测试。
测试分为每个layer(或activation)的单元测试和简单网络的整体测试。
我们会添加`test_MKLDNN.cpp``MKLDNNTester.*`用于MKL-DNN的测试。
测试分为每个Layer(或Activation)的单元测试和简单网络的整体测试。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用中文括号

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,thx

并且可能在`python/paddle/trainer_config_helper`目录下的`activations.py ``layers.py`里面添加必要的MKL-DNN的接口
同时,会在`paddle/utils.Flags`中添加一个`use_mkldnn`的flag,用于选择是否使用MKL-DNN的相关功能

### Demos
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

demos也可以去掉,v1_api_demo是一个临时目录,不定时就会被删。如果要做demo,还是做成V2 api的吧。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,没问题。

会在`v1_api_demo`目录下添加一个`mkldnn`的文件夹,里面放入一些用于MKL-DNN测试的demo脚本。
可能会在`v1_api_demo`目录下添加一个`mkldnn`的文件夹,里面放入一些用于MKL-DNN测试的demo脚本。

### Benchmarking
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarking->Benchmark

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是因为这里


### Benchmarking
会添加`benchmark/paddle/image/run_mkldnn.sh`用于测试使用MKL-DNN之后的性能
会添加`benchmark/paddle/image/run_mkldnn.sh`用于测试和对比,在使用MKL-DNN前后的性能
Copy link
Contributor

@luotao1 luotao1 Nov 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • benchmark/paddle/image/run_mkldnn.sh可以放链接
  • 用于测试和对比在使用MKL-DNN前后的CNN网络性能。
  • 放上benchmark的IntelOptimized文档。
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,没问题。

2. 重写父类Layer的**init**函数,修改`deviceId_``-2`,代表这个layer是用于跑在MKL-DNN的环境下。
3. 创建`MKLDNNBase`,定义一些除了layer和memory相关的类和函数。
包括MKL-DNN会用到`MKLDNNStream``CPUEngine`,和未来可能还会用到`FPGAEngine`等。
4. 如果MKL-DNN layer的后面接有cpu device,那么就会使`output_.value``extOutVal_`共享内存,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cpu device是指非MKLDNN的layer么?换一个说法:普通的CPU layer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

包括MKL-DNN会用到`MKLDNNStream``CPUEngine`,和未来可能还会用到`FPGAEngine`等。
4. 如果MKL-DNN layer的后面接有cpu device,那么就会使`output_.value``extOutVal_`共享内存,
同时数据格式就是`nchw`,这样下一个cpu device就能拿到正确的数据。
在有cpu device的时候,external的memory的格式始终是`nchw`或者`nc`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • external的memory-》extOutVal和extOutGrad?这样更清晰
  • NCHW等都大写吧,我看reference里面是大写,求统一。
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,没问题。

@tensor-tang tensor-tang force-pushed the doc branch 5 times, most recently from f18ce97 to d86b018 Compare December 1, 2017 10:24
Copy link
Contributor

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@luotao1 luotao1 merged commit 9b761ed into PaddlePaddle:develop Dec 2, 2017
@tensor-tang tensor-tang deleted the doc branch December 2, 2017 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants