Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
ae917097
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看板
未验证
提交
ae917097
编写于
7月 12, 2022
作者:
W
Weilong Wu
提交者:
GitHub
7月 12, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix ci tests under eager mode (#1264)
上级
1e9fd86c
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
414 addition
and
186 deletion
+414
-186
paddleslim/core/dygraph.py
paddleslim/core/dygraph.py
+10
-8
paddleslim/nas/ofa/layers.py
paddleslim/nas/ofa/layers.py
+224
-66
paddleslim/nas/ofa/layers_old.py
paddleslim/nas/ofa/layers_old.py
+177
-81
tests/dygraph/test_flops.py
tests/dygraph/test_flops.py
+0
-1
tests/test_dy2prog.py
tests/test_dy2prog.py
+1
-1
tests/test_ofa.py
tests/test_ofa.py
+2
-0
tests/test_ofa_v2.py
tests/test_ofa_v2.py
+0
-29
未找到文件。
paddleslim/core/dygraph.py
浏览文件 @
ae917097
...
@@ -3,7 +3,8 @@ import paddle
...
@@ -3,7 +3,8 @@ import paddle
import
collections
import
collections
import
logging
import
logging
import
numpy
as
np
import
numpy
as
np
from
paddle.fluid.framework
import
_dygraph_tracer
,
dygraph_only
,
_dygraph_guard
,
program_guard
from
paddle.fluid
import
core
from
paddle.fluid.framework
import
_dygraph_tracer
,
dygraph_only
,
_dygraph_guard
,
program_guard
,
in_dygraph_mode
from
paddle.fluid.dygraph.base
import
program_desc_tracing_guard
,
_switch_declarative_mode_guard_
from
paddle.fluid.dygraph.base
import
program_desc_tracing_guard
,
_switch_declarative_mode_guard_
from
paddle.fluid.dygraph.layers
import
Layer
from
paddle.fluid.dygraph.layers
import
Layer
from
paddle.fluid.framework
import
Block
,
ParamBase
,
Program
,
Variable
from
paddle.fluid.framework
import
Block
,
ParamBase
,
Program
,
Variable
...
@@ -108,8 +109,8 @@ def to_variables(inputs, is_static=False):
...
@@ -108,8 +109,8 @@ def to_variables(inputs, is_static=False):
"""
"""
Find and rename variables. Find np.ndarray and convert it to variable.
Find and rename variables. Find np.ndarray and convert it to variable.
"""
"""
if
isinstance
(
inputs
,
if
isinstance
(
inputs
,
(
Variable
,
paddle
.
Tensor
))
or
isinstance
(
inputs
,
(
Variable
,
paddle
.
Tensor
))
or
isinstance
(
inputs
,
np
.
ndarray
):
np
.
ndarray
):
if
is_static
:
if
is_static
:
return
_to_var
(
inputs
)
return
_to_var
(
inputs
)
else
:
else
:
...
@@ -118,7 +119,7 @@ def to_variables(inputs, is_static=False):
...
@@ -118,7 +119,7 @@ def to_variables(inputs, is_static=False):
ret
=
{}
ret
=
{}
for
_key
in
inputs
:
for
_key
in
inputs
:
ret
[
_key
]
=
to_variables
(
inputs
[
_key
],
is_static
)
ret
[
_key
]
=
to_variables
(
inputs
[
_key
],
is_static
)
return
inputs
return
ret
elif
isinstance
(
inputs
,
list
):
elif
isinstance
(
inputs
,
list
):
ret
=
[]
ret
=
[]
for
_value
in
inputs
:
for
_value
in
inputs
:
...
@@ -140,7 +141,7 @@ def dygraph2program(layer,
...
@@ -140,7 +141,7 @@ def dygraph2program(layer,
extract_inputs_fn
=
extract_inputs_fn
if
extract_inputs_fn
is
not
None
else
extract_vars
extract_inputs_fn
=
extract_inputs_fn
if
extract_inputs_fn
is
not
None
else
extract_vars
extract_outputs_fn
=
extract_outputs_fn
if
extract_outputs_fn
is
not
None
else
extract_vars
extract_outputs_fn
=
extract_outputs_fn
if
extract_outputs_fn
is
not
None
else
extract_vars
if
os
.
environ
.
get
(
"FLAGS_enable_eager_mode"
)
==
"1"
:
if
in_dygraph_mode
()
:
return
_dy2prog
(
layer
,
inputs
,
feed_prefix
,
fetch_prefix
,
tmp_prefix
,
return
_dy2prog
(
layer
,
inputs
,
feed_prefix
,
fetch_prefix
,
tmp_prefix
,
extract_inputs_fn
,
extract_outputs_fn
,
dtypes
)
extract_inputs_fn
,
extract_outputs_fn
,
dtypes
)
...
@@ -187,7 +188,6 @@ def _dy2prog(layer,
...
@@ -187,7 +188,6 @@ def _dy2prog(layer,
Tracing program in Eager Mode.
Tracing program in Eager Mode.
"""
"""
paddle
.
enable_static
()
paddle
.
enable_static
()
program
=
Program
()
program
=
Program
()
# convert ParamBase into Parameter automatically by _switch_declarative_mode_guard_
# convert ParamBase into Parameter automatically by _switch_declarative_mode_guard_
with
program_guard
(
program
),
_switch_declarative_mode_guard_
(
True
):
with
program_guard
(
program
),
_switch_declarative_mode_guard_
(
True
):
...
@@ -198,8 +198,10 @@ def _dy2prog(layer,
...
@@ -198,8 +198,10 @@ def _dy2prog(layer,
inputs
=
_create_tensors
(
inputs
,
dtypes
=
dtypes
,
is_static
=
True
)
inputs
=
_create_tensors
(
inputs
,
dtypes
=
dtypes
,
is_static
=
True
)
else
:
else
:
inputs
=
to_variables
(
inputs
,
is_static
=
True
)
inputs
=
to_variables
(
inputs
,
is_static
=
True
)
inputs
=
extract_inputs_fn
(
inputs
)
if
isinstance
(
inputs
,
list
):
outputs
=
layer
(
*
inputs
)
outputs
=
layer
(
*
inputs
)
else
:
outputs
=
layer
(
inputs
)
paddle
.
disable_static
()
paddle
.
disable_static
()
...
...
paddleslim/nas/ofa/layers.py
浏览文件 @
ae917097
...
@@ -21,7 +21,9 @@ import paddle.nn as nn
...
@@ -21,7 +21,9 @@ import paddle.nn as nn
import
paddle.nn.functional
as
F
import
paddle.nn.functional
as
F
import
paddle.fluid.core
as
core
import
paddle.fluid.core
as
core
from
paddle
import
_C_ops
from
paddle
import
_C_ops
from
paddle.fluid.framework
import
in_dygraph_mode
from
paddle.fluid.framework
import
in_dygraph_mode
,
_in_legacy_dygraph
,
_non_static_mode
from
paddle.fluid.data_feeder
import
check_variable_and_dtype
from
paddle.fluid.dygraph.layer_object_helper
import
LayerObjectHelper
from
...common
import
get_logger
from
...common
import
get_logger
from
.utils.utils
import
compute_start_end
,
get_same_padding
,
convert_to_list
from
.utils.utils
import
compute_start_end
,
get_same_padding
,
convert_to_list
...
@@ -209,6 +211,9 @@ class SuperConv2D(nn.Conv2D):
...
@@ -209,6 +211,9 @@ class SuperConv2D(nn.Conv2D):
def
get_active_filter
(
self
,
in_nc
,
out_nc
,
kernel_size
):
def
get_active_filter
(
self
,
in_nc
,
out_nc
,
kernel_size
):
start
,
end
=
compute_start_end
(
self
.
_kernel_size
[
0
],
kernel_size
)
start
,
end
=
compute_start_end
(
self
.
_kernel_size
[
0
],
kernel_size
)
### if NOT transform kernel, intercept a center filter with kernel_size from largest filter
### if NOT transform kernel, intercept a center filter with kernel_size from largest filter
if
self
.
weight
.
shape
[
0
]
<=
out_nc
and
self
.
weight
.
shape
[
1
]
<=
in_nc
:
filters
=
self
.
weight
else
:
filters
=
self
.
weight
[:
out_nc
,
:
in_nc
,
start
:
end
,
start
:
end
]
filters
=
self
.
weight
[:
out_nc
,
:
in_nc
,
start
:
end
,
start
:
end
]
if
self
.
transform_kernel
!=
False
and
kernel_size
<
self
.
_kernel_size
[
if
self
.
transform_kernel
!=
False
and
kernel_size
<
self
.
_kernel_size
[
0
]:
0
]:
...
@@ -304,6 +309,9 @@ class SuperConv2D(nn.Conv2D):
...
@@ -304,6 +309,9 @@ class SuperConv2D(nn.Conv2D):
### if in_nc = groups, slice the shape of bias by weight_out_nc.
### if in_nc = groups, slice the shape of bias by weight_out_nc.
if
groups
!=
in_nc
:
if
groups
!=
in_nc
:
weight_out_nc
=
weight_out_nc
*
groups
weight_out_nc
=
weight_out_nc
*
groups
if
weight_out_nc
>=
self
.
bias
.
shape
[
0
]:
bias
=
self
.
bias
else
:
bias
=
self
.
bias
[:
weight_out_nc
]
bias
=
self
.
bias
[:
weight_out_nc
]
else
:
else
:
bias
=
self
.
bias
bias
=
self
.
bias
...
@@ -884,9 +892,14 @@ class SuperLinear(nn.Linear):
...
@@ -884,9 +892,14 @@ class SuperLinear(nn.Linear):
out_nc
=
int
(
channel
)
out_nc
=
int
(
channel
)
else
:
else
:
out_nc
=
self
.
_out_features
out_nc
=
self
.
_out_features
if
self
.
weight
.
shape
[
0
]
<=
in_nc
and
self
.
weight
.
shape
[
1
]
<=
out_nc
:
weight
=
self
.
weight
else
:
weight
=
self
.
weight
[:
in_nc
,
:
out_nc
]
weight
=
self
.
weight
[:
in_nc
,
:
out_nc
]
if
self
.
_bias_attr
!=
False
:
if
self
.
_bias_attr
!=
False
:
if
self
.
bias
.
shape
[
0
]
<=
out_nc
:
bias
=
self
.
bias
else
:
bias
=
self
.
bias
[:
out_nc
]
bias
=
self
.
bias
[:
out_nc
]
else
:
else
:
bias
=
self
.
bias
bias
=
self
.
bias
...
@@ -944,9 +957,21 @@ class SuperBatchNorm2D(nn.BatchNorm2D):
...
@@ -944,9 +957,21 @@ class SuperBatchNorm2D(nn.BatchNorm2D):
self
.
_check_input_dim
(
input
)
self
.
_check_input_dim
(
input
)
feature_dim
=
int
(
input
.
shape
[
1
])
feature_dim
=
int
(
input
.
shape
[
1
])
if
self
.
weight
.
shape
[
0
]
<=
feature_dim
:
weight
=
self
.
weight
else
:
weight
=
self
.
weight
[:
feature_dim
]
weight
=
self
.
weight
[:
feature_dim
]
if
self
.
bias
.
shape
[
0
]
<=
feature_dim
:
bias
=
self
.
bias
else
:
bias
=
self
.
bias
[:
feature_dim
]
bias
=
self
.
bias
[:
feature_dim
]
if
self
.
_mean
.
shape
[
0
]
<=
feature_dim
:
mean
=
self
.
_mean
else
:
mean
=
self
.
_mean
[:
feature_dim
]
mean
=
self
.
_mean
[:
feature_dim
]
if
self
.
_variance
.
shape
[
0
]
<=
feature_dim
:
variance
=
self
.
_variance
else
:
variance
=
self
.
_variance
[:
feature_dim
]
variance
=
self
.
_variance
[:
feature_dim
]
mean_out
=
self
.
_mean
mean_out
=
self
.
_mean
...
@@ -965,51 +990,91 @@ class SuperBatchNorm2D(nn.BatchNorm2D):
...
@@ -965,51 +990,91 @@ class SuperBatchNorm2D(nn.BatchNorm2D):
"use_mkldnn"
,
False
,
"fuse_with_relu"
,
False
,
"use_mkldnn"
,
False
,
"fuse_with_relu"
,
False
,
"use_global_stats"
,
self
.
_use_global_stats
,
"use_global_stats"
,
self
.
_use_global_stats
,
"trainable_statistics"
,
trainable_statistics
)
"trainable_statistics"
,
trainable_statistics
)
try
:
from
paddle
import
_C_ops
from
paddle.fluid.framework
import
in_dygraph_mode
,
_in_legacy_dygraph
if
in_dygraph_mode
():
if
in_dygraph_mode
():
if
feature_dim
!=
self
.
_mean
.
shape
[
0
]:
if
feature_dim
!=
self
.
_mean
.
shape
[
0
]:
batch_norm_out
=
_C_ops
.
final_state_batch_norm
(
batch_norm_out
,
t1
,
t2
,
t3
,
t4
,
_
=
_C_ops
.
final_state_batch_norm
(
input
,
weight
,
bias
,
mean
,
variance
,
mean_out_tmp
,
input
,
weight
,
bias
,
mean
,
variance
,
self
.
_momentum
,
variance_out_tmp
,
*
attrs
)
self
.
_epsilon
,
self
.
_data_format
,
not
self
.
training
,
self
.
_use_global_stats
,
trainable_statistics
,
False
,
False
)
self
.
_mean
[:
feature_dim
].
set_value
(
mean
)
self
.
_mean
[:
feature_dim
].
set_value
(
mean
)
self
.
_variance
[:
feature_dim
].
set_value
(
variance
)
self
.
_variance
[:
feature_dim
].
set_value
(
variance
)
mean_out
[:
feature_dim
].
set_value
(
mean_out_tmp
)
mean_out
[:
feature_dim
].
set_value
(
mean_out_tmp
)
variance_out
[:
feature_dim
].
set_value
(
variance_out_tmp
)
variance_out
[:
feature_dim
].
set_value
(
variance_out_tmp
)
return
batch_norm_out
else
:
else
:
batch_norm_out
=
_C_ops
.
final_state_batch_norm
(
batch_norm_out
,
t1
,
t2
,
t3
,
t4
,
_
=
_C_ops
.
final_state_batch_norm
(
input
,
weight
,
bias
,
self
.
_mean
,
self
.
_variance
,
input
,
weight
,
bias
,
mean
,
variance
,
self
.
_momentum
,
mean_out
,
variance_out
,
*
attrs
)
self
.
_epsilon
,
self
.
_data_format
,
not
self
.
training
,
self
.
_use_global_stats
,
trainable_statistics
,
False
)
return
batch_norm_out
elif
_in_legacy_dygraph
():
elif
_in_legacy_dygraph
():
if
feature_dim
!=
self
.
_mean
.
shape
[
0
]:
if
feature_dim
!=
self
.
_mean
.
shape
[
0
]:
batch_norm_out
=
core
.
ops
.
batch_norm
(
batch_norm_out
,
t1
,
t2
,
t3
,
t4
,
_
=
_C_
ops
.
batch_norm
(
input
,
weight
,
bias
,
mean
,
variance
,
None
,
mean_out_tmp
,
input
,
weight
,
bias
,
mean
,
variance
,
None
,
mean_out_tmp
,
variance_out_tmp
,
*
attrs
)
variance_out_tmp
,
*
attrs
)
self
.
_mean
[:
feature_dim
].
set_value
(
mean
)
self
.
_mean
[:
feature_dim
].
set_value
(
mean
)
self
.
_variance
[:
feature_dim
].
set_value
(
variance
)
self
.
_variance
[:
feature_dim
].
set_value
(
variance
)
mean_out
[:
feature_dim
].
set_value
(
mean_out_tmp
)
mean_out
[:
feature_dim
].
set_value
(
mean_out_tmp
)
variance_out
[:
feature_dim
].
set_value
(
variance_out_tmp
)
variance_out
[:
feature_dim
].
set_value
(
variance_out_tmp
)
return
batch_norm_out
else
:
else
:
batch_norm_out
=
core
.
ops
.
batch_norm
(
batch_norm_out
,
t1
,
t2
,
t3
,
t4
,
_
=
_C_
ops
.
batch_norm
(
input
,
weight
,
bias
,
self
.
_mean
,
self
.
_variance
,
None
,
input
,
weight
,
bias
,
self
.
_mean
,
self
.
_variance
,
None
,
mean_out
,
variance_out
,
*
attrs
)
mean_out
,
variance_out
,
*
attrs
)
except
:
return
batch_norm_out
if
feature_dim
!=
self
.
_mean
.
shape
[
0
]:
batch_norm_out
=
core
.
ops
.
batch_norm
(
input
,
weight
,
bias
,
mean
,
check_variable_and_dtype
(
input
,
'input'
,
variance
,
mean_out_tmp
,
[
'float16'
,
'float32'
,
'float64'
],
'BatchNorm'
)
variance_out_tmp
,
*
attrs
)
self
.
_mean
[:
feature_dim
].
set_value
(
mean
)
# for static need dict
self
.
_variance
[:
feature_dim
].
set_value
(
variance
)
attrs
=
{
mean_out
[:
feature_dim
].
set_value
(
mean_out_tmp
)
"momentum"
:
self
.
_momentum
,
variance_out
[:
feature_dim
].
set_value
(
variance_out_tmp
)
"epsilon"
:
self
.
_epsilon
,
else
:
"is_test"
:
not
self
.
training
,
batch_norm_out
=
core
.
ops
.
batch_norm
(
"data_layout"
:
self
.
_data_format
,
input
,
weight
,
bias
,
self
.
_mean
,
self
.
_variance
,
mean_out
,
"use_mkldnn"
:
False
,
variance_out
,
*
attrs
)
"fuse_with_relu"
:
False
,
"use_global_stats"
:
self
.
_use_global_stats
,
"trainable_statistics"
:
trainable_statistics
,
}
inputs
=
{
"X"
:
[
input
],
"Scale"
:
[
weight
],
"Bias"
:
[
bias
],
"Mean"
:
[
mean
],
"Variance"
:
[
variance
]
}
helper
=
LayerObjectHelper
(
'batch_norm'
)
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
)
outputs
=
{
"Y"
:
[
batch_norm_out
],
"MeanOut"
:
[
mean
],
"VarianceOut"
:
[
variance
],
"SavedMean"
:
[
saved_mean
],
"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
)
outputs
[
"ReserveSpace"
]
=
[
reserve_space
]
helper
.
append_op
(
type
=
"batch_norm"
,
inputs
=
inputs
,
outputs
=
outputs
,
attrs
=
attrs
)
self
.
cur_config
=
{
'prune_dim'
:
feature_dim
}
self
.
cur_config
=
{
'prune_dim'
:
feature_dim
}
return
batch_norm_out
[
0
]
return
batch_norm_out
class
SuperSyncBatchNorm
(
nn
.
SyncBatchNorm
):
class
SuperSyncBatchNorm
(
nn
.
SyncBatchNorm
):
...
@@ -1045,21 +1110,67 @@ class SuperSyncBatchNorm(nn.SyncBatchNorm):
...
@@ -1045,21 +1110,67 @@ class SuperSyncBatchNorm(nn.SyncBatchNorm):
"is_test"
,
not
self
.
training
,
"data_layout"
,
self
.
_data_format
,
"is_test"
,
not
self
.
training
,
"data_layout"
,
self
.
_data_format
,
"use_mkldnn"
,
False
,
"fuse_with_relu"
,
False
,
"use_mkldnn"
,
False
,
"fuse_with_relu"
,
False
,
"use_global_stats"
,
False
,
'trainable_statistics'
,
False
)
"use_global_stats"
,
False
,
'trainable_statistics'
,
False
)
if
_non_static_mode
():
if
feature_dim
!=
self
.
_mean
.
shape
[
0
]:
if
feature_dim
!=
self
.
_mean
.
shape
[
0
]:
sync_batch_norm_out
,
_
,
_
,
_
,
_
,
_
=
core
.
ops
.
sync_batch_norm
(
sync_batch_norm_out
,
_
,
_
,
_
,
_
,
_
=
_C_ops
.
sync_batch_norm
(
input
,
weight
,
bias
,
mean
,
variance
,
mean_out_tmp
,
input
,
weight
,
bias
,
self
.
_mean
,
self
.
_variance
,
mean_out
,
variance_out_tmp
,
*
attrs
)
variance_out
,
*
attrs
)
self
.
_mean
[:
feature_dim
].
set_value
(
mean
)
self
.
_mean
[:
feature_dim
].
set_value
(
mean
)
self
.
_variance
[:
feature_dim
].
set_value
(
variance
)
self
.
_variance
[:
feature_dim
].
set_value
(
variance
)
mean_out
[:
feature_dim
].
set_value
(
mean_out_tmp
)
mean_out
[:
feature_dim
].
set_value
(
mean_out_tmp
)
variance_out
[:
feature_dim
].
set_value
(
variance_out_tmp
)
variance_out
[:
feature_dim
].
set_value
(
variance_out_tmp
)
else
:
else
:
sync_batch_norm_out
,
_
,
_
,
_
,
_
,
_
=
core
.
ops
.
sync_batch_norm
(
sync_batch_norm_out
,
_
,
_
,
_
,
_
,
_
=
_C_
ops
.
sync_batch_norm
(
input
,
weight
,
bias
,
self
.
_mean
,
self
.
_variance
,
mean_out
,
input
,
weight
,
bias
,
self
.
_mean
,
self
.
_variance
,
mean_out
,
variance_out
,
*
attrs
)
variance_out
,
*
attrs
)
return
sync_batch_norm_out
return
sync_batch_norm_out
check_variable_and_dtype
(
input
,
'input'
,
[
'float16'
,
'float32'
,
'float64'
],
'SyncBatchNorm'
)
attrs
=
{
"momentum"
:
self
.
_momentum
,
"epsilon"
:
self
.
_epsilon
,
"is_test"
:
not
self
.
training
,
"data_layout"
:
self
.
_data_format
,
"use_mkldnn"
:
False
,
"fuse_with_relu"
:
False
,
"use_global_stats"
:
False
,
"trainable_statistics"
:
False
,
}
inputs
=
{
"X"
:
[
input
],
"Scale"
:
[
weight
],
"Bias"
:
[
bias
],
"Mean"
:
[
self
.
_mean
],
"Variance"
:
[
self
.
_variance
]
}
helper
=
LayerObjectHelper
(
'sync_batch_norm'
)
saved_mean
=
helper
.
create_variable_for_type_inference
(
dtype
=
self
.
_dtype
,
stop_gradient
=
True
)
saved_variance
=
helper
.
create_variable_for_type_inference
(
dtype
=
self
.
_dtype
,
stop_gradient
=
True
)
sync_batch_norm_out
=
helper
.
create_variable_for_type_inference
(
self
.
_dtype
)
outputs
=
{
"Y"
:
[
sync_batch_norm_out
],
"MeanOut"
:
[
mean_out
],
"VarianceOut"
:
[
variance_out
],
"SavedMean"
:
[
saved_mean
],
"SavedVariance"
:
[
saved_variance
]
}
helper
.
append_op
(
type
=
"sync_batch_norm"
,
inputs
=
inputs
,
outputs
=
outputs
,
attrs
=
attrs
)
return
sync_batch_norm_out
class
SuperInstanceNorm2D
(
nn
.
InstanceNorm2D
):
class
SuperInstanceNorm2D
(
nn
.
InstanceNorm2D
):
"""
"""
...
@@ -1173,22 +1284,66 @@ class SuperLayerNorm(nn.LayerNorm):
...
@@ -1173,22 +1284,66 @@ class SuperLayerNorm(nn.LayerNorm):
begin_norm_axis
=
input_ndim
-
normalized_ndim
begin_norm_axis
=
input_ndim
-
normalized_ndim
feature_dim
=
int
(
input
.
shape
[
-
1
])
feature_dim
=
int
(
input
.
shape
[
-
1
])
if
self
.
_weight_attr
!=
False
:
if
self
.
_weight_attr
!=
False
:
if
self
.
weight
.
shape
[
0
]
<=
feature_dim
:
weight
=
self
.
weight
else
:
weight
=
self
.
weight
[:
feature_dim
]
weight
=
self
.
weight
[:
feature_dim
]
else
:
else
:
weight
=
None
weight
=
None
if
self
.
_bias_attr
!=
False
:
if
self
.
_bias_attr
!=
False
:
if
self
.
bias
.
shape
[
0
]
<=
feature_dim
:
bias
=
self
.
bias
else
:
bias
=
self
.
bias
[:
feature_dim
]
bias
=
self
.
bias
[:
feature_dim
]
else
:
else
:
bias
=
None
bias
=
None
self
.
cur_config
=
{
'prune_dim'
:
feature_dim
}
self
.
cur_config
=
{
'prune_dim'
:
feature_dim
}
if
in_dygraph_mode
():
if
in_dygraph_mode
():
out
,
_
,
_
,
=
_C_ops
.
final_state_layer_norm
(
out
,
_
,
_
=
_C_ops
.
final_state_layer_norm
(
input
,
weight
,
bias
,
self
.
_epsilon
,
begin_norm_axis
,
False
)
input
,
weight
,
bias
,
self
.
_epsilon
,
begin_norm_axis
,
False
)
el
se
:
el
if
_in_legacy_dygraph
()
:
out
,
_
,
_
=
core
.
ops
.
layer_norm
(
input
,
weight
,
bias
,
'epsilon'
,
out
,
_
,
_
=
_C_
ops
.
layer_norm
(
input
,
weight
,
bias
,
'epsilon'
,
self
.
_epsilon
,
'begin_norm_axis'
,
self
.
_epsilon
,
'begin_norm_axis'
,
begin_norm_axis
)
begin_norm_axis
)
else
:
check_variable_and_dtype
(
input
,
'input'
,
[
'float32'
,
'float64'
],
'LayerNorm'
)
inputs
=
dict
()
inputs
[
'X'
]
=
[
input
]
if
weight
:
inputs
[
'Scale'
]
=
[
weight
]
if
bias
:
inputs
[
'Bias'
]
=
[
bias
]
attrs
=
{
"epsilon"
:
self
.
_epsilon
,
"begin_norm_axis"
:
begin_norm_axis
}
helper
=
LayerObjectHelper
(
'layer_norm'
)
dtype
=
input
.
dtype
mean_out
=
helper
.
create_variable_for_type_inference
(
dtype
=
dtype
,
stop_gradient
=
True
)
variance_out
=
helper
.
create_variable_for_type_inference
(
dtype
=
dtype
,
stop_gradient
=
True
)
layer_norm_out
=
helper
.
create_variable_for_type_inference
(
dtype
)
helper
.
append_op
(
type
=
"layer_norm"
,
inputs
=
inputs
,
outputs
=
{
"Y"
:
layer_norm_out
,
"Mean"
:
mean_out
,
"Variance"
:
variance_out
,
},
attrs
=
{
"epsilon"
:
self
.
_epsilon
,
"begin_norm_axis"
:
begin_norm_axis
})
return
layer_norm_out
return
out
return
out
...
@@ -1274,6 +1429,9 @@ class SuperEmbedding(nn.Embedding):
...
@@ -1274,6 +1429,9 @@ class SuperEmbedding(nn.Embedding):
else
:
else
:
out_nc
=
self
.
_embedding_dim
out_nc
=
self
.
_embedding_dim
if
self
.
weight
.
shape
[
1
]
<=
out_nc
:
weight
=
self
.
weight
else
:
weight
=
self
.
weight
[:,
:
out_nc
]
weight
=
self
.
weight
[:,
:
out_nc
]
self
.
cur_config
=
{
'prune_dim'
:
list
(
weight
.
shape
)}
self
.
cur_config
=
{
'prune_dim'
:
list
(
weight
.
shape
)}
return
F
.
embedding
(
return
F
.
embedding
(
...
...
paddleslim/nas/ofa/layers_old.py
浏览文件 @
ae917097
...
@@ -20,7 +20,10 @@ import paddle.fluid as fluid
...
@@ -20,7 +20,10 @@ import paddle.fluid as fluid
import
paddle.fluid.core
as
core
import
paddle.fluid.core
as
core
import
paddle.fluid.dygraph_utils
as
dygraph_utils
import
paddle.fluid.dygraph_utils
as
dygraph_utils
from
paddle.fluid.data_feeder
import
check_variable_and_dtype
from
paddle.fluid.data_feeder
import
check_variable_and_dtype
from
paddle.fluid.framework
import
_varbase_creator
from
paddle.fluid.framework
import
_varbase_creator
,
in_dygraph_mode
,
_in_legacy_dygraph
,
_non_static_mode
from
paddle
import
_C_ops
from
paddle.fluid.data_feeder
import
check_variable_and_dtype
from
paddle.fluid.dygraph.layer_object_helper
import
LayerObjectHelper
from
paddle.fluid.dygraph.nn
import
InstanceNorm
,
Conv2D
,
Conv2DTranspose
,
BatchNorm
from
paddle.fluid.dygraph.nn
import
InstanceNorm
,
Conv2D
,
Conv2DTranspose
,
BatchNorm
from
...common
import
get_logger
from
...common
import
get_logger
...
@@ -235,11 +238,13 @@ class SuperConv2D(fluid.dygraph.Conv2D):
...
@@ -235,11 +238,13 @@ class SuperConv2D(fluid.dygraph.Conv2D):
shape
=
[(
_input_filter
.
shape
[
0
]
*
_input_filter
.
shape
[
1
]),
shape
=
[(
_input_filter
.
shape
[
0
]
*
_input_filter
.
shape
[
1
]),
-
1
])
-
1
])
_tmp_filter
=
_varbase_creator
(
dtype
=
_input_filter
.
dtype
)
_tmp_filter
=
_varbase_creator
(
dtype
=
_input_filter
.
dtype
)
core
.
ops
.
matmul
(
_input_filter
,
if
_non_static_mode
():
_C_ops
.
matmul
(
_input_filter
,
self
.
__getattr__
(
'%dto%d_matrix'
%
self
.
__getattr__
(
'%dto%d_matrix'
%
(
src_ks
,
target_ks
)),
(
src_ks
,
target_ks
)),
_tmp_filter
,
'transpose_X'
,
False
,
_tmp_filter
,
'transpose_X'
,
False
,
'transpose_Y'
,
False
,
"alpha"
,
1
)
'transpose_Y'
,
False
,
"alpha"
,
1
)
_tmp_filter
=
fluid
.
layers
.
reshape
(
_tmp_filter
=
fluid
.
layers
.
reshape
(
_tmp_filter
,
_tmp_filter
,
shape
=
[
shape
=
[
...
@@ -306,7 +311,12 @@ class SuperConv2D(fluid.dygraph.Conv2D):
...
@@ -306,7 +311,12 @@ class SuperConv2D(fluid.dygraph.Conv2D):
attrs
=
(
'strides'
,
self
.
_stride
,
'paddings'
,
padding
,
'dilations'
,
attrs
=
(
'strides'
,
self
.
_stride
,
'paddings'
,
padding
,
'dilations'
,
self
.
_dilation
,
'groups'
,
groups
self
.
_dilation
,
'groups'
,
groups
if
groups
else
1
,
'use_cudnn'
,
self
.
_use_cudnn
)
if
groups
else
1
,
'use_cudnn'
,
self
.
_use_cudnn
)
out
=
core
.
ops
.
conv2d
(
input
,
weight
,
*
attrs
)
if
in_dygraph_mode
():
out
=
_C_ops
.
final_state_conv2d
(
input
,
weight
,
self
.
_stride
,
padding
,
"EXPLICIT"
,
groups
if
groups
else
1
,
self
.
_dilation
,
"NCHW"
,
False
,
-
1
,
False
)
elif
_in_legacy_dygraph
():
out
=
_C_ops
.
conv2d
(
input
,
weight
,
*
attrs
)
elif
self
.
_l_type
==
'depthwise_conv2d'
:
elif
self
.
_l_type
==
'depthwise_conv2d'
:
attrs
=
(
'strides'
,
self
.
_stride
,
'paddings'
,
padding
,
'dilations'
,
attrs
=
(
'strides'
,
self
.
_stride
,
'paddings'
,
padding
,
'dilations'
,
self
.
_dilation
,
'groups'
,
groups
self
.
_dilation
,
'groups'
,
groups
...
@@ -540,11 +550,13 @@ class SuperConv2DTranspose(fluid.dygraph.Conv2DTranspose):
...
@@ -540,11 +550,13 @@ class SuperConv2DTranspose(fluid.dygraph.Conv2DTranspose):
shape
=
[(
_input_filter
.
shape
[
0
]
*
_input_filter
.
shape
[
1
]),
shape
=
[(
_input_filter
.
shape
[
0
]
*
_input_filter
.
shape
[
1
]),
-
1
])
-
1
])
_tmp_filter
=
_varbase_creator
(
dtype
=
_input_filter
.
dtype
)
_tmp_filter
=
_varbase_creator
(
dtype
=
_input_filter
.
dtype
)
core
.
ops
.
matmul
(
_input_filter
,
if
_non_static_mode
():
_C_ops
.
matmul
(
_input_filter
,
self
.
__getattr__
(
'%dto%d_matrix'
%
self
.
__getattr__
(
'%dto%d_matrix'
%
(
src_ks
,
target_ks
)),
(
src_ks
,
target_ks
)),
_tmp_filter
,
'transpose_X'
,
False
,
_tmp_filter
,
'transpose_X'
,
False
,
'transpose_Y'
,
False
,
"alpha"
,
1
)
'transpose_Y'
,
False
,
"alpha"
,
1
)
_tmp_filter
=
fluid
.
layers
.
reshape
(
_tmp_filter
=
fluid
.
layers
.
reshape
(
_tmp_filter
,
_tmp_filter
,
shape
=
[
shape
=
[
...
@@ -607,10 +619,13 @@ class SuperConv2DTranspose(fluid.dygraph.Conv2DTranspose):
...
@@ -607,10 +619,13 @@ class SuperConv2DTranspose(fluid.dygraph.Conv2DTranspose):
else
:
else
:
padding
=
self
.
_padding
padding
=
self
.
_padding
op
=
getattr
(
core
.
ops
,
self
.
_op_type
)
if
_non_static_mode
():
op
=
getattr
(
_C_ops
,
self
.
_op_type
)
out
=
op
(
input
,
weight
,
'output_size'
,
self
.
_output_size
,
'strides'
,
out
=
op
(
input
,
weight
,
'output_size'
,
self
.
_output_size
,
'strides'
,
self
.
_stride
,
'paddings'
,
padding
,
'dilations'
,
self
.
_dilation
,
self
.
_stride
,
'paddings'
,
padding
,
'dilations'
,
'groups'
,
groups
,
'use_cudnn'
,
self
.
_use_cudnn
)
self
.
_dilation
,
'groups'
,
groups
,
'use_cudnn'
,
self
.
_use_cudnn
)
pre_bias
=
out
pre_bias
=
out
out_nc
=
int
(
pre_bias
.
shape
[
1
])
out_nc
=
int
(
pre_bias
.
shape
[
1
])
if
self
.
bias
is
not
None
:
if
self
.
bias
is
not
None
:
...
@@ -749,15 +764,33 @@ class SuperSeparableConv2D(fluid.dygraph.Layer):
...
@@ -749,15 +764,33 @@ class SuperSeparableConv2D(fluid.dygraph.Layer):
weight
=
self
.
conv
[
0
].
weight
[:
in_nc
]
weight
=
self
.
conv
[
0
].
weight
[:
in_nc
]
### conv1
### conv1
if
self
.
conv
[
0
].
_l_type
==
'conv2d'
:
if
self
.
conv
[
0
].
_l_type
==
'conv2d'
:
if
in_dygraph_mode
():
out
=
_C_ops
.
final_state_conv2d
(
input
,
weight
,
self
.
conv
[
0
].
_stride
,
self
.
conv
[
0
].
_padding
,
"EXPLICIT"
,
in_nc
,
self
.
conv
[
0
].
_dilation
,
"NCHW"
,
False
,
-
1
,
False
)
elif
_in_legacy_dygraph
():
attrs
=
(
'strides'
,
self
.
conv
[
0
].
_stride
,
'paddings'
,
attrs
=
(
'strides'
,
self
.
conv
[
0
].
_stride
,
'paddings'
,
self
.
conv
[
0
].
_padding
,
'dilations'
,
self
.
conv
[
0
].
_dilation
,
self
.
conv
[
0
].
_padding
,
'dilations'
,
'groups'
,
in_nc
,
'use_cudnn'
,
self
.
conv
[
0
].
_use_cudnn
)
self
.
conv
[
0
].
_dilation
,
'groups'
,
in_nc
,
'use_cudnn'
,
out
=
core
.
ops
.
conv2d
(
input
,
weight
,
*
attrs
)
self
.
conv
[
0
].
_use_cudnn
)
out
=
_C_ops
.
conv2d
(
input
,
weight
,
*
attrs
)
elif
self
.
conv
[
0
].
_l_type
==
'depthwise_conv2d'
:
elif
self
.
conv
[
0
].
_l_type
==
'depthwise_conv2d'
:
if
in_dygraph_mode
():
out
=
_C_ops
.
final_state_depthwise_conv2d
(
input
,
weight
,
self
.
conv
[
0
].
_stride
,
self
.
conv
[
0
].
_padding
,
"EXPLICIT"
,
in_nc
,
self
.
conv
[
0
].
_dilation
,
"NCHW"
,
False
,
-
1
,
False
,
False
,
self
.
conv
[
0
].
_use_cudnn
)
elif
_in_legacy_dygraph
():
attrs
=
(
'strides'
,
self
.
conv
[
0
].
_stride
,
'paddings'
,
attrs
=
(
'strides'
,
self
.
conv
[
0
].
_stride
,
'paddings'
,
self
.
conv
[
0
].
_padding
,
'dilations'
,
self
.
conv
[
0
].
_dilation
,
self
.
conv
[
0
].
_padding
,
'dilations'
,
'groups'
,
in_nc
,
'use_cudnn'
,
self
.
conv
[
0
].
_use_cudnn
)
self
.
conv
[
0
].
_dilation
,
'groups'
,
in_nc
,
'use_cudnn'
,
out
=
core
.
ops
.
depthwise_conv2d
(
input
,
weight
,
*
attrs
)
self
.
conv
[
0
].
_use_cudnn
)
out
=
_C_ops
.
depthwise_conv2d
(
input
,
weight
,
*
attrs
)
else
:
else
:
raise
ValueError
(
"conv type error"
)
raise
ValueError
(
"conv type error"
)
...
@@ -776,11 +809,19 @@ class SuperSeparableConv2D(fluid.dygraph.Layer):
...
@@ -776,11 +809,19 @@ class SuperSeparableConv2D(fluid.dygraph.Layer):
weight
=
self
.
conv
[
2
].
weight
[:
out_nc
,
:
in_nc
,
:,
:]
weight
=
self
.
conv
[
2
].
weight
[:
out_nc
,
:
in_nc
,
:,
:]
if
self
.
conv
[
2
].
_l_type
==
'conv2d'
:
if
self
.
conv
[
2
].
_l_type
==
'conv2d'
:
if
in_dygraph_mode
():
out
=
_C_ops
.
final_state_conv2d
(
input
,
weight
,
self
.
conv
[
2
].
_stride
,
self
.
conv
[
2
].
_padding
,
"EXPLICIT"
,
self
.
conv
[
2
].
_groups
if
self
.
conv
[
2
].
_groups
else
1
,
self
.
conv
[
2
].
_dilation
,
"NCHW"
,
False
,
-
1
,
False
)
elif
_in_legacy_dygraph
():
attrs
=
(
'strides'
,
self
.
conv
[
2
].
_stride
,
'paddings'
,
attrs
=
(
'strides'
,
self
.
conv
[
2
].
_stride
,
'paddings'
,
self
.
conv
[
2
].
_padding
,
'dilations'
,
self
.
conv
[
2
].
_dilation
,
self
.
conv
[
2
].
_padding
,
'dilations'
,
'groups'
,
self
.
conv
[
2
].
_groups
if
self
.
conv
[
2
].
_groups
else
self
.
conv
[
2
].
_dilation
,
'groups'
,
self
.
conv
[
2
].
_groups
1
,
'use_cudnn'
,
self
.
conv
[
2
].
_use_cudnn
)
if
self
.
conv
[
2
].
_groups
else
1
,
'use_cudnn'
,
out
=
core
.
ops
.
conv2d
(
norm_out
,
weight
,
*
attrs
)
self
.
conv
[
2
].
_use_cudnn
)
out
=
_C_ops
.
conv2d
(
norm_out
,
weight
,
*
attrs
)
elif
self
.
conv
[
2
].
_l_type
==
'depthwise_conv2d'
:
elif
self
.
conv
[
2
].
_l_type
==
'depthwise_conv2d'
:
attrs
=
(
'strides'
,
self
.
conv
[
2
].
_stride
,
'paddings'
,
attrs
=
(
'strides'
,
self
.
conv
[
2
].
_stride
,
'paddings'
,
self
.
conv
[
2
].
_padding
,
'dilations'
,
self
.
conv
[
2
].
_dilation
,
self
.
conv
[
2
].
_padding
,
'dilations'
,
self
.
conv
[
2
].
_dilation
,
...
@@ -847,8 +888,10 @@ class SuperLinear(fluid.dygraph.Linear):
...
@@ -847,8 +888,10 @@ class SuperLinear(fluid.dygraph.Linear):
use_bias
=
True
use_bias
=
True
pre_bias
=
_varbase_creator
(
dtype
=
input
.
dtype
)
pre_bias
=
_varbase_creator
(
dtype
=
input
.
dtype
)
core
.
ops
.
matmul
(
input
,
weight
,
pre_bias
,
'transpose_X'
,
False
,
if
_non_static_mode
():
_C_ops
.
matmul
(
input
,
weight
,
pre_bias
,
'transpose_X'
,
False
,
'transpose_Y'
,
False
,
"alpha"
,
1
)
'transpose_Y'
,
False
,
"alpha"
,
1
)
if
self
.
_bias_attr
!=
False
:
if
self
.
_bias_attr
!=
False
:
pre_act
=
dygraph_utils
.
_append_bias_in_dygraph
(
pre_act
=
dygraph_utils
.
_append_bias_in_dygraph
(
pre_bias
,
bias
,
axis
=
len
(
input
.
shape
)
-
1
)
pre_bias
,
bias
,
axis
=
len
(
input
.
shape
)
-
1
)
...
@@ -903,25 +946,27 @@ class SuperBatchNorm(fluid.dygraph.BatchNorm):
...
@@ -903,25 +946,27 @@ class SuperBatchNorm(fluid.dygraph.BatchNorm):
"use_mkldnn"
,
False
,
"fuse_with_relu"
,
self
.
_fuse_with_relu
,
"use_mkldnn"
,
False
,
"fuse_with_relu"
,
self
.
_fuse_with_relu
,
"use_global_stats"
,
self
.
_use_global_stats
,
"use_global_stats"
,
self
.
_use_global_stats
,
'trainable_statistics'
,
self
.
_trainable_statistics
)
'trainable_statistics'
,
self
.
_trainable_statistics
)
try
:
from
paddle
import
_C_ops
from
paddle.fluid.framework
import
in_dygraph_mode
,
_in_legacy_dygraph
if
in_dygraph_mode
():
if
in_dygraph_mode
():
if
feature_dim
!=
self
.
_mean
.
shape
[
0
]:
if
feature_dim
!=
self
.
_mean
.
shape
[
0
]:
batch_norm_out
=
_C_ops
.
final_state_batch_norm
(
batch_norm_out
,
t1
,
t2
,
t3
,
t4
,
_
=
_C_ops
.
final_state_batch_norm
(
input
,
weight
,
bias
,
mean
,
variance
,
mean_out_tmp
,
input
,
weight
,
bias
,
mean
,
variance
,
self
.
_momentum
,
variance_out_tmp
,
*
attrs
)
self
.
_epsilon
,
self
.
_data_layout
,
not
self
.
training
,
self
.
_use_global_stats
,
self
.
_trainable_statistics
,
False
)
self
.
_mean
[:
feature_dim
]
=
mean
self
.
_mean
[:
feature_dim
]
=
mean
self
.
_variance
[:
feature_dim
]
=
variance
self
.
_variance
[:
feature_dim
]
=
variance
mean_out
[:
feature_dim
]
=
mean_out_tmp
mean_out
[:
feature_dim
]
=
mean_out_tmp
variance_out
[:
feature_dim
]
=
variance_out_tmp
variance_out
[:
feature_dim
]
=
variance_out_tmp
else
:
else
:
batch_norm_out
=
core
.
ops
.
batch_norm
(
batch_norm_out
,
t1
,
t2
,
t3
,
t4
,
_
=
_C_ops
.
final_state_batch_norm
(
input
,
weight
,
bias
,
self
.
_mean
,
self
.
_variance
,
input
,
weight
,
bias
,
mean
,
variance
,
self
.
_momentum
,
mean_out
,
variance_out
,
*
attrs
)
self
.
_epsilon
,
self
.
_data_layout
,
not
self
.
training
,
self
.
_use_global_stats
,
self
.
_trainable_statistics
,
False
)
return
batch_norm_out
elif
_in_legacy_dygraph
():
elif
_in_legacy_dygraph
():
if
feature_dim
!=
self
.
_mean
.
shape
[
0
]:
if
feature_dim
!=
self
.
_mean
.
shape
[
0
]:
batch_norm_out
=
core
.
ops
.
batch_norm
(
batch_norm_out
,
t1
,
t2
,
t3
,
t4
,
_
=
_C_
ops
.
batch_norm
(
input
,
weight
,
bias
,
mean
,
variance
,
None
,
mean_out_tmp
,
input
,
weight
,
bias
,
mean
,
variance
,
None
,
mean_out_tmp
,
variance_out_tmp
,
*
attrs
)
variance_out_tmp
,
*
attrs
)
self
.
_mean
[:
feature_dim
].
set_value
(
mean
)
self
.
_mean
[:
feature_dim
].
set_value
(
mean
)
...
@@ -929,25 +974,63 @@ class SuperBatchNorm(fluid.dygraph.BatchNorm):
...
@@ -929,25 +974,63 @@ class SuperBatchNorm(fluid.dygraph.BatchNorm):
mean_out
[:
feature_dim
].
set_value
(
mean_out_tmp
)
mean_out
[:
feature_dim
].
set_value
(
mean_out_tmp
)
variance_out
[:
feature_dim
].
set_value
(
variance_out_tmp
)
variance_out
[:
feature_dim
].
set_value
(
variance_out_tmp
)
else
:
else
:
batch_norm_out
=
core
.
ops
.
batch_norm
(
batch_norm_out
,
t1
,
t2
,
t3
,
t4
,
_
=
_C_
ops
.
batch_norm
(
input
,
weight
,
bias
,
self
.
_mean
,
self
.
_variance
,
None
,
input
,
weight
,
bias
,
self
.
_mean
,
self
.
_variance
,
None
,
mean_out
,
variance_out
,
*
attrs
)
mean_out
,
variance_out
,
*
attrs
)
except
:
return
batch_norm_out
if
feature_dim
!=
self
.
_mean
.
shape
[
0
]:
batch_norm_out
=
core
.
ops
.
batch_norm
(
input
,
weight
,
bias
,
mean
,
variance
,
mean_out_tmp
,
variance_out_tmp
,
*
attrs
)
self
.
_mean
[:
feature_dim
].
set_value
(
mean
)
self
.
_variance
[:
feature_dim
].
set_value
(
variance
)
mean_out
[:
feature_dim
].
set_value
(
mean_out_tmp
)
variance_out
[:
feature_dim
].
set_value
(
variance_out_tmp
)
else
:
else
:
batch_norm_out
=
core
.
ops
.
batch_norm
(
check_variable_and_dtype
(
input
,
weight
,
bias
,
self
.
_mean
,
self
.
_variance
,
mean_out
,
input
,
'input'
,
[
'float16'
,
'float32'
,
'float64'
],
'BatchNorm'
)
variance_out
,
*
attrs
)
# for static need dict
attrs
=
{
"momentum"
:
self
.
_momentum
,
"epsilon"
:
self
.
_epsilon
,
"is_test"
:
not
self
.
training
,
"data_layout"
:
self
.
_data_layout
,
"use_mkldnn"
:
False
,
"fuse_with_relu"
:
False
,
"use_global_stats"
:
self
.
_use_global_stats
,
"trainable_statistics"
:
self
.
_trainable_statistics
,
}
return
dygraph_utils
.
_append_activation_in_dygraph
(
inputs
=
{
batch_norm_out
[
0
],
act
=
self
.
_act
)
"X"
:
[
input
],
"Scale"
:
[
weight
],
"Bias"
:
[
bias
],
"Mean"
:
[
mean
],
"Variance"
:
[
variance
]
}
helper
=
LayerObjectHelper
(
'batch_norm'
)
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
)
outputs
=
{
"Y"
:
[
batch_norm_out
],
"MeanOut"
:
[
mean
],
"VarianceOut"
:
[
variance
],
"SavedMean"
:
[
saved_mean
],
"SavedVariance"
:
[
saved_variance
]
}
if
self
.
training
or
self
.
_trainable_statistics
:
# reserve_space is only used for training.
reserve_space
=
helper
.
create_variable_for_type_inference
(
dtype
=
input
.
dtype
,
stop_gradient
=
True
)
outputs
[
"ReserveSpace"
]
=
[
reserve_space
]
helper
.
append_op
(
type
=
"batch_norm"
,
inputs
=
inputs
,
outputs
=
outputs
,
attrs
=
attrs
)
return
batch_norm_out
class
SuperInstanceNorm
(
fluid
.
dygraph
.
InstanceNorm
):
class
SuperInstanceNorm
(
fluid
.
dygraph
.
InstanceNorm
):
...
@@ -973,7 +1056,12 @@ class SuperInstanceNorm(fluid.dygraph.InstanceNorm):
...
@@ -973,7 +1056,12 @@ class SuperInstanceNorm(fluid.dygraph.InstanceNorm):
scale
=
self
.
scale
[:
feature_dim
]
scale
=
self
.
scale
[:
feature_dim
]
bias
=
self
.
bias
[:
feature_dim
]
bias
=
self
.
bias
[:
feature_dim
]
out
,
_
,
_
=
core
.
ops
.
instance_norm
(
input
,
scale
,
bias
,
'epsilon'
,
if
in_dygraph_mode
():
out
=
_C_ops
.
final_state_instance_norm
(
input
,
scale
,
bias
,
self
.
_epsilon
)
return
out
if
_in_legacy_dygraph
():
out
,
_
,
_
=
_C_ops
.
instance_norm
(
input
,
scale
,
bias
,
'epsilon'
,
self
.
_epsilon
)
self
.
_epsilon
)
return
out
return
out
...
@@ -1002,11 +1090,15 @@ class SuperLayerNorm(fluid.dygraph.LayerNorm):
...
@@ -1002,11 +1090,15 @@ class SuperLayerNorm(fluid.dygraph.LayerNorm):
feature_dim
=
int
(
input
.
shape
[
-
1
])
feature_dim
=
int
(
input
.
shape
[
-
1
])
weight
=
self
.
weight
[:
feature_dim
]
weight
=
self
.
weight
[:
feature_dim
]
bias
=
self
.
bias
[:
feature_dim
]
bias
=
self
.
bias
[:
feature_dim
]
pre_act
,
_
,
_
=
core
.
ops
.
layer_norm
(
input
,
weight
,
bias
,
'epsilon'
,
if
in_dygraph_mode
():
pre_act
,
_
,
_
,
=
_C_ops
.
final_state_layer_norm
(
input
,
weight
,
bias
,
self
.
_epsilon
,
self
.
_begin_norm_axis
,
False
)
elif
_in_legacy_dygraph
():
pre_act
,
_
,
_
=
_C_ops
.
layer_norm
(
input
,
weight
,
bias
,
'epsilon'
,
self
.
_epsilon
,
'begin_norm_axis'
,
self
.
_epsilon
,
'begin_norm_axis'
,
self
.
_begin_norm_axis
)
self
.
_begin_norm_axis
)
return
dygraph_utils
.
_append_activation_in_dygraph
(
return
pre_act
pre_act
,
act
=
self
.
_act
)
class
SuperEmbedding
(
fluid
.
dygraph
.
Embedding
):
class
SuperEmbedding
(
fluid
.
dygraph
.
Embedding
):
...
@@ -1039,7 +1131,11 @@ class SuperEmbedding(fluid.dygraph.Embedding):
...
@@ -1039,7 +1131,11 @@ class SuperEmbedding(fluid.dygraph.Embedding):
out_nc
=
self
.
_size
[
-
1
]
out_nc
=
self
.
_size
[
-
1
]
weight
=
self
.
weight
[:,
:
out_nc
]
weight
=
self
.
weight
[:,
:
out_nc
]
return
core
.
ops
.
lookup_table_v2
(
if
in_dygraph_mode
():
return
_C_ops
.
final_state_embedding
(
input
,
weight
,
self
.
_padding_idx
,
self
.
_is_sparse
)
elif
_in_legacy_dygraph
():
return
_C_ops
.
lookup_table_v2
(
weight
,
input
,
'is_sparse'
,
self
.
_is_sparse
,
'is_distributed'
,
weight
,
input
,
'is_sparse'
,
self
.
_is_sparse
,
'is_distributed'
,
self
.
_is_distributed
,
'remote_prefetch'
,
self
.
_remote_prefetch
,
self
.
_is_distributed
,
'remote_prefetch'
,
self
.
_remote_prefetch
,
'padding_idx'
,
self
.
_padding_idx
)
'padding_idx'
,
self
.
_padding_idx
)
tests/dygraph/test_flops.py
浏览文件 @
ae917097
...
@@ -65,7 +65,6 @@ class TestFLOPsCase2(unittest.TestCase):
...
@@ -65,7 +65,6 @@ class TestFLOPsCase2(unittest.TestCase):
net
=
Net2
()
net
=
Net2
()
x
=
np
.
random
.
uniform
(
-
1
,
1
,
x_shape
).
astype
(
'float32'
)
x
=
np
.
random
.
uniform
(
-
1
,
1
,
x_shape
).
astype
(
'float32'
)
y
=
np
.
random
.
uniform
(
-
1
,
1
,
y_shape
).
astype
(
'float32'
)
y
=
np
.
random
.
uniform
(
-
1
,
1
,
y_shape
).
astype
(
'float32'
)
inputs
=
[
paddle
.
to_tensor
(
x
),
paddle
.
to_tensor
(
y
)]
inputs
=
[
paddle
.
to_tensor
(
x
),
paddle
.
to_tensor
(
y
)]
FLOPs1
=
flops
(
net
,
inputs
,
only_conv
=
False
)
FLOPs1
=
flops
(
net
,
inputs
,
only_conv
=
False
)
shapes
=
[
x_shape
,
y_shape
]
shapes
=
[
x_shape
,
y_shape
]
...
...
tests/test_dy2prog.py
浏览文件 @
ae917097
import
os
import
os
import
sys
import
sys
sys
.
path
.
append
(
"../"
)
sys
.
path
.
append
(
"../"
)
os
.
environ
[
'FLAGS_enable_eager_mode'
]
=
"1"
import
paddle
import
paddle
import
unittest
import
unittest
from
paddleslim.core
import
dygraph2program
from
paddleslim.core
import
dygraph2program
...
@@ -25,7 +26,6 @@ class Model(paddle.nn.Layer):
...
@@ -25,7 +26,6 @@ class Model(paddle.nn.Layer):
class
TestEagerDygraph2Program
(
unittest
.
TestCase
):
class
TestEagerDygraph2Program
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
os
.
environ
[
'FLAGS_enable_eager_mode'
]
=
"1"
self
.
prepare_inputs
()
self
.
prepare_inputs
()
self
.
prepare_layer
()
self
.
prepare_layer
()
...
...
tests/test_ofa.py
浏览文件 @
ae917097
...
@@ -323,6 +323,8 @@ class TestOFA(unittest.TestCase):
...
@@ -323,6 +323,8 @@ class TestOFA(unittest.TestCase):
for
model_no
in
range
(
self
.
run_config
.
dynamic_batch_size
[
for
model_no
in
range
(
self
.
run_config
.
dynamic_batch_size
[
idx
]):
idx
]):
output
=
ofa_model
(
self
.
data
)
output
=
ofa_model
(
self
.
data
)
if
(
isinstance
(
output
,
tuple
)):
output
=
output
[
0
]
loss
=
paddle
.
mean
(
output
)
loss
=
paddle
.
mean
(
output
)
if
self
.
distill_config
.
mapping_layers
!=
None
:
if
self
.
distill_config
.
mapping_layers
!=
None
:
dis_loss
=
ofa_model
.
calc_distill_loss
()
dis_loss
=
ofa_model
.
calc_distill_loss
()
...
...
tests/test_ofa_v2.py
浏览文件 @
ae917097
...
@@ -297,34 +297,5 @@ class TestInputDict(unittest.TestCase):
...
@@ -297,34 +297,5 @@ class TestInputDict(unittest.TestCase):
input_dtypes
=
[
'float32'
,
'float32'
])
input_dtypes
=
[
'float32'
,
'float32'
])
class
TestInputDict
(
unittest
.
TestCase
):
def
setUp
(
self
):
model
=
ModelInputDict
()
sp_net_config
=
supernet
(
expand_ratio
=
[
0.5
,
1.0
])
self
.
model
=
Convert
(
sp_net_config
).
convert
(
model
)
self
.
images
=
paddle
.
randn
(
shape
=
[
2
,
3
,
32
,
32
],
dtype
=
'float32'
)
self
.
images2
=
{
'data'
:
paddle
.
randn
(
shape
=
[
2
,
12
,
32
,
32
],
dtype
=
'float32'
)
}
default_run_config
=
{
'skip_layers'
:
[
'conv1.0'
,
'conv2.0'
]}
self
.
run_config
=
RunConfig
(
**
default_run_config
)
self
.
ofa_model
=
OFA
(
self
.
model
,
run_config
=
self
.
run_config
)
self
.
ofa_model
.
_clear_search_space
(
self
.
images
,
data
=
self
.
images2
)
def
test_export
(
self
):
config
=
self
.
ofa_model
.
_sample_config
(
task
=
"expand_ratio"
,
sample_type
=
"smallest"
)
self
.
ofa_model
.
export
(
config
,
input_shapes
=
[[
1
,
3
,
32
,
32
],
{
'data'
:
[
1
,
12
,
32
,
32
]
}],
input_dtypes
=
[
'float32'
,
'float32'
])
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录