Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
44a476c2
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
44a476c2
编写于
11月 04, 2020
作者:
L
Leo Chen
提交者:
GitHub
11月 04, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support cuda pinned place (#28416)
上级
12b9587b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
65 addition
and
25 deletion
+65
-25
paddle/fluid/imperative/amp_auto_cast.cc
paddle/fluid/imperative/amp_auto_cast.cc
+8
-8
python/paddle/fluid/tests/unittests/test_imperative_auto_mixed_precision.py
...d/tests/unittests/test_imperative_auto_mixed_precision.py
+57
-17
未找到文件。
paddle/fluid/imperative/amp_auto_cast.cc
浏览文件 @
44a476c2
...
...
@@ -49,15 +49,15 @@ inline std::string GetDtypeStr(
}
inline
bool
NeedCast
(
const
std
::
shared_ptr
<
VarBase
>&
var
)
{
if
(
!
platform
::
is_gpu_place
(
var
->
Place
()))
{
return
false
;
}
if
(
var
->
DataType
()
==
framework
::
proto
::
VarType
::
FP32
||
var
->
DataType
()
==
framework
::
proto
::
VarType
::
FP16
)
{
return
true
;
}
else
{
return
false
;
if
(
platform
::
is_gpu_place
(
var
->
Place
())
||
platform
::
is_cuda_pinned_place
(
var
->
Place
()))
{
// CudaPinndePlace is added for varbase created by dataloader
if
(
var
->
DataType
()
==
framework
::
proto
::
VarType
::
FP32
||
var
->
DataType
()
==
framework
::
proto
::
VarType
::
FP16
)
{
return
true
;
}
}
return
false
;
}
// NOTE: Trace a cast op, so if a var is casted from fp32 to fp16, then the grad
...
...
python/paddle/fluid/tests/unittests/test_imperative_auto_mixed_precision.py
浏览文件 @
44a476c2
...
...
@@ -196,15 +196,27 @@ class TestAmpScaler(unittest.TestCase):
np
.
array_equal
(
param
.
numpy
(),
params_init
[
param
.
name
]))
def
reader_decorator
(
reader
):
def
__reader__
():
for
item
in
reader
():
img
=
np
.
array
(
item
[
0
]).
astype
(
'float32'
).
reshape
(
3
,
224
,
224
)
label
=
np
.
array
(
item
[
1
]).
astype
(
'int64'
).
reshape
(
1
)
yield
img
,
label
return
__reader__
class
TestResnet2
(
unittest
.
TestCase
):
def
train_resnet
(
self
,
enable_amp
=
True
):
"""
Use paddle-2.0 API
"""
def
train_resnet
(
self
,
enable_amp
=
True
,
use_data_loader
=
False
):
seed
=
90
batch_size
=
train_parameters
[
"batch_size"
]
batch_num
=
1
paddle
.
disable_static
()
paddle
.
seed
(
seed
)
paddle
.
framework
.
random
.
_manual_program_seed
(
seed
)
...
...
@@ -223,18 +235,35 @@ class TestResnet2(unittest.TestCase):
scaler
=
paddle
.
amp
.
GradScaler
(
enable
=
enable_amp
,
init_loss_scaling
=
2.
**
10
)
if
use_data_loader
:
train_reader
=
paddle
.
batch
(
reader_decorator
(
paddle
.
dataset
.
flowers
.
train
(
use_xmap
=
False
)),
batch_size
=
batch_size
,
drop_last
=
True
)
train_loader
=
fluid
.
io
.
DataLoader
.
from_generator
(
capacity
=
4
,
use_double_buffer
=
True
,
iterable
=
True
,
return_list
=
True
)
train_loader
.
set_sample_list_generator
(
train_reader
)
train_reader
=
train_loader
for
batch_id
,
data
in
enumerate
(
train_reader
()):
if
batch_id
>=
batch_num
:
break
dy_x_data
=
np
.
array
(
[
x
[
0
].
reshape
(
3
,
224
,
224
)
for
x
in
data
]).
astype
(
'float32'
)
if
len
(
np
.
array
([
x
[
1
]
for
x
in
data
]).
astype
(
'int64'
))
!=
batch_size
:
continue
y_data
=
np
.
array
([
x
[
1
]
for
x
in
data
]).
astype
(
'int64'
).
reshape
(
-
1
,
1
)
img
=
paddle
.
to_tensor
(
dy_x_data
)
label
=
paddle
.
to_tensor
(
y_data
)
if
use_data_loader
:
img
,
label
=
data
else
:
dy_x_data
=
np
.
array
(
[
x
[
0
].
reshape
(
3
,
224
,
224
)
for
x
in
data
]).
astype
(
'float32'
)
if
len
(
np
.
array
([
x
[
1
]
for
x
in
data
]).
astype
(
'int64'
))
!=
batch_size
:
continue
y_data
=
np
.
array
([
x
[
1
]
for
x
in
data
]).
astype
(
'int64'
).
reshape
(
-
1
,
1
)
img
=
paddle
.
to_tensor
(
dy_x_data
)
label
=
paddle
.
to_tensor
(
y_data
)
label
.
stop_gradient
=
True
with
paddle
.
amp
.
auto_cast
(
enable
=
enable_amp
):
...
...
@@ -262,19 +291,30 @@ class TestResnet2(unittest.TestCase):
dy_param_value
=
{}
for
param
in
resnet
.
parameters
():
dy_param_value
[
param
.
name
]
=
param
.
numpy
()
paddle
.
enable_static
()
if
use_data_loader
:
train_reader
.
_reset
()
return
dy_out
,
dy_param_value
,
dy_grad_value
def
test_resnet
(
self
):
out_fp32
=
self
.
train_resnet
(
enable_amp
=
False
)
out_amp
=
self
.
train_resnet
(
enable_amp
=
True
)
with
fluid
.
dygraph
.
guard
():
out_fp32
=
self
.
train_resnet
(
enable_amp
=
False
)
out_amp
=
self
.
train_resnet
(
enable_amp
=
True
)
print
(
out_fp32
[
0
],
out_amp
[
0
])
self
.
assertTrue
(
np
.
allclose
(
out_fp32
[
0
],
out_amp
[
0
],
atol
=
1.e-2
))
def
test_with_data_loader
(
self
):
with
fluid
.
dygraph
.
guard
():
out_fp32
=
self
.
train_resnet
(
enable_amp
=
False
,
use_data_loader
=
True
)
out_amp
=
self
.
train_resnet
(
enable_amp
=
True
,
use_data_loader
=
True
)
print
(
out_fp32
[
0
],
out_amp
[
0
])
self
.
assertTrue
(
np
.
allclose
(
out_fp32
[
0
],
out_amp
[
0
],
atol
=
1.e-2
))
class
TestResnet
(
unittest
.
TestCase
):
"""
Use paddle-1.x API
"""
def
train_resnet
(
self
,
enable_amp
=
True
):
seed
=
90
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录