Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
8c5f1581
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
8c5f1581
编写于
1月 25, 2021
作者:
Y
yukavio
提交者:
GitHub
1月 25, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove PrettyTable dependence from paddle.flops (#30675)
上级
fb7fbc7a
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
51 addition
and
18 deletion
+51
-18
python/paddle/hapi/dynamic_flops.py
python/paddle/hapi/dynamic_flops.py
+4
-10
python/paddle/hapi/static_flops.py
python/paddle/hapi/static_flops.py
+47
-8
未找到文件。
python/paddle/hapi/dynamic_flops.py
浏览文件 @
8c5f1581
...
@@ -16,7 +16,7 @@ import paddle
...
@@ -16,7 +16,7 @@ import paddle
import
warnings
import
warnings
import
paddle.nn
as
nn
import
paddle.nn
as
nn
import
numpy
as
np
import
numpy
as
np
from
.static_flops
import
static_flops
from
.static_flops
import
static_flops
,
Table
__all__
=
[
'flops'
]
__all__
=
[
'flops'
]
...
@@ -265,13 +265,7 @@ def dynamic_flops(model, inputs, custom_ops=None, print_detail=False):
...
@@ -265,13 +265,7 @@ def dynamic_flops(model, inputs, custom_ops=None, print_detail=False):
for
handler
in
handler_collection
:
for
handler
in
handler_collection
:
handler
.
remove
()
handler
.
remove
()
try
:
table
=
Table
(
from
prettytable
import
PrettyTable
except
ImportError
:
raise
ImportError
(
"paddle.flops() requires package `prettytable`, place install it firstly using `pip install prettytable`. "
)
table
=
PrettyTable
(
[
"Layer Name"
,
"Input Shape"
,
"Output Shape"
,
"Params"
,
"Flops"
])
[
"Layer Name"
,
"Input Shape"
,
"Output Shape"
,
"Params"
,
"Flops"
])
for
n
,
m
in
model
.
named_sublayers
():
for
n
,
m
in
model
.
named_sublayers
():
...
@@ -288,8 +282,8 @@ def dynamic_flops(model, inputs, custom_ops=None, print_detail=False):
...
@@ -288,8 +282,8 @@ def dynamic_flops(model, inputs, custom_ops=None, print_detail=False):
m
.
_buffers
.
pop
(
"total_params"
)
m
.
_buffers
.
pop
(
"total_params"
)
m
.
_buffers
.
pop
(
'input_shape'
)
m
.
_buffers
.
pop
(
'input_shape'
)
m
.
_buffers
.
pop
(
'output_shape'
)
m
.
_buffers
.
pop
(
'output_shape'
)
if
(
print_detail
)
:
if
print_detail
:
print
(
table
)
table
.
print_table
(
)
print
(
'Total Flops: {} Total Params: {}'
.
format
(
print
(
'Total Flops: {} Total Params: {}'
.
format
(
int
(
total_ops
),
int
(
total_params
)))
int
(
total_ops
),
int
(
total_params
)))
return
int
(
total_ops
)
return
int
(
total_ops
)
python/paddle/hapi/static_flops.py
浏览文件 @
8c5f1581
...
@@ -169,13 +169,7 @@ def count_element_op(op):
...
@@ -169,13 +169,7 @@ def count_element_op(op):
def
_graph_flops
(
graph
,
detail
=
False
):
def
_graph_flops
(
graph
,
detail
=
False
):
assert
isinstance
(
graph
,
GraphWrapper
)
assert
isinstance
(
graph
,
GraphWrapper
)
flops
=
0
flops
=
0
try
:
table
=
Table
([
"OP Type"
,
'Param name'
,
"Flops"
])
from
prettytable
import
PrettyTable
except
ImportError
:
raise
ImportError
(
"paddle.flops() requires package `prettytable`, place install it firstly using `pip install prettytable`. "
)
table
=
PrettyTable
([
"OP Type"
,
'Param name'
,
"Flops"
])
for
op
in
graph
.
ops
():
for
op
in
graph
.
ops
():
param_name
=
''
param_name
=
''
if
op
.
type
()
in
[
'conv2d'
,
'depthwise_conv2d'
]:
if
op
.
type
()
in
[
'conv2d'
,
'depthwise_conv2d'
]:
...
@@ -200,10 +194,55 @@ def _graph_flops(graph, detail=False):
...
@@ -200,10 +194,55 @@ def _graph_flops(graph, detail=False):
table
.
add_row
([
op
.
type
(),
param_name
,
op_flops
])
table
.
add_row
([
op
.
type
(),
param_name
,
op_flops
])
op_flops
=
0
op_flops
=
0
if
detail
:
if
detail
:
print
(
table
)
table
.
print_table
(
)
return
flops
return
flops
def
static_flops
(
program
,
print_detail
=
False
):
def
static_flops
(
program
,
print_detail
=
False
):
graph
=
GraphWrapper
(
program
)
graph
=
GraphWrapper
(
program
)
return
_graph_flops
(
graph
,
detail
=
print_detail
)
return
_graph_flops
(
graph
,
detail
=
print_detail
)
class
Table
(
object
):
def
__init__
(
self
,
table_heads
):
self
.
table_heads
=
table_heads
self
.
table_len
=
[]
self
.
data
=
[]
self
.
col_num
=
len
(
table_heads
)
for
head
in
table_heads
:
self
.
table_len
.
append
(
len
(
head
))
def
add_row
(
self
,
row_str
):
if
not
isinstance
(
row_str
,
list
):
print
(
'The row_str should be a list'
)
if
len
(
row_str
)
!=
self
.
col_num
:
print
(
'The length of row data should be equal the length of table heads, but the data: {} is not equal table heads {}'
.
format
(
len
(
row_str
),
self
.
col_num
))
for
i
in
range
(
self
.
col_num
):
if
len
(
str
(
row_str
[
i
]))
>
self
.
table_len
[
i
]:
self
.
table_len
[
i
]
=
len
(
str
(
row_str
[
i
]))
self
.
data
.
append
(
row_str
)
def
print_row
(
self
,
row
):
string
=
''
for
i
in
range
(
self
.
col_num
):
string
+=
'|'
+
str
(
row
[
i
]).
center
(
self
.
table_len
[
i
]
+
2
)
string
+=
'|'
print
(
string
)
def
print_shelf
(
self
):
string
=
''
for
length
in
self
.
table_len
:
string
+=
'+'
string
+=
'-'
*
(
length
+
2
)
string
+=
'+'
print
(
string
)
def
print_table
(
self
):
self
.
print_shelf
()
self
.
print_row
(
self
.
table_heads
)
self
.
print_shelf
()
for
data
in
self
.
data
:
self
.
print_row
(
data
)
self
.
print_shelf
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录