Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
192cc5dd
P
PaddleDetection
项目概览
s920243400
/
PaddleDetection
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleDetection
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
192cc5dd
编写于
3月 13, 2018
作者:
T
Tomasz Patejko
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implementation of MKLDNN LRN
上级
a431f984
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
253 addition
and
1 deletion
+253
-1
paddle/fluid/operators/lrn_mkldnn_op.cc
paddle/fluid/operators/lrn_mkldnn_op.cc
+189
-0
paddle/fluid/operators/lrn_op.cc
paddle/fluid/operators/lrn_op.cc
+54
-1
python/paddle/fluid/tests/unittests/test_lrn_op.py
python/paddle/fluid/tests/unittests/test_lrn_op.py
+10
-0
未找到文件。
paddle/fluid/operators/lrn_mkldnn_op.cc
0 → 100644
浏览文件 @
192cc5dd
/* Copyright (c) 2018 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 "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/operators/lrn_op.h"
#include "paddle/fluid/platform/mkldnn_helper.h"
namespace
paddle
{
namespace
operators
{
using
paddle
::
framework
::
Tensor
;
using
paddle
::
platform
::
MKLDNNDeviceContext
;
namespace
{
mkldnn
::
algorithm
LRNAlgorithm
(
const
paddle
::
framework
::
ExecutionContext
&
ctx
)
{
mkldnn
::
algorithm
algorithm
=
mkldnn
::
lrn_across_channels
;
std
::
string
algorithm_str
=
ctx
.
Attr
<
std
::
string
>
(
"algorithm"
);
if
(
algorithm_str
==
"WITHIN_CHANNEL"
)
{
algorithm
=
mkldnn
::
lrn_within_channel
;
}
return
algorithm
;
}
}
// namespace
template
<
typename
T
>
class
LRNMKLDNNOpKernel
:
public
paddle
::
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
paddle
::
framework
::
ExecutionContext
&
ctx
)
const
override
{
PADDLE_ENFORCE
(
std
::
is_same
<
T
,
float
>::
value
,
"MKLDNN LRN must use float data."
);
PADDLE_ENFORCE
(
paddle
::
platform
::
is_cpu_place
(
ctx
.
GetPlace
()),
"MKLDNN LRN must use CPUPlace."
);
auto
&
dev_ctx
=
ctx
.
template
device_context
<
MKLDNNDeviceContext
>();
const
auto
&
mkldnn_engine
=
dev_ctx
.
GetEngine
();
auto
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
out
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
auto
mid
=
ctx
.
Output
<
Tensor
>
(
"MidOut"
);
auto
input_data
=
x
->
data
<
T
>
();
auto
output_data
=
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
mid
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
const
std
::
string
key
=
ctx
.
op
().
Output
(
"Out"
);
const
std
::
string
key_src_memory
=
key
+
"@lrn_src_memory"
;
const
std
::
string
key_pd
=
key
+
"@lrn_pd"
;
const
std
::
string
key_workspace_memory
=
key
+
"@lrn_workspace_memory"
;
const
int
n
=
ctx
.
Attr
<
int
>
(
"n"
);
const
float
alpha
=
ctx
.
Attr
<
float
>
(
"alpha"
);
const
float
beta
=
ctx
.
Attr
<
float
>
(
"beta"
);
const
float
k
=
ctx
.
Attr
<
float
>
(
"k"
);
auto
algorithm
=
LRNAlgorithm
(
ctx
);
auto
e_mid
=
framework
::
EigenTensor
<
T
,
4
>::
From
(
*
mid
);
e_mid
=
e_mid
.
constant
(
k
);
auto
dims
=
paddle
::
framework
::
vectorize2int
(
x
->
dims
());
auto
src_md
=
paddle
::
platform
::
MKLDNNMemDesc
(
dims
,
mkldnn
::
memory
::
data_type
::
f32
,
mkldnn
::
memory
::
format
::
nchw
);
auto
dst_md
=
paddle
::
platform
::
MKLDNNMemDesc
(
dims
,
mkldnn
::
memory
::
data_type
::
f32
,
mkldnn
::
memory
::
format
::
nchw
);
auto
forward_desc
=
mkldnn
::
lrn_forward
::
desc
{
mkldnn
::
prop_kind
::
forward
,
algorithm
,
src_md
,
n
,
alpha
,
beta
,
k
};
auto
forward_pd
=
std
::
make_shared
<
mkldnn
::
lrn_forward
::
primitive_desc
>
(
forward_desc
,
mkldnn_engine
);
dev_ctx
.
SetBlob
(
key_pd
,
forward_pd
);
auto
src_memory_pd
=
mkldnn
::
memory
::
primitive_desc
{
src_md
,
mkldnn_engine
};
auto
src_memory
=
std
::
make_shared
<
mkldnn
::
memory
>
(
src_memory_pd
,
static_cast
<
void
*>
(
const_cast
<
float
*>
(
input_data
)));
dev_ctx
.
SetBlob
(
key_src_memory
,
src_memory
);
auto
dst_memory
=
mkldnn
::
memory
{{
dst_md
,
mkldnn_engine
},
static_cast
<
void
*>
(
output_data
)};
auto
workspace_md
=
forward_pd
->
workspace_primitive_desc
();
auto
workspace_memory
=
std
::
make_shared
<
mkldnn
::
memory
>
(
workspace_md
);
dev_ctx
.
SetBlob
(
key_workspace_memory
,
workspace_memory
);
auto
forward_op
=
mkldnn
::
lrn_forward
{
*
forward_pd
,
*
src_memory
,
*
workspace_memory
,
dst_memory
};
std
::
vector
<
mkldnn
::
primitive
>
pipeline
=
{
forward_op
};
mkldnn
::
stream
(
mkldnn
::
stream
::
kind
::
eager
).
submit
(
pipeline
).
wait
();
}
};
template
<
typename
T
>
class
LRNMKLDNNGradOpKernel
:
public
paddle
::
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
paddle
::
framework
::
ExecutionContext
&
ctx
)
const
override
{
PADDLE_ENFORCE
(
std
::
is_same
<
T
,
float
>::
value
,
"MKLDNN LRN must use float data."
);
PADDLE_ENFORCE
(
paddle
::
platform
::
is_cpu_place
(
ctx
.
GetPlace
()),
"MKLDNN LRN must use CPUPlace."
);
auto
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
out_grad
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
x_grad
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
const
std
::
string
key
=
ctx
.
op
().
Input
(
"Out"
);
const
std
::
string
key_src_memory
=
key
+
"@lrn_src_memory"
;
const
std
::
string
key_pd
=
key
+
"@lrn_pd"
;
const
std
::
string
key_workspace_memory
=
key
+
"@lrn_workspace_memory"
;
const
int
n
=
ctx
.
Attr
<
int
>
(
"n"
);
const
float
alpha
=
ctx
.
Attr
<
float
>
(
"alpha"
);
const
float
beta
=
ctx
.
Attr
<
float
>
(
"beta"
);
const
float
k
=
ctx
.
Attr
<
float
>
(
"k"
);
auto
&
dev_ctx
=
ctx
.
template
device_context
<
MKLDNNDeviceContext
>();
const
auto
&
mkldnn_engine
=
dev_ctx
.
GetEngine
();
auto
x_grad_data
=
x_grad
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
out_grad_data
=
out_grad
->
data
<
T
>
();
auto
dims
=
paddle
::
framework
::
vectorize2int
(
x
->
dims
());
auto
src_md
=
paddle
::
platform
::
MKLDNNMemDesc
(
dims
,
mkldnn
::
memory
::
data_type
::
f32
,
mkldnn
::
memory
::
format
::
nchw
);
auto
diff_src_md
=
paddle
::
platform
::
MKLDNNMemDesc
(
dims
,
mkldnn
::
memory
::
data_type
::
f32
,
mkldnn
::
memory
::
format
::
nchw
);
auto
diff_dst_md
=
paddle
::
platform
::
MKLDNNMemDesc
(
dims
,
mkldnn
::
memory
::
data_type
::
f32
,
mkldnn
::
memory
::
format
::
nchw
);
auto
diff_dst_memory
=
mkldnn
::
memory
{{
diff_dst_md
,
mkldnn_engine
},
static_cast
<
void
*>
(
const_cast
<
float
*>
(
out_grad_data
))};
auto
diff_src_memory
=
mkldnn
::
memory
{{
diff_src_md
,
mkldnn_engine
},
static_cast
<
void
*>
(
x_grad_data
)};
auto
algorithm
=
LRNAlgorithm
(
ctx
);
auto
backward_desc
=
mkldnn
::
lrn_backward
::
desc
{
algorithm
,
src_md
,
diff_src_md
,
n
,
alpha
,
beta
,
k
};
auto
forward_pd
=
dev_ctx
.
GetBlob
(
key_pd
);
auto
backward_pd
=
mkldnn
::
lrn_backward
::
primitive_desc
{
backward_desc
,
mkldnn_engine
,
*
static_cast
<
mkldnn
::
lrn_forward
::
primitive_desc
*>
(
forward_pd
.
get
())};
std
::
shared_ptr
<
void
>
workspace_memory
=
dev_ctx
.
GetBlob
(
key_workspace_memory
);
auto
src_memory
=
dev_ctx
.
GetBlob
(
key_src_memory
);
auto
backward_op
=
mkldnn
::
lrn_backward
{
backward_pd
,
*
static_cast
<
mkldnn
::
memory
*>
(
src_memory
.
get
()),
diff_dst_memory
,
*
static_cast
<
mkldnn
::
memory
*>
(
workspace_memory
.
get
()),
diff_src_memory
};
std
::
vector
<
mkldnn
::
primitive
>
pipeline
=
{
backward_op
};
mkldnn
::
stream
(
mkldnn
::
stream
::
kind
::
eager
).
submit
(
pipeline
).
wait
();
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_KERNEL
(
lrn
,
MKLDNN
,
paddle
::
platform
::
CPUPlace
,
ops
::
LRNMKLDNNOpKernel
<
float
>
);
REGISTER_OP_KERNEL
(
lrn_grad
,
MKLDNN
,
paddle
::
platform
::
CPUPlace
,
ops
::
LRNMKLDNNGradOpKernel
<
float
>
);
paddle/fluid/operators/lrn_op.cc
浏览文件 @
192cc5dd
...
...
@@ -13,6 +13,9 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/operators/lrn_op.h"
#ifdef PADDLE_WITH_MKLDNN
#include "paddle/fluid/platform/mkldnn_helper.h"
#endif
namespace
paddle
{
namespace
operators
{
...
...
@@ -135,6 +138,24 @@ class LRNOp : public framework::OperatorWithKernel {
ctx
->
SetOutputDim
(
"MidOut"
,
x_dim
);
ctx
->
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
framework
::
LibraryType
library_
{
framework
::
LibraryType
::
kPlain
};
#ifdef PADDLE_WITH_MKLDNN
if
(
library_
==
framework
::
LibraryType
::
kPlain
&&
platform
::
CanMKLDNNBeUsed
(
ctx
))
{
library_
=
framework
::
LibraryType
::
kMKLDNN
;
}
#endif
std
::
string
data_format
=
ctx
.
Attr
<
std
::
string
>
(
"data_format"
);
// TODO(pzelazko-intel): enable MKLDNN layout when it's ready
framework
::
DataLayout
layout_
=
framework
::
StringToDataLayout
(
data_format
);
return
framework
::
OpKernelType
(
framework
::
ToDataType
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
type
()),
ctx
.
GetPlace
(),
layout_
,
library_
);
}
};
template
<
typename
T
>
...
...
@@ -176,6 +197,21 @@ class LRNOpMaker : public framework::OpProtoAndCheckerMaker {
"beta is the power number."
)
.
SetDefault
(
0.75
)
.
GreaterThan
(
0.0
);
AddAttr
<
bool
>
(
"use_mkldnn"
,
"(bool, default false) Only used in mkldnn kernel"
)
.
SetDefault
(
false
);
AddAttr
<
std
::
string
>
(
"data_format"
,
"(string, default NCHW) Only used in "
"An optional string from:
\"
NHWC
\"
,
\"
NCHW
\"
. "
"Defaults to
\"
NHWC
\"
. Specify the data format of the output data, "
"the input will be transformed automatically. "
)
.
SetDefault
(
"AnyLayout"
);
AddAttr
<
std
::
string
>
(
"algorithm"
,
"(string default ACROSS_CHANNELS"
"An optional string:
\"
ACROSS_CHANNELS
\"
, "
"
\"
WITHIN_CHANNEL
\"
. Used by MKLDNN library"
)
.
SetDefault
(
"ACROSS_CHANNELS"
);
AddComment
(
R"DOC(
Local Response Normalization Operator.
...
...
@@ -223,8 +259,25 @@ class LRNOpGrad : public framework::OperatorWithKernel {
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"X"
),
x_dims
);
}
};
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
framework
::
LibraryType
library_
{
framework
::
LibraryType
::
kPlain
};
#ifdef PADDLE_WITH_MKLDNN
if
(
library_
==
framework
::
LibraryType
::
kPlain
&&
platform
::
CanMKLDNNBeUsed
(
ctx
))
{
library_
=
framework
::
LibraryType
::
kMKLDNN
;
}
#endif
std
::
string
data_format
=
ctx
.
Attr
<
std
::
string
>
(
"data_format"
);
// TODO(pzelazko-intel): enable MKLDNN layout when it's ready
framework
::
DataLayout
layout_
=
framework
::
StringToDataLayout
(
data_format
);
return
framework
::
OpKernelType
(
framework
::
ToDataType
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
type
()),
ctx
.
GetPlace
(),
layout_
,
library_
);
}
};
}
// namespace operators
}
// namespace paddle
...
...
python/paddle/fluid/tests/unittests/test_lrn_op.py
浏览文件 @
192cc5dd
...
...
@@ -87,5 +87,15 @@ class TestLRNOp(OpTest):
self
.
check_grad
([
'X'
],
'Out'
,
max_relative_error
=
0.01
)
class
TestLRNMKLDNNOp
(
TestLRNOp
):
def
get_attrs
(
self
):
attrs
=
TestLRNOp
.
get_attrs
(
self
)
attrs
[
'use_mkldnn'
]
=
True
return
attrs
def
test_check_output
(
self
):
self
.
check_output
(
atol
=
0.002
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录