Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
c4b6d1ae
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
未验证
提交
c4b6d1ae
编写于
3月 29, 2023
作者:
Y
YuhangLi
提交者:
GitHub
3月 29, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[AMP OP&Test]label_smooth op fp/bf16 support (#52193)
上级
b8b0712c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
94 addition
and
5 deletion
+94
-5
paddle/phi/kernels/gpu/label_smooth_grad_kernel.cu
paddle/phi/kernels/gpu/label_smooth_grad_kernel.cu
+2
-1
paddle/phi/kernels/gpu/label_smooth_kernel.cu
paddle/phi/kernels/gpu/label_smooth_kernel.cu
+2
-1
python/paddle/fluid/tests/unittests/test_label_smooth_op.py
python/paddle/fluid/tests/unittests/test_label_smooth_op.py
+86
-2
python/paddle/nn/functional/common.py
python/paddle/nn/functional/common.py
+4
-1
未找到文件。
paddle/phi/kernels/gpu/label_smooth_grad_kernel.cu
浏览文件 @
c4b6d1ae
...
...
@@ -55,4 +55,5 @@ PD_REGISTER_KERNEL(label_smooth_grad,
phi
::
LabelSmoothGradKernel
,
float
,
double
,
phi
::
dtype
::
float16
)
{}
phi
::
dtype
::
float16
,
phi
::
dtype
::
bfloat16
)
{}
paddle/phi/kernels/gpu/label_smooth_kernel.cu
浏览文件 @
c4b6d1ae
...
...
@@ -96,4 +96,5 @@ PD_REGISTER_KERNEL(label_smooth,
phi
::
LabelSmoothKernel
,
float
,
double
,
phi
::
dtype
::
float16
)
{}
phi
::
dtype
::
float16
,
phi
::
dtype
::
bfloat16
)
{}
python/paddle/fluid/tests/unittests/test_label_smooth_op.py
浏览文件 @
c4b6d1ae
...
...
@@ -15,9 +15,10 @@
import
unittest
import
numpy
as
np
from
eager_op_test
import
OpTest
from
op_test
import
OpTest
,
convert_float_to_uint16
import
paddle
from
paddle.fluid
import
core
class
TestLabelSmoothOp
(
OpTest
):
...
...
@@ -50,6 +51,39 @@ class TestLabelSmoothOp(OpTest):
self
.
check_grad
([
"X"
],
"Out"
)
@
unittest
.
skipIf
(
not
core
.
is_compiled_with_cuda
()
or
not
core
.
supports_bfloat16
(),
"core is not compiled with CUDA or place do not support bfloat16"
,
)
class
TestLabelSmoothOpBF16
(
OpTest
):
def
config
(
self
):
self
.
op_type
=
"label_smooth"
self
.
python_api
=
paddle
.
nn
.
functional
.
label_smooth
self
.
epsilon
=
0.1
self
.
dtype
=
np
.
uint16
batch_size
,
self
.
label_dim
=
10
,
12
self
.
label
=
np
.
zeros
((
batch_size
,
self
.
label_dim
)).
astype
(
np
.
float32
)
nonzero_index
=
np
.
random
.
randint
(
self
.
label_dim
,
size
=
(
batch_size
))
self
.
label
[
np
.
arange
(
batch_size
),
nonzero_index
]
=
1
def
setUp
(
self
):
self
.
config
()
smoothed_label
=
(
1
-
self
.
epsilon
)
*
self
.
label
+
self
.
epsilon
/
self
.
label_dim
self
.
inputs
=
{
'X'
:
convert_float_to_uint16
(
self
.
label
)}
self
.
attrs
=
{
'epsilon'
:
self
.
epsilon
}
self
.
outputs
=
{
'Out'
:
convert_float_to_uint16
(
smoothed_label
)}
def
test_check_output
(
self
):
place
=
core
.
CUDAPlace
(
0
)
self
.
check_output_with_place
(
place
,
check_eager
=
True
)
def
test_check_grad
(
self
):
place
=
core
.
CUDAPlace
(
0
)
self
.
check_grad_with_place
(
place
,
[
"X"
],
"Out"
,
check_eager
=
True
)
class
TestLabelSmoothFP16OP
(
TestLabelSmoothOp
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
float16
...
...
@@ -58,13 +92,31 @@ class TestLabelSmoothFP16OP(TestLabelSmoothOp):
class
TestLabelSmoothOpWithPriorDist
(
TestLabelSmoothOp
):
def
setUp
(
self
):
self
.
config
()
dist
=
np
.
random
.
random
((
1
,
self
.
label_dim
))
dist
=
np
.
random
.
random
((
1
,
self
.
label_dim
))
.
astype
(
self
.
dtype
)
smoothed_label
=
(
1
-
self
.
epsilon
)
*
self
.
label
+
self
.
epsilon
*
dist
self
.
inputs
=
{
'X'
:
self
.
label
,
'PriorDist'
:
dist
}
self
.
attrs
=
{
'epsilon'
:
self
.
epsilon
}
self
.
outputs
=
{
'Out'
:
smoothed_label
}
class
TestLabelSmoothFP16OPWithPriorDist
(
TestLabelSmoothOpWithPriorDist
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
float16
class
TestLabelSmoothBF16OPWithPriorDist
(
TestLabelSmoothOpBF16
):
def
setUp
(
self
):
self
.
config
()
dist
=
np
.
random
.
random
((
1
,
self
.
label_dim
)).
astype
(
np
.
float32
)
smoothed_label
=
(
1
-
self
.
epsilon
)
*
self
.
label
+
self
.
epsilon
*
dist
self
.
inputs
=
{
'X'
:
convert_float_to_uint16
(
self
.
label
),
'PriorDist'
:
convert_float_to_uint16
(
dist
),
}
self
.
attrs
=
{
'epsilon'
:
self
.
epsilon
}
self
.
outputs
=
{
'Out'
:
convert_float_to_uint16
(
smoothed_label
)}
class
TestLabelSmoothOp3D
(
TestLabelSmoothOp
):
def
setUp
(
self
):
super
().
setUp
()
...
...
@@ -76,6 +128,22 @@ class TestLabelSmoothOp3D(TestLabelSmoothOp):
)
class
TestLabelSmoothOp3DBF16
(
TestLabelSmoothOpBF16
):
def
setUp
(
self
):
super
().
setUp
()
self
.
inputs
[
'X'
]
=
self
.
inputs
[
'X'
].
reshape
(
[
2
,
-
1
,
self
.
inputs
[
'X'
].
shape
[
-
1
]]
)
self
.
outputs
[
'Out'
]
=
self
.
outputs
[
'Out'
].
reshape
(
self
.
inputs
[
'X'
].
shape
)
class
TestLabelSmoothFP16OP3D
(
TestLabelSmoothOp3D
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
float16
class
TestLabelSmoothOpWithPriorDist3D
(
TestLabelSmoothOpWithPriorDist
):
def
setUp
(
self
):
super
().
setUp
()
...
...
@@ -87,6 +155,22 @@ class TestLabelSmoothOpWithPriorDist3D(TestLabelSmoothOpWithPriorDist):
)
class
TestLabelSmoothFP16OPWithPriorDist3D
(
TestLabelSmoothOpWithPriorDist3D
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
float16
class
TestLabelSmoothBF16OpWithPriorDist3D
(
TestLabelSmoothBF16OPWithPriorDist
):
def
setUp
(
self
):
super
().
setUp
()
self
.
inputs
[
'X'
]
=
self
.
inputs
[
'X'
].
reshape
(
[
2
,
-
1
,
self
.
inputs
[
'X'
].
shape
[
-
1
]]
)
self
.
outputs
[
'Out'
]
=
self
.
outputs
[
'Out'
].
reshape
(
self
.
inputs
[
'X'
].
shape
)
if
__name__
==
'__main__'
:
paddle
.
enable_static
()
unittest
.
main
()
python/paddle/nn/functional/common.py
浏览文件 @
c4b6d1ae
...
...
@@ -1954,7 +1954,10 @@ def label_smooth(label, prior_dist=None, epsilon=0.1, name=None):
)
check_variable_and_dtype
(
label
,
'label'
,
[
'float16'
,
'float32'
,
'float64'
],
'label_smooth'
label
,
'label'
,
[
'uint16'
,
'float16'
,
'float32'
,
'float64'
],
'label_smooth'
,
)
helper
=
LayerHelper
(
"label_smooth"
,
**
locals
())
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录