Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
b3d47ac6
P
PaddleSlim
项目概览
PaddlePaddle
/
PaddleSlim
大约 1 年 前同步成功
通知
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看板
未验证
提交
b3d47ac6
编写于
12月 19, 2020
作者:
C
ceci3
提交者:
GitHub
12月 19, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Cherry pick] Fix params ofa (#564)
* fix when param name is not None
上级
aae6d797
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
68 addition
and
16 deletion
+68
-16
paddleslim/nas/ofa/convert_super.py
paddleslim/nas/ofa/convert_super.py
+39
-11
tests/test_ofa.py
tests/test_ofa.py
+29
-5
未找到文件。
paddleslim/nas/ofa/convert_super.py
浏览文件 @
b3d47ac6
...
...
@@ -23,12 +23,14 @@ pd_ver = get_paddle_version()
if
pd_ver
==
185
:
import
paddle.fluid.dygraph.nn
as
nn
from
paddle.fluid.dygraph.nn
import
Conv2D
,
Conv2DTranspose
,
Linear
,
LayerNorm
,
Embedding
from
paddle.fluid
import
ParamAttr
from
.layers
import
*
from
.
import
layers
Layer
=
paddle
.
fluid
.
dygraph
.
Layer
else
:
import
paddle.nn
as
nn
from
paddle.nn
import
Conv2D
,
Conv2DTranspose
,
Linear
,
LayerNorm
,
Embedding
from
paddle
import
ParamAttr
from
.layers_new
import
*
from
.
import
layers_new
as
layers
Layer
=
paddle
.
nn
.
Layer
...
...
@@ -44,6 +46,22 @@ class Convert:
def
__init__
(
self
,
context
):
self
.
context
=
context
def
_change_name
(
self
,
layer
,
pd_ver
,
has_bias
=
True
,
conv
=
False
):
if
conv
:
w_attr
=
layer
.
_param_attr
else
:
w_attr
=
layer
.
_param_attr
if
pd_ver
==
185
else
layer
.
_weight_attr
if
isinstance
(
w_attr
,
ParamAttr
):
if
w_attr
!=
None
and
not
isinstance
(
w_attr
,
bool
):
w_attr
.
name
=
'super_'
+
w_attr
.
name
if
has_bias
:
if
isinstance
(
layer
.
_bias_attr
,
ParamAttr
):
if
layer
.
_bias_attr
!=
None
and
not
isinstance
(
layer
.
_bias_attr
,
bool
):
layer
.
_bias_attr
.
name
=
'super_'
+
layer
.
_bias_attr
.
name
def
convert
(
self
,
network
):
# search the first and last weight layer, don't change out channel of the last weight layer
# don't change in channel of the first weight layer
...
...
@@ -88,6 +106,7 @@ class Convert:
'weight_attr'
,
'data_format'
,
'padding_mode'
]
self
.
_change_name
(
layer
,
pd_ver
,
conv
=
True
)
new_attr_dict
=
dict
.
fromkeys
(
new_attr_name
,
None
)
new_attr_dict
[
'candidate_config'
]
=
dict
()
if
pd_ver
==
185
:
...
...
@@ -104,7 +123,7 @@ class Convert:
fks
=
'_filter_size'
if
'_filter_size'
in
attr_dict
.
keys
(
)
else
'_kernel_size'
ks
=
list
(
attr_dict
[
fks
])
if
isinstance
(
ks
=
[
attr_dict
[
fks
]]
if
isinstance
(
attr_dict
[
fks
],
numbers
.
Integral
)
else
attr_dict
[
fks
]
if
self
.
kernel_size
and
int
(
ks
[
0
])
!=
1
:
...
...
@@ -214,6 +233,7 @@ class Convert:
else
:
new_attr_name
+=
[
'weight_attr'
,
'data_format'
,
'name'
]
self
.
_change_name
(
layer
,
pd_ver
)
new_attr_dict
=
dict
.
fromkeys
(
new_attr_name
,
None
)
if
pd_ver
==
185
:
new_attr_dict
[
'num_channels'
]
=
None
...
...
@@ -237,8 +257,9 @@ class Convert:
del
layer
,
attr_dict
layer
=
getattr
(
layers
,
'SuperBatchNorm'
,
SuperBatchNorm2D
)(
**
new_attr_dict
)
layer
=
layers
.
SuperBatchNorm
(
**
new_attr_dict
)
if
pd_ver
==
185
else
layers
.
SuperBatchNorm2D
(
**
new_attr_dict
)
model
[
idx
]
=
layer
### assume output_size = None, filter_size != None
...
...
@@ -273,12 +294,14 @@ class Convert:
new_attr_dict
[
'in_channels'
]
=
None
new_attr_dict
[
'out_channels'
]
=
None
new_attr_dict
[
'kernel_size'
]
=
None
self
.
_change_name
(
layer
,
pd_ver
,
conv
=
True
)
self
.
kernel_size
=
getattr
(
self
.
context
,
'kernel_size'
,
None
)
# if the kernel_size of conv transpose is 1, don't change it.
fks
=
'_filter_size'
if
'_filter_size'
in
attr_dict
.
keys
(
)
else
'_kernel_size'
ks
=
list
(
attr_dict
[
fks
])
if
isinstance
(
ks
=
[
attr_dict
[
fks
]]
if
isinstance
(
attr_dict
[
fks
],
numbers
.
Integral
)
else
attr_dict
[
fks
]
if
self
.
kernel_size
and
int
(
ks
[
0
])
!=
1
:
...
...
@@ -381,7 +404,7 @@ class Convert:
attr_dict
=
layer
.
__dict__
key
=
attr_dict
[
'_full_name'
]
if
pd_ver
==
185
:
new_attr_name
=
[
'
param_attr'
,
'bias_attr'
,
'
act'
,
'dtype'
]
new_attr_name
=
[
'act'
,
'dtype'
]
else
:
new_attr_name
=
[
'weight_attr'
,
'bias_attr'
]
in_nc
,
out_nc
=
layer
.
_parameters
[
'weight'
].
shape
...
...
@@ -395,10 +418,8 @@ class Convert:
new_attr_dict
[
'in_features'
]
=
None
new_attr_dict
[
'out_features'
]
=
None
in_key
=
'_input_dim'
if
'_input_dim'
in
attr_dict
.
keys
(
)
else
'_in_features'
out_key
=
'_output_dim'
if
'_output_dim'
in
attr_dict
.
keys
(
)
else
'_out_features'
in_key
=
'_input_dim'
if
pd_ver
==
185
else
'_in_features'
out_key
=
'_output_dim'
if
pd_ver
==
185
else
'_out_features'
attr_dict
[
in_key
]
=
in_nc
attr_dict
[
out_key
]
=
out_nc
if
self
.
context
.
expand
:
...
...
@@ -461,6 +482,8 @@ class Convert:
]
else
:
new_attr_name
=
[
'bias_attr'
,
'epsilon'
,
'weight_attr'
]
self
.
_change_name
(
layer
,
pd_ver
)
new_attr_dict
=
dict
.
fromkeys
(
new_attr_name
,
None
)
if
pd_ver
==
185
:
new_attr_dict
[
'num_channels'
]
=
None
...
...
@@ -485,8 +508,10 @@ class Convert:
del
layer
,
attr_dict
layer
=
getattr
(
layers
,
'SuperInstanceNorm2D'
,
'SuperInstanceNorm'
)(
**
new_attr_dict
)
layer
=
layers
.
SuperInstanceNorm
(
**
new_attr_dict
)
if
pd_ver
==
185
else
layers
.
SuperInstanceNorm2D
(
**
new_attr_dict
)
model
[
idx
]
=
layer
elif
isinstance
(
layer
,
LayerNorm
)
and
(
...
...
@@ -505,6 +530,7 @@ class Convert:
else
:
new_attr_name
+=
[
'weight_attr'
]
self
.
_change_name
(
layer
,
pd_ver
)
new_attr_dict
=
dict
.
fromkeys
(
new_attr_name
,
None
)
new_attr_dict
[
'normalized_shape'
]
=
None
if
self
.
context
.
expand
:
...
...
@@ -540,6 +566,8 @@ class Convert:
'weight_attr'
,
'name'
]
self
.
_change_name
(
layer
,
pd_ver
,
has_bias
=
False
)
new_attr_dict
=
dict
.
fromkeys
(
new_attr_name
,
None
)
new_attr_dict
[
'candidate_config'
]
=
dict
()
bef_size
=
attr_dict
[
'_size'
]
...
...
tests/test_ofa.py
浏览文件 @
b3d47ac6
...
...
@@ -92,8 +92,16 @@ class ModelConv2(nn.Layer):
super
(
ModelConv2
,
self
).
__init__
()
with
supernet
(
expand_ratio
=
(
1
,
2
,
4
))
as
ofa_super
:
models
=
[]
models
+=
[
nn
.
Conv2DTranspose
(
4
,
4
,
3
)]
models
+=
[
nn
.
BatchNorm2D
(
4
)]
models
+=
[
nn
.
Conv2DTranspose
(
4
,
4
,
3
,
weight_attr
=
paddle
.
ParamAttr
(
name
=
'conv1_w'
))
]
models
+=
[
nn
.
BatchNorm2D
(
4
,
weight_attr
=
paddle
.
ParamAttr
(
name
=
'bn1_w'
),
bias_attr
=
paddle
.
ParamAttr
(
name
=
'bn1_b'
))
]
models
+=
[
ReLU
()]
models
+=
[
nn
.
Conv2D
(
4
,
4
,
3
)]
models
+=
[
nn
.
BatchNorm2D
(
4
)]
...
...
@@ -197,9 +205,25 @@ class ModelLinear2(nn.Layer):
super
(
ModelLinear2
,
self
).
__init__
()
with
supernet
(
expand_ratio
=
None
)
as
ofa_super
:
models
=
[]
models
+=
[
nn
.
Embedding
(
num_embeddings
=
64
,
embedding_dim
=
64
)]
models
+=
[
nn
.
Linear
(
64
,
128
)]
models
+=
[
nn
.
LayerNorm
(
128
)]
models
+=
[
nn
.
Embedding
(
num_embeddings
=
64
,
embedding_dim
=
64
,
weight_attr
=
paddle
.
ParamAttr
(
name
=
'emb'
))
]
models
+=
[
nn
.
Linear
(
64
,
128
,
weight_attr
=
paddle
.
ParamAttr
(
name
=
'fc1_w'
),
bias_attr
=
paddle
.
ParamAttr
(
name
=
'fc1_b'
))
]
models
+=
[
nn
.
LayerNorm
(
128
,
weight_attr
=
paddle
.
ParamAttr
(
name
=
'ln1_w'
),
bias_attr
=
paddle
.
ParamAttr
(
name
=
'ln1_b'
))
]
models
+=
[
nn
.
Linear
(
128
,
256
)]
models
=
ofa_super
.
convert
(
models
)
self
.
models
=
paddle
.
nn
.
Sequential
(
*
models
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录