-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Enable mkldnn_fc for general format #3337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks to @tensor-tang for this PR!
I have some comments about naming and a question about the newly added flags.
paddle/gserver/layers/MkldnnBase.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mkldnn => MKLDNN 英语缩写应该大写
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
paddle/gserver/layers/MkldnnBase.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cpu => CPU 英语缩写应该大写
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CpuEngine这个直接写成一个全局函数这样是否就可以了?
mkldnn::engine& getCpuEngine() {
static mkldnn::engine cpuEngine(mkldnn::engine::cpu, 0);
return cpuEngine;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里参考是这里
后续可能还会考虑FPGA的engine。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cpu => CPU 英语缩写应该大写
@wangkuiyi 还是我之前的疑虑。
因为我参照的paddle原有的命名规则,比如CpuMatrix, GpuMatrix, 还包括文件名的例子CudnnPoolLayer.cpp等等。
这些的命名规则我都没有问题,可以修改的,只需要告诉我最终采用哪种即可,我的目的还是只要符合paddle的需求就好。
Thanks~
后面类似的我就不一一comment了吧。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我很理解 @tensor-tang 。这是我们之前没有说明白的地方。Paddle之前和Caffe2的代码里都采用了“非常常见的,以至于形成了新词的英语缩写不再当做缩写,也就不用完全大写”的规则。但是我们实际上很难执行,因为不好判断哪些缩写已经常用到可以作为新词。比如FPGA在做FPGA的人眼里足够常见了,但是很多deep learning researchers甚至都没有听说过。为此,我们的新代码库里,所有的英语缩写都保持大写。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,那我会统一按照最新标准执行。Thanks~
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MKLDNNFcLayer.h
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MKLDNNLayer.h
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
paddle/gserver/layers/MkldnnLayer.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MKLDNNLayer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
paddle/gserver/layers/MkldnnLayer.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need ; here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MKLDNNTester.h
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get it -- why we need this flags. If we don't want to convert weights from Paddle, how about we just do not call this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是一个支持。
因为mkldnn训练出来的weight, 数据的排列方式与paddle原有的不一样。
比如,原先我们用paddle的cpu已经训练出来了一组weight,现在想要用mkldnn的engine去加速inference,就需要use_mkldnn_wgt=False,这个函数就很有必要了。
在当前FC的例子里面,仅仅是一个转置的关系,还比较简单,但是后面别的layer就会比较复杂。
采用这个flag是给用户一个选择,输入的weight是不是用的mkldnn格式weight,如果是的,就不用这步转换了。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
采用这个flag是给用户一个选择,输入的weight是不是用的mkldnn格式weight
这个对用户来说要求太高了。这个能做成自动透明的么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同意。
所以,我本来有个额外的计划,目前暂时采用这个flag,最终我也希望最后mkldnn在保存weight的时候(也不好在每个iteration结束后都转换,这样太浪费了)自动调用convert函数,存为cpu的格式,这样这个flag就可以去掉了。
但是我遇到一个问题,调用save的时候用的都是parameter的接口,这个函数要么存在mkldnnlayer,要么以后希望放在MkldnnMatrix里面,没有地方加这样一个逻辑。所以,暂时用这个flag来兼顾。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cvtWgtFromPaddle => ConvertWeightsFromPaddle
paddle/utils/Flags.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get it -- why would we need these flags. We had a discussion that we shouldn't add new flags, because they are actually global variables. For this specific PR, are these flags necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是像 use_gpu一样,引入 use_mkldnn是为了做layer间的选择,是选择用mkldnn还是普通的cpu layer。如果不采用这个flag的话,mkldnnfc就需要单独用api来使用了,不是很通用。
use_mkldnn_wgt这个flag就像前面说的一样,是为了更好的支持paddle原有的cpu weight,并且,如果本来就是用mkldnn进行pretrain的话,也可以省掉不必要的转换。
We had a discussion that we shouldn't add new flags
这一点我记得我们是align过的,当时说的是可以添加flag的。
|
Thanks @wangkuiyi . 关于整体命名规则,我有点疑问,因为paddle原有的规则看起来,好像只有首字母大写了:
因为我参考的是CpuMatrix和CpuMatrixPtr
所以整体才是现在这样。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use_mkldnn_wgt这个flag就像前面说的一样,是为了更好的支持paddle原有的cpu weight,并且,如果本来就是用mkldnn进行pretrain的话,也可以省掉不必要的转换。
这个flag在这个PR中可以先去掉么?等之后考虑pretrain的时候,再加入。
- mkldnnLayer中有FC相关的代码,这部分能挪到paddle/mkl里面,类似paddle/cuda的做法?父类中不应该有子类的信息。
- 关于单测,可以考虑复用paddle的testLayerGrad函数和test_CompareTwoNetworks么?
paddle/gserver/layers/MkldnnBase.h
Outdated
| DNN_SIZES, | ||
| DNN_FMTS, | ||
| DNN_ALL, | ||
| } DNN_LOG_LEVEL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 21-27行每行都需要注释。
- DNN_BASE和DNN_TESTS为什么都是1呢
- 这里的DNN需要换成MKLDNN么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
21-27行每行都需要注释。
ok
DNN_BASE和DNN_TESTS为什么都是1呢
这是是自己定的,为了方便区分名字,但是实际值没必要区分
这里的DNN需要换成MKLDNN么?
是可以改成DNN => MKLDNN的, 我是考虑名字改完后名字太长了~
这些都是VLOG的时候用的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为了和cudnn做区分,所以要改成MKLDNN。
如果两个都是1,直接用DNN_BASE就行了,不用再区分。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里会改为MKLDNN_BASE。
还是要区分下,用的场合不一样,一个用做基本信息,一个仅仅用在test里面。只是值一样罢了,如果以后觉得值想不一样,改起来也很方便。
paddle/gserver/layers/MkldnnBase.h
Outdated
| /** | ||
| * @brief Submit stream | ||
| * @param prims The primitives vector | ||
| * block Waiting for the stream to complete |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@param block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx
| } | ||
| // TODO(TJ): change me when mkldnn have method to reset this state | ||
| stream_.reset(new mkldnn::stream(mkldnn::stream::kind::eager)); | ||
| // stream_.reset(new mkldnn::stream(mkldnn::stream::kind::lazy)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
88行注释的代码去掉。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个先留着吧,不建议去掉吧,因为是一种选择,现在不好说哪个性能好。加在下面对比用的。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果要留的话,也应该留在87行前面,和86行的注释连在一起。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,我挪一下位置。thx
paddle/gserver/layers/MkldnnLayer.h
Outdated
| */ | ||
| virtual void convertWeightsToPaddle() {} | ||
|
|
||
| void resetForwardFC(int bs, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resetForwardFC和resetBackwardFC要加注释
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
paddle/gserver/layers/MkldnnLayer.h
Outdated
|
|
||
| // TODO(TJ): change below memory as MkldnnMatrixPtr type | ||
| // input == bottom, output == top | ||
| // value == data, grad == diff |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
53-54的注释是描述哪段呢?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
就是下面的命名的解释。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
下面的55-62行没有出现input、bottom、output、top、value、data、grad、diff的任何一个单词。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
本来的目的是一个提示作用,因为比如bwd时的真正“input数据” 实际是这里的outGrad,为了起到一个提示作用,所以才加注释用bottom和top区分。
如果不希望看到,就删掉吧。
|
|
||
| #include "MkldnnLayer.h" | ||
|
|
||
| using mem = mkldnn::memory; // NOLINT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mem->mklmem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个仅仅是这个cpp里面,目的就是为了简写,所以没必要专门改为mklmem了吧?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要修改,mklmem比mem更直观。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok,会调整。但是我觉得如果要改,mklmem也不好吧。采用use namespace吧,会把先都改为memory。
| const ParameterMap& parameterMap) { | ||
| if (!Layer::init(layerMap, parameterMap)) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
直接调用Layer::init(layerMap, parameterMap即可,不用条件判断
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有返回值的,需要判断的。
| outGrad_->set_data_handle(topDiff); | ||
|
|
||
| stream_->submit(pipelineBwd_); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mkldnnLayer.cpp中存在很多FC相关的代码,这些应该和cuda一样,放在paddle/mkl/src里面。mkldnnLayer.cpp只是一个基类,不能放继承类的信息。
@hedaoyuan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是一个整体,fc子类只是调用接口,父类提供了一系列的方法,不是想代表继承类的信息,以后也会包括其它,包括conv等。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为这里父类为每个子类提供了不同的方法,但这些方法应该是在子类中实现。父类只是提供一个通用的方法,具体实现应该在子类中完成。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我也有疑问,问什么mkldnnForwardFC, mkldnnBackwardFC放在MkldnnLayer里面,而不是放在MkldnnFcLayer里面。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那么我来调整下,想办法放到fc中。
@luotao1 @hedaoyuan
| TestConfig ref = cfg; | ||
| ref.layerConfig.set_type(compareTypes[1]); | ||
| for (auto bs : {pm.bs, 1}) { | ||
| tester.run(cfg, ref, bs, pm.ih, pm.iw); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里不能直接调用testLayerGrad函数么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里check的不是grad的波动等信息,并且check的点也不一样。这里着重比较在不同size下的与cpu各方面结果一致。
用c++ code更方便,也更好维护。如果用test_compareNet会更麻烦,其实这里等于说每个size的比较都需要写两个配置文件,这里用gtest只需要一行。
|
|
||
| // reset configs and layer names | ||
| configs_[DNN] = dnn; | ||
| configs_[REF] = ref; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mkldnnTest是用来比较两个layer间是否无diff。
能不能配置两个简单的网络(只有data+fc),直接调用paddle的test_CompareTwoNetworks?这样代码少很多。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
目前不好直接用,因为mkldnn的中间输出是内部格式的weight, 肯定与cpu的不一样。
并且我建议把mkldnn相关的检查与原有的方法区分开来,方便维护。
后期功能齐全了之后可以考虑用comparenet,目前仍然建议安装每个layer的逐一确认。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个flag在这个PR中可以先去掉么?等之后考虑pretrain的时候,再加入。
建议选择加入,因为在gtest的时候需要比较weight,所以会用到。
关于单测,可以考虑复用paddle的testLayerGrad函数和test_CompareTwoNetworks么?
单测不建议使用testLayerGrad函数和test_CompareTwoNetworks。
- 用一个test_mkldnn搞定所有mkldnn相关的检测,也很方便我后期维护。
- 这里测试的重点在于对于,不同size的输入输出,都能等于cpu的结果,而不是在layergrad。
- 这里用一行代码可以解决一个test case, 如果用test_CompareTwoNetworks则需要配置两个小网络,更加烦琐。并且现在功能还不齐全,可以等后面layer齐全点,一次比较一个深一点的网络更有意义。
mkldnnLayer中有FC相关的代码,这部分能挪到paddle/mkl里面,类似paddle/cuda的做法?父类中不应该有子类的信息。
像之前说的那样,mkldnn与cudnn既有相似也有不一样。相似的是接口相似,不相似的是使用时mkldnn还需要很多primitive和memory的desc,这些东西还是放在父类最合适。因为要用到一系列的mkldnn 自己的memory,这些留在父类直接访问就好,父类提供每个layer的方法,子类直接用接口就好了。
paddle/gserver/layers/MkldnnBase.h
Outdated
| DNN_SIZES, | ||
| DNN_FMTS, | ||
| DNN_ALL, | ||
| } DNN_LOG_LEVEL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
21-27行每行都需要注释。
ok
DNN_BASE和DNN_TESTS为什么都是1呢
这是是自己定的,为了方便区分名字,但是实际值没必要区分
这里的DNN需要换成MKLDNN么?
是可以改成DNN => MKLDNN的, 我是考虑名字改完后名字太长了~
这些都是VLOG的时候用的
paddle/gserver/layers/MkldnnBase.h
Outdated
| /** | ||
| * @brief Submit stream | ||
| * @param prims The primitives vector | ||
| * block Waiting for the stream to complete |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx
| } | ||
| // TODO(TJ): change me when mkldnn have method to reset this state | ||
| stream_.reset(new mkldnn::stream(mkldnn::stream::kind::eager)); | ||
| // stream_.reset(new mkldnn::stream(mkldnn::stream::kind::lazy)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个先留着吧,不建议去掉吧,因为是一种选择,现在不好说哪个性能好。加在下面对比用的。
| const ParameterMap& parameterMap) { | ||
| if (!MkldnnLayer::init(layerMap, parameterMap)) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为有返回值
| return false; | ||
| } | ||
|
|
||
| CHECK_EQ(inputLayers_.size(), 1) << "Only support one input layer yet!"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
paddle/gserver/layers/MkldnnLayer.h
Outdated
| */ | ||
| virtual void convertWeightsToPaddle() {} | ||
|
|
||
| void resetForwardFC(int bs, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
|
|
||
| // reset configs and layer names | ||
| configs_[DNN] = dnn; | ||
| configs_[REF] = ref; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
目前不好直接用,因为mkldnn的中间输出是内部格式的weight, 肯定与cpu的不一样。
并且我建议把mkldnn相关的检查与原有的方法区分开来,方便维护。
后期功能齐全了之后可以考虑用comparenet,目前仍然建议安装每个layer的逐一确认。
| } | ||
|
|
||
| void MkldnnTester::checkBackwardData() { | ||
| const bool isBN = dnnLayer_->getType() == "mkldnn_batch_norm"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个留着没关系吧,担心以后考虑忘了。并且这一行不会影响fc的逻辑。他也是属于mkldnn test的一部分,从这个角度也是属于mkldnn tester应该有的。
| TestConfig ref = cfg; | ||
| ref.layerConfig.set_type(compareTypes[1]); | ||
| for (auto bs : {pm.bs, 1}) { | ||
| tester.run(cfg, ref, bs, pm.ih, pm.iw); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里check的不是grad的波动等信息,并且check的点也不一样。这里着重比较在不同size下的与cpu各方面结果一致。
用c++ code更方便,也更好维护。如果用test_compareNet会更麻烦,其实这里等于说每个size的比较都需要写两个配置文件,这里用gtest只需要一行。
| if use_mkldnn and use_mkldnn_wgt: | ||
| dims = [self.config.size, input_layer.size] | ||
| else: | ||
| dims = [input_layer.size, self.config.size] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里你的逻辑是有问题的,在else部分,你需要多写一段code。我建议还是用现有的逻辑。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- use_mkldnn_wgt这个flag目前只有在单测中使用,未考虑pretrain的过程。这个flag对用户太不友好了,以后设计pretrain的时候需要重新考虑。因此现在为了单测,可以在mkldnnlayer.h里面创建一个变量 use_mkldnn_wgt,补充get和set这个变量的方法,然后用这个变量来控制单测。
- PR应该是相对独立和小的,所以和这个PR无关的信息,比如mkldnn_batch_norm,要去掉。等以后写到这块的时候,再加。
父类提供每个layer的方法,子类直接用接口就好了。
- mkldnnlayer是父类,写完后就不应该动了。不能写一个layer,就往父类中加该layer的方法,这样父类会越来越大。因此fc相关的代码还是要挪走。
mkldnn还需要很多primitive和memory的desc
从代码中看只是一些typedef的格式,不一定要放在父类中。
paddle/gserver/layers/MkldnnBase.h
Outdated
| DNN_SIZES, | ||
| DNN_FMTS, | ||
| DNN_ALL, | ||
| } DNN_LOG_LEVEL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为了和cudnn做区分,所以要改成MKLDNN。
如果两个都是1,直接用DNN_BASE就行了,不用再区分。
| } | ||
| // TODO(TJ): change me when mkldnn have method to reset this state | ||
| stream_.reset(new mkldnn::stream(mkldnn::stream::kind::eager)); | ||
| // stream_.reset(new mkldnn::stream(mkldnn::stream::kind::lazy)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果要留的话,也应该留在87行前面,和86行的注释连在一起。
| int batchSize = input.getBatchSize(); | ||
| if (bs_ == batchSize) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
从代码来看,除了 resetOutput(bs_, oc_)和batchsize有关,其他变量的设置都和bs_无关。bs_不变的时候,其他变量也不变么
| outGrad_->set_data_handle(topDiff); | ||
|
|
||
| stream_->submit(pipelineBwd_); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为这里父类为每个子类提供了不同的方法,但这些方法应该是在子类中实现。父类只是提供一个通用的方法,具体实现应该在子类中完成。
|
|
||
| #include "MkldnnLayer.h" | ||
|
|
||
| using mem = mkldnn::memory; // NOLINT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要修改,mklmem比mem更直观。
| // Then test FLAGS_use_mkldnn_wgt = true | ||
| FLAGS_use_mkldnn_wgt = true; | ||
| // after run once the mkldnn weight has been stored in dnnlayer | ||
| // then save the weigths and restart again |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weigths->weights
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx
| int bs; | ||
| int ic; | ||
| int oc; | ||
| int ih, iw; // oh == ow == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int bs;
int ic;
int ih;
int iw;
int oc;
按照顺序写。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
参考的是这里
| {INPUT_DATA, | ||
| "layer_0", | ||
| /* size of input layer= */ size_t(pm.ic * pm.ih * pm.iw), | ||
| /* size of weight= */ size_t(pm.oc * pm.ic * pm.ih * pm.iw)}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weight的size=pm.oc * pm.ic * pm.ih * pm.iw。对么?weight的size应该和output.value一样大。pm.oc * pm.oh *pm.ow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里weigth的size没有问题。请参考FC的实现原理。
| int ih, iw; // oh == ow == 1 | ||
| }; | ||
|
|
||
| void testFcLayer(const testFCDesc& pm) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
testMkldnnFcLayer。这样开发者grep源码的时候,不会与普通的testFcLayer混淆了。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的函数已经在test_Mkldnn里面,我觉得没有再加入MKLDNN的信息了。
开发者grep也会根据文件名区分的。
paddle/gserver/tests/test_Mkldnn.cpp
Outdated
| } | ||
|
|
||
| TEST(MkldnnLayer, fcLayer) { | ||
| testFcLayer({2, 2, 3, 1, 1}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TEST(MkldnnLayer, MkldnnFcLayer) {
testMkldnnFcLayer({/*bs*/ 2, /*xx*/ 2, /*xx*/ 3,...})
}
加上注释更直观。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其实在前面的有了信息定义,所以没加入,既然需求,稍后会加入。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use_mkldnn_wgt这个flag目前只有在单测中使用
实际上这个在config_parser.py里面也用了,是一个从外面传递进来的信息。
现在一时也不是仅仅为单测存在的。
并且也不完全是因为pretrain,inference的时候也需要这个信息。
我认为现在可以暂时先加入这个flag,后续有了非常完整的解决weight格式问题的时候再去掉,现在也不会有需要用户去touch到这个变量。并且这个变量在紧接着的下一个PR中再来想办法,我觉得更方便。
从代码中看只是一些typedef的格式,不一定要放在父类中。
目的不是因为typedef,而是本来准备所有的layer信息都汇总在这个父类,这样也整齐好管理。
但是如果不希望看到放在父类,那么会想办法挪到别处。
这个PR的内容是有点多,因为也算是真正的第一个功能性PR,需要准备的东西很多是后面公用的,后期每个PR的内容应该会少很多。
辛苦大家review 了,谢谢。
paddle/gserver/layers/MkldnnBase.h
Outdated
| DNN_SIZES, | ||
| DNN_FMTS, | ||
| DNN_ALL, | ||
| } DNN_LOG_LEVEL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里会改为MKLDNN_BASE。
还是要区分下,用的场合不一样,一个用做基本信息,一个仅仅用在test里面。只是值一样罢了,如果以后觉得值想不一样,改起来也很方便。
| } | ||
| // TODO(TJ): change me when mkldnn have method to reset this state | ||
| stream_.reset(new mkldnn::stream(mkldnn::stream::kind::eager)); | ||
| // stream_.reset(new mkldnn::stream(mkldnn::stream::kind::lazy)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,我挪一下位置。thx
| int batchSize = input.getBatchSize(); | ||
| if (bs_ == batchSize) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
后面的量一旦init之后,是不会变的,在FC里面会影响weight大小。
这一点我已经在code这里有过注释了
|
|
||
| #include "MkldnnLayer.h" | ||
|
|
||
| using mem = mkldnn::memory; // NOLINT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok,会调整。但是我觉得如果要改,mklmem也不好吧。采用use namespace吧,会把先都改为memory。
| outGrad_->set_data_handle(topDiff); | ||
|
|
||
| stream_->submit(pipelineBwd_); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那么我来调整下,想办法放到fc中。
@luotao1 @hedaoyuan
| int ih, iw; // oh == ow == 1 | ||
| }; | ||
|
|
||
| void testFcLayer(const testFCDesc& pm) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的函数已经在test_Mkldnn里面,我觉得没有再加入MKLDNN的信息了。
开发者grep也会根据文件名区分的。
| {INPUT_DATA, | ||
| "layer_0", | ||
| /* size of input layer= */ size_t(pm.ic * pm.ih * pm.iw), | ||
| /* size of weight= */ size_t(pm.oc * pm.ic * pm.ih * pm.iw)}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里weigth的size没有问题。请参考FC的实现原理。
paddle/gserver/tests/test_Mkldnn.cpp
Outdated
| } | ||
|
|
||
| TEST(MkldnnLayer, fcLayer) { | ||
| testFcLayer({2, 2, 3, 1, 1}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其实在前面的有了信息定义,所以没加入,既然需求,稍后会加入。
| if use_mkldnn and use_mkldnn_wgt: | ||
| dims = [self.config.size, input_layer.size] | ||
| else: | ||
| dims = [input_layer.size, self.config.size] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
代码风格问题,如果觉得那种方式好,会按照你的需求改动。
paddle/gserver/layers/MkldnnBase.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里参考是这里
后续可能还会考虑FPGA的engine。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- use_mkldnn_wgt这个flag如果改起来比较麻烦,建议在下一个PR先解决这个问题,再加入新layer。
- batch_norm还是建议删掉,如果要考虑后面的问题,那每个PR要考虑的会太多。我们无法预测后面出现的问题,所以每个PR应遵循“less is more”的设计思想。
- 关于mkldnnfclayer.cpp的写法 @hedaoyuan @Xreki 有什么意见么
paddle/gserver/tests/MkldnnTester.h
Outdated
| enum { | ||
| DNN = 0, | ||
| REF = 1, | ||
| NUM = 2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enum每行需要注释,特别是NUM表示的是什么,这个词太普通了,所以需要注释。
paddle/gserver/tests/MkldnnTester.h
Outdated
| * Get delta percent | ||
| * if many(>failRate) wrong(abs(dnn-ref)/abs(ref)>thres) points return the | ||
| * max(diff/ref) | ||
| * else return sum(abs(a-b)) / sum(abs(b)) should smaller than eps |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* if many(>failRate) wrong(abs(dnn-ref)/abs(ref)>thres) points:
* return the max(diff/ref)
* else:
* return sum(abs(a-b)) / sum(abs(b))
should smaller than eps这句表示什么?和前面的return连起来不通
| add_unittest_without_exec(test_Mkldnn | ||
| test_Mkldnn.cpp | ||
| MkldnnTester.cpp | ||
| LayerGradUtil.cpp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test_mkldnn依赖layerGradUtil.cpp么,从代码上看,是完全独立的,26行可以去掉了吧。
| size_t iLayerSize_; // == ic * ih * iw | ||
|
|
||
| bool hasInitedWgt_; | ||
| bool hasSpatial_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hasSpatial_这个变量用来做什么?求注释。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename done.
Mkldnn => MKLDNN and Cpu => CPU
paddle/gserver/layers/MkldnnBase.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
paddle/gserver/layers/MkldnnLayer.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
paddle/gserver/layers/MkldnnLayer.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
这个变量是用来判断输入的layer包不包含图像信息,就是iw和ih是不是有值,且都不为1。
没问题,我觉得可以提一个task在project里面或者提一个issue,我下一个PR要先解决这件事情。因为这个flag对我们都还挺重要的。
ok, 我先注释掉,然后加一个TODO在这里 Done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| bool hasInitedWgt_; | ||
|
|
||
| // if input layer has image size info (ih>1 && iw>1) | ||
| bool hasSpatial_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
照注释的意思,这个变量改成:hasImgSizeInfo_类似的更加直观。或者其他名字也可。
| NUM = 2, | ||
| DNN = 0, // MKLDNN layer | ||
| REF = 1, // Reference layer | ||
| NUM = 2, // Number of total |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
total是啥?Number of MKLDNN layer and Reference layer?
| * max(diff/ref) | ||
| * else return sum(abs(a-b)) / sum(abs(b)) should smaller than eps | ||
| * else return sum(abs(a-b)) / sum(abs(b)) | ||
| * The return value should smaller than eps when passing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value should be (缺少动词)
|
嗯表示的是一共就这两种,主要是为了避免magic number的存在,所以起了一个简单短小的名字。
嗯嗯,thx。如果问题不大的话,我下次一起改掉。 Thanks |
enable mkldnn_fc and gtest for mkldnn
fix #3383