Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OPTHREE
Paddle
提交
bcafe317
P
Paddle
项目概览
OPTHREE
/
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
bcafe317
编写于
4月 03, 2020
作者:
F
Feiyu Chan
提交者:
GitHub
4月 03, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add MKL computation back to gelu's non-approximate part (#23420)
上级
dbfbd7ea
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
55 addition
and
0 deletion
+55
-0
paddle/fluid/operators/gelu_op.h
paddle/fluid/operators/gelu_op.h
+55
-0
未找到文件。
paddle/fluid/operators/gelu_op.h
浏览文件 @
bcafe317
...
...
@@ -41,9 +41,28 @@ struct GeluFunctor {
.
tanh
();
out
.
device
(
d
)
=
x
*
static_cast
<
T
>
(
0.5
)
*
(
static_cast
<
T
>
(
1
)
+
temp
);
}
else
{
#if defined(PADDLE_WITH_MKLML) && !defined(_WIN32) && !defined(__APPLE__) && \
!defined(__OSX__) && !defined(PADDLE_WITH_CUDA)
auto
x_data
=
x
.
data
();
auto
out_data
=
out
.
data
();
int
n
=
std
::
min
(
x
.
size
(),
out
.
size
());
std
::
memset
(
out_data
,
0
,
n
*
sizeof
(
T
));
math
::
CBlas
<
T
>::
AXPY
(
n
,
static_cast
<
T
>
(
M_SQRT1_2
),
x_data
,
1
,
out_data
,
1
);
math
::
CBlas
<
T
>::
VMERF
(
n
,
out_data
,
out_data
,
VML_LA
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
out_data
[
i
]
+=
static_cast
<
T
>
(
1
);
}
math
::
CBlas
<
T
>::
VMUL
(
n
,
x_data
,
out_data
,
out_data
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
out_data
[
i
]
*=
static_cast
<
T
>
(
0.5
);
}
#else
// gelu(x) = 0.5 * x * (1 + erf(x / sqrt(2)))
auto
temp
=
(
x
*
static_cast
<
T
>
(
M_SQRT1_2
)).
erf
();
out
.
device
(
d
)
=
x
*
static_cast
<
T
>
(
0.5
)
*
(
static_cast
<
T
>
(
1
)
+
temp
);
#endif
}
}
};
...
...
@@ -61,6 +80,41 @@ struct GeluGradFunctor {
(
static_cast
<
T
>
(
1
)
+
y
+
(
x
-
x
*
y
.
square
())
*
(
kAlpha
+
kBeta
*
x
.
square
()));
}
else
{
#if defined(PADDLE_WITH_MKLML) && !defined(_WIN32) && !defined(__APPLE__) && \
!defined(__OSX__) && !defined(PADDLE_WITH_CUDA)
auto
x_data
=
x
.
data
();
auto
dx_data
=
dx
.
data
();
auto
dout_data
=
dout
.
data
();
int
n
=
std
::
min
(
x
.
size
(),
dx
.
size
());
auto
first
=
static_cast
<
T
*>
(
std
::
malloc
(
n
*
sizeof
(
T
)));
std
::
memset
(
first
,
0
,
n
*
sizeof
(
T
));
auto
second
=
static_cast
<
T
*>
(
std
::
malloc
(
n
*
sizeof
(
T
)));
std
::
memset
(
second
,
0
,
n
*
sizeof
(
T
));
// first = (0.5 * (1 + erf(x / sqrt(2))))
math
::
CBlas
<
T
>::
AXPY
(
n
,
static_cast
<
T
>
(
M_SQRT1_2
),
x_data
,
1
,
first
,
1
);
math
::
CBlas
<
T
>::
VMERF
(
n
,
first
,
first
,
VML_LA
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
first
[
i
]
+=
static_cast
<
T
>
(
1
);
}
math
::
CBlas
<
T
>::
SCAL
(
n
,
static_cast
<
T
>
(
0.5
),
first
,
1
);
// second = (0.5 * 2/sqrt(pi) * 1/sqrt(2) * x * exp(-0.5 * x^2))
math
::
CBlas
<
T
>::
VSQUARE
(
n
,
x_data
,
second
);
math
::
CBlas
<
T
>::
SCAL
(
n
,
-
static_cast
<
T
>
(
0.5
),
second
,
1
);
math
::
CBlas
<
T
>::
VEXP
(
n
,
second
,
second
);
math
::
CBlas
<
T
>::
VMUL
(
n
,
x_data
,
second
,
second
);
math
::
CBlas
<
T
>::
SCAL
(
n
,
static_cast
<
T
>
(
0.5
*
M_2_SQRTPI
*
M_SQRT1_2
),
second
,
1
);
// dx = dout * (first + second);
math
::
CBlas
<
T
>::
VADD
(
n
,
first
,
second
,
first
);
math
::
CBlas
<
T
>::
VMUL
(
n
,
dout_data
,
first
,
dx_data
);
std
::
free
(
first
);
std
::
free
(
second
);
#else
// gelu_grad(x) = dout * 0.5 * (1 + erf(x / sqrt(2)) + x * sqrt(2 / pi) *
// exp(- x^2 / 2)
auto
first
=
...
...
@@ -70,6 +124,7 @@ struct GeluGradFunctor {
auto
second
=
static_cast
<
T
>
(
0.5
*
M_2_SQRTPI
*
M_SQRT1_2
)
*
x
*
(
-
static_cast
<
T
>
(
0.5
)
*
x
.
square
()).
exp
();
dx
.
device
(
d
)
=
dout
*
(
first
+
second
);
#endif
}
}
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录