Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
044909e7
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看板
未验证
提交
044909e7
编写于
12月 02, 2022
作者:
W
whs
提交者:
GitHub
12月 02, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove fluid API (#1567)
上级
87cfc2e6
变更
44
隐藏空白更改
内联
并排
Showing
44 changed file
with
179 addition
and
265 deletion
+179
-265
ce_tests/dygraph/quant/src/save_quant_model.py
ce_tests/dygraph/quant/src/save_quant_model.py
+1
-1
demo/darts/model.py
demo/darts/model.py
+5
-5
demo/darts/model_search.py
demo/darts/model_search.py
+13
-11
demo/darts/operations.py
demo/darts/operations.py
+2
-2
demo/darts/search.py
demo/darts/search.py
+2
-1
demo/darts/train.py
demo/darts/train.py
+6
-6
demo/darts/train_imagenet.py
demo/darts/train_imagenet.py
+8
-8
demo/distillation/distill.py
demo/distillation/distill.py
+4
-4
demo/mkldnn_quant/sample_tester.py
demo/mkldnn_quant/sample_tester.py
+2
-2
demo/models/mobilenet.py
demo/models/mobilenet.py
+2
-8
demo/models/mobilenet_v2.py
demo/models/mobilenet_v2.py
+2
-8
demo/models/mobilenet_v3.py
demo/models/mobilenet_v3.py
+9
-14
demo/models/pvanet.py
demo/models/pvanet.py
+18
-19
demo/models/resnet.py
demo/models/resnet.py
+5
-3
demo/models/resnet_vd.py
demo/models/resnet_vd.py
+3
-3
demo/models/slimfacenet.py
demo/models/slimfacenet.py
+18
-23
demo/ofa/ernie/ernie_supernet/modeling_ernie_supernet.py
demo/ofa/ernie/ernie_supernet/modeling_ernie_supernet.py
+8
-7
demo/ofa/ernie/ofa_ernie.py
demo/ofa/ernie/ofa_ernie.py
+1
-1
demo/optimizer.py
demo/optimizer.py
+1
-1
demo/quant/quant_embedding/cluster_train.py
demo/quant/quant_embedding/cluster_train.py
+1
-1
demo/quant/quant_embedding/net.py
demo/quant/quant_embedding/net.py
+1
-1
demo/quant/quant_embedding/train.py
demo/quant/quant_embedding/train.py
+1
-1
demo/slimfacenet/train_eval.py
demo/slimfacenet/train_eval.py
+7
-6
paddleslim/common/recover_program.py
paddleslim/common/recover_program.py
+1
-1
paddleslim/common/rl_controller/lstm/lstm_controller.py
paddleslim/common/rl_controller/lstm/lstm_controller.py
+1
-1
paddleslim/dygraph/prune/pruning_plan.py
paddleslim/dygraph/prune/pruning_plan.py
+8
-5
paddleslim/nas/darts/architect.py
paddleslim/nas/darts/architect.py
+1
-2
paddleslim/nas/ofa/ofa.py
paddleslim/nas/ofa/ofa.py
+2
-1
paddleslim/nas/ofa/utils/utils.py
paddleslim/nas/ofa/utils/utils.py
+4
-16
paddleslim/quant/analysis_qat.py
paddleslim/quant/analysis_qat.py
+1
-1
paddleslim/quant/quant_aware_with_infermodel.py
paddleslim/quant/quant_aware_with_infermodel.py
+1
-1
paddleslim/quant/quant_embedding.py
paddleslim/quant/quant_embedding.py
+12
-12
paddleslim/quant/quanter.py
paddleslim/quant/quanter.py
+5
-9
tests/dygraph/test_prune.py
tests/dygraph/test_prune.py
+12
-4
tests/dygraph/test_sensitivity.py
tests/dygraph/test_sensitivity.py
+1
-1
tests/test_autoprune.py
tests/test_autoprune.py
+1
-1
tests/test_fpgm_prune.py
tests/test_fpgm_prune.py
+1
-1
tests/test_fsp_loss.py
tests/test_fsp_loss.py
+0
-65
tests/test_optimal_threshold.py
tests/test_optimal_threshold.py
+1
-1
tests/test_prune.py
tests/test_prune.py
+1
-1
tests/test_prune_op.py
tests/test_prune_op.py
+3
-3
tests/test_prune_walker.py
tests/test_prune_walker.py
+1
-1
tests/test_quant_aware.py
tests/test_quant_aware.py
+1
-1
tests/test_slim_prune.py
tests/test_slim_prune.py
+1
-1
未找到文件。
ce_tests/dygraph/quant/src/save_quant_model.py
浏览文件 @
044909e7
...
...
@@ -24,7 +24,7 @@ import time
import
paddle
from
paddle.fluid.framework
import
IrGraph
from
paddle.fluid.contrib.slim.quantization
import
Quant2Int8MkldnnPass
from
paddle.f
luid
import
core
from
paddle.f
ramework
import
core
paddle
.
enable_static
()
...
...
demo/darts/model.py
浏览文件 @
044909e7
...
...
@@ -74,7 +74,7 @@ class Classifier(paddle.nn.Layer):
def
forward
(
self
,
x
):
x
=
self
.
pool2d
(
x
)
x
=
fluid
.
layers
.
squeeze
(
x
,
axes
=
[
2
,
3
])
x
=
paddle
.
squeeze
(
x
,
axes
=
[
2
,
3
])
out
=
self
.
fc
(
x
)
return
out
...
...
@@ -85,7 +85,7 @@ def drop_path(x, drop_prob):
mask
=
1
-
np
.
random
.
binomial
(
1
,
drop_prob
,
size
=
[
x
.
shape
[
0
]]).
astype
(
np
.
float32
)
mask
=
to_variable
(
mask
)
x
=
fluid
.
layers
.
elementwise_mul
(
x
/
keep_prob
,
mask
,
axis
=
0
)
x
=
paddle
.
multiply
(
x
/
keep_prob
,
mask
)
return
x
...
...
@@ -122,7 +122,7 @@ class Cell(paddle.nn.Layer):
op
=
OPS
[
op_name
](
c_curr
,
stride
,
True
)
ops
+=
[
op
]
edge_index
+=
1
self
.
_ops
=
fluid
.
dygraph
.
LayerList
(
ops
)
self
.
_ops
=
paddle
.
nn
.
LayerList
(
ops
)
self
.
_indices
=
indices
def
forward
(
self
,
s0
,
s1
,
drop_prob
,
training
):
...
...
@@ -206,7 +206,7 @@ class NetworkCIFAR(paddle.nn.Layer):
c_prev_prev
,
c_prev
=
c_prev
,
cell
.
_multiplier
*
c_curr
if
i
==
2
*
layers
//
3
:
c_to_auxiliary
=
c_prev
self
.
cells
=
fluid
.
dygraph
.
LayerList
(
cells
)
self
.
cells
=
paddle
.
nn
.
LayerList
(
cells
)
if
auxiliary
:
self
.
auxiliary_head
=
AuxiliaryHeadCIFAR
(
c_to_auxiliary
,
...
...
@@ -288,7 +288,7 @@ class NetworkImageNet(paddle.nn.Layer):
c_prev_prev
,
c_prev
=
c_prev
,
cell
.
_multiplier
*
c_curr
if
i
==
2
*
layers
//
3
:
c_to_auxiliary
=
c_prev
self
.
cells
=
fluid
.
dygraph
.
LayerList
(
cells
)
self
.
cells
=
paddle
.
nn
.
LayerList
(
cells
)
if
auxiliary
:
self
.
auxiliary_head
=
AuxiliaryHeadImageNet
(
c_to_auxiliary
,
...
...
demo/darts/model_search.py
浏览文件 @
044909e7
...
...
@@ -16,12 +16,14 @@ from __future__ import absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
paddle
import
paddle.fluid
as
fluid
from
paddle.nn.initializer
import
Normal
,
KaimingUniform
,
Constant
from
paddle.
fluid.dygraph.
nn
import
Conv2D
,
Pool2D
,
BatchNorm
,
Linear
from
paddle.nn
import
Conv2D
,
Pool2D
,
BatchNorm
,
Linear
from
paddle.fluid.dygraph.base
import
to_variable
from
genotypes
import
PRIMITIVES
from
operations
import
*
import
paddleslim
def
channel_shuffle
(
x
,
groups
):
...
...
@@ -31,7 +33,7 @@ def channel_shuffle(x, groups):
# reshape
x
=
paddle
.
reshape
(
x
,
[
batchsize
,
groups
,
channels_per_group
,
height
,
width
])
x
=
fluid
.
layers
.
transpose
(
x
,
[
0
,
2
,
1
,
3
,
4
])
x
=
paddle
.
transpose
(
x
,
[
0
,
2
,
1
,
3
,
4
])
# flatten
x
=
paddle
.
reshape
(
x
,
[
batchsize
,
num_channels
,
height
,
width
])
...
...
@@ -59,12 +61,12 @@ class MixedOp(paddle.nn.Layer):
trainable
=
False
)
BN
=
BatchNorm
(
c_cur
//
self
.
_k
,
param_attr
=
gama
,
bias_attr
=
beta
)
op
=
fluid
.
dygraph
.
Sequential
(
op
,
BN
)
op
=
paddle
.
nn
.
Sequential
(
op
,
BN
)
ops
.
append
(
op
)
self
.
_ops
=
fluid
.
dygraph
.
LayerList
(
ops
)
self
.
_ops
=
paddle
.
nn
.
LayerList
(
ops
)
def
forward
(
self
,
x
,
weights
):
return
fluid
.
layers
.
sums
(
return
paddle
.
add_n
(
[
weights
[
i
]
*
op
(
x
)
for
i
,
op
in
enumerate
(
self
.
_ops
)])
...
...
@@ -89,7 +91,7 @@ class Cell(paddle.nn.Layer):
stride
=
2
if
reduction
and
j
<
2
else
1
op
=
MixedOp
(
c_cur
,
stride
,
method
)
ops
.
append
(
op
)
self
.
_ops
=
fluid
.
dygraph
.
LayerList
(
ops
)
self
.
_ops
=
paddle
.
nn
.
LayerList
(
ops
)
def
forward
(
self
,
s0
,
s1
,
weights
,
weights2
=
None
):
s0
=
self
.
preprocess0
(
s0
)
...
...
@@ -98,7 +100,7 @@ class Cell(paddle.nn.Layer):
states
=
[
s0
,
s1
]
offset
=
0
for
i
in
range
(
self
.
_steps
):
s
=
fluid
.
layers
.
sums
([
s
=
paddle
.
add_n
([
self
.
_ops
[
offset
+
j
](
h
,
weights
[
offset
+
j
])
for
j
,
h
in
enumerate
(
states
)
])
...
...
@@ -127,7 +129,7 @@ class Network(paddle.nn.Layer):
self
.
_method
=
method
c_cur
=
stem_multiplier
*
c_in
self
.
stem
=
fluid
.
dygraph
.
Sequential
(
self
.
stem
=
paddle
.
nn
.
Sequential
(
Conv2D
(
num_channels
=
3
,
num_filters
=
c_cur
,
...
...
@@ -154,7 +156,7 @@ class Network(paddle.nn.Layer):
reduction_prev
=
reduction
cells
.
append
(
cell
)
c_prev_prev
,
c_prev
=
c_prev
,
multiplier
*
c_cur
self
.
cells
=
fluid
.
dygraph
.
LayerList
(
cells
)
self
.
cells
=
paddle
.
nn
.
LayerList
(
cells
)
self
.
global_pooling
=
Pool2D
(
pool_type
=
'avg'
,
global_pooling
=
True
)
self
.
classifier
=
Linear
(
input_dim
=
c_prev
,
...
...
@@ -174,13 +176,13 @@ class Network(paddle.nn.Layer):
weights
=
paddle
.
nn
.
functional
.
softmax
(
self
.
alphas_normal
)
s0
,
s1
=
s1
,
cell
(
s0
,
s1
,
weights
,
weights2
)
out
=
self
.
global_pooling
(
s1
)
out
=
fluid
.
layers
.
squeeze
(
out
,
axes
=
[
2
,
3
])
out
=
paddle
.
squeeze
(
out
,
axes
=
[
2
,
3
])
logits
=
self
.
classifier
(
out
)
return
logits
def
_loss
(
self
,
input
,
target
):
logits
=
self
(
input
)
loss
=
fluid
.
layers
.
reduce_
mean
(
loss
=
paddle
.
mean
(
paddle
.
nn
.
functional
.
softmax_with_cross_entropy
(
logits
,
target
))
return
loss
...
...
demo/darts/operations.py
浏览文件 @
044909e7
...
...
@@ -71,8 +71,8 @@ class Zero(paddle.nn.Layer):
def
forward
(
self
,
x
):
pooled
=
self
.
pool
(
x
)
x
=
fluid
.
layers
.
zeros_like
(
x
)
if
self
.
stride
==
1
else
fluid
.
layers
.
zeros_like
(
pooled
)
x
=
paddle
.
zeros_like
(
x
)
if
self
.
stride
==
1
else
paddle
.
zeros_like
(
pooled
)
return
x
...
...
demo/darts/search.py
浏览文件 @
044909e7
...
...
@@ -63,7 +63,8 @@ def main(args):
elif
not
args
.
use_data_parallel
:
place
=
paddle
.
CUDAPlace
(
0
)
else
:
place
=
paddle
.
CUDAPlace
(
fluid
.
dygraph
.
parallel
.
Env
().
dev_id
)
place
=
paddle
.
CUDAPlace
(
paddle
.
distributed
.
parallel
.
ParallelEnv
()
.
dev_id
)
train_reader
,
valid_reader
=
reader
.
train_search
(
batch_size
=
args
.
batch_size
,
...
...
demo/darts/train.py
浏览文件 @
044909e7
...
...
@@ -79,10 +79,10 @@ def train(model, train_reader, optimizer, epoch, drop_path_prob, args):
prec1
=
paddle
.
static
.
accuracy
(
input
=
logits
,
label
=
label
,
k
=
1
)
prec5
=
paddle
.
static
.
accuracy
(
input
=
logits
,
label
=
label
,
k
=
5
)
loss
=
fluid
.
layers
.
reduce_
mean
(
loss
=
paddle
.
mean
(
paddle
.
nn
.
functional
.
softmax_with_cross_entropy
(
logits
,
label
))
if
args
.
auxiliary
:
loss_aux
=
fluid
.
layers
.
reduce_
mean
(
loss_aux
=
paddle
.
mean
(
paddle
.
nn
.
functional
.
softmax_with_cross_entropy
(
logits_aux
,
label
))
loss
=
loss
+
args
.
auxiliary_weight
*
loss_aux
...
...
@@ -122,7 +122,7 @@ def valid(model, valid_reader, epoch, args):
logits
,
_
=
model
(
image
,
0
,
False
)
prec1
=
paddle
.
static
.
accuracy
(
input
=
logits
,
label
=
label
,
k
=
1
)
prec5
=
paddle
.
static
.
accuracy
(
input
=
logits
,
label
=
label
,
k
=
5
)
loss
=
fluid
.
layers
.
reduce_
mean
(
loss
=
paddle
.
mean
(
paddle
.
nn
.
functional
.
softmax_with_cross_entropy
(
logits
,
label
))
n
=
image
.
shape
[
0
]
...
...
@@ -137,7 +137,7 @@ def valid(model, valid_reader, epoch, args):
def
main
(
args
):
place
=
paddle
.
CUDAPlace
(
fluid
.
dygraph
.
parallel
.
Env
().
dev_id
)
\
place
=
paddle
.
CUDAPlace
(
paddle
.
distributed
.
parallel
.
Parallel
Env
().
dev_id
)
\
if
args
.
use_data_parallel
else
paddle
.
CUDAPlace
(
0
)
with
fluid
.
dygraph
.
guard
(
place
):
...
...
@@ -152,7 +152,7 @@ def main(args):
logger
.
info
(
"param size = {:.6f}MB"
.
format
(
count_parameters_in_MB
(
model
.
parameters
())))
device_num
=
fluid
.
dygraph
.
parallel
.
Env
().
nranks
device_num
=
paddle
.
distributed
.
parallel
.
Parallel
Env
().
nranks
step_per_epoch
=
int
(
args
.
trainset_num
/
(
args
.
batch_size
*
device_num
))
learning_rate
=
fluid
.
dygraph
.
CosineDecay
(
args
.
learning_rate
,
step_per_epoch
,
args
.
epochs
)
...
...
@@ -200,7 +200,7 @@ def main(args):
save_parameters
=
(
not
args
.
use_data_parallel
)
or
(
args
.
use_data_parallel
and
fluid
.
dygraph
.
parallel
.
Env
().
local_rank
==
0
)
paddle
.
distributed
.
parallel
.
Parallel
Env
().
local_rank
==
0
)
best_acc
=
0
for
epoch
in
range
(
args
.
epochs
):
drop_path_prob
=
args
.
drop_path_prob
*
epoch
/
args
.
epochs
...
...
demo/darts/train_imagenet.py
浏览文件 @
044909e7
...
...
@@ -69,9 +69,9 @@ add_arg('use_data_parallel', ast.literal_eval, False, "The flag indicating whet
def
cross_entropy_label_smooth
(
preds
,
targets
,
epsilon
):
preds
=
paddle
.
nn
.
functional
.
softmax
(
preds
)
targets_one_hot
=
fluid
.
one_hot
(
input
=
targets
,
depth
=
args
.
class_num
)
targets_smooth
=
fluid
.
layers
.
label_smooth
(
targets_smooth
=
paddle
.
nn
.
functional
.
label_smooth
(
targets_one_hot
,
epsilon
=
epsilon
,
dtype
=
"float32"
)
loss
=
fluid
.
layers
.
cross_entropy
(
loss
=
paddle
.
nn
.
functional
.
cross_entropy
(
input
=
preds
,
label
=
targets_smooth
,
soft_label
=
True
)
return
loss
...
...
@@ -91,11 +91,11 @@ def train(model, train_reader, optimizer, epoch, args):
prec1
=
paddle
.
static
.
accuracy
(
input
=
logits
,
label
=
label
,
k
=
1
)
prec5
=
paddle
.
static
.
accuracy
(
input
=
logits
,
label
=
label
,
k
=
5
)
loss
=
fluid
.
layers
.
reduce_
mean
(
loss
=
paddle
.
mean
(
cross_entropy_label_smooth
(
logits
,
label
,
args
.
label_smooth
))
if
args
.
auxiliary
:
loss_aux
=
fluid
.
layers
.
reduce_
mean
(
loss_aux
=
paddle
.
mean
(
cross_entropy_label_smooth
(
logits_aux
,
label
,
args
.
label_smooth
))
loss
=
loss
+
args
.
auxiliary_weight
*
loss_aux
...
...
@@ -135,7 +135,7 @@ def valid(model, valid_reader, epoch, args):
logits
,
_
=
model
(
image
,
False
)
prec1
=
paddle
.
static
.
accuracy
(
input
=
logits
,
label
=
label
,
k
=
1
)
prec5
=
paddle
.
static
.
accuracy
(
input
=
logits
,
label
=
label
,
k
=
5
)
loss
=
fluid
.
layers
.
reduce_
mean
(
loss
=
paddle
.
mean
(
cross_entropy_label_smooth
(
logits
,
label
,
args
.
label_smooth
))
n
=
image
.
shape
[
0
]
...
...
@@ -150,7 +150,7 @@ def valid(model, valid_reader, epoch, args):
def
main
(
args
):
place
=
paddle
.
CUDAPlace
(
fluid
.
dygraph
.
parallel
.
Env
().
dev_id
)
\
place
=
paddle
.
CUDAPlace
(
paddle
.
distributed
.
parallel
.
Parallel
Env
().
dev_id
)
\
if
args
.
use_data_parallel
else
paddle
.
CUDAPlace
(
0
)
with
fluid
.
dygraph
.
guard
(
place
):
...
...
@@ -165,7 +165,7 @@ def main(args):
logger
.
info
(
"param size = {:.6f}MB"
.
format
(
count_parameters_in_MB
(
model
.
parameters
())))
device_num
=
fluid
.
dygraph
.
parallel
.
Env
().
nranks
device_num
=
paddle
.
distributed
.
parallel
.
Parallel
Env
().
nranks
step_per_epoch
=
int
(
args
.
trainset_num
/
(
args
.
batch_size
*
device_num
))
learning_rate
=
fluid
.
dygraph
.
ExponentialDecay
(
args
.
learning_rate
,
step_per_epoch
,
args
.
decay_rate
,
staircase
=
True
)
...
...
@@ -209,7 +209,7 @@ def main(args):
save_parameters
=
(
not
args
.
use_data_parallel
)
or
(
args
.
use_data_parallel
and
fluid
.
dygraph
.
parallel
.
Env
().
local_rank
==
0
)
paddle
.
distributed
.
parallel
.
Parallel
Env
().
local_rank
==
0
)
best_top1
=
0
for
epoch
in
range
(
args
.
epochs
):
logger
.
info
(
'Epoch {}, lr {:.6f}'
.
format
(
epoch
,
optimizer
.
get_lr
()))
...
...
demo/distillation/distill.py
浏览文件 @
044909e7
...
...
@@ -13,7 +13,7 @@ import numpy as np
sys
.
path
[
0
]
=
os
.
path
.
join
(
os
.
path
.
dirname
(
"__file__"
),
os
.
path
.
pardir
)
import
models
from
utility
import
add_arguments
,
print_arguments
,
_download
,
_decompress
from
paddleslim.dist
import
merge
,
l2
,
soft_label
,
fsp
from
paddleslim.dist
import
merge
,
l2
,
soft_label
logging
.
basicConfig
(
format
=
'%(asctime)s-%(levelname)s: %(message)s'
)
_logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -99,11 +99,11 @@ def compress(args):
)
if
args
.
use_gpu
else
paddle
.
static
.
cpu_places
()
place
=
places
[
0
]
if
args
.
use_gpu
:
devices_num
=
paddle
.
f
luid
.
core
.
get_cuda_device_count
()
devices_num
=
paddle
.
f
ramework
.
core
.
get_cuda_device_count
()
else
:
devices_num
=
int
(
os
.
environ
.
get
(
'CPU_NUM'
,
1
))
with
paddle
.
static
.
program_guard
(
student_program
,
s_startup
):
with
paddle
.
fluid
.
unique_name
.
guard
():
with
paddle
.
utils
.
unique_name
.
guard
():
image
=
paddle
.
static
.
data
(
name
=
'image'
,
shape
=
[
None
]
+
image_shape
,
dtype
=
'float32'
)
label
=
paddle
.
static
.
data
(
...
...
@@ -144,7 +144,7 @@ def compress(args):
teacher_program
=
paddle
.
static
.
Program
()
t_startup
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
teacher_program
,
t_startup
):
with
paddle
.
fluid
.
unique_name
.
guard
():
with
paddle
.
utils
.
unique_name
.
guard
():
image
=
paddle
.
static
.
data
(
name
=
'image'
,
shape
=
[
None
]
+
image_shape
,
dtype
=
'float32'
)
predict
=
teacher_model
.
net
(
image
,
class_dim
=
class_dim
)
...
...
demo/mkldnn_quant/sample_tester.py
浏览文件 @
044909e7
...
...
@@ -24,7 +24,7 @@ import time
import
paddle
import
paddle.fluid
as
fluid
from
paddle.fluid.framework
import
IrGraph
from
paddle.f
luid
import
core
from
paddle.f
ramework
import
core
logging
.
basicConfig
(
format
=
'%(asctime)s-%(levelname)s: %(message)s'
)
_logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -244,7 +244,7 @@ class SampleTester(unittest.TestCase):
return
outputs
,
acc1_avg
,
acc5_avg
,
fps_avg
,
latency_avg
def
test_graph_transformation
(
self
):
if
not
fluid
.
core
.
is_compiled_with_mkldnn
():
if
not
paddle
.
fluid
.
core
.
is_compiled_with_mkldnn
():
return
infer_model_path
=
test_case_args
.
infer_model
...
...
demo/models/mobilenet.py
浏览文件 @
044909e7
...
...
@@ -2,7 +2,6 @@ from __future__ import absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
paddle
import
paddle.fluid
as
fluid
from
paddle.nn.initializer
import
KaimingUniform
__all__
=
[
'MobileNet'
]
...
...
@@ -121,13 +120,8 @@ class MobileNet():
scale
=
scale
,
name
=
"conv6"
)
input
=
fluid
.
layers
.
pool2d
(
input
=
input
,
pool_size
=
0
,
pool_stride
=
1
,
pool_type
=
'avg'
,
global_pooling
=
True
)
with
fluid
.
name_scope
(
'last_fc'
):
input
=
paddle
.
nn
.
functional
.
adaptive_avg_pool2d
(
input
,
1
)
with
paddle
.
static
.
name_scope
(
'last_fc'
):
output
=
paddle
.
static
.
nn
.
fc
(
input
,
class_dim
,
...
...
demo/models/mobilenet_v2.py
浏览文件 @
044909e7
...
...
@@ -16,7 +16,6 @@ from __future__ import absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
paddle
import
paddle.fluid
as
fluid
from
paddle.nn.initializer
import
KaimingUniform
__all__
=
[
...
...
@@ -101,12 +100,7 @@ class MobileNetV2():
if_act
=
True
,
name
=
'conv9'
)
input
=
fluid
.
layers
.
pool2d
(
input
=
input
,
pool_size
=
7
,
pool_stride
=
1
,
pool_type
=
'avg'
,
global_pooling
=
True
)
input
=
paddle
.
nn
.
functional
.
adaptive_avg_pool2d
(
input
,
1
)
output
=
paddle
.
static
.
nn
.
fc
(
input
,
...
...
@@ -150,7 +144,7 @@ class MobileNetV2():
return
bn
def
shortcut
(
self
,
input
,
data_residual
):
return
fluid
.
layers
.
elementwise_
add
(
input
,
data_residual
)
return
paddle
.
add
(
input
,
data_residual
)
def
inverted_residual_unit
(
self
,
input
,
...
...
demo/models/mobilenet_v3.py
浏览文件 @
044909e7
import
paddle
import
paddle.fluid
as
fluid
from
paddle.nn.initializer
import
KaimingUniform
import
math
...
...
@@ -103,8 +102,7 @@ class MobileNetV3():
if_act
=
True
,
act
=
'hard_swish'
,
name
=
'conv_last'
)
conv
=
fluid
.
layers
.
pool2d
(
input
=
conv
,
pool_type
=
'avg'
,
global_pooling
=
True
,
use_cudnn
=
False
)
conv
=
paddle
.
nn
.
functional
.
adaptive_avg_pool2d
(
conv
,
1
)
conv
=
paddle
.
static
.
nn
.
conv2d
(
input
=
conv
,
num_filters
=
cls_ch_expand
,
...
...
@@ -114,7 +112,7 @@ class MobileNetV3():
act
=
None
,
param_attr
=
paddle
.
ParamAttr
(
name
=
'last_1x1_conv_weights'
),
bias_attr
=
False
)
conv
=
fluid
.
layers
.
hard_
swish
(
conv
)
conv
=
paddle
.
nn
.
functional
.
hard
swish
(
conv
)
out
=
paddle
.
static
.
nn
.
fc
(
conv
,
class_dim
,
...
...
@@ -149,19 +147,17 @@ class MobileNetV3():
input
=
conv
,
param_attr
=
paddle
.
ParamAttr
(
name
=
bn_name
+
"_scale"
,
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
regularizer
=
paddle
.
regularizer
.
L2Decay
(
coeff
=
0.0
)),
bias_attr
=
paddle
.
ParamAttr
(
name
=
bn_name
+
"_offset"
,
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
regularizer
=
paddle
.
regularizer
.
L2Decay
(
coeff
=
0.0
)),
moving_mean_name
=
bn_name
+
'_mean'
,
moving_variance_name
=
bn_name
+
'_variance'
)
if
if_act
:
if
act
==
'relu'
:
bn
=
paddle
.
nn
.
functional
.
relu
(
bn
)
elif
act
==
'hard_swish'
:
bn
=
fluid
.
layers
.
hard_
swish
(
bn
)
bn
=
paddle
.
nn
.
functional
.
hard
swish
(
bn
)
return
bn
def
hard_swish
(
self
,
x
):
...
...
@@ -169,8 +165,7 @@ class MobileNetV3():
def
se_block
(
self
,
input
,
num_out_filter
,
ratio
=
4
,
name
=
None
):
num_mid_filter
=
int
(
num_out_filter
//
ratio
)
pool
=
fluid
.
layers
.
pool2d
(
input
=
input
,
pool_type
=
'avg'
,
global_pooling
=
True
,
use_cudnn
=
False
)
pool
=
paddle
.
nn
.
functional
.
adaptive_avg_pool2d
(
input
,
1
)
conv1
=
paddle
.
static
.
nn
.
conv2d
(
input
=
pool
,
filter_size
=
1
,
...
...
@@ -186,7 +181,7 @@ class MobileNetV3():
param_attr
=
paddle
.
ParamAttr
(
name
=
name
+
'_2_weights'
),
bias_attr
=
paddle
.
ParamAttr
(
name
=
name
+
'_2_offset'
))
scale
=
fluid
.
layers
.
elementwise_mul
(
x
=
input
,
y
=
conv2
,
axis
=
0
)
scale
=
paddle
.
multiply
(
x
=
input
,
y
=
conv2
)
return
scale
def
residual_unit
(
self
,
...
...
@@ -224,7 +219,7 @@ class MobileNetV3():
name
=
name
+
'_depthwise'
)
if
use_se
:
with
fluid
.
name_scope
(
'se_block_skip'
):
with
paddle
.
static
.
name_scope
(
'se_block_skip'
):
conv1
=
self
.
se_block
(
input
=
conv1
,
num_out_filter
=
num_mid_filter
,
...
...
@@ -241,7 +236,7 @@ class MobileNetV3():
if
num_in_filter
!=
num_out_filter
or
stride
!=
1
:
return
conv2
else
:
return
fluid
.
layers
.
elementwise_add
(
x
=
input_data
,
y
=
conv2
,
act
=
None
)
return
paddle
.
add
(
x
=
input_data
,
y
=
conv2
)
def
MobileNetV3_small_x0_25
():
...
...
demo/models/pvanet.py
浏览文件 @
044909e7
...
...
@@ -227,10 +227,8 @@ class PVANet():
is_bias
=
True
,
default_initializer
=
paddle
.
nn
.
initializer
.
Constant
(
value
=
0.0
))
output
=
fluid
.
layers
.
elementwise_mul
(
input
,
scale_param
,
axis
=
axis
,
name
=
prefix
+
'mul'
)
output
=
fluid
.
layers
.
elementwise_add
(
output
,
offset_param
,
axis
=
axis
,
name
=
prefix
+
'add'
)
output
=
paddle
.
multiply
(
input
,
scale_param
,
name
=
prefix
+
'mul'
)
output
=
paddle
.
add
(
output
,
offset_param
,
name
=
prefix
+
'add'
)
return
output
def
_conv
(
self
,
...
...
@@ -293,7 +291,7 @@ class PVANet():
def
_bn_crelu
(
self
,
input
,
name
):
net
=
self
.
_bn
(
input
,
None
,
name
+
'_bn_1'
)
neg_net
=
fluid
.
layers
.
scale
(
net
,
scale
=-
1.0
,
name
=
name
+
'_neg'
)
neg_net
=
paddle
.
scale
(
net
,
scale
=-
1.0
,
name
=
name
+
'_neg'
)
net
=
paddle
.
concat
([
net
,
neg_net
],
axis
=
1
)
net
=
self
.
_scale
(
net
,
name
+
'_scale'
)
net
=
paddle
.
nn
.
functional
.
relu
(
net
,
name
=
name
+
'_relu'
)
...
...
@@ -369,13 +367,13 @@ def Fpn_Fusion(blocks, net):
if
i
==
0
:
g
[
i
]
=
net
.
deconv_bn_layer
(
h
[
i
],
num_outputs
[
i
],
name
=
'fpn_0'
)
else
:
out
=
fluid
.
layers
.
elementwise_
add
(
x
=
g
[
i
-
1
],
y
=
h
[
i
])
out
=
paddle
.
add
(
x
=
g
[
i
-
1
],
y
=
h
[
i
])
out
=
net
.
conv_bn_layer
(
out
,
num_outputs
[
i
],
1
,
'fpn_trans_'
+
str
(
i
))
g
[
i
]
=
net
.
deconv_bn_layer
(
out
,
num_outputs
[
i
],
name
=
'fpn_'
+
str
(
i
))
out
=
fluid
.
layers
.
elementwise_
add
(
x
=
g
[
-
2
],
y
=
h
[
-
1
])
out
=
paddle
.
add
(
x
=
g
[
-
2
],
y
=
h
[
-
1
])
out
=
net
.
conv_bn_layer
(
out
,
num_outputs
[
-
1
],
1
,
'fpn_post_0'
)
out
=
net
.
conv_bn_layer
(
out
,
num_outputs
[
-
1
],
3
,
'fpn_post_1'
)
...
...
@@ -409,7 +407,7 @@ def Detector_Header(f_common, net, class_num):
bias_attr
=
paddle
.
ParamAttr
(
name
=
name
+
'_conv_bias'
),
name
=
name
+
'_conv'
)
f_score
=
fluid
.
layers
.
transpose
(
f_score
,
perm
=
[
0
,
2
,
3
,
1
])
f_score
=
paddle
.
transpose
(
f_score
,
perm
=
[
0
,
2
,
3
,
1
])
f_score
=
paddle
.
reshape
(
f_score
,
shape
=
[
-
1
,
class_num
+
1
])
f_score
=
paddle
.
nn
.
functional
.
softmax
(
input
=
f_score
)
...
...
@@ -448,7 +446,7 @@ def inference(input, class_num=1, nms_thresh=0.2, score_thresh=0.5):
f_score
,
f_geo
=
east
(
input
,
class_num
)
print
(
"f_geo shape={}"
.
format
(
f_geo
.
shape
))
print
(
"f_score shape={}"
.
format
(
f_score
.
shape
))
f_score
=
fluid
.
layers
.
transpose
(
f_score
,
perm
=
[
1
,
0
])
f_score
=
paddle
.
transpose
(
f_score
,
perm
=
[
1
,
0
])
return
f_score
,
f_geo
...
...
@@ -460,20 +458,20 @@ def loss(f_score, f_geo, l_score, l_geo, l_mask, class_num=1):
'''
#smooth_l1_loss
channels
=
8
l_geo_split
,
l_short_edge
=
fluid
.
layers
.
split
(
l_geo_split
,
l_short_edge
=
paddle
.
split
(
l_geo
,
num_or_sections
=
[
channels
,
1
],
dim
=
1
)
#last channel is short_edge_norm
f_geo_split
=
fluid
.
layers
.
split
(
f_geo
,
num_or_sections
=
[
channels
],
dim
=
1
)
f_geo_split
=
paddle
.
split
(
f_geo
,
num_or_sections
=
[
channels
],
dim
=
1
)
f_geo_split
=
f_geo_split
[
0
]
geo_diff
=
l_geo_split
-
f_geo_split
abs_geo_diff
=
fluid
.
layers
.
abs
(
geo_diff
)
abs_geo_diff
=
paddle
.
abs
(
geo_diff
)
l_flag
=
l_score
>=
1
l_flag
=
fluid
.
layers
.
cast
(
x
=
l_flag
,
dtype
=
"float32"
)
l_flag
=
paddle
.
cast
(
x
=
l_flag
,
dtype
=
"float32"
)
l_flag
=
fluid
.
layers
.
expand
(
x
=
l_flag
,
expand_times
=
[
1
,
channels
,
1
,
1
])
smooth_l1_sign
=
abs_geo_diff
<
l_flag
smooth_l1_sign
=
fluid
.
layers
.
cast
(
x
=
smooth_l1_sign
,
dtype
=
"float32"
)
smooth_l1_sign
=
paddle
.
cast
(
x
=
smooth_l1_sign
,
dtype
=
"float32"
)
in_loss
=
abs_geo_diff
*
abs_geo_diff
*
smooth_l1_sign
+
(
abs_geo_diff
-
0.5
)
*
(
1.0
-
smooth_l1_sign
)
...
...
@@ -481,18 +479,19 @@ def loss(f_score, f_geo, l_score, l_geo, l_mask, class_num=1):
x
=
l_short_edge
,
expand_times
=
[
1
,
channels
,
1
,
1
])
out_loss
=
l_short_edge
*
in_loss
*
l_flag
out_loss
=
out_loss
*
l_flag
smooth_l1_loss
=
fluid
.
layers
.
reduce_
mean
(
out_loss
)
smooth_l1_loss
=
paddle
.
mean
(
out_loss
)
##softmax_loss
l_score
.
stop_gradient
=
True
l_score
=
fluid
.
layers
.
transpose
(
l_score
,
perm
=
[
0
,
2
,
3
,
1
])
l_score
=
paddle
.
transpose
(
l_score
,
perm
=
[
0
,
2
,
3
,
1
])
l_score
.
stop_gradient
=
True
l_score
=
paddle
.
reshape
(
l_score
,
shape
=
[
-
1
,
1
])
l_score
.
stop_gradient
=
True
l_score
=
fluid
.
layers
.
cast
(
x
=
l_score
,
dtype
=
"int64"
)
l_score
=
paddle
.
cast
(
x
=
l_score
,
dtype
=
"int64"
)
l_score
.
stop_gradient
=
True
softmax_loss
=
fluid
.
layers
.
cross_entropy
(
input
=
f_score
,
label
=
l_score
)
softmax_loss
=
fluid
.
layers
.
reduce_mean
(
softmax_loss
)
softmax_loss
=
paddle
.
nn
.
functional
.
cross_entropy
(
input
=
f_score
,
label
=
l_score
)
softmax_loss
=
paddle
.
mean
(
softmax_loss
)
return
softmax_loss
,
smooth_l1_loss
demo/models/resnet.py
浏览文件 @
044909e7
...
...
@@ -184,8 +184,8 @@ class ResNet():
is_first
=
False
,
name
=
name
+
"_branch1"
)
return
fluid
.
layers
.
elementwise_add
(
x
=
short
,
y
=
conv2
,
act
=
'relu'
,
name
=
name
+
".add.output.5"
)
out
=
paddle
.
add
(
x
=
short
,
y
=
conv2
,
name
=
name
+
".add.output.5"
)
return
paddle
.
nn
.
functional
.
relu
(
out
)
def
basic_block
(
self
,
input
,
num_filters
,
stride
,
is_first
,
name
):
conv0
=
self
.
conv_bn_layer
(
...
...
@@ -203,7 +203,9 @@ class ResNet():
name
=
name
+
"_branch2b"
)
short
=
self
.
shortcut
(
input
,
num_filters
,
stride
,
is_first
,
name
=
name
+
"_branch1"
)
return
fluid
.
layers
.
elementwise_add
(
x
=
short
,
y
=
conv1
,
act
=
'relu'
)
out
=
paddle
.
add
(
x
=
short
,
y
=
conv1
)
return
paddle
.
nn
.
functional
.
relu
(
out
)
def
ResNet34
(
prefix_name
=
''
):
...
...
demo/models/resnet_vd.py
浏览文件 @
044909e7
...
...
@@ -234,8 +234,8 @@ class ResNet():
stride
,
if_first
=
if_first
,
name
=
name
+
"_branch1"
)
return
fluid
.
layers
.
elementwise_add
(
x
=
short
,
y
=
conv2
,
act
=
'relu'
)
out
=
paddle
.
add
(
x
=
short
,
y
=
conv2
)
return
paddle
.
nn
.
functional
.
relu
(
out
)
def
basic_block
(
self
,
input
,
num_filters
,
stride
,
name
,
if_first
):
conv0
=
self
.
conv_bn_layer
(
...
...
@@ -257,7 +257,7 @@ class ResNet():
stride
,
if_first
=
if_first
,
name
=
name
+
"_branch1"
)
return
fluid
.
layers
.
elementwise_
add
(
x
=
short
,
y
=
conv1
,
act
=
'relu'
)
return
paddle
.
add
(
x
=
short
,
y
=
conv1
,
act
=
'relu'
)
def
ResNet18_vd
():
...
...
demo/models/slimfacenet.py
浏览文件 @
044909e7
...
...
@@ -172,8 +172,8 @@ class SlimFaceNet():
out
=
self
.
arc_margin_product
(
x
,
label
,
self
.
class_dim
,
s
=
32.0
,
m
=
0.50
,
mode
=
2
)
softmax
=
paddle
.
nn
.
functional
.
softmax
(
input
=
out
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
softmax
,
label
=
label
)
loss
=
fluid
.
layers
.
mean
(
x
=
cost
)
cost
=
paddle
.
nn
.
functional
.
cross_entropy
(
input
=
softmax
,
label
=
label
)
loss
=
paddle
.
mean
(
x
=
cost
)
acc
=
paddle
.
static
.
accuracy
(
input
=
out
,
label
=
label
,
k
=
1
)
return
loss
,
acc
...
...
@@ -227,8 +227,9 @@ class SlimFaceNet():
if
num_in_filter
!=
num_out_filter
or
stride
!=
1
:
return
linear_conv
else
:
return
fluid
.
layers
.
elementwise_add
(
x
=
input_data
,
y
=
linear_conv
,
act
=
None
)
out
=
paddle
.
add
(
x
=
input_data
,
y
=
linear_conv
,
act
=
None
)
return
paddle
.
nn
.
functional
.
relu
(
out
)
def
se_block
(
self
,
input
,
num_out_filter
,
ratio
=
4
,
name
=
None
):
num_mid_filter
=
int
(
num_out_filter
//
ratio
)
...
...
@@ -241,7 +242,7 @@ class SlimFaceNet():
act
=
None
,
param_attr
=
paddle
.
ParamAttr
(
name
=
name
+
'_1_weights'
),
bias_attr
=
paddle
.
ParamAttr
(
name
=
name
+
'_1_offset'
))
conv1
=
fluid
.
layers
.
prelu
(
conv1
=
paddle
.
static
.
nn
.
prelu
(
conv1
,
mode
=
'channel'
,
param_attr
=
paddle
.
ParamAttr
(
...
...
@@ -254,7 +255,7 @@ class SlimFaceNet():
act
=
'hard_sigmoid'
,
param_attr
=
paddle
.
ParamAttr
(
name
=
name
+
'_2_weights'
),
bias_attr
=
paddle
.
ParamAttr
(
name
=
name
+
'_2_offset'
))
scale
=
fluid
.
layers
.
elementwise_mul
(
x
=
input
,
y
=
conv2
,
axis
=
0
)
scale
=
paddle
.
multiply
(
x
=
input
,
y
=
conv2
)
return
scale
def
conv_bn_layer
(
self
,
...
...
@@ -287,7 +288,7 @@ class SlimFaceNet():
moving_mean_name
=
bn_name
+
'_mean'
,
moving_variance_name
=
bn_name
+
'_variance'
)
if
if_act
:
return
fluid
.
layers
.
prelu
(
return
paddle
.
static
.
nn
.
prelu
(
bn
,
mode
=
'channel'
,
param_attr
=
paddle
.
ParamAttr
(
...
...
@@ -297,10 +298,8 @@ class SlimFaceNet():
return
bn
def
arc_margin_product
(
self
,
input
,
label
,
out_dim
,
s
=
32.0
,
m
=
0.50
,
mode
=
2
):
input_norm
=
fluid
.
layers
.
sqrt
(
fluid
.
layers
.
reduce_sum
(
paddle
.
square
(
input
),
dim
=
1
))
input
=
fluid
.
layers
.
elementwise_div
(
input
,
input_norm
,
axis
=
0
)
input_norm
=
paddle
.
sqrt
(
paddle
.
sum
(
paddle
.
square
(
input
),
dim
=
1
))
input
=
paddle
.
divide
(
input
,
input_norm
,
axis
=
0
)
weight
=
paddle
.
static
.
create_parameter
(
shape
=
[
out_dim
,
input
.
shape
[
1
]],
...
...
@@ -310,13 +309,11 @@ class SlimFaceNet():
initializer
=
paddle
.
nn
.
initializer
.
Xavier
(),
regularizer
=
fluid
.
regularizer
.
L2Decay
(
4e-4
)))
weight_norm
=
fluid
.
layers
.
sqrt
(
fluid
.
layers
.
reduce_sum
(
paddle
.
square
(
weight
),
dim
=
1
))
weight
=
fluid
.
layers
.
elementwise_div
(
weight
,
weight_norm
,
axis
=
0
)
weight
=
fluid
.
layers
.
transpose
(
weight
,
perm
=
[
1
,
0
])
weight_norm
=
paddle
.
sqrt
(
paddle
.
sum
(
paddle
.
square
(
weight
),
dim
=
1
))
weight
=
paddle
.
divide
(
weight
,
weight_norm
,
axis
=
0
)
weight
=
paddle
.
transpose
(
weight
,
perm
=
[
1
,
0
])
cosine
=
fluid
.
layers
.
mul
(
input
,
weight
)
sine
=
fluid
.
layers
.
sqrt
(
1.0
-
paddle
.
square
(
cosine
))
sine
=
paddle
.
sqrt
(
1.0
-
paddle
.
square
(
cosine
))
cos_m
=
math
.
cos
(
m
)
sin_m
=
math
.
sin
(
m
)
...
...
@@ -333,16 +330,14 @@ class SlimFaceNet():
pass
one_hot
=
fluid
.
layers
.
one_hot
(
input
=
label
,
depth
=
out_dim
)
output
=
fluid
.
layers
.
elementwise_mul
(
one_hot
,
phi
)
+
fluid
.
layers
.
elementwise_mul
(
(
1.0
-
one_hot
),
cosine
)
output
=
paddle
.
multiply
(
one_hot
,
phi
)
+
paddle
.
multiply
(
(
1.0
-
one_hot
),
cosine
)
output
=
output
*
s
return
output
def
paddle_where_more_than
(
self
,
target
,
limit
,
x
,
y
):
mask
=
fluid
.
layers
.
cast
(
x
=
(
target
>
limit
),
dtype
=
'float32'
)
output
=
fluid
.
layers
.
elementwise_mul
(
mask
,
x
)
+
fluid
.
layers
.
elementwise_mul
((
1.0
-
mask
),
y
)
mask
=
paddle
.
cast
(
x
=
(
target
>
limit
),
dtype
=
'float32'
)
output
=
paddle
.
multiply
(
mask
,
x
)
+
paddle
.
multiply
((
1.0
-
mask
),
y
)
return
output
...
...
demo/ofa/ernie/ernie_supernet/modeling_ernie_supernet.py
浏览文件 @
044909e7
...
...
@@ -31,6 +31,7 @@ if six.PY2:
else
:
from
pathlib
import
Path
import
paddle
import
paddle.fluid.dygraph
as
D
import
paddle.fluid
as
F
import
paddle.fluid.layers
as
L
...
...
@@ -73,14 +74,14 @@ def _attn_forward(self,
else
:
n_head
=
self
.
n_head
q
=
L
.
transpose
(
L
.
reshape
(
q
,
[
0
,
0
,
n_head
,
q
.
shape
[
-
1
]
//
n_head
]),
q
=
paddle
.
transpose
(
paddle
.
reshape
(
q
,
[
0
,
0
,
n_head
,
q
.
shape
[
-
1
]
//
n_head
]),
[
0
,
2
,
1
,
3
])
#[batch, head, seq, dim]
k
=
L
.
transpose
(
L
.
reshape
(
k
,
[
0
,
0
,
n_head
,
k
.
shape
[
-
1
]
//
n_head
]),
k
=
paddle
.
transpose
(
paddle
.
reshape
(
k
,
[
0
,
0
,
n_head
,
k
.
shape
[
-
1
]
//
n_head
]),
[
0
,
2
,
1
,
3
])
#[batch, head, seq, dim]
v
=
L
.
transpose
(
L
.
reshape
(
v
,
[
0
,
0
,
n_head
,
v
.
shape
[
-
1
]
//
n_head
]),
v
=
paddle
.
transpose
(
paddle
.
reshape
(
v
,
[
0
,
0
,
n_head
,
v
.
shape
[
-
1
]
//
n_head
]),
[
0
,
2
,
1
,
3
])
#[batch, head, seq, dim]
q
=
L
.
scale
(
q
,
scale
=
self
.
d_key
**-
0.5
)
...
...
@@ -275,7 +276,7 @@ def _seqence_forward(self, *args, **kwargs):
if
len
(
labels
.
shape
)
==
1
:
labels
=
L
.
reshape
(
labels
,
[
-
1
,
1
])
loss
=
L
.
softmax_with_cross_entropy
(
logits
,
labels
)
loss
=
L
.
reduce_
mean
(
loss
)
loss
=
paddle
.
mean
(
loss
)
else
:
loss
=
None
return
loss
,
logits
,
additional_info
...
...
demo/ofa/ernie/ofa_ernie.py
浏览文件 @
044909e7
...
...
@@ -46,7 +46,7 @@ from paddleslim.nas.ofa.convert_super import Convert, supernet
def
soft_cross_entropy
(
inp
,
target
):
inp_likelihood
=
L
.
log_softmax
(
inp
,
axis
=-
1
)
target_prob
=
L
.
softmax
(
target
,
axis
=-
1
)
return
-
1.
*
L
.
mean
(
L
.
reduce_
sum
(
inp_likelihood
*
target_prob
,
dim
=-
1
))
return
-
1.
*
L
.
mean
(
paddle
.
sum
(
inp_likelihood
*
target_prob
,
dim
=-
1
))
if
__name__
==
'__main__'
:
...
...
demo/optimizer.py
浏览文件 @
044909e7
...
...
@@ -62,7 +62,7 @@ class Optimizer(object):
self
.
decay_rate
=
decay_rate
self
.
total_images
=
total_images
if
args
.
use_gpu
:
devices_num
=
paddle
.
f
luid
.
core
.
get_cuda_device_count
()
devices_num
=
paddle
.
f
ramework
.
core
.
get_cuda_device_count
()
else
:
devices_num
=
int
(
os
.
environ
.
get
(
'CPU_NUM'
,
1
))
...
...
demo/quant/quant_embedding/cluster_train.py
浏览文件 @
044909e7
...
...
@@ -182,7 +182,7 @@ def train_loop(args, train_program, reader, py_reader, loss, trainer_id, weight,
print
(
"model saved in %s"
%
model_dir
)
batch_id
+=
1
except
paddle
.
f
luid
.
core
.
EOFException
:
except
paddle
.
f
ramework
.
core
.
EOFException
:
py_reader
.
reset
()
epoch_end
=
time
.
time
()
logger
.
info
(
"Epoch: {0}, Train total expend: {1} "
.
format
(
...
...
demo/quant/quant_embedding/net.py
浏览文件 @
044909e7
...
...
@@ -86,7 +86,7 @@ def skip_gram_word2vec(dict_size, embedding_size, is_sparse=False, neg_num=5):
neg_emb_b_vec
=
paddle
.
reshape
(
neg_emb_b
,
shape
=
[
-
1
,
neg_num
])
true_logits
=
paddle
.
add
(
paddle
.
mean
(
paddle
.
multiply
(
input_emb
,
true_emb_w
),
axis
=
1
,
keepdim
=
True
),
paddle
.
multiply
(
input_emb
,
true_emb_w
),
keepdim
=
True
),
true_emb_b
)
input_emb_re
=
paddle
.
reshape
(
input_emb
,
shape
=
[
-
1
,
1
,
embedding_size
])
neg_matmul
=
fluid
.
layers
.
matmul
(
...
...
demo/quant/quant_embedding/train.py
浏览文件 @
044909e7
...
...
@@ -176,7 +176,7 @@ def train_loop(args, train_program, reader, py_reader, loss, trainer_id, weight,
print
(
"model saved in %s"
%
model_dir
)
batch_id
+=
1
except
paddle
.
f
luid
.
core
.
EOFException
:
except
paddle
.
f
ramework
.
core
.
EOFException
:
py_reader
.
reset
()
epoch_end
=
time
.
time
()
logger
.
info
(
"Epoch: {0}, Train total expend: {1} "
.
format
(
...
...
demo/slimfacenet/train_eval.py
浏览文件 @
044909e7
...
...
@@ -151,17 +151,17 @@ def train(exe, train_program, train_out, test_program, test_out, args):
def
build_program
(
program
,
startup
,
args
,
is_train
=
True
):
if
args
.
use_gpu
:
num_trainers
=
fluid
.
core
.
get_cuda_
device_count
()
num_trainers
=
paddle
.
device
.
cuda
.
device_count
()
else
:
num_trainers
=
int
(
os
.
environ
.
get
(
'CPU_NUM'
,
1
))
places
=
fluid
.
cuda_places
()
if
args
.
use_gpu
else
paddle
.
CPUPlace
()
places
=
paddle
.
static
.
cuda_places
()
if
args
.
use_gpu
else
paddle
.
CPUPlace
()
train_dataset
=
CASIA_Face
(
root
=
args
.
train_data_dir
)
trainset_scale
=
len
(
train_dataset
)
with
paddle
.
static
.
program_guard
(
main_program
=
program
,
startup_program
=
startup
):
with
fluid
.
unique_name
.
guard
():
with
paddle
.
utils
.
unique_name
.
guard
():
# Model construction
model
=
models
.
__dict__
[
args
.
model
](
class_dim
=
train_dataset
.
class_nums
)
...
...
@@ -215,7 +215,7 @@ def build_program(program, startup, args, is_train=True):
return_list
=
False
)
reader
.
set_sample_list_generator
(
test_reader
,
places
=
fluid
.
cuda_places
()
places
=
paddle
.
static
.
cuda_places
()
if
args
.
use_gpu
else
paddle
.
CPUPlace
())
model
.
extract_feature
=
True
...
...
@@ -296,7 +296,7 @@ def main():
args
=
parser
.
parse_args
()
if
args
.
use_gpu
:
num_trainers
=
fluid
.
core
.
get_cuda_device_count
()
num_trainers
=
paddle
.
fluid
.
core
.
get_cuda_device_count
()
else
:
num_trainers
=
int
(
os
.
environ
.
get
(
'CPU_NUM'
,
1
))
print
(
args
)
...
...
@@ -366,7 +366,8 @@ def main():
return_list
=
False
)
reader
.
set_sample_list_generator
(
test_reader
,
places
=
fluid
.
cuda_places
()
if
args
.
use_gpu
else
paddle
.
CPUPlace
())
places
=
paddle
.
static
.
cuda_places
()
if
args
.
use_gpu
else
paddle
.
CPUPlace
())
test_out
=
(
fetch_targets
,
reader
,
flods
,
flags
)
print
(
'fetch_targets[0]: '
,
fetch_targets
[
0
])
print
(
'feed_target_names: '
,
feed_target_names
)
...
...
paddleslim/common/recover_program.py
浏览文件 @
044909e7
...
...
@@ -45,7 +45,7 @@ def _recover_reserve_space_with_bn(program):
generate_with_ignorable_key
(
"."
.
join
(
[
"reserve_space"
,
'tmp'
])),
dtype
=
block
.
var
(
op
.
input
(
"X"
)[
0
]).
dtype
,
type
=
paddle
.
f
luid
.
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
type
=
paddle
.
f
ramework
.
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
persistable
=
False
,
stop_gradient
=
True
)
op
.
desc
.
set_output
(
"ReserveSpace"
,
[
reserve_space
.
name
])
...
...
paddleslim/common/rl_controller/lstm/lstm_controller.py
浏览文件 @
044909e7
...
...
@@ -113,7 +113,7 @@ class LSTM(RLBaseController):
entropies
=
[]
sample_log_probs
=
[]
with
fluid
.
unique_name
.
guard
(
'Controller'
):
with
paddle
.
utils
.
unique_name
.
guard
(
'Controller'
):
self
.
_create_parameter
()
inputs
=
self
.
g_emb
...
...
paddleslim/dygraph/prune/pruning_plan.py
浏览文件 @
044909e7
...
...
@@ -114,7 +114,7 @@ class PruningPlan():
elif
p
.
is_cuda_pinned_place
():
place
=
paddle
.
CUDAPinnedPlace
()
else
:
p
=
paddle
.
f
luid
.
core
.
Place
()
p
=
paddle
.
f
ramework
.
core
.
Place
()
p
.
set_place
(
t_value
.
_place
())
place
=
paddle
.
CUDAPlace
(
p
.
gpu_device_id
())
...
...
@@ -153,7 +153,7 @@ class PruningPlan():
elif
p
.
is_cuda_pinned_place
():
place
=
paddle
.
CUDAPinnedPlace
()
else
:
p
=
paddle
.
f
luid
.
core
.
Place
()
p
=
paddle
.
f
ramework
.
core
.
Place
()
p
.
set_place
(
t_value
.
_place
())
place
=
paddle
.
CUDAPlace
(
p
.
gpu_device_id
())
...
...
@@ -190,12 +190,15 @@ class PruningPlan():
"float32"
)
p
=
t_value
.
_place
()
print
(
f
"hit pruning plan--------------------"
)
if
p
.
is_cpu_place
():
print
(
f
"hit pruning plan cpu--------------------"
)
place
=
paddle
.
CPUPlace
()
elif
p
.
is_cuda_pinned_place
():
place
=
paddle
.
CUDAPinnedPlace
()
else
:
p
=
paddle
.
fluid
.
core
.
Place
()
print
(
f
"hit pruning plan gpu--------------------"
)
p
=
paddle
.
framework
.
core
.
Place
()
p
.
set_place
(
t_value
.
_place
())
place
=
paddle
.
CUDAPlace
(
p
.
gpu_device_id
())
...
...
@@ -246,7 +249,7 @@ class PruningPlan():
elif
p
.
is_cuda_pinned_place
():
place
=
paddle
.
CUDAPinnedPlace
()
else
:
p
=
paddle
.
f
luid
.
core
.
Place
()
p
=
paddle
.
f
ramework
.
core
.
Place
()
p
.
set_place
(
t_value
.
_place
())
place
=
paddle
.
CUDAPlace
(
p
.
gpu_device_id
())
t_value
.
set
(
pruned_value
,
place
)
...
...
@@ -274,7 +277,7 @@ class PruningPlan():
elif
p
.
is_cuda_pinned_place
():
place
=
paddle
.
CUDAPinnedPlace
()
else
:
p
=
paddle
.
f
luid
.
core
.
Place
()
p
=
paddle
.
f
ramework
.
core
.
Place
()
p
.
set_place
(
t_value
.
_place
())
place
=
paddle
.
CUDAPlace
(
p
.
gpu_device_id
())
...
...
paddleslim/nas/darts/architect.py
浏览文件 @
044909e7
...
...
@@ -102,8 +102,7 @@ class Architect(object):
def
_hessian_vector_product
(
self
,
vector
,
input
,
target
,
r
=
1e-2
):
R
=
r
*
paddle
.
rsqrt
(
paddle
.
fluid
.
layers
.
sum
(
[
paddle
.
sum
(
x
=
paddle
.
square
(
v
))
for
v
in
vector
]))
paddle
.
add_n
([
paddle
.
sum
(
x
=
paddle
.
square
(
v
))
for
v
in
vector
]))
model_params
=
[
p
for
p
in
self
.
model
.
parameters
()
...
...
paddleslim/nas/ofa/ofa.py
浏览文件 @
044909e7
...
...
@@ -610,7 +610,8 @@ class OFA(OFABase):
print
(
f
"hit cpu in ofa-------------------------------"
)
place
=
paddle
.
CPUPlace
()
else
:
place
=
paddle
.
fluid
.
core
.
CUDAPlace
(
p
.
gpu_device_id
())
place
=
paddle
.
framework
.
core
.
CUDAPlace
(
p
.
gpu_device_id
(
))
t_value
.
set
(
pruned_state_dict
[
name
],
place
)
if
super_model_state_dict
!=
None
and
len
(
super_model_state_dict
)
!=
0
:
...
...
paddleslim/nas/ofa/utils/utils.py
浏览文件 @
044909e7
...
...
@@ -60,20 +60,6 @@ def set_state_dict(model, state_dict):
_logger
.
info
(
'{} is not in state_dict'
.
format
(
tmp_n
))
def
to_tensor
(
string_values
,
name
=
"text"
):
"""
Create the tensor that the value holds the list of string.
NOTICE: The value will be holded in the cpu place.
Parameters:
string_values(list[string]): The value will be setted to the tensor.
name(string): The name of the tensor.
"""
tensor
=
paddle
.
Tensor
(
paddle
.
fluid
.
core
.
VarDesc
.
VarType
.
STRING
,
[],
name
,
paddle
.
fluid
.
core
.
VarDesc
.
VarType
.
STRINGS
,
False
)
tensor
.
value
().
set_string_list
(
string_values
)
return
tensor
def
build_input
(
input_size
,
dtypes
):
if
isinstance
(
input_size
,
list
)
and
all
(
isinstance
(
i
,
numbers
.
Number
)
for
i
in
input_size
):
...
...
@@ -81,8 +67,10 @@ def build_input(input_size, dtypes):
dtype
=
dtypes
[
0
]
else
:
dtype
=
dtypes
if
dtype
==
paddle
.
fluid
.
core
.
VarDesc
.
VarType
.
STRINGS
:
return
to_tensor
([
""
])
# if dtype == paddle.framework.core.VarDesc.VarType.STRINGS:
# return to_tensor([""])
# TODO: Uncommet Add unittest for strings dtype
assert
dtype
!=
paddle
.
framework
.
core
.
VarDesc
.
VarType
.
STRINGS
return
paddle
.
cast
(
paddle
.
rand
(
list
(
input_size
)),
dtype
)
if
isinstance
(
input_size
,
dict
):
inputs
=
{}
...
...
paddleslim/quant/analysis_qat.py
浏览文件 @
044909e7
...
...
@@ -20,7 +20,7 @@ import logging
import
numpy
as
np
import
paddle
from
paddle.f
luid
import
core
from
paddle.f
ramework
import
core
from
paddle.fluid.framework
import
IrGraph
from
..common
import
get_logger
,
load_inference_model
...
...
paddleslim/quant/quant_aware_with_infermodel.py
浏览文件 @
044909e7
...
...
@@ -26,7 +26,7 @@ import paddle
from
..common.recover_program
import
recover_inference_program
from
.quanter
import
_quant_config_default
,
_parse_configs
,
pact
,
get_pact_optimizer
from
.quanter
import
quant_aware
,
convert
from
..dist
import
merge
,
l2
,
soft_label
,
fsp
from
..dist
import
merge
,
l2
,
soft_label
from
..auto_compression.create_compressed_program
import
build_distill_program
import
logging
logging
.
getLogger
().
setLevel
(
logging
.
INFO
)
...
...
paddleslim/quant/quant_embedding.py
浏览文件 @
044909e7
...
...
@@ -153,8 +153,8 @@ def _clear_var(var_name, scope):
def
_get_var_dtype
(
config
):
return
paddle
.
f
luid
.
core
.
VarDesc
.
VarType
.
INT8
if
config
[
'dtype'
]
==
'int8'
\
else
paddle
.
f
luid
.
core
.
VarDesc
.
VarType
.
INT16
return
paddle
.
f
ramework
.
core
.
VarDesc
.
VarType
.
INT8
if
config
[
'dtype'
]
==
'int8'
\
else
paddle
.
f
ramework
.
core
.
VarDesc
.
VarType
.
INT16
def
_quant_embedding_abs_max
(
graph
,
scope
,
place
,
config
,
var_name
,
...
...
@@ -189,7 +189,7 @@ def _quant_embedding_abs_max(graph, scope, place, config, var_name,
name
=
_get_dequant_var_name
(
var_node
.
name
()),
var_type
=
var_node
.
type
(),
shape
=
var_node
.
shape
(),
var_dtype
=
paddle
.
f
luid
.
core
.
VarDesc
.
VarType
.
FP32
)
var_dtype
=
paddle
.
f
ramework
.
core
.
VarDesc
.
VarType
.
FP32
)
scope
.
var
(
dequant_var_node
.
name
())
max_range
=
(
1
<<
(
config
[
'quantize_bits'
]
-
1
))
-
1
...
...
@@ -199,7 +199,7 @@ def _quant_embedding_abs_max(graph, scope, place, config, var_name,
attrs
=
{
'max_range'
:
float
(
max_range
),
'op_role'
:
paddle
.
f
luid
.
core
.
op_proto_and_checker_maker
.
OpRole
.
Forward
paddle
.
f
ramework
.
core
.
op_proto_and_checker_maker
.
OpRole
.
Forward
},
inputs
=
{
'X'
:
var_node
,
'Scale'
:
scale_node
},
...
...
@@ -232,7 +232,7 @@ def _quant_embedding_abs_max(graph, scope, place, config, var_name,
_get_scale_var_name
(
var_name
),
var_type
=
embedding_node
.
type
(),
shape
=
[
1
],
var_dtype
=
paddle
.
f
luid
.
core
.
VarDesc
.
VarType
.
FP32
)
var_dtype
=
paddle
.
f
ramework
.
core
.
VarDesc
.
VarType
.
FP32
)
quant_tensor_var
=
graph
.
create_persistable_node
(
_get_quant_var_name
(
var_name
),
var_type
=
embedding_node
.
type
(),
...
...
@@ -318,7 +318,7 @@ def _quant_embedding_log(graph, scope, place, config, var_name, embedding_node):
name
=
_get_dequant_var_name
(
var_node
.
name
()),
var_type
=
var_node
.
type
(),
shape
=
var_node
.
shape
(),
var_dtype
=
paddle
.
f
luid
.
core
.
VarDesc
.
VarType
.
FP32
)
var_dtype
=
paddle
.
f
ramework
.
core
.
VarDesc
.
VarType
.
FP32
)
scope
.
var
(
dequant_var_node
.
name
())
output_ops
=
var_node
.
outputs
...
...
@@ -326,7 +326,7 @@ def _quant_embedding_log(graph, scope, place, config, var_name, embedding_node):
op_type
=
'dequantize_log'
,
attrs
=
{
'op_role'
:
paddle
.
f
luid
.
core
.
op_proto_and_checker_maker
.
OpRole
.
Forward
paddle
.
f
ramework
.
core
.
op_proto_and_checker_maker
.
OpRole
.
Forward
},
inputs
=
{
'X'
:
var_node
,
'Dict'
:
topk_num_node
},
...
...
@@ -350,12 +350,12 @@ def _quant_embedding_log(graph, scope, place, config, var_name, embedding_node):
_get_dict_var_name
(
var_name
),
var_type
=
embedding_node
.
type
(),
shape
=
topk_num
.
shape
,
var_dtype
=
paddle
.
f
luid
.
core
.
VarDesc
.
VarType
.
FP32
)
var_dtype
=
paddle
.
f
ramework
.
core
.
VarDesc
.
VarType
.
FP32
)
quant_tensor_var
=
graph
.
create_persistable_node
(
_get_quant_var_name
(
var_name
),
var_type
=
embedding_node
.
type
(),
shape
=
embedding_node
.
shape
(),
var_dtype
=
paddle
.
f
luid
.
core
.
VarDesc
.
VarType
.
INT8
)
var_dtype
=
paddle
.
f
ramework
.
core
.
VarDesc
.
VarType
.
INT8
)
# create var in scope
scope
.
var
(
_get_quant_var_name
(
var_name
))
scope
.
var
(
_get_dict_var_name
(
var_name
))
...
...
@@ -393,7 +393,7 @@ def _split_embedding_seq_pool(graph, op):
out
=
outputs
[
0
]
lookup_out
=
graph
.
create_var_node
(
name
=
ids
.
name
()
+
'.look_up_table.out'
,
var_type
=
paddle
.
f
luid
.
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
var_type
=
paddle
.
f
ramework
.
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
shape
=
[
1
],
var_dtype
=
weight
.
dtype
())
lookup_table_op
=
graph
.
create_op_node
(
...
...
@@ -411,7 +411,7 @@ def _split_embedding_seq_pool(graph, op):
graph
.
link_to
(
lookup_table_op
,
lookup_out
)
max_index
=
graph
.
create_var_node
(
name
=
ids
.
name
()
+
'.seq_pool_op.max_index'
,
var_type
=
paddle
.
f
luid
.
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
var_type
=
paddle
.
f
ramework
.
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
shape
=
[
1
],
var_dtype
=
weight
.
dtype
())
...
...
@@ -452,7 +452,7 @@ def quant_embedding(program, place, config=None, scope=None):
scope
=
paddle
.
static
.
global_scope
()
if
scope
is
None
else
scope
graph
=
paddle
.
fluid
.
framework
.
IrGraph
(
paddle
.
f
luid
.
core
.
Graph
(
program
.
desc
),
for_test
=
True
)
paddle
.
f
ramework
.
core
.
Graph
(
program
.
desc
),
for_test
=
True
)
quantize_params_map
=
{}
all_op
=
graph
.
all_op_nodes
()
for
op
in
all_op
:
...
...
paddleslim/quant/quanter.py
浏览文件 @
044909e7
...
...
@@ -20,7 +20,7 @@ import collections
import
numpy
as
np
import
paddle
from
paddle.f
luid
import
core
from
paddle.f
ramework
import
core
from
paddle.fluid.layer_helper
import
LayerHelper
from
paddle.fluid.framework
import
IrGraph
from
paddle.fluid.contrib.slim.quantization
import
WeightQuantization
...
...
@@ -799,14 +799,10 @@ def pact(x, name=None):
regularizer
=
paddle
.
fluid
.
regularizer
.
L2Decay
(
0.0001
),
learning_rate
=
1
)
u_param
=
helper
.
create_parameter
(
attr
=
u_param_attr
,
shape
=
[
1
],
dtype
=
dtype
)
x
=
paddle
.
fluid
.
layers
.
elementwise_sub
(
x
,
paddle
.
nn
.
functional
.
relu
(
paddle
.
fluid
.
layers
.
elementwise_sub
(
x
,
u_param
)))
x
=
paddle
.
fluid
.
layers
.
elementwise_add
(
x
,
paddle
.
nn
.
functional
.
relu
(
paddle
.
fluid
.
layers
.
elementwise_sub
(
-
u_param
,
x
)))
x
=
paddle
.
subtract
(
x
,
paddle
.
nn
.
functional
.
relu
(
paddle
.
subtract
(
x
,
u_param
)))
x
=
paddle
.
paddle
.
add
(
x
,
paddle
.
nn
.
functional
.
relu
(
paddle
.
subtract
(
-
u_param
,
x
)))
return
x
...
...
tests/dygraph/test_prune.py
浏览文件 @
044909e7
...
...
@@ -28,6 +28,7 @@ class TestPrune(unittest.TestCase):
def
runTest
(
self
):
static_shapes
=
self
.
static_prune
(
self
.
_net
,
self
.
_ratios
)
dygraph_shapes
=
self
.
dygraph_prune
(
self
.
_net
,
self
.
_ratios
)
lazy_dygraph_shapes
=
self
.
dygraph_lazy_prune
(
self
.
_net
,
self
.
_ratios
)
all_right
=
True
for
_name
,
_shape
in
static_shapes
.
items
():
if
dygraph_shapes
[
_name
]
!=
list
(
_shape
):
...
...
@@ -37,11 +38,18 @@ class TestPrune(unittest.TestCase):
all_right
=
False
self
.
assertTrue
(
all_right
)
def
dygraph_prune
(
self
,
net
,
ratios
):
def
dygraph_lazy_prune
(
self
,
net
,
ratios
):
if
paddle
.
framework
.
core
.
is_compiled_with_cuda
():
paddle
.
set_device
(
'gpu'
)
self
.
dygraph_prune
(
net
,
ratios
,
apply
=
"lazy"
)
paddle
.
set_device
(
'cpu'
)
return
self
.
dygraph_prune
(
net
,
ratios
,
apply
=
"lazy"
)
def
dygraph_prune
(
self
,
net
,
ratios
,
apply
=
"impretive"
):
paddle
.
disable_static
()
model
=
net
(
pretrained
=
False
)
pruner
=
L1NormFilterPruner
(
model
,
[
1
,
3
,
16
,
16
])
pruner
.
prune_vars
(
ratios
,
0
)
pruner
.
prune_vars
(
ratios
,
0
,
apply
=
apply
)
shapes
=
{}
for
param
in
model
.
parameters
():
shapes
[
param
.
name
]
=
param
.
shape
...
...
@@ -52,7 +60,7 @@ class TestPrune(unittest.TestCase):
paddle
.
enable_static
()
main_program
=
paddle
.
static
.
Program
()
startup_program
=
paddle
.
static
.
Program
()
with
paddle
.
fluid
.
unique_name
.
guard
():
with
paddle
.
utils
.
unique_name
.
guard
():
with
paddle
.
static
.
program_guard
(
main_program
,
startup_program
):
input
=
paddle
.
static
.
data
(
name
=
"image"
,
shape
=
[
None
,
3
,
16
,
16
])
...
...
@@ -61,7 +69,7 @@ class TestPrune(unittest.TestCase):
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
scope
=
paddle
.
fluid
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
exe
.
run
(
startup_program
,
scope
=
scope
)
pruner
=
Pruner
()
main_program
,
_
,
_
=
pruner
.
prune
(
...
...
tests/dygraph/test_sensitivity.py
浏览文件 @
044909e7
...
...
@@ -98,7 +98,7 @@ class TestSensitivity(unittest.TestCase):
paddle
.
enable_static
()
main_program
=
paddle
.
static
.
Program
()
startup_program
=
paddle
.
static
.
Program
()
with
paddle
.
fluid
.
unique_name
.
guard
():
with
paddle
.
utils
.
unique_name
.
guard
():
with
paddle
.
static
.
program_guard
(
main_program
,
startup_program
):
input
=
paddle
.
static
.
data
(
name
=
"image"
,
shape
=
[
None
,
1
,
28
,
28
])
...
...
tests/test_autoprune.py
浏览文件 @
044909e7
...
...
@@ -53,7 +53,7 @@ class TestPrune(StaticCase):
val_program
=
paddle
.
static
.
default_main_program
().
clone
(
for_test
=
True
)
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
scope
=
paddle
.
fluid
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
exe
.
run
(
startup_program
,
scope
=
scope
)
pruner
=
AutoPruner
(
...
...
tests/test_fpgm_prune.py
浏览文件 @
044909e7
...
...
@@ -48,7 +48,7 @@ class TestPrune(StaticCase):
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
scope
=
paddle
.
fluid
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
exe
.
run
(
startup_program
,
scope
=
scope
)
criterion
=
'geometry_median'
pruner
=
Pruner
(
criterion
)
...
...
tests/test_fsp_loss.py
已删除
100644 → 0
浏览文件 @
87cfc2e6
# Copyright (c) 2020 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.
import
sys
sys
.
path
.
append
(
"../"
)
import
unittest
import
paddle
from
paddleslim.dist
import
merge
,
fsp
from
layers
import
conv_bn_layer
from
static_case
import
StaticCase
class
TestFSPLoss
(
StaticCase
):
def
test_fsp_loss
(
self
):
input
=
paddle
.
static
.
data
(
name
=
"image"
,
shape
=
[
None
,
3
,
224
,
224
])
conv1
=
conv_bn_layer
(
input
,
8
,
3
,
"conv1"
)
conv2
=
conv_bn_layer
(
conv1
,
8
,
3
,
"conv2"
)
student_predict
=
conv1
+
conv2
teacher_main
=
paddle
.
static
.
Program
()
teacher_startup
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
teacher_main
,
teacher_startup
):
input
=
paddle
.
static
.
data
(
name
=
"image"
,
shape
=
[
None
,
3
,
224
,
224
])
conv1
=
conv_bn_layer
(
input
,
8
,
3
,
"conv1"
)
conv2
=
conv_bn_layer
(
conv1
,
8
,
3
,
"conv2"
)
sum1
=
conv1
+
conv2
conv3
=
conv_bn_layer
(
sum1
,
8
,
3
,
"conv3"
)
conv4
=
conv_bn_layer
(
conv3
,
8
,
3
,
"conv4"
)
sum2
=
conv4
+
sum1
conv5
=
conv_bn_layer
(
sum2
,
8
,
3
,
"conv5"
)
teacher_predict
=
conv_bn_layer
(
conv5
,
8
,
3
,
"conv6"
)
place
=
paddle
.
CPUPlace
()
data_name_map
=
{
'image'
:
'image'
}
merge
(
teacher_main
,
paddle
.
static
.
default_main_program
(),
data_name_map
,
place
)
merged_ops
=
[]
for
block
in
paddle
.
static
.
default_main_program
().
blocks
:
for
op
in
block
.
ops
:
merged_ops
.
append
(
op
.
type
)
distill_loss
=
fsp
(
'teacher_conv1_out.tmp_1'
,
'teacher_conv6_out.tmp_0'
,
'conv1_out.tmp_0'
,
'conv2_out.tmp_0'
)
loss_ops
=
[]
for
block
in
paddle
.
static
.
default_main_program
().
blocks
:
for
op
in
block
.
ops
:
loss_ops
.
append
(
op
.
type
)
self
.
assertTrue
(
set
(
merged_ops
).
difference
(
set
(
loss_ops
))
==
set
())
self
.
assertTrue
(
set
(
loss_ops
).
difference
(
set
(
merged_ops
))
==
{
'elementwise_sub'
,
'reduce_mean'
,
'square'
,
'fsp'
})
if
__name__
==
'__main__'
:
unittest
.
main
()
tests/test_optimal_threshold.py
浏览文件 @
044909e7
...
...
@@ -48,7 +48,7 @@ class TestPrune(StaticCase):
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
scope
=
paddle
.
fluid
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
exe
.
run
(
startup_program
,
scope
=
scope
)
criterion
=
'bn_scale'
idx_selector
=
'optimal_threshold'
...
...
tests/test_prune.py
浏览文件 @
044909e7
...
...
@@ -52,7 +52,7 @@ class TestPrune(StaticCase):
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
scope
=
paddle
.
fluid
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
exe
.
run
(
startup_program
,
scope
=
scope
)
pruner
=
Pruner
()
main_program
,
_
,
_
=
pruner
.
prune
(
...
...
tests/test_prune_op.py
浏览文件 @
044909e7
...
...
@@ -44,7 +44,7 @@ class TestPrune(StaticCase):
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
scope
=
paddle
.
fluid
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
exe
.
run
(
startup_program
,
scope
=
scope
)
pruner
=
Pruner
()
# test backward search of concat
...
...
@@ -122,7 +122,7 @@ class TestSplit(StaticCase):
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
scope
=
paddle
.
fluid
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
exe
.
run
(
startup_program
,
scope
=
scope
)
pruner
=
Pruner
()
# test backward search of concat
...
...
@@ -159,7 +159,7 @@ class TestMul(StaticCase):
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
scope
=
paddle
.
fluid
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
exe
.
run
(
startup_program
,
scope
=
scope
)
pruner
=
Pruner
()
# test backward search of concat
...
...
tests/test_prune_walker.py
浏览文件 @
044909e7
...
...
@@ -68,7 +68,7 @@ class TestPrune(StaticCase):
return
cond_conv2
cond_output
=
paddle
.
static
.
nn
.
cond
(
cond
,
cond_block1
,
cond_block2
)
sum3
=
paddle
.
fluid
.
layers
.
sum
([
sum2
,
cond_output
])
sum3
=
paddle
.
add_n
([
sum2
,
cond_output
])
conv6
=
conv_bn_layer
(
sum3
,
8
,
3
,
"conv6"
)
sub1
=
conv6
-
sum3
...
...
tests/test_quant_aware.py
浏览文件 @
044909e7
...
...
@@ -39,7 +39,7 @@ class TestQuantAwareCase1(StaticCase):
def
get_op_number
(
self
,
prog
):
graph
=
paddle
.
fluid
.
framework
.
IrGraph
(
paddle
.
f
luid
.
core
.
Graph
(
prog
.
desc
),
for_test
=
False
)
paddle
.
f
ramework
.
core
.
Graph
(
prog
.
desc
),
for_test
=
False
)
quant_op_nums
=
0
op_nums
=
0
for
op
in
graph
.
all_op_nodes
():
...
...
tests/test_slim_prune.py
浏览文件 @
044909e7
...
...
@@ -48,7 +48,7 @@ class TestPrune(StaticCase):
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
scope
=
paddle
.
fluid
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
exe
.
run
(
startup_program
,
scope
=
scope
)
criterion
=
'bn_scale'
pruner
=
Pruner
(
criterion
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录