未验证 提交 044909e7 编写于 作者: W whs 提交者: GitHub

Remove fluid API (#1567)

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