Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
4c3e13de
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
4c3e13de
编写于
8月 02, 2022
作者:
H
houj04
提交者:
GitHub
8月 02, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[XPU] fp16 for layer_norm op (#44778)
* [XPU] fp16 for layer_norm op. test=kunlun
上级
c3d4a3d8
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
20 addition
and
21 deletion
+20
-21
paddle/fluid/operators/layer_norm_op_xpu.cc
paddle/fluid/operators/layer_norm_op_xpu.cc
+10
-14
paddle/fluid/platform/device/xpu/xpu2_op_list.h
paddle/fluid/platform/device/xpu/xpu2_op_list.h
+0
-1
python/paddle/fluid/tests/unittests/xpu/test_layer_norm_op_xpu.py
...addle/fluid/tests/unittests/xpu/test_layer_norm_op_xpu.py
+10
-5
python/paddle/fluid/tests/unittests/xpu/test_one_hot_op_xpu.py
...n/paddle/fluid/tests/unittests/xpu/test_one_hot_op_xpu.py
+0
-1
未找到文件。
paddle/fluid/operators/layer_norm_op_xpu.cc
浏览文件 @
4c3e13de
/* Copyright (c) 20
16
PaddlePaddle Authors. All Rights Reserved.
/* Copyright (c) 20
22
PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
you may not use this file except in compliance with the License.
...
@@ -15,6 +15,7 @@ limitations under the License. */
...
@@ -15,6 +15,7 @@ limitations under the License. */
#ifdef PADDLE_WITH_XPU
#ifdef PADDLE_WITH_XPU
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/platform/device/device_wrapper.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
...
@@ -48,6 +49,9 @@ class LayerNormXPUKernel : public framework::OpKernel<T> {
...
@@ -48,6 +49,9 @@ class LayerNormXPUKernel : public framework::OpKernel<T> {
auto
*
mean_data
=
mean
->
mutable_data
<
float
>
(
ctx
.
GetPlace
());
auto
*
mean_data
=
mean
->
mutable_data
<
float
>
(
ctx
.
GetPlace
());
auto
*
variance_data
=
variance
->
mutable_data
<
float
>
(
ctx
.
GetPlace
());
auto
*
variance_data
=
variance
->
mutable_data
<
float
>
(
ctx
.
GetPlace
());
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
// int layer_norm(Context* ctx, const T* x, T* y, int m, int n, float eps,
// const float* scale, const float* bias, float* mean, float* var);
int
r
=
xpu
::
layer_norm
(
dev_ctx
.
x_context
(),
int
r
=
xpu
::
layer_norm
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUType
*>
(
x_data
),
reinterpret_cast
<
const
XPUType
*>
(
x_data
),
reinterpret_cast
<
XPUType
*>
(
y_data
),
reinterpret_cast
<
XPUType
*>
(
y_data
),
...
@@ -58,12 +62,7 @@ class LayerNormXPUKernel : public framework::OpKernel<T> {
...
@@ -58,12 +62,7 @@ class LayerNormXPUKernel : public framework::OpKernel<T> {
bias_data
,
bias_data
,
mean_data
,
mean_data
,
variance_data
);
variance_data
);
PADDLE_ENFORCE_EQ
(
r
,
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"layer_norm"
);
XPU_SUCCESS
,
platform
::
errors
::
External
(
"XPU layer_norm kernel return wrong value[%d %s]"
,
r
,
XPUAPIErrorMsg
[
r
]));
}
}
};
};
...
@@ -103,6 +102,9 @@ class LayerNormGradXPUKernel : public framework::OpKernel<T> {
...
@@ -103,6 +102,9 @@ class LayerNormGradXPUKernel : public framework::OpKernel<T> {
(
dx
==
nullptr
?
nullptr
:
dx
->
mutable_data
<
T
>
(
ctx
.
GetPlace
()));
(
dx
==
nullptr
?
nullptr
:
dx
->
mutable_data
<
T
>
(
ctx
.
GetPlace
()));
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
// int layer_norm_grad(Context* ctx, const T* x, const T* dy, T* dx, int m,
// int n, float eps, const float* scale, const float* mean, const float*
// var, float* dscale, float* dbias);
int
r
=
xpu
::
layer_norm_grad
(
dev_ctx
.
x_context
(),
int
r
=
xpu
::
layer_norm_grad
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUType
*>
(
x_data
),
reinterpret_cast
<
const
XPUType
*>
(
x_data
),
reinterpret_cast
<
const
XPUType
*>
(
dy_data
),
reinterpret_cast
<
const
XPUType
*>
(
dy_data
),
...
@@ -115,13 +117,7 @@ class LayerNormGradXPUKernel : public framework::OpKernel<T> {
...
@@ -115,13 +117,7 @@ class LayerNormGradXPUKernel : public framework::OpKernel<T> {
variance_data
,
variance_data
,
dscale_data
,
dscale_data
,
dbias_data
);
dbias_data
);
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"layer_norm_grad"
);
r
,
XPU_SUCCESS
,
platform
::
errors
::
External
(
"XPU layer_norm_grad kernel return wrong value[%d %s]"
,
r
,
XPUAPIErrorMsg
[
r
]));
}
}
};
};
...
...
paddle/fluid/platform/device/xpu/xpu2_op_list.h
浏览文件 @
4c3e13de
...
@@ -266,7 +266,6 @@ XPUOpMap& get_kl2_ops() {
...
@@ -266,7 +266,6 @@ XPUOpMap& get_kl2_ops() {
{
"layer_norm_grad"
,
{
"layer_norm_grad"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
()),
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
()),
pOpKernelType
(
vartype
::
FP16
,
XPUPlace
())})},
pOpKernelType
(
vartype
::
FP16
,
XPUPlace
())})},
{
"layer_norm"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
())})},
{
"layer_norm"
,
{
"layer_norm"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
()),
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
()),
pOpKernelType
(
vartype
::
FP16
,
XPUPlace
())})},
pOpKernelType
(
vartype
::
FP16
,
XPUPlace
())})},
...
...
python/paddle/fluid/tests/unittests/xpu/test_layer_norm_op_xpu.py
浏览文件 @
4c3e13de
# Copyright (c) 202
0
PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 202
2
PaddlePaddle Authors. All Rights Reserved.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# you may not use this file except in compliance with the License.
...
@@ -60,14 +60,19 @@ class XPUTestLayerNormOp(XPUOpTestWrapper):
...
@@ -60,14 +60,19 @@ class XPUTestLayerNormOp(XPUOpTestWrapper):
self
.
begin_norm_axis
=
1
self
.
begin_norm_axis
=
1
self
.
set_attrs
()
self
.
set_attrs
()
self
.
atol
=
1e-4
if
self
.
dtype
==
np
.
float16
:
self
.
atol
=
1e-2
right
=
reduce
(
mul
,
right
=
reduce
(
mul
,
self
.
shape
[
self
.
begin_norm_axis
:
len
(
self
.
shape
)],
1
)
self
.
shape
[
self
.
begin_norm_axis
:
len
(
self
.
shape
)],
1
)
np
.
random
.
seed
(
10
)
np
.
random
.
seed
(
10
)
x_np
=
np
.
random
.
uniform
(
0.1
,
1
,
self
.
shape
).
astype
(
self
.
dtype
)
x_np
=
np
.
random
.
uniform
(
0.1
,
1
,
self
.
shape
).
astype
(
self
.
dtype
)
scale_np
=
np
.
random
.
uniform
(
0.1
,
1
,
[
right
]).
astype
(
self
.
dtype
)
scale_np
=
np
.
random
.
uniform
(
0.1
,
1
,
[
right
]).
astype
(
'float32'
)
bias_np
=
np
.
random
.
uniform
(
0.1
,
1
,
[
right
]).
astype
(
self
.
dtype
)
bias_np
=
np
.
random
.
uniform
(
0.1
,
1
,
[
right
]).
astype
(
'float32'
)
ref_y_np
,
ref_mean_np
,
ref_variance_np
=
ref_layer_norm
(
ref_y_np
,
ref_mean_np
,
ref_variance_np
=
ref_layer_norm
(
x_np
,
scale_np
,
bias_np
,
self
.
epsilon
,
self
.
begin_norm_axis
)
x_np
,
scale_np
,
bias_np
,
self
.
epsilon
,
self
.
begin_norm_axis
)
ref_y_np
=
ref_y_np
.
astype
(
self
.
dtype
)
self
.
inputs
=
{
'X'
:
x_np
,
'Scale'
:
scale_np
,
'Bias'
:
bias_np
}
self
.
inputs
=
{
'X'
:
x_np
,
'Scale'
:
scale_np
,
'Bias'
:
bias_np
}
self
.
outputs
=
{
self
.
outputs
=
{
...
@@ -84,12 +89,12 @@ class XPUTestLayerNormOp(XPUOpTestWrapper):
...
@@ -84,12 +89,12 @@ class XPUTestLayerNormOp(XPUOpTestWrapper):
pass
pass
def
test_check_output
(
self
):
def
test_check_output
(
self
):
self
.
check_output_with_place
(
paddle
.
XPUPlace
(
0
),
atol
=
1e-4
)
self
.
check_output_with_place
(
paddle
.
XPUPlace
(
0
),
atol
=
self
.
atol
)
def
test_check_grad
(
self
):
def
test_check_grad
(
self
):
self
.
check_grad_with_place
(
paddle
.
XPUPlace
(
0
),
[
'X'
],
self
.
check_grad_with_place
(
paddle
.
XPUPlace
(
0
),
[
'X'
],
'Y'
,
'Y'
,
max_relative_error
=
0.02
)
max_relative_error
=
self
.
atol
)
class
TestXPULayerNormOpAxis2
(
TestXPULayerNormOp
):
class
TestXPULayerNormOpAxis2
(
TestXPULayerNormOp
):
...
...
python/paddle/fluid/tests/unittests/xpu/test_one_hot_op_xpu.py
浏览文件 @
4c3e13de
...
@@ -121,7 +121,6 @@ class XPUTestOneHotOP(XPUOpTestWrapper):
...
@@ -121,7 +121,6 @@ class XPUTestOneHotOP(XPUOpTestWrapper):
support_types
=
get_xpu_op_support_types
(
'one_hot'
)
support_types
=
get_xpu_op_support_types
(
'one_hot'
)
print
(
"support_types: %s"
%
str
(
support_types
))
for
stype
in
support_types
:
for
stype
in
support_types
:
create_test_class
(
globals
(),
XPUTestOneHotOP
,
stype
)
create_test_class
(
globals
(),
XPUTestOneHotOP
,
stype
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录