Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
84a56b4a
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2293
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
84a56b4a
编写于
7月 26, 2023
作者:
D
Difer
提交者:
GitHub
7月 26, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add FP16 & BF16 for lamb (#55641)
上级
a4644c50
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
183 addition
and
22 deletion
+183
-22
paddle/phi/kernels/gpu/lamb_kernel.cu
paddle/phi/kernels/gpu/lamb_kernel.cu
+1
-0
test/legacy_test/test_lamb_op.py
test/legacy_test/test_lamb_op.py
+182
-22
未找到文件。
paddle/phi/kernels/gpu/lamb_kernel.cu
浏览文件 @
84a56b4a
...
...
@@ -23,6 +23,7 @@ PD_REGISTER_KERNEL(lamb,
ALL_LAYOUT
,
phi
::
LambKernel
,
phi
::
dtype
::
float16
,
phi
::
dtype
::
bfloat16
,
float
,
double
)
{
kernel
->
InputAt
(
5
).
SetBackend
(
phi
::
Backend
::
ALL_BACKEND
);
...
...
test/legacy_test/test_lamb_op.py
浏览文件 @
84a56b4a
...
...
@@ -15,7 +15,7 @@
import
unittest
import
numpy
as
np
from
eager_op_test
import
OpTest
from
eager_op_test
import
OpTest
,
convert_float_to_uint16
from
op
import
Operator
import
paddle
...
...
@@ -69,13 +69,24 @@ class TestLambOp1(OpTest):
'always_adapt'
:
False
,
}
def
set_dtype
(
self
):
self
.
dtype
=
np
.
float32
def
setUp
(
self
):
'''Test Lamb Op with supplied attributes'''
self
.
op_type
=
"lamb"
param
=
np
.
random
.
uniform
(
-
1
,
1
,
(
102
,
105
)).
astype
(
"float32"
)
grad
=
np
.
random
.
uniform
(
-
1
,
1
,
(
102
,
105
)).
astype
(
"float32"
)
moment1
=
np
.
random
.
uniform
(
-
1
,
1
,
(
102
,
105
)).
astype
(
"float32"
)
moment2
=
np
.
random
.
random
((
102
,
105
)).
astype
(
"float32"
)
self
.
set_dtype
()
if
self
.
is_bfloat16_op
():
param
=
np
.
random
.
uniform
(
-
1
,
1
,
(
102
,
105
)).
astype
(
np
.
float32
)
grad
=
np
.
random
.
uniform
(
-
1
,
1
,
(
102
,
105
)).
astype
(
np
.
float32
)
moment1
=
np
.
random
.
uniform
(
-
1
,
1
,
(
102
,
105
)).
astype
(
np
.
float32
)
moment2
=
np
.
random
.
random
((
102
,
105
)).
astype
(
np
.
float32
)
else
:
param
=
np
.
random
.
uniform
(
-
1
,
1
,
(
102
,
105
)).
astype
(
self
.
dtype
)
grad
=
np
.
random
.
uniform
(
-
1
,
1
,
(
102
,
105
)).
astype
(
self
.
dtype
)
moment1
=
np
.
random
.
uniform
(
-
1
,
1
,
(
102
,
105
)).
astype
(
self
.
dtype
)
moment2
=
np
.
random
.
random
((
102
,
105
)).
astype
(
self
.
dtype
)
learning_rate
=
0.001
self
.
set_attrs
()
...
...
@@ -86,15 +97,33 @@ class TestLambOp1(OpTest):
beta1_pow
=
self
.
attrs
[
'beta1'
]
beta2_pow
=
self
.
attrs
[
'beta2'
]
self
.
inputs
=
{
'Param'
:
param
,
'Grad'
:
grad
,
'Moment1'
:
moment1
,
'Moment2'
:
moment2
,
'LearningRate'
:
np
.
array
([
learning_rate
]).
astype
(
"float32"
),
'Beta1Pow'
:
np
.
array
([
beta1_pow
]).
astype
(
"float32"
),
'Beta2Pow'
:
np
.
array
([
beta2_pow
]).
astype
(
"float32"
),
}
if
self
.
is_bfloat16_op
():
self
.
inputs
=
{
'Param'
:
convert_float_to_uint16
(
param
),
'Grad'
:
convert_float_to_uint16
(
grad
),
'Moment1'
:
convert_float_to_uint16
(
moment1
),
'Moment2'
:
convert_float_to_uint16
(
moment2
),
'LearningRate'
:
convert_float_to_uint16
(
np
.
array
([
learning_rate
]).
astype
(
self
.
dtype
)
),
'Beta1Pow'
:
convert_float_to_uint16
(
np
.
array
([
beta1_pow
]).
astype
(
self
.
dtype
)
),
'Beta2Pow'
:
convert_float_to_uint16
(
np
.
array
([
beta2_pow
]).
astype
(
self
.
dtype
)
),
}
else
:
self
.
inputs
=
{
'Param'
:
param
,
'Grad'
:
grad
,
'Moment1'
:
moment1
,
'Moment2'
:
moment2
,
'LearningRate'
:
np
.
array
([
learning_rate
]).
astype
(
self
.
dtype
),
'Beta1Pow'
:
np
.
array
([
beta1_pow
]).
astype
(
self
.
dtype
),
'Beta2Pow'
:
np
.
array
([
beta2_pow
]).
astype
(
self
.
dtype
),
}
(
param_out
,
...
...
@@ -104,13 +133,22 @@ class TestLambOp1(OpTest):
beta2_pow_out
,
)
=
lamb_step
(
self
.
inputs
,
self
.
attrs
)
self
.
outputs
=
{
'Moment1Out'
:
moment1_out
,
'Moment2Out'
:
moment2_out
,
'ParamOut'
:
param_out
,
'Beta1PowOut'
:
beta1_pow_out
,
'Beta2PowOut'
:
beta2_pow_out
,
}
if
self
.
is_bfloat16_op
():
self
.
outputs
=
{
'Moment1Out'
:
convert_float_to_uint16
(
moment1_out
),
'Moment2Out'
:
convert_float_to_uint16
(
moment2_out
),
'ParamOut'
:
convert_float_to_uint16
(
param_out
),
'Beta1PowOut'
:
convert_float_to_uint16
(
beta1_pow_out
),
'Beta2PowOut'
:
convert_float_to_uint16
(
beta2_pow_out
),
}
else
:
self
.
outputs
=
{
'Moment1Out'
:
moment1_out
,
'Moment2Out'
:
moment2_out
,
'ParamOut'
:
param_out
,
'Beta1PowOut'
:
beta1_pow_out
,
'Beta2PowOut'
:
beta2_pow_out
,
}
def
test_check_output
(
self
):
self
.
check_output
()
...
...
@@ -181,7 +219,129 @@ class TestLambOpMultipleSteps(TestLambOp1):
# Randomize gradient for next step
self
.
inputs
[
'Grad'
]
=
np
.
random
.
uniform
(
-
1
,
1
,
(
102
,
105
)).
astype
(
"float32"
self
.
dtype
)
class
TestLambFP16Op1
(
TestLambOp1
):
def
set_dtype
(
self
):
self
.
__class__
.
op_type
=
"lamb"
self
.
dtype
=
np
.
float16
def
test_check_output
(
self
):
if
core
.
is_compiled_with_cuda
():
place
=
core
.
CUDAPlace
(
0
)
if
core
.
is_float16_supported
(
place
):
self
.
check_output_with_place
(
place
)
class
TestLambFP16Op2
(
TestLambOp2
):
def
set_dtype
(
self
):
self
.
__class__
.
op_type
=
"lamb"
self
.
dtype
=
np
.
float16
def
test_check_output
(
self
):
if
core
.
is_compiled_with_cuda
():
place
=
core
.
CUDAPlace
(
0
)
if
core
.
is_float16_supported
(
place
):
self
.
check_output_with_place
(
place
)
class
TestLambFP16Op3
(
TestLambOp3
):
def
set_dtype
(
self
):
self
.
__class__
.
op_type
=
"lamb"
self
.
dtype
=
np
.
float16
def
test_check_output
(
self
):
if
core
.
is_compiled_with_cuda
():
place
=
core
.
CUDAPlace
(
0
)
if
core
.
is_float16_supported
(
place
):
self
.
check_output_with_place
(
place
)
class
TestLambFP16OpMultipleSteps
(
TestLambOpMultipleSteps
):
def
set_dtype
(
self
):
self
.
__class__
.
op_type
=
"lamb"
self
.
dtype
=
np
.
float16
class
TestLambBF16Op1
(
TestLambOp1
):
def
set_dtype
(
self
):
self
.
__class__
.
op_type
=
"lamb"
self
.
dtype
=
np
.
uint16
def
test_check_output
(
self
):
if
core
.
is_compiled_with_cuda
():
place
=
core
.
CUDAPlace
(
0
)
if
core
.
is_bfloat16_supported
(
place
):
self
.
check_output_with_place
(
place
)
class
TestLambBF16Op2
(
TestLambOp2
):
def
set_dtype
(
self
):
self
.
__class__
.
op_type
=
"lamb"
self
.
dtype
=
np
.
uint16
def
test_check_output
(
self
):
if
core
.
is_compiled_with_cuda
():
place
=
core
.
CUDAPlace
(
0
)
if
core
.
is_bfloat16_supported
(
place
):
self
.
check_output_with_place
(
place
)
class
TestLambBF16Op3
(
TestLambOp3
):
def
set_dtype
(
self
):
self
.
__class__
.
op_type
=
"lamb"
self
.
dtype
=
np
.
uint16
def
test_check_output
(
self
):
if
core
.
is_compiled_with_cuda
():
place
=
core
.
CUDAPlace
(
0
)
if
core
.
is_bfloat16_supported
(
place
):
self
.
check_output_with_place
(
place
)
class
TestLambBF16OpMultipleSteps
(
TestLambOpMultipleSteps
):
def
set_dtype
(
self
):
self
.
__class__
.
op_type
=
"lamb"
self
.
dtype
=
np
.
uint16
def
test_check_output
(
self
):
for
i
in
range
(
self
.
num_steps
):
(
param_out
,
moment1_out
,
moment2_out
,
beta1_pow_out
,
beta2_pow_out
,
)
=
lamb_step
(
self
.
inputs
,
self
.
attrs
)
self
.
outputs
=
{
'Moment1Out'
:
convert_float_to_uint16
(
moment1_out
),
'Moment2Out'
:
convert_float_to_uint16
(
moment2_out
),
'ParamOut'
:
convert_float_to_uint16
(
param_out
),
'Beta1PowOut'
:
convert_float_to_uint16
(
beta1_pow_out
),
'Beta2PowOut'
:
convert_float_to_uint16
(
beta2_pow_out
),
}
# Verify output for this step
if
core
.
is_compiled_with_cuda
():
place
=
core
.
CUDAPlace
(
0
)
if
core
.
is_bfloat16_supported
(
place
):
self
.
check_output_with_place
(
place
)
# Output of this step becomes input for next step
self
.
inputs
[
'Param'
]
=
convert_float_to_uint16
(
param_out
)
self
.
inputs
[
'Moment1'
]
=
convert_float_to_uint16
(
moment1_out
)
self
.
inputs
[
'Moment2'
]
=
convert_float_to_uint16
(
moment2_out
)
# Update powers of Beta1 and Beta2 for next time step
self
.
inputs
[
'Beta1Pow'
]
=
convert_float_to_uint16
(
beta1_pow_out
)
self
.
inputs
[
'Beta2Pow'
]
=
convert_float_to_uint16
(
beta2_pow_out
)
# Randomize gradient for next step
self
.
inputs
[
'Grad'
]
=
convert_float_to_uint16
(
np
.
random
.
uniform
(
-
1
,
1
,
(
102
,
105
)).
astype
(
np
.
float32
)
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录