Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
54205c99
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
54205c99
编写于
12月 04, 2017
作者:
T
tensor-tang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add MKLDNNLRNLayer
上级
4786ad14
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
241 addition
and
0 deletion
+241
-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/layers/MKLDNNLRNLayer.cpp
0 → 100644
浏览文件 @
54205c99
/* 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
浏览文件 @
54205c99
/* 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
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录