Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
a34fc8b3
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
a34fc8b3
编写于
12月 07, 2017
作者:
T
Tao Luo
提交者:
GitHub
12月 07, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #6213 from tensor-tang/mkldnn_lrn
add MKLDNN LRN
上级
584c9cfc
f13d725a
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
294 addition
and
0 deletion
+294
-0
paddle/gserver/layers/MKLDNNLRNLayer.cpp
paddle/gserver/layers/MKLDNNLRNLayer.cpp
+163
-0
paddle/gserver/layers/MKLDNNLRNLayer.h
paddle/gserver/layers/MKLDNNLRNLayer.h
+78
-0
paddle/gserver/tests/mkldnn_simple_net.conf
paddle/gserver/tests/mkldnn_simple_net.conf
+2
-0
paddle/gserver/tests/test_MKLDNN.cpp
paddle/gserver/tests/test_MKLDNN.cpp
+45
-0
python/paddle/trainer/config_parser.py
python/paddle/trainer/config_parser.py
+6
-0
未找到文件。
paddle/gserver/layers/MKLDNNLRNLayer.cpp
0 → 100644
浏览文件 @
a34fc8b3
/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "MKLDNNLRNLayer.h"
#include "paddle/utils/Logging.h"
using
namespace
mkldnn
;
// NOLINT
typedef
memory
::
format
format
;
namespace
paddle
{
REGISTER_LAYER
(
mkldnn_lrn
,
MKLDNNLRNLayer
);
bool
MKLDNNLRNLayer
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
if
(
!
MKLDNNLayer
::
init
(
layerMap
,
parameterMap
))
{
return
false
;
}
/* the size of inputs for norm-layer is 1 */
CHECK_EQ
(
config_
.
inputs_size
(),
1UL
);
const
NormConfig
&
conf
=
config_
.
inputs
(
0
).
norm_conf
();
localSize_
=
conf
.
size
();
alpha_
=
conf
.
scale
();
beta_
=
conf
.
pow
();
ic_
=
conf
.
channels
();
oc_
=
ic_
;
iw_
=
conf
.
img_size
();
ow_
=
conf
.
output_x
();
ih_
=
conf
.
has_img_size_y
()
?
conf
.
img_size_y
()
:
conf
.
img_size
();
oh_
=
conf
.
has_output_y
()
?
conf
.
output_y
()
:
conf
.
output_x
();
CHECK_EQ
(
iw_
,
ow_
);
CHECK_EQ
(
ih_
,
oh_
);
return
true
;
}
void
MKLDNNLRNLayer
::
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
{
CHECK_EQ
(
inputLayers_
.
size
(),
1UL
);
reshapeInput
(
bs
,
ih
,
iw
);
// ic_ and oc can not be changed
CHECK_EQ
((
size_t
)
ic
,
inputLayers_
[
0
]
->
getOutputValue
()
->
getElementCnt
()
/
bs
/
ih
/
iw
)
<<
"Input channel can not be changed"
;
oh
=
ih
;
ow
=
iw
;
reshapeOutput
(
oh
,
ow
);
resizeOutput
(
bs
,
oc
*
oh
*
ow
);
}
void
MKLDNNLRNLayer
::
resetFwd
(
std
::
vector
<
primitive
>&
pipeline
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
resetFwdBuffers
(
inputs
[
0
],
out
);
resetFwdPD
(
fwdPD_
,
inputs
[
0
],
out
);
resetFwdPipeline
(
pipeline
,
fwdPD_
,
inputs
[
0
],
out
);
}
void
MKLDNNLRNLayer
::
resetBwd
(
std
::
vector
<
primitive
>&
pipeline
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
std
::
shared_ptr
<
lrn_bwd
::
primitive_desc
>
pd
;
resetBwdBuffers
(
inputs
[
0
],
out
);
resetBwdPD
(
pd
,
inputs
[
0
],
out
);
resetBwdPipeline
(
pipeline
,
pd
,
inputs
[
0
],
out
);
}
void
MKLDNNLRNLayer
::
resetFwdBuffers
(
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
)
{
resetInValue
(
in
);
CHECK
(
in
);
resetOutValue
(
out
,
in
->
getPrimitiveDesc
());
}
void
MKLDNNLRNLayer
::
resetFwdPD
(
std
::
shared_ptr
<
lrn_fwd
::
primitive_desc
>&
pd
,
MKLDNNMatrixPtr
in
,
MKLDNNMatrixPtr
out
)
{
prop_kind
pk
=
passType_
==
PASS_TEST
?
prop_kind
::
forward_scoring
:
prop_kind
::
forward_training
;
auto
fwdDesc
=
lrn_fwd
::
desc
(
pk
,
algorithm
::
lrn_across_channels
,
in
->
getMemoryDesc
(),
localSize_
,
alpha_
,
beta_
,
1.0
f
);
pd
.
reset
(
new
lrn_fwd
::
primitive_desc
(
fwdDesc
,
engine_
));
// prepare workspace if necessary
workspace_
=
passType_
!=
PASS_TEST
?
std
::
make_shared
<
memory
>
(
memory
(
pd
->
workspace_primitive_desc
()))
:
nullptr
;
}
void
MKLDNNLRNLayer
::
resetFwdPipeline
(
std
::
vector
<
primitive
>&
pipeline
,
std
::
shared_ptr
<
lrn_fwd
::
primitive_desc
>&
pd
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
)
{
fwd_
=
workspace_
?
std
::
make_shared
<
lrn_fwd
>
(
lrn_fwd
(
*
pd
,
*
in
,
*
workspace_
,
*
out
))
:
std
::
make_shared
<
lrn_fwd
>
(
lrn_fwd
(
*
pd
,
*
in
,
*
out
));
pipeline
.
push_back
(
*
fwd_
);
}
void
MKLDNNLRNLayer
::
resetBwdBuffers
(
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
)
{
CHECK
(
inVals_
[
0
]
&&
outVal_
);
resetOutGrad
(
out
,
outVal_
->
getPrimitiveDesc
());
resetInGrad
(
in
,
inVals_
[
0
]
->
getPrimitiveDesc
());
}
void
MKLDNNLRNLayer
::
resetBwdPD
(
std
::
shared_ptr
<
lrn_bwd
::
primitive_desc
>&
pd
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
)
{
pd
=
nullptr
;
if
(
in
==
nullptr
)
{
return
;
}
CHECK
(
out
);
auto
bwdDesc
=
lrn_bwd
::
desc
(
algorithm
::
lrn_across_channels
,
in
->
getMemoryDesc
(),
out
->
getMemoryDesc
(),
localSize_
,
alpha_
,
beta_
,
1.0
f
);
pd
.
reset
(
new
lrn_bwd
::
primitive_desc
(
bwdDesc
,
engine_
,
*
fwdPD_
));
}
void
MKLDNNLRNLayer
::
resetBwdPipeline
(
std
::
vector
<
primitive
>&
pipeline
,
std
::
shared_ptr
<
lrn_bwd
::
primitive_desc
>&
pd
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
)
{
if
(
pd
==
nullptr
)
{
return
;
}
CHECK
(
inVals_
[
0
]);
CHECK
(
workspace_
);
bwdData_
=
std
::
make_shared
<
lrn_bwd
>
(
lrn_bwd
(
*
pd
,
*
inVals_
[
0
],
*
out
,
*
workspace_
,
*
in
));
pipeline
.
push_back
(
*
bwdData_
);
}
}
// namespace paddle
paddle/gserver/layers/MKLDNNLRNLayer.h
0 → 100644
浏览文件 @
a34fc8b3
/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "MKLDNNLayer.h"
#include "mkldnn.hpp"
namespace
paddle
{
typedef
mkldnn
::
lrn_forward
lrn_fwd
;
typedef
mkldnn
::
lrn_backward
lrn_bwd
;
/**
* @brief A subclass of MKLDNNLayer LRN(Local Response Norm) layer.
*
* The config file api is mkldnn_lrn
*/
class
MKLDNNLRNLayer
:
public
MKLDNNLayer
{
protected:
// save forward primitive_desc, which can be used in backward
std
::
shared_ptr
<
lrn_fwd
::
primitive_desc
>
fwdPD_
;
// according to https://github.com/01org/mkl-dnn/blob/master/tests/gtests/
// test_lrn_backward.cpp, lrn need workspace for backward
std
::
shared_ptr
<
mkldnn
::
memory
>
workspace_
;
int
localSize_
;
float
alpha_
,
beta_
;
// scale and pow in paddle
public:
explicit
MKLDNNLRNLayer
(
const
LayerConfig
&
config
)
:
MKLDNNLayer
(
config
)
{}
~
MKLDNNLRNLayer
()
{}
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
override
;
void
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
override
;
void
resetFwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
void
resetBwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
protected:
void
resetFwdBuffers
(
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
);
void
resetFwdPD
(
std
::
shared_ptr
<
lrn_fwd
::
primitive_desc
>&
pd
,
MKLDNNMatrixPtr
in
,
MKLDNNMatrixPtr
out
);
void
resetFwdPipeline
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
std
::
shared_ptr
<
lrn_fwd
::
primitive_desc
>&
pd
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
);
void
resetBwdBuffers
(
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
);
void
resetBwdPD
(
std
::
shared_ptr
<
lrn_bwd
::
primitive_desc
>&
pd
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
);
void
resetBwdPipeline
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
std
::
shared_ptr
<
lrn_bwd
::
primitive_desc
>&
pd
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
);
};
}
// namespace paddle
paddle/gserver/tests/mkldnn_simple_net.conf
浏览文件 @
a34fc8b3
...
@@ -51,6 +51,8 @@ tmp = img_pool_layer(input=tmp,
...
@@ -51,6 +51,8 @@ tmp = img_pool_layer(input=tmp,
padding
=
1
,
padding
=
1
,
pool_type
=
MaxPooling
())
pool_type
=
MaxPooling
())
tmp
=
img_cmrnorm_layer
(
input
=
tmp
,
size
=
5
,
scale
=
0
.
0001
,
power
=
0
.
75
)
tmp
=
fc_layer
(
input
=
tmp
,
tmp
=
fc_layer
(
input
=
tmp
,
size
=
channels
,
size
=
channels
,
bias_attr
=
False
,
bias_attr
=
False
,
...
...
paddle/gserver/tests/test_MKLDNN.cpp
浏览文件 @
a34fc8b3
...
@@ -272,6 +272,51 @@ TEST(MKLDNNLayer, BatchNormLayer) {
...
@@ -272,6 +272,51 @@ TEST(MKLDNNLayer, BatchNormLayer) {
testBatchNormLayer
({
4
,
16
,
8
,
10
});
testBatchNormLayer
({
4
,
16
,
8
,
10
});
}
}
struct
testLRNDesc
{
int
bs
,
ic
,
ih
,
iw
;
float
scale
,
pow
;
int
localSize
;
};
void
getMKLDNNLRNConfig
(
TestConfig
&
cfg
,
const
testLRNDesc
&
pm
)
{
cfg
.
layerConfig
.
set_type
(
"mkldnn_lrn"
);
cfg
.
layerConfig
.
set_active_type
(
"relu"
);
size_t
layerSize
=
pm
.
ic
*
pm
.
ih
*
pm
.
iw
;
cfg
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
layerSize
,
0
});
LayerInputConfig
*
input
=
cfg
.
layerConfig
.
add_inputs
();
NormConfig
*
norm
=
input
->
mutable_norm_conf
();
norm
->
set_channels
(
pm
.
ic
);
norm
->
set_size
(
pm
.
localSize
);
norm
->
set_scale
(
pm
.
scale
);
norm
->
set_pow
(
pm
.
pow
);
norm
->
set_blocked
(
0
);
norm
->
set_img_size
(
pm
.
iw
);
norm
->
set_img_size_y
(
pm
.
ih
);
norm
->
set_output_x
(
norm
->
img_size
());
norm
->
set_output_y
(
norm
->
img_size_y
());
cfg
.
layerConfig
.
set_size
(
layerSize
);
cfg
.
biasSize
=
0
;
}
void
testLRNLayer
(
const
testLRNDesc
&
pm
)
{
TestConfig
dnnConfig
;
getMKLDNNLRNConfig
(
dnnConfig
,
pm
);
// mkldnn_lrn <==> norm with cmrnorm-projection type
TestConfig
refConfig
=
dnnConfig
;
refConfig
.
layerConfig
.
set_type
(
"norm"
);
LayerInputConfig
*
input
=
refConfig
.
layerConfig
.
mutable_inputs
(
0
);
NormConfig
*
norm
=
input
->
mutable_norm_conf
();
norm
->
set_norm_type
(
"cmrnorm-projection"
);
norm
->
set_scale
(
norm
->
scale
()
/
norm
->
size
());
RUN_MKLDNN_TEST
(
dnnConfig
,
refConfig
,
pm
)
}
TEST
(
MKLDNNLayer
,
LRNLayer
)
{
testLRNLayer
({
4
,
10
,
12
,
12
,
0.001
f
,
0.75
f
,
5
});
testLRNLayer
({
2
,
32
,
6
,
6
,
0.001
f
,
0.75
f
,
5
});
testLRNLayer
({
4
,
16
,
8
,
10
,
0.01
f
,
0.5
f
,
5
});
}
struct
testImageDesc
{
struct
testImageDesc
{
int
bs
,
ic
,
ih
,
iw
;
int
bs
,
ic
,
ih
,
iw
;
};
};
...
...
python/paddle/trainer/config_parser.py
浏览文件 @
a34fc8b3
...
@@ -2289,11 +2289,17 @@ class Conv3DLayer(Conv3DLayerBase):
...
@@ -2289,11 +2289,17 @@ class Conv3DLayer(Conv3DLayerBase):
class
NormLayer
(
LayerBase
):
class
NormLayer
(
LayerBase
):
def
__init__
(
self
,
name
,
inputs
,
**
xargs
):
def
__init__
(
self
,
name
,
inputs
,
**
xargs
):
super
(
NormLayer
,
self
).
__init__
(
name
,
'norm'
,
0
,
inputs
=
inputs
,
**
xargs
)
super
(
NormLayer
,
self
).
__init__
(
name
,
'norm'
,
0
,
inputs
=
inputs
,
**
xargs
)
use_mkldnn
=
bool
(
int
(
g_command_config_args
.
get
(
"use_mkldnn"
,
0
)))
use_mkldnn
=
True
if
use_mkldnn
and
self
.
inputs
[
0
].
norm
.
norm_type
==
'cmrnorm-projection'
else
False
self
.
config
.
type
=
'mkldnn_lrn'
if
use_mkldnn
else
self
.
config
.
type
for
input_index
in
xrange
(
len
(
self
.
inputs
)):
for
input_index
in
xrange
(
len
(
self
.
inputs
)):
input_layer
=
self
.
get_input_layer
(
input_index
)
input_layer
=
self
.
get_input_layer
(
input_index
)
norm_conf
=
self
.
config
.
inputs
[
input_index
].
norm_conf
norm_conf
=
self
.
config
.
inputs
[
input_index
].
norm_conf
parse_norm
(
self
.
inputs
[
input_index
].
norm
,
input_layer
.
name
,
parse_norm
(
self
.
inputs
[
input_index
].
norm
,
input_layer
.
name
,
norm_conf
)
norm_conf
)
norm_conf
.
scale
=
self
.
inputs
[
input_index
].
norm
.
scale
if
use_mkldnn
else
norm_conf
.
scale
self
.
set_cnn_layer
(
name
,
norm_conf
.
output_y
,
norm_conf
.
output_x
,
self
.
set_cnn_layer
(
name
,
norm_conf
.
output_y
,
norm_conf
.
output_x
,
norm_conf
.
channels
,
False
)
norm_conf
.
channels
,
False
)
if
norm_conf
.
norm_type
==
"cross-channel-norm"
:
if
norm_conf
.
norm_type
==
"cross-channel-norm"
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录