Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
35f1a89e
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看板
未验证
提交
35f1a89e
编写于
5月 23, 2023
作者:
N
niuliling123
提交者:
GitHub
5月 23, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add test case for fp16_guard for amp O2 (#53971)
上级
5f8e7d8f
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
84 addition
and
0 deletion
+84
-0
test/amp/test_amp_api.py
test/amp/test_amp_api.py
+84
-0
未找到文件。
test/amp/test_amp_api.py
浏览文件 @
35f1a89e
...
@@ -18,6 +18,7 @@ import numpy as np
...
@@ -18,6 +18,7 @@ import numpy as np
from
amp_base_models
import
AmpTestBase
from
amp_base_models
import
AmpTestBase
import
paddle
import
paddle
import
paddle.nn.functional
as
F
from
paddle
import
nn
from
paddle
import
nn
from
paddle.static
import
amp
from
paddle.static
import
amp
...
@@ -153,5 +154,88 @@ class TestGradScaler(AmpTestBase):
...
@@ -153,5 +154,88 @@ class TestGradScaler(AmpTestBase):
self
.
assertTrue
(
'check_finite_and_unscale'
not
in
op_list
)
self
.
assertTrue
(
'check_finite_and_unscale'
not
in
op_list
)
class
TestFp16Guard
(
AmpTestBase
):
def
test_fp16_gurad
(
self
):
paddle
.
enable_static
()
def
run_example_code
():
place
=
paddle
.
CUDAPlace
(
0
)
main_program
=
paddle
.
static
.
Program
()
startup_program
=
paddle
.
static
.
Program
()
exe
=
paddle
.
static
.
Executor
(
place
)
fetch_vars
=
[]
# 1) Use fp16_guard to control the range of fp16 kernels used.
with
paddle
.
static
.
program_guard
(
main_program
,
startup_program
):
with
paddle
.
static
.
amp
.
fp16_guard
():
data
=
paddle
.
static
.
data
(
name
=
'X'
,
shape
=
[
None
,
1
,
28
,
28
],
dtype
=
'float32'
)
conv2d
=
paddle
.
static
.
nn
.
conv2d
(
input
=
data
,
num_filters
=
6
,
filter_size
=
3
)
bn
=
paddle
.
static
.
nn
.
batch_norm
(
input
=
conv2d
,
act
=
"relu"
)
pool
=
F
.
max_pool2d
(
bn
,
kernel_size
=
2
,
stride
=
2
)
hidden
=
paddle
.
static
.
nn
.
fc
(
pool
,
size
=
10
)
loss
=
paddle
.
mean
(
hidden
)
fetch_vars
=
[
loss
]
# 2) Create the optimizer and set `multi_precision` to True.
# Setting `multi_precision` to True can avoid the poor accuracy
# or the slow convergence in a way.
optimizer
=
paddle
.
optimizer
.
Momentum
(
learning_rate
=
0.01
,
multi_precision
=
True
)
# 3) These ops in `custom_black_list` will keep in the float32 computation type.
amp_list
=
paddle
.
static
.
amp
.
CustomOpLists
(
custom_black_list
=
[
'pool2d'
]
)
# 4) The entry of Paddle AMP.
# Enable pure fp16 training by setting `use_pure_fp16` to True.
optimizer
=
paddle
.
static
.
amp
.
decorate
(
optimizer
,
amp_list
,
init_loss_scaling
=
128.0
,
use_dynamic_loss_scaling
=
True
,
use_pure_fp16
=
True
,
)
# If you don't use the default_startup_program(), you sholud pass
# your defined `startup_program` into `minimize`.
optimizer
.
minimize
(
loss
)
exe
.
run
(
startup_program
)
# 5) Use `amp_init` after FP32 parameters initialization(such as `exe.run(startup_program)`).
# If you want to perform the testing process, you should pass `test_program` into `amp_init`.
optimizer
.
amp_init
(
place
,
scope
=
paddle
.
static
.
global_scope
())
x_fp32
=
np
.
random
.
random
(
size
=
[
1
,
1
,
28
,
28
]).
astype
(
"float32"
)
(
loss_data
,)
=
exe
.
run
(
main_program
,
feed
=
{
"X"
:
x_fp32
},
fetch_list
=
[
loss
.
name
]
)
self
.
assertEqual
(
paddle
.
static
.
global_scope
()
.
find_var
(
"conv2d_0.b_0"
)
.
get_tensor
()
.
_dtype
(),
paddle
.
float16
,
)
self
.
assertEqual
(
paddle
.
static
.
global_scope
()
.
find_var
(
"fc_0.b_0"
)
.
get_tensor
()
.
_dtype
(),
paddle
.
float32
,
)
if
(
paddle
.
is_compiled_with_cuda
()
and
len
(
paddle
.
static
.
cuda_places
())
>
0
):
run_example_code
()
paddle
.
disable_static
()
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录