Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
f269614b
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
f269614b
编写于
8月 24, 2018
作者:
T
tensor-tang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
further optimize tanh with avx and mkl
上级
c70a3fec
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
90 addition
and
85 deletion
+90
-85
paddle/fluid/operators/math/cpu_vec.h
paddle/fluid/operators/math/cpu_vec.h
+90
-85
未找到文件。
paddle/fluid/operators/math/cpu_vec.h
浏览文件 @
f269614b
...
@@ -45,6 +45,13 @@ inline void vec_exp(const int n, const T* x, T* y) {
...
@@ -45,6 +45,13 @@ inline void vec_exp(const int n, const T* x, T* y) {
}
}
}
}
template
<
typename
T
>
inline
void
vec_scal
(
const
int
n
,
const
T
a
,
T
*
x
)
{
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
x
[
i
]
=
a
*
x
[
i
];
}
}
#ifdef PADDLE_WITH_MKLML
#ifdef PADDLE_WITH_MKLML
template
<
>
template
<
>
inline
void
vec_exp
<
float
>
(
const
int
n
,
const
float
*
x
,
float
*
y
)
{
inline
void
vec_exp
<
float
>
(
const
int
n
,
const
float
*
x
,
float
*
y
)
{
...
@@ -55,7 +62,74 @@ template <>
...
@@ -55,7 +62,74 @@ template <>
inline
void
vec_exp
<
double
>
(
const
int
n
,
const
double
*
x
,
double
*
y
)
{
inline
void
vec_exp
<
double
>
(
const
int
n
,
const
double
*
x
,
double
*
y
)
{
platform
::
dynload
::
vdExp
(
n
,
x
,
y
);
platform
::
dynload
::
vdExp
(
n
,
x
,
y
);
}
}
template
<
>
inline
void
vec_scal
<
float
>
(
const
int
n
,
const
float
a
,
float
*
x
)
{
platform
::
dynload
::
cblas_sscal
(
n
,
a
,
x
,
1
);
}
template
<
>
inline
void
vec_scal
<
double
>
(
const
int
n
,
const
double
a
,
double
*
x
)
{
platform
::
dynload
::
cblas_dscal
(
n
,
a
,
x
,
1
);
}
#endif
// MKL scal only support inplace, choose this if src and dst are not equal
template
<
typename
T
,
platform
::
jit
::
cpu_isa_t
isa
=
platform
::
jit
::
isa_any
>
inline
void
vec_scal
(
const
int
n
,
const
T
a
,
const
T
*
x
,
T
*
y
)
{
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
y
[
i
]
=
a
*
x
[
i
];
}
}
template
<
>
inline
void
vec_scal
<
float
,
platform
::
jit
::
avx
>
(
const
int
n
,
const
float
a
,
const
float
*
x
,
float
*
y
)
{
#ifdef __AVX__
constexpr
int
block
=
AVX_FLOAT_BLOCK
;
if
(
n
<
block
*
4
)
{
// use larger threshold, since small ones has no boost
vec_scal
<
float
,
platform
::
jit
::
isa_any
>
(
n
,
a
,
x
,
y
);
return
;
}
const
int
rest
=
n
%
block
;
const
int
end
=
n
-
rest
;
int
i
=
0
;
__m256
scalar
=
_mm256_set1_ps
(
a
);
__m256
tmp
;
#define MOVE_ONE_STEP \
tmp = _mm256_loadu_ps(x + i); \
tmp = _mm256_mul_ps(tmp, scalar); \
_mm256_storeu_ps(y + i, tmp)
for
(
i
=
0
;
i
<
end
;
i
+=
block
)
{
MOVE_ONE_STEP
;
}
#undef MOVE_ONE_STEP
if
(
rest
==
0
)
{
return
;
}
// can not continue move step if src and dst are inplace
for
(
i
=
n
-
rest
;
i
<
n
;
++
i
)
{
y
[
i
]
=
a
*
x
[
i
];
}
#else
vec_scal
<
float
,
platform
::
jit
::
isa_any
>
(
n
,
a
,
x
,
y
);
#endif
#endif
}
template
<
>
inline
void
vec_scal
<
float
,
platform
::
jit
::
avx2
>
(
const
int
n
,
const
float
a
,
const
float
*
x
,
float
*
y
)
{
vec_scal
<
float
,
platform
::
jit
::
avx
>
(
n
,
a
,
x
,
y
);
}
template
<
>
inline
void
vec_scal
<
float
,
platform
::
jit
::
avx512_common
>
(
const
int
n
,
const
float
a
,
const
float
*
x
,
float
*
y
)
{
// TODO(TJ): enable me
vec_scal
<
float
,
platform
::
jit
::
avx2
>
(
n
,
a
,
x
,
y
);
}
template
<
typename
T
,
platform
::
jit
::
cpu_isa_t
isa
=
platform
::
jit
::
isa_any
>
template
<
typename
T
,
platform
::
jit
::
cpu_isa_t
isa
=
platform
::
jit
::
isa_any
>
inline
void
vec_identity
(
const
int
n
,
const
T
*
x
,
T
*
y
)
{
inline
void
vec_identity
(
const
int
n
,
const
T
*
x
,
T
*
y
)
{
...
@@ -82,7 +156,7 @@ inline void vec_sigmoid<float, platform::jit::avx>(const int n, const float* x,
...
@@ -82,7 +156,7 @@ inline void vec_sigmoid<float, platform::jit::avx>(const int n, const float* x,
float
*
y
)
{
float
*
y
)
{
#ifdef __AVX__
#ifdef __AVX__
constexpr
int
block
=
AVX_FLOAT_BLOCK
;
constexpr
int
block
=
AVX_FLOAT_BLOCK
;
if
(
n
<
block
)
{
// can use larger threshold if necessary
if
(
n
<
block
)
{
vec_sigmoid
<
float
,
platform
::
jit
::
isa_any
>
(
n
,
x
,
y
);
vec_sigmoid
<
float
,
platform
::
jit
::
isa_any
>
(
n
,
x
,
y
);
return
;
return
;
}
}
...
@@ -102,11 +176,15 @@ inline void vec_sigmoid<float, platform::jit::avx>(const int n, const float* x,
...
@@ -102,11 +176,15 @@ inline void vec_sigmoid<float, platform::jit::avx>(const int n, const float* x,
for
(
i
=
0
;
i
<
end
;
i
+=
block
)
{
for
(
i
=
0
;
i
<
end
;
i
+=
block
)
{
MOVE_ONE_STEP
;
MOVE_ONE_STEP
;
}
}
#undef MOVE_ONE_STEP
if
(
rest
!=
0
)
{
if
(
rest
!=
0
)
{
i
=
n
-
block
;
// can not continue move step since the src and dst address could be equal
MOVE_ONE_STEP
;
const
float
xmin
=
SIGMOID_THRESHOLD_MIN
;
const
float
xmax
=
SIGMOID_THRESHOLD_MAX
;
for
(
i
=
n
-
rest
;
i
<
n
;
++
i
)
{
y
[
i
]
=
0.
f
-
((
x
[
i
]
<
xmin
)
?
xmin
:
((
x
[
i
]
>
xmax
)
?
xmax
:
x
[
i
]));
}
}
}
#undef MOVE_ONE_STEP
vec_exp
<
float
>
(
n
,
y
,
y
);
vec_exp
<
float
>
(
n
,
y
,
y
);
...
@@ -142,65 +220,17 @@ template <>
...
@@ -142,65 +220,17 @@ template <>
inline
void
vec_sigmoid
<
float
,
platform
::
jit
::
avx512_common
>
(
const
int
n
,
inline
void
vec_sigmoid
<
float
,
platform
::
jit
::
avx512_common
>
(
const
int
n
,
const
float
*
x
,
const
float
*
x
,
float
*
y
)
{
float
*
y
)
{
#ifdef __AVX512F__
// TODO(TJ): enable me
constexpr
int
block
=
AVX512_FLOAT_BLOCK
;
vec_sigmoid
<
float
,
platform
::
jit
::
avx2
>
(
n
,
x
,
y
);
if
(
n
<
block
)
{
vec_sigmoid
<
float
,
platform
::
jit
::
isa_any
>
(
n
,
x
,
y
);
return
;
}
const
int
rest
=
n
%
block
;
const
int
end
=
n
-
rest
;
int
i
=
0
;
__m512
max
=
_mm512_set1_ps
(
SIGMOID_THRESHOLD_MAX
);
__m512
min
=
_mm512_set1_ps
(
SIGMOID_THRESHOLD_MIN
);
__m512
zeros
=
_mm512_setzero_ps
();
__m512
tmp
;
#define MOVE_ONE_STEP \
tmp = _mm512_loadu_ps(x + i); \
tmp = _mm512_max_ps(tmp, min); \
tmp = _mm512_min_ps(tmp, max); \
tmp = _mm512_sub_ps(zeros, tmp); \
_mm512_storeu_ps(y + i, tmp)
for
(
i
=
0
;
i
<
end
;
i
+=
block
)
{
MOVE_ONE_STEP
;
}
if
(
rest
!=
0
)
{
i
=
n
-
block
;
MOVE_ONE_STEP
;
}
#undef MOVE_ONE_STEP
vec_exp
<
float
>
(
n
,
y
,
y
);
__m512
ones
=
_mm512_set1_ps
(
1.0
f
);
#define MOVE_ONE_STEP \
tmp = _mm512_loadu_ps(y + i); \
tmp = _mm512_add_ps(ones, tmp); \
tmp = _mm512_div_ps(ones, tmp); \
_mm512_storeu_ps(y + i, tmp)
for
(
i
=
0
;
i
<
end
;
i
+=
block
)
{
MOVE_ONE_STEP
;
}
#undef MOVE_ONE_STEP
if
(
rest
==
0
)
{
return
;
}
for
(
i
=
n
-
rest
;
i
<
n
;
++
i
)
{
y
[
i
]
=
1.
f
/
(
1.
f
+
y
[
i
]);
}
#else
vec_sigmoid
<
float
,
platform
::
jit
::
isa_any
>
(
n
,
x
,
y
);
#endif
}
}
template
<
typename
T
,
platform
::
jit
::
cpu_isa_t
isa
=
platform
::
jit
::
isa_any
>
template
<
typename
T
,
platform
::
jit
::
cpu_isa_t
isa
=
platform
::
jit
::
isa_any
>
inline
void
vec_tanh
(
const
int
n
,
const
T
*
x
,
T
*
y
)
{
inline
void
vec_tanh
(
const
int
n
,
const
T
*
x
,
T
*
y
)
{
vec_scal
<
T
,
isa
>
(
n
,
static_cast
<
T
>
(
2
),
x
,
y
);
vec_sigmoid
<
T
,
isa
>
(
n
,
y
,
y
);
vec_scal
<
T
>
(
n
,
static_cast
<
T
>
(
2
),
y
);
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
y
[
i
]
=
static_cast
<
T
>
(
2
)
*
x
[
i
];
y
[
i
]
=
y
[
i
]
-
static_cast
<
T
>
(
1
);
}
vec_sigmoid
<
T
>
(
n
,
y
,
y
);
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
y
[
i
]
=
static_cast
<
T
>
(
2
)
*
y
[
i
]
-
static_cast
<
T
>
(
1
);
}
}
}
}
...
@@ -255,35 +285,10 @@ template <>
...
@@ -255,35 +285,10 @@ template <>
inline
void
vec_relu
<
float
,
platform
::
jit
::
avx512_common
>
(
const
int
n
,
inline
void
vec_relu
<
float
,
platform
::
jit
::
avx512_common
>
(
const
int
n
,
const
float
*
x
,
const
float
*
x
,
float
*
y
)
{
float
*
y
)
{
#ifdef __AVX512F__
// TODO(TJ): enable me
// test me
constexpr
int
block
=
AVX512_FLOAT_BLOCK
;
if
(
n
<
block
)
{
vec_relu
<
float
,
platform
::
jit
::
avx2
>
(
n
,
x
,
y
);
return
;
}
const
int
rest
=
n
%
block
;
const
int
end
=
n
-
rest
;
int
i
=
0
;
__m512
zeros
=
_mm512_setzero_ps
();
__m512
tmp
;
#define MOVE_ONE_STEP \
tmp = _mm512_loadu_ps(x + i); \
tmp = _mm512_max_ps(tmp, zeros); \
_mm512_storeu_ps(y + i, tmp)
for
(
i
=
0
;
i
<
end
;
i
+=
block
)
{
MOVE_ONE_STEP
;
}
if
(
rest
==
0
)
{
return
;
}
i
=
n
-
block
;
MOVE_ONE_STEP
;
#undef MOVE_ONE_STEP
#else
vec_relu
<
float
,
platform
::
jit
::
avx2
>
(
n
,
x
,
y
);
vec_relu
<
float
,
platform
::
jit
::
avx2
>
(
n
,
x
,
y
);
#endif
}
}
// TODO(TJ): add vec add bias, make relu clip
// TODO(TJ): optimize double of sigmoid, tanh and relu if necessary
// TODO(TJ): optimize double of sigmoid, tanh and relu if necessary
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录