Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
c4a5c960
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
331
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
271
列表
看板
标记
里程碑
合并请求
78
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle-Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
271
Issue
271
列表
看板
标记
里程碑
合并请求
78
合并请求
78
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
c4a5c960
编写于
9月 25, 2020
作者:
Q
Qi Li
提交者:
GitHub
9月 25, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[X86] add new kernel of relu6 and reduce_mean, test=develop (#4431)
上级
ea4fc0bc
变更
8
显示空白变更内容
内联
并排
Showing
8 changed file
with
132 addition
and
16 deletion
+132
-16
lite/kernels/x86/activation_compute.cc
lite/kernels/x86/activation_compute.cc
+11
-0
lite/kernels/x86/activation_compute.h
lite/kernels/x86/activation_compute.h
+36
-0
lite/kernels/x86/reduce_compute.cc
lite/kernels/x86/reduce_compute.cc
+10
-0
lite/kernels/x86/reduce_compute.h
lite/kernels/x86/reduce_compute.h
+58
-12
lite/operators/activation_ops.cc
lite/operators/activation_ops.cc
+3
-0
lite/operators/op_params.h
lite/operators/op_params.h
+2
-0
lite/tests/kernels/activation_compute_test.cc
lite/tests/kernels/activation_compute_test.cc
+8
-1
lite/tests/kernels/reduce_mean_compute_test.cc
lite/tests/kernels/reduce_mean_compute_test.cc
+4
-3
未找到文件。
lite/kernels/x86/activation_compute.cc
浏览文件 @
c4a5c960
...
@@ -88,3 +88,14 @@ REGISTER_LITE_KERNEL(sigmoid,
...
@@ -88,3 +88,14 @@ REGISTER_LITE_KERNEL(sigmoid,
.
BindInput
(
"X"
,
{
LiteType
::
GetTensorTy
(
TARGET
(
kX86
))})
.
BindInput
(
"X"
,
{
LiteType
::
GetTensorTy
(
TARGET
(
kX86
))})
.
BindOutput
(
"Out"
,
{
LiteType
::
GetTensorTy
(
TARGET
(
kX86
))})
.
BindOutput
(
"Out"
,
{
LiteType
::
GetTensorTy
(
TARGET
(
kX86
))})
.
Finalize
();
.
Finalize
();
// float
REGISTER_LITE_KERNEL
(
relu6
,
kX86
,
kFloat
,
kNCHW
,
paddle
::
lite
::
kernels
::
x86
::
Relu6Compute
<
float
>
,
def
)
.
BindInput
(
"X"
,
{
LiteType
::
GetTensorTy
(
TARGET
(
kX86
))})
.
BindOutput
(
"Out"
,
{
LiteType
::
GetTensorTy
(
TARGET
(
kX86
))})
.
Finalize
();
lite/kernels/x86/activation_compute.h
浏览文件 @
c4a5c960
...
@@ -248,6 +248,42 @@ class SoftsignCompute : public KernelLite<TARGET(kX86), PRECISION(kFloat)> {
...
@@ -248,6 +248,42 @@ class SoftsignCompute : public KernelLite<TARGET(kX86), PRECISION(kFloat)> {
virtual
~
SoftsignCompute
()
=
default
;
virtual
~
SoftsignCompute
()
=
default
;
};
};
// relu6(x) = min(max(0, x), 6)
template
<
typename
T
>
struct
Relu6Functor
{
float
threshold
;
explicit
Relu6Functor
(
float
threshold_
)
:
threshold
(
threshold_
)
{}
template
<
typename
Device
,
typename
X
,
typename
Out
>
void
operator
()(
Device
d
,
X
x
,
Out
out
)
const
{
out
.
device
(
d
)
=
x
.
cwiseMax
(
static_cast
<
T
>
(
0
)).
cwiseMin
(
static_cast
<
T
>
(
threshold
));
}
};
template
<
typename
T
>
class
Relu6Compute
:
public
KernelLite
<
TARGET
(
kX86
),
PRECISION
(
kFloat
)
>
{
public:
using
param_t
=
operators
::
ActivationParam
;
void
Run
()
override
{
auto
&
param
=
*
param_
.
get_mutable
<
operators
::
ActivationParam
>
();
param
.
Out
->
template
mutable_data
<
T
>();
auto
X
=
param
.
X
;
auto
Out
=
param
.
Out
;
auto
place
=
lite
::
fluid
::
EigenDeviceType
<
TARGET
(
kX86
)
>
();
CHECK
(
X
);
CHECK
(
Out
);
auto
x
=
lite
::
fluid
::
EigenVector
<
T
>::
Flatten
(
*
X
);
auto
out
=
lite
::
fluid
::
EigenVector
<
T
>::
Flatten
(
*
Out
);
Relu6Functor
<
T
>
functor
(
param
.
threshold
);
functor
(
place
,
x
,
out
);
}
virtual
~
Relu6Compute
()
=
default
;
};
}
// namespace x86
}
// namespace x86
}
// namespace kernels
}
// namespace kernels
}
// namespace lite
}
// namespace lite
...
...
lite/kernels/x86/reduce_compute.cc
浏览文件 @
c4a5c960
...
@@ -23,3 +23,13 @@ REGISTER_LITE_KERNEL(reduce_sum,
...
@@ -23,3 +23,13 @@ REGISTER_LITE_KERNEL(reduce_sum,
.
BindInput
(
"X"
,
{
LiteType
::
GetTensorTy
(
TARGET
(
kX86
))})
.
BindInput
(
"X"
,
{
LiteType
::
GetTensorTy
(
TARGET
(
kX86
))})
.
BindOutput
(
"Out"
,
{
LiteType
::
GetTensorTy
(
TARGET
(
kX86
))})
.
BindOutput
(
"Out"
,
{
LiteType
::
GetTensorTy
(
TARGET
(
kX86
))})
.
Finalize
();
.
Finalize
();
REGISTER_LITE_KERNEL
(
reduce_mean
,
kX86
,
kFloat
,
kNCHW
,
paddle
::
lite
::
kernels
::
x86
::
ReduceMeanCompute
<
float
>
,
def
)
.
BindInput
(
"X"
,
{
LiteType
::
GetTensorTy
(
TARGET
(
kX86
))})
.
BindOutput
(
"Out"
,
{
LiteType
::
GetTensorTy
(
TARGET
(
kX86
))})
.
Finalize
();
lite/kernels/x86/reduce_compute.h
浏览文件 @
c4a5c960
...
@@ -31,10 +31,17 @@ struct SumFunctor {
...
@@ -31,10 +31,17 @@ struct SumFunctor {
}
}
};
};
#define HANDLE_DIM(NDIM, RDIM) \
struct
MeanFunctor
{
template
<
typename
X
,
typename
Y
,
typename
Dim
>
void
operator
()(
X
*
x
,
Y
*
y
,
const
Dim
&
dim
)
{
y
->
device
(
lite
::
fluid
::
EigenDeviceType
<
TARGET
(
kX86
)
>
())
=
x
->
mean
(
dim
);
}
};
#define HANDLE_DIM(NDIM, RDIM, FUNCTOR) \
if (ndim == NDIM && rdim == RDIM) { \
if (ndim == NDIM && rdim == RDIM) { \
paddle::lite::kernels::x86:: \
paddle::lite::kernels::x86:: \
ReduceFunctor<lite::TargetType::kX86, T, NDIM, RDIM,
SumFunctor
>( \
ReduceFunctor<lite::TargetType::kX86, T, NDIM, RDIM,
FUNCTOR
>( \
*input, output, dims, keep_dim); \
*input, output, dims, keep_dim); \
}
}
...
@@ -64,19 +71,58 @@ class ReduceSumCompute : public KernelLite<TARGET(kX86), PRECISION(kFloat)> {
...
@@ -64,19 +71,58 @@ class ReduceSumCompute : public KernelLite<TARGET(kX86), PRECISION(kFloat)> {
}
else
{
}
else
{
int
ndim
=
input
->
dims
().
size
();
int
ndim
=
input
->
dims
().
size
();
int
rdim
=
dims
.
size
();
int
rdim
=
dims
.
size
();
HANDLE_DIM
(
4
,
3
);
HANDLE_DIM
(
4
,
3
,
SumFunctor
);
HANDLE_DIM
(
4
,
2
);
HANDLE_DIM
(
4
,
2
,
SumFunctor
);
HANDLE_DIM
(
4
,
1
);
HANDLE_DIM
(
4
,
1
,
SumFunctor
);
HANDLE_DIM
(
3
,
2
);
HANDLE_DIM
(
3
,
2
,
SumFunctor
);
HANDLE_DIM
(
3
,
1
);
HANDLE_DIM
(
3
,
1
,
SumFunctor
);
HANDLE_DIM
(
2
,
1
);
HANDLE_DIM
(
2
,
1
,
SumFunctor
);
HANDLE_DIM
(
1
,
1
);
HANDLE_DIM
(
1
,
1
,
SumFunctor
);
}
}
}
}
virtual
~
ReduceSumCompute
()
=
default
;
virtual
~
ReduceSumCompute
()
=
default
;
};
};
template
<
typename
T
>
class
ReduceMeanCompute
:
public
KernelLite
<
TARGET
(
kX86
),
PRECISION
(
kFloat
)
>
{
public:
using
param_t
=
operators
::
ReduceParam
;
void
Run
()
override
{
auto
&
param
=
*
param_
.
get_mutable
<
operators
::
ReduceParam
>
();
// auto& context = ctx_->As<X86Context>();
auto
*
input
=
param
.
x
;
auto
*
output
=
param
.
output
;
param
.
output
->
template
mutable_data
<
T
>();
const
auto
&
dims
=
param
.
dim
;
bool
keep_dim
=
param
.
keep_dim
;
if
(
dims
.
size
()
==
0
)
{
// Flatten and reduce 1-D tensor
auto
x
=
lite
::
fluid
::
EigenVector
<
T
>::
Flatten
(
*
input
);
auto
out
=
lite
::
fluid
::
EigenScalar
<
T
>::
From
(
output
);
// auto& place = *platform::CPUDeviceContext().eigen_device();
auto
reduce_dim
=
Eigen
::
array
<
int
,
1
>
({{
0
}});
MeanFunctor
functor
;
functor
(
&
x
,
&
out
,
reduce_dim
);
}
else
{
int
ndim
=
input
->
dims
().
size
();
int
rdim
=
dims
.
size
();
HANDLE_DIM
(
4
,
3
,
MeanFunctor
);
HANDLE_DIM
(
4
,
2
,
MeanFunctor
);
HANDLE_DIM
(
4
,
1
,
MeanFunctor
);
HANDLE_DIM
(
3
,
2
,
MeanFunctor
);
HANDLE_DIM
(
3
,
1
,
MeanFunctor
);
HANDLE_DIM
(
2
,
1
,
MeanFunctor
);
HANDLE_DIM
(
1
,
1
,
MeanFunctor
);
}
}
virtual
~
ReduceMeanCompute
()
=
default
;
};
}
// namespace x86
}
// namespace x86
}
// namespace kernels
}
// namespace kernels
}
// namespace lite
}
// namespace lite
...
...
lite/operators/activation_ops.cc
浏览文件 @
c4a5c960
...
@@ -89,6 +89,9 @@ bool ActivationOp::AttachImpl(const cpp::OpDesc& opdesc, lite::Scope* scope) {
...
@@ -89,6 +89,9 @@ bool ActivationOp::AttachImpl(const cpp::OpDesc& opdesc, lite::Scope* scope) {
}
else
if
(
opdesc
.
Type
()
==
"elu"
)
{
}
else
if
(
opdesc
.
Type
()
==
"elu"
)
{
param_
.
active_type
=
lite_api
::
ActivationType
::
kElu
;
param_
.
active_type
=
lite_api
::
ActivationType
::
kElu
;
param_
.
Elu_alpha
=
opdesc
.
GetAttr
<
float
>
(
"alpha"
);
param_
.
Elu_alpha
=
opdesc
.
GetAttr
<
float
>
(
"alpha"
);
}
else
if
(
opdesc
.
Type
()
==
"relu6"
)
{
param_
.
active_type
=
lite_api
::
ActivationType
::
kRelu6
;
param_
.
threshold
=
opdesc
.
GetAttr
<
float
>
(
"threshold"
);
}
}
VLOG
(
4
)
<<
"opdesc.Type():"
<<
opdesc
.
Type
();
VLOG
(
4
)
<<
"opdesc.Type():"
<<
opdesc
.
Type
();
...
...
lite/operators/op_params.h
浏览文件 @
c4a5c960
...
@@ -403,6 +403,8 @@ struct ActivationParam : ParamBase {
...
@@ -403,6 +403,8 @@ struct ActivationParam : ParamBase {
float
relu_threshold
{
1.0
f
};
float
relu_threshold
{
1.0
f
};
// elu
// elu
float
Elu_alpha
{
1.0
f
};
float
Elu_alpha
{
1.0
f
};
// relu6
float
threshold
{
6.0
f
};
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
// get a vector of input tensors
// get a vector of input tensors
...
...
lite/tests/kernels/activation_compute_test.cc
浏览文件 @
c4a5c960
...
@@ -58,6 +58,7 @@ class ActivationComputeTester : public arena::TestCase {
...
@@ -58,6 +58,7 @@ class ActivationComputeTester : public arena::TestCase {
float
hard_swish_offset
=
3.0
;
float
hard_swish_offset
=
3.0
;
float
relu_threshold_
=
1.0
;
float
relu_threshold_
=
1.0
;
float
elu_alpha_
=
1.0
;
float
elu_alpha_
=
1.0
;
float
threshold_
=
6.0
;
DDim
dims_
{{
1
}};
DDim
dims_
{{
1
}};
std
::
string
type_
=
""
;
std
::
string
type_
=
""
;
activation_type_test
act_type_
=
RELU
;
activation_type_test
act_type_
=
RELU
;
...
@@ -170,7 +171,8 @@ class ActivationComputeTester : public arena::TestCase {
...
@@ -170,7 +171,8 @@ class ActivationComputeTester : public arena::TestCase {
case
RELU6
:
{
case
RELU6
:
{
for
(
int
i
=
0
;
i
<
dims_
.
production
();
i
++
)
{
for
(
int
i
=
0
;
i
<
dims_
.
production
();
i
++
)
{
output_data
[
i
]
=
x_data
[
i
]
>
0.
f
?
x_data
[
i
]
:
0.
f
;
output_data
[
i
]
=
x_data
[
i
]
>
0.
f
?
x_data
[
i
]
:
0.
f
;
output_data
[
i
]
=
output_data
[
i
]
<
6.0
?
output_data
[
i
]
:
6.0
;
output_data
[
i
]
=
output_data
[
i
]
<
threshold_
?
output_data
[
i
]
:
threshold_
;
}
}
break
;
break
;
}
}
...
@@ -273,6 +275,9 @@ class ActivationComputeTester : public arena::TestCase {
...
@@ -273,6 +275,9 @@ class ActivationComputeTester : public arena::TestCase {
if
(
act_type_
==
ELU
)
{
if
(
act_type_
==
ELU
)
{
op_desc
->
SetAttr
(
"alpha"
,
elu_alpha_
);
op_desc
->
SetAttr
(
"alpha"
,
elu_alpha_
);
}
}
if
(
act_type_
==
RELU6
)
{
op_desc
->
SetAttr
(
"threshold"
,
threshold_
);
}
}
}
void
PrepareData
()
override
{
void
PrepareData
()
override
{
...
@@ -510,6 +515,8 @@ TEST(Activation_relu6, precision) {
...
@@ -510,6 +515,8 @@ TEST(Activation_relu6, precision) {
#elif defined(LITE_WITH_HUAWEI_ASCEND_NPU)
#elif defined(LITE_WITH_HUAWEI_ASCEND_NPU)
place
=
TARGET
(
kHuaweiAscendNPU
);
place
=
TARGET
(
kHuaweiAscendNPU
);
abs_error
=
1e-2
;
// precision_mode default is force_fp16
abs_error
=
1e-2
;
// precision_mode default is force_fp16
#elif defined(LITE_WITH_X86)
place
=
TARGET
(
kX86
);
#else
#else
return
;
return
;
#endif
#endif
...
...
lite/tests/kernels/reduce_mean_compute_test.cc
浏览文件 @
c4a5c960
...
@@ -333,9 +333,10 @@ void test_reduce_mean(Place place) {
...
@@ -333,9 +333,10 @@ void test_reduce_mean(Place place) {
}
}
TEST
(
ReduceMean
,
precision
)
{
TEST
(
ReduceMean
,
precision
)
{
// #ifdef LITE_WITH_X86
#ifdef LITE_WITH_X86
// Place place(TARGET(kX86));
Place
place
(
TARGET
(
kX86
));
// #endif
test_reduce_mean
(
place
);
#endif
#ifdef LITE_WITH_ARM
#ifdef LITE_WITH_ARM
Place
place
(
TARGET
(
kARM
));
Place
place
(
TARGET
(
kARM
));
test_reduce_mean
(
place
);
test_reduce_mean
(
place
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录