Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
70714d1b
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看板
未验证
提交
70714d1b
编写于
2月 15, 2022
作者:
A
arlesniak
提交者:
GitHub
2月 15, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added hapi BF16 lenet script (#39298)
* hapi lenet BF16 * ops list updated * year typo fix * tests updated fo CI
上级
9d0baeab
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
126 addition
and
0 deletion
+126
-0
python/paddle/tests/hapi_mnist_bf16_static.py
python/paddle/tests/hapi_mnist_bf16_static.py
+126
-0
未找到文件。
python/paddle/tests/hapi_mnist_bf16_static.py
0 → 100644
浏览文件 @
70714d1b
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
__future__
import
division
from
__future__
import
print_function
import
numpy
as
np
import
paddle
from
paddle
import
Model
,
set_device
from
paddle.static
import
InputSpec
as
Input
from
paddle.metric
import
Accuracy
from
paddle.vision.datasets
import
MNIST
from
paddle.vision.models
import
LeNet
import
paddle.static.amp
as
amp
import
random
from
paddle
import
callbacks
import
argparse
import
ast
SEED
=
2
paddle
.
seed
(
SEED
)
paddle
.
framework
.
random
.
_manual_program_seed
(
SEED
)
np
.
random
.
seed
(
SEED
)
random
.
seed
(
SEED
)
paddle
.
enable_static
()
set_device
(
'cpu'
)
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
"Lenet BF16 train static script"
)
parser
.
add_argument
(
'-bf16'
,
'--bf16'
,
type
=
ast
.
literal_eval
,
default
=
False
,
help
=
"whether use bf16"
)
args
=
parser
.
parse_args
()
return
args
class
MnistDataset
(
MNIST
):
def
__init__
(
self
,
mode
,
return_label
=
True
):
super
(
MnistDataset
,
self
).
__init__
(
mode
=
mode
)
self
.
return_label
=
return_label
def
__getitem__
(
self
,
idx
):
img
=
np
.
reshape
(
self
.
images
[
idx
],
[
1
,
28
,
28
])
if
self
.
return_label
:
return
img
,
np
.
array
(
self
.
labels
[
idx
]).
astype
(
'int64'
)
return
img
,
def
__len__
(
self
):
return
len
(
self
.
images
)
def
compute_accuracy
(
pred
,
gt
):
pred
=
np
.
argmax
(
pred
,
-
1
)
gt
=
np
.
array
(
gt
)
correct
=
pred
[:,
np
.
newaxis
]
==
gt
return
np
.
sum
(
correct
)
/
correct
.
shape
[
0
]
def
main
(
args
):
print
(
'download training data and load training data'
)
train_dataset
=
MnistDataset
(
mode
=
'train'
,
)
val_dataset
=
MnistDataset
(
mode
=
'test'
,
)
test_dataset
=
MnistDataset
(
mode
=
'test'
,
return_label
=
False
)
im_shape
=
(
-
1
,
1
,
28
,
28
)
batch_size
=
64
inputs
=
[
Input
(
im_shape
,
'float32'
,
'image'
)]
labels
=
[
Input
([
None
,
1
],
'int64'
,
'label'
)]
model
=
Model
(
LeNet
(),
inputs
,
labels
)
optim
=
paddle
.
optimizer
.
SGD
(
learning_rate
=
0.001
)
if
args
.
bf16
:
optim
=
amp
.
bf16
.
decorate_bf16
(
optim
,
amp_lists
=
amp
.
bf16
.
AutoMixedPrecisionListsBF16
(
custom_bf16_list
=
{
'matmul_v2'
,
'pool2d'
,
'relu'
,
'scale'
,
'elementwise_add'
,
'reshape2'
,
'slice'
,
'reduce_mean'
,
'conv2d'
},
))
# Configuration model
model
.
prepare
(
optim
,
paddle
.
nn
.
CrossEntropyLoss
(),
Accuracy
())
# Training model #
if
args
.
bf16
:
print
(
'Training BF16'
)
else
:
print
(
'Training FP32'
)
model
.
fit
(
train_dataset
,
epochs
=
2
,
batch_size
=
batch_size
,
verbose
=
1
)
eval_result
=
model
.
evaluate
(
val_dataset
,
batch_size
=
batch_size
,
verbose
=
1
)
output
=
model
.
predict
(
test_dataset
,
batch_size
=
batch_size
,
stack_outputs
=
True
)
np
.
testing
.
assert_equal
(
output
[
0
].
shape
[
0
],
len
(
test_dataset
))
acc
=
compute_accuracy
(
output
[
0
],
val_dataset
.
labels
)
print
(
"acc"
,
acc
)
print
(
"eval_result['acc']"
,
eval_result
[
'acc'
])
np
.
testing
.
assert_allclose
(
acc
,
eval_result
[
'acc'
])
if
__name__
==
"__main__"
:
args
=
parse_args
()
main
(
args
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录