Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
18dd3aad
P
PaddleSlim
项目概览
PaddlePaddle
/
PaddleSlim
接近 2 年 前同步成功
通知
51
Star
1434
Fork
344
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
53
列表
看板
标记
里程碑
合并请求
16
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleSlim
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
53
Issue
53
列表
看板
标记
里程碑
合并请求
16
合并请求
16
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
18dd3aad
编写于
3月 28, 2023
作者:
C
ceci3
提交者:
GitHub
3月 28, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix ofa (#1703)
上级
adb69ed6
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
34 addition
and
23 deletion
+34
-23
paddleslim/nas/ofa/get_sub_model.py
paddleslim/nas/ofa/get_sub_model.py
+23
-11
paddleslim/nas/ofa/layers.py
paddleslim/nas/ofa/layers.py
+11
-12
未找到文件。
paddleslim/nas/ofa/get_sub_model.py
浏览文件 @
18dd3aad
...
...
@@ -82,7 +82,7 @@ def _find_weight_ops(op, graph, weights):
"""
pre_ops
=
sorted
(
graph
.
pre_ops
(
op
))
for
pre_op
in
pre_ops
:
### if depthwise conv is one of elementwise's input,
### if depthwise conv is one of elementwise's input,
### add it into this same search space
if
_is_depthwise
(
pre_op
):
for
inp
in
pre_op
.
all_inputs
():
...
...
@@ -122,6 +122,14 @@ def _is_output_weight_ops(op, graph):
return
True
def
if_is_bias
(
op
,
graph
):
pre_ops
=
sorted
(
graph
.
pre_ops
(
op
))
if
'conv'
in
pre_ops
[
0
].
type
()
and
pre_ops
[
1
].
type
()
==
"reshape2"
:
if
pre_ops
[
1
].
inputs
(
'X'
)[
0
].
_var
.
persistable
==
True
:
return
True
return
False
def
check_search_space
(
graph
):
""" Find the shortcut in the model and set same config for this situation.
"""
...
...
@@ -130,7 +138,7 @@ def check_search_space(graph):
depthwise_conv
=
[]
fixed_by_input
=
[]
for
op
in
graph
.
ops
():
# if there is no weight ops after this op,
# if there is no weight ops after this op,
# this op can be seen as an output
if
_is_output_weight_ops
(
op
,
graph
)
and
_is_dynamic_weight_op
(
op
):
for
inp
in
op
.
all_inputs
():
...
...
@@ -139,8 +147,10 @@ def check_search_space(graph):
if
op
.
type
()
==
'elementwise_add'
or
op
.
type
()
==
'elementwise_mul'
:
inp1
,
inp2
=
op
.
all_inputs
()[
0
],
op
.
all_inputs
()[
1
]
if
(
not
inp1
.
_var
.
persistable
)
and
(
not
inp2
.
_var
.
persistable
):
# if one of two vars comes from input,
is_bias
=
if_is_bias
(
op
,
graph
)
if
((
not
inp1
.
_var
.
persistable
)
and
(
not
inp2
.
_var
.
persistable
))
and
not
is_bias
:
# if one of two vars comes from input,
# then the two vars in this elementwise op should be all fixed
if
inp1
.
inputs
()
and
inp2
.
inputs
():
pre_fixed_op_1
,
pre_fixed_op_2
=
[],
[]
...
...
@@ -152,11 +162,11 @@ def check_search_space(graph):
fixed_by_input
+=
pre_fixed_op_2
if
not
pre_fixed_op_2
:
fixed_by_input
+=
pre_fixed_op_1
elif
(
not
inp1
.
inputs
()
and
inp2
.
inputs
())
or
(
inp1
.
inputs
()
and
not
inp2
.
inputs
()):
elif
(
not
inp1
.
inputs
()
and
inp2
.
inputs
())
or
(
inp1
.
inputs
()
and
not
inp2
.
inputs
()):
pre_fixed_op
=
[]
inputs
=
inp1
.
inputs
(
)
if
not
inp2
.
inputs
(
)
else
inp2
.
inputs
()
inputs
=
inp1
.
inputs
(
)
if
not
inp2
.
inputs
()
else
inp2
.
inputs
()
pre_fixed_op
=
_find_weight_ops
(
inputs
[
0
],
graph
,
pre_fixed_op
)
fixed_by_input
+=
pre_fixed_op
...
...
@@ -213,14 +223,16 @@ def broadcast_search_space(same_search_space, param2key, origin_config):
if
key
in
origin_config
:
if
'expand_ratio'
in
origin_config
[
pre_key
]:
origin_config
[
key
].
update
({
'expand_ratio'
:
origin_config
[
pre_key
][
'expand_ratio'
]
'expand_ratio'
:
origin_config
[
pre_key
][
'expand_ratio'
]
})
elif
'channel'
in
origin_config
[
pre_key
]:
origin_config
[
key
].
update
({
'channel'
:
origin_config
[
pre_key
][
'channel'
]
'channel'
:
origin_config
[
pre_key
][
'channel'
]
})
else
:
# if the pre_key is removed from config for some reasons
# if the pre_key is removed from config for some reasons
# such as it is fixed by hand or by elementwise op
if
pre_key
in
origin_config
:
if
'expand_ratio'
in
origin_config
[
pre_key
]:
...
...
paddleslim/nas/ofa/layers.py
浏览文件 @
18dd3aad
...
...
@@ -1047,14 +1047,16 @@ class SuperBatchNorm2D(paddle.nn.BatchNorm2D):
"Variance"
:
[
variance
]
}
helper
=
paddle
.
fluid
.
layer_helper
.
LayerHelper
(
'batch_norm'
)
saved_mean
=
self
.
_helper
.
create_variable_for_type_inference
(
dtype
=
self
.
_dtype
,
stop_gradient
=
True
)
saved_variance
=
self
.
_helper
.
create_variable_for_type_inference
(
dtype
=
self
.
_dtype
,
stop_gradient
=
True
)
reserve_space
=
self
.
_helper
.
create_variable_for_type_inference
(
dtype
=
self
.
_helper
.
input_dtype
(
input
),
stop_gradient
=
True
)
param_dtype
=
input
.
dtype
if
input
.
dtype
!=
'float16'
else
'float32'
saved_mean
=
helper
.
create_variable_for_type_inference
(
dtype
=
param_dtype
,
stop_gradient
=
True
)
saved_variance
=
helper
.
create_variable_for_type_inference
(
dtype
=
param_dtype
,
stop_gradient
=
True
)
batch_norm_out
=
helper
.
create_variable_for_type_inference
(
input
.
dtype
)
batch_norm_out
=
(
input
if
self
.
_in_place
else
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dtype
))
outputs
=
{
"Y"
:
[
batch_norm_out
],
...
...
@@ -1064,13 +1066,10 @@ class SuperBatchNorm2D(paddle.nn.BatchNorm2D):
"SavedVariance"
:
[
saved_variance
]
}
if
self
.
training
or
trainable_statistics
:
# reserve_space is only used for training.
reserve_space
=
helper
.
create_variable_for_type_inference
(
dtype
=
input
.
dtype
,
stop_gradient
=
True
)
if
reserve_space
is
not
None
:
outputs
[
"ReserveSpace"
]
=
[
reserve_space
]
helper
.
append_op
(
self
.
_
helper
.
append_op
(
type
=
"batch_norm"
,
inputs
=
inputs
,
outputs
=
outputs
,
attrs
=
attrs
)
self
.
cur_config
=
{
'prune_dim'
:
feature_dim
}
return
batch_norm_out
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录