Skip to content

Commit 2a0156f

Browse files
committed
[NPU] Optimize Performance on NPU
1 parent dddf008 commit 2a0156f

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import paddle
2+
import paddle.nn as nn
3+
import paddle.nn.functional as F
4+
5+
__all__ = ["AdaptiveAvgPool2D"]
6+
7+
8+
class AdaptiveAvgPool2D(nn.AdaptiveAvgPool2D):
9+
def __init__(self, *args, **kwargs):
10+
super().__init__(*args, **kwargs)
11+
12+
if paddle.device.get_device().startswith("npu"):
13+
self.device = "npu"
14+
else:
15+
self.device = None
16+
17+
if isinstance(self._output_size, int) and self._output_size == 1:
18+
self._gap = True
19+
elif isinstance(self._output_size, tuple) and self._output_size[
20+
0] == 1 and self._output_size[1] == 1:
21+
self._gap = True
22+
else:
23+
self._gap = False
24+
25+
def forward(self, x):
26+
if self.device == "npu" and self._gap:
27+
# Global Average Pooling
28+
N, C, _, _ = x.shape
29+
x_mean = paddle.mean(x, axis=[2, 3])
30+
x_mean = paddle.reshape(x_mean, [N, C, 1, 1])
31+
return x_mean
32+
else:
33+
return F.adaptive_avg_pool2d(
34+
x,
35+
output_size=self._output_size,
36+
data_format=self._data_format,
37+
name=self._name, )

ppocr/modeling/backbones/det_pp_lcnet.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
import paddle
1919
import paddle.nn as nn
2020
from paddle import ParamAttr
21-
from paddle.nn import AdaptiveAvgPool2D, BatchNorm, Conv2D, Dropout, Linear
21+
from paddle.nn import BatchNorm, Conv2D, Dropout, Linear
2222
from paddle.regularizer import L2Decay
2323
from paddle.nn.initializer import KaimingNormal
2424
from paddle.utils.download import get_path_from_url
2525

26+
from .custom_devices_layers import AdaptiveAvgPool2D
27+
2628
MODEL_URLS = {
2729
"PPLCNet_x0.25":
2830
"https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/PPLCNet_x0_25_pretrained.pdparams",
@@ -191,7 +193,7 @@ def __init__(self,
191193
num_filters=make_divisible(16 * scale),
192194
stride=2)
193195

194-
self.blocks2 = nn.Sequential(* [
196+
self.blocks2 = nn.Sequential(*[
195197
DepthwiseSeparable(
196198
num_channels=make_divisible(in_c * scale),
197199
num_filters=make_divisible(out_c * scale),
@@ -201,7 +203,7 @@ def __init__(self,
201203
for i, (k, in_c, out_c, s, se) in enumerate(NET_CONFIG["blocks2"])
202204
])
203205

204-
self.blocks3 = nn.Sequential(* [
206+
self.blocks3 = nn.Sequential(*[
205207
DepthwiseSeparable(
206208
num_channels=make_divisible(in_c * scale),
207209
num_filters=make_divisible(out_c * scale),
@@ -211,7 +213,7 @@ def __init__(self,
211213
for i, (k, in_c, out_c, s, se) in enumerate(NET_CONFIG["blocks3"])
212214
])
213215

214-
self.blocks4 = nn.Sequential(* [
216+
self.blocks4 = nn.Sequential(*[
215217
DepthwiseSeparable(
216218
num_channels=make_divisible(in_c * scale),
217219
num_filters=make_divisible(out_c * scale),
@@ -221,7 +223,7 @@ def __init__(self,
221223
for i, (k, in_c, out_c, s, se) in enumerate(NET_CONFIG["blocks4"])
222224
])
223225

224-
self.blocks5 = nn.Sequential(* [
226+
self.blocks5 = nn.Sequential(*[
225227
DepthwiseSeparable(
226228
num_channels=make_divisible(in_c * scale),
227229
num_filters=make_divisible(out_c * scale),
@@ -231,7 +233,7 @@ def __init__(self,
231233
for i, (k, in_c, out_c, s, se) in enumerate(NET_CONFIG["blocks5"])
232234
])
233235

234-
self.blocks6 = nn.Sequential(* [
236+
self.blocks6 = nn.Sequential(*[
235237
DepthwiseSeparable(
236238
num_channels=make_divisible(in_c * scale),
237239
num_filters=make_divisible(out_c * scale),

ppocr/modeling/backbones/rec_hgnet.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
import paddle.nn as nn
1717
import paddle.nn.functional as F
1818
from paddle.nn.initializer import KaimingNormal, Constant
19-
from paddle.nn import Conv2D, BatchNorm2D, ReLU, AdaptiveAvgPool2D, MaxPool2D
19+
from paddle.nn import Conv2D, BatchNorm2D, ReLU, MaxPool2D
2020
from paddle.regularizer import L2Decay
2121
from paddle import ParamAttr
2222

23+
from .custom_devices_layers import AdaptiveAvgPool2D
24+
2325
kaiming_normal_ = KaimingNormal()
2426
zeros_ = Constant(value=0.)
2527
ones_ = Constant(value=1.)
@@ -204,13 +206,13 @@ def __init__(
204206

205207
# stem
206208
stem_channels.insert(0, in_channels)
207-
self.stem = nn.Sequential(* [
209+
self.stem = nn.Sequential(*[
208210
ConvBNAct(
209211
in_channels=stem_channels[i],
210212
out_channels=stem_channels[i + 1],
211213
kernel_size=3,
212-
stride=2 if i == 0 else 1) for i in range(
213-
len(stem_channels) - 1)
214+
stride=2 if i == 0 else 1)
215+
for i in range(len(stem_channels) - 1)
214216
])
215217

216218
if self.det:

ppocr/modeling/backbones/rec_lcnetv3.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import paddle.nn.functional as F
2222
from paddle import ParamAttr
2323
from paddle.nn.initializer import Constant, KaimingNormal
24-
from paddle.nn import AdaptiveAvgPool2D, BatchNorm2D, Conv2D, Dropout, Hardsigmoid, Hardswish, Identity, Linear, ReLU
24+
from paddle.nn import BatchNorm2D, Conv2D, Dropout, Hardsigmoid, Hardswish, Identity, Linear, ReLU
2525
from paddle.regularizer import L2Decay
2626

27+
from .custom_devices_layers import AdaptiveAvgPool2D
28+
2729
NET_CONFIG_det = {
2830
"blocks2":
2931
#k, in_c, out_c, s, use_se

0 commit comments

Comments
 (0)